svelte-highlight 7.11.0 → 7.13.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 +193 -0
- package/AnsiOutput.svelte.d.ts +112 -0
- package/CodeWindow.svelte +110 -0
- package/CodeWindow.svelte.d.ts +136 -0
- package/CopyButton.svelte +115 -0
- package/CopyButton.svelte.d.ts +134 -0
- package/FileTabs.svelte +161 -0
- package/FileTabs.svelte.d.ts +96 -0
- package/Highlight.svelte.d.ts +9 -10
- package/HighlightAuto.svelte.d.ts +2 -2
- package/HighlightEditable.svelte +385 -0
- package/HighlightEditable.svelte.d.ts +113 -0
- package/HighlightStyle.svelte +40 -0
- package/HighlightStyle.svelte.d.ts +50 -0
- package/HighlightSvelte.svelte.d.ts +1 -1
- package/LangTag.svelte +10 -1
- package/LangTag.svelte.d.ts +3 -3
- package/LineNumbers.svelte +18 -6
- package/LineNumbers.svelte.d.ts +24 -8
- package/README.md +832 -7
- package/Typewriter.svelte +238 -0
- package/Typewriter.svelte.d.ts +65 -0
- package/action.d.ts +20 -0
- package/action.js +25 -0
- package/ansi.d.ts +36 -0
- package/ansi.js +198 -0
- package/index.d.ts +12 -0
- package/index.js +10 -0
- package/langtag.css +1 -0
- package/languages/bibtex.d.ts +2 -0
- package/languages/bibtex.js +47 -0
- package/languages/bicep.d.ts +2 -0
- package/languages/bicep.js +72 -0
- package/languages/blade.d.ts +2 -0
- package/languages/blade.js +56 -0
- package/languages/caddy.d.ts +2 -0
- package/languages/caddy.js +59 -0
- package/languages/cairo.d.ts +2 -0
- package/languages/cairo.js +69 -0
- package/languages/clarity.d.ts +2 -0
- package/languages/clarity.js +55 -0
- package/languages/cue.d.ts +2 -0
- package/languages/cue.js +76 -0
- package/languages/cypher.d.ts +2 -0
- package/languages/cypher.js +61 -0
- package/languages/d2.d.ts +2 -0
- package/languages/d2.js +61 -0
- package/languages/dhall.d.ts +2 -0
- package/languages/dhall.js +74 -0
- package/languages/dotenv.d.ts +2 -0
- package/languages/dotenv.js +44 -0
- package/languages/fish.d.ts +2 -0
- package/languages/fish.js +68 -0
- package/languages/gleam.d.ts +2 -0
- package/languages/gleam.js +63 -0
- package/languages/groq.d.ts +2 -0
- package/languages/groq.js +69 -0
- package/languages/hcl.d.ts +2 -0
- package/languages/hcl.js +84 -0
- package/languages/index.d.ts +73 -1
- package/languages/index.js +36 -0
- package/languages/json5.d.ts +2 -0
- package/languages/json5.js +55 -0
- package/languages/jsonc.d.ts +2 -0
- package/languages/jsonc.js +36 -0
- package/languages/jsonnet.d.ts +2 -0
- package/languages/jsonnet.js +64 -0
- package/languages/liquid.d.ts +2 -0
- package/languages/liquid.js +55 -0
- package/languages/move.d.ts +2 -0
- package/languages/move.js +72 -0
- package/languages/nickel.d.ts +2 -0
- package/languages/nickel.js +65 -0
- package/languages/nushell.d.ts +2 -0
- package/languages/nushell.js +77 -0
- package/languages/odin.d.ts +2 -0
- package/languages/odin.js +78 -0
- package/languages/pkl.d.ts +2 -0
- package/languages/pkl.js +82 -0
- package/languages/prisma.d.ts +2 -0
- package/languages/prisma.js +70 -0
- package/languages/promql.d.ts +2 -0
- package/languages/promql.js +61 -0
- package/languages/pug.d.ts +2 -0
- package/languages/pug.js +50 -0
- package/languages/razor.d.ts +2 -0
- package/languages/razor.js +69 -0
- package/languages/rescript.d.ts +2 -0
- package/languages/rescript.js +71 -0
- package/languages/solidity.d.ts +2 -0
- package/languages/solidity.js +101 -0
- package/languages/starlark.d.ts +2 -0
- package/languages/starlark.js +54 -0
- package/languages/toml.d.ts +2 -0
- package/languages/toml.js +74 -0
- package/languages/v.d.ts +2 -0
- package/languages/v.js +90 -0
- package/languages/vyper.d.ts +2 -0
- package/languages/vyper.js +57 -0
- package/languages/wgsl.d.ts +2 -0
- package/languages/wgsl.js +56 -0
- package/languages/zig.d.ts +2 -0
- package/languages/zig.js +100 -0
- package/load-language.d.ts +9 -0
- package/load-language.js +15 -0
- package/package.json +1 -1
- package/scoped.d.ts +23 -0
- package/scoped.js +307 -0
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
<script>
|
|
2
|
+
/** @type {import("./languages").LanguageType<string>} */
|
|
3
|
+
export let language;
|
|
4
|
+
|
|
5
|
+
/** @type {string} */
|
|
6
|
+
export let code = "";
|
|
7
|
+
|
|
8
|
+
/** @type {number} Number of spaces inserted by the Tab key. */
|
|
9
|
+
export let tabSize = 2;
|
|
10
|
+
|
|
11
|
+
/** @type {number} Maximum number of undo snapshots retained. */
|
|
12
|
+
export let historyLimit = 200;
|
|
13
|
+
|
|
14
|
+
import hljs from "highlight.js/lib/core";
|
|
15
|
+
import { createEventDispatcher, onMount } from "svelte";
|
|
16
|
+
|
|
17
|
+
const dispatch = createEventDispatcher();
|
|
18
|
+
const TRAILING_NEWLINE = /\n$/;
|
|
19
|
+
|
|
20
|
+
/** @type {HTMLElement} */
|
|
21
|
+
let editor;
|
|
22
|
+
|
|
23
|
+
// Don't re-highlight during IME composition.
|
|
24
|
+
let composing = false;
|
|
25
|
+
let mounted = false;
|
|
26
|
+
let restoringSelection = false;
|
|
27
|
+
|
|
28
|
+
// Tracks `code` so parent updates vs local edits can be distinguished.
|
|
29
|
+
let internalCode = code;
|
|
30
|
+
|
|
31
|
+
$: hljs.registerLanguage(language.name, language.register);
|
|
32
|
+
$: indentUnit = " ".repeat(tabSize);
|
|
33
|
+
$: if (mounted && code !== internalCode) syncExternal();
|
|
34
|
+
|
|
35
|
+
function getSelectionRange() {
|
|
36
|
+
const selection = window.getSelection();
|
|
37
|
+
if (!selection || selection.rangeCount === 0) return null;
|
|
38
|
+
const range = selection.getRangeAt(0);
|
|
39
|
+
if (!editor.contains(range.commonAncestorContainer)) return null;
|
|
40
|
+
const pre = range.cloneRange();
|
|
41
|
+
pre.selectNodeContents(editor);
|
|
42
|
+
pre.setEnd(range.startContainer, range.startOffset);
|
|
43
|
+
const start = pre.toString().length;
|
|
44
|
+
return { start, end: start + range.toString().length };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function getCaretOffset() {
|
|
48
|
+
const sel = getSelectionRange();
|
|
49
|
+
return sel ? sel.end : null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function nodeAtOffset(offset) {
|
|
53
|
+
const walker = document.createTreeWalker(
|
|
54
|
+
editor,
|
|
55
|
+
NodeFilter.SHOW_TEXT,
|
|
56
|
+
null,
|
|
57
|
+
);
|
|
58
|
+
let count = 0;
|
|
59
|
+
for (let node = walker.nextNode(); node; node = walker.nextNode()) {
|
|
60
|
+
const next = count + node.textContent.length;
|
|
61
|
+
if (offset <= next) return { node, offset: offset - count };
|
|
62
|
+
count = next;
|
|
63
|
+
}
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function setSelection(start, end) {
|
|
68
|
+
const from = nodeAtOffset(start);
|
|
69
|
+
if (!from) return;
|
|
70
|
+
const range = document.createRange();
|
|
71
|
+
range.setStart(from.node, from.offset);
|
|
72
|
+
const to = end != null && end !== start ? nodeAtOffset(end) : null;
|
|
73
|
+
if (to) range.setEnd(to.node, to.offset);
|
|
74
|
+
else range.collapse(true);
|
|
75
|
+
const selection = window.getSelection();
|
|
76
|
+
selection.removeAllRanges();
|
|
77
|
+
selection.addRange(range);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function paint() {
|
|
81
|
+
const html = hljs.highlight(code, { language: language.name }).value;
|
|
82
|
+
// Trailing empty line needs a phantom `\n` for caret placement.
|
|
83
|
+
editor.innerHTML = code === "" || code.endsWith("\n") ? `${html}\n` : html;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function renderAt(start, end) {
|
|
87
|
+
paint();
|
|
88
|
+
if (start == null) return;
|
|
89
|
+
restoringSelection = true;
|
|
90
|
+
setSelection(start, end);
|
|
91
|
+
restoringSelection = false;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function syncCaretToHistory() {
|
|
95
|
+
if (restoringSelection || composing || undoStack.length === 0) return;
|
|
96
|
+
const sel = getSelectionRange();
|
|
97
|
+
if (!sel) return;
|
|
98
|
+
const top = undoStack[undoStack.length - 1];
|
|
99
|
+
if (top.code !== code) return;
|
|
100
|
+
undoStack[undoStack.length - 1] = {
|
|
101
|
+
code: top.code,
|
|
102
|
+
start: sel.start,
|
|
103
|
+
end: sel.end,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** @type {Array<{ code: string; start: number; end: number }>} */
|
|
108
|
+
let undoStack = [];
|
|
109
|
+
/** @type {Array<{ code: string; start: number; end: number }>} */
|
|
110
|
+
let redoStack = [];
|
|
111
|
+
let lastRecord = 0;
|
|
112
|
+
|
|
113
|
+
function emitHistory() {
|
|
114
|
+
const future = redoStack.slice().reverse();
|
|
115
|
+
dispatch("history", {
|
|
116
|
+
entries: [...undoStack, ...future].map((snap) => ({
|
|
117
|
+
size: snap.code.length,
|
|
118
|
+
})),
|
|
119
|
+
index: undoStack.length - 1,
|
|
120
|
+
canUndo: canUndo(),
|
|
121
|
+
canRedo: canRedo(),
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Coalesce typing within 250ms; Enter/Tab/paste pass coalesce=false.
|
|
126
|
+
function pushHistory(snap, coalesce) {
|
|
127
|
+
redoStack = [];
|
|
128
|
+
const now = Date.now();
|
|
129
|
+
if (coalesce && undoStack.length > 1 && now - lastRecord < 250) {
|
|
130
|
+
undoStack[undoStack.length - 1] = snap;
|
|
131
|
+
} else {
|
|
132
|
+
undoStack.push(snap);
|
|
133
|
+
const limit = Math.max(1, historyLimit);
|
|
134
|
+
if (undoStack.length > limit) {
|
|
135
|
+
undoStack.splice(0, undoStack.length - limit);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
lastRecord = coalesce ? now : 0;
|
|
139
|
+
emitHistory();
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function applySnapshot(snap) {
|
|
143
|
+
code = snap.code;
|
|
144
|
+
internalCode = snap.code;
|
|
145
|
+
dispatch("change", { code });
|
|
146
|
+
emitHistory();
|
|
147
|
+
renderAt(snap.start, snap.end);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function commit(value, start, end, coalesce) {
|
|
151
|
+
code = value;
|
|
152
|
+
internalCode = value;
|
|
153
|
+
dispatch("change", { code });
|
|
154
|
+
pushHistory({ code: value, start, end }, coalesce);
|
|
155
|
+
renderAt(start, end);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function syncExternal() {
|
|
159
|
+
internalCode = code;
|
|
160
|
+
pushHistory({ code, start: code.length, end: code.length }, false);
|
|
161
|
+
paint();
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function insertText(text) {
|
|
165
|
+
const sel = getSelectionRange();
|
|
166
|
+
if (!sel) return;
|
|
167
|
+
const value = code.slice(0, sel.start) + text + code.slice(sel.end);
|
|
168
|
+
commit(value, sel.start + text.length, undefined, false);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// Indent/dedent selected lines; blank lines skipped on indent.
|
|
172
|
+
function shiftIndent(outdent) {
|
|
173
|
+
const sel = getSelectionRange();
|
|
174
|
+
if (!sel) return;
|
|
175
|
+
const blockStart = code.lastIndexOf("\n", sel.start - 1) + 1;
|
|
176
|
+
const lines = code.slice(blockStart, sel.end).split("\n");
|
|
177
|
+
|
|
178
|
+
let firstDelta = 0;
|
|
179
|
+
let totalDelta = 0;
|
|
180
|
+
const next = lines
|
|
181
|
+
.map((line, index) => {
|
|
182
|
+
if (outdent) {
|
|
183
|
+
const leading = line.length - line.trimStart().length;
|
|
184
|
+
const remove = Math.min(tabSize, leading);
|
|
185
|
+
if (index === 0) firstDelta = -remove;
|
|
186
|
+
totalDelta -= remove;
|
|
187
|
+
return line.slice(remove);
|
|
188
|
+
}
|
|
189
|
+
if (line.length === 0) return line;
|
|
190
|
+
if (index === 0) firstDelta = tabSize;
|
|
191
|
+
totalDelta += tabSize;
|
|
192
|
+
return indentUnit + line;
|
|
193
|
+
})
|
|
194
|
+
.join("\n");
|
|
195
|
+
|
|
196
|
+
if (totalDelta === 0) return;
|
|
197
|
+
const value = code.slice(0, blockStart) + next + code.slice(sel.end);
|
|
198
|
+
const start = Math.max(blockStart, sel.start + firstDelta);
|
|
199
|
+
commit(value, start, sel.end + totalDelta, false);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function indentCommand() {
|
|
203
|
+
const sel = getSelectionRange();
|
|
204
|
+
if (sel && code.slice(sel.start, sel.end).includes("\n"))
|
|
205
|
+
shiftIndent(false);
|
|
206
|
+
else insertText(indentUnit);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function ensureFocus() {
|
|
210
|
+
if (document.activeElement === editor) return;
|
|
211
|
+
// Restore selection after focus; default caret to end.
|
|
212
|
+
const prior = getSelectionRange();
|
|
213
|
+
editor.focus();
|
|
214
|
+
if (prior) setSelection(prior.start, prior.end);
|
|
215
|
+
else setSelection(code.length);
|
|
216
|
+
syncCaretToHistory();
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export function undo() {
|
|
220
|
+
if (undoStack.length <= 1) return;
|
|
221
|
+
redoStack.push(undoStack.pop());
|
|
222
|
+
applySnapshot(undoStack[undoStack.length - 1]);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export function redo() {
|
|
226
|
+
if (redoStack.length === 0) return;
|
|
227
|
+
const snap = redoStack.pop();
|
|
228
|
+
undoStack.push(snap);
|
|
229
|
+
applySnapshot(snap);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export function focus() {
|
|
233
|
+
editor.focus();
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export function selectAll() {
|
|
237
|
+
editor.focus();
|
|
238
|
+
setSelection(0, code.length);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export function insert(text) {
|
|
242
|
+
ensureFocus();
|
|
243
|
+
insertText(text);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
export function indent() {
|
|
247
|
+
ensureFocus();
|
|
248
|
+
indentCommand();
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
export function outdent() {
|
|
252
|
+
ensureFocus();
|
|
253
|
+
shiftIndent(true);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
export function setCode(value) {
|
|
257
|
+
if (value === code) return;
|
|
258
|
+
internalCode = value;
|
|
259
|
+
code = value;
|
|
260
|
+
dispatch("change", { code });
|
|
261
|
+
pushHistory({ code: value, start: value.length, end: value.length }, false);
|
|
262
|
+
paint();
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
export function clear() {
|
|
266
|
+
setCode("");
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export function getCode() {
|
|
270
|
+
return code;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
export function canUndo() {
|
|
274
|
+
return undoStack.length > 1;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
export function canRedo() {
|
|
278
|
+
return redoStack.length > 0;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function onInput() {
|
|
282
|
+
if (composing) return;
|
|
283
|
+
// innerText keeps contenteditable line breaks; textContent doesn't.
|
|
284
|
+
code = editor.innerText.replace(TRAILING_NEWLINE, "");
|
|
285
|
+
internalCode = code;
|
|
286
|
+
const caret = getCaretOffset() ?? code.length;
|
|
287
|
+
dispatch("change", { code });
|
|
288
|
+
pushHistory({ code, start: caret, end: caret }, true);
|
|
289
|
+
renderAt(caret);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function onKeydown(event) {
|
|
293
|
+
const mod = event.metaKey || event.ctrlKey;
|
|
294
|
+
|
|
295
|
+
if (mod && event.key === "z" && !event.shiftKey) {
|
|
296
|
+
event.preventDefault();
|
|
297
|
+
undo();
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
if (mod && (event.key === "y" || (event.key === "z" && event.shiftKey))) {
|
|
301
|
+
event.preventDefault();
|
|
302
|
+
redo();
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
if (event.key === "Enter") {
|
|
306
|
+
event.preventDefault();
|
|
307
|
+
insertText("\n");
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
if (event.key === "Tab") {
|
|
311
|
+
event.preventDefault();
|
|
312
|
+
if (event.shiftKey) shiftIndent(true);
|
|
313
|
+
else indentCommand();
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function onPaste(event) {
|
|
318
|
+
event.preventDefault();
|
|
319
|
+
insertText(event.clipboardData?.getData("text/plain") ?? "");
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
function onBlur() {
|
|
323
|
+
dispatch("blur", { code: getCode() });
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
function onCompositionStart() {
|
|
327
|
+
composing = true;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
function onCompositionEnd() {
|
|
331
|
+
composing = false;
|
|
332
|
+
onInput();
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
onMount(() => {
|
|
336
|
+
paint();
|
|
337
|
+
undoStack = [{ code, start: 0, end: 0 }];
|
|
338
|
+
mounted = true;
|
|
339
|
+
emitHistory();
|
|
340
|
+
|
|
341
|
+
editor.addEventListener("input", onInput);
|
|
342
|
+
editor.addEventListener("keydown", onKeydown);
|
|
343
|
+
editor.addEventListener("paste", onPaste);
|
|
344
|
+
editor.addEventListener("mouseup", syncCaretToHistory);
|
|
345
|
+
editor.addEventListener("keyup", syncCaretToHistory);
|
|
346
|
+
document.addEventListener("selectionchange", syncCaretToHistory);
|
|
347
|
+
editor.addEventListener("blur", onBlur);
|
|
348
|
+
editor.addEventListener("compositionstart", onCompositionStart);
|
|
349
|
+
editor.addEventListener("compositionend", onCompositionEnd);
|
|
350
|
+
|
|
351
|
+
return () => {
|
|
352
|
+
editor.removeEventListener("input", onInput);
|
|
353
|
+
editor.removeEventListener("keydown", onKeydown);
|
|
354
|
+
editor.removeEventListener("paste", onPaste);
|
|
355
|
+
editor.removeEventListener("mouseup", syncCaretToHistory);
|
|
356
|
+
editor.removeEventListener("keyup", syncCaretToHistory);
|
|
357
|
+
document.removeEventListener("selectionchange", syncCaretToHistory);
|
|
358
|
+
editor.removeEventListener("blur", onBlur);
|
|
359
|
+
editor.removeEventListener("compositionstart", onCompositionStart);
|
|
360
|
+
editor.removeEventListener("compositionend", onCompositionEnd);
|
|
361
|
+
};
|
|
362
|
+
});
|
|
363
|
+
</script>
|
|
364
|
+
|
|
365
|
+
<pre
|
|
366
|
+
style:overflow-x="var(--overflow-x, auto)"
|
|
367
|
+
style:border-radius="var(--border-radius, 0)"
|
|
368
|
+
{...$$restProps}
|
|
369
|
+
><code
|
|
370
|
+
bind:this={editor}
|
|
371
|
+
class:hljs={true}
|
|
372
|
+
contenteditable="true"
|
|
373
|
+
spellcheck="false"
|
|
374
|
+
></code></pre>
|
|
375
|
+
|
|
376
|
+
<style>
|
|
377
|
+
code {
|
|
378
|
+
outline: none;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
code:focus {
|
|
382
|
+
outline: var(--outline-width, 2px) solid var(--outline-color, #4589ff);
|
|
383
|
+
outline-offset: var(--outline-offset, -2px);
|
|
384
|
+
}
|
|
385
|
+
</style>
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import type { SvelteComponentTyped } from "svelte";
|
|
2
|
+
import type { HTMLAttributes } from "svelte/elements";
|
|
3
|
+
import type { LanguageType } from "./languages";
|
|
4
|
+
|
|
5
|
+
export type HighlightEditableProps = HTMLAttributes<HTMLPreElement> & {
|
|
6
|
+
/**
|
|
7
|
+
* Editable code (`bind:code`).
|
|
8
|
+
*/
|
|
9
|
+
code?: string;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* highlight.js language module.
|
|
13
|
+
* Import languages from `svelte-highlight/languages/*`.
|
|
14
|
+
* @example
|
|
15
|
+
* import typescript from "svelte-highlight/languages/typescript";
|
|
16
|
+
*/
|
|
17
|
+
language: LanguageType<string>;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Number of spaces inserted by the Tab key.
|
|
21
|
+
* @default 2
|
|
22
|
+
*/
|
|
23
|
+
tabSize?: number;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Maximum number of undo snapshots retained. Older snapshots are dropped
|
|
27
|
+
* once the limit is exceeded.
|
|
28
|
+
* @default 200
|
|
29
|
+
*/
|
|
30
|
+
historyLimit?: number;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Color of the focus outline.
|
|
34
|
+
* @default "#4589ff"
|
|
35
|
+
*/
|
|
36
|
+
"--outline-color"?: string;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Width of the focus outline.
|
|
40
|
+
* @default "2px"
|
|
41
|
+
*/
|
|
42
|
+
"--outline-width"?: string | number;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Offset of the focus outline.
|
|
46
|
+
* @default "-2px"
|
|
47
|
+
*/
|
|
48
|
+
"--outline-offset"?: string | number;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export type HighlightEditableEvents = {
|
|
52
|
+
/**
|
|
53
|
+
* Fired on each edit with current plain-text code.
|
|
54
|
+
*/
|
|
55
|
+
change: CustomEvent<{ code: string }>;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Fired when the editor loses focus with the current plain-text code.
|
|
59
|
+
*/
|
|
60
|
+
blur: CustomEvent<{ code: string }>;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Undo/redo history. `entries` is oldest→newest; `index` is current.
|
|
64
|
+
*/
|
|
65
|
+
history: CustomEvent<{
|
|
66
|
+
entries: { size: number }[];
|
|
67
|
+
index: number;
|
|
68
|
+
canUndo: boolean;
|
|
69
|
+
canRedo: boolean;
|
|
70
|
+
}>;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
export default class HighlightEditable extends SvelteComponentTyped<
|
|
74
|
+
HighlightEditableProps,
|
|
75
|
+
HighlightEditableEvents,
|
|
76
|
+
Record<string, never>
|
|
77
|
+
> {
|
|
78
|
+
/** Undo to the previous history snapshot. */
|
|
79
|
+
undo(): void;
|
|
80
|
+
|
|
81
|
+
/** Redo the last undone history snapshot. */
|
|
82
|
+
redo(): void;
|
|
83
|
+
|
|
84
|
+
/** Focus the editor. */
|
|
85
|
+
focus(): void;
|
|
86
|
+
|
|
87
|
+
/** Select the entire document. */
|
|
88
|
+
selectAll(): void;
|
|
89
|
+
|
|
90
|
+
/** Insert text at the caret (replacing any selection). */
|
|
91
|
+
insert(text: string): void;
|
|
92
|
+
|
|
93
|
+
/** Indent the selected lines (or insert one indent at the caret). */
|
|
94
|
+
indent(): void;
|
|
95
|
+
|
|
96
|
+
/** Dedent the selected lines. */
|
|
97
|
+
outdent(): void;
|
|
98
|
+
|
|
99
|
+
/** Replace the entire document, recording it as an undo step. */
|
|
100
|
+
setCode(value: string): void;
|
|
101
|
+
|
|
102
|
+
/** Empty the document. */
|
|
103
|
+
clear(): void;
|
|
104
|
+
|
|
105
|
+
/** Read the current code. */
|
|
106
|
+
getCode(): string;
|
|
107
|
+
|
|
108
|
+
/** Whether an undo step is available. */
|
|
109
|
+
canUndo(): boolean;
|
|
110
|
+
|
|
111
|
+
/** Whether a redo step is available. */
|
|
112
|
+
canRedo(): boolean;
|
|
113
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
<script>
|
|
2
|
+
import { dualStyle, scopeClassFor, scopeStyle } from "./scoped.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Theme CSS from `svelte-highlight/styles/<theme>`.
|
|
6
|
+
* @example
|
|
7
|
+
* import a11yDark from "svelte-highlight/styles/a11y-dark";
|
|
8
|
+
* @type {string}
|
|
9
|
+
*/
|
|
10
|
+
export let theme = undefined;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Light theme CSS. With `dark`, overrides `theme`.
|
|
14
|
+
* @type {string | undefined}
|
|
15
|
+
*/
|
|
16
|
+
export let light = undefined;
|
|
17
|
+
|
|
18
|
+
/** Dark theme CSS; pair with `light`. */
|
|
19
|
+
export let dark = undefined;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Theme switch: `"auto"` | `"light"` | `"dark"` | CSS selector for dark.
|
|
23
|
+
* @type {"auto" | "light" | "dark" | string}
|
|
24
|
+
*/
|
|
25
|
+
export let mode = "auto";
|
|
26
|
+
|
|
27
|
+
/** Scope class for prefixed selectors. */
|
|
28
|
+
export let scopeClass = scopeClassFor(theme ?? `${light}${dark}`);
|
|
29
|
+
|
|
30
|
+
$: style =
|
|
31
|
+
light !== undefined && dark !== undefined
|
|
32
|
+
? dualStyle(light, dark, scopeClass, mode)
|
|
33
|
+
: scopeStyle(theme, scopeClass);
|
|
34
|
+
</script>
|
|
35
|
+
|
|
36
|
+
<svelte:head> {@html style} </svelte:head>
|
|
37
|
+
|
|
38
|
+
<div class={scopeClass} {...$$restProps}>
|
|
39
|
+
<slot {scopeClass} />
|
|
40
|
+
</div>
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { SvelteComponentTyped } from "svelte";
|
|
2
|
+
import type { HTMLAttributes } from "svelte/elements";
|
|
3
|
+
|
|
4
|
+
export type HighlightStyleProps = HTMLAttributes<HTMLDivElement> & {
|
|
5
|
+
/**
|
|
6
|
+
* Theme CSS from `svelte-highlight/styles/<theme>`.
|
|
7
|
+
* @example
|
|
8
|
+
* import a11yDark from "svelte-highlight/styles/a11y-dark";
|
|
9
|
+
*/
|
|
10
|
+
theme?: string;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Light theme CSS; pair with `dark` to emit both stylesheets and switch
|
|
14
|
+
* automatically. Takes precedence over `theme` when both `light` and `dark`
|
|
15
|
+
* are provided.
|
|
16
|
+
*/
|
|
17
|
+
light?: string;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Dark theme CSS; pair with `light` to emit both stylesheets.
|
|
21
|
+
*/
|
|
22
|
+
dark?: string;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* How to switch between `light` and `dark`:
|
|
26
|
+
* `"auto"` uses `prefers-color-scheme` media queries, `"light"`/`"dark"`
|
|
27
|
+
* force a single theme, and any other string is a CSS selector that gates
|
|
28
|
+
* the dark block (e.g. `[data-theme="dark"]`).
|
|
29
|
+
* @default "auto"
|
|
30
|
+
*/
|
|
31
|
+
mode?: "auto" | "light" | "dark" | (string & {});
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Wrapper class the scoped selectors target.
|
|
35
|
+
* @default hash of `theme`
|
|
36
|
+
*/
|
|
37
|
+
scopeClass?: string;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export type HighlightStyleSlots = {
|
|
41
|
+
default: {
|
|
42
|
+
scopeClass: string;
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export default class HighlightStyle extends SvelteComponentTyped<
|
|
47
|
+
HighlightStyleProps,
|
|
48
|
+
Record<string, never>,
|
|
49
|
+
HighlightStyleSlots
|
|
50
|
+
> {}
|
package/LangTag.svelte
CHANGED
|
@@ -12,7 +12,16 @@
|
|
|
12
12
|
export let langtag = false;
|
|
13
13
|
</script>
|
|
14
14
|
|
|
15
|
-
<pre
|
|
15
|
+
<pre
|
|
16
|
+
class:langtag={langtag}
|
|
17
|
+
data-language={languageName}
|
|
18
|
+
style:overflow-x="var(--overflow-x, auto)"
|
|
19
|
+
style:overflow-y="var(--overflow-y, auto)"
|
|
20
|
+
style:border-radius="var(--border-radius, 0)"
|
|
21
|
+
style:width="var(--width, auto)"
|
|
22
|
+
style:max-width="var(--max-width, none)"
|
|
23
|
+
{...$$restProps}
|
|
24
|
+
><code
|
|
16
25
|
class:hljs={true}
|
|
17
26
|
>{#if highlighted}{@html highlighted}{:else}{code}{/if}</code
|
|
18
27
|
></pre>
|
package/LangTag.svelte.d.ts
CHANGED
|
@@ -6,17 +6,17 @@ import type { LanguageName } from "./languages";
|
|
|
6
6
|
export type LangTagProps = HTMLAttributes<HTMLPreElement> &
|
|
7
7
|
LangtagProps & {
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
9
|
+
* Code to highlight.
|
|
10
10
|
*/
|
|
11
11
|
code: any;
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
|
-
*
|
|
14
|
+
* Highlighted HTML.
|
|
15
15
|
*/
|
|
16
16
|
highlighted: string;
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
|
-
*
|
|
19
|
+
* Language name.
|
|
20
20
|
* @default "plaintext"
|
|
21
21
|
*/
|
|
22
22
|
languageName?: LanguageName | (string & {});
|
package/LineNumbers.svelte
CHANGED
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
const HIGHLIGHTED_BACKGROUND = "rgba(254, 241, 96, 0.2)";
|
|
26
26
|
|
|
27
27
|
$: lines = highlighted.split("\n");
|
|
28
|
+
$: focusMode = highlightedLines.length > 0;
|
|
28
29
|
$: len_digits = lines.length.toString().length;
|
|
29
30
|
$: len = len_digits - MIN_DIGITS < 1 ? MIN_DIGITS : len_digits;
|
|
30
31
|
$: width = len * DIGIT_WIDTH;
|
|
@@ -33,14 +34,19 @@
|
|
|
33
34
|
<div
|
|
34
35
|
class:langtag={langtag}
|
|
35
36
|
data-language={languageName}
|
|
36
|
-
style:overflow-x="auto"
|
|
37
|
+
style:overflow-x="var(--overflow-x, auto)"
|
|
38
|
+
style:overflow-y="var(--overflow-y, auto)"
|
|
39
|
+
style:border-radius="var(--border-radius, 0)"
|
|
40
|
+
style:width="var(--width, auto)"
|
|
41
|
+
style:max-width="var(--max-width, none)"
|
|
37
42
|
{...$$restProps}
|
|
38
43
|
>
|
|
39
44
|
<table>
|
|
40
45
|
<tbody class:hljs={true}>
|
|
41
46
|
{#each lines as line, i}
|
|
42
47
|
{@const lineNumber = i + startingLineNumber}
|
|
43
|
-
|
|
48
|
+
{@const isHighlighted = highlightedLines.includes(i)}
|
|
49
|
+
<tr class:dimmed={focusMode && !isHighlighted}>
|
|
44
50
|
<td
|
|
45
51
|
class:hljs={true}
|
|
46
52
|
class:hideBorder
|
|
@@ -53,7 +59,7 @@
|
|
|
53
59
|
<code style:color="var(--line-number-color, currentColor)">
|
|
54
60
|
{lineNumber}
|
|
55
61
|
</code>
|
|
56
|
-
{#if
|
|
62
|
+
{#if isHighlighted}
|
|
57
63
|
<div
|
|
58
64
|
class:line-background={true}
|
|
59
65
|
style:background="var(--highlighted-background, {HIGHLIGHTED_BACKGROUND})"
|
|
@@ -62,7 +68,7 @@
|
|
|
62
68
|
</td>
|
|
63
69
|
<td>
|
|
64
70
|
<pre class:wrapLines><code>{@html line || "\n"}</code></pre>
|
|
65
|
-
{#if
|
|
71
|
+
{#if isHighlighted}
|
|
66
72
|
<div
|
|
67
73
|
class:line-background={true}
|
|
68
74
|
style:background="var(--highlighted-background, {HIGHLIGHTED_BACKGROUND})"
|
|
@@ -110,8 +116,8 @@
|
|
|
110
116
|
}
|
|
111
117
|
|
|
112
118
|
td {
|
|
113
|
-
padding-left: var(--padding-left, 1em);
|
|
114
|
-
padding-right: var(--padding-right, 1em);
|
|
119
|
+
padding-left: var(--padding-left, var(--padding, 1em));
|
|
120
|
+
padding-right: var(--padding-right, var(--padding, 1em));
|
|
115
121
|
}
|
|
116
122
|
|
|
117
123
|
td.hljs:not(.hideBorder):after {
|
|
@@ -160,4 +166,10 @@
|
|
|
160
166
|
tr:last-of-type td .line-background {
|
|
161
167
|
bottom: 1em;
|
|
162
168
|
}
|
|
169
|
+
|
|
170
|
+
tr.dimmed td > code,
|
|
171
|
+
tr.dimmed pre {
|
|
172
|
+
opacity: var(--unhighlighted-opacity, 1);
|
|
173
|
+
filter: var(--unhighlighted-filter, none);
|
|
174
|
+
}
|
|
163
175
|
</style>
|