-
Notifications
You must be signed in to change notification settings - Fork 0
/
tooltip.js
87 lines (80 loc) · 2.91 KB
/
tooltip.js
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
(() => {
const { WidgetType, Decoration, ViewPlugin } = require("@codemirror/view");
const { syntaxTree } = require("@codemirror/language");
class CheckboxWidget extends WidgetType {
constructor(checked) {
super();
this.checked = checked;
}
eq(other) { return other.checked == this.checked }
toDOM() {
let wrap = document.createElement("span")
wrap.setAttribute("aria-hidden", "true")
wrap.className = "cm-boolean-toggle"
let box = wrap.appendChild(document.createElement("input"))
box.type = "checkbox"
box.className = 'form-check-input'
box.style.margin = '.125rem';
box.style.setProperty('--thiscolor', '#a497dc')
box.checked = this.checked
return wrap
}
ignoreEvent() { return false }
}
function checkboxes(view) {
let widgets = []
for (let { from, to } of view.visibleRanges) {
syntaxTree(view.state).iterate({
enter: (node) => {
//console.log(node.name,node)
if (node.name == "BooleanLiteral") {
let isTrue = view.state.doc.sliceString(node.from, node.to) == "true"
//console.log(isTrue, "vvv")
let deco = Decoration.widget({
widget: new CheckboxWidget(isTrue),
side: 1
})
widgets.push(deco.range(node.from))
}
},
from,
to
})
}
return Decoration.set(widgets)
}
const checkboxPlugin = ViewPlugin.fromClass(class {
constructor(view) {
this.decorations = checkboxes(view);
}
update(update) {
if (update.docChanged || update.viewportChanged)
this.decorations = checkboxes(update.view);
}
}, {
decorations: v => v.decorations,
eventHandlers: {
mousedown: (e, view) => {
let target = e.target;
if (target.nodeName == "INPUT" &&
target.parentElement.classList.contains("cm-boolean-toggle"))
return toggleBoolean(view, view.posAtDOM(target));
}
}
});
function toggleBoolean(view, pos) {
let before = view.state.doc.sliceString(Math.max(0, pos), pos + 5)
let change
//console.log(before)
if (before == "false") {
change = { from: pos, to: pos + 5, insert: "true" }
}
else if (before.startsWith("true"))
change = { from: pos, to: pos + 4, insert: "false" }
else
return false
view.dispatch({ changes: change })
return true
}
require.addMod("@Fe2/tooltip", { checkboxPlugin });
})();