forked from hackclub/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
editors.js
117 lines (98 loc) · 3.04 KB
/
editors.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { EditorView, WidgetType, Decoration } from "@codemirror/view";
import { StateField } from "@codemirror/state";
import { syntaxTree, foldService } from "@codemirror/language";
import { getTag } from "./util.js";
import { dispatch } from "../dispatch.js";
const pairs = [
[ "bitmap", "bitmap" ],
[ "tune", "sequencer" ],
[ "map", "map" ]
]
export class OpenButtonWidget extends WidgetType {
constructor(label, editorType, text, from, to) {
super();
this.label = label;
this.editorType = editorType;
this.text = text;
this.from = from;
this.to = to;
}
eq(other) { return other.text === this.text && other.from === this.from && other.to === this.to; }
ignoreEvent() { return false; }
toDOM() {
const container = document.createElement("span");
container.classList.add("cm-open-button");
const button = container.appendChild(document.createElement("button"));
button.textContent = this.label;
button.addEventListener("click", () => this.onClick());
if (this.editorType === "bitmap") container.appendChild(document.createElement("bitmap-preview")).setAttribute("text", this.text);
return container;
}
updateDOM(container) {
const oldButton = container.children[0];
const button = oldButton.cloneNode(true); // This'll remove all event listeners.
button.addEventListener("click", () => this.onClick());
container.replaceChild(button, oldButton);
if (this.editorType === "bitmap") {
container
.querySelector("bitmap-preview")
.setAttribute("text", this.text);
}
return true;
}
onClick() {
dispatch("SET_EDIT_RANGE", {
range: [this.from, this.to]
});
dispatch("SET_ASSET_EDITOR", {
type: this.editorType,
text: this.text,
});
}
}
function makeValue(state) {
const widgets = [];
const foldRanges = [];
const syntax = syntaxTree(state);
syntax.iterate({
enter(node) {
for (const [label, editorType] of pairs) {
const tag = getTag(label, node, syntax, state.doc);
if (!tag) continue;
const decoration = Decoration.replace({
widget: new OpenButtonWidget(label, editorType, tag.text, tag.textFrom, tag.textTo)
});
widgets.push(decoration.range(tag.nameFrom, tag.nameTo));
foldRanges.push({ from: tag.textFrom, to: tag.textTo });
break;
}
}
})
return {
decorations: Decoration.set(widgets),
foldRanges
};
}
export default StateField.define({
create(state) {
return makeValue(state);
},
update(value, transaction) {
if (transaction.docChanged) {
return makeValue(transaction.state);
} else {
return {
...value,
decorations: value.decorations.map(transaction.changes)
};
}
},
provide(field) {
return [
EditorView.decorations.from(field, value => value.decorations),
foldService.from(field, value => (_, lineStart, lineEnd) => (
value.foldRanges.find(range => range.from >= lineStart && range.from <= lineEnd) ?? null
))
];
}
});