|
| 1 | +import { SvelteComponent, init, safe_not_equal, element, append, text, insert, set_data, detach, space, empty, add_binding_callback, noop } from 'svelte/internal'; |
| 2 | +import { createEventDispatcher, onMount } from 'svelte'; |
| 3 | + |
| 4 | +/* src/CodeMirror.svelte generated by Svelte v3.5.1 */ |
| 5 | + |
| 6 | +function add_css() { |
| 7 | + var style = element("style"); |
| 8 | + style.id = 'svelte-1jpkv2x-style'; |
| 9 | + style.textContent = "textarea.svelte-1jpkv2x{visibility:hidden}pre.svelte-1jpkv2x{position:absolute;width:100%;height:100%;top:0;left:0;border:none;padding:4px 4px 4px 60px;resize:none;font-family:var(--font-mono);font-size:13px;line-height:1.7;user-select:none;pointer-events:none;color:#ccc;tab-size:2;-moz-tab-size:2}"; |
| 10 | + append(document.head, style); |
| 11 | +} |
| 12 | + |
| 13 | +// (225:0) {#if !CodeMirror} |
| 14 | +function create_if_block(ctx) { |
| 15 | + var pre, t; |
| 16 | + |
| 17 | + return { |
| 18 | + c() { |
| 19 | + pre = element("pre"); |
| 20 | + t = text(ctx.code); |
| 21 | + pre.className = "svelte-1jpkv2x"; |
| 22 | + }, |
| 23 | + |
| 24 | + m(target, anchor) { |
| 25 | + insert(target, pre, anchor); |
| 26 | + append(pre, t); |
| 27 | + }, |
| 28 | + |
| 29 | + p(changed, ctx) { |
| 30 | + if (changed.code) { |
| 31 | + set_data(t, ctx.code); |
| 32 | + } |
| 33 | + }, |
| 34 | + |
| 35 | + d(detaching) { |
| 36 | + if (detaching) { |
| 37 | + detach(pre); |
| 38 | + } |
| 39 | + } |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +function create_fragment(ctx) { |
| 44 | + var textarea, t, if_block_anchor; |
| 45 | + |
| 46 | + var if_block = (!ctx.CodeMirror) && create_if_block(ctx); |
| 47 | + |
| 48 | + return { |
| 49 | + c() { |
| 50 | + textarea = element("textarea"); |
| 51 | + t = space(); |
| 52 | + if (if_block) if_block.c(); |
| 53 | + if_block_anchor = empty(); |
| 54 | + textarea.tabIndex = "0"; |
| 55 | + textarea.readOnly = true; |
| 56 | + textarea.value = ctx.code; |
| 57 | + textarea.className = "svelte-1jpkv2x"; |
| 58 | + }, |
| 59 | + |
| 60 | + m(target, anchor) { |
| 61 | + insert(target, textarea, anchor); |
| 62 | + add_binding_callback(() => ctx.textarea_binding(textarea, null)); |
| 63 | + insert(target, t, anchor); |
| 64 | + if (if_block) if_block.m(target, anchor); |
| 65 | + insert(target, if_block_anchor, anchor); |
| 66 | + }, |
| 67 | + |
| 68 | + p(changed, ctx) { |
| 69 | + if (changed.items) { |
| 70 | + ctx.textarea_binding(null, textarea); |
| 71 | + ctx.textarea_binding(textarea, null); |
| 72 | + } |
| 73 | + |
| 74 | + if (changed.code) { |
| 75 | + textarea.value = ctx.code; |
| 76 | + } |
| 77 | + |
| 78 | + if (!ctx.CodeMirror) { |
| 79 | + if (if_block) { |
| 80 | + if_block.p(changed, ctx); |
| 81 | + } else { |
| 82 | + if_block = create_if_block(ctx); |
| 83 | + if_block.c(); |
| 84 | + if_block.m(if_block_anchor.parentNode, if_block_anchor); |
| 85 | + } |
| 86 | + } else if (if_block) { |
| 87 | + if_block.d(1); |
| 88 | + if_block = null; |
| 89 | + } |
| 90 | + }, |
| 91 | + |
| 92 | + i: noop, |
| 93 | + o: noop, |
| 94 | + |
| 95 | + d(detaching) { |
| 96 | + if (detaching) { |
| 97 | + detach(textarea); |
| 98 | + } |
| 99 | + |
| 100 | + ctx.textarea_binding(null, textarea); |
| 101 | + |
| 102 | + if (detaching) { |
| 103 | + detach(t); |
| 104 | + } |
| 105 | + |
| 106 | + if (if_block) if_block.d(detaching); |
| 107 | + |
| 108 | + if (detaching) { |
| 109 | + detach(if_block_anchor); |
| 110 | + } |
| 111 | + } |
| 112 | + }; |
| 113 | +} |
| 114 | + |
| 115 | +const is_browser = typeof window !== "undefined"; |
| 116 | + |
| 117 | +let codemirror_promise; |
| 118 | +let _CodeMirror; |
| 119 | + |
| 120 | +if (is_browser) { |
| 121 | + codemirror_promise = import('codemirror'); |
| 122 | + |
| 123 | + codemirror_promise.then(mod => { |
| 124 | + _CodeMirror = mod.default; |
| 125 | + }); |
| 126 | +} |
| 127 | + |
| 128 | +function sleep(ms) { |
| 129 | + return new Promise(fulfil => setTimeout(fulfil, ms)); |
| 130 | +} |
| 131 | + |
| 132 | +function instance($$self, $$props, $$invalidate) { |
| 133 | + const dispatch = createEventDispatcher(); |
| 134 | + |
| 135 | + let { code = "", readonly = false, errorLoc = null, flex = false, lineNumbers = true, tab = true } = $$props; |
| 136 | + |
| 137 | + let w; |
| 138 | + let h; |
| 139 | + let mode; |
| 140 | + |
| 141 | + // We have to expose set and update methods, rather |
| 142 | + // than making this state-driven through props, |
| 143 | + // because it's difficult to update an editor |
| 144 | + // without resetting scroll otherwise |
| 145 | + async function set(new_code, new_mode) { |
| 146 | + if (new_mode !== mode) { |
| 147 | + await createEditor((mode = new_mode)); } |
| 148 | + |
| 149 | + $$invalidate('code', code = new_code); |
| 150 | + updating_externally = true; |
| 151 | + if (editor) editor.setValue(code); |
| 152 | + updating_externally = false; |
| 153 | + } |
| 154 | + |
| 155 | + function update(new_code) { |
| 156 | + $$invalidate('code', code = new_code); |
| 157 | + |
| 158 | + if (editor) { |
| 159 | + const { left, top } = editor.getScrollInfo(); |
| 160 | + editor.setValue((code = new_code)); $$invalidate('code', code); |
| 161 | + editor.scrollTo(left, top); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + function resize() { |
| 166 | + editor.refresh(); |
| 167 | + } |
| 168 | + |
| 169 | + function focus() { |
| 170 | + editor.focus(); |
| 171 | + } |
| 172 | + |
| 173 | + const modes = { |
| 174 | + js: { |
| 175 | + name: "javascript", |
| 176 | + json: false |
| 177 | + }, |
| 178 | + json: { |
| 179 | + name: "javascript", |
| 180 | + json: true |
| 181 | + }, |
| 182 | + svelte: { |
| 183 | + name: "handlebars", |
| 184 | + base: "text/html" |
| 185 | + } |
| 186 | + }; |
| 187 | + |
| 188 | + const refs = {}; |
| 189 | + let editor; |
| 190 | + let updating_externally = false; |
| 191 | + let marker; |
| 192 | + let error_line; |
| 193 | + let destroyed = false; |
| 194 | + let CodeMirror; |
| 195 | + |
| 196 | + let previous_error_line; |
| 197 | + |
| 198 | + onMount(() => { |
| 199 | + if (_CodeMirror) { |
| 200 | + $$invalidate('CodeMirror', CodeMirror = _CodeMirror); |
| 201 | + createEditor(mode || "svelte").then(() => { |
| 202 | + if (editor) editor.setValue(code || ""); |
| 203 | + }); |
| 204 | + } else { |
| 205 | + codemirror_promise.then(async mod => { |
| 206 | + $$invalidate('CodeMirror', CodeMirror = mod.default); |
| 207 | + await createEditor(mode || "svelte"); |
| 208 | + if (editor) editor.setValue(code || ""); |
| 209 | + }); |
| 210 | + } |
| 211 | + |
| 212 | + return () => { |
| 213 | + destroyed = true; |
| 214 | + if (editor) editor.toTextArea(); |
| 215 | + }; |
| 216 | + }); |
| 217 | + |
| 218 | + let first = true; |
| 219 | + |
| 220 | + async function createEditor(mode) { |
| 221 | + if (destroyed || !CodeMirror) return; |
| 222 | + |
| 223 | + if (editor) editor.toTextArea(); |
| 224 | + |
| 225 | + const opts = { |
| 226 | + lineNumbers, |
| 227 | + lineWrapping: true, |
| 228 | + indentWithTabs: true, |
| 229 | + indentUnit: 2, |
| 230 | + tabSize: 2, |
| 231 | + value: "", |
| 232 | + mode: modes[mode] || { |
| 233 | + name: mode |
| 234 | + }, |
| 235 | + readOnly: readonly, |
| 236 | + autoCloseBrackets: true, |
| 237 | + autoCloseTags: true |
| 238 | + }; |
| 239 | + |
| 240 | + if (!tab) |
| 241 | + opts.extraKeys = { |
| 242 | + Tab: tab, |
| 243 | + "Shift-Tab": tab |
| 244 | + }; |
| 245 | + |
| 246 | + // Creating a text editor is a lot of work, so we yield |
| 247 | + // the main thread for a moment. This helps reduce jank |
| 248 | + if (first) await sleep(50); |
| 249 | + |
| 250 | + if (destroyed) return; |
| 251 | + |
| 252 | + $$invalidate('editor', editor = CodeMirror.fromTextArea(refs.editor, opts)); |
| 253 | + |
| 254 | + editor.on("change", instance => { |
| 255 | + if (!updating_externally) { |
| 256 | + const value = instance.getValue(); |
| 257 | + dispatch("change", { value }); |
| 258 | + } |
| 259 | + }); |
| 260 | + |
| 261 | + if (first) await sleep(50); |
| 262 | + editor.refresh(); |
| 263 | + |
| 264 | + first = false; |
| 265 | + } |
| 266 | + |
| 267 | + function textarea_binding($$node, check) { |
| 268 | + if ($$node || (!$$node && refs.editor === check)) refs.editor = $$node; |
| 269 | + $$invalidate('refs', refs); |
| 270 | + } |
| 271 | + |
| 272 | + $$self.$set = $$props => { |
| 273 | + if ('code' in $$props) $$invalidate('code', code = $$props.code); |
| 274 | + if ('readonly' in $$props) $$invalidate('readonly', readonly = $$props.readonly); |
| 275 | + if ('errorLoc' in $$props) $$invalidate('errorLoc', errorLoc = $$props.errorLoc); |
| 276 | + if ('flex' in $$props) $$invalidate('flex', flex = $$props.flex); |
| 277 | + if ('lineNumbers' in $$props) $$invalidate('lineNumbers', lineNumbers = $$props.lineNumbers); |
| 278 | + if ('tab' in $$props) $$invalidate('tab', tab = $$props.tab); |
| 279 | + }; |
| 280 | + |
| 281 | + $$self.$$.update = ($$dirty = { editor: 1, w: 1, h: 1, marker: 1, errorLoc: 1, previous_error_line: 1, error_line: 1 }) => { |
| 282 | + if ($$dirty.editor || $$dirty.w || $$dirty.h) { if (editor && w && h) { |
| 283 | + editor.refresh(); |
| 284 | + } } |
| 285 | + if ($$dirty.marker || $$dirty.errorLoc || $$dirty.editor) { { |
| 286 | + if (marker) marker.clear(); |
| 287 | + |
| 288 | + if (errorLoc) { |
| 289 | + const line = errorLoc.line - 1; |
| 290 | + const ch = errorLoc.column; |
| 291 | + |
| 292 | + $$invalidate('marker', marker = editor.markText( |
| 293 | + { line, ch }, |
| 294 | + { line, ch: ch + 1 }, |
| 295 | + { |
| 296 | + className: "error-loc" |
| 297 | + } |
| 298 | + )); |
| 299 | + |
| 300 | + $$invalidate('error_line', error_line = line); |
| 301 | + } else { |
| 302 | + $$invalidate('error_line', error_line = null); |
| 303 | + } |
| 304 | + } } |
| 305 | + if ($$dirty.editor || $$dirty.previous_error_line || $$dirty.error_line) { if (editor) { |
| 306 | + if (previous_error_line != null) { |
| 307 | + editor.removeLineClass(previous_error_line, "wrap", "error-line"); |
| 308 | + } |
| 309 | + |
| 310 | + if (error_line && error_line !== previous_error_line) { |
| 311 | + editor.addLineClass(error_line, "wrap", "error-line"); |
| 312 | + $$invalidate('previous_error_line', previous_error_line = error_line); |
| 313 | + } |
| 314 | + } } |
| 315 | + }; |
| 316 | + |
| 317 | + return { |
| 318 | + code, |
| 319 | + readonly, |
| 320 | + errorLoc, |
| 321 | + flex, |
| 322 | + lineNumbers, |
| 323 | + tab, |
| 324 | + set, |
| 325 | + update, |
| 326 | + resize, |
| 327 | + focus, |
| 328 | + refs, |
| 329 | + CodeMirror, |
| 330 | + textarea_binding |
| 331 | + }; |
| 332 | +} |
| 333 | + |
| 334 | +class CodeMirror_1 extends SvelteComponent { |
| 335 | + constructor(options) { |
| 336 | + super(); |
| 337 | + if (!document.getElementById("svelte-1jpkv2x-style")) add_css(); |
| 338 | + init(this, options, instance, create_fragment, safe_not_equal, ["code", "readonly", "errorLoc", "flex", "lineNumbers", "tab", "set", "update", "resize", "focus"]); |
| 339 | + } |
| 340 | + |
| 341 | + get set() { |
| 342 | + return this.$$.ctx.set; |
| 343 | + } |
| 344 | + |
| 345 | + get update() { |
| 346 | + return this.$$.ctx.update; |
| 347 | + } |
| 348 | + |
| 349 | + get resize() { |
| 350 | + return this.$$.ctx.resize; |
| 351 | + } |
| 352 | + |
| 353 | + get focus() { |
| 354 | + return this.$$.ctx.focus; |
| 355 | + } |
| 356 | +} |
| 357 | + |
| 358 | +export default CodeMirror_1; |
0 commit comments