svelte-rtf-editor 1.13.0 → 1.16.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.
@@ -36,6 +36,8 @@ interface Props {
36
36
  fontSizes?: number[];
37
37
  /** The fonts offered for text, beside the document's own; none unless given. */
38
38
  fonts?: string[];
39
+ /** A right-click on the text opens a menu of `fonts`; the browser's menu is Shift + right-click. */
40
+ fontMenu?: boolean;
39
41
  /** `data-` attributes of the application's own, kept on HTML that is set or pasted. */
40
42
  keepAttributes?: string[];
41
43
  /** Class names of the application's own, kept the same way. */
@@ -61,7 +63,19 @@ declare const InkEditor: import("svelte").Component<Props, {
61
63
  importRtf: () => void;
62
64
  setFontSize: (points: number) => void;
63
65
  setFont: (name: string | null) => void;
66
+ undo: () => boolean;
67
+ redo: () => boolean;
64
68
  clearFormatting: () => void;
69
+ setLineSpacing: (lines: number | null) => void;
70
+ setParagraphSpacing: (space: {
71
+ before?: number | null;
72
+ after?: number | null;
73
+ }) => void;
74
+ setListMarkerGap: (points: number | null) => void;
75
+ insertTable: (rows: number, columns: number, options?: {
76
+ borders?: boolean;
77
+ header?: boolean;
78
+ }) => void;
65
79
  }, "">;
66
80
  type InkEditor = ReturnType<typeof InkEditor>;
67
81
  export default InkEditor;
@@ -9,6 +9,7 @@
9
9
  onblock?: (tag: string) => void;
10
10
  oninsertlink?: () => void;
11
11
  oninsertimage?: () => void;
12
+ oninserttable?: () => void;
12
13
  /** The size of the text at the caret, in points; null where the selection has more than one. */
13
14
  fontSize?: number | null;
14
15
  /** The sizes offered, in points. */
@@ -21,6 +22,12 @@
21
22
  /** The document font's name: the first of the list, and what '' is shown as. */
22
23
  documentFont?: string;
23
24
  onfont?: (name: string) => void;
25
+ /**
26
+ * The spacing of the paragraph at the caret: lines apart (null: none set), and points before
27
+ * and after; and in a list, the gap after its markers in points (null: the list sets none).
28
+ * Out of a list `gap` is left out, and the list does not offer it.
29
+ */
30
+ spacing?: { line: number | null; before: number; after: number; gap?: number | null };
24
31
  /** The Copy format brush is in hand: the button shows as pressed. */
25
32
  copyingFormat?: boolean;
26
33
  oncopyformat?: () => void;
@@ -33,6 +40,7 @@
33
40
  onblock,
34
41
  oninsertlink,
35
42
  oninsertimage,
43
+ oninserttable,
36
44
  fontSize = null,
37
45
  fontSizes = [],
38
46
  onfontsize,
@@ -40,6 +48,7 @@
40
48
  fonts = [],
41
49
  documentFont = '',
42
50
  onfont,
51
+ spacing = { line: null, before: 0, after: 0 },
43
52
  copyingFormat = false,
44
53
  oncopyformat
45
54
  }: Props = $props();
@@ -53,6 +62,24 @@
53
62
  function exec(cmd: string, value: string | null = null) {
54
63
  onexec?.(cmd, value);
55
64
  }
65
+
66
+ // What the spacing list offers. The paragraph's own setting is offered too where it is
67
+ // another (8 pt after, in a document from elsewhere), so the list shows what the paragraph has.
68
+ const offered = (choices: number[], current: number | null) =>
69
+ current !== null && !choices.includes(current) ? [...choices, current].sort((a, b) => a - b) : choices;
70
+ const lineChoices = $derived(offered([1, 1.15, 1.5, 2], spacing.line));
71
+ const beforeChoices = $derived(offered([0, 6, 12], spacing.before));
72
+ const afterChoices = $derived(offered([0, 6, 12], spacing.after));
73
+ const gapChoices = $derived(offered([9, 18, 27, 36], spacing.gap ?? null));
74
+ const mark = (on: boolean) => (on ? '\u2713 ' : '\u2003');
75
+
76
+ /** One choice of the spacing list is a command; the list goes back to showing its name. */
77
+ function chooseSpacing(e: Event) {
78
+ const list = e.target as HTMLSelectElement;
79
+ const [cmd, value] = list.value.split(':');
80
+ list.value = '';
81
+ if (cmd) exec(cmd, value === 'none' ? null : value);
82
+ }
56
83
  </script>
57
84
 
58
85
  <div class="toolbar" role="toolbar" aria-label="Formatting">
@@ -153,10 +180,10 @@
153
180
 
154
181
  <!-- Lists -->
155
182
  <div class="toolbar-group">
156
- <ToolbarButton title="Bullet list" onclick={() => exec('insertUnorderedList')}>
183
+ <ToolbarButton active={formatState.bulletList} title="Bullet list" onclick={() => exec('insertUnorderedList')}>
157
184
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><line x1="9" y1="6" x2="20" y2="6"/><line x1="9" y1="12" x2="20" y2="12"/><line x1="9" y1="18" x2="20" y2="18"/><circle cx="5" cy="6" r="1.5" fill="currentColor"/><circle cx="5" cy="12" r="1.5" fill="currentColor"/><circle cx="5" cy="18" r="1.5" fill="currentColor"/></svg>
158
185
  </ToolbarButton>
159
- <ToolbarButton title="Numbered list" onclick={() => exec('insertOrderedList')}>
186
+ <ToolbarButton active={formatState.numberedList} title="Numbered list" onclick={() => exec('insertOrderedList')}>
160
187
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><line x1="10" y1="6" x2="20" y2="6"/><line x1="10" y1="12" x2="20" y2="12"/><line x1="10" y1="18" x2="20" y2="18"/><text x="3" y="8" font-size="8" fill="currentColor" stroke="none" font-family="sans-serif">1</text><text x="3" y="14" font-size="8" fill="currentColor" stroke="none" font-family="sans-serif">2</text><text x="3" y="20" font-size="8" fill="currentColor" stroke="none" font-family="sans-serif">3</text></svg>
161
188
  </ToolbarButton>
162
189
  </div>
@@ -205,6 +232,39 @@
205
232
 
206
233
  <div class="toolbar-divider"></div>
207
234
 
235
+ <!-- Spacing -->
236
+ <div class="toolbar-group">
237
+ <select class="tb-select tb-spacing" title="Line and paragraph spacing" aria-label="Line and paragraph spacing" value="" onchange={chooseSpacing}>
238
+ <option value="" hidden>Spacing</option>
239
+ <optgroup label="Line spacing">
240
+ {#each lineChoices as lines (lines)}
241
+ <option value="lineSpacing:{lines}">{mark(spacing.line === lines)}{lines === 1 ? '1.0' : lines === 2 ? '2.0' : lines}</option>
242
+ {/each}
243
+ <option value="lineSpacing:none">{mark(spacing.line === null)}Default</option>
244
+ </optgroup>
245
+ <optgroup label="Space before paragraph">
246
+ {#each beforeChoices as points (points)}
247
+ <option value="spaceBefore:{points}">{mark(spacing.before === points)}{points} pt</option>
248
+ {/each}
249
+ </optgroup>
250
+ <optgroup label="Space after paragraph">
251
+ {#each afterChoices as points (points)}
252
+ <option value="spaceAfter:{points}">{mark(spacing.after === points)}{points} pt</option>
253
+ {/each}
254
+ </optgroup>
255
+ {#if spacing.gap !== undefined}
256
+ <optgroup label="Bullet or number gap">
257
+ {#each gapChoices as points (points)}
258
+ <option value="markerGap:{points}">{mark(spacing.gap === points)}{points} pt</option>
259
+ {/each}
260
+ <option value="markerGap:none">{mark(spacing.gap === null)}Default</option>
261
+ </optgroup>
262
+ {/if}
263
+ </select>
264
+ </div>
265
+
266
+ <div class="toolbar-divider"></div>
267
+
208
268
  <!-- Copy format -->
209
269
  <div class="toolbar-group">
210
270
  <ToolbarButton active={copyingFormat} title="Copy format" onclick={oncopyformat}>
@@ -222,6 +282,9 @@
222
282
  <ToolbarButton title="Insert image" onclick={oninsertimage}>
223
283
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><rect x="3" y="3" width="18" height="18" rx="2"/><circle cx="8.5" cy="8.5" r="1.5"/><path d="M21 15l-5-5L5 21"/></svg>
224
284
  </ToolbarButton>
285
+ <ToolbarButton title="Insert table" onclick={oninserttable}>
286
+ <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="4" width="18" height="16" rx="2"/><line x1="3" y1="10" x2="21" y2="10"/><line x1="3" y1="15" x2="21" y2="15"/><line x1="9" y1="4" x2="9" y2="20"/><line x1="15" y1="4" x2="15" y2="20"/></svg>
287
+ </ToolbarButton>
225
288
  <ToolbarButton title="Horizontal rule" onclick={() => exec('insertHorizontalRule')}>
226
289
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><line x1="3" y1="12" x2="21" y2="12"/></svg>
227
290
  </ToolbarButton>
@@ -305,6 +368,11 @@
305
368
  min-width: 62px;
306
369
  }
307
370
 
371
+ /* It shows its name, not the longest of its choices. */
372
+ .tb-spacing {
373
+ width: 6.6em;
374
+ }
375
+
308
376
  /* (The color alone: `background` would take the arrow off with it.) */
309
377
  .tb-select:hover {
310
378
  background-color: var(--surface, #f6f4f0);
@@ -5,6 +5,7 @@ interface Props {
5
5
  onblock?: (tag: string) => void;
6
6
  oninsertlink?: () => void;
7
7
  oninsertimage?: () => void;
8
+ oninserttable?: () => void;
8
9
  /** The size of the text at the caret, in points; null where the selection has more than one. */
9
10
  fontSize?: number | null;
10
11
  /** The sizes offered, in points. */
@@ -21,6 +22,17 @@ interface Props {
21
22
  /** The document font's name: the first of the list, and what '' is shown as. */
22
23
  documentFont?: string;
23
24
  onfont?: (name: string) => void;
25
+ /**
26
+ * The spacing of the paragraph at the caret: lines apart (null: none set), and points before
27
+ * and after; and in a list, the gap after its markers in points (null: the list sets none).
28
+ * Out of a list `gap` is left out, and the list does not offer it.
29
+ */
30
+ spacing?: {
31
+ line: number | null;
32
+ before: number;
33
+ after: number;
34
+ gap?: number | null;
35
+ };
24
36
  /** The Copy format brush is in hand: the button shows as pressed. */
25
37
  copyingFormat?: boolean;
26
38
  oncopyformat?: () => void;
@@ -0,0 +1,49 @@
1
+ /**
2
+ * The editor's own Undo and Redo.
3
+ *
4
+ * A browser's history knows what its own commands and the keyboard changed. It knows nothing
5
+ * of what the editor changes in the document itself: a picture or a table put in, a picture
6
+ * moved, an indent. Its Undo skipped those and took back the typing before them, and what
7
+ * the editor then changed behind it could leave it unable to undo at all. So the editor
8
+ * keeps the history: the document as it was before each step, and Undo puts that back.
9
+ *
10
+ * A step is whatever changed between two calls of `step()`, however it was changed: by a
11
+ * key, by a browser command, or by the editor. The editor calls `step()` before each thing it
12
+ * does, and `typing()` before each key's worth of input, which keeps a run of typing as one
13
+ * step. Nothing has to be said after a change: a MutationObserver knows there was one.
14
+ *
15
+ * What is kept is the document's HTML, with each embedded picture kept once, and the
16
+ * selection. Putting a step back replaces only the blocks that differ, so a picture in a
17
+ * part of the document that did not change is not read again.
18
+ */
19
+ export interface HistoryOptions {
20
+ /** How many steps are kept. */
21
+ limit?: number;
22
+ /** Typing after a pause this long, in ms, is a new step. */
23
+ pause?: number;
24
+ }
25
+ export interface DocumentHistory {
26
+ /** What changed since the last step is a step, from here. Call before changing the document. */
27
+ step(): void;
28
+ /**
29
+ * A key's worth of input is about to change the document. A run of the same kind ('insert'
30
+ * or 'delete'), at the caret the last one left, with no pause, is one step; `null` is input
31
+ * that is a step of its own (a new paragraph, a paste, a cut).
32
+ */
33
+ typing(kind: 'insert' | 'delete' | null): void;
34
+ /** The input has changed the document. */
35
+ typed(): void;
36
+ /** Put the document back as it was before the last step. `then` runs once it is back, and what it changes is part of what was put back. */
37
+ undo(then?: () => void): boolean;
38
+ redo(then?: () => void): boolean;
39
+ canUndo(): boolean;
40
+ canRedo(): boolean;
41
+ /** A change that is no step of its own: with nothing else changed since the last step, it is part of that one. */
42
+ quietly(change: () => void): void;
43
+ /** Forget every step: the document is another one. */
44
+ reset(): void;
45
+ /** Text is being composed (an input method): it is one run of typing, and nothing is undone until it is done. */
46
+ composing(on: boolean): void;
47
+ destroy(): void;
48
+ }
49
+ export declare function createHistory(root: HTMLElement, options?: HistoryOptions): DocumentHistory;
@@ -0,0 +1,329 @@
1
+ /**
2
+ * The editor's own Undo and Redo.
3
+ *
4
+ * A browser's history knows what its own commands and the keyboard changed. It knows nothing
5
+ * of what the editor changes in the document itself: a picture or a table put in, a picture
6
+ * moved, an indent. Its Undo skipped those and took back the typing before them, and what
7
+ * the editor then changed behind it could leave it unable to undo at all. So the editor
8
+ * keeps the history: the document as it was before each step, and Undo puts that back.
9
+ *
10
+ * A step is whatever changed between two calls of `step()`, however it was changed: by a
11
+ * key, by a browser command, or by the editor. The editor calls `step()` before each thing it
12
+ * does, and `typing()` before each key's worth of input, which keeps a run of typing as one
13
+ * step. Nothing has to be said after a change: a MutationObserver knows there was one.
14
+ *
15
+ * What is kept is the document's HTML, with each embedded picture kept once, and the
16
+ * selection. Putting a step back replaces only the blocks that differ, so a picture in a
17
+ * part of the document that did not change is not read again.
18
+ */
19
+ const DEFAULT_LIMIT = 200;
20
+ const DEFAULT_PAUSE = 1000;
21
+ export function createHistory(root, options = {}) {
22
+ const limit = options.limit ?? DEFAULT_LIMIT;
23
+ const pause = options.pause ?? DEFAULT_PAUSE;
24
+ const doc = root.ownerDocument;
25
+ const view = doc.defaultView;
26
+ // ── Pictures ──
27
+ // A picture is in the HTML as its bytes, and a report has megabytes of them. Each step
28
+ // would keep them all again: so a picture is kept once, and the HTML of a step has a mark
29
+ // where it goes. (The mark has a part no document has, so that nothing a writer typed is
30
+ // taken for one.)
31
+ const mark = `ink-kept-${Math.random().toString(36).slice(2)}-`;
32
+ const pictures = [];
33
+ const pictureIds = new Map();
34
+ const pack = (html) => html.replace(/(\ssrc=")(data:[^"]{64,})(")/g, (_all, before, picture, after) => {
35
+ let id = pictureIds.get(picture);
36
+ if (id === undefined) {
37
+ id = pictures.length;
38
+ pictures.push(picture);
39
+ pictureIds.set(picture, id);
40
+ }
41
+ return `${before}${mark}${id}${after}`;
42
+ });
43
+ const unpack = (html) => html.replace(new RegExp(`(\\ssrc=")${mark}(\\d+)(")`, 'g'), (all, before, id, after) => pictures[Number(id)] === undefined ? all : `${before}${pictures[Number(id)]}${after}`);
44
+ // ── The selection ──
45
+ // A place is said as the HTML will have it when it is read again: text nodes that are side
46
+ // by side are one there, and one with nothing in it is not there at all.
47
+ const isText = (node) => node?.nodeType === 3;
48
+ /** Where a child is among its parent's children as read again, and for text how far into the run of text it begins. */
49
+ function placeOf(parent, child, upTo = parent.childNodes.length) {
50
+ let index = -1;
51
+ let run = 0;
52
+ let inText = false;
53
+ for (let i = 0; i < parent.childNodes.length; i++) {
54
+ const at = parent.childNodes[i];
55
+ if (child === null && i === upTo)
56
+ break;
57
+ if (isText(at)) {
58
+ if (at === child || at.length > 0) {
59
+ if (!inText) {
60
+ index++;
61
+ run = 0;
62
+ inText = true;
63
+ }
64
+ if (at === child)
65
+ return { index, shift: run };
66
+ run += at.length;
67
+ }
68
+ }
69
+ else {
70
+ inText = false;
71
+ index++;
72
+ if (at === child)
73
+ return { index, shift: 0 };
74
+ }
75
+ }
76
+ return { index: index + 1, shift: 0 };
77
+ }
78
+ function pointOf(node, offset) {
79
+ const path = [];
80
+ let at = node;
81
+ // (In an element, the offset counts its children: as they will be read again.)
82
+ let within = isText(node) ? offset : placeOf(node, null, offset).index;
83
+ while (at !== root) {
84
+ const parent = at.parentNode;
85
+ if (!parent)
86
+ return null;
87
+ const { index, shift } = placeOf(parent, at);
88
+ if (at === node)
89
+ within += shift;
90
+ path.unshift(index);
91
+ at = parent;
92
+ }
93
+ return { path, offset: within };
94
+ }
95
+ /** The child that is the `index`th when read again: for text, the first piece of it. */
96
+ function childAt(parent, index) {
97
+ let at = -1;
98
+ let inText = false;
99
+ for (let i = 0; i < parent.childNodes.length; i++) {
100
+ const child = parent.childNodes[i];
101
+ if (isText(child)) {
102
+ if (child.length === 0 || inText)
103
+ continue;
104
+ inText = true;
105
+ }
106
+ else {
107
+ inText = false;
108
+ }
109
+ if (++at === index)
110
+ return child;
111
+ }
112
+ return null;
113
+ }
114
+ /** The place in the document as it is now: its text may be in pieces, or in one. */
115
+ function nodeAt(point) {
116
+ let node = root;
117
+ for (const index of point.path) {
118
+ const child = childAt(node, index);
119
+ if (!child)
120
+ return null;
121
+ node = child;
122
+ }
123
+ if (isText(node)) {
124
+ let text = node;
125
+ let offset = point.offset;
126
+ while (offset > text.length && isText(text.nextSibling)) {
127
+ offset -= text.length;
128
+ text = text.nextSibling;
129
+ }
130
+ return { node: text, offset: Math.min(offset, text.length) };
131
+ }
132
+ const before = childAt(node, point.offset);
133
+ return { node, offset: before ? Array.prototype.indexOf.call(node.childNodes, before) : node.childNodes.length };
134
+ }
135
+ function selectionNow() {
136
+ const sel = view?.getSelection();
137
+ if (!sel || sel.rangeCount === 0 || !sel.anchorNode || !sel.focusNode)
138
+ return null;
139
+ if (!root.contains(sel.anchorNode) || !root.contains(sel.focusNode))
140
+ return null;
141
+ const anchor = pointOf(sel.anchorNode, sel.anchorOffset);
142
+ const focus = pointOf(sel.focusNode, sel.focusOffset);
143
+ return anchor && focus ? { anchor, focus } : null;
144
+ }
145
+ function select(kept) {
146
+ const sel = view?.getSelection();
147
+ if (!sel || !kept)
148
+ return;
149
+ const anchor = nodeAt(kept.anchor);
150
+ const focus = nodeAt(kept.focus);
151
+ if (!anchor || !focus)
152
+ return;
153
+ try {
154
+ sel.setBaseAndExtent(anchor.node, anchor.offset, focus.node, focus.offset);
155
+ }
156
+ catch {
157
+ // A place that is no longer one: the selection stays where the browser left it.
158
+ }
159
+ }
160
+ // ── What is kept ──
161
+ const take = () => ({ html: pack(root.innerHTML), selection: selectionNow() });
162
+ /** The documents before each step, oldest first; and those after each step undone. */
163
+ let before = [];
164
+ let after = [];
165
+ /** The document as it was when the last step ended. */
166
+ let base = take();
167
+ // ── Whether anything changed ──
168
+ // (Without a MutationObserver, every step compares the HTML, which says the same.)
169
+ let dirty = false;
170
+ const own = (record) => !(record.type === 'attributes' && record.target === root);
171
+ const Observer = view?.MutationObserver ?? (typeof MutationObserver !== 'undefined' ? MutationObserver : null);
172
+ const observer = Observer
173
+ ? new Observer((records) => {
174
+ if (records.some(own))
175
+ dirty = true;
176
+ })
177
+ : null;
178
+ observer?.observe(root, { childList: true, subtree: true, characterData: true, attributes: true });
179
+ function changed() {
180
+ if (!observer)
181
+ return true;
182
+ if (observer.takeRecords().some(own))
183
+ dirty = true;
184
+ return dirty;
185
+ }
186
+ function settled() {
187
+ observer?.takeRecords();
188
+ dirty = false;
189
+ }
190
+ // ── Steps ──
191
+ let run = null;
192
+ let caret = null;
193
+ let last = 0;
194
+ let timer;
195
+ let inComposition = false;
196
+ function step() {
197
+ clearTimeout(timer);
198
+ run = null;
199
+ caret = null;
200
+ if (!changed()) {
201
+ // Where the next step begins is where its Undo puts the caret back.
202
+ base = { html: base.html, selection: selectionNow() ?? base.selection };
203
+ return;
204
+ }
205
+ settled();
206
+ const now = take();
207
+ if (now.html === base.html) {
208
+ base = now;
209
+ return;
210
+ }
211
+ before.push(base);
212
+ if (before.length > limit)
213
+ before.shift();
214
+ after = [];
215
+ base = now;
216
+ }
217
+ function caretIsWhereLeft() {
218
+ const sel = view?.getSelection();
219
+ return !!caret && !!sel && sel.isCollapsed && sel.anchorNode === caret.node && sel.anchorOffset === caret.offset;
220
+ }
221
+ function typing(kind) {
222
+ const now = Date.now();
223
+ // What an input method composes is one run of typing, from its first input to its
224
+ // last: its text is replaced as it is composed, and the caret goes where the method
225
+ // puts it, neither of which ends a step in the middle of a word.
226
+ if (inComposition && run === 'insert') {
227
+ last = now;
228
+ return;
229
+ }
230
+ if (kind === null || kind !== run || now - last > pause || !caretIsWhereLeft())
231
+ step();
232
+ run = kind;
233
+ last = now;
234
+ }
235
+ function typed() {
236
+ const sel = view?.getSelection();
237
+ caret = sel?.anchorNode ? { node: sel.anchorNode, offset: sel.anchorOffset } : null;
238
+ last = Date.now();
239
+ clearTimeout(timer);
240
+ if (run === null)
241
+ return;
242
+ // A pause ends the step: it is kept while nothing is being typed.
243
+ const end = () => {
244
+ if (inComposition)
245
+ timer = setTimeout(end, pause);
246
+ else
247
+ step();
248
+ };
249
+ timer = setTimeout(end, pause);
250
+ }
251
+ // ── Putting a step back ──
252
+ function show(kept) {
253
+ const holder = doc.createElement('template');
254
+ holder.innerHTML = unpack(kept.html);
255
+ const wanted = Array.from(holder.content.childNodes);
256
+ const shown = Array.from(root.childNodes);
257
+ let same = 0;
258
+ while (same < wanted.length && same < shown.length && shown[same].isEqualNode(wanted[same]))
259
+ same++;
260
+ let wantedEnd = wanted.length;
261
+ let shownEnd = shown.length;
262
+ while (wantedEnd > same && shownEnd > same && shown[shownEnd - 1].isEqualNode(wanted[wantedEnd - 1])) {
263
+ wantedEnd--;
264
+ shownEnd--;
265
+ }
266
+ const next = shown[shownEnd] ?? null;
267
+ for (let i = same; i < shownEnd; i++)
268
+ root.removeChild(shown[i]);
269
+ for (let i = same; i < wantedEnd; i++)
270
+ root.insertBefore(wanted[i], next);
271
+ select(kept.selection);
272
+ }
273
+ function back(undo, then) {
274
+ // (Not while an input method composes: the document put back would be taken from under
275
+ // the text it holds, which it then writes somewhere that is no longer there.)
276
+ if (inComposition)
277
+ return false;
278
+ // (What changed since the last step is a step first: and then there is nothing to redo.)
279
+ step();
280
+ const kept = (undo ? before : after).pop();
281
+ if (!kept)
282
+ return false;
283
+ (undo ? after : before).push(base);
284
+ base = kept;
285
+ show(kept);
286
+ then?.();
287
+ // (What `then` made of it is the document that was put back: a check that took
288
+ // something out of it is no step to undo.)
289
+ if (changed())
290
+ base = { html: pack(root.innerHTML), selection: base.selection };
291
+ settled();
292
+ return true;
293
+ }
294
+ return {
295
+ step,
296
+ typing,
297
+ typed,
298
+ undo: (then) => back(true, then),
299
+ redo: (then) => back(false, then),
300
+ canUndo: () => before.length > 0 || changed(),
301
+ canRedo: () => after.length > 0 && !changed(),
302
+ quietly(change) {
303
+ const open = changed();
304
+ change();
305
+ if (open || !changed())
306
+ return;
307
+ settled();
308
+ base = { html: pack(root.innerHTML), selection: base.selection };
309
+ },
310
+ reset() {
311
+ clearTimeout(timer);
312
+ settled();
313
+ before = [];
314
+ after = [];
315
+ pictures.length = 0;
316
+ pictureIds.clear();
317
+ run = null;
318
+ caret = null;
319
+ base = take();
320
+ },
321
+ composing(on) {
322
+ inComposition = on;
323
+ },
324
+ destroy() {
325
+ clearTimeout(timer);
326
+ observer?.disconnect();
327
+ }
328
+ };
329
+ }
@@ -36,3 +36,29 @@ export interface LetterReading {
36
36
  * mixed case, more than two letters that are no Roman numeral.
37
37
  */
38
38
  export declare function letterReadings(letters: string): LetterReading[];
39
+ /** The least gap there is, in twips: 6 pt. Closer, a number runs into its text. */
40
+ export declare const LEAST_MARKER_GAP = 120;
41
+ /** The most, in twips: two inches. */
42
+ export declare const MOST_MARKER_GAP = 2880;
43
+ /**
44
+ * The gap a list sets after its markers, in twips, or null where it sets none: no
45
+ * `padding-left` of its own, or one out of the range above (a page's `padding-left: 0`).
46
+ */
47
+ export declare function markerGapTwips(list: Element): number | null;
48
+ /**
49
+ * Where the editor writes, for the screen, the marker an item of a list with a gap shows. A
50
+ * browser draws its own markers against the item's text, and cannot be told how far before
51
+ * it; so for such a list the editor draws them itself, at the list's edge (see the editor's
52
+ * style), from this. It is worked out here, as the RTF writer works it out: a browser's own
53
+ * count does not see an item's `value` in every browser. It is no part of the document, and
54
+ * the editor takes it out of what it gives (getHTML, onsave, the draft).
55
+ */
56
+ export declare const MARKER_ATTRIBUTE = "data-ink-marker";
57
+ /**
58
+ * Write the marker of each item of each list with a gap under `root`, and take it off the
59
+ * items of every other list. Only what differs is written, so that a document that has not
60
+ * changed is not changed by this either.
61
+ */
62
+ export declare function markItems(root: Element): void;
63
+ /** Take the editor's markers out of a copy of a document. */
64
+ export declare function withoutMarkers(root: Element): void;