svelte-highlight 7.22.0 → 7.23.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 +6 -30
- package/FileTabs.svelte +3 -1
- package/HighlightEditable.svelte +115 -143
- package/HighlightStream.svelte +44 -48
- package/HighlightStyle.svelte +9 -21
- package/HighlightVirtual.svelte +82 -4
- package/HighlightVirtual.svelte.d.ts +19 -2
- package/MarkdownStream.svelte +16 -9
- package/README.md +27 -7
- package/ansi-color.js +109 -22
- package/ansi-output.js +93 -0
- package/ansi.d.ts +18 -0
- package/ansi.js +85 -24
- package/css-walk.js +9 -12
- package/editable-css-paint.js +118 -0
- package/editable-dom-paint.js +217 -146
- package/editable-line-dom.js +241 -0
- package/engine.d.ts +28 -6
- package/engine.js +707 -144
- package/fence.d.ts +5 -5
- package/fence.js +196 -56
- package/highlight-theme.js +10 -4
- package/incremental-tokenize.js +95 -17
- package/package.json +1 -1
- package/preprocessor.js +16 -2
- package/scoped.js +15 -13
- package/search.d.ts +6 -4
- package/search.js +427 -85
- package/stream-final-highlight.js +52 -0
- package/stream-highlighted.js +17 -0
- package/stream-preview.js +7 -2
- package/stream-regenerate.js +262 -0
- package/style-registry.js +60 -0
- package/text-diff.js +66 -18
- package/tokenized-document.d.ts +8 -0
- package/tokenized-document.js +60 -15
- package/worker.js +14 -2
package/editable-dom-paint.js
CHANGED
|
@@ -3,25 +3,15 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Tokenization via `reparseIncremental` is already incremental, but the
|
|
5
5
|
* default `"dom"` engine still ran `renderHtml` + `splitLines` over the full
|
|
6
|
-
* event stream every keystroke.
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* and O(n) total to type a document of size n.
|
|
6
|
+
* event stream every keystroke. Now only the first paint does; every edit
|
|
7
|
+
* after it, append or mid-document, re-renders only the lines whose events
|
|
8
|
+
* changed (`patchLineHtml`).
|
|
10
9
|
*/
|
|
11
10
|
|
|
12
|
-
import { extendLines, renderHtml } from "./engine.js";
|
|
11
|
+
import { CLOSE, extendLines, OPEN, renderHtml, TEXT } from "./engine.js";
|
|
12
|
+
import { sharedEvents } from "./incremental-tokenize.js";
|
|
13
13
|
import { splitLines } from "./split-lines.js";
|
|
14
14
|
|
|
15
|
-
/**
|
|
16
|
-
* @param {string} previousCode
|
|
17
|
-
* @param {string} nextCode
|
|
18
|
-
*/
|
|
19
|
-
export function isPureAppend(previousCode, nextCode) {
|
|
20
|
-
return (
|
|
21
|
-
nextCode.length >= previousCode.length && nextCode.startsWith(previousCode)
|
|
22
|
-
);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
15
|
/**
|
|
26
16
|
* Full line-HTML paint via `renderHtml` + `splitLines` (the historical path).
|
|
27
17
|
* @param {import("./engine.d.ts").ScopeEvent[]} events
|
|
@@ -57,161 +47,242 @@ export function linesFromStreamState(completedLines, previewLines, code) {
|
|
|
57
47
|
}
|
|
58
48
|
|
|
59
49
|
/**
|
|
60
|
-
* @typedef {import("./engine.d.ts").Registry} Registry
|
|
61
50
|
* @typedef {import("./engine.d.ts").ScopeEvent} ScopeEvent
|
|
62
|
-
* @typedef {import("./
|
|
51
|
+
* @typedef {import("./incremental-tokenize.js").EventReuse} EventReuse
|
|
52
|
+
*/
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Number of "\n" in `code` within `[from, end)`.
|
|
56
|
+
* @param {string} code
|
|
57
|
+
* @param {number} from
|
|
58
|
+
* @param {number} end
|
|
63
59
|
*/
|
|
60
|
+
function countNewlines(code, from, end) {
|
|
61
|
+
let count = 0;
|
|
62
|
+
for (let i = code.indexOf("\n", from); i !== -1 && i < end; ) {
|
|
63
|
+
count++;
|
|
64
|
+
i = code.indexOf("\n", i + 1);
|
|
65
|
+
}
|
|
66
|
+
return count;
|
|
67
|
+
}
|
|
64
68
|
|
|
65
69
|
/**
|
|
66
|
-
*
|
|
70
|
+
* Where `patchLineHtml`'s walk to the first changed line ended: the state
|
|
71
|
+
* after the first `index` events of `events`. Valid for a later call whose
|
|
72
|
+
* `prevEvents` is `events`, while `index` stays inside the unchanged prefix.
|
|
73
|
+
* @typedef {{
|
|
74
|
+
* events: ScopeEvent[] | undefined,
|
|
75
|
+
* index: number,
|
|
76
|
+
* stack: string[],
|
|
77
|
+
* offset: number,
|
|
78
|
+
* newlines: number,
|
|
79
|
+
* }} PatchMemo
|
|
80
|
+
*/
|
|
81
|
+
|
|
82
|
+
/** @returns {PatchMemo} */
|
|
83
|
+
export function createPatchMemo() {
|
|
84
|
+
return { events: undefined, index: 0, stack: [], offset: 0, newlines: 0 };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Same result as `lineHtmlFromEvents(events, code)`, given `prevLines` =
|
|
89
|
+
* `lineHtmlFromEvents(prevEvents, prevCode)`, but only re-renders the lines
|
|
90
|
+
* whose events changed and reuses `prevLines`' strings for the rest.
|
|
91
|
+
*
|
|
92
|
+
* `reparseIncremental` keeps the unchanged head and converged tail of the
|
|
93
|
+
* event stream as the same event objects, so the changed region is found
|
|
94
|
+
* by identity: a common prefix and suffix of `===` events. Events are
|
|
95
|
+
* immutable, and a line's HTML depends only on its own events plus the
|
|
96
|
+
* scopes open at its start, so:
|
|
97
|
+
* - every line that ends before the first changed event is unchanged;
|
|
98
|
+
* - every line after the first "\n" in the common suffix is unchanged too,
|
|
99
|
+
* if the same scopes are open at that "\n" on both sides (checked).
|
|
100
|
+
* Only the lines between are rendered, via `extendLines` from the scopes
|
|
101
|
+
* open at the first changed line's start. Mid-document typing then costs
|
|
102
|
+
* pointer compares and a stack walk over the document instead of
|
|
103
|
+
* `renderHtml` + `splitLines` over it: see dom-paint.bench.ts's
|
|
104
|
+
* mid-document group.
|
|
105
|
+
*
|
|
106
|
+
* Pass the same `memo` across consecutive calls to resume that walk from
|
|
107
|
+
* where the last call's ended, so repeated edits near one spot don't
|
|
108
|
+
* re-walk the document before it.
|
|
67
109
|
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
110
|
+
* Pass `reparseIncremental`'s `reuse` (when its `from` is `prevEvents`) to
|
|
111
|
+
* take the common prefix and suffix from it instead of comparing the two
|
|
112
|
+
* event arrays, which is O(document) per call.
|
|
113
|
+
*
|
|
114
|
+
* @param {ScopeEvent[]} prevEvents
|
|
115
|
+
* @param {string[]} prevLines
|
|
116
|
+
* @param {ScopeEvent[]} events
|
|
117
|
+
* @param {string} code
|
|
118
|
+
* @param {PatchMemo} [memo]
|
|
119
|
+
* @param {EventReuse} [reuse]
|
|
120
|
+
* @returns {string[]}
|
|
71
121
|
*/
|
|
72
|
-
export function
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
* @returns {StreamSession}
|
|
96
|
-
*/
|
|
97
|
-
function resetSession(nextLanguage) {
|
|
98
|
-
session = registry.createSession(nextLanguage);
|
|
99
|
-
sessionLanguage = nextLanguage;
|
|
100
|
-
fedCode = "";
|
|
101
|
-
renderedEventCount = 0;
|
|
102
|
-
openScopes = [];
|
|
103
|
-
pendingHtml = "";
|
|
104
|
-
completedLines = [];
|
|
105
|
-
hasAppended = false;
|
|
106
|
-
needsResync = false;
|
|
107
|
-
return session;
|
|
122
|
+
export function patchLineHtml(
|
|
123
|
+
prevEvents,
|
|
124
|
+
prevLines,
|
|
125
|
+
events,
|
|
126
|
+
code,
|
|
127
|
+
memo,
|
|
128
|
+
reuse,
|
|
129
|
+
) {
|
|
130
|
+
if (events === prevEvents) return prevLines;
|
|
131
|
+
const prevCount = prevEvents.length;
|
|
132
|
+
const count = events.length;
|
|
133
|
+
const { prefix, suffix } = sharedEvents(prevEvents, events, reuse);
|
|
134
|
+
if (prefix === prevCount && prefix === count) return prevLines;
|
|
135
|
+
|
|
136
|
+
// The first changed line starts after the last "\n" before `prefix`.
|
|
137
|
+
let breakEvent = prefix - 1;
|
|
138
|
+
let breakAt = -1;
|
|
139
|
+
for (; breakEvent >= 0; breakEvent--) {
|
|
140
|
+
const event = /** @type {ScopeEvent} */ (events[breakEvent]);
|
|
141
|
+
if (event.t === TEXT) {
|
|
142
|
+
breakAt = event.v.lastIndexOf("\n");
|
|
143
|
+
if (breakAt !== -1) break;
|
|
144
|
+
}
|
|
108
145
|
}
|
|
109
146
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
147
|
+
// Scopes open at that line's start, and its offset into `code`, walked
|
|
148
|
+
// from the memo when it still lies in the unchanged prefix.
|
|
149
|
+
const resume =
|
|
150
|
+
memo !== undefined && memo.events === prevEvents && memo.index <= breakEvent
|
|
151
|
+
? memo
|
|
152
|
+
: undefined;
|
|
153
|
+
/** @type {string[]} */
|
|
154
|
+
const stack = resume ? resume.stack.slice() : [];
|
|
155
|
+
const walkFrom = resume ? resume.offset : 0;
|
|
156
|
+
let offset = walkFrom;
|
|
157
|
+
for (let i = resume ? resume.index : 0; i < breakEvent; i++) {
|
|
158
|
+
const event = /** @type {ScopeEvent} */ (events[i]);
|
|
159
|
+
if (event.t === OPEN) stack.push(event.s);
|
|
160
|
+
else if (event.t === CLOSE) stack.pop();
|
|
161
|
+
else offset += event.v.length;
|
|
162
|
+
}
|
|
163
|
+
let firstLine = 0;
|
|
164
|
+
if (breakEvent >= 0) {
|
|
165
|
+
const newlines =
|
|
166
|
+
(resume ? resume.newlines : 0) + countNewlines(code, walkFrom, offset);
|
|
167
|
+
firstLine = newlines + countNewlines(code, offset, offset + breakAt + 1);
|
|
168
|
+
if (memo !== undefined) {
|
|
169
|
+
memo.events = events;
|
|
170
|
+
memo.index = breakEvent;
|
|
171
|
+
memo.stack = stack.slice();
|
|
172
|
+
memo.offset = offset;
|
|
173
|
+
memo.newlines = newlines;
|
|
122
174
|
}
|
|
123
|
-
fedCode = base;
|
|
124
175
|
}
|
|
125
176
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
committed.slice(renderedEventCount),
|
|
133
|
-
openScopes,
|
|
134
|
-
pendingHtml,
|
|
135
|
-
);
|
|
136
|
-
// push, not concat: concat copies the whole (ever-growing) array on
|
|
137
|
-
// every call, which is O(lines) per completed line - push is O(1)
|
|
138
|
-
// amortized (same reasoning as HighlightStream's sealed-chunk append).
|
|
139
|
-
for (const line of result.completedLines) completedLines.push(line);
|
|
140
|
-
openScopes = result.openScopes;
|
|
141
|
-
pendingHtml = result.pendingHtml;
|
|
142
|
-
renderedEventCount = committed.length;
|
|
177
|
+
// Re-render from the line start: reopen its scopes, then its text.
|
|
178
|
+
/** @type {ScopeEvent[]} */
|
|
179
|
+
const region = stack.map((s) => ({ t: OPEN, s }));
|
|
180
|
+
if (breakEvent >= 0) {
|
|
181
|
+
const event = /** @type {{ v: string }} */ (events[breakEvent]);
|
|
182
|
+
region.push({ t: TEXT, v: event.v.slice(breakAt + 1) });
|
|
143
183
|
}
|
|
144
184
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
if (
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
185
|
+
// The first "\n" in the common suffix ends the last line to re-render.
|
|
186
|
+
let endEvent = count - suffix;
|
|
187
|
+
let endAt = -1;
|
|
188
|
+
for (; endEvent < count; endEvent++) {
|
|
189
|
+
const event = /** @type {ScopeEvent} */ (events[endEvent]);
|
|
190
|
+
if (event.t === TEXT) {
|
|
191
|
+
endAt = event.v.indexOf("\n");
|
|
192
|
+
if (endAt !== -1) break;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (endAt !== -1) {
|
|
197
|
+
for (let i = breakEvent + 1; i < endEvent; i++) {
|
|
198
|
+
region.push(/** @type {ScopeEvent} */ (events[i]));
|
|
199
|
+
}
|
|
200
|
+
const event = /** @type {{ v: string }} */ (events[endEvent]);
|
|
201
|
+
region.push({ t: TEXT, v: event.v.slice(0, endAt + 1) });
|
|
202
|
+
const rendered = extendLines(region, [], "");
|
|
203
|
+
|
|
204
|
+
// Walk the old side of the same span: the scopes open at that "\n",
|
|
205
|
+
// and how many old lines the re-rendered ones replace.
|
|
206
|
+
const prevStack = stack.slice();
|
|
207
|
+
let prevLineCount = 1;
|
|
208
|
+
const prevEndEvent = endEvent - count + prevCount;
|
|
209
|
+
for (let i = breakEvent + 1; i < prevEndEvent; i++) {
|
|
210
|
+
const prev = /** @type {ScopeEvent} */ (prevEvents[i]);
|
|
211
|
+
if (prev.t === OPEN) prevStack.push(prev.s);
|
|
212
|
+
else if (prev.t === CLOSE) prevStack.pop();
|
|
213
|
+
else prevLineCount += countNewlines(prev.v, 0, prev.v.length);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const openScopes = rendered.openScopes;
|
|
217
|
+
let sameScopes = openScopes.length === prevStack.length;
|
|
218
|
+
for (let i = 0; sameScopes && i < openScopes.length; i++) {
|
|
219
|
+
sameScopes = openScopes[i] === prevStack[i];
|
|
158
220
|
}
|
|
159
|
-
|
|
221
|
+
if (sameScopes) {
|
|
222
|
+
return prevLines
|
|
223
|
+
.slice(0, firstLine)
|
|
224
|
+
.concat(
|
|
225
|
+
rendered.completedLines,
|
|
226
|
+
prevLines.slice(firstLine + prevLineCount),
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
// Different scopes reach the tail: re-render through the end instead.
|
|
230
|
+
region.length = stack.length + (breakEvent >= 0 ? 1 : 0);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
for (let i = breakEvent + 1; i < count; i++) {
|
|
234
|
+
region.push(/** @type {ScopeEvent} */ (events[i]));
|
|
160
235
|
}
|
|
236
|
+
const rendered = extendLines(region, [], "");
|
|
237
|
+
return linesFromStreamState(
|
|
238
|
+
prevLines.slice(0, firstLine).concat(rendered.completedLines),
|
|
239
|
+
[rendered.pendingHtml],
|
|
240
|
+
code,
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Stateful line-HTML painter: the first paint (and any paint after a
|
|
246
|
+
* language change) renders the whole document via `lineHtmlFromEvents`;
|
|
247
|
+
* every later one patches the previous lines via `patchLineHtml`.
|
|
248
|
+
*
|
|
249
|
+
* Appends used to go through a separate stream session instead, which
|
|
250
|
+
* tokenized the document a second time at mount and again after every
|
|
251
|
+
* switch from mid-document editing back to appending. With
|
|
252
|
+
* `reparseIncremental`'s `reuse`, the patch path no longer compares the
|
|
253
|
+
* event arrays, so it costs about the same for appends and needs no
|
|
254
|
+
* second tokenizer: see dom-paint.bench.ts.
|
|
255
|
+
*/
|
|
256
|
+
export function createDomLinePainter() {
|
|
257
|
+
let language = "";
|
|
258
|
+
/** @type {ScopeEvent[] | undefined} */
|
|
259
|
+
let prevEvents;
|
|
260
|
+
/** @type {string[]} */
|
|
261
|
+
let prevLines = [];
|
|
262
|
+
const memo = createPatchMemo();
|
|
161
263
|
|
|
162
264
|
return {
|
|
163
265
|
reset() {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
openScopes = [];
|
|
169
|
-
pendingHtml = "";
|
|
170
|
-
completedLines = [];
|
|
171
|
-
lastUsedIncremental = false;
|
|
172
|
-
hasAppended = false;
|
|
173
|
-
needsResync = false;
|
|
174
|
-
},
|
|
175
|
-
lastUsedIncremental() {
|
|
176
|
-
return lastUsedIncremental;
|
|
266
|
+
language = "";
|
|
267
|
+
prevEvents = undefined;
|
|
268
|
+
prevLines = [];
|
|
269
|
+
Object.assign(memo, createPatchMemo());
|
|
177
270
|
},
|
|
178
271
|
/**
|
|
179
|
-
* @param {ScopeEvent[]} events
|
|
180
|
-
* for the non-append fallback path (mid-document edits).
|
|
272
|
+
* @param {ScopeEvent[]} events
|
|
181
273
|
* @param {string} code
|
|
182
274
|
* @param {string} languageName
|
|
275
|
+
* @param {EventReuse} [reuse] `reparseIncremental`'s result `reuse`
|
|
183
276
|
* @returns {string[]}
|
|
184
277
|
*/
|
|
185
|
-
paint(events, code, languageName) {
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
// Defer the O(n) stream resync until the next pure append.
|
|
194
|
-
needsResync = true;
|
|
195
|
-
fedCode = code;
|
|
196
|
-
return lines;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
if (needsResync) {
|
|
200
|
-
resyncSession(languageName, fedCode);
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
const hadContent = hasAppended;
|
|
204
|
-
if (code.length > fedCode.length) {
|
|
205
|
-
session.append(code.slice(fedCode.length));
|
|
206
|
-
fedCode = code;
|
|
207
|
-
hasAppended = true;
|
|
208
|
-
consumeCommitted(session.events());
|
|
209
|
-
} else if (code.length === 0 && fedCode.length === 0) {
|
|
210
|
-
// Empty document paint.
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
lastUsedIncremental = hadContent;
|
|
214
|
-
return linesForCurrentCode(code);
|
|
278
|
+
paint(events, code, languageName, reuse) {
|
|
279
|
+
prevLines =
|
|
280
|
+
prevEvents === undefined || languageName !== language
|
|
281
|
+
? lineHtmlFromEvents(events, code)
|
|
282
|
+
: patchLineHtml(prevEvents, prevLines, events, code, memo, reuse);
|
|
283
|
+
prevEvents = events;
|
|
284
|
+
language = languageName;
|
|
285
|
+
return prevLines;
|
|
215
286
|
},
|
|
216
287
|
};
|
|
217
288
|
}
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Line-element DOM helpers for HighlightEditable, kept out of the component
|
|
3
|
+
* so they can be benchmarked and unit-tested against a fake DOM (see
|
|
4
|
+
* line-dom.bench.ts).
|
|
5
|
+
*
|
|
6
|
+
* The editor holds one `<span>` per line, joined by literal "\n" text nodes
|
|
7
|
+
* so caret offset math stays identical to a flat paint.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** `NodeFilter.SHOW_TEXT`, inlined so this module loads outside a browser. */
|
|
11
|
+
const SHOW_TEXT = 4;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The editor's line elements and what they currently show.
|
|
15
|
+
* @typedef {{
|
|
16
|
+
* lineEls: HTMLElement[],
|
|
17
|
+
* lineLengths: number[],
|
|
18
|
+
* renderedLines: string[],
|
|
19
|
+
* }} LineView
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Callbacks for state kept per line outside the view (the css-highlights
|
|
24
|
+
* engine's Highlight ranges): `onReset` after a full rebuild, `onSplice`
|
|
25
|
+
* after lines `[index, index + removed)` were replaced by `inserted` new
|
|
26
|
+
* line elements.
|
|
27
|
+
* @typedef {{
|
|
28
|
+
* onReset: () => void,
|
|
29
|
+
* onSplice: (index: number, removed: number, inserted: number) => void,
|
|
30
|
+
* }} LineHooks
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
/** @returns {LineView} */
|
|
34
|
+
export function createLineView() {
|
|
35
|
+
return { lineEls: [], lineLengths: [], renderedLines: [] };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Character offset (in the editor's flat text) where line `index` starts.
|
|
40
|
+
* @param {LineView} view
|
|
41
|
+
* @param {number} index
|
|
42
|
+
*/
|
|
43
|
+
export function lineStartOffset(view, index) {
|
|
44
|
+
let start = index; // one "\n" separator per preceding line
|
|
45
|
+
for (let i = 0; i < index; i++)
|
|
46
|
+
start += /** @type {number} */ (view.lineLengths[i]);
|
|
47
|
+
return start;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* `array.splice(index, removed, ...items)`, without spreading a huge `items`
|
|
52
|
+
* (a large paste) into call arguments.
|
|
53
|
+
* @template T
|
|
54
|
+
* @param {T[]} array
|
|
55
|
+
* @param {number} index
|
|
56
|
+
* @param {number} removed
|
|
57
|
+
* @param {T[]} items
|
|
58
|
+
* @returns {T[]}
|
|
59
|
+
*/
|
|
60
|
+
function spliceIn(array, index, removed, items) {
|
|
61
|
+
if (items.length <= 1024) {
|
|
62
|
+
array.splice(index, removed, ...items);
|
|
63
|
+
return array;
|
|
64
|
+
}
|
|
65
|
+
return array.slice(0, index).concat(items, array.slice(index + removed));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Patches `editor` to match `lines`. Only lines whose content actually
|
|
70
|
+
* changed touch the DOM (assigned via `setContent`: innerHTML for the "dom"
|
|
71
|
+
* engine, textContent for "css-highlights"). Returns the index of the
|
|
72
|
+
* single changed line when nothing else shifted (used to scope caret
|
|
73
|
+
* restoration), or null.
|
|
74
|
+
*
|
|
75
|
+
* Lines are matched from both ends, not by index: the common leading and
|
|
76
|
+
* trailing runs keep their elements, and only the middle is rewritten,
|
|
77
|
+
* inserted, or removed. Enter or Backspace across a line break mid-document
|
|
78
|
+
* then writes one line and inserts or removes one, instead of rewriting
|
|
79
|
+
* every line below the edit: see line-dom.bench.ts.
|
|
80
|
+
* @param {HTMLElement} editor
|
|
81
|
+
* @param {LineView} view
|
|
82
|
+
* @param {string[]} lines
|
|
83
|
+
* @param {(el: HTMLElement, line: string) => void} setContent
|
|
84
|
+
* @param {LineHooks} hooks
|
|
85
|
+
* @returns {number | null}
|
|
86
|
+
*/
|
|
87
|
+
export function renderLines(editor, view, lines, setContent, hooks) {
|
|
88
|
+
const doc = /** @type {Document} */ (editor.ownerDocument);
|
|
89
|
+
// Some browsers can place a native selection boundary just outside a
|
|
90
|
+
// line's <span> (e.g. Firefox collapsing a select-all there); typing at
|
|
91
|
+
// that point inserts a stray sibling text node our diffing never touches.
|
|
92
|
+
// Detect the drift by child count and self-heal with a full rebuild.
|
|
93
|
+
const expectedChildren =
|
|
94
|
+
view.lineEls.length === 0 ? 0 : view.lineEls.length * 2 - 1;
|
|
95
|
+
if (editor.childNodes.length !== expectedChildren) {
|
|
96
|
+
editor.textContent = "";
|
|
97
|
+
view.lineEls = [];
|
|
98
|
+
view.lineLengths = [];
|
|
99
|
+
view.renderedLines = [];
|
|
100
|
+
hooks.onReset();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const { lineEls, lineLengths, renderedLines } = view;
|
|
104
|
+
const prevLen = lineEls.length;
|
|
105
|
+
const newLen = lines.length;
|
|
106
|
+
const shared = Math.min(prevLen, newLen);
|
|
107
|
+
|
|
108
|
+
// `patchLineHtml` reuses unchanged lines' strings, so most of these
|
|
109
|
+
// compares end at a pointer check.
|
|
110
|
+
let head = 0;
|
|
111
|
+
while (head < shared && renderedLines[head] === lines[head]) head++;
|
|
112
|
+
let tail = 0;
|
|
113
|
+
while (
|
|
114
|
+
tail < shared - head &&
|
|
115
|
+
renderedLines[prevLen - 1 - tail] === lines[newLen - 1 - tail]
|
|
116
|
+
) {
|
|
117
|
+
tail++;
|
|
118
|
+
}
|
|
119
|
+
const prevEnd = prevLen - tail;
|
|
120
|
+
const newEnd = newLen - tail;
|
|
121
|
+
const pairedEnd = Math.min(prevEnd, newEnd);
|
|
122
|
+
|
|
123
|
+
// Lines present on both sides of the middle reuse their element.
|
|
124
|
+
let changedIndex = null;
|
|
125
|
+
let changedCount = 0;
|
|
126
|
+
for (let i = head; i < pairedEnd; i++) {
|
|
127
|
+
if (renderedLines[i] === lines[i]) continue;
|
|
128
|
+
const el = /** @type {HTMLElement} */ (lineEls[i]);
|
|
129
|
+
setContent(el, /** @type {string} */ (lines[i]));
|
|
130
|
+
lineLengths[i] = /** @type {string} */ (el.textContent).length;
|
|
131
|
+
changedIndex = i;
|
|
132
|
+
changedCount++;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (newEnd > pairedEnd) {
|
|
136
|
+
// Insert the extra lines before the first trailing line, or append.
|
|
137
|
+
const before = pairedEnd < prevLen ? lineEls[pairedEnd] : null;
|
|
138
|
+
/** @type {HTMLElement[]} */
|
|
139
|
+
const spans = [];
|
|
140
|
+
/** @type {number[]} */
|
|
141
|
+
const lengths = [];
|
|
142
|
+
for (let i = pairedEnd; i < newEnd; i++) {
|
|
143
|
+
const span = doc.createElement("span");
|
|
144
|
+
setContent(span, /** @type {string} */ (lines[i]));
|
|
145
|
+
if (before) {
|
|
146
|
+
editor.insertBefore(span, before);
|
|
147
|
+
editor.insertBefore(doc.createTextNode("\n"), before);
|
|
148
|
+
} else {
|
|
149
|
+
if (i > 0) editor.appendChild(doc.createTextNode("\n"));
|
|
150
|
+
editor.appendChild(span);
|
|
151
|
+
}
|
|
152
|
+
spans.push(span);
|
|
153
|
+
lengths.push(/** @type {string} */ (span.textContent).length);
|
|
154
|
+
}
|
|
155
|
+
view.lineEls = spliceIn(lineEls, pairedEnd, 0, spans);
|
|
156
|
+
view.lineLengths = spliceIn(lineLengths, pairedEnd, 0, lengths);
|
|
157
|
+
hooks.onSplice(pairedEnd, 0, spans.length);
|
|
158
|
+
} else if (prevEnd > pairedEnd) {
|
|
159
|
+
// Drop the surplus lines, each with one adjacent separator: the one
|
|
160
|
+
// after it, or before it for the last line.
|
|
161
|
+
for (let i = pairedEnd; i < prevEnd; i++) {
|
|
162
|
+
const el = /** @type {HTMLElement} */ (lineEls[i]);
|
|
163
|
+
const separator = el.nextSibling ?? el.previousSibling;
|
|
164
|
+
editor.removeChild(el);
|
|
165
|
+
if (separator) editor.removeChild(separator);
|
|
166
|
+
}
|
|
167
|
+
lineEls.splice(pairedEnd, prevEnd - pairedEnd);
|
|
168
|
+
lineLengths.splice(pairedEnd, prevEnd - pairedEnd);
|
|
169
|
+
hooks.onSplice(pairedEnd, prevEnd - pairedEnd, 0);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
view.renderedLines = lines;
|
|
173
|
+
return prevLen === newLen && changedCount === 1 ? changedIndex : null;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* The text node and offset within it at character `offset` of `root`'s
|
|
178
|
+
* text, or null past the end.
|
|
179
|
+
* @param {Node} root
|
|
180
|
+
* @param {number} offset
|
|
181
|
+
* @returns {{ node: Node, offset: number } | null}
|
|
182
|
+
*/
|
|
183
|
+
export function nodeAtOffset(root, offset) {
|
|
184
|
+
const doc = /** @type {Document} */ (root.ownerDocument);
|
|
185
|
+
const walker = doc.createTreeWalker(root, SHOW_TEXT, null);
|
|
186
|
+
let count = 0;
|
|
187
|
+
for (let node = walker.nextNode(); node; node = walker.nextNode()) {
|
|
188
|
+
const next = count + /** @type {string} */ (node.textContent).length;
|
|
189
|
+
if (offset <= next) return { node, offset: offset - count };
|
|
190
|
+
count = next;
|
|
191
|
+
}
|
|
192
|
+
return null;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* `nodeAtOffset(editor, offset)` for an editor painted by `renderLines`,
|
|
197
|
+
* without walking every text node before the caret's line.
|
|
198
|
+
*
|
|
199
|
+
* Every text node before the separator that precedes line `k` ends at or
|
|
200
|
+
* before that line's start minus one, so for the last line `k` starting at
|
|
201
|
+
* or before `offset`, none of them can match: the walk can start at that
|
|
202
|
+
* separator with the count it would have reached there. The line is found
|
|
203
|
+
* from `lineLengths` (integer adds) instead of the DOM: see
|
|
204
|
+
* line-dom.bench.ts's Enter group.
|
|
205
|
+
* @param {HTMLElement} editor
|
|
206
|
+
* @param {LineView} view
|
|
207
|
+
* @param {number} offset
|
|
208
|
+
* @returns {{ node: Node, offset: number } | null}
|
|
209
|
+
*/
|
|
210
|
+
export function editorNodeAtOffset(editor, view, offset) {
|
|
211
|
+
const { lineEls, lineLengths } = view;
|
|
212
|
+
if (editor.childNodes.length !== lineEls.length * 2 - 1) {
|
|
213
|
+
return nodeAtOffset(editor, offset);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
let line = 0;
|
|
217
|
+
let lineStart = 0;
|
|
218
|
+
while (line + 1 < lineEls.length) {
|
|
219
|
+
const next = lineStart + /** @type {number} */ (lineLengths[line]) + 1;
|
|
220
|
+
if (next > offset) break;
|
|
221
|
+
lineStart = next;
|
|
222
|
+
line++;
|
|
223
|
+
}
|
|
224
|
+
if (line === 0) return nodeAtOffset(editor, offset);
|
|
225
|
+
|
|
226
|
+
const separator = /** @type {HTMLElement} */ (lineEls[line]).previousSibling;
|
|
227
|
+
if (separator?.nodeType !== 3) {
|
|
228
|
+
return nodeAtOffset(editor, offset);
|
|
229
|
+
}
|
|
230
|
+
const doc = /** @type {Document} */ (editor.ownerDocument);
|
|
231
|
+
const walker = doc.createTreeWalker(editor, SHOW_TEXT, null);
|
|
232
|
+
walker.currentNode = separator;
|
|
233
|
+
let count = lineStart - 1;
|
|
234
|
+
for (let node = /** @type {Node | null} */ (separator); node; ) {
|
|
235
|
+
const next = count + /** @type {string} */ (node.textContent).length;
|
|
236
|
+
if (offset <= next) return { node, offset: offset - count };
|
|
237
|
+
count = next;
|
|
238
|
+
node = walker.nextNode();
|
|
239
|
+
}
|
|
240
|
+
return null;
|
|
241
|
+
}
|