svelte-highlight 7.23.2 → 7.25.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/abap.d.ts +2 -0
- package/languages/abap.js +2 -0
- package/languages/aliases.js +1 -1
- package/languages/all.js +18 -1
- package/languages/angular.js +1 -1
- package/languages/antlr.d.ts +2 -0
- package/languages/antlr.js +2 -0
- package/languages/apex.d.ts +2 -0
- package/languages/apex.js +2 -0
- package/languages/bash.js +1 -1
- package/languages/cobol.d.ts +2 -0
- package/languages/cobol.js +2 -0
- package/languages/coq.js +1 -1
- package/languages/cpp.js +1 -1
- package/languages/csharp.js +1 -1
- package/languages/css.js +1 -1
- package/languages/cython.d.ts +2 -0
- package/languages/cython.js +2 -0
- package/languages/dart.js +1 -1
- package/languages/detect-index.d.ts +16 -0
- package/languages/detect-index.js +6 -0
- package/languages/fluent.d.ts +2 -0
- package/languages/fluent.js +2 -0
- package/languages/freemarker.d.ts +2 -0
- package/languages/freemarker.js +3 -0
- package/languages/handlebars.js +1 -1
- package/languages/httpfile.d.ts +2 -0
- package/languages/httpfile.js +3 -0
- package/languages/index.d.ts +34 -0
- package/languages/index.js +17 -0
- package/languages/jinja.js +1 -1
- package/languages/json.js +1 -1
- package/languages/kotlin.js +1 -1
- package/languages/lex.d.ts +2 -0
- package/languages/lex.js +3 -0
- package/languages/lisp.js +1 -1
- package/languages/loaders.d.ts +8 -0
- package/languages/loaders.js +387 -0
- package/languages/mathematica.js +1 -1
- package/languages/meson.d.ts +2 -0
- package/languages/meson.js +2 -0
- package/languages/metal.d.ts +2 -0
- package/languages/metal.js +2 -0
- package/languages/mlir.d.ts +2 -0
- package/languages/mlir.js +2 -0
- package/languages/mojo.js +1 -1
- package/languages/nextflow.js +1 -1
- package/languages/objectivec.js +1 -1
- package/languages/pgsql.js +1 -1
- package/languages/php.js +1 -1
- package/languages/po.d.ts +2 -0
- package/languages/po.js +2 -0
- package/languages/riscv.d.ts +2 -0
- package/languages/riscv.js +2 -0
- package/languages/robotframework.d.ts +2 -0
- package/languages/robotframework.js +2 -0
- package/languages/roc.js +1 -1
- package/languages/solidity.js +1 -1
- package/languages/sshconfig.d.ts +2 -0
- package/languages/sshconfig.js +2 -0
- package/languages/svelte.js +1 -1
- package/languages/uiua.js +1 -1
- package/languages/verilog.js +1 -1
- package/languages/vim.js +1 -1
- package/languages/yacc.d.ts +2 -0
- package/languages/yacc.js +3 -0
- package/languages/zig.js +1 -1
- 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
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Paints HighlightEditable's lines with either engine: "dom" (a span per
|
|
3
|
+
* token) or "css-highlights" (plain text plus CSS Custom Highlight ranges).
|
|
4
|
+
* Holds the incremental parse so each paint only re-tokenizes what changed.
|
|
5
|
+
* Framework-free; needs a DOM, plus `CSS.highlights` for "css-highlights".
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { createScopeHighlights } from "./css-highlights.js";
|
|
9
|
+
import { lineTokenRanges, retokenizedEnd } from "./editable-css-paint.js";
|
|
10
|
+
import { createDomLinePainter } from "./editable-dom-paint.js";
|
|
11
|
+
import { lineStartOffset, renderLines } from "./editable-line-dom.js";
|
|
12
|
+
import { toRanges } from "./engine.js";
|
|
13
|
+
import {
|
|
14
|
+
parseIncremental,
|
|
15
|
+
reparseIncremental,
|
|
16
|
+
} from "./incremental-tokenize.js";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @typedef {import("./css-highlights.js").PaintEngine} PaintEngine
|
|
20
|
+
* @typedef {string | import("./theme.d.ts").ThemePalette} Theme
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The resolver loads on the first themed "css-highlights" paint, so
|
|
25
|
+
* "dom"-only pages don't bundle it. One load serves every editor.
|
|
26
|
+
* @type {Promise<typeof import("./highlight-cascade.js")> | undefined}
|
|
27
|
+
*/
|
|
28
|
+
let cascadeLoad;
|
|
29
|
+
|
|
30
|
+
/** @type {typeof import("./highlight-cascade.js") | undefined} */
|
|
31
|
+
let cascadeModule;
|
|
32
|
+
|
|
33
|
+
function loadCascade() {
|
|
34
|
+
// A static specifier, so bundlers split the module into its own chunk.
|
|
35
|
+
cascadeLoad ??= import("./highlight-cascade.js").then(
|
|
36
|
+
(module) => (cascadeModule = module),
|
|
37
|
+
(error) => {
|
|
38
|
+
cascadeLoad = undefined;
|
|
39
|
+
throw error;
|
|
40
|
+
},
|
|
41
|
+
);
|
|
42
|
+
return cascadeLoad;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** @param {HTMLElement} el @param {string} line */
|
|
46
|
+
const setHtml = (el, line) => {
|
|
47
|
+
el.innerHTML = line;
|
|
48
|
+
};
|
|
49
|
+
/** @param {HTMLElement} el @param {string} line */
|
|
50
|
+
const setText = (el, line) => {
|
|
51
|
+
el.textContent = line;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @param {object} options
|
|
56
|
+
* @param {() => HTMLElement} options.editor Read on each paint, so the
|
|
57
|
+
* element can be bound after the painter is made.
|
|
58
|
+
* @param {import("./editable-line-dom.js").LineView} options.view
|
|
59
|
+
* @param {import("./engine.d.ts").Registry} options.registry
|
|
60
|
+
* @param {string} options.highlightPrefix Prefix for `CSS.highlights` names,
|
|
61
|
+
* unique per editor, e.g. `hljs-3`. Names are `${prefix}-${scope}`, or
|
|
62
|
+
* `${prefix}-c<n>` with a theme.
|
|
63
|
+
* @param {() => void} [options.onCascadeLoad] Called once the resolver for
|
|
64
|
+
* a theme loads. Until then, "css-highlights" paints as "dom" does, so
|
|
65
|
+
* the code never shows without colors. Repaint from here.
|
|
66
|
+
*/
|
|
67
|
+
export function createEditorPainter({
|
|
68
|
+
editor,
|
|
69
|
+
view,
|
|
70
|
+
registry,
|
|
71
|
+
highlightPrefix,
|
|
72
|
+
onCascadeLoad,
|
|
73
|
+
}) {
|
|
74
|
+
let destroyed = false;
|
|
75
|
+
let cascadeRequested = false;
|
|
76
|
+
/**
|
|
77
|
+
* The engine the last paint drew with.
|
|
78
|
+
* @type {PaintEngine}
|
|
79
|
+
*/
|
|
80
|
+
let paintedEngine = "dom";
|
|
81
|
+
/** @type {import("./incremental-tokenize.js").IncrementalParse | undefined} */
|
|
82
|
+
let incrementalParse;
|
|
83
|
+
const domLinePainter = createDomLinePainter();
|
|
84
|
+
|
|
85
|
+
const cssHighlights = createScopeHighlights(highlightPrefix);
|
|
86
|
+
/** @type {{ scope: string; range: Range }[][]} */
|
|
87
|
+
let lineHighlightRanges = [];
|
|
88
|
+
/** @type {import("./engine.d.ts").ScopeEvent[] | undefined} */
|
|
89
|
+
let cssPaintedEvents;
|
|
90
|
+
|
|
91
|
+
// With a theme, ranges are named by their resolved style, as in Highlight.
|
|
92
|
+
// The run table only grows, so a name means the same style on every line.
|
|
93
|
+
/** @type {Theme | undefined} */
|
|
94
|
+
let theme;
|
|
95
|
+
/** @type {import("./highlight-cascade.js").ThemeCascade | undefined} */
|
|
96
|
+
let cascade;
|
|
97
|
+
/** @type {import("./highlight-cascade.js").RunTable} */
|
|
98
|
+
let runTable = { styles: [], names: new Map() };
|
|
99
|
+
/** Rules in `styleElement`, as a count of `runTable.styles`. */
|
|
100
|
+
let styledCount = 0;
|
|
101
|
+
/** @type {HTMLStyleElement | undefined} */
|
|
102
|
+
let styleElement;
|
|
103
|
+
|
|
104
|
+
function outsideColor() {
|
|
105
|
+
const outside = editor().closest("pre")?.parentElement;
|
|
106
|
+
return outside ? getComputedStyle(outside).color : null;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* @param {import("./engine.d.ts").ScopeEvent[]} events
|
|
111
|
+
* @returns {import("./engine.d.ts").TokenRange[]}
|
|
112
|
+
*/
|
|
113
|
+
function tokenRanges(events) {
|
|
114
|
+
if (theme === undefined || !cascadeModule) return toRanges(events);
|
|
115
|
+
cascade ??= cascadeModule.createThemeCascade(theme, outsideColor);
|
|
116
|
+
return cascadeModule.cascadeRanges(events, cascade, runTable).ranges;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Adds rules for styles first seen in this paint.
|
|
120
|
+
function syncStyle() {
|
|
121
|
+
const { styles } = runTable;
|
|
122
|
+
if (styles.length === styledCount || !cascadeModule) return;
|
|
123
|
+
styledCount = styles.length;
|
|
124
|
+
if (!styleElement) {
|
|
125
|
+
const el = editor();
|
|
126
|
+
const doc = /** @type {Document} */ (el.ownerDocument);
|
|
127
|
+
const root = el.getRootNode();
|
|
128
|
+
styleElement = doc.createElement("style");
|
|
129
|
+
// In a shadow root, rules in the document head don't apply.
|
|
130
|
+
const parent =
|
|
131
|
+
root.nodeType === Node.DOCUMENT_FRAGMENT_NODE && "host" in root
|
|
132
|
+
? /** @type {ShadowRoot} */ (root)
|
|
133
|
+
: doc.head;
|
|
134
|
+
parent.append(styleElement);
|
|
135
|
+
}
|
|
136
|
+
styleElement.textContent = cascadeModule.cascadeHighlightRules(
|
|
137
|
+
styles,
|
|
138
|
+
highlightPrefix,
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function removeStyle() {
|
|
143
|
+
styleElement?.remove();
|
|
144
|
+
styleElement = undefined;
|
|
145
|
+
styledCount = 0;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function requestCascade() {
|
|
149
|
+
if (cascadeRequested) return;
|
|
150
|
+
cascadeRequested = true;
|
|
151
|
+
loadCascade().then(
|
|
152
|
+
() => {
|
|
153
|
+
// Destroyed meanwhile: paint nothing.
|
|
154
|
+
if (!destroyed) onCascadeLoad?.();
|
|
155
|
+
},
|
|
156
|
+
() => {
|
|
157
|
+
// Keep the "dom" paint. A later paint tries again.
|
|
158
|
+
cascadeRequested = false;
|
|
159
|
+
},
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** @param {string} code @param {string} language */
|
|
164
|
+
function getEvents(code, language) {
|
|
165
|
+
incrementalParse = incrementalParse
|
|
166
|
+
? reparseIncremental(registry, language, incrementalParse, code)
|
|
167
|
+
: parseIncremental(registry, language, code);
|
|
168
|
+
return incrementalParse.events;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** @param {number} index */
|
|
172
|
+
function clearLineHighlights(index) {
|
|
173
|
+
const previous = lineHighlightRanges[index];
|
|
174
|
+
if (!previous) return;
|
|
175
|
+
for (const { scope, range } of previous) {
|
|
176
|
+
cssHighlights.delete(scope, range);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function clearHighlights() {
|
|
181
|
+
cssHighlights.clear();
|
|
182
|
+
lineHighlightRanges = [];
|
|
183
|
+
cssPaintedEvents = undefined;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** @type {import("./editable-line-dom.js").LineHooks} */
|
|
187
|
+
const lineHooks = {
|
|
188
|
+
onReset: () => clearHighlights(),
|
|
189
|
+
onSplice: (index, removed, inserted) => {
|
|
190
|
+
if (index >= lineHighlightRanges.length) return;
|
|
191
|
+
for (let i = index; i < index + removed; i++) clearLineHighlights(i);
|
|
192
|
+
lineHighlightRanges = lineHighlightRanges.slice(0, index).concat(
|
|
193
|
+
Array.from({ length: inserted }, () => []),
|
|
194
|
+
lineHighlightRanges.slice(index + removed),
|
|
195
|
+
);
|
|
196
|
+
},
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
// Re-registers ranges for lines from `first` (at offset `base`) through the
|
|
200
|
+
// one ending at `end`; `tokenRanges` are relative to `base`. They're sorted
|
|
201
|
+
// and disjoint, so `tokenIndex` only advances: O(lines + tokens).
|
|
202
|
+
/**
|
|
203
|
+
* @param {number} first
|
|
204
|
+
* @param {number} base
|
|
205
|
+
* @param {number} end
|
|
206
|
+
* @param {{ start: number; end: number; scope: string }[]} tokenRanges
|
|
207
|
+
*/
|
|
208
|
+
function paintLineHighlights(first, base, end, tokenRanges) {
|
|
209
|
+
let tokenIndex = 0;
|
|
210
|
+
let lineStart = 0;
|
|
211
|
+
for (
|
|
212
|
+
let i = first;
|
|
213
|
+
i < view.lineEls.length && base + lineStart <= end;
|
|
214
|
+
i++
|
|
215
|
+
) {
|
|
216
|
+
clearLineHighlights(i);
|
|
217
|
+
const lineEnd = lineStart + /** @type {number} */ (view.lineLengths[i]);
|
|
218
|
+
const textNode = view.lineEls[i]?.firstChild;
|
|
219
|
+
/** @type {{ scope: string; range: Range }[]} */
|
|
220
|
+
const next = [];
|
|
221
|
+
if (textNode) {
|
|
222
|
+
while (
|
|
223
|
+
tokenIndex < tokenRanges.length &&
|
|
224
|
+
/** @type {{ end: number }} */ (tokenRanges[tokenIndex]).end <=
|
|
225
|
+
lineStart
|
|
226
|
+
) {
|
|
227
|
+
tokenIndex++;
|
|
228
|
+
}
|
|
229
|
+
for (
|
|
230
|
+
let j = tokenIndex;
|
|
231
|
+
j < tokenRanges.length &&
|
|
232
|
+
/** @type {{ start: number }} */ (tokenRanges[j]).start < lineEnd;
|
|
233
|
+
j++
|
|
234
|
+
) {
|
|
235
|
+
const token = /** @type {(typeof tokenRanges)[number]} */ (
|
|
236
|
+
tokenRanges[j]
|
|
237
|
+
);
|
|
238
|
+
if (token.end <= lineStart) continue;
|
|
239
|
+
const start = Math.max(token.start, lineStart) - lineStart;
|
|
240
|
+
const end = Math.min(token.end, lineEnd) - lineStart;
|
|
241
|
+
if (start === end) continue;
|
|
242
|
+
const range = new Range();
|
|
243
|
+
range.setStart(textNode, start);
|
|
244
|
+
range.setEnd(textNode, end);
|
|
245
|
+
cssHighlights.add(token.scope, range);
|
|
246
|
+
next.push({ scope: token.scope, range });
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
lineHighlightRanges[i] = next;
|
|
250
|
+
lineStart = lineEnd + 1; // +1 for the "\n" separator
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** @param {string} code @param {string} language */
|
|
255
|
+
function paintCssHighlights(code, language) {
|
|
256
|
+
const changedIndex = renderLines(
|
|
257
|
+
editor(),
|
|
258
|
+
view,
|
|
259
|
+
code.split("\n"),
|
|
260
|
+
setText,
|
|
261
|
+
lineHooks,
|
|
262
|
+
);
|
|
263
|
+
const previousEvents = cssPaintedEvents;
|
|
264
|
+
const events = getEvents(code, language);
|
|
265
|
+
cssPaintedEvents = events;
|
|
266
|
+
|
|
267
|
+
// Nothing painted yet (e.g. after a theme change): paint every line.
|
|
268
|
+
if (changedIndex == null || previousEvents === undefined) {
|
|
269
|
+
paintLineHighlights(0, 0, code.length, tokenRanges(events));
|
|
270
|
+
} else {
|
|
271
|
+
// One line's text changed, but its tokens can affect later lines
|
|
272
|
+
// (typing `/*`): repaint through where the re-parse converged.
|
|
273
|
+
const base = lineStartOffset(view, changedIndex);
|
|
274
|
+
const end = retokenizedEnd(
|
|
275
|
+
code,
|
|
276
|
+
events,
|
|
277
|
+
previousEvents,
|
|
278
|
+
incrementalParse?.reuse,
|
|
279
|
+
base,
|
|
280
|
+
);
|
|
281
|
+
paintLineHighlights(
|
|
282
|
+
changedIndex,
|
|
283
|
+
base,
|
|
284
|
+
end,
|
|
285
|
+
lineTokenRanges(events, base, end, tokenRanges),
|
|
286
|
+
);
|
|
287
|
+
}
|
|
288
|
+
syncStyle();
|
|
289
|
+
return changedIndex;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
return {
|
|
293
|
+
/**
|
|
294
|
+
* Brings the editor's lines up to date with `code`.
|
|
295
|
+
* @param {string} code
|
|
296
|
+
* @param {string} language
|
|
297
|
+
* @param {PaintEngine} engine
|
|
298
|
+
* @returns {number | null} The one changed line's index, or null when
|
|
299
|
+
* more than one line changed.
|
|
300
|
+
*/
|
|
301
|
+
paint(code, language, engine) {
|
|
302
|
+
if (engine === "css-highlights" && theme !== undefined && !cascadeModule)
|
|
303
|
+
requestCascade();
|
|
304
|
+
else if (engine === "css-highlights") {
|
|
305
|
+
paintedEngine = "css-highlights";
|
|
306
|
+
return paintCssHighlights(code, language);
|
|
307
|
+
}
|
|
308
|
+
paintedEngine = "dom";
|
|
309
|
+
const events = getEvents(code, language);
|
|
310
|
+
return renderLines(
|
|
311
|
+
editor(),
|
|
312
|
+
view,
|
|
313
|
+
domLinePainter.paint(events, code, language, incrementalParse?.reuse),
|
|
314
|
+
setHtml,
|
|
315
|
+
lineHooks,
|
|
316
|
+
);
|
|
317
|
+
},
|
|
318
|
+
/** Drops cached parses, e.g. after a language or engine change. */
|
|
319
|
+
reset() {
|
|
320
|
+
incrementalParse = undefined;
|
|
321
|
+
domLinePainter.reset();
|
|
322
|
+
},
|
|
323
|
+
/**
|
|
324
|
+
* Sets the theme that "css-highlights" ranges resolve their colors
|
|
325
|
+
* from. Clears the ranges when it changes; the next paint redraws them.
|
|
326
|
+
* @param {Theme | undefined} next
|
|
327
|
+
* @returns {boolean} Whether the theme changed.
|
|
328
|
+
*/
|
|
329
|
+
setTheme(next) {
|
|
330
|
+
if (next === theme) return false;
|
|
331
|
+
theme = next;
|
|
332
|
+
cascade = undefined;
|
|
333
|
+
runTable = { styles: [], names: new Map() };
|
|
334
|
+
removeStyle();
|
|
335
|
+
clearHighlights();
|
|
336
|
+
return true;
|
|
337
|
+
},
|
|
338
|
+
/**
|
|
339
|
+
* The engine the last paint drew with. `"dom"` while a themed
|
|
340
|
+
* `"css-highlights"` paint waits for the resolver, or after its load
|
|
341
|
+
* failed.
|
|
342
|
+
* @returns {PaintEngine}
|
|
343
|
+
*/
|
|
344
|
+
paintedEngine() {
|
|
345
|
+
return paintedEngine;
|
|
346
|
+
},
|
|
347
|
+
/** Removes this editor's CSS highlights. */
|
|
348
|
+
clearHighlights,
|
|
349
|
+
/** Removes this editor's CSS highlights and their `<style>`. */
|
|
350
|
+
destroy() {
|
|
351
|
+
destroyed = true;
|
|
352
|
+
clearHighlights();
|
|
353
|
+
removeStyle();
|
|
354
|
+
},
|
|
355
|
+
};
|
|
356
|
+
}
|
package/editable.d.ts
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import type { LanguageType } from "./languages";
|
|
2
|
+
import type { ThemePalette } from "./theme.d.ts";
|
|
3
|
+
|
|
4
|
+
export type EditorEngine = "dom" | "css-highlights";
|
|
5
|
+
|
|
6
|
+
export interface EditorSettings {
|
|
7
|
+
/** Language module from `svelte-highlight/languages/*`. */
|
|
8
|
+
language: LanguageType<string>;
|
|
9
|
+
/** Number of spaces inserted by the Tab key. @default 2 */
|
|
10
|
+
tabSize: number;
|
|
11
|
+
/** Maximum number of undo snapshots retained. @default 200 */
|
|
12
|
+
historyLimit: number;
|
|
13
|
+
/** Blocks edits while keeping highlighting, caret and selection. @default false */
|
|
14
|
+
readonly: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* `"css-highlights"` (experimental) paints with the CSS Custom Highlight
|
|
17
|
+
* API and falls back to `"dom"` where it is missing. @default "dom"
|
|
18
|
+
*/
|
|
19
|
+
engine: EditorEngine;
|
|
20
|
+
/**
|
|
21
|
+
* Theme CSS or `ThemePalette` that `"css-highlights"` resolves token
|
|
22
|
+
* colors from, as `Highlight` does. The editor adds the matching
|
|
23
|
+
* `::highlight()` rules to the page itself. Colors only.
|
|
24
|
+
*/
|
|
25
|
+
theme: string | ThemePalette | undefined;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface EditorHistorySnapshot {
|
|
29
|
+
/** Oldest to newest. */
|
|
30
|
+
entries: { size: number }[];
|
|
31
|
+
/** Index of the current entry. */
|
|
32
|
+
index: number;
|
|
33
|
+
canUndo: boolean;
|
|
34
|
+
canRedo: boolean;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface EditorOptions extends Partial<EditorSettings> {
|
|
38
|
+
language: LanguageType<string>;
|
|
39
|
+
/** Initial code. @default "" */
|
|
40
|
+
code?: string;
|
|
41
|
+
/**
|
|
42
|
+
* Inserted into `CSS.highlights` names: `hljs-<id>-c<n>` with `theme`,
|
|
43
|
+
* `hljs-<id>-<scope>` without. Pass the same id to
|
|
44
|
+
* `instanceHighlightStyle`. @default a unique id
|
|
45
|
+
*/
|
|
46
|
+
instanceId?: string | number;
|
|
47
|
+
/** Called on each edit, undo and redo. */
|
|
48
|
+
onChange?: (code: string) => void;
|
|
49
|
+
/** Called when the undo history changes. */
|
|
50
|
+
onHistory?: (snapshot: EditorHistorySnapshot) => void;
|
|
51
|
+
/** Called on blur. */
|
|
52
|
+
onBlur?: (code: string) => void;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface Editor {
|
|
56
|
+
/** The id used in this editor's `CSS.highlights` names. */
|
|
57
|
+
readonly instanceId: string | number;
|
|
58
|
+
/** Applies changed settings. A new language, engine or theme repaints. */
|
|
59
|
+
update(settings: Partial<EditorSettings>): void;
|
|
60
|
+
/**
|
|
61
|
+
* Takes code changed outside the editor. Records an undo step, ignores
|
|
62
|
+
* `readonly` and doesn't call `onChange`.
|
|
63
|
+
*/
|
|
64
|
+
syncCode(code: string): void;
|
|
65
|
+
undo(): void;
|
|
66
|
+
redo(): void;
|
|
67
|
+
focus(): void;
|
|
68
|
+
selectAll(): void;
|
|
69
|
+
/** Inserts text at the caret, replacing any selection. */
|
|
70
|
+
insert(text: string): void;
|
|
71
|
+
/** Indents the selected lines, or inserts one indent at the caret. */
|
|
72
|
+
indent(): void;
|
|
73
|
+
/** Dedents the selected lines. */
|
|
74
|
+
outdent(): void;
|
|
75
|
+
/** Replaces the whole document as one undo step. */
|
|
76
|
+
setCode(code: string): void;
|
|
77
|
+
clear(): void;
|
|
78
|
+
getCode(): string;
|
|
79
|
+
canUndo(): boolean;
|
|
80
|
+
canRedo(): boolean;
|
|
81
|
+
/**
|
|
82
|
+
* The engine on screen. `"css-highlights"` once it paints. `"dom"`
|
|
83
|
+
* where `CSS.highlights` is missing, while a theme's color resolver
|
|
84
|
+
* loads, and while that load has failed.
|
|
85
|
+
*/
|
|
86
|
+
resolvedEngine(): EditorEngine;
|
|
87
|
+
/** Removes listeners, this editor's CSS highlights and their `<style>`. */
|
|
88
|
+
destroy(): void;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Makes `element` a highlighted code editor, with no framework. Use a
|
|
93
|
+
* `<code contenteditable>` inside a `<pre>`.
|
|
94
|
+
* @example
|
|
95
|
+
* import { createEditor } from "svelte-highlight/editable";
|
|
96
|
+
* import typescript from "svelte-highlight/languages/typescript";
|
|
97
|
+
*
|
|
98
|
+
* const editor = createEditor(document.querySelector("code"), {
|
|
99
|
+
* language: typescript,
|
|
100
|
+
* code: "const a = 1;",
|
|
101
|
+
* onChange: (code) => console.log(code),
|
|
102
|
+
* });
|
|
103
|
+
*/
|
|
104
|
+
export function createEditor(
|
|
105
|
+
element: HTMLElement,
|
|
106
|
+
options: EditorOptions,
|
|
107
|
+
): Editor;
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* `<style>`-wrapped `::highlight()` rules for one editor's
|
|
111
|
+
* `"css-highlights"` engine, for an editor made without `theme`. Pass the
|
|
112
|
+
* editor's `instanceId`. Only single-class `.hljs-<scope>` rules convert.
|
|
113
|
+
* Prefer the `theme` option, which resolves every rule as `Highlight` does.
|
|
114
|
+
*/
|
|
115
|
+
export function instanceHighlightStyle(
|
|
116
|
+
theme: string | ThemePalette,
|
|
117
|
+
instanceId: string | number,
|
|
118
|
+
): string;
|