svelte-rtf-editor 1.13.0 → 1.15.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.
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Lists, as the editor makes them.
3
+ *
4
+ * The browsers' own list commands each make something else of the same click. Chromium and
5
+ * Safari leave the list inside the paragraph it was made from, which no HTML can say;
6
+ * Firefox starts a new list beside the one a line is next to, so that every line made an item
7
+ * one at a time is numbered 1; Chromium puts the caret at the start of the line. None of
8
+ * them nests an item under the one before it. So the document is changed here, the same way
9
+ * in every browser, and the editor's own history (history.ts) undoes it.
10
+ *
11
+ * The shape made is the one the RTF reader writes and the writer reads: a <ul> or <ol> of
12
+ * <li>, and a list inside a list as the last thing in the item it is under.
13
+ *
14
+ * <ol><li>Skin, left arm<ul><li>margins clear</li></ul></li><li>Lymph node</li></ol>
15
+ *
16
+ * Every function takes the editor's element and the selection, changes the document where
17
+ * there is something to change, puts the selection back in the words it was in, and returns
18
+ * whether it changed anything.
19
+ */
20
+ export type ListKind = 'ul' | 'ol';
21
+ /**
22
+ * The toolbar's list button. The lines the selection touches are made items of a list of
23
+ * this kind, and one list with a list of that kind they are next to. Where they are all
24
+ * items of such a list already, they are paragraphs again. Where they are items of the
25
+ * other kind: with a caret, the list the caret is in is made this kind; with words
26
+ * selected, the items selected are.
27
+ */
28
+ export declare function toggleList(root: HTMLElement, sel: Selection | null, kind: ListKind): boolean;
29
+ /** Which kind of list the selection is in: the innermost list of the line it begins in. */
30
+ export declare function listKindAt(root: HTMLElement, sel: Selection | null): ListKind | null;
31
+ /**
32
+ * Put the items the selection touches one list further in, each under the item before it.
33
+ * Returns false, with nothing changed, where the selection is not all items, or the first of
34
+ * them has no item before it to be under.
35
+ */
36
+ export declare function nestItems(root: HTMLElement, sel: Selection | null): boolean;
37
+ /** Put the items the selection touches that are in a list within a list one list further out. Returns false where none is. */
38
+ export declare function unnestItems(root: HTMLElement, sel: Selection | null): boolean;
39
+ /**
40
+ * The item the caret is in leaves its list by one step: one list further out, or, from a
41
+ * list that is in no other, a paragraph. (What Enter does in an item with nothing in it, and
42
+ * Backspace at the start of one.)
43
+ */
44
+ export declare function leaveList(root: HTMLElement, sel: Selection | null): boolean;
45
+ /** Whether the caret is in an item with nothing in it: no words, no picture. (One with a list under it has that.) */
46
+ export declare function inEmptyItem(root: HTMLElement, sel: Selection | null): boolean;
47
+ /** Whether the caret is at the very start of an item: nothing of the item before it. */
48
+ export declare function atItemStart(root: HTMLElement, sel: Selection | null): boolean;
package/dist/lists.js ADDED
@@ -0,0 +1,576 @@
1
+ /**
2
+ * Lists, as the editor makes them.
3
+ *
4
+ * The browsers' own list commands each make something else of the same click. Chromium and
5
+ * Safari leave the list inside the paragraph it was made from, which no HTML can say;
6
+ * Firefox starts a new list beside the one a line is next to, so that every line made an item
7
+ * one at a time is numbered 1; Chromium puts the caret at the start of the line. None of
8
+ * them nests an item under the one before it. So the document is changed here, the same way
9
+ * in every browser, and the editor's own history (history.ts) undoes it.
10
+ *
11
+ * The shape made is the one the RTF reader writes and the writer reads: a <ul> or <ol> of
12
+ * <li>, and a list inside a list as the last thing in the item it is under.
13
+ *
14
+ * <ol><li>Skin, left arm<ul><li>margins clear</li></ul></li><li>Lymph node</li></ol>
15
+ *
16
+ * Every function takes the editor's element and the selection, changes the document where
17
+ * there is something to change, puts the selection back in the words it was in, and returns
18
+ * whether it changed anything.
19
+ */
20
+ /** What ends a line: an element that is laid out as a block. */
21
+ const BLOCKS = 'ul, ol, li, div, p, table, thead, tbody, tfoot, tr, td, th, blockquote, pre, h1, h2, h3, h4, h5, h6, hr, figure, figcaption';
22
+ /** Blocks that are a line of text, which can be an item. */
23
+ const TEXT_BLOCK = /^(p|h[1-6]|div)$/;
24
+ /** Blocks nothing in which is a line that can be an item: a picture, a table's own parts, code. */
25
+ const NOT_LINES = /^(pre|figure|figcaption|table|thead|tbody|tfoot|tr|hr|ul|ol)$/;
26
+ const isElement = (node) => node?.nodeType === 1;
27
+ const isText = (node) => node?.nodeType === 3;
28
+ const isList = (node) => isElement(node) && (node.localName === 'ul' || node.localName === 'ol');
29
+ const isItem = (node) => isElement(node) && node.localName === 'li';
30
+ const isBlock = (node) => isElement(node) && node.matches(BLOCKS);
31
+ const isBreak = (node) => isElement(node) && node.localName === 'br';
32
+ const isBlank = (node) => isText(node) && node.data.trim() === '';
33
+ const keyOf = (line) => ('item' in line ? line.item : 'block' in line ? line.block : (line.loose[0] ?? line.end ?? line.parent));
34
+ /** The line a node is in, or null where it is in none that can be an item. */
35
+ function lineAt(root, node) {
36
+ let at = node;
37
+ /** The node on the way up that is straight inside the block found. */
38
+ let inside = null;
39
+ while (at && at !== root && !isBlock(at)) {
40
+ inside = at;
41
+ at = at.parentNode;
42
+ }
43
+ if (!at)
44
+ return null;
45
+ if (at !== root && isElement(at)) {
46
+ const name = at.localName;
47
+ if (name === 'li')
48
+ return { item: at };
49
+ if (NOT_LINES.test(name))
50
+ return null;
51
+ if (TEXT_BLOCK.test(name) && !Array.from(at.children).some(isBlock)) {
52
+ // (A paragraph straight inside an item, as Word pastes a list, is the item's text.)
53
+ return isItem(at.parentNode) ? { item: at.parentNode } : { block: at };
54
+ }
55
+ }
56
+ // Words straight inside the document, a table cell, a quotation, or a <div> of blocks.
57
+ if (!inside)
58
+ return null;
59
+ if (isBreak(inside)) {
60
+ const loose = [];
61
+ for (let before = inside.previousSibling; before && !isBlock(before) && !isBreak(before); before = before.previousSibling)
62
+ loose.unshift(before);
63
+ return { loose, end: inside, parent: at };
64
+ }
65
+ const loose = [inside];
66
+ for (let before = inside.previousSibling; before && !isBlock(before) && !isBreak(before); before = before.previousSibling)
67
+ loose.unshift(before);
68
+ let after = inside.nextSibling;
69
+ for (; after && !isBlock(after) && !isBreak(after); after = after.nextSibling)
70
+ loose.push(after);
71
+ // (The space a source has between two blocks is no line.)
72
+ if (!isBreak(after) && loose.every(isBlank))
73
+ return null;
74
+ return { loose, end: isBreak(after) ? after : null, parent: at };
75
+ }
76
+ /** The lines a selection touches, in the order of the document. */
77
+ function linesIn(root, range) {
78
+ const lines = [];
79
+ const seen = new Set();
80
+ const add = (node) => {
81
+ const line = lineAt(root, node);
82
+ if (!line || seen.has(keyOf(line)))
83
+ return;
84
+ seen.add(keyOf(line));
85
+ lines.push(line);
86
+ };
87
+ if (range.collapsed) {
88
+ let node = range.startContainer;
89
+ // (A caret said by an element is at one of its children: <p><br></p>, an empty item.)
90
+ if (isElement(node) && node.childNodes.length > 0)
91
+ node = node.childNodes[Math.min(range.startOffset, node.childNodes.length - 1)];
92
+ // (And before a block, it is in the block: the document's element, before its first paragraph.)
93
+ while (isBlock(node) && !NOT_LINES.test(node.localName) && node.firstChild && Array.from(node.children).some(isBlock))
94
+ node = node.firstChild;
95
+ add(node);
96
+ return lines;
97
+ }
98
+ const doc = root.ownerDocument;
99
+ const within = doc.createRange();
100
+ const walker = doc.createTreeWalker(root, 0x1 | 0x4 /* elements and text */);
101
+ for (let node = walker.nextNode(); node; node = walker.nextNode()) {
102
+ const leaf = isText(node) ? node.data !== '' : isElement(node) && (node.localName === 'br' || node.localName === 'img');
103
+ if (!leaf)
104
+ continue;
105
+ if (isText(node))
106
+ within.selectNodeContents(node);
107
+ else
108
+ within.selectNode(node);
109
+ // A line is touched where the selection ends after the line's start: one that ends
110
+ // at the very start of the next line does not take that line in. (It begins in the
111
+ // line it begins in, also at the end of it.)
112
+ const endsAfterStart = range.compareBoundaryPoints(Range.START_TO_END, within) > 0;
113
+ const beginsBeforeEnd = range.compareBoundaryPoints(Range.END_TO_START, within) <= 0;
114
+ if (endsAfterStart && beginsBeforeEnd)
115
+ add(node);
116
+ }
117
+ return lines;
118
+ }
119
+ // ── The selection ──
120
+ // A node that is moved takes no selection with it: the browser puts a selection that was in
121
+ // it at the place it was taken from. So the selection is read before anything moves and put
122
+ // back after, in the same text; and where it was said by an element that was replaced (an
123
+ // empty paragraph made an item), by the element that took its place.
124
+ let replaced = new Map();
125
+ /**
126
+ * An end of the selection that is said by an element, as the same place said by the text it is
127
+ * at: text keeps its place in the selection wherever it is moved, and "after the document's
128
+ * first child" (Firefox's Select All) is somewhere else once that child is three paragraphs.
129
+ */
130
+ function inward(node, offset, toEnd) {
131
+ while (isElement(node)) {
132
+ const next = node.childNodes[toEnd ? offset - 1 : offset];
133
+ // (An empty line, said by the line itself, is said so: there is no text in it.)
134
+ if (!next || !(isText(next) || next.childNodes.length > 0))
135
+ break;
136
+ node = next;
137
+ offset = !toEnd ? 0 : isText(next) ? next.length : next.childNodes.length;
138
+ }
139
+ return [node, offset];
140
+ }
141
+ function keepingSelection(sel, change) {
142
+ replaced = new Map();
143
+ let ends = null;
144
+ if (sel && sel.rangeCount > 0 && sel.anchorNode && sel.focusNode) {
145
+ const range = sel.getRangeAt(0);
146
+ const forward = sel.anchorNode === range.startContainer && sel.anchorOffset === range.startOffset;
147
+ const start = inward(range.startContainer, range.startOffset, false);
148
+ const end = range.collapsed ? start : inward(range.endContainer, range.endOffset, true);
149
+ ends = forward ? { anchor: start, focus: end } : { anchor: end, focus: start };
150
+ }
151
+ const done = change();
152
+ if (done && sel && ends) {
153
+ const now = ([node, offset]) => {
154
+ const at = replaced.get(node) ?? node;
155
+ if (!at.isConnected)
156
+ return null;
157
+ return [at, Math.min(offset, isText(at) ? at.length : at.childNodes.length)];
158
+ };
159
+ const anchor = now(ends.anchor);
160
+ const focus = now(ends.focus);
161
+ if (anchor && focus)
162
+ sel.setBaseAndExtent(anchor[0], anchor[1], focus[0], focus[1]);
163
+ }
164
+ replaced = new Map();
165
+ return done;
166
+ }
167
+ // ── Lists ──
168
+ const listOf = (item) => (isList(item.parentNode) ? item.parentNode : null);
169
+ /** Whether an item is in a list that is itself in a list. */
170
+ function isNested(item) {
171
+ const above = listOf(item)?.parentNode;
172
+ return isItem(above) || isList(above);
173
+ }
174
+ /** A list like this one, for the items that are parted from it: it looks the same, and is not the same list. */
175
+ function another(list) {
176
+ const copy = list.cloneNode(false);
177
+ copy.removeAttribute('start');
178
+ copy.removeAttribute('id');
179
+ return copy;
180
+ }
181
+ /** Add a list to the list of its kind right before it, and the one right after it to it. Returns the list the items are in. */
182
+ function joinAround(list) {
183
+ const beside = (from, step) => {
184
+ let at = from[step];
185
+ while (at && isBlank(at))
186
+ at = at[step];
187
+ return isList(at) && at.localName === list.localName ? at : null;
188
+ };
189
+ const before = beside(list, 'previousSibling');
190
+ if (before) {
191
+ while (list.firstChild)
192
+ before.appendChild(list.firstChild);
193
+ list.remove();
194
+ list = before;
195
+ }
196
+ const after = beside(list, 'nextSibling');
197
+ if (after) {
198
+ while (after.firstChild)
199
+ list.appendChild(after.firstChild);
200
+ after.remove();
201
+ }
202
+ return list;
203
+ }
204
+ /** Part a list around one of its items. Returns the list that item is now alone in, between what came before it and what came after. */
205
+ function alone(item) {
206
+ const list = listOf(item);
207
+ const after = [];
208
+ for (let next = item.nextSibling; next; next = next.nextSibling)
209
+ after.push(next);
210
+ if (after.some((node) => !isBlank(node))) {
211
+ const rest = another(list);
212
+ rest.append(...after);
213
+ list.after(rest);
214
+ }
215
+ else {
216
+ for (const node of after)
217
+ node.parentNode?.removeChild(node);
218
+ }
219
+ let first = true;
220
+ for (let before = item.previousSibling; before; before = before.previousSibling)
221
+ if (!isBlank(before))
222
+ first = false;
223
+ if (first)
224
+ return list;
225
+ const own = another(list);
226
+ list.after(own);
227
+ own.appendChild(item);
228
+ return own;
229
+ }
230
+ /** The same list as the other kind: what says how its items are marked is the other kind's to say. */
231
+ function asKind(list, kind) {
232
+ if (list.localName === kind)
233
+ return list;
234
+ const made = list.ownerDocument.createElement(kind);
235
+ for (const attr of Array.from(list.attributes)) {
236
+ if (attr.name !== 'type' && attr.name !== 'start' && attr.name !== 'reversed')
237
+ made.setAttribute(attr.name, attr.value);
238
+ }
239
+ made.style.listStyleType = '';
240
+ made.style.listStyle = '';
241
+ if (!made.getAttribute('style'))
242
+ made.removeAttribute('style');
243
+ list.replaceWith(made);
244
+ while (list.firstChild)
245
+ made.appendChild(list.firstChild);
246
+ replaced.set(list, made);
247
+ return made;
248
+ }
249
+ /** A line that has nothing in it is given the line break that keeps it a line to type in. */
250
+ function keepOpen(el) {
251
+ if (!Array.from(el.childNodes).some((node) => !isBlank(node)))
252
+ el.appendChild(el.ownerDocument.createElement('br'));
253
+ }
254
+ // ── What a line says of itself ──
255
+ // A paragraph made an item, and an item made a paragraph again, is the same line: its id, its
256
+ // class, an application's own data-* marks, its language, and the look of its words (a color,
257
+ // a font) go with it. What says how a paragraph is laid out does not: an item has the list's
258
+ // indent, and is written left-aligned with no spacing of its own (see the RTF writer), so on
259
+ // an item it would show what the RTF does not say. Nor does what says how an item is marked.
260
+ /** A paragraph's layout: not an item's. */
261
+ const PARAGRAPH_LAYOUT = { attributes: /^(align|data-rtf-tab-stops)$/, style: /^(margin|padding|text-indent|text-align|line-height)(-|$)/ };
262
+ /** An item's marker and number: not a paragraph's. */
263
+ const ITEM_MARKER = { attributes: /^(value|type)$/, style: /^list-style(-|$)/ };
264
+ /** Move what `from` says of itself to `to`, but what `leave` names; what `to` says already stays. */
265
+ function carry(from, to, leave) {
266
+ for (const attr of Array.from(from.attributes)) {
267
+ if (leave.attributes.test(attr.name) || to.hasAttribute(attr.name))
268
+ continue;
269
+ if (attr.name !== 'style') {
270
+ to.setAttribute(attr.name, attr.value);
271
+ continue;
272
+ }
273
+ const style = from.style;
274
+ const own = to.style;
275
+ for (const name of Array.from(style))
276
+ if (!leave.style.test(name))
277
+ own.setProperty(name, style.getPropertyValue(name), style.getPropertyPriority(name));
278
+ if (!to.getAttribute('style'))
279
+ to.removeAttribute('style');
280
+ }
281
+ }
282
+ /** Make a line an item of a list of this kind: of the list it is next to, where it is next to one. */
283
+ function toItem(line, kind) {
284
+ const doc = keyOf(line).ownerDocument;
285
+ const list = doc.createElement(kind);
286
+ const item = doc.createElement('li');
287
+ if ('block' in line) {
288
+ line.block.replaceWith(list);
289
+ while (line.block.firstChild)
290
+ item.appendChild(line.block.firstChild);
291
+ carry(line.block, item, PARAGRAPH_LAYOUT);
292
+ replaced.set(line.block, item);
293
+ }
294
+ else {
295
+ line.parent.insertBefore(list, line.loose[0] ?? line.end);
296
+ item.append(...line.loose);
297
+ // (The line break that ended the line: an item ends its own.)
298
+ line.end?.parentNode?.removeChild(line.end);
299
+ // (A caret said by what the words were straight inside, an empty table cell, is in the item.)
300
+ if (!replaced.has(line.parent))
301
+ replaced.set(line.parent, item);
302
+ }
303
+ keepOpen(item);
304
+ list.appendChild(item);
305
+ joinAround(list);
306
+ }
307
+ /** Put an item one list further in: in a list at the end of the item before it. Returns false where there is none before it. */
308
+ function nest(item) {
309
+ const list = listOf(item);
310
+ let before = item.previousSibling;
311
+ while (before && isBlank(before))
312
+ before = before.previousSibling;
313
+ if (!list || !isItem(before))
314
+ return false;
315
+ let last = before.lastChild;
316
+ while (last && isBlank(last))
317
+ last = last.previousSibling;
318
+ let under = isList(last) && last.localName === list.localName ? last : null;
319
+ if (!under) {
320
+ under = list.ownerDocument.createElement(list.localName);
321
+ before.appendChild(under);
322
+ }
323
+ under.appendChild(item);
324
+ // (A list of its own that it had is one list with what it is put beside.)
325
+ return true;
326
+ }
327
+ /** Put an item one list further out: after the item its list is under. What followed it in its list is under it now. */
328
+ function unnest(item) {
329
+ const list = listOf(item);
330
+ const above = list?.parentNode;
331
+ if (!list || !(isItem(above) || isList(above)))
332
+ return false;
333
+ const after = [];
334
+ for (let next = item.nextSibling; next; next = next.nextSibling)
335
+ after.push(next);
336
+ if (after.some((node) => !isBlank(node))) {
337
+ let last = item.lastChild;
338
+ while (last && isBlank(last))
339
+ last = last.previousSibling;
340
+ let under = isList(last) && last.localName === list.localName ? last : null;
341
+ if (!under) {
342
+ under = another(list);
343
+ item.appendChild(under);
344
+ }
345
+ under.append(...after);
346
+ }
347
+ else {
348
+ for (const node of after)
349
+ node.parentNode?.removeChild(node);
350
+ }
351
+ // What the item it was under holds after this list comes after the item in the document,
352
+ // and still does: it is under the item now, with what followed it in its own list. (An
353
+ // item may hold two lists, bullets and then numbers, or a paragraph after its list, as
354
+ // Word pastes one. Left where it was, it was before the item that had been above it:
355
+ // "Negative for dysplasia" went under "See comment".)
356
+ if (isItem(above)) {
357
+ const rest = [];
358
+ for (let next = list.nextSibling; next; next = next.nextSibling)
359
+ rest.push(next);
360
+ if (rest.some((node) => !isBlank(node)))
361
+ item.append(...rest);
362
+ }
363
+ // (A list straight inside a list, as some editors nest, is parted where the item was.)
364
+ (isItem(above) ? above : list).after(item);
365
+ if (!Array.from(list.childNodes).some((node) => !isBlank(node)))
366
+ list.remove();
367
+ return true;
368
+ }
369
+ /** Make an item a paragraph again, where it is: its list is parted around it, and a list it had under it is a list after it. */
370
+ function toParagraph(item) {
371
+ while (isNested(item))
372
+ unnest(item);
373
+ if (!listOf(item))
374
+ return;
375
+ const own = alone(item);
376
+ const doc = item.ownerDocument;
377
+ const parts = [];
378
+ let paragraph = null;
379
+ for (const child of Array.from(item.childNodes)) {
380
+ if (isBlock(child)) {
381
+ paragraph = null;
382
+ parts.push(child);
383
+ continue;
384
+ }
385
+ // (The space between two blocks of an item is no paragraph.)
386
+ if (!paragraph && isBlank(child) && parts.length > 0)
387
+ continue;
388
+ if (!paragraph) {
389
+ paragraph = doc.createElement('p');
390
+ parts.push(paragraph);
391
+ }
392
+ paragraph.appendChild(child);
393
+ }
394
+ if (parts.length === 0)
395
+ parts.push(doc.createElement('p'));
396
+ for (const part of parts)
397
+ if (part.localName === 'p')
398
+ keepOpen(part);
399
+ // (What the item said of itself is the first line's that it was: the paragraph of its own
400
+ // words, or the paragraph Word put them in.)
401
+ const first = parts.find((part) => TEXT_BLOCK.test(part.localName));
402
+ if (first)
403
+ carry(item, first, ITEM_MARKER);
404
+ own.replaceWith(...parts);
405
+ replaced.set(item, parts[0]);
406
+ for (const part of parts)
407
+ if (isList(part) && part.isConnected)
408
+ joinAround(part);
409
+ }
410
+ /** The items among these that are not under another of them: that one takes them with it. */
411
+ function outermost(items) {
412
+ const all = new Set(items);
413
+ return items.filter((item) => {
414
+ for (let up = item.parentNode; up; up = up.parentNode)
415
+ if (all.has(up))
416
+ return false;
417
+ return true;
418
+ });
419
+ }
420
+ const rangeOf = (sel, root) => {
421
+ if (!sel || sel.rangeCount === 0)
422
+ return null;
423
+ const range = sel.getRangeAt(0);
424
+ return root.contains(range.startContainer) && root.contains(range.endContainer) ? range : null;
425
+ };
426
+ /** The items the selection touches, if every line it touches is one; else none. */
427
+ function itemsOnly(root, range) {
428
+ const lines = linesIn(root, range);
429
+ return lines.length > 0 && lines.every((line) => 'item' in line) ? lines.map((line) => line.item) : [];
430
+ }
431
+ /**
432
+ * The toolbar's list button. The lines the selection touches are made items of a list of
433
+ * this kind, and one list with a list of that kind they are next to. Where they are all
434
+ * items of such a list already, they are paragraphs again. Where they are items of the
435
+ * other kind: with a caret, the list the caret is in is made this kind; with words
436
+ * selected, the items selected are.
437
+ */
438
+ export function toggleList(root, sel, kind) {
439
+ const range = rangeOf(sel, root);
440
+ if (!range)
441
+ return false;
442
+ const collapsed = range.collapsed;
443
+ const lines = linesIn(root, range);
444
+ if (lines.length === 0)
445
+ return false;
446
+ return keepingSelection(sel, () => {
447
+ const items = lines.filter((line) => 'item' in line).map((line) => line.item);
448
+ if (items.length === lines.length) {
449
+ if (items.every((item) => listOf(item)?.localName === kind)) {
450
+ // (Each of them: one that was under another is a paragraph after it.)
451
+ for (const item of items)
452
+ if (item.isConnected)
453
+ toParagraph(item);
454
+ return true;
455
+ }
456
+ if (collapsed) {
457
+ const list = listOf(items[0]);
458
+ if (list)
459
+ joinAround(asKind(list, kind));
460
+ return true;
461
+ }
462
+ }
463
+ for (const line of lines) {
464
+ if (!('item' in line))
465
+ toItem(line, kind);
466
+ else if (line.item.isConnected && listOf(line.item) && listOf(line.item).localName !== kind)
467
+ joinAround(asKind(alone(line.item), kind));
468
+ }
469
+ return true;
470
+ });
471
+ }
472
+ /** Which kind of list the selection is in: the innermost list of the line it begins in. */
473
+ export function listKindAt(root, sel) {
474
+ let node = sel?.anchorNode;
475
+ if (!sel || !node || !root.contains(node))
476
+ return null;
477
+ // (A selection said by an element begins at one of its children: Firefox's Select All
478
+ // is said by the document's element.)
479
+ for (let offset = sel.anchorOffset; isElement(node) && node.localName !== 'li' && node.childNodes.length > 0; offset = 0) {
480
+ node = node.childNodes[Math.min(offset, node.childNodes.length - 1)];
481
+ }
482
+ const item = (isElement(node) ? node : node.parentElement)?.closest('li');
483
+ const list = item && root.contains(item) ? item.parentElement : null;
484
+ return isList(list) ? list.localName : null;
485
+ }
486
+ /**
487
+ * Put the items the selection touches one list further in, each under the item before it.
488
+ * Returns false, with nothing changed, where the selection is not all items, or the first of
489
+ * them has no item before it to be under.
490
+ */
491
+ export function nestItems(root, sel) {
492
+ const range = rangeOf(sel, root);
493
+ const items = range ? outermost(itemsOnly(root, range)) : [];
494
+ if (items.length === 0)
495
+ return false;
496
+ let before = items[0].previousSibling;
497
+ while (before && isBlank(before))
498
+ before = before.previousSibling;
499
+ if (!isItem(before))
500
+ return false;
501
+ return keepingSelection(sel, () => {
502
+ let any = false;
503
+ for (const item of items)
504
+ any = nest(item) || any;
505
+ return any;
506
+ });
507
+ }
508
+ /** Put the items the selection touches that are in a list within a list one list further out. Returns false where none is. */
509
+ export function unnestItems(root, sel) {
510
+ const range = rangeOf(sel, root);
511
+ const items = range ? outermost(itemsOnly(root, range)).filter(isNested) : [];
512
+ if (items.length === 0)
513
+ return false;
514
+ return keepingSelection(sel, () => {
515
+ let any = false;
516
+ for (const item of items)
517
+ any = unnest(item) || any;
518
+ return any;
519
+ });
520
+ }
521
+ /**
522
+ * The item the caret is in leaves its list by one step: one list further out, or, from a
523
+ * list that is in no other, a paragraph. (What Enter does in an item with nothing in it, and
524
+ * Backspace at the start of one.)
525
+ */
526
+ export function leaveList(root, sel) {
527
+ const range = rangeOf(sel, root);
528
+ const items = range ? itemsOnly(root, range) : [];
529
+ if (items.length !== 1)
530
+ return false;
531
+ return keepingSelection(sel, () => {
532
+ if (isNested(items[0]))
533
+ return unnest(items[0]);
534
+ toParagraph(items[0]);
535
+ return true;
536
+ });
537
+ }
538
+ /** The item's own words and pictures: not those of a list under it. */
539
+ function ownContent(item, upTo) {
540
+ const range = item.ownerDocument.createRange();
541
+ range.selectNodeContents(item);
542
+ const under = Array.from(item.childNodes).find(isList);
543
+ if (under)
544
+ range.setEndBefore(under);
545
+ if (upTo)
546
+ range.setEnd(upTo.startContainer, upTo.startOffset);
547
+ const held = range.cloneContents();
548
+ return { text: (held.textContent ?? '').replace(/​/g, ''), shows: held.querySelector('img, br, hr, table') !== null };
549
+ }
550
+ /** The item the caret is in, where it is a caret in an item. */
551
+ function caretItem(root, sel) {
552
+ const range = rangeOf(sel, root);
553
+ if (!range || !range.collapsed)
554
+ return null;
555
+ const [item] = itemsOnly(root, range);
556
+ return item ? { item, range } : null;
557
+ }
558
+ /** Whether the caret is in an item with nothing in it: no words, no picture. (One with a list under it has that.) */
559
+ export function inEmptyItem(root, sel) {
560
+ const at = caretItem(root, sel);
561
+ if (!at || Array.from(at.item.childNodes).some(isList))
562
+ return false;
563
+ const own = ownContent(at.item);
564
+ return own.text.trim() === '' && at.item.querySelector('img, hr, table') === null;
565
+ }
566
+ /** Whether the caret is at the very start of an item: nothing of the item before it. */
567
+ export function atItemStart(root, sel) {
568
+ const at = caretItem(root, sel);
569
+ if (!at)
570
+ return false;
571
+ const under = Array.from(at.item.childNodes).find(isList);
572
+ if (under && (under === at.range.startContainer || under.contains(at.range.startContainer)))
573
+ return false;
574
+ const before = ownContent(at.item, at.range);
575
+ return before.text === '' && !before.shows;
576
+ }
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Pictures side by side with a set gap between them.
3
+ *
4
+ * Pictures side by side are a table of one row, a picture in each cell. Without a gap the row
5
+ * is as wide as the page and its cells share it equally; how far apart the pictures are then
6
+ * depends on how wide the page is, and each is aligned within its share.
7
+ *
8
+ * With a gap, the row is laid out exactly: each picture's cell is as wide as the picture,
9
+ * and between two pictures is an empty cell as wide as the gap. The table has no width of
10
+ * its own, so it is as wide as what it holds, on the screen and in the RTF, where each cell
11
+ * ends at an edge in twips (\cellx) and nothing is stretched to the page.
12
+ *
13
+ * <table style="border-collapse:collapse;margin:4px 0"><tbody><tr>
14
+ * <td style="padding:0;width:150pt"><figure><img …></figure></td>
15
+ * <td style="padding:0;width:18pt"></td>
16
+ * <td style="padding:0;width:150pt"><figure><img …></figure></td>
17
+ * </tr></tbody></table>
18
+ *
19
+ * The RTF reader knows the shape again, a row of picture cells with an empty cell between
20
+ * each two, and gives the same widths back.
21
+ */
22
+ /** The style of the table of a row of pictures with a set gap: no width of its own. */
23
+ export declare const GAPPED_ROW_STYLE = "border-collapse:collapse;margin:4px 0";
24
+ /** The widest gap that can be set, in points: two inches. */
25
+ export declare const MOST_GAP_PT = 144;
26
+ /** The width an element asks for as a length (not a share), in twips; null where it asks for none. */
27
+ export declare function lengthTwips(el: Element): number | null;
28
+ /** A width in twips as the points a cell is given: exact to the twip. */
29
+ export declare const pointsOf: (twips: number) => string;
30
+ /**
31
+ * A cell nothing is drawn on or around: no border, no background. A table that has them is a
32
+ * table of its own, whatever it holds, and is written and read as one.
33
+ */
34
+ export declare function isPlainCell(cell: Element): boolean;
35
+ /**
36
+ * The gap between two pictures: a plain cell with a width of its own and nothing in it. (A
37
+ * line break or blanks are nothing: the caret put in the cell and taken out again leaves a
38
+ * <br> there in Chromium, and that is still the gap.)
39
+ */
40
+ export declare function isGapCell(cell: Element): boolean;
41
+ /** A cell that holds one picture and nothing else: alone, or in its figure with its caption. */
42
+ export declare function holdsOnePicture(cell: Element): boolean;
43
+ /**
44
+ * Each column's width in twips, where the table is a row of pictures with set gaps: one row,
45
+ * a picture's cell first and last and between each two of them a gap cell, every cell plain
46
+ * and with a length of its own, and the table with no width. Null for any other table, which is written
47
+ * across the page as it always was.
48
+ */
49
+ export declare function gappedRowWidths(table: Element): number[] | null;