generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
livePreview.ts
71 lines (60 loc) · 2.67 KB
/
livePreview.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { App, setIcon } from "obsidian";
import { Decoration, DecorationSet, EditorView, ViewPlugin, ViewUpdate, WidgetType } from "@codemirror/view";
import { RangeSetBuilder, Text } from "@codemirror/state";
import { syntaxTree } from "@codemirror/language";
import { tokenClassNodeProp } from "@codemirror/language";
import * as Status from "statusTypes";
import tippy from "tippy.js";
import { tooltips, hoverTooltip } from "@codemirror/view"
export const editorTaskTooltip = hoverTooltip((view, pos, side) => {
let { from, to, text } = view.state.doc.lineAt(pos)
const taskRegex = new RegExp(/^(?<prefix>>?(\s+)?)- \[(?<status>.*)\](?<taskContent>.*)/)
if (!taskRegex.test(text)) return null
let start = pos, end = to
while (start > from && taskRegex.test(text[start - from - 1])) start--
if (start == pos && side < 0 || end == pos && side > 0)
return null
return {
pos: from,
end,
above: false,
create(view) {
let dom = document.createElement("div")
dom.addClass("suggestion-container")
for (const status in Status.Status) {
const btn = createDiv()
btn.addClass("taskBtn")
setIcon(btn, Status.statusIcon[status as keyof typeof Status.Status])
btn.onclick = async (e) => {
//doesn't work for task in callout:
//when clicked, the callout is rendered to preview in editorview and this view is destroyed
console.log("from liveView")
//@ts-ignore
const lineDOM = view.contentDOM.cmView.children.find(c => c.posAtStart === from)
const input = lineDOM.dom.querySelector("input")
const tR = text.match(taskRegex)
if (tR?.groups) {
const { prefix, taskContent } = tR.groups
if (input) {
input.setAttr('data-task', status);
(input as HTMLInputElement).checked = true;
}
view.dispatch({
changes: {
from: from,
to: to,
insert: `${prefix}- [${status}]${taskContent}`
}
})
}
}
dom.appendChild(btn)
}
return { dom, offset: { x: 0, y: 100 } }
}
}
}, {
hoverTime: 10,
hideOn: (tr, tooltip) => { return false }
})
export const editorTooltips = tooltips({ parent: document.body })