-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscript-block.js
142 lines (113 loc) · 4.04 KB
/
script-block.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { html, svg } from "https://cdn.skypack.dev/htl";
const elements = new Map();
export function defineElement(name, render) {
let elementClass = elements.get(name);
if (elementClass === undefined) {
elementClass = class extends HTMLElement {
constructor() {
super();
this._shadow = this.attachShadow({ mode: "open" });
this.render();
this.addEventListener(
"click",
(e) => e.stopImmediatePropagation(),
);
}
render() {}
};
elements.set(name, elementClass);
customElements.define(name, elementClass);
}
elementClass.prototype.render = function() {
this._shadow.innerHTML = "";
const htmlBefore = this.outerHTML,
state = { html, svg };
for (const attr of this.attributes) {
let value = attr.value;
if (value[0] === "{")
value = JSON.parse(value);
state[attr.name] = value;
}
state.save = (x = state) => {
for (const name in x) {
let value = x[name];
if (typeof value === "object")
value = JSON.stringify(value);
this.setAttribute(name, value);
}
const htmlAfter = this.outerHTML,
block = this.closest("[blockid]"),
blockId = block.getAttribute("blockid");
requestAnimationFrame(() => {
const textarea = document.getElementById("edit-block-1-" + blockId);
textarea.value = textarea.value.replace(htmlBefore, htmlAfter);
setTimeout(() => {
textarea.dispatchEvent(new KeyboardEvent("keydown", { keyCode: 27 }));
}, 100);
});
block.click();
};
let content = render(state);
if (!(content instanceof Node)) {
content = Object.assign(document.createElement("pre"), { innerText: content });
}
this._shadow.appendChild(content);
};
for (const existingElement of document.querySelectorAll(name)) {
existingElement.render();
}
}
class ScriptBlock extends HTMLElement {
constructor() {
super();
const stateJson = this.getAttribute("state") ?? "{}",
state = JSON.parse(stateJson),
pattern = this.getAttribute("pattern") ?? "(<script-block state=').+('>)",
patternRe = new RegExp(pattern);
const saveState = (x = state) => {
const json = JSON.stringify(x),
block = this.closest("[blockid]"),
blockId = block.getAttribute("blockid");
requestAnimationFrame(() => {
const textarea = document.getElementById("edit-block-1-" + blockId),
escaped = json.replace(/&/g, "&").replace(/'/g, "'")
.replace(/</g, "<").replace(/>/g, ">")
.replace(/\r\n/g, " ").replace(/[\r\n]/g, " ");
textarea.value = textarea.value.replace(
pattern,
(_, before, after) => before + escaped + after,
);
setTimeout(() => {
textarea.dispatchEvent(new KeyboardEvent("keydown", { keyCode: 27 }));
}, 100);
});
block.click();
};
const body = this.innerHTML.slice(4, this.innerHTML.length - 3),
f = Function("save", "html", "svg", body),
content = f.call(state, saveState, html, svg),
shadow = this.attachShadow({mode: 'open'});
if (content instanceof Node) {
shadow.appendChild(content);
} else {
const wrapper = document.createElement("pre");
wrapper.innerText = JSON.stringify(content);
shadow.appendChild(wrapper);
}
this.addEventListener(
"click",
(e) => e.stopImmediatePropagation(),
);
}
}
class DefineScriptBlock extends HTMLElement {
constructor() {
super();
const name = this.getAttribute("name"),
body = this.innerHTML.slice(4, this.innerHTML.length - 3),
render = new Function("save", "html", "svg", body);
defineElement(name, ({ save, html, svg, ...state }) => render.call(state, save, html, svg));
}
}
customElements.define("script-block", ScriptBlock);
customElements.define("define-script-block", DefineScriptBlock);