svelte-highlight 7.23.2 → 7.24.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AnsiOutput.svelte +33 -6
- package/AnsiOutput.svelte.d.ts +8 -0
- package/CodeToolbar.svelte +144 -0
- package/CodeToolbar.svelte.d.ts +125 -0
- package/CodeWindow.svelte +19 -0
- package/CodeWindow.svelte.d.ts +10 -0
- package/CopyButton.svelte +107 -3
- package/CopyButton.svelte.d.ts +17 -1
- package/DiffFileList.svelte +523 -0
- package/DiffFileList.svelte.d.ts +94 -0
- package/DiffFileListItem.svelte +44 -0
- package/DiffMinimap.svelte +99 -0
- package/DiffMinimap.svelte.d.ts +17 -0
- package/DiffStats.svelte +43 -0
- package/DiffStats.svelte.d.ts +17 -0
- package/DiffView.svelte +1213 -0
- package/DiffView.svelte.d.ts +76 -0
- package/FileTabs.svelte +2 -5
- package/Highlight.svelte +158 -2
- package/Highlight.svelte.d.ts +50 -2
- package/HighlightAuto.svelte.d.ts +3 -2
- package/HighlightAutoLazy.svelte +112 -0
- package/HighlightAutoLazy.svelte.d.ts +66 -0
- package/HighlightDiff.svelte +162 -0
- package/HighlightDiff.svelte.d.ts +102 -0
- package/HighlightEditable.svelte +65 -756
- package/HighlightEditable.svelte.d.ts +15 -5
- package/HighlightStream.svelte +117 -218
- package/HighlightStream.svelte.d.ts +8 -0
- package/HighlightSvelte.svelte.d.ts +3 -2
- package/HighlightVirtual.svelte +41 -8
- package/HighlightVirtual.svelte.d.ts +8 -0
- package/LangTag.svelte +33 -4
- package/LangTag.svelte.d.ts +6 -2
- package/LineNumbers.svelte +25 -4
- package/LineNumbers.svelte.d.ts +8 -0
- package/MarkdownStream.svelte +1 -0
- package/README.md +735 -18
- package/ToolbarButton.svelte +90 -0
- package/ToolbarButton.svelte.d.ts +106 -0
- package/Typewriter.svelte +59 -10
- package/Typewriter.svelte.d.ts +7 -0
- package/WrapToggle.svelte +34 -0
- package/WrapToggle.svelte.d.ts +39 -0
- package/action.js +7 -3
- package/ansi.d.ts +1 -3
- package/auto-detect.d.ts +52 -0
- package/auto-detect.js +208 -0
- package/code-region.d.ts +7 -0
- package/code-region.js +20 -0
- package/css-highlights.js +115 -0
- package/css-walk.js +50 -0
- package/diff-controller.d.ts +120 -0
- package/diff-controller.js +721 -0
- package/diff-edits.d.ts +101 -0
- package/diff-edits.js +890 -0
- package/diff-html.d.ts +8 -0
- package/diff-html.js +60 -0
- package/diff-languages.d.ts +4 -0
- package/diff-languages.js +73 -0
- package/diff.d.ts +161 -0
- package/diff.js +1111 -0
- package/editable-commands.js +81 -0
- package/editable-css-paint.js +5 -3
- package/editable-dom-paint.js +1 -1
- package/editable-history.js +199 -0
- package/editable-line-dom.js +176 -1
- package/editable-painter.js +356 -0
- package/editable.d.ts +118 -0
- package/editable.js +513 -0
- package/engine.d.ts +1 -1
- package/fence.js +1 -1
- package/highlight-cascade.js +679 -0
- package/highlight-theme.js +6 -47
- package/incremental-tokenize.js +1 -1
- package/index.d.ts +39 -6
- package/index.js +30 -0
- package/languages/detect-index.d.ts +16 -0
- package/languages/detect-index.js +6 -0
- package/languages/loaders.d.ts +8 -0
- package/languages/loaders.js +370 -0
- package/package.json +50 -2
- package/preprocessor.js +20 -5
- package/search.js +2 -2
- package/stream-highlighter.js +198 -0
- package/stream.css +16 -0
- package/stream.d.ts +60 -0
- package/stream.js +283 -0
- package/style-registry.js +34 -3
- package/themes/_base-css.d.ts +1 -0
- package/themes/_base-css.js +1 -0
- package/typewriter-units.d.ts +3 -0
- package/typewriter-units.js +43 -0
- package/virtual-window.js +9 -4
package/HighlightEditable.svelte
CHANGED
|
@@ -33,635 +33,80 @@
|
|
|
33
33
|
export let engine = "dom";
|
|
34
34
|
|
|
35
35
|
/**
|
|
36
|
-
* Theme CSS or `ThemePalette`
|
|
37
|
-
*
|
|
36
|
+
* Theme CSS or `ThemePalette` that `"css-highlights"` resolves token colors
|
|
37
|
+
* from, as `Highlight` does. Colors only.
|
|
38
38
|
* @type {string | import("./theme.d.ts").ThemePalette | undefined}
|
|
39
39
|
*/
|
|
40
40
|
export let theme = undefined;
|
|
41
41
|
|
|
42
|
+
/**
|
|
43
|
+
* Accessible name of the editor (`aria-label`).
|
|
44
|
+
* @type {string}
|
|
45
|
+
*/
|
|
46
|
+
export let label = "Code editor";
|
|
47
|
+
|
|
42
48
|
import { createEventDispatcher, onMount } from "svelte";
|
|
43
|
-
import {
|
|
44
|
-
import { createDomLinePainter } from "./editable-dom-paint.js";
|
|
45
|
-
import {
|
|
46
|
-
createLineView,
|
|
47
|
-
editorNodeAtOffset,
|
|
48
|
-
lineStartOffset as lineStartIn,
|
|
49
|
-
nodeAtOffset,
|
|
50
|
-
renderLines as renderLineDom,
|
|
51
|
-
} from "./editable-line-dom.js";
|
|
52
|
-
import { toRanges } from "./engine.js";
|
|
53
|
-
import { instanceHighlightStyle } from "./highlight-theme.js";
|
|
54
|
-
import {
|
|
55
|
-
parseIncremental,
|
|
56
|
-
reparseIncremental,
|
|
57
|
-
} from "./incremental-tokenize.js";
|
|
58
|
-
import { ensureRegistered, registry } from "./registry.js";
|
|
59
|
-
import { diffText } from "./text-diff.js";
|
|
49
|
+
import { createEditor } from "./editable.js";
|
|
60
50
|
|
|
61
51
|
const dispatch = createEventDispatcher();
|
|
62
|
-
const TRAILING_NEWLINE = /\n$/;
|
|
63
52
|
const instanceId = nextInstanceId();
|
|
64
53
|
|
|
65
54
|
/** @type {HTMLElement} */
|
|
66
|
-
let
|
|
55
|
+
let element;
|
|
67
56
|
|
|
68
|
-
|
|
69
|
-
let
|
|
70
|
-
let mounted = false;
|
|
71
|
-
let restoringSelection = false;
|
|
72
|
-
|
|
73
|
-
// Set by Escape so the next Tab moves focus instead of indenting.
|
|
74
|
-
let tabTrapReleased = false;
|
|
75
|
-
|
|
76
|
-
const view = createLineView();
|
|
57
|
+
/** @type {ReturnType<typeof createEditor> | undefined} */
|
|
58
|
+
let editor;
|
|
77
59
|
|
|
78
60
|
// Tracks `code` so parent updates vs local edits can be distinguished.
|
|
79
61
|
let internalCode = code;
|
|
80
62
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
return incrementalParse.events;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
$: resolvedEngineValue =
|
|
94
|
-
engine === "css-highlights" &&
|
|
95
|
-
typeof CSS !== "undefined" &&
|
|
96
|
-
typeof CSS.highlights !== "undefined"
|
|
97
|
-
? "css-highlights"
|
|
98
|
-
: "dom";
|
|
99
|
-
|
|
100
|
-
let previousLanguageName;
|
|
101
|
-
let previousEngine;
|
|
102
|
-
$: {
|
|
103
|
-
ensureRegistered(language);
|
|
104
|
-
if (
|
|
105
|
-
mounted &&
|
|
106
|
-
(language.name !== previousLanguageName ||
|
|
107
|
-
resolvedEngineValue !== previousEngine)
|
|
108
|
-
) {
|
|
109
|
-
if (previousEngine === "css-highlights") clearCssHighlights();
|
|
110
|
-
repaintForLanguageChange();
|
|
111
|
-
}
|
|
112
|
-
previousLanguageName = language.name;
|
|
113
|
-
previousEngine = resolvedEngineValue;
|
|
114
|
-
}
|
|
115
|
-
$: indentUnit = " ".repeat(tabSize);
|
|
116
|
-
$: if (mounted && code !== internalCode) syncExternal();
|
|
117
|
-
$: cssHighlightStyle =
|
|
118
|
-
resolvedEngineValue === "css-highlights" && theme !== undefined
|
|
119
|
-
? instanceHighlightStyle(theme, instanceId)
|
|
120
|
-
: "";
|
|
121
|
-
|
|
122
|
-
function sumChildLengths(el, count) {
|
|
123
|
-
let total = 0;
|
|
124
|
-
for (let i = 0; i < count && i < el.childNodes.length; i++) {
|
|
125
|
-
total += el.childNodes[i].textContent.length;
|
|
126
|
-
}
|
|
127
|
-
return total;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// Character offset of a Selection boundary, derived from the line
|
|
131
|
-
// structure instead of serializing the document. Null when the DOM has
|
|
132
|
-
// drifted from `view`, so the caller falls back to cloneRange/toString.
|
|
133
|
-
function offsetOfBoundary(container, offsetInContainer) {
|
|
134
|
-
const expectedChildren =
|
|
135
|
-
view.lineEls.length === 0 ? 0 : view.lineEls.length * 2 - 1;
|
|
136
|
-
if (editor.childNodes.length !== expectedChildren) return null;
|
|
137
|
-
|
|
138
|
-
if (container === editor) {
|
|
139
|
-
if (view.lineEls.length === 0) return offsetInContainer === 0 ? 0 : null;
|
|
140
|
-
const lineIndex = offsetInContainer >> 1;
|
|
141
|
-
if (lineIndex >= view.lineEls.length) return null;
|
|
142
|
-
return offsetInContainer % 2 === 0
|
|
143
|
-
? lineStartOffset(lineIndex)
|
|
144
|
-
: lineStartOffset(lineIndex) + view.lineLengths[lineIndex];
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
let node = container;
|
|
148
|
-
while (node.parentNode !== editor) {
|
|
149
|
-
node = node.parentNode;
|
|
150
|
-
if (!node) return null;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
let childIndex = 0;
|
|
154
|
-
for (let sib = node.previousSibling; sib; sib = sib.previousSibling) {
|
|
155
|
-
childIndex++;
|
|
156
|
-
}
|
|
157
|
-
const lineIndex = childIndex >> 1;
|
|
158
|
-
|
|
159
|
-
if (childIndex % 2 === 1) {
|
|
160
|
-
// A "\n" separator has no children, so it must be `container` itself.
|
|
161
|
-
if (container !== node || lineIndex >= view.lineLengths.length)
|
|
162
|
-
return null;
|
|
163
|
-
return (
|
|
164
|
-
lineStartOffset(lineIndex) +
|
|
165
|
-
view.lineLengths[lineIndex] +
|
|
166
|
-
offsetInContainer
|
|
167
|
-
);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
const span = node;
|
|
171
|
-
if (lineIndex >= view.lineEls.length || view.lineEls[lineIndex] !== span)
|
|
172
|
-
return null;
|
|
173
|
-
|
|
174
|
-
let charOffset =
|
|
175
|
-
container.nodeType === Node.TEXT_NODE
|
|
176
|
-
? offsetInContainer
|
|
177
|
-
: sumChildLengths(container, offsetInContainer);
|
|
178
|
-
let n = container;
|
|
179
|
-
while (n !== span) {
|
|
180
|
-
for (let sib = n.previousSibling; sib; sib = sib.previousSibling) {
|
|
181
|
-
charOffset += sib.textContent.length;
|
|
182
|
-
}
|
|
183
|
-
n = n.parentNode;
|
|
184
|
-
if (!n) return null;
|
|
185
|
-
}
|
|
186
|
-
return lineStartOffset(lineIndex) + charOffset;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
function getSelectionRange() {
|
|
190
|
-
const selection = window.getSelection();
|
|
191
|
-
if (!selection || selection.rangeCount === 0) return null;
|
|
192
|
-
const range = selection.getRangeAt(0);
|
|
193
|
-
if (!editor.contains(range.commonAncestorContainer)) return null;
|
|
194
|
-
const start = offsetOfBoundary(range.startContainer, range.startOffset);
|
|
195
|
-
const end = offsetOfBoundary(range.endContainer, range.endOffset);
|
|
196
|
-
if (start != null && end != null) return { start, end };
|
|
197
|
-
const pre = range.cloneRange();
|
|
198
|
-
pre.selectNodeContents(editor);
|
|
199
|
-
pre.setEnd(range.startContainer, range.startOffset);
|
|
200
|
-
const fallbackStart = pre.toString().length;
|
|
201
|
-
return {
|
|
202
|
-
start: fallbackStart,
|
|
203
|
-
end: fallbackStart + range.toString().length,
|
|
204
|
-
};
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
function getCaretOffset() {
|
|
208
|
-
const sel = getSelectionRange();
|
|
209
|
-
return sel ? sel.end : null;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
// Firefox: a boundary at a text node's edge breaks its native undo after
|
|
213
|
-
// a repaint, so anchor edges via setStartBefore/After. A boundary at a
|
|
214
|
-
// separator's end goes inside the next line's <span>, else Firefox types
|
|
215
|
-
// into a stray text node (forcing a full rebuild).
|
|
216
|
-
function setRangeBoundary(range, side, node, offset) {
|
|
217
|
-
const nextLine = node.nextSibling;
|
|
218
|
-
if (
|
|
219
|
-
node.parentNode === editor &&
|
|
220
|
-
offset === node.textContent.length &&
|
|
221
|
-
nextLine?.nodeType === Node.ELEMENT_NODE
|
|
222
|
-
) {
|
|
223
|
-
range[side === "Start" ? "setStart" : "setEnd"](nextLine, 0);
|
|
224
|
-
} else if (offset === 0) range[`set${side}Before`](node);
|
|
225
|
-
else if (offset === node.textContent.length) range[`set${side}After`](node);
|
|
226
|
-
else range[side === "Start" ? "setStart" : "setEnd"](node, offset);
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
function setSelection(start, end, root = editor) {
|
|
230
|
-
const locate = (offset) =>
|
|
231
|
-
root === editor
|
|
232
|
-
? editorNodeAtOffset(editor, view, offset)
|
|
233
|
-
: nodeAtOffset(root, offset);
|
|
234
|
-
const from = locate(start);
|
|
235
|
-
if (!from) return;
|
|
236
|
-
const range = document.createRange();
|
|
237
|
-
setRangeBoundary(range, "Start", from.node, from.offset);
|
|
238
|
-
const to = end != null && end !== start ? locate(end) : null;
|
|
239
|
-
if (to) setRangeBoundary(range, "End", to.node, to.offset);
|
|
240
|
-
else range.collapse(true);
|
|
241
|
-
const selection = window.getSelection();
|
|
242
|
-
selection.removeAllRanges();
|
|
243
|
-
selection.addRange(range);
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
function lineStartOffset(index) {
|
|
247
|
-
return lineStartIn(view, index);
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
const setHtml = (el, line) => {
|
|
251
|
-
el.innerHTML = line;
|
|
252
|
-
};
|
|
253
|
-
const setText = (el, line) => {
|
|
254
|
-
el.textContent = line;
|
|
255
|
-
};
|
|
256
|
-
|
|
257
|
-
function renderLines(lines, setContent) {
|
|
258
|
-
return renderLineDom(editor, view, lines, setContent, lineHooks);
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
const lineHooks = {
|
|
262
|
-
onReset: () => clearCssHighlights(),
|
|
263
|
-
onSplice: (index, removed, inserted) => {
|
|
264
|
-
if (index >= lineHighlightRanges.length) return;
|
|
265
|
-
for (let i = index; i < index + removed; i++) clearLineHighlights(i);
|
|
266
|
-
lineHighlightRanges = lineHighlightRanges.slice(0, index).concat(
|
|
267
|
-
Array.from({ length: inserted }, () => []),
|
|
268
|
-
lineHighlightRanges.slice(index + removed),
|
|
269
|
-
);
|
|
270
|
-
},
|
|
271
|
-
};
|
|
272
|
-
|
|
273
|
-
/** @type {Map<string, InstanceType<typeof Highlight>>} */
|
|
274
|
-
let cssHighlights = new Map();
|
|
275
|
-
/** @type {{ scope: string; range: Range }[][]} */
|
|
276
|
-
let lineHighlightRanges = [];
|
|
277
|
-
/** @type {import("./engine.d.ts").ScopeEvent[] | undefined} */
|
|
278
|
-
let cssPaintedEvents;
|
|
279
|
-
|
|
280
|
-
function cssHighlightFor(scope) {
|
|
281
|
-
let highlight = cssHighlights.get(scope);
|
|
282
|
-
if (!highlight) {
|
|
283
|
-
highlight = new Highlight();
|
|
284
|
-
cssHighlights.set(scope, highlight);
|
|
285
|
-
CSS.highlights.set(`hljs-${instanceId}-${scope}`, highlight);
|
|
286
|
-
}
|
|
287
|
-
return highlight;
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
function clearLineHighlights(index) {
|
|
291
|
-
const previous = lineHighlightRanges[index];
|
|
292
|
-
if (!previous) return;
|
|
293
|
-
for (const { scope, range } of previous) {
|
|
294
|
-
cssHighlights.get(scope)?.delete(range);
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
function clearCssHighlights() {
|
|
299
|
-
for (const scope of cssHighlights.keys()) {
|
|
300
|
-
CSS.highlights.delete(`hljs-${instanceId}-${scope}`);
|
|
301
|
-
}
|
|
302
|
-
cssHighlights = new Map();
|
|
303
|
-
lineHighlightRanges = [];
|
|
304
|
-
cssPaintedEvents = undefined;
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
// Re-registers ranges for lines from `first` (at offset `base`) through the
|
|
308
|
-
// one ending at `end`; `tokenRanges` are relative to `base`. They're sorted
|
|
309
|
-
// and disjoint, so `tokenIndex` only advances: O(lines + tokens).
|
|
310
|
-
function paintLineHighlights(first, base, end, tokenRanges) {
|
|
311
|
-
let tokenIndex = 0;
|
|
312
|
-
let lineStart = 0;
|
|
313
|
-
for (
|
|
314
|
-
let i = first;
|
|
315
|
-
i < view.lineEls.length && base + lineStart <= end;
|
|
316
|
-
i++
|
|
317
|
-
) {
|
|
318
|
-
clearLineHighlights(i);
|
|
319
|
-
const lineEnd = lineStart + view.lineLengths[i];
|
|
320
|
-
const textNode = view.lineEls[i].firstChild;
|
|
321
|
-
const next = [];
|
|
322
|
-
if (textNode) {
|
|
323
|
-
while (
|
|
324
|
-
tokenIndex < tokenRanges.length &&
|
|
325
|
-
tokenRanges[tokenIndex].end <= lineStart
|
|
326
|
-
) {
|
|
327
|
-
tokenIndex++;
|
|
328
|
-
}
|
|
329
|
-
for (
|
|
330
|
-
let j = tokenIndex;
|
|
331
|
-
j < tokenRanges.length && tokenRanges[j].start < lineEnd;
|
|
332
|
-
j++
|
|
333
|
-
) {
|
|
334
|
-
const token = tokenRanges[j];
|
|
335
|
-
if (token.end <= lineStart) continue;
|
|
336
|
-
const start = Math.max(token.start, lineStart) - lineStart;
|
|
337
|
-
const end = Math.min(token.end, lineEnd) - lineStart;
|
|
338
|
-
if (start === end) continue;
|
|
339
|
-
const range = new Range();
|
|
340
|
-
range.setStart(textNode, start);
|
|
341
|
-
range.setEnd(textNode, end);
|
|
342
|
-
cssHighlightFor(token.scope).add(range);
|
|
343
|
-
next.push({ scope: token.scope, range });
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
lineHighlightRanges[i] = next;
|
|
347
|
-
lineStart = lineEnd + 1; // +1 for the "\n" separator
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
function paintCssHighlights() {
|
|
352
|
-
const changedIndex = renderLines(code.split("\n"), setText);
|
|
353
|
-
const previousEvents = cssPaintedEvents;
|
|
354
|
-
const events = getEvents();
|
|
355
|
-
cssPaintedEvents = events;
|
|
356
|
-
|
|
357
|
-
if (changedIndex == null) {
|
|
358
|
-
paintLineHighlights(0, 0, code.length, toRanges(events));
|
|
359
|
-
} else {
|
|
360
|
-
// One line's text changed, but its tokens can affect later lines
|
|
361
|
-
// (typing `/*`): repaint through where the re-parse converged.
|
|
362
|
-
const base = lineStartOffset(changedIndex);
|
|
363
|
-
const end = retokenizedEnd(
|
|
364
|
-
code,
|
|
365
|
-
events,
|
|
366
|
-
previousEvents,
|
|
367
|
-
incrementalParse?.reuse,
|
|
368
|
-
base,
|
|
369
|
-
);
|
|
370
|
-
paintLineHighlights(
|
|
371
|
-
changedIndex,
|
|
372
|
-
base,
|
|
373
|
-
end,
|
|
374
|
-
lineTokenRanges(events, base, end),
|
|
375
|
-
);
|
|
376
|
-
}
|
|
377
|
-
return changedIndex;
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
function paint() {
|
|
381
|
-
if (resolvedEngineValue === "css-highlights") return paintCssHighlights();
|
|
382
|
-
const events = getEvents();
|
|
383
|
-
return renderLines(
|
|
384
|
-
domLinePainter.paint(
|
|
385
|
-
events,
|
|
386
|
-
code,
|
|
387
|
-
language.name,
|
|
388
|
-
incrementalParse?.reuse,
|
|
389
|
-
),
|
|
390
|
-
setHtml,
|
|
391
|
-
);
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
function renderAt(start, end) {
|
|
395
|
-
const changedIndex = paint();
|
|
396
|
-
if (start == null) return;
|
|
397
|
-
restoringSelection = true;
|
|
398
|
-
if (changedIndex != null) {
|
|
399
|
-
const lineStart = lineStartOffset(changedIndex);
|
|
400
|
-
const lineEnd = lineStart + view.lineLengths[changedIndex];
|
|
401
|
-
const inLine = (offset) =>
|
|
402
|
-
offset == null || (offset >= lineStart && offset <= lineEnd);
|
|
403
|
-
if (start >= lineStart && start <= lineEnd && inLine(end)) {
|
|
404
|
-
setSelection(
|
|
405
|
-
start - lineStart,
|
|
406
|
-
end == null ? end : end - lineStart,
|
|
407
|
-
view.lineEls[changedIndex],
|
|
408
|
-
);
|
|
409
|
-
restoringSelection = false;
|
|
410
|
-
return;
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
setSelection(start, end);
|
|
414
|
-
restoringSelection = false;
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
function repaintForLanguageChange() {
|
|
418
|
-
incrementalParse = undefined;
|
|
419
|
-
domLinePainter.reset();
|
|
420
|
-
const caret = document.activeElement === editor ? getCaretOffset() : null;
|
|
421
|
-
if (caret == null) paint();
|
|
422
|
-
else renderAt(caret, caret);
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
function syncCaretToHistory() {
|
|
426
|
-
if (!editor.contains(document.activeElement)) return;
|
|
427
|
-
if (restoringSelection || composing || undoStack.length === 0) return;
|
|
428
|
-
const sel = getSelectionRange();
|
|
429
|
-
if (!sel) return;
|
|
430
|
-
const top = undoStack[undoStack.length - 1];
|
|
431
|
-
if (top.resultLength !== code.length) return;
|
|
432
|
-
undoStack[undoStack.length - 1] = {
|
|
433
|
-
...top,
|
|
434
|
-
selectionAfter: { start: sel.start, end: sel.end },
|
|
435
|
-
};
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
/**
|
|
439
|
-
* @typedef {{ start: number; end: number }} Selection
|
|
440
|
-
* @typedef {{
|
|
441
|
-
* start: number;
|
|
442
|
-
* removed: string;
|
|
443
|
-
* inserted: string;
|
|
444
|
-
* resultLength: number;
|
|
445
|
-
* selectionBefore: Selection;
|
|
446
|
-
* selectionAfter: Selection;
|
|
447
|
-
* }} HistoryEntry
|
|
448
|
-
*/
|
|
449
|
-
|
|
450
|
-
/** @type {HistoryEntry[]} */
|
|
451
|
-
let undoStack = [];
|
|
452
|
-
/** @type {HistoryEntry[]} */
|
|
453
|
-
let redoStack = [];
|
|
454
|
-
let lastRecord = 0;
|
|
455
|
-
|
|
456
|
-
function emitHistory() {
|
|
457
|
-
const future = redoStack.slice().reverse();
|
|
458
|
-
dispatch("history", {
|
|
459
|
-
entries: [...undoStack, ...future].map((snap) => ({
|
|
460
|
-
size: snap.resultLength,
|
|
461
|
-
})),
|
|
462
|
-
index: undoStack.length - 1,
|
|
463
|
-
canUndo: canUndo(),
|
|
464
|
-
canRedo: canRedo(),
|
|
465
|
-
});
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
/** @returns {HistoryEntry} */
|
|
469
|
-
function makeEntry(before, after, selectionBefore, selectionAfter) {
|
|
470
|
-
const { start, removed, inserted } = diffText(before, after);
|
|
471
|
-
return {
|
|
472
|
-
start,
|
|
473
|
-
removed,
|
|
474
|
-
inserted,
|
|
475
|
-
resultLength: after.length,
|
|
476
|
-
selectionBefore,
|
|
477
|
-
selectionAfter,
|
|
478
|
-
};
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
// The code an entry transformed *from*, given the code it produced.
|
|
482
|
-
function beforeCodeOf(entry, resultCode) {
|
|
483
|
-
return (
|
|
484
|
-
resultCode.slice(0, entry.start) +
|
|
485
|
-
entry.removed +
|
|
486
|
-
resultCode.slice(entry.start + entry.inserted.length)
|
|
487
|
-
);
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
// Coalesce typing within 250ms; Enter/Tab/paste pass coalesce=false.
|
|
491
|
-
function pushHistory(previousCode, newCode, selectionAfter, coalesce) {
|
|
492
|
-
redoStack = [];
|
|
493
|
-
const now = Date.now();
|
|
494
|
-
const top = undoStack[undoStack.length - 1];
|
|
495
|
-
|
|
496
|
-
if (coalesce && undoStack.length > 1 && now - lastRecord < 250) {
|
|
497
|
-
const burstStart = beforeCodeOf(top, previousCode);
|
|
498
|
-
undoStack[undoStack.length - 1] = makeEntry(
|
|
499
|
-
burstStart,
|
|
500
|
-
newCode,
|
|
501
|
-
top.selectionBefore,
|
|
502
|
-
selectionAfter,
|
|
503
|
-
);
|
|
504
|
-
} else {
|
|
505
|
-
undoStack.push(
|
|
506
|
-
makeEntry(previousCode, newCode, top.selectionAfter, selectionAfter),
|
|
507
|
-
);
|
|
508
|
-
const limit = Math.max(1, historyLimit);
|
|
509
|
-
if (undoStack.length > limit) {
|
|
510
|
-
undoStack.splice(0, undoStack.length - limit);
|
|
511
|
-
}
|
|
512
|
-
}
|
|
513
|
-
lastRecord = coalesce ? now : 0;
|
|
514
|
-
emitHistory();
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
function applyHistoryMove(newCode, selection) {
|
|
518
|
-
code = newCode;
|
|
519
|
-
internalCode = newCode;
|
|
520
|
-
dispatch("change", { code });
|
|
521
|
-
emitHistory();
|
|
522
|
-
renderAt(selection.start, selection.end);
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
function commit(value, start, end, coalesce) {
|
|
526
|
-
if (readonly) return;
|
|
527
|
-
const previousCode = code;
|
|
528
|
-
code = value;
|
|
529
|
-
internalCode = value;
|
|
530
|
-
dispatch("change", { code });
|
|
531
|
-
pushHistory(
|
|
532
|
-
previousCode,
|
|
533
|
-
value,
|
|
534
|
-
{ start, end: end == null ? start : end },
|
|
535
|
-
coalesce,
|
|
536
|
-
);
|
|
537
|
-
renderAt(start, end);
|
|
538
|
-
}
|
|
539
|
-
|
|
540
|
-
function syncExternal() {
|
|
541
|
-
const previousCode = internalCode;
|
|
63
|
+
$: editor?.update({
|
|
64
|
+
language,
|
|
65
|
+
tabSize,
|
|
66
|
+
historyLimit,
|
|
67
|
+
readonly,
|
|
68
|
+
engine,
|
|
69
|
+
theme,
|
|
70
|
+
});
|
|
71
|
+
$: if (editor && code !== internalCode) {
|
|
542
72
|
internalCode = code;
|
|
543
|
-
|
|
544
|
-
previousCode,
|
|
545
|
-
code,
|
|
546
|
-
{ start: code.length, end: code.length },
|
|
547
|
-
false,
|
|
548
|
-
);
|
|
549
|
-
paint();
|
|
550
|
-
}
|
|
551
|
-
|
|
552
|
-
function insertText(text) {
|
|
553
|
-
const sel = getSelectionRange();
|
|
554
|
-
if (!sel) return;
|
|
555
|
-
const value = code.slice(0, sel.start) + text + code.slice(sel.end);
|
|
556
|
-
commit(value, sel.start + text.length, undefined, false);
|
|
557
|
-
}
|
|
558
|
-
|
|
559
|
-
// Indent/dedent selected lines; blank lines skipped on indent.
|
|
560
|
-
function shiftIndent(outdent) {
|
|
561
|
-
const sel = getSelectionRange();
|
|
562
|
-
if (!sel) return;
|
|
563
|
-
const blockStart = code.lastIndexOf("\n", sel.start - 1) + 1;
|
|
564
|
-
const lines = code.slice(blockStart, sel.end).split("\n");
|
|
565
|
-
|
|
566
|
-
let firstDelta = 0;
|
|
567
|
-
let totalDelta = 0;
|
|
568
|
-
const next = lines
|
|
569
|
-
.map((line, index) => {
|
|
570
|
-
if (outdent) {
|
|
571
|
-
const leading = line.length - line.trimStart().length;
|
|
572
|
-
const remove = Math.min(tabSize, leading);
|
|
573
|
-
if (index === 0) firstDelta = -remove;
|
|
574
|
-
totalDelta -= remove;
|
|
575
|
-
return line.slice(remove);
|
|
576
|
-
}
|
|
577
|
-
if (line.length === 0) return line;
|
|
578
|
-
if (index === 0) firstDelta = tabSize;
|
|
579
|
-
totalDelta += tabSize;
|
|
580
|
-
return indentUnit + line;
|
|
581
|
-
})
|
|
582
|
-
.join("\n");
|
|
583
|
-
|
|
584
|
-
if (totalDelta === 0) return;
|
|
585
|
-
const value = code.slice(0, blockStart) + next + code.slice(sel.end);
|
|
586
|
-
const start = Math.max(blockStart, sel.start + firstDelta);
|
|
587
|
-
commit(value, start, sel.end + totalDelta, false);
|
|
588
|
-
}
|
|
589
|
-
|
|
590
|
-
function indentCommand() {
|
|
591
|
-
const sel = getSelectionRange();
|
|
592
|
-
if (sel && code.slice(sel.start, sel.end).includes("\n"))
|
|
593
|
-
shiftIndent(false);
|
|
594
|
-
else insertText(indentUnit);
|
|
595
|
-
}
|
|
596
|
-
|
|
597
|
-
function ensureFocus() {
|
|
598
|
-
if (document.activeElement === editor) return;
|
|
599
|
-
const prior = getSelectionRange();
|
|
600
|
-
editor.focus();
|
|
601
|
-
if (prior) setSelection(prior.start, prior.end);
|
|
602
|
-
else setSelection(code.length);
|
|
603
|
-
syncCaretToHistory();
|
|
73
|
+
editor.syncCode(code);
|
|
604
74
|
}
|
|
605
75
|
|
|
606
76
|
export function undo() {
|
|
607
|
-
|
|
608
|
-
const entry = undoStack.pop();
|
|
609
|
-
redoStack.push(entry);
|
|
610
|
-
applyHistoryMove(beforeCodeOf(entry, code), entry.selectionBefore);
|
|
77
|
+
editor?.undo();
|
|
611
78
|
}
|
|
612
79
|
|
|
613
80
|
export function redo() {
|
|
614
|
-
|
|
615
|
-
const entry = redoStack.pop();
|
|
616
|
-
const nextCode =
|
|
617
|
-
code.slice(0, entry.start) +
|
|
618
|
-
entry.inserted +
|
|
619
|
-
code.slice(entry.start + entry.removed.length);
|
|
620
|
-
undoStack.push(entry);
|
|
621
|
-
applyHistoryMove(nextCode, entry.selectionAfter);
|
|
81
|
+
editor?.redo();
|
|
622
82
|
}
|
|
623
83
|
|
|
624
84
|
export function focus() {
|
|
625
|
-
editor
|
|
85
|
+
editor?.focus();
|
|
626
86
|
}
|
|
627
87
|
|
|
628
88
|
export function selectAll() {
|
|
629
|
-
editor
|
|
630
|
-
setSelection(0, code.length);
|
|
89
|
+
editor?.selectAll();
|
|
631
90
|
}
|
|
632
91
|
|
|
633
92
|
export function insert(text) {
|
|
634
|
-
|
|
635
|
-
insertText(text);
|
|
93
|
+
editor?.insert(text);
|
|
636
94
|
}
|
|
637
95
|
|
|
638
96
|
export function indent() {
|
|
639
|
-
|
|
640
|
-
indentCommand();
|
|
97
|
+
editor?.indent();
|
|
641
98
|
}
|
|
642
99
|
|
|
643
100
|
export function outdent() {
|
|
644
|
-
|
|
645
|
-
shiftIndent(true);
|
|
101
|
+
editor?.outdent();
|
|
646
102
|
}
|
|
647
103
|
|
|
648
104
|
export function setCode(value) {
|
|
649
|
-
|
|
650
|
-
const previousCode = code;
|
|
651
|
-
internalCode = value;
|
|
652
|
-
code = value;
|
|
653
|
-
dispatch("change", { code });
|
|
654
|
-
pushHistory(
|
|
655
|
-
previousCode,
|
|
656
|
-
value,
|
|
657
|
-
{ start: value.length, end: value.length },
|
|
658
|
-
false,
|
|
659
|
-
);
|
|
660
|
-
paint();
|
|
105
|
+
editor?.setCode(value);
|
|
661
106
|
}
|
|
662
107
|
|
|
663
108
|
export function clear() {
|
|
664
|
-
|
|
109
|
+
editor?.clear();
|
|
665
110
|
}
|
|
666
111
|
|
|
667
112
|
export function getCode() {
|
|
@@ -669,195 +114,59 @@
|
|
|
669
114
|
}
|
|
670
115
|
|
|
671
116
|
export function canUndo() {
|
|
672
|
-
return
|
|
117
|
+
return editor?.canUndo() ?? false;
|
|
673
118
|
}
|
|
674
119
|
|
|
675
120
|
export function canRedo() {
|
|
676
|
-
return
|
|
121
|
+
return editor?.canRedo() ?? false;
|
|
677
122
|
}
|
|
678
123
|
|
|
679
|
-
/**
|
|
124
|
+
/**
|
|
125
|
+
* The engine on screen. `"css-highlights"` once it paints. `"dom"`
|
|
126
|
+
* before mount, where `CSS.highlights` is missing, while the color
|
|
127
|
+
* resolver loads, and while that load has failed.
|
|
128
|
+
*/
|
|
680
129
|
export function resolvedEngine() {
|
|
681
|
-
return
|
|
682
|
-
}
|
|
683
|
-
|
|
684
|
-
// WebKit fires historyUndo/historyRedo twice per execCommand; ignore the
|
|
685
|
-
// repeat until the next task.
|
|
686
|
-
let handlingNativeHistory = false;
|
|
687
|
-
function runNativeHistory(action) {
|
|
688
|
-
if (handlingNativeHistory) return;
|
|
689
|
-
handlingNativeHistory = true;
|
|
690
|
-
action();
|
|
691
|
-
setTimeout(() => {
|
|
692
|
-
handlingNativeHistory = false;
|
|
693
|
-
}, 0);
|
|
694
|
-
}
|
|
695
|
-
|
|
696
|
-
function onInput(event) {
|
|
697
|
-
if (composing) return;
|
|
698
|
-
// Chromium's execCommand undo/redo skips cancelable beforeinput:
|
|
699
|
-
// re-render from our history instead of recording the mutation.
|
|
700
|
-
if (event?.inputType === "historyUndo") {
|
|
701
|
-
runNativeHistory(undo);
|
|
702
|
-
return;
|
|
703
|
-
}
|
|
704
|
-
if (event?.inputType === "historyRedo") {
|
|
705
|
-
runNativeHistory(redo);
|
|
706
|
-
return;
|
|
707
|
-
}
|
|
708
|
-
const previousCode = code;
|
|
709
|
-
// Not innerText: it forces layout and Firefox can return a stale value
|
|
710
|
-
// after editing a node written via textContent. `white-space: pre`
|
|
711
|
-
// means native inserts use "\n", not <br>.
|
|
712
|
-
code = editor.textContent.replace(TRAILING_NEWLINE, "");
|
|
713
|
-
internalCode = code;
|
|
714
|
-
const caret = getCaretOffset() ?? code.length;
|
|
715
|
-
dispatch("change", { code });
|
|
716
|
-
pushHistory(previousCode, code, { start: caret, end: caret }, true);
|
|
717
|
-
renderAt(caret);
|
|
718
|
-
}
|
|
719
|
-
|
|
720
|
-
function onKeydown(event) {
|
|
721
|
-
const mod = event.metaKey || event.ctrlKey;
|
|
722
|
-
|
|
723
|
-
if (event.key === "Escape") {
|
|
724
|
-
tabTrapReleased = true;
|
|
725
|
-
return;
|
|
726
|
-
}
|
|
727
|
-
if (tabTrapReleased) {
|
|
728
|
-
tabTrapReleased = false;
|
|
729
|
-
if (event.key === "Tab") return;
|
|
730
|
-
}
|
|
731
|
-
|
|
732
|
-
if (mod && event.key === "z" && !event.shiftKey) {
|
|
733
|
-
event.preventDefault();
|
|
734
|
-
if (!readonly) undo();
|
|
735
|
-
return;
|
|
736
|
-
}
|
|
737
|
-
if (mod && (event.key === "y" || (event.key === "z" && event.shiftKey))) {
|
|
738
|
-
event.preventDefault();
|
|
739
|
-
if (!readonly) redo();
|
|
740
|
-
return;
|
|
741
|
-
}
|
|
742
|
-
if (event.key === "Enter") {
|
|
743
|
-
event.preventDefault();
|
|
744
|
-
insertText("\n");
|
|
745
|
-
return;
|
|
746
|
-
}
|
|
747
|
-
if (event.key === "Tab") {
|
|
748
|
-
event.preventDefault();
|
|
749
|
-
if (event.shiftKey) shiftIndent(true);
|
|
750
|
-
else indentCommand();
|
|
751
|
-
}
|
|
752
|
-
}
|
|
753
|
-
|
|
754
|
-
function onPaste(event) {
|
|
755
|
-
event.preventDefault();
|
|
756
|
-
insertText(event.clipboardData?.getData("text/plain") ?? "");
|
|
757
|
-
}
|
|
758
|
-
|
|
759
|
-
function onDrop(event) {
|
|
760
|
-
event.preventDefault();
|
|
761
|
-
insertText(event.dataTransfer?.getData("text/plain") ?? "");
|
|
762
|
-
}
|
|
763
|
-
|
|
764
|
-
// Edit-menu / execCommand undo-redo bypass onKeydown.
|
|
765
|
-
function onBeforeInput(event) {
|
|
766
|
-
if (event.inputType === "historyUndo") {
|
|
767
|
-
event.preventDefault();
|
|
768
|
-
runNativeHistory(undo);
|
|
769
|
-
} else if (event.inputType === "historyRedo") {
|
|
770
|
-
event.preventDefault();
|
|
771
|
-
runNativeHistory(redo);
|
|
772
|
-
}
|
|
773
|
-
}
|
|
774
|
-
|
|
775
|
-
function onBlur() {
|
|
776
|
-
tabTrapReleased = false;
|
|
777
|
-
dispatch("blur", { code: getCode() });
|
|
778
|
-
}
|
|
779
|
-
|
|
780
|
-
// Only listen to document-wide selectionchange while focused.
|
|
781
|
-
function onFocusIn() {
|
|
782
|
-
document.addEventListener("selectionchange", syncCaretToHistory);
|
|
783
|
-
}
|
|
784
|
-
|
|
785
|
-
function onFocusOut() {
|
|
786
|
-
document.removeEventListener("selectionchange", syncCaretToHistory);
|
|
787
|
-
}
|
|
788
|
-
|
|
789
|
-
function onCompositionStart() {
|
|
790
|
-
composing = true;
|
|
791
|
-
}
|
|
792
|
-
|
|
793
|
-
function onCompositionEnd() {
|
|
794
|
-
composing = false;
|
|
795
|
-
onInput();
|
|
130
|
+
return editor?.resolvedEngine() ?? "dom";
|
|
796
131
|
}
|
|
797
132
|
|
|
798
133
|
onMount(() => {
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
134
|
+
editor = createEditor(element, {
|
|
135
|
+
language,
|
|
136
|
+
code,
|
|
137
|
+
tabSize,
|
|
138
|
+
historyLimit,
|
|
139
|
+
readonly,
|
|
140
|
+
engine,
|
|
141
|
+
theme,
|
|
142
|
+
instanceId,
|
|
143
|
+
onChange: (value) => {
|
|
144
|
+
code = value;
|
|
145
|
+
internalCode = value;
|
|
146
|
+
dispatch("change", { code });
|
|
808
147
|
},
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
editor.addEventListener("input", onInput);
|
|
814
|
-
editor.addEventListener("keydown", onKeydown);
|
|
815
|
-
editor.addEventListener("beforeinput", onBeforeInput);
|
|
816
|
-
editor.addEventListener("paste", onPaste);
|
|
817
|
-
editor.addEventListener("drop", onDrop);
|
|
818
|
-
editor.addEventListener("mouseup", syncCaretToHistory);
|
|
819
|
-
editor.addEventListener("keyup", syncCaretToHistory);
|
|
820
|
-
editor.addEventListener("focusin", onFocusIn);
|
|
821
|
-
editor.addEventListener("focusout", onFocusOut);
|
|
822
|
-
editor.addEventListener("blur", onBlur);
|
|
823
|
-
editor.addEventListener("compositionstart", onCompositionStart);
|
|
824
|
-
editor.addEventListener("compositionend", onCompositionEnd);
|
|
825
|
-
|
|
826
|
-
return () => {
|
|
827
|
-
editor.removeEventListener("input", onInput);
|
|
828
|
-
editor.removeEventListener("keydown", onKeydown);
|
|
829
|
-
editor.removeEventListener("beforeinput", onBeforeInput);
|
|
830
|
-
editor.removeEventListener("paste", onPaste);
|
|
831
|
-
editor.removeEventListener("drop", onDrop);
|
|
832
|
-
editor.removeEventListener("mouseup", syncCaretToHistory);
|
|
833
|
-
editor.removeEventListener("keyup", syncCaretToHistory);
|
|
834
|
-
editor.removeEventListener("focusin", onFocusIn);
|
|
835
|
-
editor.removeEventListener("focusout", onFocusOut);
|
|
836
|
-
document.removeEventListener("selectionchange", syncCaretToHistory);
|
|
837
|
-
editor.removeEventListener("blur", onBlur);
|
|
838
|
-
editor.removeEventListener("compositionstart", onCompositionStart);
|
|
839
|
-
editor.removeEventListener("compositionend", onCompositionEnd);
|
|
840
|
-
clearCssHighlights();
|
|
841
|
-
};
|
|
148
|
+
onHistory: (snapshot) => dispatch("history", snapshot),
|
|
149
|
+
onBlur: (value) => dispatch("blur", { code: value }),
|
|
150
|
+
});
|
|
151
|
+
return () => editor?.destroy();
|
|
842
152
|
});
|
|
843
153
|
</script>
|
|
844
154
|
|
|
845
|
-
|
|
846
|
-
>{#if cssHighlightStyle}
|
|
847
|
-
{@html cssHighlightStyle}
|
|
848
|
-
{/if}</svelte:head
|
|
849
|
-
>
|
|
850
|
-
|
|
155
|
+
<!-- svelte-ignore a11y-no-noninteractive-element-to-interactive-role -->
|
|
851
156
|
<pre
|
|
852
157
|
style:overflow-x="var(--overflow-x, auto)"
|
|
853
158
|
style:border-radius="var(--border-radius, 0)"
|
|
854
|
-
{...$$restProps}
|
|
855
|
-
|
|
856
|
-
bind:this={editor}
|
|
159
|
+
{...$$restProps}><code
|
|
160
|
+
bind:this={element}
|
|
857
161
|
class:hljs={true}
|
|
858
162
|
contenteditable={readonly ? "false" : "true"}
|
|
859
163
|
spellcheck="false"
|
|
860
|
-
|
|
164
|
+
tabindex="0"
|
|
165
|
+
role="textbox"
|
|
166
|
+
aria-multiline="true"
|
|
167
|
+
aria-readonly={readonly ? "true" : undefined}
|
|
168
|
+
aria-label={label}>{code}</code
|
|
169
|
+
></pre>
|
|
861
170
|
|
|
862
171
|
<style>
|
|
863
172
|
code {
|