MCPcopy Create free account
hub / github.com/71/logseq-snippets / constructor

Method constructor

script-block.js:80–126  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

78
79class ScriptBlock extends HTMLElement {
80 constructor() {
81 super();
82
83 const stateJson = this.getAttribute("state") ?? "{}",
84 state = JSON.parse(stateJson),
85 pattern = this.getAttribute("pattern") ?? "(<script-block state=').+('>)",
86 patternRe = new RegExp(pattern);
87 const saveState = (x = state) => {
88 const json = JSON.stringify(x),
89 block = this.closest("[blockid]"),
90 blockId = block.getAttribute("blockid");
91
92 requestAnimationFrame(() => {
93 const textarea = document.getElementById("edit-block-1-" + blockId),
94 escaped = json.replace(/&/g, "&amp;").replace(/'/g, "&apos;")
95 .replace(/</g, "&lt;").replace(/>/g, "&gt;")
96 .replace(/\r\n/g, "&#13;").replace(/[\r\n]/g, "&#13;");
97
98 textarea.value = textarea.value.replace(
99 pattern,
100 (_, before, after) => before + escaped + after,
101 );
102 setTimeout(() => {
103 textarea.dispatchEvent(new KeyboardEvent("keydown", { keyCode: 27 }));
104 }, 100);
105 });
106 block.click();
107 };
108
109 const body = this.innerHTML.slice(4, this.innerHTML.length - 3),
110 f = Function("save", "html", "svg", body),
111 content = f.call(state, saveState, html, svg),
112 shadow = this.attachShadow({mode: 'open'});
113
114 if (content instanceof Node) {
115 shadow.appendChild(content);
116 } else {
117 const wrapper = document.createElement("pre");
118 wrapper.innerText = JSON.stringify(content);
119 shadow.appendChild(wrapper);
120 }
121
122 this.addEventListener(
123 "click",
124 (e) => e.stopImmediatePropagation(),
125 );
126 }
127}
128
129class DefineScriptBlock extends HTMLElement {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected