()
| 78 | |
| 79 | class 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, "&").replace(/'/g, "'") |
| 95 | .replace(/</g, "<").replace(/>/g, ">") |
| 96 | .replace(/\r\n/g, " ").replace(/[\r\n]/g, " "); |
| 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 | |
| 129 | class DefineScriptBlock extends HTMLElement { |
nothing calls this directly
no outgoing calls
no test coverage detected