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.
- package/README.md +116 -13
- package/dist/RtfViewer.svelte +32 -1
- package/dist/components/FontMenu.svelte +187 -0
- package/dist/components/FontMenu.svelte.d.ts +20 -0
- package/dist/components/ImageOverlay.svelte +153 -17
- package/dist/components/ImageOverlay.svelte.d.ts +23 -2
- package/dist/components/InkEditor.svelte +1398 -266
- package/dist/components/InkEditor.svelte.d.ts +14 -0
- package/dist/components/Toolbar.svelte +70 -2
- package/dist/components/Toolbar.svelte.d.ts +12 -0
- package/dist/history.d.ts +49 -0
- package/dist/history.js +329 -0
- package/dist/list-markers.d.ts +26 -0
- package/dist/list-markers.js +82 -0
- package/dist/lists.d.ts +48 -0
- package/dist/lists.js +576 -0
- package/dist/picture-rows.d.ts +49 -0
- package/dist/picture-rows.js +109 -0
- package/dist/rtf-parser.js +290 -38
- package/dist/rtf-writer.d.ts +10 -0
- package/dist/rtf-writer.js +255 -29
- package/dist/sanitize.js +85 -2
- package/dist/utils.js +41 -3
- package/package.json +1 -1
|
@@ -50,6 +50,12 @@
|
|
|
50
50
|
* any other font is taken off HTML as it comes in, set or pasted.
|
|
51
51
|
* Default: none: there is no font list, and no font but the
|
|
52
52
|
* document's is let in.
|
|
53
|
+
* fontMenu — If true, a right-click on the text opens the editor's own menu of
|
|
54
|
+
* `fonts` in place of the browser's, to set the font of the selected
|
|
55
|
+
* text. Shift with the right-click still opens the browser's menu (its
|
|
56
|
+
* spelling suggestions, Copy and Paste), and so does a right-click on a
|
|
57
|
+
* link or a picture. The menu key and Shift+F10 open it at the caret.
|
|
58
|
+
* Nothing changes without `fonts`, or when readonly. Default: false.
|
|
53
59
|
* keepAttributes — `data-` attributes the application's documents carry, which
|
|
54
60
|
* the editor leaves on their elements: a mark on a paragraph that
|
|
55
61
|
* the application reads back (`data-section="2"`). Every other
|
|
@@ -88,11 +94,16 @@
|
|
|
88
94
|
import { onMount } from 'svelte';
|
|
89
95
|
import Toolbar from './Toolbar.svelte';
|
|
90
96
|
import Modal from './Modal.svelte';
|
|
91
|
-
import
|
|
97
|
+
import FontMenu from './FontMenu.svelte';
|
|
98
|
+
import ImageOverlay, { type PicturePlace } from './ImageOverlay.svelte';
|
|
92
99
|
import { htmlToMarkdown, downloadFile } from '../utils.js';
|
|
93
100
|
import { keptMarks, pasteHasWords, pasteNotes, pasteWithoutFrames, removeWordHidden, sanitizedNodes, sanitizeTree, type PasteNotes } from '../sanitize.js';
|
|
94
101
|
import { givePastedPictures, pastePictures, type PastePictures } from '../paste-pictures.js';
|
|
95
102
|
import { readRtfFile, DEFAULT_MAX_RTF_BYTES } from '../rtf-parser.js';
|
|
103
|
+
import { createHistory, type DocumentHistory } from '../history.js';
|
|
104
|
+
import { GAPPED_ROW_STYLE, MOST_GAP_PT, isGapCell, isPlainCell, lengthTwips, pointsOf } from '../picture-rows.js';
|
|
105
|
+
import { toggleList, nestItems, unnestItems, leaveList, inEmptyItem, atItemStart, listKindAt, type ListKind } from '../lists.js';
|
|
106
|
+
import { LEAST_MARKER_GAP, MOST_MARKER_GAP, markerGapTwips, markItems, withoutMarkers, MARKER_ATTRIBUTE } from '../list-markers.js';
|
|
96
107
|
import {
|
|
97
108
|
htmlToRtf,
|
|
98
109
|
estimateRtfBytes,
|
|
@@ -103,7 +114,8 @@
|
|
|
103
114
|
fontKey,
|
|
104
115
|
normalizeFontSize,
|
|
105
116
|
DEFAULT_FONT_FAMILY,
|
|
106
|
-
DEFAULT_FONT_SIZE
|
|
117
|
+
DEFAULT_FONT_SIZE,
|
|
118
|
+
LEAST_LINES
|
|
107
119
|
} from '../rtf-writer.js';
|
|
108
120
|
import {
|
|
109
121
|
fileToImageSrc,
|
|
@@ -155,6 +167,8 @@
|
|
|
155
167
|
fontSizes?: number[];
|
|
156
168
|
/** The fonts offered for text, beside the document's own; none unless given. */
|
|
157
169
|
fonts?: string[];
|
|
170
|
+
/** A right-click on the text opens a menu of `fonts`; the browser's menu is Shift + right-click. */
|
|
171
|
+
fontMenu?: boolean;
|
|
158
172
|
/** `data-` attributes of the application's own, kept on HTML that is set or pasted. */
|
|
159
173
|
keepAttributes?: string[];
|
|
160
174
|
/** Class names of the application's own, kept the same way. */
|
|
@@ -198,6 +212,7 @@
|
|
|
198
212
|
fontSize = DEFAULT_FONT_SIZE,
|
|
199
213
|
fontSizes = DEFAULT_FONT_SIZES,
|
|
200
214
|
fonts = NO_KEPT_ATTRIBUTES,
|
|
215
|
+
fontMenu = false,
|
|
201
216
|
keepAttributes = NO_KEPT_ATTRIBUTES,
|
|
202
217
|
keepClasses = NO_KEPT_ATTRIBUTES,
|
|
203
218
|
onchange,
|
|
@@ -225,6 +240,8 @@
|
|
|
225
240
|
let currentBlock: string = $state('p');
|
|
226
241
|
/** The size of the text at the caret, in points, for the toolbar. */
|
|
227
242
|
let currentFontSize: number | null = $state(null);
|
|
243
|
+
/** The spacing of the paragraph at the caret, as its own style sets it: see spacingOf. */
|
|
244
|
+
let currentSpacing: ParagraphSpacing = $state({ line: null, before: 0, after: 0 });
|
|
228
245
|
/** The font at the selection, by its key: '' for the document font, null where it has more than one. */
|
|
229
246
|
let currentFont: string | null = $state('');
|
|
230
247
|
|
|
@@ -232,12 +249,19 @@
|
|
|
232
249
|
bold: false,
|
|
233
250
|
italic: false,
|
|
234
251
|
underline: false,
|
|
235
|
-
strikeThrough: false
|
|
252
|
+
strikeThrough: false,
|
|
253
|
+
bulletList: false,
|
|
254
|
+
numberedList: false
|
|
236
255
|
});
|
|
237
256
|
|
|
238
257
|
// Modal state
|
|
239
258
|
let linkModalOpen: boolean = $state(false);
|
|
240
259
|
let imageModalOpen: boolean = $state(false);
|
|
260
|
+
let tableModalOpen: boolean = $state(false);
|
|
261
|
+
let tableRows: number | null = $state(3);
|
|
262
|
+
let tableColumns: number | null = $state(3);
|
|
263
|
+
let tableBorders: boolean = $state(true);
|
|
264
|
+
let tableHeader: boolean = $state(false);
|
|
241
265
|
let linkUrl: string = $state('');
|
|
242
266
|
let linkError: string = $state('');
|
|
243
267
|
let linkText: string = $state('');
|
|
@@ -260,7 +284,85 @@
|
|
|
260
284
|
|
|
261
285
|
// ── Public API (exposed via bind:this) ──
|
|
262
286
|
export function getHTML(): string {
|
|
263
|
-
return
|
|
287
|
+
return documentHtml();
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** What a paragraph cannot hold in HTML: a parser that meets one ends the paragraph there. */
|
|
291
|
+
const BLOCKS = 'ul, ol, div, p, table, blockquote, pre, h1, h2, h3, h4, h5, h6, hr, figure';
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Put what no paragraph can hold beside the paragraph it is in, in the document itself.
|
|
295
|
+
*
|
|
296
|
+
* A browser leaves a block inside a paragraph in more than one way: Firefox leaves the
|
|
297
|
+
* paragraphs of a paste inside the paragraph they were pasted in, and Chromium's and
|
|
298
|
+
* Safari's list commands left the list inside the paragraph it was made from. No HTML can
|
|
299
|
+
* say that. Read again, by an application that keeps what getHTML() or onsave gave it, or
|
|
300
|
+
* by the editor's own Undo, it is an empty paragraph, the block, and another empty
|
|
301
|
+
* paragraph, and the RTF of that has an empty line before and after. So each such block is
|
|
302
|
+
* put where a parser would put it, with no empty paragraph around it; what the paragraph
|
|
303
|
+
* held before a block and after it are paragraphs of their own.
|
|
304
|
+
*
|
|
305
|
+
* `live` says it is the document being edited: the caret stays in the words it was in, and
|
|
306
|
+
* an empty line it is in stays to be typed in.
|
|
307
|
+
*/
|
|
308
|
+
function liftBlocks(root: HTMLElement, live: boolean): void {
|
|
309
|
+
if (!root.querySelector(`p > :is(${BLOCKS})`)) return;
|
|
310
|
+
const sel = live ? window.getSelection() : null;
|
|
311
|
+
const holds = (el: Element) => (el.textContent ?? '').trim() !== '' || !!el.querySelector(':not(br)');
|
|
312
|
+
const caretIn = (el: Element) => !!sel && sel.rangeCount > 0 && (el.contains(sel.anchorNode) || el.contains(sel.focusNode));
|
|
313
|
+
for (const paragraph of Array.from(root.querySelectorAll('p'))) {
|
|
314
|
+
if (!root.contains(paragraph) || !Array.from(paragraph.children).some((child) => child.matches(BLOCKS))) continue;
|
|
315
|
+
// A caret that is said by the paragraph itself (before its third child) is at that child.
|
|
316
|
+
const caretAt = sel && sel.isCollapsed && sel.anchorNode === paragraph ? { child: paragraph.childNodes[sel.anchorOffset] ?? null } : null;
|
|
317
|
+
const parts: Element[] = [];
|
|
318
|
+
const lines = new Set<Element>();
|
|
319
|
+
let line: Element | null = null;
|
|
320
|
+
for (const child of Array.from(paragraph.childNodes)) {
|
|
321
|
+
if (child instanceof Element && child.matches(BLOCKS)) {
|
|
322
|
+
line = null;
|
|
323
|
+
parts.push(child);
|
|
324
|
+
} else {
|
|
325
|
+
if (!line) {
|
|
326
|
+
line = paragraph.cloneNode(false) as Element;
|
|
327
|
+
lines.add(line);
|
|
328
|
+
parts.push(line);
|
|
329
|
+
}
|
|
330
|
+
line.appendChild(child);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
// (A line with nothing in it is the paragraph the block was put in: the empty line
|
|
334
|
+
// the browser left before it or after it. It is no paragraph; a comment in it,
|
|
335
|
+
// which is an application's own mark, stays where it was.)
|
|
336
|
+
const kept = parts.flatMap((part): Node[] =>
|
|
337
|
+
!lines.has(part) || holds(part) || caretIn(part) ? [part] : Array.from(part.childNodes).filter((node) => node.nodeType === Node.COMMENT_NODE)
|
|
338
|
+
);
|
|
339
|
+
paragraph.replaceWith(...kept);
|
|
340
|
+
if (!caretAt || !sel) continue;
|
|
341
|
+
const at = caretAt.child?.isConnected ? caretAt.child : kept[kept.length - 1];
|
|
342
|
+
if (!at) continue;
|
|
343
|
+
const caret = document.createRange();
|
|
344
|
+
if (at instanceof Text) caret.setStart(at, caretAt.child === at ? 0 : at.length);
|
|
345
|
+
else caret.selectNodeContents(at);
|
|
346
|
+
caret.collapse(caretAt.child === at);
|
|
347
|
+
sel.removeAllRanges();
|
|
348
|
+
sel.addRange(caret);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* The document as HTML that says what the document is: see liftBlocks. (The document is
|
|
354
|
+
* kept that way as it is edited. This is for one that is not, at the moment it is asked
|
|
355
|
+
* for: it is never handed out as HTML that reads back as another document.)
|
|
356
|
+
*/
|
|
357
|
+
function documentHtml(): string {
|
|
358
|
+
if (!editorEl) return '';
|
|
359
|
+
const marked = editorEl.querySelector(`[${MARKER_ATTRIBUTE}]`) !== null;
|
|
360
|
+
if (!marked && !editorEl.querySelector(`p > :is(${BLOCKS})`)) return editorEl.innerHTML;
|
|
361
|
+
const copy = editorEl.cloneNode(true) as HTMLElement;
|
|
362
|
+
liftBlocks(copy, false);
|
|
363
|
+
// (The markers the editor draws a list with a gap by are the screen's, not the document's.)
|
|
364
|
+
if (marked) withoutMarkers(copy);
|
|
365
|
+
return copy.innerHTML;
|
|
264
366
|
}
|
|
265
367
|
|
|
266
368
|
export function getText(): string {
|
|
@@ -272,6 +374,7 @@
|
|
|
272
374
|
selectedImage = null;
|
|
273
375
|
documentReplaced();
|
|
274
376
|
const shown = showDocument(html);
|
|
377
|
+
newDocument();
|
|
275
378
|
updateCounts();
|
|
276
379
|
if (!shown) {
|
|
277
380
|
// The caller is told, and the editor is empty: see showDocument.
|
|
@@ -366,6 +469,7 @@
|
|
|
366
469
|
} catch {
|
|
367
470
|
editorEl.innerHTML = EMPTY_DOCUMENT;
|
|
368
471
|
documentReplaced();
|
|
472
|
+
newDocument();
|
|
369
473
|
clearTimeout(saveTimer);
|
|
370
474
|
showError(NOT_SHOWN);
|
|
371
475
|
return false;
|
|
@@ -477,6 +581,7 @@
|
|
|
477
581
|
selectedImage = null;
|
|
478
582
|
documentReplaced();
|
|
479
583
|
editorEl.innerHTML = EMPTY_DOCUMENT;
|
|
584
|
+
newDocument();
|
|
480
585
|
dropDraft();
|
|
481
586
|
updateCounts();
|
|
482
587
|
fireChange();
|
|
@@ -491,7 +596,7 @@
|
|
|
491
596
|
if (!editorEl) return;
|
|
492
597
|
switch (format) {
|
|
493
598
|
case 'html': {
|
|
494
|
-
const html = `<!DOCTYPE html>\n<html lang="en">\n<head><meta charset="UTF-8"><title>Document</title></head>\n<body>\n${
|
|
599
|
+
const html = `<!DOCTYPE html>\n<html lang="en">\n<head><meta charset="UTF-8"><title>Document</title></head>\n<body>\n${documentHtml()}\n</body>\n</html>`;
|
|
495
600
|
downloadFile('document.html', html, 'text/html');
|
|
496
601
|
break;
|
|
497
602
|
}
|
|
@@ -515,11 +620,11 @@
|
|
|
515
620
|
|
|
516
621
|
/**
|
|
517
622
|
* The whole document is about to be replaced (setHTML, clear, an RTF import): nothing that
|
|
518
|
-
* points into the old one may act on the new one.
|
|
519
|
-
*
|
|
623
|
+
* points into the old one may act on the new one. The Copy format brush is put down, and a
|
|
624
|
+
* paste that inserted nothing is forgotten. (And once the new one is shown, the history is
|
|
625
|
+
* forgotten: see newDocument.)
|
|
520
626
|
*/
|
|
521
627
|
function documentReplaced(): void {
|
|
522
|
-
indentUndo = [];
|
|
523
628
|
pastedOver = null;
|
|
524
629
|
pastedLoss = null;
|
|
525
630
|
pastedPictures = null;
|
|
@@ -531,10 +636,17 @@
|
|
|
531
636
|
broughtIn.clear();
|
|
532
637
|
pastesWithWords.length = 0;
|
|
533
638
|
hiddenTaken.length = 0;
|
|
534
|
-
clearUndo = clearRedo = clearPast = null;
|
|
535
639
|
stopCopyFormat();
|
|
536
640
|
}
|
|
537
641
|
|
|
642
|
+
/**
|
|
643
|
+
* Another document is shown: Undo does not lead back into the one before it. (It is one
|
|
644
|
+
* patient's report, and this another's.)
|
|
645
|
+
*/
|
|
646
|
+
function newDocument(): void {
|
|
647
|
+
history?.reset();
|
|
648
|
+
}
|
|
649
|
+
|
|
538
650
|
// The frame around the paragraph copied from follows it when the text reflows: typing is caught
|
|
539
651
|
// by the input handler, and a picture finishing loading or the editor changing width by this.
|
|
540
652
|
$effect(() => {
|
|
@@ -631,26 +743,14 @@
|
|
|
631
743
|
// (<font style="font-size:14pt">), which is what the RTF writer reads. Only attributes are
|
|
632
744
|
// changed, never the elements, so the browser's undo still finds what it made.
|
|
633
745
|
//
|
|
634
|
-
//
|
|
635
|
-
//
|
|
636
|
-
//
|
|
637
|
-
//
|
|
638
|
-
//
|
|
639
|
-
//
|
|
640
|
-
// And undo is only right where the browser recorded something to put back. It records the
|
|
641
|
-
// size it takes off an element, not the one given here afterwards. So a <font> element is
|
|
642
|
-
// never left without a size of its own: where the text is to have the size of the text around
|
|
643
|
-
// it, the size stated is 1em, which is that size. The writer reads 1em as no size.
|
|
746
|
+
// A <font> element is never left without a size of its own: where the text is to have the
|
|
747
|
+
// size of the text around it, the size stated is 1em, which is that size. The writer reads
|
|
748
|
+
// 1em as no size. (The browser's Undo put back only a size its command had taken off an
|
|
749
|
+
// element, which is why one was always there; Undo is the editor's own now, see history.ts,
|
|
750
|
+
// and the HTML of a document is left as it has been.)
|
|
644
751
|
const SIZE_MARK = '7';
|
|
645
752
|
/** The size that is the size of the text around: what "no size of its own" is written as. */
|
|
646
753
|
const SIZE_AROUND = '1em';
|
|
647
|
-
/** One change of size: the elements it sized, each with the font-size it was given. */
|
|
648
|
-
type SizeStep = Map<HTMLElement, string>;
|
|
649
|
-
/** The changes of size made, in order; those from `sizeStepsApplied` on were undone and can be redone. */
|
|
650
|
-
let sizeSteps: SizeStep[] = [];
|
|
651
|
-
let sizeStepsApplied = 0;
|
|
652
|
-
/** Every element this editor has given a size: a mark on one of these is the browser redoing a step. */
|
|
653
|
-
const sizedHere = new WeakSet<Element>();
|
|
654
754
|
/** The size chosen and not yet given to an element: a caret has no text to carry it until something is typed. */
|
|
655
755
|
let pendingSize: number | null = null;
|
|
656
756
|
/** Elements that were marked size 7 before the command (pasted that way): not ours to change. */
|
|
@@ -671,10 +771,9 @@
|
|
|
671
771
|
const markedElements = (): HTMLElement[] =>
|
|
672
772
|
editorEl ? Array.from(editorEl.querySelectorAll<HTMLElement>(`font[size="${SIZE_MARK}"]`)) : [];
|
|
673
773
|
|
|
674
|
-
/** Give the elements the command marked the size that was chosen
|
|
774
|
+
/** Give the elements the command marked the size that was chosen. */
|
|
675
775
|
function applyPendingSize(): void {
|
|
676
776
|
if (!editorEl || pendingSize === null) return;
|
|
677
|
-
const step: SizeStep = new Map();
|
|
678
777
|
for (const font of markedElements()) {
|
|
679
778
|
if (markedBefore.has(font)) continue;
|
|
680
779
|
sizeElement(font, SIZE_AROUND);
|
|
@@ -682,39 +781,6 @@
|
|
|
682
781
|
// size, a heading to the heading's), it stays at the size around it: the text then
|
|
683
782
|
// follows the document's size, as text that was never sized does.
|
|
684
783
|
if (shownSize(font) !== pendingSize) sizeElement(font, `${pendingSize}pt`);
|
|
685
|
-
step.set(font, font.style.fontSize);
|
|
686
|
-
sizedHere.add(font);
|
|
687
|
-
}
|
|
688
|
-
if (step.size === 0) return;
|
|
689
|
-
// A new change: what was undone before it can no longer be redone.
|
|
690
|
-
sizeSteps = [...sizeSteps.slice(0, sizeStepsApplied), step];
|
|
691
|
-
sizeStepsApplied = sizeSteps.length;
|
|
692
|
-
}
|
|
693
|
-
|
|
694
|
-
/** Whether a change of size is in the document as it was made: every element there, with its size. */
|
|
695
|
-
function stepStands(step: SizeStep | undefined): boolean {
|
|
696
|
-
if (!step) return false;
|
|
697
|
-
for (const [font, fontSize] of step) {
|
|
698
|
-
if (!font.isConnected || font.getAttribute('size') === SIZE_MARK || font.style.fontSize !== fontSize) return false;
|
|
699
|
-
}
|
|
700
|
-
return true;
|
|
701
|
-
}
|
|
702
|
-
|
|
703
|
-
/** After an undo or a redo: follow which changes of size are in the document, and finish a redone one. */
|
|
704
|
-
function afterHistory(): void {
|
|
705
|
-
const next = sizeSteps[sizeStepsApplied];
|
|
706
|
-
const marked = markedElements().filter((font) => sizedHere.has(font));
|
|
707
|
-
if (marked.length > 0) {
|
|
708
|
-
// The browser redid its part of a change of size. (A mark with no step to go with it
|
|
709
|
-
// should not happen; it is then taken off, and the text has the size around it,
|
|
710
|
-
// which is better than 36 pt.)
|
|
711
|
-
for (const font of marked) sizeElement(font, next?.get(font) ?? SIZE_AROUND);
|
|
712
|
-
if (next) sizeStepsApplied++;
|
|
713
|
-
} else if (stepStands(next)) {
|
|
714
|
-
// Redone, and nothing was marked: the step only made elements, which came back as they were.
|
|
715
|
-
sizeStepsApplied++;
|
|
716
|
-
} else if (sizeStepsApplied > 0 && !stepStands(sizeSteps[sizeStepsApplied - 1])) {
|
|
717
|
-
sizeStepsApplied--;
|
|
718
784
|
}
|
|
719
785
|
}
|
|
720
786
|
|
|
@@ -742,8 +808,7 @@
|
|
|
742
808
|
if (typeof points !== 'number' || !Number.isFinite(points) || points <= 0) return;
|
|
743
809
|
restoreSelection();
|
|
744
810
|
editorEl.focus();
|
|
745
|
-
|
|
746
|
-
indentUndo = [];
|
|
811
|
+
history?.step();
|
|
747
812
|
const size = normalizeFontSize(points);
|
|
748
813
|
// Nothing to change: no step is made that would undo to the same thing.
|
|
749
814
|
if (selectionHasSize(size)) {
|
|
@@ -757,7 +822,7 @@
|
|
|
757
822
|
if (!font.style.fontSize && !markedBefore.has(font)) font.style.fontSize = SIZE_AROUND;
|
|
758
823
|
}
|
|
759
824
|
pendingSize = size;
|
|
760
|
-
|
|
825
|
+
browserCommand('fontSize', false, SIZE_MARK);
|
|
761
826
|
applyPendingSize();
|
|
762
827
|
// With text selected the size is on it now. At a caret it waits for the typing.
|
|
763
828
|
const selection = window.getSelection();
|
|
@@ -822,7 +887,7 @@
|
|
|
822
887
|
}
|
|
823
888
|
// (Given the document font as the screen has it, Chromium and Safari take the font off
|
|
824
889
|
// the text; Firefox writes it, which the writer and the font list read as no font.)
|
|
825
|
-
|
|
890
|
+
browserCommand('fontName', false, choice ? choice.css : screenFontFamily);
|
|
826
891
|
// With text selected the font is on it now. At a caret it waits for the typing.
|
|
827
892
|
pendingFont = selection.isCollapsed ? wanted : null;
|
|
828
893
|
updateToolbarState();
|
|
@@ -837,6 +902,53 @@
|
|
|
837
902
|
return '';
|
|
838
903
|
}
|
|
839
904
|
|
|
905
|
+
// ── The font menu (`fontMenu`) ──
|
|
906
|
+
// A right-click on the text opens it where the pointer is; the menu key, or Shift+F10, at the
|
|
907
|
+
// caret. It acts on the selection the right-click left: on a Mac the word it was on, elsewhere
|
|
908
|
+
// what was selected, or a caret where the pointer was.
|
|
909
|
+
|
|
910
|
+
/** Where the font menu is open, in the window's coordinates; null where it is not. */
|
|
911
|
+
let fontMenuAt: { x: number; y: number } | null = $state(null);
|
|
912
|
+
const fontMenuChoices = $derived([
|
|
913
|
+
{ key: '', name: fontChoices([fontFamily])[0]?.name ?? DEFAULT_FONT_FAMILY, css: screenFontFamily },
|
|
914
|
+
...fontList
|
|
915
|
+
]);
|
|
916
|
+
|
|
917
|
+
function openFontMenu(e: MouseEvent): void {
|
|
918
|
+
if (!fontMenu || readonly || fontList.length === 0 || !editorEl) return;
|
|
919
|
+
const target = e.target instanceof Element ? e.target : null;
|
|
920
|
+
// The browser's own menu: asked for with Shift, and the one with what a link or a picture needs.
|
|
921
|
+
if (e.shiftKey || target?.closest('a[href], img')) return;
|
|
922
|
+
e.preventDefault();
|
|
923
|
+
// (From the keyboard the event has no place of its own: the menu opens at the caret.)
|
|
924
|
+
let { clientX: x, clientY: y } = e;
|
|
925
|
+
const fromKeyboard = e.button !== 2 || (x === 0 && y === 0);
|
|
926
|
+
if (fromKeyboard) {
|
|
927
|
+
const selection = window.getSelection();
|
|
928
|
+
const range = selection && selection.rangeCount > 0 && selectionInEditor() ? selection.getRangeAt(0) : null;
|
|
929
|
+
const box = range?.getClientRects()[0] ?? range?.getBoundingClientRect() ?? editorEl.getBoundingClientRect();
|
|
930
|
+
x = box.left;
|
|
931
|
+
y = box.bottom + 4;
|
|
932
|
+
}
|
|
933
|
+
saveSelection();
|
|
934
|
+
currentFont = fontAtSelection();
|
|
935
|
+
fontMenuAt = { x, y };
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
function closeFontMenu(refocus: boolean): void {
|
|
939
|
+
if (!fontMenuAt) return;
|
|
940
|
+
fontMenuAt = null;
|
|
941
|
+
if (refocus && editorEl) {
|
|
942
|
+
restoreSelection();
|
|
943
|
+
editorEl.focus({ preventScroll: true });
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
function pickFont(key: string): void {
|
|
948
|
+
fontMenuAt = null;
|
|
949
|
+
setFont(key);
|
|
950
|
+
}
|
|
951
|
+
|
|
840
952
|
/** The font of the text at the selection, for the toolbar: null where the text selected is in more than one. */
|
|
841
953
|
function fontAtSelection(): string | null {
|
|
842
954
|
if (pendingFont !== null) return pendingFont;
|
|
@@ -931,8 +1043,15 @@
|
|
|
931
1043
|
clearFormatting();
|
|
932
1044
|
return;
|
|
933
1045
|
}
|
|
1046
|
+
if (cmd === 'undo' || cmd === 'redo') {
|
|
1047
|
+
stepBack(cmd === 'undo');
|
|
1048
|
+
return;
|
|
1049
|
+
}
|
|
934
1050
|
restoreSelection();
|
|
935
1051
|
editorEl?.focus();
|
|
1052
|
+
// Each command is a step of its own in the history: what was typed before it is the
|
|
1053
|
+
// step before.
|
|
1054
|
+
history?.step();
|
|
936
1055
|
// Color commands require an actual text selection — a collapsed cursor
|
|
937
1056
|
// would silently apply the color to nothing (or to subsequently typed
|
|
938
1057
|
// text), which causes accidental color spans to appear in the RTF output.
|
|
@@ -948,20 +1067,157 @@
|
|
|
948
1067
|
hangAtCaret();
|
|
949
1068
|
return;
|
|
950
1069
|
}
|
|
951
|
-
if (cmd === '
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
1070
|
+
if (cmd === 'markerGap') {
|
|
1071
|
+
const n = value === null || value === '' ? null : Number(value);
|
|
1072
|
+
if (n !== null && !Number.isFinite(n)) return;
|
|
1073
|
+
setListMarkerGap(n);
|
|
1074
|
+
return;
|
|
1075
|
+
}
|
|
1076
|
+
if (cmd === 'lineSpacing' || cmd === 'spaceBefore' || cmd === 'spaceAfter') {
|
|
1077
|
+
const n = value === null || value === '' ? null : Number(value);
|
|
1078
|
+
if (n !== null && !Number.isFinite(n)) return;
|
|
1079
|
+
if (cmd === 'lineSpacing') applySpacing({ line: n });
|
|
1080
|
+
else applySpacing(cmd === 'spaceBefore' ? { before: n } : { after: n });
|
|
1081
|
+
return;
|
|
1082
|
+
}
|
|
1083
|
+
if (cmd === 'insertUnorderedList' || cmd === 'insertOrderedList') {
|
|
1084
|
+
listCommand(cmd === 'insertOrderedList' ? 'ol' : 'ul');
|
|
1085
|
+
return;
|
|
1086
|
+
}
|
|
1087
|
+
browserCommand(cmd, false, value ?? undefined);
|
|
955
1088
|
updateToolbarState();
|
|
956
1089
|
}
|
|
957
1090
|
|
|
1091
|
+
// ── Undo and Redo ──
|
|
1092
|
+
// The editor's own (see history.ts): the browser's history knows nothing of a picture or a
|
|
1093
|
+
// table put in, a picture moved or an indent, and its Undo went past them to the typing
|
|
1094
|
+
// before. Every way to it leads here: the toolbar, the keys, and the browser's own menu.
|
|
1095
|
+
let history: DocumentHistory | null = null;
|
|
1096
|
+
|
|
1097
|
+
/** Undo the last step, or redo the one last undone. Returns whether there was one. */
|
|
1098
|
+
function stepBack(undo: boolean): boolean {
|
|
1099
|
+
if (readonly || !editorEl || !history) return false;
|
|
1100
|
+
let shown = true;
|
|
1101
|
+
const done = (undo ? history.undo : history.redo)(() => {
|
|
1102
|
+
// Nothing that points into the document as it was may act on it as it is now.
|
|
1103
|
+
selectedImage = null;
|
|
1104
|
+
pendingSize = null;
|
|
1105
|
+
pendingFont = null;
|
|
1106
|
+
pastedOver = null;
|
|
1107
|
+
pastedLoss = null;
|
|
1108
|
+
pastedPictures = null;
|
|
1109
|
+
commentsBefore = null;
|
|
1110
|
+
savedSelection = null;
|
|
1111
|
+
dialogSelection = null;
|
|
1112
|
+
if (copySource && !copySource.isConnected) stopCopyFormat();
|
|
1113
|
+
// What is put back was checked when it was first in the document; it is HTML that
|
|
1114
|
+
// was kept as text all the same, and is checked as any other is.
|
|
1115
|
+
shown = checkDocument();
|
|
1116
|
+
});
|
|
1117
|
+
if (!done) return false;
|
|
1118
|
+
// (A document that could not be checked is not shown, and has no history.)
|
|
1119
|
+
if (!shown) history.reset();
|
|
1120
|
+
if (document.activeElement !== editorEl && selectionInEditor()) editorEl.focus({ preventScroll: true });
|
|
1121
|
+
placeCopySourceBox();
|
|
1122
|
+
overlayRef?.reposition();
|
|
1123
|
+
updateToolbarState();
|
|
1124
|
+
keepCaretInSight();
|
|
1125
|
+
fireChange();
|
|
1126
|
+
if (autosave && shown) scheduleAutoSave();
|
|
1127
|
+
return true;
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
/** Undo the last step: what was typed, a command, a picture or a table put in, a picture moved. */
|
|
1131
|
+
export function undo(): boolean {
|
|
1132
|
+
return stepBack(true);
|
|
1133
|
+
}
|
|
1134
|
+
|
|
1135
|
+
/** Redo the step last undone. */
|
|
1136
|
+
export function redo(): boolean {
|
|
1137
|
+
return stepBack(false);
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1140
|
+
/**
|
|
1141
|
+
* A command of the browser's, run by the editor. Safari says what such a command is about
|
|
1142
|
+
* to do as it says a key: heard as one, it ended the step the editor's command was in the
|
|
1143
|
+
* middle of, and a Clear formatting was two steps to undo, the first of them half done. So
|
|
1144
|
+
* what is said while one runs is not input to the history.
|
|
1145
|
+
*/
|
|
1146
|
+
let inCommand = 0;
|
|
1147
|
+
function browserCommand(cmd: string, ui = false, value?: string): boolean {
|
|
1148
|
+
inCommand++;
|
|
1149
|
+
try {
|
|
1150
|
+
return document.execCommand(cmd, ui, value);
|
|
1151
|
+
} finally {
|
|
1152
|
+
inCommand--;
|
|
1153
|
+
}
|
|
1154
|
+
}
|
|
1155
|
+
|
|
1156
|
+
/**
|
|
1157
|
+
* Take back the browser's own last command, inside a command of the editor's that tried
|
|
1158
|
+
* it and found it wanting. (The browser's history is used for nothing else.)
|
|
1159
|
+
*/
|
|
1160
|
+
function browserUndo(): void {
|
|
1161
|
+
browserCommand('undo');
|
|
1162
|
+
}
|
|
1163
|
+
|
|
1164
|
+
/** What the browser is about to do to the document: the history is told, and Undo and Redo are the editor's. */
|
|
1165
|
+
function handleBeforeInput(e: InputEvent): void {
|
|
1166
|
+
const kind = e.inputType;
|
|
1167
|
+
if (inCommand > 0) return;
|
|
1168
|
+
if (kind === 'historyUndo' || kind === 'historyRedo') {
|
|
1169
|
+
e.preventDefault();
|
|
1170
|
+
stepBack(kind === 'historyUndo');
|
|
1171
|
+
return;
|
|
1172
|
+
}
|
|
1173
|
+
if (readonly) return;
|
|
1174
|
+
// Words dragged from one place in the document to another are two inputs, the words
|
|
1175
|
+
// taken out and the words dropped: one move, and one step. (As two, the first Undo took
|
|
1176
|
+
// the dropped words out, and they were nowhere until the second.)
|
|
1177
|
+
const dropAfterTaking = kind === 'insertFromDrop' && dragTaken;
|
|
1178
|
+
dragTaken = kind === 'deleteByDrag';
|
|
1179
|
+
if (dropAfterTaking) return;
|
|
1180
|
+
// (Text an input method composes, and what Safari makes of it at the end, is typing.)
|
|
1181
|
+
const typed = /^insert(Text|CompositionText|ReplacementText|FromComposition)$/.test(kind) || kind === 'deleteCompositionText';
|
|
1182
|
+
const deleted = /^delete(Content|Word|SoftLine|HardLine)/.test(kind);
|
|
1183
|
+
history?.typing(typed ? 'insert' : deleted ? 'delete' : null);
|
|
1184
|
+
}
|
|
1185
|
+
|
|
1186
|
+
/** The last input was words dragged in the document taken out where they were: the drop that follows is the same step. */
|
|
1187
|
+
let dragTaken = false;
|
|
1188
|
+
|
|
1189
|
+
// ── Lists ──
|
|
1190
|
+
// The editor's own (see lists.ts): each browser's list command made something else of the
|
|
1191
|
+
// same click, and none of them puts an item under the one before it.
|
|
1192
|
+
|
|
1193
|
+
/** The toolbar's list buttons: make the lines selected a list of this kind, or paragraphs again. */
|
|
1194
|
+
function listCommand(kind: ListKind): void {
|
|
1195
|
+
if (readonly || !editorEl) return;
|
|
1196
|
+
if (toggleList(editorEl, window.getSelection(), kind)) indentChanged();
|
|
1197
|
+
else updateToolbarState();
|
|
1198
|
+
}
|
|
1199
|
+
|
|
1200
|
+
/**
|
|
1201
|
+
* Enter in an item with nothing in it, and Backspace at the start of an item, take the item
|
|
1202
|
+
* one step out of its list: out of a list within a list, and then a paragraph. Returns
|
|
1203
|
+
* whether the key was that.
|
|
1204
|
+
*/
|
|
1205
|
+
function leaveListAt(e: KeyboardEvent): boolean {
|
|
1206
|
+
if (readonly || !editorEl || e.isComposing || e.ctrlKey || e.metaKey || e.altKey || e.shiftKey) return false;
|
|
1207
|
+
const sel = window.getSelection();
|
|
1208
|
+
if (e.key === 'Enter' ? !inEmptyItem(editorEl, sel) : e.key !== 'Backspace' || !atItemStart(editorEl, sel)) return false;
|
|
1209
|
+
e.preventDefault();
|
|
1210
|
+
history?.step();
|
|
1211
|
+
if (leaveList(editorEl, sel)) indentChanged();
|
|
1212
|
+
return true;
|
|
1213
|
+
}
|
|
1214
|
+
|
|
958
1215
|
// ── Clear formatting ──
|
|
959
1216
|
// The browser's own command does this, and each browser does it differently. Chromium and
|
|
960
1217
|
// Safari take a paragraph's alignment and first-line indent with its color; Firefox leaves
|
|
961
1218
|
// the color on the paragraph, and takes out the element an application's mark is on; Safari
|
|
962
1219
|
// leaves a size where it is. So the command is used for what only it can do, which is to
|
|
963
|
-
// take the <b>, <i> and <font> elements out
|
|
964
|
-
// (taken out by the editor instead, the next Undo in Safari empties the document). What is
|
|
1220
|
+
// take the <b>, <i> and <font> elements out, cutting them where the selection ends. What is
|
|
965
1221
|
// written on the elements is the editor's to change: before the command, the looks of the
|
|
966
1222
|
// text are taken off every element whose text is all selected, and after it, whatever the
|
|
967
1223
|
// command took off that is not a look of the text is written back.
|
|
@@ -982,49 +1238,9 @@
|
|
|
982
1238
|
const LOOKS = ['style', 'color', 'size', 'face'] as const;
|
|
983
1239
|
type Looks = Record<(typeof LOOKS)[number], string | null>;
|
|
984
1240
|
|
|
985
|
-
/** One Clear formatting: what was written on each element before and after, to put back. */
|
|
986
|
-
interface ClearStep {
|
|
987
|
-
before: Map<Element, Looks>;
|
|
988
|
-
after: Map<Element, Looks>;
|
|
989
|
-
/** The browser's command changed something, and has an entry in its history. */
|
|
990
|
-
native: boolean;
|
|
991
|
-
/** The document as the editor handed it to the command: where the browser's Undo of the command leads back to. */
|
|
992
|
-
pre: string;
|
|
993
|
-
/** The document as the step left it. */
|
|
994
|
-
cleared: string;
|
|
995
|
-
/** The document as it is now, the step done or undone: it is undone, or redone, only from there. */
|
|
996
|
-
html: string;
|
|
997
|
-
}
|
|
998
|
-
let clearUndo: ClearStep | null = null;
|
|
999
|
-
let clearRedo: ClearStep | null = null;
|
|
1000
|
-
/** The last one made, also after more was typed: the browser's Undo may yet lead back to it. */
|
|
1001
|
-
let clearPast: ClearStep | null = null;
|
|
1002
1241
|
/** The input events of the editor's own commands, while it clears: what they leave is not yet the document. */
|
|
1003
1242
|
let clearing = false;
|
|
1004
1243
|
|
|
1005
|
-
/**
|
|
1006
|
-
* After the browser's own Undo. A Clear formatting is the editor's to undo only while nothing
|
|
1007
|
-
* else has been done. After that the browser undoes its command with everything else, and
|
|
1008
|
-
* knows nothing of what the editor took off the elements: bold was back, and the color was
|
|
1009
|
-
* not. Where its Undo has led back to the document as the command was given it, the rest is
|
|
1010
|
-
* put back here. A step the command left nothing of (a paragraph's color and no more) has no
|
|
1011
|
-
* such place to be led back to: it is the editor's to undo again once the document is as
|
|
1012
|
-
* that step left it, which is when what was typed after it has been undone.
|
|
1013
|
-
*/
|
|
1014
|
-
function afterUndoOfClear(): void {
|
|
1015
|
-
const step = clearPast;
|
|
1016
|
-
if (!step || !editorEl || clearing) return;
|
|
1017
|
-
const now = editorEl.innerHTML;
|
|
1018
|
-
if (step.native && step.pre !== step.cleared) {
|
|
1019
|
-
if (now !== step.pre) return;
|
|
1020
|
-
writeLooks(step.before);
|
|
1021
|
-
clearUndo = null;
|
|
1022
|
-
clearRedo = { ...step, html: editorEl.innerHTML };
|
|
1023
|
-
} else if (now === step.cleared) {
|
|
1024
|
-
clearUndo = { ...step, html: now };
|
|
1025
|
-
}
|
|
1026
|
-
}
|
|
1027
|
-
|
|
1028
1244
|
const looksOf = (el: Element): Looks => ({
|
|
1029
1245
|
style: el.getAttribute('style'),
|
|
1030
1246
|
color: el.getAttribute('color'),
|
|
@@ -1032,17 +1248,6 @@
|
|
|
1032
1248
|
face: el.getAttribute('face')
|
|
1033
1249
|
});
|
|
1034
1250
|
|
|
1035
|
-
function writeLooks(looks: Map<Element, Looks>): void {
|
|
1036
|
-
for (const [el, wanted] of looks) {
|
|
1037
|
-
if (!el.isConnected) continue;
|
|
1038
|
-
for (const name of LOOKS) {
|
|
1039
|
-
if (el.getAttribute(name) === wanted[name]) continue;
|
|
1040
|
-
if (wanted[name] === null) el.removeAttribute(name);
|
|
1041
|
-
else el.setAttribute(name, wanted[name]);
|
|
1042
|
-
}
|
|
1043
|
-
}
|
|
1044
|
-
}
|
|
1045
|
-
|
|
1046
1251
|
/** Whether all the text of an element is selected. (One with no text is, if it is in the selection.) */
|
|
1047
1252
|
function allSelected(range: Range, el: Element): boolean {
|
|
1048
1253
|
const walker = document.createTreeWalker(el, NodeFilter.SHOW_TEXT);
|
|
@@ -1096,12 +1301,12 @@
|
|
|
1096
1301
|
// At a caret there is nothing to clear but what was chosen for the words typed next.
|
|
1097
1302
|
pendingSize = null;
|
|
1098
1303
|
pendingFont = null;
|
|
1099
|
-
|
|
1304
|
+
history?.step();
|
|
1305
|
+
browserCommand('removeFormat');
|
|
1100
1306
|
updateToolbarState();
|
|
1101
1307
|
return;
|
|
1102
1308
|
}
|
|
1103
|
-
|
|
1104
|
-
indentUndo = [];
|
|
1309
|
+
history?.step();
|
|
1105
1310
|
const range = sel.getRangeAt(0);
|
|
1106
1311
|
const touched = Array.from(editorEl.querySelectorAll<HTMLElement>('*')).filter((el) => range.intersectsNode(el));
|
|
1107
1312
|
const before = new Map(touched.map((el) => [el, looksOf(el)]));
|
|
@@ -1126,7 +1331,7 @@
|
|
|
1126
1331
|
removeFormat(selected, kept, closed);
|
|
1127
1332
|
const lost = kept.filter((el) => !el.isConnected || el.textContent !== marks.get(el));
|
|
1128
1333
|
if (lost.length > 0) {
|
|
1129
|
-
|
|
1334
|
+
browserUndo();
|
|
1130
1335
|
if (lost.some((el) => el.tagName === 'SPAN')) spansAreTakenOut = true;
|
|
1131
1336
|
closed = Array.from(new Set([...closed, ...lost])).filter((el) => el.isConnected);
|
|
1132
1337
|
removeFormat(selected, kept.filter((el) => el.isConnected), closed);
|
|
@@ -1151,9 +1356,6 @@
|
|
|
1151
1356
|
for (const el of whole) if (el.isConnected) plain(el);
|
|
1152
1357
|
const after = new Map(touched.map((el) => [el, looksOf(el)]));
|
|
1153
1358
|
const changed = native || touched.some((el) => LOOKS.some((name) => before.get(el)![name] !== after.get(el)![name]));
|
|
1154
|
-
const cleared = editorEl.innerHTML;
|
|
1155
|
-
clearUndo = clearPast = changed ? { before, after, native, pre: html, cleared, html: cleared } : null;
|
|
1156
|
-
clearRedo = null;
|
|
1157
1359
|
updateToolbarState();
|
|
1158
1360
|
if (!changed) return;
|
|
1159
1361
|
fireChange();
|
|
@@ -1186,7 +1388,7 @@
|
|
|
1186
1388
|
const was = closed.map((el) => el.getAttribute('contenteditable'));
|
|
1187
1389
|
for (const el of kept) el.setAttribute(KEEP_MARK, '');
|
|
1188
1390
|
for (const el of closed) el.setAttribute('contenteditable', 'false');
|
|
1189
|
-
|
|
1391
|
+
browserCommand('removeFormat');
|
|
1190
1392
|
// (Also off the copy the command makes of an element it cuts in two.)
|
|
1191
1393
|
for (const el of [...kept, ...Array.from(editorEl?.querySelectorAll(`[${KEEP_MARK}]`) ?? [])]) el.removeAttribute(KEEP_MARK);
|
|
1192
1394
|
closed.forEach((el, i) => {
|
|
@@ -1195,57 +1397,12 @@
|
|
|
1195
1397
|
});
|
|
1196
1398
|
}
|
|
1197
1399
|
|
|
1198
|
-
/** Undo the last Clear formatting, if nothing has changed since. */
|
|
1199
|
-
function undoClear(): boolean {
|
|
1200
|
-
const step = clearUndo;
|
|
1201
|
-
clearUndo = null;
|
|
1202
|
-
if (!step || !editorEl || editorEl.innerHTML !== step.html) return false;
|
|
1203
|
-
clearing = true;
|
|
1204
|
-
try {
|
|
1205
|
-
if (step.native) document.execCommand('undo');
|
|
1206
|
-
} finally {
|
|
1207
|
-
clearing = false;
|
|
1208
|
-
}
|
|
1209
|
-
writeLooks(step.before);
|
|
1210
|
-
// (Safari may take the step before back with it, and that may be a change of size.)
|
|
1211
|
-
afterHistory();
|
|
1212
|
-
clearRedo = { ...step, html: editorEl.innerHTML };
|
|
1213
|
-
clearChanged();
|
|
1214
|
-
return true;
|
|
1215
|
-
}
|
|
1216
|
-
|
|
1217
|
-
/** Do it again, if nothing has changed since it was undone. */
|
|
1218
|
-
function redoClear(): boolean {
|
|
1219
|
-
const step = clearRedo;
|
|
1220
|
-
clearRedo = null;
|
|
1221
|
-
if (!step || !editorEl || editorEl.innerHTML !== step.html) return false;
|
|
1222
|
-
// (In the order it was done: the command read the elements as the editor had left them.)
|
|
1223
|
-
writeLooks(step.after);
|
|
1224
|
-
clearing = true;
|
|
1225
|
-
try {
|
|
1226
|
-
if (step.native) document.execCommand('redo');
|
|
1227
|
-
} finally {
|
|
1228
|
-
clearing = false;
|
|
1229
|
-
}
|
|
1230
|
-
writeLooks(step.after);
|
|
1231
|
-
clearUndo = { ...step, html: editorEl.innerHTML };
|
|
1232
|
-
clearChanged();
|
|
1233
|
-
return true;
|
|
1234
|
-
}
|
|
1235
|
-
|
|
1236
|
-
function clearChanged(): void {
|
|
1237
|
-
updateToolbarState();
|
|
1238
|
-
fireChange();
|
|
1239
|
-
if (autosave) scheduleAutoSave();
|
|
1240
|
-
}
|
|
1241
|
-
|
|
1242
1400
|
// ── Indent ──
|
|
1243
1401
|
// Indent and Outdent move the paragraphs the selection touches by half an inch (48px, written
|
|
1244
1402
|
// to RTF as \li720) per press, as margin-left on each paragraph. The browser's own commands are
|
|
1245
1403
|
// not used: Chromium's Outdent strips the first paragraph to loose text ("one<br>"), and the
|
|
1246
1404
|
// browsers disagree on the markup. Only a style changes (and a line of loose text gets a
|
|
1247
|
-
// paragraph of its own)
|
|
1248
|
-
// these steps until the next edit.
|
|
1405
|
+
// paragraph of its own). Each press is a step of the editor's history.
|
|
1249
1406
|
//
|
|
1250
1407
|
// The right indent and the hanging indent move by the same step, on a paragraph (a <p>, or a
|
|
1251
1408
|
// <div> that holds text) outside a table cell, which is where the RTF writer sends them:
|
|
@@ -1272,23 +1429,20 @@
|
|
|
1272
1429
|
/** CSS lengths a margin is written in, in px. */
|
|
1273
1430
|
const PX_PER_UNIT: Record<string, number> = { px: 1, pt: 96 / 72, in: 96, cm: 96 / 2.54, mm: 96 / 25.4 };
|
|
1274
1431
|
|
|
1275
|
-
/** One block an Indent or Outdent changed, and how to put it back. */
|
|
1276
|
-
interface IndentChange {
|
|
1277
|
-
el: HTMLElement;
|
|
1278
|
-
before: string | null;
|
|
1279
|
-
after: string | null;
|
|
1280
|
-
/** The block is a paragraph this step made around loose text, and the <br> that ended it. */
|
|
1281
|
-
wrapped: { br: HTMLBRElement | null } | null;
|
|
1282
|
-
/** The paragraph's data-rtf-tab-stops before a step that changed it. */
|
|
1283
|
-
stops?: { before: string | null; after: string | null };
|
|
1284
|
-
}
|
|
1285
|
-
let indentUndo: IndentChange[][] = [];
|
|
1286
|
-
|
|
1287
1432
|
function indentBlocks(cmd: IndentCommand): void {
|
|
1288
1433
|
if (readonly || !editorEl) return;
|
|
1289
1434
|
editorEl.focus();
|
|
1290
1435
|
const sel = window.getSelection();
|
|
1291
1436
|
if (!sel || sel.rangeCount === 0 || !sel.anchorNode || !editorEl.contains(sel.anchorNode)) return;
|
|
1437
|
+
history?.step();
|
|
1438
|
+
// In a list, an indent puts the items under the item before them, and an outdent
|
|
1439
|
+
// takes them out from under it, as a word processor's do. (The first item of a list
|
|
1440
|
+
// has none before it, and an item of a list in no other list is under none: there
|
|
1441
|
+
// the list itself is moved, as a paragraph is.)
|
|
1442
|
+
if ((cmd === 'indent' && nestItems(editorEl, sel)) || (cmd === 'outdent' && unnestItems(editorEl, sel))) {
|
|
1443
|
+
indentChanged();
|
|
1444
|
+
return;
|
|
1445
|
+
}
|
|
1292
1446
|
const { anchorNode, anchorOffset, focusNode, focusOffset } = sel;
|
|
1293
1447
|
const wrapped = new Map<HTMLElement, HTMLBRElement | null>();
|
|
1294
1448
|
const targets = indentTargets(sel.getRangeAt(0), cmd, wrapped);
|
|
@@ -1297,9 +1451,8 @@
|
|
|
1297
1451
|
if (wrapped.size > 0 && anchorNode instanceof Text && focusNode instanceof Text && anchorNode.isConnected && focusNode.isConnected) {
|
|
1298
1452
|
sel.setBaseAndExtent(anchorNode, anchorOffset, focusNode, focusOffset);
|
|
1299
1453
|
}
|
|
1300
|
-
|
|
1454
|
+
let moved = wrapped.size > 0;
|
|
1301
1455
|
for (const el of targets) {
|
|
1302
|
-
const before = el.getAttribute('style');
|
|
1303
1456
|
const left = marginLeftPx(el);
|
|
1304
1457
|
const first = textIndentPx(el);
|
|
1305
1458
|
const setPx = (prop: 'marginLeft' | 'marginRight' | 'textIndent', px: number) => {
|
|
@@ -1329,11 +1482,189 @@
|
|
|
1329
1482
|
setPx('textIndent', first + back);
|
|
1330
1483
|
}
|
|
1331
1484
|
if (!el.getAttribute('style')) el.removeAttribute('style');
|
|
1332
|
-
|
|
1333
|
-
step.push({ el, before, after: el.getAttribute('style'), wrapped: br === undefined ? null : { br } });
|
|
1485
|
+
moved = true;
|
|
1334
1486
|
}
|
|
1335
|
-
if (
|
|
1336
|
-
|
|
1487
|
+
if (!moved) return;
|
|
1488
|
+
indentChanged();
|
|
1489
|
+
}
|
|
1490
|
+
|
|
1491
|
+
// ── Spacing ──
|
|
1492
|
+
// The space before and after a paragraph, and between its lines, as a style on each
|
|
1493
|
+
// paragraph the selection touches: a paragraph, a heading, and a picture (which has the
|
|
1494
|
+
// space around it, and no lines). It is what the RTF writer sends:
|
|
1495
|
+
//
|
|
1496
|
+
// | Set | Style on the paragraph | RTF |
|
|
1497
|
+
// |----------------------|------------------------|-------------------|
|
|
1498
|
+
// | 6 pt before | margin-top: 6pt | \sb120 |
|
|
1499
|
+
// | 6 pt after | margin-bottom: 6pt | \sa120 |
|
|
1500
|
+
// | lines 1.5 apart | line-height: 1.5 | \sl360\slmult1 |
|
|
1501
|
+
//
|
|
1502
|
+
// A list item is spaced as one paragraph: the style is on the <li>, as Word's list
|
|
1503
|
+
// paragraphs carry it. In a table cell, the paragraph the caret is in: a line typed straight
|
|
1504
|
+
// into the cell is made a paragraph first. Each change is one step of the editor's history,
|
|
1505
|
+
// as an indent is.
|
|
1506
|
+
|
|
1507
|
+
/** A paragraph's spacing as its own style sets it: lines apart (null: the editor's own), and points before and after. */
|
|
1508
|
+
interface ParagraphSpacing {
|
|
1509
|
+
line: number | null;
|
|
1510
|
+
before: number;
|
|
1511
|
+
after: number;
|
|
1512
|
+
/** In a list: the gap after its markers, in points (null: it sets none). Left out elsewhere. */
|
|
1513
|
+
gap?: number | null;
|
|
1514
|
+
}
|
|
1515
|
+
/** The most that is set: ten lines, and a space of two inches. */
|
|
1516
|
+
const MOST_LINES = 10;
|
|
1517
|
+
const MOST_SPACE_PT = 144;
|
|
1518
|
+
|
|
1519
|
+
/** A margin a style sets, in points; 0 where it sets none, or one that cannot be read. */
|
|
1520
|
+
function spacePt(value: string): number {
|
|
1521
|
+
const m = value.trim().toLowerCase().match(/^(\d+(?:\.\d+)?|\.\d+)(px|pt|in|cm|mm)$/);
|
|
1522
|
+
return m ? Math.round(((parseFloat(m[1]) * PX_PER_UNIT[m[2]]) / PX_PER_UNIT.pt) * 100) / 100 : 0;
|
|
1523
|
+
}
|
|
1524
|
+
|
|
1525
|
+
function spacingOf(el: HTMLElement): ParagraphSpacing {
|
|
1526
|
+
const line = el.style.lineHeight.trim();
|
|
1527
|
+
const lines = /^(\d+(\.\d+)?|\.\d+)%?$/.test(line) ? parseFloat(line) / (line.endsWith('%') ? 100 : 1) : null;
|
|
1528
|
+
return { line: lines !== null && lines > 0 ? Math.round(lines * 100) / 100 : null, before: spacePt(el.style.marginTop), after: spacePt(el.style.marginBottom) };
|
|
1529
|
+
}
|
|
1530
|
+
|
|
1531
|
+
/** The paragraph, heading, list item or picture the caret is in, where spacing can be set on it. */
|
|
1532
|
+
function spacedBlockAtCaret(): HTMLElement | null {
|
|
1533
|
+
if (!editorEl) return null;
|
|
1534
|
+
const node = selectedImage ?? window.getSelection()?.anchorNode ?? null;
|
|
1535
|
+
if (!node || !editorEl.contains(node)) return null;
|
|
1536
|
+
// (A picture in a table cell is the cell's: no paragraph is spaced there.)
|
|
1537
|
+
if (selectedImage?.closest('td, th')) return null;
|
|
1538
|
+
return spacedBlockFor(node);
|
|
1539
|
+
}
|
|
1540
|
+
|
|
1541
|
+
/**
|
|
1542
|
+
* The block that is spaced where a node is: the list item it is in (the innermost), or the
|
|
1543
|
+
* paragraph, heading or picture it is in, in a table cell too. Null where there is none,
|
|
1544
|
+
* as for a line typed straight into the document or a cell, which is made one to be spaced.
|
|
1545
|
+
*/
|
|
1546
|
+
function spacedBlockFor(node: Node): HTMLElement | null {
|
|
1547
|
+
if (!editorEl || node === editorEl || !editorEl.contains(node)) return null;
|
|
1548
|
+
const el = node instanceof Element ? node : node.parentElement;
|
|
1549
|
+
if (!el) return null;
|
|
1550
|
+
const cell = el.closest('td, th');
|
|
1551
|
+
const inScope = (b: Element | null): b is HTMLElement =>
|
|
1552
|
+
b instanceof HTMLElement && b !== editorEl && editorEl!.contains(b) && (!cell || cell.contains(b));
|
|
1553
|
+
const item = el.closest('li');
|
|
1554
|
+
if (inScope(item)) return item;
|
|
1555
|
+
const block = el.closest<HTMLElement>(INDENT_TARGETS);
|
|
1556
|
+
if (!inScope(block)) return null;
|
|
1557
|
+
return isPlainParagraph(block) || /^(H[1-6]|FIGURE)$/.test(block.tagName) ? block : null;
|
|
1558
|
+
}
|
|
1559
|
+
|
|
1560
|
+
function applySpacing(change: { line?: number | null; before?: number | null; after?: number | null }): void {
|
|
1561
|
+
if (readonly || !editorEl) return;
|
|
1562
|
+
const valid = (n: number | null | undefined, most: number) => n == null || (typeof n === 'number' && Number.isFinite(n) && n >= 0 && n <= most);
|
|
1563
|
+
// (Lines closer than LEAST_LINES are printed over each other: no spacing to set.)
|
|
1564
|
+
if (!valid(change.line, MOST_LINES) || (change.line != null && change.line < LEAST_LINES) || !valid(change.before, MOST_SPACE_PT) || !valid(change.after, MOST_SPACE_PT)) return;
|
|
1565
|
+
// A picture that is selected is spaced as the paragraph it is.
|
|
1566
|
+
const figure = selectedImage?.closest<HTMLElement>('figure') ?? null;
|
|
1567
|
+
// (Not one in a table cell, where no paragraph is spaced: nor is the paragraph the
|
|
1568
|
+
// caret was last in, which is not what is selected.)
|
|
1569
|
+
if (selectedImage?.closest('td, th')) return;
|
|
1570
|
+
const wrapped = new Map<HTMLElement, HTMLBRElement | null>();
|
|
1571
|
+
let targets: HTMLElement[];
|
|
1572
|
+
history?.step();
|
|
1573
|
+
if (figure && editorEl.contains(figure)) {
|
|
1574
|
+
targets = [figure];
|
|
1575
|
+
} else {
|
|
1576
|
+
restoreSelection();
|
|
1577
|
+
editorEl.focus();
|
|
1578
|
+
const sel = window.getSelection();
|
|
1579
|
+
if (!sel || sel.rangeCount === 0 || !sel.anchorNode || !editorEl.contains(sel.anchorNode)) return;
|
|
1580
|
+
const { anchorNode, anchorOffset, focusNode, focusOffset } = sel;
|
|
1581
|
+
targets = indentTargets(sel.getRangeAt(0), 'spacing', wrapped);
|
|
1582
|
+
if (wrapped.size > 0 && anchorNode instanceof Text && focusNode instanceof Text && anchorNode.isConnected && focusNode.isConnected) {
|
|
1583
|
+
sel.setBaseAndExtent(anchorNode, anchorOffset, focusNode, focusOffset);
|
|
1584
|
+
}
|
|
1585
|
+
}
|
|
1586
|
+
const points = (n: number) => `${Math.round(n * 100) / 100}pt`;
|
|
1587
|
+
let spaced = false;
|
|
1588
|
+
for (const el of targets) {
|
|
1589
|
+
const before = el.getAttribute('style');
|
|
1590
|
+
// (A picture has no lines to space.)
|
|
1591
|
+
if (change.line !== undefined && el.tagName !== 'FIGURE') el.style.lineHeight = change.line === null ? '' : String(Math.round(change.line * 100) / 100);
|
|
1592
|
+
if (change.before !== undefined) el.style.marginTop = change.before ? points(change.before) : '';
|
|
1593
|
+
if (change.after !== undefined) el.style.marginBottom = change.after ? points(change.after) : '';
|
|
1594
|
+
if (!el.getAttribute('style')) el.removeAttribute('style');
|
|
1595
|
+
if (el.getAttribute('style') !== before || wrapped.has(el)) spaced = true;
|
|
1596
|
+
}
|
|
1597
|
+
if (!spaced) return;
|
|
1598
|
+
indentChanged();
|
|
1599
|
+
overlayRef?.reposition();
|
|
1600
|
+
}
|
|
1601
|
+
|
|
1602
|
+
/**
|
|
1603
|
+
* Set how far apart the lines are in the paragraphs the selection touches, in lines: 1, 1.15,
|
|
1604
|
+
* 1.5, 2. `null` takes the setting off, and the lines are as far apart as the editor's own
|
|
1605
|
+
* style has them. Also what the toolbar's spacing list calls.
|
|
1606
|
+
*/
|
|
1607
|
+
export function setLineSpacing(lines: number | null): void {
|
|
1608
|
+
applySpacing({ line: lines });
|
|
1609
|
+
}
|
|
1610
|
+
|
|
1611
|
+
/**
|
|
1612
|
+
* Set the space before and after the paragraphs the selection touches, in points; a picture
|
|
1613
|
+
* that is selected is spaced the same way. `0` or `null` takes a space off, and a side that
|
|
1614
|
+
* is left out stays as it is: `setParagraphSpacing({ after: 6 })`.
|
|
1615
|
+
*/
|
|
1616
|
+
export function setParagraphSpacing(space: { before?: number | null; after?: number | null }): void {
|
|
1617
|
+
if (!space || typeof space !== 'object') return;
|
|
1618
|
+
applySpacing({ before: space.before, after: space.after });
|
|
1619
|
+
}
|
|
1620
|
+
|
|
1621
|
+
// ── The gap after a list's markers ──
|
|
1622
|
+
// How far an item's text starts from its bullet or number: the list's text indent, as Word
|
|
1623
|
+
// has it, written as the list's padding-left (see markerGapTwips). The marker stays where
|
|
1624
|
+
// it is; the text moves. The editor draws the markers of such a list itself (markItems).
|
|
1625
|
+
|
|
1626
|
+
/** The list the caret is in: the innermost, for an item of a list within a list. */
|
|
1627
|
+
function listAtCaret(): HTMLElement | null {
|
|
1628
|
+
const node = window.getSelection()?.anchorNode ?? null;
|
|
1629
|
+
if (!editorEl || !node || !editorEl.contains(node)) return null;
|
|
1630
|
+
const item = (node instanceof Element ? node : node.parentElement)?.closest('li');
|
|
1631
|
+
const list = item?.parentElement;
|
|
1632
|
+
return list && editorEl.contains(list) && /^(UL|OL)$/.test(list.tagName) ? list : null;
|
|
1633
|
+
}
|
|
1634
|
+
|
|
1635
|
+
/**
|
|
1636
|
+
* Set how far the text of the items of the lists the selection touches starts from their
|
|
1637
|
+
* bullet or number, in points: 6 to 144. `null` takes the setting off, and the list has the
|
|
1638
|
+
* quarter inch (18 pt) it always had. Also what the toolbar's spacing list calls.
|
|
1639
|
+
*/
|
|
1640
|
+
export function setListMarkerGap(points: number | null): void {
|
|
1641
|
+
if (readonly || !editorEl) return;
|
|
1642
|
+
if (points !== null && (typeof points !== 'number' || !Number.isFinite(points) || points * 20 < LEAST_MARKER_GAP || points * 20 > MOST_MARKER_GAP)) return;
|
|
1643
|
+
restoreSelection();
|
|
1644
|
+
editorEl.focus();
|
|
1645
|
+
const sel = window.getSelection();
|
|
1646
|
+
if (!sel || sel.rangeCount === 0 || !sel.anchorNode || !editorEl.contains(sel.anchorNode)) return;
|
|
1647
|
+
const range = sel.getRangeAt(0);
|
|
1648
|
+
const lists: HTMLElement[] = [];
|
|
1649
|
+
const add = (node: Node) => {
|
|
1650
|
+
const item = (node instanceof Element ? node : node.parentElement)?.closest('li');
|
|
1651
|
+
const list = item?.parentElement;
|
|
1652
|
+
if (list && editorEl!.contains(list) && /^(UL|OL)$/.test(list.tagName) && !lists.includes(list)) lists.push(list);
|
|
1653
|
+
};
|
|
1654
|
+
add(range.startContainer);
|
|
1655
|
+
const walker = document.createTreeWalker(editorEl, NodeFilter.SHOW_TEXT);
|
|
1656
|
+
for (let n = walker.nextNode(); n; n = walker.nextNode()) if (range.intersectsNode(n) && (n.textContent ?? '').trim()) add(n);
|
|
1657
|
+
if (lists.length === 0) return;
|
|
1658
|
+
history?.step();
|
|
1659
|
+
let changed = false;
|
|
1660
|
+
for (const list of lists) {
|
|
1661
|
+
const before = list.getAttribute('style');
|
|
1662
|
+
list.style.paddingLeft = points === null ? '' : `${Math.round(points * 100) / 100}pt`;
|
|
1663
|
+
if (!list.getAttribute('style')) list.removeAttribute('style');
|
|
1664
|
+
if (list.getAttribute('style') !== before) changed = true;
|
|
1665
|
+
}
|
|
1666
|
+
if (!changed) return;
|
|
1667
|
+
markItems(editorEl);
|
|
1337
1668
|
indentChanged();
|
|
1338
1669
|
}
|
|
1339
1670
|
|
|
@@ -1403,6 +1734,7 @@
|
|
|
1403
1734
|
const offset = caret.startOffset;
|
|
1404
1735
|
const el = node instanceof Element ? node : node.parentElement;
|
|
1405
1736
|
if (!el || el.closest('td, th, li, pre, figure, h1, h2, h3, h4, h5, h6')) return;
|
|
1737
|
+
history?.step();
|
|
1406
1738
|
const x = caretLeft(caret);
|
|
1407
1739
|
if (x === null) return;
|
|
1408
1740
|
|
|
@@ -1466,7 +1798,6 @@
|
|
|
1466
1798
|
const after = block.getAttribute('style');
|
|
1467
1799
|
const stopsAfter = block.getAttribute(TAB_STOPS);
|
|
1468
1800
|
if (!wrapped && after === before && stopsAfter === stopsBefore) return;
|
|
1469
|
-
indentUndo.push([{ el: block, before, after, wrapped, stops: { before: stopsBefore, after: stopsAfter } }]);
|
|
1470
1801
|
indentChanged();
|
|
1471
1802
|
}
|
|
1472
1803
|
|
|
@@ -1536,8 +1867,12 @@
|
|
|
1536
1867
|
/** The tab stop that goes with a hanging indent: tab-size on screen, \tx in the RTF. */
|
|
1537
1868
|
tabSize: string;
|
|
1538
1869
|
tabStops: string;
|
|
1870
|
+
/** The space before and after it, and between its lines. */
|
|
1871
|
+
marginTop: string;
|
|
1872
|
+
marginBottom: string;
|
|
1873
|
+
lineHeight: string;
|
|
1539
1874
|
}
|
|
1540
|
-
const NO_FORMAT: ParagraphFormat = { textAlign: '', marginLeft: '', marginRight: '', textIndent: '', tabSize: '', tabStops: '' };
|
|
1875
|
+
const NO_FORMAT: ParagraphFormat = { textAlign: '', marginLeft: '', marginRight: '', textIndent: '', tabSize: '', tabStops: '', marginTop: '', marginBottom: '', lineHeight: '' };
|
|
1541
1876
|
const PAINT_BLOCKS = 'p, div, h1, h2, h3, h4, h5, h6';
|
|
1542
1877
|
|
|
1543
1878
|
/** 'pick': waiting for the paragraph to copy from. 'apply': each click applies the format. */
|
|
@@ -1584,7 +1919,10 @@
|
|
|
1584
1919
|
marginRight: el.style.marginRight,
|
|
1585
1920
|
textIndent: el.style.textIndent,
|
|
1586
1921
|
tabSize: el.style.tabSize,
|
|
1587
|
-
tabStops: el.getAttribute(TAB_STOPS) ?? ''
|
|
1922
|
+
tabStops: el.getAttribute(TAB_STOPS) ?? '',
|
|
1923
|
+
marginTop: el.style.marginTop,
|
|
1924
|
+
marginBottom: el.style.marginBottom,
|
|
1925
|
+
lineHeight: el.style.lineHeight
|
|
1588
1926
|
};
|
|
1589
1927
|
}
|
|
1590
1928
|
|
|
@@ -1635,6 +1973,7 @@
|
|
|
1635
1973
|
return;
|
|
1636
1974
|
}
|
|
1637
1975
|
if (block && block === copySource) return;
|
|
1976
|
+
history?.step();
|
|
1638
1977
|
let target = block;
|
|
1639
1978
|
let wrapped: { br: HTMLBRElement | null } | null = null;
|
|
1640
1979
|
if (!target && loose) {
|
|
@@ -1659,6 +1998,10 @@
|
|
|
1659
1998
|
if (indents) {
|
|
1660
1999
|
target.style.marginRight = copiedFormat.marginRight;
|
|
1661
2000
|
target.style.textIndent = copiedFormat.textIndent;
|
|
2001
|
+
// (Spacing goes where the writer sends it: the same blocks.)
|
|
2002
|
+
target.style.marginTop = copiedFormat.marginTop;
|
|
2003
|
+
target.style.marginBottom = copiedFormat.marginBottom;
|
|
2004
|
+
target.style.lineHeight = copiedFormat.lineHeight;
|
|
1662
2005
|
}
|
|
1663
2006
|
if (whole) {
|
|
1664
2007
|
target.style.tabSize = copiedFormat.tabSize;
|
|
@@ -1669,33 +2012,10 @@
|
|
|
1669
2012
|
const after = target.getAttribute('style');
|
|
1670
2013
|
const stopsAfter = target.getAttribute(TAB_STOPS);
|
|
1671
2014
|
if (!wrapped && after === before && stopsAfter === stopsBefore) return;
|
|
1672
|
-
indentUndo.push([{ el: target, before, after, wrapped, stops: { before: stopsBefore, after: stopsAfter } }]);
|
|
1673
2015
|
indentChanged();
|
|
1674
2016
|
placeCopySourceBox();
|
|
1675
2017
|
}
|
|
1676
2018
|
|
|
1677
|
-
/** Undo the last Indent or Outdent, if nothing has changed since. */
|
|
1678
|
-
function undoIndent(): boolean {
|
|
1679
|
-
const step = indentUndo.pop();
|
|
1680
|
-
if (!step) return false;
|
|
1681
|
-
// Content replaced or edited since (setHTML, an import, a link): the step no longer applies.
|
|
1682
|
-
if (step.some(({ el, after }) => !el.isConnected || el.getAttribute('style') !== after)) {
|
|
1683
|
-
indentUndo = [];
|
|
1684
|
-
return false;
|
|
1685
|
-
}
|
|
1686
|
-
for (const { el, before, wrapped, stops } of step) {
|
|
1687
|
-
if (before === null) el.removeAttribute('style');
|
|
1688
|
-
else el.setAttribute('style', before);
|
|
1689
|
-
if (stops) {
|
|
1690
|
-
if (stops.before === null) el.removeAttribute(TAB_STOPS);
|
|
1691
|
-
else el.setAttribute(TAB_STOPS, stops.before);
|
|
1692
|
-
}
|
|
1693
|
-
if (wrapped) undoWrap(el, wrapped);
|
|
1694
|
-
}
|
|
1695
|
-
indentChanged();
|
|
1696
|
-
return true;
|
|
1697
|
-
}
|
|
1698
|
-
|
|
1699
2019
|
function indentChanged(): void {
|
|
1700
2020
|
placeCopySourceBox();
|
|
1701
2021
|
updateToolbarState();
|
|
@@ -1712,7 +2032,7 @@
|
|
|
1712
2032
|
*/
|
|
1713
2033
|
function indentTargets(
|
|
1714
2034
|
range: Range,
|
|
1715
|
-
cmd: IndentCommand,
|
|
2035
|
+
cmd: IndentCommand | 'spacing',
|
|
1716
2036
|
wrapped: Map<HTMLElement, HTMLBRElement | null>
|
|
1717
2037
|
): HTMLElement[] {
|
|
1718
2038
|
if (!editorEl) return [];
|
|
@@ -1731,6 +2051,15 @@
|
|
|
1731
2051
|
const item = el.closest('li');
|
|
1732
2052
|
const closest = el.closest(INDENT_TARGETS);
|
|
1733
2053
|
const block = inScope(item) ? item.parentElement : inScope(closest) ? closest : null;
|
|
2054
|
+
if (cmd === 'spacing') {
|
|
2055
|
+
const spaced = spacedBlockFor(node);
|
|
2056
|
+
if (spaced) return spaced;
|
|
2057
|
+
// (Nor is a block that holds others, a list or a quotation, spaced as one.)
|
|
2058
|
+
if (inScope(closest) || inScope(item)) return null;
|
|
2059
|
+
const made = wrapLooseLine(node);
|
|
2060
|
+
if (made) wrapped.set(made.p, made.br);
|
|
2061
|
+
return made?.p ?? null;
|
|
2062
|
+
}
|
|
1734
2063
|
if (cmd !== 'indent' && cmd !== 'outdent') {
|
|
1735
2064
|
if (cell) return null;
|
|
1736
2065
|
if (inScope(block)) return isPlainParagraph(block) ? block : null;
|
|
@@ -1805,7 +2134,8 @@
|
|
|
1805
2134
|
function setBlock(tag: string): void {
|
|
1806
2135
|
restoreSelection();
|
|
1807
2136
|
editorEl?.focus();
|
|
1808
|
-
|
|
2137
|
+
history?.step();
|
|
2138
|
+
browserCommand('formatBlock', false, tag);
|
|
1809
2139
|
updateToolbarState();
|
|
1810
2140
|
}
|
|
1811
2141
|
|
|
@@ -1816,12 +2146,23 @@
|
|
|
1816
2146
|
saveSelection();
|
|
1817
2147
|
currentFontSize = sizeAtSelection();
|
|
1818
2148
|
currentFont = fontList.length > 0 ? fontAtSelection() : '';
|
|
2149
|
+
const spaced = spacedBlockAtCaret();
|
|
2150
|
+
currentSpacing = spaced ? spacingOf(spaced) : { line: null, before: 0, after: 0 };
|
|
2151
|
+
const gapped = listAtCaret();
|
|
2152
|
+
if (gapped) {
|
|
2153
|
+
const twips = markerGapTwips(gapped);
|
|
2154
|
+
currentSpacing.gap = twips === null ? null : twips / 20;
|
|
2155
|
+
}
|
|
1819
2156
|
|
|
2157
|
+
// The list the caret's item is in: the innermost, for an item of a list within a list.
|
|
2158
|
+
const listKind = editorEl ? listKindAt(editorEl, window.getSelection()) : null;
|
|
1820
2159
|
formatState = {
|
|
1821
2160
|
bold: document.queryCommandState('bold'),
|
|
1822
2161
|
italic: document.queryCommandState('italic'),
|
|
1823
2162
|
underline: document.queryCommandState('underline'),
|
|
1824
|
-
strikeThrough: document.queryCommandState('strikeThrough')
|
|
2163
|
+
strikeThrough: document.queryCommandState('strikeThrough'),
|
|
2164
|
+
bulletList: listKind === 'ul',
|
|
2165
|
+
numberedList: listKind === 'ol'
|
|
1825
2166
|
};
|
|
1826
2167
|
|
|
1827
2168
|
const block = document.queryCommandValue('formatBlock');
|
|
@@ -1905,7 +2246,7 @@
|
|
|
1905
2246
|
let value: { v: T } | null = null;
|
|
1906
2247
|
return () => (value ??= { v: read() }).v;
|
|
1907
2248
|
};
|
|
1908
|
-
const html = once(() => el
|
|
2249
|
+
const html = once(() => (el ? documentHtml() : ''));
|
|
1909
2250
|
const text = once(() => el?.innerText || '');
|
|
1910
2251
|
const estimate = once(() => (el ? estimateRtfBytes(el, options) : 0));
|
|
1911
2252
|
onchange({
|
|
@@ -1924,7 +2265,7 @@
|
|
|
1924
2265
|
function keepDraft(): void {
|
|
1925
2266
|
if (!editorEl) return;
|
|
1926
2267
|
try {
|
|
1927
|
-
localStorage.setItem(storageKey,
|
|
2268
|
+
localStorage.setItem(storageKey, documentHtml());
|
|
1928
2269
|
lastSaved = `Saved ${new Date().toLocaleTimeString()}`;
|
|
1929
2270
|
} catch (e) {
|
|
1930
2271
|
// Embedded images can push a document past the localStorage quota. Say
|
|
@@ -1948,7 +2289,7 @@
|
|
|
1948
2289
|
function autoSaveNow(): void {
|
|
1949
2290
|
if (!editorEl || !autosave) return;
|
|
1950
2291
|
keepDraft();
|
|
1951
|
-
onsave?.({ html:
|
|
2292
|
+
onsave?.({ html: documentHtml() });
|
|
1952
2293
|
}
|
|
1953
2294
|
|
|
1954
2295
|
/**
|
|
@@ -1961,7 +2302,7 @@
|
|
|
1961
2302
|
// The save that was waiting is this one.
|
|
1962
2303
|
clearTimeout(saveTimer);
|
|
1963
2304
|
if (autosave) keepDraft();
|
|
1964
|
-
onsave?.({ html:
|
|
2305
|
+
onsave?.({ html: documentHtml() });
|
|
1965
2306
|
}
|
|
1966
2307
|
|
|
1967
2308
|
function scheduleAutoSave(): void {
|
|
@@ -1976,14 +2317,16 @@
|
|
|
1976
2317
|
function handleInput(e?: Event): void {
|
|
1977
2318
|
placeCopySourceBox();
|
|
1978
2319
|
const kind = (e as InputEvent | undefined)?.inputType ?? '';
|
|
1979
|
-
|
|
2320
|
+
// (The editor's own Undo and Redo do not come this way. These are the browser's, where
|
|
2321
|
+
// one could not be stopped: what it puts back is checked as it always was.)
|
|
2322
|
+
const browserHistory = kind === 'historyUndo' || kind === 'historyRedo';
|
|
1980
2323
|
// (Words dragged from one place in the document to another are first taken out where
|
|
1981
2324
|
// they were, which is an input event of its own: what was dropped has not come in yet.)
|
|
1982
2325
|
let shown = true;
|
|
1983
2326
|
if (pastedOver && kind !== 'deleteByDrag') {
|
|
1984
2327
|
tidyPasted(pastedOver);
|
|
1985
2328
|
pastedOver = null;
|
|
1986
|
-
} else if (
|
|
2329
|
+
} else if (browserHistory || MARKUP_FROM_OUTSIDE.has(kind)) {
|
|
1987
2330
|
// Undo and Redo put back what the browser remembers, not what the editor made of it:
|
|
1988
2331
|
// Redo after a paste brings back the frame that was taken out of the paste, the
|
|
1989
2332
|
// style that was taken off what stayed, and Word's hidden text as words. And a drop
|
|
@@ -1992,20 +2335,12 @@
|
|
|
1992
2335
|
shown = checkDocument();
|
|
1993
2336
|
}
|
|
1994
2337
|
followPastes(kind === 'historyUndo');
|
|
1995
|
-
|
|
1996
|
-
if (
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
} else {
|
|
2002
|
-
// (And a Clear formatting before this edit is the browser's to undo from here on.)
|
|
2003
|
-
clearUndo = clearRedo = null;
|
|
2004
|
-
// An edit: what was undone can no longer be redone.
|
|
2005
|
-
sizeSteps = sizeSteps.slice(0, sizeStepsApplied);
|
|
2006
|
-
// What was typed at a caret after a size was chosen came in marked: give it the size.
|
|
2007
|
-
applyPendingSize();
|
|
2008
|
-
}
|
|
2338
|
+
// What was typed at a caret after a size was chosen came in marked: give it the size.
|
|
2339
|
+
if (!browserHistory) applyPendingSize();
|
|
2340
|
+
// (Not between the commands of a Clear formatting, which is not yet the document.)
|
|
2341
|
+
if (editorEl && !clearing) liftBlocks(editorEl, true);
|
|
2342
|
+
// (What the editor made of the input is part of the same step as the input.)
|
|
2343
|
+
history?.typed();
|
|
2009
2344
|
pendingSize = null;
|
|
2010
2345
|
pendingFont = null;
|
|
2011
2346
|
updateToolbarState();
|
|
@@ -2038,8 +2373,7 @@
|
|
|
2038
2373
|
return;
|
|
2039
2374
|
}
|
|
2040
2375
|
linkError = '';
|
|
2041
|
-
|
|
2042
|
-
indentUndo = [];
|
|
2376
|
+
history?.step();
|
|
2043
2377
|
restoreFor(dialogSelection);
|
|
2044
2378
|
editorEl?.focus();
|
|
2045
2379
|
|
|
@@ -2057,7 +2391,7 @@
|
|
|
2057
2391
|
sel.addRange(range);
|
|
2058
2392
|
}
|
|
2059
2393
|
} else {
|
|
2060
|
-
|
|
2394
|
+
browserCommand('createLink', false, linkUrl.trim());
|
|
2061
2395
|
}
|
|
2062
2396
|
linkModalOpen = false;
|
|
2063
2397
|
updateCounts();
|
|
@@ -2077,6 +2411,93 @@
|
|
|
2077
2411
|
act();
|
|
2078
2412
|
}
|
|
2079
2413
|
|
|
2414
|
+
// ── Tables ──
|
|
2415
|
+
// A table is put in from a dialog: its rows and columns, whether its cells have borders,
|
|
2416
|
+
// and whether its first row is a header. It is written the way rtfToHtml reads a table
|
|
2417
|
+
// back, so that a document is the same after it is saved and opened again: the cells
|
|
2418
|
+
// share the width of the table, and a border is a style on each cell, which is what the
|
|
2419
|
+
// RTF writer sends (\clbrdrt\brdrs\brdrw15 and so on). A table with no borders has none
|
|
2420
|
+
// in the RTF either; the editor draws a faint line around its cells while it is edited.
|
|
2421
|
+
const MOST_TABLE_ROWS = 50;
|
|
2422
|
+
const MOST_TABLE_COLUMNS = 12;
|
|
2423
|
+
const TABLE_STYLE = 'border-collapse:collapse;width:100%;margin:4px 0';
|
|
2424
|
+
const CELL_BORDER = 'border:1px solid;';
|
|
2425
|
+
|
|
2426
|
+
/**
|
|
2427
|
+
* A column's share of the width of a table of `count` equal columns, as the reader gives
|
|
2428
|
+
* it: the edges, each rounded to a hundredth of a percent, and a cell the difference of two.
|
|
2429
|
+
*/
|
|
2430
|
+
function columnShare(column: number, count: number): string {
|
|
2431
|
+
const edge = (k: number) => Math.round((k / count) * 10000) / 100;
|
|
2432
|
+
return (edge(column + 1) - edge(column)).toFixed(2).replace(/0$/, '');
|
|
2433
|
+
}
|
|
2434
|
+
|
|
2435
|
+
function openTableModal(): void {
|
|
2436
|
+
keepForDialog();
|
|
2437
|
+
tableModalOpen = true;
|
|
2438
|
+
}
|
|
2439
|
+
|
|
2440
|
+
function applyTable(): void {
|
|
2441
|
+
const at = dialogSelection;
|
|
2442
|
+
tableModalOpen = false;
|
|
2443
|
+
restoreFor(at);
|
|
2444
|
+
insertTable(tableRows ?? 0, tableColumns ?? 0, { borders: tableBorders, header: tableHeader });
|
|
2445
|
+
}
|
|
2446
|
+
|
|
2447
|
+
/**
|
|
2448
|
+
* Put a table of empty cells in after the paragraph the caret is in (in its place where
|
|
2449
|
+
* that is empty), and the caret in its first cell. `borders` (default true) gives every
|
|
2450
|
+
* cell a border, which is written to the RTF; `header` makes the first row header cells.
|
|
2451
|
+
* At most 50 rows and 12 columns. Also what the toolbar's Insert table dialog calls.
|
|
2452
|
+
*/
|
|
2453
|
+
export function insertTable(rows: number, columns: number, options: { borders?: boolean; header?: boolean } = {}): void {
|
|
2454
|
+
if (readonly || !editorEl) return;
|
|
2455
|
+
const whole = (n: unknown, most: number) => (typeof n === 'number' && Number.isFinite(n) ? Math.max(1, Math.min(most, Math.floor(n))) : 0);
|
|
2456
|
+
const rowCount = whole(rows, MOST_TABLE_ROWS);
|
|
2457
|
+
const columnCount = whole(columns, MOST_TABLE_COLUMNS);
|
|
2458
|
+
if (rowCount < 1 || columnCount < 1 || rows < 1 || columns < 1) return;
|
|
2459
|
+
const borders = options?.borders !== false;
|
|
2460
|
+
const header = options?.header === true;
|
|
2461
|
+
editorEl.focus();
|
|
2462
|
+
history?.step();
|
|
2463
|
+
|
|
2464
|
+
const table = document.createElement('table');
|
|
2465
|
+
table.setAttribute('style', TABLE_STYLE);
|
|
2466
|
+
const body = document.createElement('tbody');
|
|
2467
|
+
for (let r = 0; r < rowCount; r++) {
|
|
2468
|
+
const row = document.createElement('tr');
|
|
2469
|
+
for (let c = 0; c < columnCount; c++) {
|
|
2470
|
+
const head = header && r === 0;
|
|
2471
|
+
const cell = document.createElement(head ? 'th' : 'td');
|
|
2472
|
+
const width = `width:${columnShare(c, columnCount)}%`;
|
|
2473
|
+
// (A header cell says how it is aligned: a browser centers one that does not.
|
|
2474
|
+
// With no borders it has the line under it that marks a header cell in the RTF.)
|
|
2475
|
+
cell.setAttribute('style', head ? `${borders ? CELL_BORDER : 'border-bottom:1px solid;'}padding:2px 6px;text-align:left;${width}` : `${borders ? CELL_BORDER : ''}padding:2px 6px;${width}`);
|
|
2476
|
+
cell.appendChild(document.createElement('br'));
|
|
2477
|
+
row.appendChild(cell);
|
|
2478
|
+
}
|
|
2479
|
+
body.appendChild(row);
|
|
2480
|
+
}
|
|
2481
|
+
table.appendChild(body);
|
|
2482
|
+
|
|
2483
|
+
const block = caretBlock();
|
|
2484
|
+
if (block && isEmptyBlock(block)) block.replaceWith(table);
|
|
2485
|
+
else if (block) block.after(table);
|
|
2486
|
+
else editorEl.appendChild(table);
|
|
2487
|
+
// Somewhere to go on typing after the table.
|
|
2488
|
+
const next = table.nextElementSibling;
|
|
2489
|
+
if (!next || next.tagName === 'TABLE' || next.tagName === 'FIGURE') {
|
|
2490
|
+
const p = document.createElement('p');
|
|
2491
|
+
p.appendChild(document.createElement('br'));
|
|
2492
|
+
table.after(p);
|
|
2493
|
+
}
|
|
2494
|
+
selectedImage = null;
|
|
2495
|
+
placeCaret(table.querySelector('td, th') as HTMLElement);
|
|
2496
|
+
updateToolbarState();
|
|
2497
|
+
fireChange();
|
|
2498
|
+
if (autosave) scheduleAutoSave();
|
|
2499
|
+
}
|
|
2500
|
+
|
|
2080
2501
|
// ── Images ──
|
|
2081
2502
|
function openImageModal(): void {
|
|
2082
2503
|
keepForDialog();
|
|
@@ -2199,7 +2620,7 @@
|
|
|
2199
2620
|
caption: string
|
|
2200
2621
|
): { img: HTMLImageElement; after: HTMLElement } | null {
|
|
2201
2622
|
if (!editorEl) return null;
|
|
2202
|
-
|
|
2623
|
+
history?.step();
|
|
2203
2624
|
|
|
2204
2625
|
const figure = document.createElement('figure');
|
|
2205
2626
|
const img = document.createElement('img');
|
|
@@ -2247,10 +2668,13 @@
|
|
|
2247
2668
|
fitImage(img);
|
|
2248
2669
|
return;
|
|
2249
2670
|
}
|
|
2671
|
+
// (Not if the picture has been taken out again by then.)
|
|
2250
2672
|
img.addEventListener(
|
|
2251
2673
|
'load',
|
|
2252
2674
|
() => {
|
|
2253
|
-
|
|
2675
|
+
// (Part of putting the picture in, not a step of its own to undo.)
|
|
2676
|
+
if (history) history.quietly(() => fitImage(img));
|
|
2677
|
+
else fitImage(img);
|
|
2254
2678
|
updateCounts();
|
|
2255
2679
|
fireChange();
|
|
2256
2680
|
if (autosave) scheduleAutoSave();
|
|
@@ -2422,8 +2846,468 @@
|
|
|
2422
2846
|
return !!el?.closest('figcaption');
|
|
2423
2847
|
}
|
|
2424
2848
|
|
|
2849
|
+
// ── Pictures: where they are ──
|
|
2850
|
+
// A picture is moved up and down the document as the block it is: its figure, with its
|
|
2851
|
+
// caption. Two pictures side by side are a table of one row with no borders, a picture
|
|
2852
|
+
// and its own caption in each cell: that is what an RTF can say (a row of cells, read by
|
|
2853
|
+
// Word and by every other reader), and what rtfToHtml gives back. The frame around a
|
|
2854
|
+
// selected picture has the buttons: Move up, Move down, Place beside the picture above,
|
|
2855
|
+
// and, for a picture that is beside another, Place on its own line.
|
|
2856
|
+
const MOST_PICTURES_IN_ROW = 4;
|
|
2857
|
+
const PICTURE_CELL_STYLE = 'padding:2px 6px;';
|
|
2858
|
+
/** The padding of such a cell, left and right together, in px. */
|
|
2859
|
+
const PICTURE_CELL_PADDING = 12;
|
|
2860
|
+
|
|
2861
|
+
/** The picture a cell holds and nothing else: its figure, or the picture itself. */
|
|
2862
|
+
function pictureInCell(cell: Element): HTMLElement | null {
|
|
2863
|
+
const held = Array.from(cell.children).filter((child) => child.tagName !== 'BR');
|
|
2864
|
+
if (held.length !== 1) return null;
|
|
2865
|
+
const unit = held[0] as HTMLElement;
|
|
2866
|
+
const pictures = unit.tagName === 'IMG' ? 1 : unit.tagName === 'FIGURE' ? unit.querySelectorAll('img').length : 0;
|
|
2867
|
+
if (pictures !== 1) return null;
|
|
2868
|
+
// (Words typed beside the picture make the cell an ordinary one.)
|
|
2869
|
+
return (cell.textContent ?? '').trim() === (unit.textContent ?? '').trim() ? unit : null;
|
|
2870
|
+
}
|
|
2871
|
+
|
|
2872
|
+
/**
|
|
2873
|
+
* The row of pictures a picture is in: a table of one row, a picture in each of its cells.
|
|
2874
|
+
* `cells` are the pictures' cells; `gap` is the space set between two of them, in points,
|
|
2875
|
+
* or null for a row that shares the page equally (see picture-rows.ts).
|
|
2876
|
+
*/
|
|
2877
|
+
function pictureRowOf(node: Element | null): { table: HTMLTableElement; cells: HTMLTableCellElement[]; gap: number | null } | null {
|
|
2878
|
+
const table = node?.closest('table');
|
|
2879
|
+
if (!table || !editorEl || table.parentElement !== editorEl || table.querySelector('table')) return null;
|
|
2880
|
+
if (table.rows.length !== 1 || table.querySelector('caption')) return null;
|
|
2881
|
+
const all = Array.from(table.rows[0].cells);
|
|
2882
|
+
// (Only a row with no width of its own has gaps: an empty cell in a row across the page is no gap.)
|
|
2883
|
+
const fixed = !table.style.width && !table.getAttribute('width');
|
|
2884
|
+
const gaps = fixed ? all.filter(isGapCell) : [];
|
|
2885
|
+
const cells = all.filter((cell) => !gaps.includes(cell));
|
|
2886
|
+
if (cells.length === 0 || !cells.every((cell) => pictureInCell(cell))) return null;
|
|
2887
|
+
// (A row with gaps is laid out by the editor, which writes each cell's style: one with
|
|
2888
|
+
// a border or a background of its own is a table of its own, and is left as it is.)
|
|
2889
|
+
if (gaps.length > 0 && !all.every(isPlainCell)) return null;
|
|
2890
|
+
const twips = gaps.length > 0 ? lengthTwips(gaps[0]) : null;
|
|
2891
|
+
return { table, cells, gap: twips === null ? null : twips / 20 };
|
|
2892
|
+
}
|
|
2893
|
+
|
|
2894
|
+
/** A block of the document that is one picture: its figure, or a paragraph that holds it alone. */
|
|
2895
|
+
function isPictureBlock(block: Element | null): block is HTMLElement {
|
|
2896
|
+
if (!(block instanceof HTMLElement) || block.parentElement !== editorEl) return false;
|
|
2897
|
+
if (block.tagName === 'IMG') return true;
|
|
2898
|
+
if (!/^(FIGURE|P|DIV)$/.test(block.tagName) || block.querySelectorAll('img').length !== 1) return false;
|
|
2899
|
+
const caption = block.tagName === 'FIGURE' ? block.querySelector('figcaption') : null;
|
|
2900
|
+
return (block.textContent ?? '').trim() === (caption?.textContent ?? '').trim();
|
|
2901
|
+
}
|
|
2902
|
+
|
|
2903
|
+
/** What is moved when a picture is: its own block, or the row of pictures it is in. Null for a picture in a line of text. */
|
|
2904
|
+
function movedWith(img: HTMLImageElement | null): HTMLElement | null {
|
|
2905
|
+
if (!img || !editorEl?.contains(img)) return null;
|
|
2906
|
+
const row = pictureRowOf(img);
|
|
2907
|
+
if (row) return row.table;
|
|
2908
|
+
let block: HTMLElement = img.closest<HTMLElement>('figure') ?? img;
|
|
2909
|
+
while (block.parentElement && block.parentElement !== editorEl) block = block.parentElement;
|
|
2910
|
+
return isPictureBlock(block) ? block : null;
|
|
2911
|
+
}
|
|
2912
|
+
|
|
2913
|
+
/** An empty line at the end of the document: where typing goes on, and nothing to move a picture past. */
|
|
2914
|
+
function isLastEmptyLine(el: Element | null): boolean {
|
|
2915
|
+
return !!el && el === editorEl?.lastElementChild && /^(P|DIV)$/.test(el.tagName) && isEmptyBlock(el as HTMLElement);
|
|
2916
|
+
}
|
|
2917
|
+
|
|
2918
|
+
function picturePlace(): PicturePlace {
|
|
2919
|
+
const none: PicturePlace = { up: false, down: false, beside: null, gap: null };
|
|
2920
|
+
const block = readonly ? null : movedWith(selectedImage);
|
|
2921
|
+
if (!block) return none;
|
|
2922
|
+
const above = block.previousElementSibling;
|
|
2923
|
+
const below = block.nextElementSibling;
|
|
2924
|
+
const inRow = block.tagName === 'TABLE';
|
|
2925
|
+
const joins = !inRow && !!above && (isPictureBlock(above) || (pictureRowOf(above)?.cells.length ?? MOST_PICTURES_IN_ROW) < MOST_PICTURES_IN_ROW);
|
|
2926
|
+
const gap = inRow ? (pictureRowOf(block)?.gap ?? null) : null;
|
|
2927
|
+
return { up: !!above, down: !!below && !isLastEmptyLine(below), beside: inRow ? 'leave' : joins ? 'join' : null, gap };
|
|
2928
|
+
}
|
|
2929
|
+
|
|
2930
|
+
function pictureMoved(): void {
|
|
2931
|
+
const last = editorEl?.lastElementChild;
|
|
2932
|
+
// Somewhere to go on typing after a picture that is now the last thing in the document.
|
|
2933
|
+
if (editorEl && last && (last.tagName === 'FIGURE' || last.tagName === 'TABLE')) {
|
|
2934
|
+
const p = document.createElement('p');
|
|
2935
|
+
p.appendChild(document.createElement('br'));
|
|
2936
|
+
editorEl.appendChild(p);
|
|
2937
|
+
}
|
|
2938
|
+
selectedImage?.scrollIntoView?.({ block: 'nearest' });
|
|
2939
|
+
overlayRef?.reposition();
|
|
2940
|
+
handleImageEdited();
|
|
2941
|
+
}
|
|
2942
|
+
|
|
2943
|
+
/** Move the selected picture (with its caption, or the row of pictures it is in) over the block above it or below it. */
|
|
2944
|
+
function moveSelectedImage(direction: -1 | 1): void {
|
|
2945
|
+
const place = picturePlace();
|
|
2946
|
+
const block = movedWith(selectedImage);
|
|
2947
|
+
if (!block || !(direction < 0 ? place.up : place.down)) return;
|
|
2948
|
+
history?.step();
|
|
2949
|
+
if (direction < 0) block.previousElementSibling!.before(block);
|
|
2950
|
+
else block.nextElementSibling!.after(block);
|
|
2951
|
+
pictureMoved();
|
|
2952
|
+
}
|
|
2953
|
+
|
|
2954
|
+
/** A picture block as the figure it is put in a cell as. */
|
|
2955
|
+
function figureOf(block: HTMLElement): { figure: HTMLElement; align: string } {
|
|
2956
|
+
const align = block.style.textAlign;
|
|
2957
|
+
if (block.tagName === 'FIGURE') {
|
|
2958
|
+
// (In a cell it is the cell that is aligned, and nothing is indented or spaced.)
|
|
2959
|
+
block.removeAttribute('style');
|
|
2960
|
+
return { figure: block, align };
|
|
2961
|
+
}
|
|
2962
|
+
const figure = document.createElement('figure');
|
|
2963
|
+
const img = block.tagName === 'IMG' ? block : block.querySelector('img')!;
|
|
2964
|
+
figure.append(img, document.createElement('figcaption'));
|
|
2965
|
+
if (block !== img) block.remove();
|
|
2966
|
+
return { figure, align };
|
|
2967
|
+
}
|
|
2968
|
+
|
|
2969
|
+
function pictureCell(block: HTMLElement): HTMLTableCellElement {
|
|
2970
|
+
const cell = document.createElement('td');
|
|
2971
|
+
const { figure, align } = figureOf(block);
|
|
2972
|
+
cell.appendChild(figure);
|
|
2973
|
+
cell.setAttribute('style', PICTURE_CELL_STYLE);
|
|
2974
|
+
if (align && align !== 'left') cell.style.textAlign = align;
|
|
2975
|
+
return cell;
|
|
2976
|
+
}
|
|
2977
|
+
|
|
2978
|
+
/** How wide a picture is shown, in px. */
|
|
2979
|
+
function pictureWidth(img: HTMLImageElement): number {
|
|
2980
|
+
return parseFloat(img.style.width) || img.getBoundingClientRect().width || img.naturalWidth || 0;
|
|
2981
|
+
}
|
|
2982
|
+
|
|
2983
|
+
/** A picture shown this wide, in px: its style says so, and the RTF writes it so. */
|
|
2984
|
+
function sizePicture(img: HTMLImageElement, px: number): void {
|
|
2985
|
+
img.removeAttribute('width');
|
|
2986
|
+
img.removeAttribute('height');
|
|
2987
|
+
img.style.width = `${px}px`;
|
|
2988
|
+
img.style.height = 'auto';
|
|
2989
|
+
}
|
|
2990
|
+
|
|
2991
|
+
/**
|
|
2992
|
+
* Lay out a row of pictures: across the page in equal shares (`gap` null), or with `gap`
|
|
2993
|
+
* points between each two, each picture in a cell as wide as it is. Pictures too wide for
|
|
2994
|
+
* the page together, with the gaps between them, are made smaller alike until they fit.
|
|
2995
|
+
*/
|
|
2996
|
+
function layoutPictureRow(table: HTMLTableElement, cells: HTMLTableCellElement[], gap: number | null): void {
|
|
2997
|
+
for (const cell of Array.from(table.rows[0]?.cells ?? [])) if (isGapCell(cell)) cell.remove();
|
|
2998
|
+
if (gap === null) {
|
|
2999
|
+
table.setAttribute('style', TABLE_STYLE);
|
|
3000
|
+
fitPictureRow(cells);
|
|
3001
|
+
return;
|
|
3002
|
+
}
|
|
3003
|
+
table.setAttribute('style', GAPPED_ROW_STYLE);
|
|
3004
|
+
const page = contentWidth();
|
|
3005
|
+
// (No wider a gap than leaves each picture its least width on the page.)
|
|
3006
|
+
gap = Math.min(gap, fittingGap(page, cells.length) ?? gap);
|
|
3007
|
+
const imgs = cells.map((cell) => cell.querySelector('img')!);
|
|
3008
|
+
// (A picture not yet loaded has no width to go by: it is laid out once it has one.)
|
|
3009
|
+
const widths = imgs.map((img) => Math.round(pictureWidth(img)));
|
|
3010
|
+
const room = page - (gap * 4) / 3 * (cells.length - 1);
|
|
3011
|
+
const total = widths.reduce((a, b) => a + b, 0);
|
|
3012
|
+
const scale = room > 0 && total > room ? room / total : 1;
|
|
3013
|
+
cells.forEach((cell, i) => {
|
|
3014
|
+
if (widths[i] > 0) {
|
|
3015
|
+
const width = Math.max(MIN_PICTURE_IN_ROW, Math.floor(widths[i] * scale));
|
|
3016
|
+
sizePicture(imgs[i], width);
|
|
3017
|
+
cell.setAttribute('style', `padding:0;width:${pointsOf(width * 15)}`);
|
|
3018
|
+
} else {
|
|
3019
|
+
cell.setAttribute('style', 'padding:0');
|
|
3020
|
+
imgs[i].addEventListener(
|
|
3021
|
+
'load',
|
|
3022
|
+
() => {
|
|
3023
|
+
if (!table.isConnected) return;
|
|
3024
|
+
if (history) history.quietly(() => relayoutPictureRow(table));
|
|
3025
|
+
else relayoutPictureRow(table);
|
|
3026
|
+
overlayRef?.reposition();
|
|
3027
|
+
},
|
|
3028
|
+
{ once: true }
|
|
3029
|
+
);
|
|
3030
|
+
}
|
|
3031
|
+
if (i === 0) return;
|
|
3032
|
+
const between = document.createElement('td');
|
|
3033
|
+
between.setAttribute('style', `padding:0;width:${pointsOf(gap * 20)}`);
|
|
3034
|
+
cell.before(between);
|
|
3035
|
+
});
|
|
3036
|
+
}
|
|
3037
|
+
|
|
3038
|
+
/**
|
|
3039
|
+
* The widest gap, in points, that leaves `count` pictures their least width each on a page
|
|
3040
|
+
* this wide (px); null where the page has no width to go by (not laid out).
|
|
3041
|
+
*/
|
|
3042
|
+
function fittingGap(page: number, count: number): number | null {
|
|
3043
|
+
if (page <= 0 || count < 2) return null;
|
|
3044
|
+
const px = (page - MIN_PICTURE_IN_ROW * count) / (count - 1);
|
|
3045
|
+
return Math.max(0, Math.floor(px * 0.75 * 20) / 20);
|
|
3046
|
+
}
|
|
3047
|
+
|
|
3048
|
+
/** Lay a row of pictures out again, as it was laid out: with the gap it has. */
|
|
3049
|
+
function relayoutPictureRow(table: HTMLTableElement): void {
|
|
3050
|
+
const row = pictureRowOf(table.rows[0]?.cells[0] ?? null);
|
|
3051
|
+
if (row) layoutPictureRow(table, row.cells, row.gap);
|
|
3052
|
+
}
|
|
3053
|
+
|
|
3054
|
+
/** The narrowest a picture in a row with a set gap is made, in px. */
|
|
3055
|
+
const MIN_PICTURE_IN_ROW = 40;
|
|
3056
|
+
|
|
3057
|
+
/** Set the space between the pictures of the selected picture's row, in points; null: they share the page equally. */
|
|
3058
|
+
function setPictureGap(gap: number | null): void {
|
|
3059
|
+
const row = !readonly && selectedImage ? pictureRowOf(selectedImage) : null;
|
|
3060
|
+
if (!row) return;
|
|
3061
|
+
let pt = gap === null || !Number.isFinite(gap) ? null : Math.min(MOST_GAP_PT, Math.max(1, Math.round(gap * 20) / 20));
|
|
3062
|
+
// (On a page too narrow for the gap asked for, the widest that fits; and none where not even 1 pt does.)
|
|
3063
|
+
const fits = fittingGap(contentWidth(), row.cells.length);
|
|
3064
|
+
if (pt !== null && fits !== null) {
|
|
3065
|
+
if (fits < 1) return;
|
|
3066
|
+
pt = Math.min(pt, fits);
|
|
3067
|
+
}
|
|
3068
|
+
if (pt === row.gap) return;
|
|
3069
|
+
history?.step();
|
|
3070
|
+
layoutPictureRow(row.table, row.cells, pt);
|
|
3071
|
+
pictureMoved();
|
|
3072
|
+
}
|
|
3073
|
+
|
|
3074
|
+
/** While a picture in a row with a set gap is resized, its cell follows it: the gap stays the gap. */
|
|
3075
|
+
function pictureResizing(): void {
|
|
3076
|
+
const row = selectedImage ? pictureRowOf(selectedImage) : null;
|
|
3077
|
+
if (!row || row.gap === null || !selectedImage) return;
|
|
3078
|
+
const cell = selectedImage.closest('td, th') as HTMLElement;
|
|
3079
|
+
cell.style.width = pointsOf(Math.round(pictureWidth(selectedImage)) * 15);
|
|
3080
|
+
}
|
|
3081
|
+
|
|
3082
|
+
/** The cells of a row of pictures share its width, and no picture is wider than its cell. */
|
|
3083
|
+
function fitPictureRow(cells: HTMLTableCellElement[]): void {
|
|
3084
|
+
const whole = contentWidth();
|
|
3085
|
+
cells.forEach((cell, i) => {
|
|
3086
|
+
const align = cell.style.textAlign;
|
|
3087
|
+
cell.setAttribute('style', `${PICTURE_CELL_STYLE}width:${columnShare(i, cells.length)}%`);
|
|
3088
|
+
if (align && align !== 'left') cell.style.textAlign = align;
|
|
3089
|
+
const img = cell.querySelector('img');
|
|
3090
|
+
const room = Math.floor(whole / cells.length) - PICTURE_CELL_PADDING;
|
|
3091
|
+
if (!img || room < 40) return;
|
|
3092
|
+
const width = parseFloat(img.style.width) || img.getBoundingClientRect().width || img.naturalWidth;
|
|
3093
|
+
if (width > room || !img.style.width) {
|
|
3094
|
+
img.removeAttribute('width');
|
|
3095
|
+
img.removeAttribute('height');
|
|
3096
|
+
img.style.width = `${Math.min(width || room, room)}px`;
|
|
3097
|
+
img.style.height = 'auto';
|
|
3098
|
+
}
|
|
3099
|
+
});
|
|
3100
|
+
}
|
|
3101
|
+
|
|
3102
|
+
/** The picture of a cell back on a line of its own, where `at` is. */
|
|
3103
|
+
function pictureOutOfCell(cell: HTMLTableCellElement, at: Element, where: 'before' | 'after'): void {
|
|
3104
|
+
const unit = pictureInCell(cell);
|
|
3105
|
+
if (!unit) return;
|
|
3106
|
+
const { figure } = figureOf(unit);
|
|
3107
|
+
const align = cell.style.textAlign;
|
|
3108
|
+
if (align && align !== 'left') figure.style.textAlign = align;
|
|
3109
|
+
at[where](figure);
|
|
3110
|
+
cell.remove();
|
|
3111
|
+
}
|
|
3112
|
+
|
|
3113
|
+
/** What is left of a row of pictures after one has gone: a narrower row, or the one picture on its own line. */
|
|
3114
|
+
function closeUpPictureRow(table: HTMLTableElement): void {
|
|
3115
|
+
const all = Array.from(table.rows[0]?.cells ?? []);
|
|
3116
|
+
const fixed = !table.style.width && !table.getAttribute('width');
|
|
3117
|
+
const gapCell = fixed ? all.find(isGapCell) : undefined;
|
|
3118
|
+
const twips = gapCell ? lengthTwips(gapCell) : null;
|
|
3119
|
+
const cells = all.filter((cell) => !(fixed && isGapCell(cell)));
|
|
3120
|
+
if (cells.length > 1) {
|
|
3121
|
+
layoutPictureRow(table, cells, twips === null ? null : twips / 20);
|
|
3122
|
+
return;
|
|
3123
|
+
}
|
|
3124
|
+
for (const cell of all) if (!cells.includes(cell)) cell.remove();
|
|
3125
|
+
if (cells.length === 1) pictureOutOfCell(cells[0], table, 'before');
|
|
3126
|
+
table.remove();
|
|
3127
|
+
}
|
|
3128
|
+
|
|
3129
|
+
/**
|
|
3130
|
+
* Put the selected picture beside the picture above it; or, where it is beside another,
|
|
3131
|
+
* back on a line of its own, under the row it was in.
|
|
3132
|
+
*/
|
|
3133
|
+
function toggleBeside(): void {
|
|
3134
|
+
const place = picturePlace();
|
|
3135
|
+
const img = selectedImage;
|
|
3136
|
+
const block = movedWith(img);
|
|
3137
|
+
if (!img || !block || !place.beside) return;
|
|
3138
|
+
history?.step();
|
|
3139
|
+
if (place.beside === 'leave') {
|
|
3140
|
+
const cell = img.closest('td, th') as HTMLTableCellElement;
|
|
3141
|
+
pictureOutOfCell(cell, block, 'after');
|
|
3142
|
+
closeUpPictureRow(block as HTMLTableElement);
|
|
3143
|
+
} else {
|
|
3144
|
+
const above = block.previousElementSibling as HTMLElement;
|
|
3145
|
+
const joined = pictureRowOf(above);
|
|
3146
|
+
let row = joined?.table.rows[0];
|
|
3147
|
+
if (!row) {
|
|
3148
|
+
const table = document.createElement('table');
|
|
3149
|
+
table.setAttribute('style', TABLE_STYLE);
|
|
3150
|
+
const body = document.createElement('tbody');
|
|
3151
|
+
row = document.createElement('tr');
|
|
3152
|
+
body.appendChild(row);
|
|
3153
|
+
table.appendChild(body);
|
|
3154
|
+
above.before(table);
|
|
3155
|
+
row.appendChild(pictureCell(above));
|
|
3156
|
+
}
|
|
3157
|
+
row.appendChild(pictureCell(block));
|
|
3158
|
+
// (With the gap the row has: one more picture, the same distance from the one before.)
|
|
3159
|
+
relayoutPictureRow(row.closest('table')!);
|
|
3160
|
+
}
|
|
3161
|
+
pictureMoved();
|
|
3162
|
+
}
|
|
3163
|
+
|
|
3164
|
+
// ── Pictures: dragged by the mouse ──
|
|
3165
|
+
// A picture dropped on the left or right half of another picture is put beside it, as
|
|
3166
|
+
// Place beside does: the two are a row, each with its caption under it. Dropped on a
|
|
3167
|
+
// picture of a row, it joins the row there (or changes places in its own row). Dropped
|
|
3168
|
+
// anywhere else, it is moved there whole, with its caption, before or after the block it
|
|
3169
|
+
// is dropped on.
|
|
3170
|
+
|
|
3171
|
+
/** A picture of the document being dragged by the mouse. */
|
|
3172
|
+
let draggedPicture: HTMLImageElement | null = null;
|
|
3173
|
+
|
|
3174
|
+
function pictureDragStarted(e: DragEvent): void {
|
|
3175
|
+
const img = e.target instanceof HTMLImageElement ? e.target : null;
|
|
3176
|
+
draggedPicture = !readonly && img && movedWith(img) ? img : null;
|
|
3177
|
+
if (draggedPicture && e.dataTransfer) e.dataTransfer.effectAllowed = 'move';
|
|
3178
|
+
}
|
|
3179
|
+
|
|
3180
|
+
/** The block of the document (a child of the editor) at a point; between blocks, the nearest one. */
|
|
3181
|
+
function blockAtPoint(x: number, y: number): HTMLElement | null {
|
|
3182
|
+
if (!editorEl) return null;
|
|
3183
|
+
for (const el of document.elementsFromPoint(x, y)) {
|
|
3184
|
+
if (el === editorEl || !editorEl.contains(el)) continue;
|
|
3185
|
+
let top: Element = el;
|
|
3186
|
+
while (top.parentElement && top.parentElement !== editorEl) top = top.parentElement;
|
|
3187
|
+
return top as HTMLElement;
|
|
3188
|
+
}
|
|
3189
|
+
let nearest: HTMLElement | null = null;
|
|
3190
|
+
let distance = Infinity;
|
|
3191
|
+
for (const child of Array.from(editorEl.children) as HTMLElement[]) {
|
|
3192
|
+
const box = child.getBoundingClientRect();
|
|
3193
|
+
const d = y < box.top ? box.top - y : y > box.bottom ? y - box.bottom : 0;
|
|
3194
|
+
if (d < distance) {
|
|
3195
|
+
distance = d;
|
|
3196
|
+
nearest = child;
|
|
3197
|
+
}
|
|
3198
|
+
}
|
|
3199
|
+
return nearest;
|
|
3200
|
+
}
|
|
3201
|
+
|
|
3202
|
+
/** Take a picture out of where it is, as a block of its own (not yet put anywhere): out of its row, which closes up. */
|
|
3203
|
+
function takePictureOut(img: HTMLImageElement): HTMLElement | null {
|
|
3204
|
+
const row = pictureRowOf(img);
|
|
3205
|
+
if (!row) {
|
|
3206
|
+
const block = movedWith(img);
|
|
3207
|
+
block?.remove();
|
|
3208
|
+
return block;
|
|
3209
|
+
}
|
|
3210
|
+
const cell = img.closest('td, th') as HTMLTableCellElement;
|
|
3211
|
+
const unit = pictureInCell(cell);
|
|
3212
|
+
if (!unit) return null;
|
|
3213
|
+
const { figure } = figureOf(unit);
|
|
3214
|
+
const align = cell.style.textAlign;
|
|
3215
|
+
if (align && align !== 'left') figure.style.textAlign = align;
|
|
3216
|
+
cell.remove();
|
|
3217
|
+
closeUpPictureRow(row.table);
|
|
3218
|
+
return figure;
|
|
3219
|
+
}
|
|
3220
|
+
|
|
3221
|
+
/** Move a picture dragged to a point: beside the picture there, or before or after the block there. Returns whether it moved. */
|
|
3222
|
+
function dropPicture(img: HTMLImageElement, x: number, y: number): boolean {
|
|
3223
|
+
const source = movedWith(img);
|
|
3224
|
+
const target = blockAtPoint(x, y);
|
|
3225
|
+
if (!source || !target) return false;
|
|
3226
|
+
const sourceRow = pictureRowOf(img);
|
|
3227
|
+
const targetRow = pictureRowOf(target);
|
|
3228
|
+
const halfOf = (box: DOMRect, along: 'x' | 'y') => ((along === 'x' ? x < box.left + box.width / 2 : y < box.top + box.height / 2) ? 'before' : 'after');
|
|
3229
|
+
|
|
3230
|
+
if (targetRow || isPictureBlock(target)) {
|
|
3231
|
+
// The picture of the row it is dropped on: the one under the pointer, or the nearest.
|
|
3232
|
+
const cell = targetRow
|
|
3233
|
+
? targetRow.cells.reduce((best, c) => {
|
|
3234
|
+
const box = c.getBoundingClientRect();
|
|
3235
|
+
const d = x < box.left ? box.left - x : x > box.right ? x - box.right : 0;
|
|
3236
|
+
const bestBox = best.getBoundingClientRect();
|
|
3237
|
+
const bestD = x < bestBox.left ? bestBox.left - x : x > bestBox.right ? x - bestBox.right : 0;
|
|
3238
|
+
return d < bestD ? c : best;
|
|
3239
|
+
})
|
|
3240
|
+
: null;
|
|
3241
|
+
const onto = cell ?? target;
|
|
3242
|
+
const side = halfOf((cell?.querySelector('img') ?? target.querySelector('img') ?? target).getBoundingClientRect(), 'x');
|
|
3243
|
+
if (onto === source || onto === img.closest('td, th')) return false;
|
|
3244
|
+
if (sourceRow && targetRow && sourceRow.table === targetRow.table) {
|
|
3245
|
+
// (Its own row: it changes places.)
|
|
3246
|
+
cell!);
|
|
3247
|
+
relayoutPictureRow(targetRow.table);
|
|
3248
|
+
return true;
|
|
3249
|
+
}
|
|
3250
|
+
if (!targetRow || targetRow.cells.length < MOST_PICTURES_IN_ROW) {
|
|
3251
|
+
const unit = takePictureOut(img);
|
|
3252
|
+
if (!unit) return false;
|
|
3253
|
+
if (targetRow) {
|
|
3254
|
+
cell);
|
|
3255
|
+
relayoutPictureRow(targetRow.table);
|
|
3256
|
+
} else {
|
|
3257
|
+
const table = document.createElement('table');
|
|
3258
|
+
table.setAttribute('style', TABLE_STYLE);
|
|
3259
|
+
const body = document.createElement('tbody');
|
|
3260
|
+
const row = document.createElement('tr');
|
|
3261
|
+
body.appendChild(row);
|
|
3262
|
+
table.appendChild(body);
|
|
3263
|
+
target.before(table);
|
|
3264
|
+
// (The cells in the order they are seen: the one dropped on its left is first.)
|
|
3265
|
+
const first = pictureCell(side === 'before' ? unit : target);
|
|
3266
|
+
const second = pictureCell(side === 'before' ? target : unit);
|
|
3267
|
+
row.append(first, second);
|
|
3268
|
+
fitPictureRow([first, second]);
|
|
3269
|
+
}
|
|
3270
|
+
return true;
|
|
3271
|
+
}
|
|
3272
|
+
// (A row that is full: the picture goes under it, or over it.)
|
|
3273
|
+
}
|
|
3274
|
+
if (target === source) return false;
|
|
3275
|
+
const side = halfOf(target.getBoundingClientRect(), 'y');
|
|
3276
|
+
const unit = takePictureOut(img);
|
|
3277
|
+
if (!unit || !target.isConnected) return false;
|
|
3278
|
+
target[side](unit);
|
|
3279
|
+
return true;
|
|
3280
|
+
}
|
|
3281
|
+
|
|
3282
|
+
/** Width in px a picture has room for: its cell where it is one of a row of pictures, else the page. */
|
|
3283
|
+
function pictureRoom(): number {
|
|
3284
|
+
const whole = contentWidth();
|
|
3285
|
+
const row = selectedImage ? pictureRowOf(selectedImage) : null;
|
|
3286
|
+
if (!row) return whole;
|
|
3287
|
+
if (row.gap === null) return Math.max(0, Math.floor(whole / row.cells.length) - PICTURE_CELL_PADDING);
|
|
3288
|
+
// (With a set gap: the page, less the gaps and the other pictures.)
|
|
3289
|
+
const others = row.cells.reduce((sum, cell) => (cell.contains(selectedImage) ? sum : sum + pictureWidth(cell.querySelector('img')!)), 0);
|
|
3290
|
+
// (At least a little over the least width a picture is given here: the frame takes 40 px
|
|
3291
|
+
// and less as no limit at all.)
|
|
3292
|
+
return Math.max(MIN_PICTURE_IN_ROW + 1, Math.floor(whole - (row.gap * 4) / 3 * (row.cells.length - 1) - others));
|
|
3293
|
+
}
|
|
3294
|
+
|
|
2425
3295
|
function removeSelectedImage(): void {
|
|
2426
3296
|
if (!selectedImage) return;
|
|
3297
|
+
history?.step();
|
|
3298
|
+
const row = pictureRowOf(selectedImage);
|
|
3299
|
+
if (row) {
|
|
3300
|
+
// One of a row of pictures: the others close up, and a last one is on its own line again.
|
|
3301
|
+
selectedImage.closest('td, th')!.remove();
|
|
3302
|
+
selectedImage = null;
|
|
3303
|
+
const next = row.table.nextElementSibling as HTMLElement | null;
|
|
3304
|
+
closeUpPictureRow(row.table);
|
|
3305
|
+
if (next && !/^(TABLE|FIGURE)$/.test(next.tagName)) placeCaret(next);
|
|
3306
|
+
updateCounts();
|
|
3307
|
+
fireChange();
|
|
3308
|
+
if (autosave) scheduleAutoSave();
|
|
3309
|
+
return;
|
|
3310
|
+
}
|
|
2427
3311
|
const figure = selectedImage.closest('figure');
|
|
2428
3312
|
const next = (figure ?? selectedImage).nextElementSibling as HTMLElement | null;
|
|
2429
3313
|
(figure ?? selectedImage).remove();
|
|
@@ -2436,7 +3320,16 @@
|
|
|
2436
3320
|
|
|
2437
3321
|
/** Put the caret in the image's description line, creating it if needed. */
|
|
2438
3322
|
function focusCaption(img: HTMLImageElement): void {
|
|
2439
|
-
|
|
3323
|
+
history?.step();
|
|
3324
|
+
let figure = img.closest('figure');
|
|
3325
|
+
// One of a row of pictures that had no caption is read back as the picture alone in
|
|
3326
|
+
// its cell: it is a figure again when it is given one.
|
|
3327
|
+
const cell = img.parentElement;
|
|
3328
|
+
if (!figure && cell && /^(TD|TH)$/.test(cell.tagName) && pictureInCell(cell) === img) {
|
|
3329
|
+
figure = document.createElement('figure');
|
|
3330
|
+
img.before(figure);
|
|
3331
|
+
figure.appendChild(img);
|
|
3332
|
+
}
|
|
2440
3333
|
if (!figure) return;
|
|
2441
3334
|
let caption = figure.querySelector('figcaption');
|
|
2442
3335
|
if (!caption) {
|
|
@@ -2456,10 +3349,33 @@
|
|
|
2456
3349
|
function handleDragOver(e: DragEvent): void {
|
|
2457
3350
|
if (readonly) return;
|
|
2458
3351
|
if (Array.from(e.dataTransfer?.types || []).includes('Files')) e.preventDefault();
|
|
3352
|
+
// (A picture of the document is the editor's to drop: said so, or WebKit drops it nowhere.)
|
|
3353
|
+
if (draggedPicture) {
|
|
3354
|
+
e.preventDefault();
|
|
3355
|
+
if (e.dataTransfer) e.dataTransfer.dropEffect = 'move';
|
|
3356
|
+
}
|
|
2459
3357
|
}
|
|
2460
3358
|
|
|
2461
3359
|
function handleDrop(e: DragEvent): void {
|
|
2462
3360
|
if (readonly) return;
|
|
3361
|
+
// A picture of the document dragged by the mouse is moved by the editor, as its buttons
|
|
3362
|
+
// move it: the browser put it in the line it was dropped in, inside another picture's
|
|
3363
|
+
// figure, with its caption left behind as an empty line of its own.
|
|
3364
|
+
if (draggedPicture) {
|
|
3365
|
+
const img = draggedPicture;
|
|
3366
|
+
draggedPicture = null;
|
|
3367
|
+
e.preventDefault();
|
|
3368
|
+
if (!img.isConnected) return;
|
|
3369
|
+
history?.step();
|
|
3370
|
+
if (dropPicture(img, e.clientX, e.clientY)) {
|
|
3371
|
+
selectedImage = img;
|
|
3372
|
+
pictureMoved();
|
|
3373
|
+
}
|
|
3374
|
+
return;
|
|
3375
|
+
}
|
|
3376
|
+
// (A drop is a step of its own; where the words were already taken out from further up
|
|
3377
|
+
// the document, it is the step that began with that.)
|
|
3378
|
+
if (!dragTaken) history?.step();
|
|
2463
3379
|
const files = Array.from(e.dataTransfer?.files || []).filter((f) => f.type.startsWith('image/'));
|
|
2464
3380
|
if (files.length === 0) {
|
|
2465
3381
|
// Text or markup dragged in, from another page or from further up this one. The
|
|
@@ -2480,7 +3396,7 @@
|
|
|
2480
3396
|
e.preventDefault();
|
|
2481
3397
|
editorEl?.focus();
|
|
2482
3398
|
placeCaretAtPoint(e.clientX, e.clientY);
|
|
2483
|
-
|
|
3399
|
+
browserCommand('insertHTML', false, unframed);
|
|
2484
3400
|
}
|
|
2485
3401
|
return;
|
|
2486
3402
|
}
|
|
@@ -2538,6 +3454,7 @@
|
|
|
2538
3454
|
// (What the reader writes is safe as it stands; it goes the same way as any other
|
|
2539
3455
|
// HTML so there is one way in.)
|
|
2540
3456
|
const shown = showDocument(html);
|
|
3457
|
+
newDocument();
|
|
2541
3458
|
updateCounts();
|
|
2542
3459
|
// (Shown or not, the document before it is gone, and the application is told so.)
|
|
2543
3460
|
fireChange();
|
|
@@ -2570,10 +3487,13 @@
|
|
|
2570
3487
|
if (mod && e.key === 'u') { e.preventDefault(); exec('underline'); }
|
|
2571
3488
|
if (mod && e.key === 'k') { e.preventDefault(); openLinkModal(); }
|
|
2572
3489
|
if (mod && e.key === 's') { e.preventDefault(); saveNow(); }
|
|
2573
|
-
|
|
3490
|
+
// Undo and Redo are the editor's own: the browser's is never asked. (While an input
|
|
3491
|
+
// method composes, the keys are the method's: what it does with them is its own.)
|
|
3492
|
+
const composingKey = e.isComposing || e.keyCode === 229;
|
|
3493
|
+
if (mod && !composingKey && !e.shiftKey && !e.altKey && e.key.toLowerCase() === 'z') { e.preventDefault(); stepBack(true); return; }
|
|
2574
3494
|
// (Redo is Ctrl+Y as well, but not Cmd+Y.)
|
|
2575
3495
|
const redoKey = (e.shiftKey && e.key.toLowerCase() === 'z') || (e.ctrlKey && !e.metaKey && e.key.toLowerCase() === 'y');
|
|
2576
|
-
if (mod && redoKey &&
|
|
3496
|
+
if (mod && !composingKey && redoKey && !e.altKey) { e.preventDefault(); stepBack(false); return; }
|
|
2577
3497
|
if (mod && e.key === ']') { e.preventDefault(); indentBlocks('indent'); return; }
|
|
2578
3498
|
if (mod && e.key === '[') { e.preventDefault(); indentBlocks('outdent'); return; }
|
|
2579
3499
|
|
|
@@ -2593,6 +3513,8 @@
|
|
|
2593
3513
|
return;
|
|
2594
3514
|
}
|
|
2595
3515
|
|
|
3516
|
+
if ((e.key === 'Enter' || e.key === 'Backspace') && leaveListAt(e)) return;
|
|
3517
|
+
|
|
2596
3518
|
if (e.key === 'Tab') handleTab(e);
|
|
2597
3519
|
else tabLeavesEditor = e.key === 'Escape';
|
|
2598
3520
|
}
|
|
@@ -2614,7 +3536,10 @@
|
|
|
2614
3536
|
e.preventDefault();
|
|
2615
3537
|
|
|
2616
3538
|
if (sel.anchorNode.parentElement?.closest?.('pre')) {
|
|
2617
|
-
if (!e.shiftKey)
|
|
3539
|
+
if (!e.shiftKey) {
|
|
3540
|
+
history?.typing('insert');
|
|
3541
|
+
browserCommand('insertText', false, ' ');
|
|
3542
|
+
}
|
|
2618
3543
|
return;
|
|
2619
3544
|
}
|
|
2620
3545
|
// Several lines selected: Tab and Shift+Tab indent and outdent them all, as a word
|
|
@@ -2624,12 +3549,28 @@
|
|
|
2624
3549
|
return;
|
|
2625
3550
|
}
|
|
2626
3551
|
if (!e.shiftKey) {
|
|
3552
|
+
// At the start of an item, Tab puts the item under the one before it. (Anywhere
|
|
3553
|
+
// else in it, and in the first item, it types a tab.)
|
|
3554
|
+
if (atItemStart(editorEl, sel)) {
|
|
3555
|
+
history?.step();
|
|
3556
|
+
if (nestItems(editorEl, sel)) {
|
|
3557
|
+
indentChanged();
|
|
3558
|
+
return;
|
|
3559
|
+
}
|
|
3560
|
+
}
|
|
3561
|
+
// (A tab is typed, with the words around it.)
|
|
3562
|
+
history?.typing('insert');
|
|
2627
3563
|
insertTab(sel);
|
|
2628
3564
|
return;
|
|
2629
3565
|
}
|
|
2630
3566
|
// Shift+Tab takes one tab off the start of the line the caret is on, wherever the caret is
|
|
2631
|
-
// on it, as a word processor outdents
|
|
2632
|
-
|
|
3567
|
+
// on it, as a word processor outdents; and an item that begins with none out from under
|
|
3568
|
+
// the item it is under.
|
|
3569
|
+
if (sel.isCollapsed) {
|
|
3570
|
+
history?.step();
|
|
3571
|
+
if (leadingTab(sel)) removeLeadingTab(sel);
|
|
3572
|
+
else if (unnestItems(editorEl, sel)) indentChanged();
|
|
3573
|
+
}
|
|
2633
3574
|
}
|
|
2634
3575
|
|
|
2635
3576
|
/**
|
|
@@ -2639,10 +3580,10 @@
|
|
|
2639
3580
|
* is not typed inside it.
|
|
2640
3581
|
*/
|
|
2641
3582
|
function insertTab(sel: Selection): void {
|
|
2642
|
-
|
|
3583
|
+
browserCommand('insertText', false, '\t');
|
|
2643
3584
|
if (textBeforeCaret(sel).endsWith('\t')) return;
|
|
2644
|
-
|
|
2645
|
-
|
|
3585
|
+
browserUndo();
|
|
3586
|
+
browserCommand('insertHTML', false, '<span style="white-space:pre">\t</span>');
|
|
2646
3587
|
const node = sel.anchorNode;
|
|
2647
3588
|
const span = (node instanceof Element ? node : node?.parentElement)?.closest('span');
|
|
2648
3589
|
if (span && span.textContent === '\t' && editorEl?.contains(span)) {
|
|
@@ -2722,7 +3663,7 @@
|
|
|
2722
3663
|
// Delete through execCommand so Ctrl+Z puts the tab back, then return the caret.
|
|
2723
3664
|
sel.removeAllRanges();
|
|
2724
3665
|
sel.addRange(range);
|
|
2725
|
-
|
|
3666
|
+
browserCommand('delete');
|
|
2726
3667
|
if (caret.startContainer.isConnected) {
|
|
2727
3668
|
sel.removeAllRanges();
|
|
2728
3669
|
sel.addRange(caret);
|
|
@@ -2730,6 +3671,8 @@
|
|
|
2730
3671
|
}
|
|
2731
3672
|
|
|
2732
3673
|
function handlePaste(e: ClipboardEvent): void {
|
|
3674
|
+
// (A paste is a step of its own, with what the editor makes of it.)
|
|
3675
|
+
if (!readonly) history?.step();
|
|
2733
3676
|
// clipboardData is only valid during the event — pull the files out first.
|
|
2734
3677
|
const files = Array.from(e.clipboardData?.items || [])
|
|
2735
3678
|
.filter((item) => item.kind === 'file' && item.type.startsWith('image/'))
|
|
@@ -2751,7 +3694,7 @@
|
|
|
2751
3694
|
if (sel?.anchorNode?.parentElement?.closest?.('pre')) {
|
|
2752
3695
|
e.preventDefault();
|
|
2753
3696
|
const text = e.clipboardData?.getData('text/plain') || '';
|
|
2754
|
-
|
|
3697
|
+
browserCommand('insertText', false, text);
|
|
2755
3698
|
return;
|
|
2756
3699
|
}
|
|
2757
3700
|
// The browser pastes next; the input event that follows tidies what came in.
|
|
@@ -2770,7 +3713,7 @@
|
|
|
2770
3713
|
const unframed = pasteWithoutFrames(html, keptNames);
|
|
2771
3714
|
if (unframed !== null) {
|
|
2772
3715
|
e.preventDefault();
|
|
2773
|
-
|
|
3716
|
+
browserCommand('insertHTML', false, unframed);
|
|
2774
3717
|
}
|
|
2775
3718
|
}
|
|
2776
3719
|
}
|
|
@@ -3303,7 +4246,11 @@
|
|
|
3303
4246
|
onMount(() => {
|
|
3304
4247
|
if (!editorEl) return;
|
|
3305
4248
|
document.addEventListener('selectionchange', trackSelection);
|
|
3306
|
-
const stopTracking = () =>
|
|
4249
|
+
const stopTracking = () => {
|
|
4250
|
+
document.removeEventListener('selectionchange', trackSelection);
|
|
4251
|
+
history?.destroy();
|
|
4252
|
+
history = null;
|
|
4253
|
+
};
|
|
3307
4254
|
if (autosave) {
|
|
3308
4255
|
let saved: string | null = null;
|
|
3309
4256
|
try {
|
|
@@ -3314,6 +4261,7 @@
|
|
|
3314
4261
|
// origin's storage can write it.
|
|
3315
4262
|
if (showDocument(saved)) {
|
|
3316
4263
|
updateCounts();
|
|
4264
|
+
history = createHistory(editorEl);
|
|
3317
4265
|
return stopTracking;
|
|
3318
4266
|
}
|
|
3319
4267
|
// (A draft that cannot be shown was dropped without a word; it is said now, and
|
|
@@ -3328,9 +4276,33 @@
|
|
|
3328
4276
|
// used to go up through the component as it was made, and there was no editor.)
|
|
3329
4277
|
if (!showDocument(content || EMPTY_DOCUMENT)) showError(NOT_SHOWN);
|
|
3330
4278
|
updateCounts();
|
|
4279
|
+
// (From the document as it is first shown: there is nothing before it to undo to.)
|
|
4280
|
+
history = createHistory(editorEl);
|
|
3331
4281
|
return stopTracking;
|
|
3332
4282
|
});
|
|
3333
4283
|
|
|
4284
|
+
// The markers of a list with a gap, drawn by the editor (see markItems), follow every change
|
|
4285
|
+
// to the document: an item put in or taken out, a list renumbered, its gap set. They are no
|
|
4286
|
+
// step of the history of their own: they go with the change they follow.
|
|
4287
|
+
$effect(() => {
|
|
4288
|
+
if (!editorEl || typeof MutationObserver === 'undefined') return;
|
|
4289
|
+
const root = editorEl;
|
|
4290
|
+
let queued = false;
|
|
4291
|
+
const mark = () => {
|
|
4292
|
+
queued = false;
|
|
4293
|
+
if (history) history.quietly(() => markItems(root));
|
|
4294
|
+
else markItems(root);
|
|
4295
|
+
};
|
|
4296
|
+
const observer = new MutationObserver(() => {
|
|
4297
|
+
if (queued) return;
|
|
4298
|
+
queued = true;
|
|
4299
|
+
queueMicrotask(mark);
|
|
4300
|
+
});
|
|
4301
|
+
observer.observe(root, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'start', 'type', 'value'] });
|
|
4302
|
+
mark();
|
|
4303
|
+
return () => observer.disconnect();
|
|
4304
|
+
});
|
|
4305
|
+
|
|
3334
4306
|
// Suppress unused warning for importing state (used in template)
|
|
3335
4307
|
$effect(() => { void importing; });
|
|
3336
4308
|
</script>
|
|
@@ -3372,8 +4344,14 @@
|
|
|
3372
4344
|
}}
|
|
3373
4345
|
/>
|
|
3374
4346
|
|
|
4347
|
+
<!-- (Whatever a click or a press on the editor goes on to do is a step of its own in the
|
|
4348
|
+
history: what was typed before it ends there. Heard on the way down, before the button
|
|
4349
|
+
or the picture's handle that does it.) -->
|
|
4350
|
+
<!-- svelte-ignore a11y_no_static_element_interactions, a11y_click_events_have_key_events -->
|
|
3375
4351
|
<div
|
|
3376
4352
|
class="ink-editor"
|
|
4353
|
+
onpointerdowncapture={() => history?.step()}
|
|
4354
|
+
onclickcapture={() => history?.step()}
|
|
3377
4355
|
style:--ink-min-height={minHeight}
|
|
3378
4356
|
style:--ink-font-family={screenFontFamily}
|
|
3379
4357
|
style:--ink-font-size="{screenFontSize}pt"
|
|
@@ -3388,6 +4366,7 @@
|
|
|
3388
4366
|
onblock={setBlock}
|
|
3389
4367
|
oninsertlink={openLinkModal}
|
|
3390
4368
|
oninsertimage={openImageModal}
|
|
4369
|
+
oninserttable={openTableModal}
|
|
3391
4370
|
fontSize={currentFontSize}
|
|
3392
4371
|
fontSizes={sizeChoices}
|
|
3393
4372
|
onfontsize={setFontSize}
|
|
@@ -3395,6 +4374,7 @@
|
|
|
3395
4374
|
fonts={fontList}
|
|
3396
4375
|
documentFont={fontChoices([fontFamily])[0]?.name ?? DEFAULT_FONT_FAMILY}
|
|
3397
4376
|
onfont={setFont}
|
|
4377
|
+
spacing={currentSpacing}
|
|
3398
4378
|
copyingFormat={copyFormat !== 'off'}
|
|
3399
4379
|
oncopyformat={toggleCopyFormat}
|
|
3400
4380
|
/>
|
|
@@ -3434,7 +4414,10 @@
|
|
|
3434
4414
|
aria-readonly={readonly ? 'true' : undefined}
|
|
3435
4415
|
spellcheck="true"
|
|
3436
4416
|
bind:this={editorEl}
|
|
4417
|
+
onbeforeinput={handleBeforeInput}
|
|
3437
4418
|
oninput={handleInput}
|
|
4419
|
+
oncompositionstart={() => history?.composing(true)}
|
|
4420
|
+
oncompositionend={() => history?.composing(false)}
|
|
3438
4421
|
onmouseup={caretMoved}
|
|
3439
4422
|
onkeyup={caretMoved}
|
|
3440
4423
|
onscroll={contentScrolled}
|
|
@@ -3448,13 +4431,31 @@
|
|
|
3448
4431
|
onblur={() => (tabLeavesEditor = false)}
|
|
3449
4432
|
onpaste={handlePaste}
|
|
3450
4433
|
onclick={handleContentClick}
|
|
3451
|
-
ondragstart={() =>
|
|
3452
|
-
|
|
4434
|
+
ondragstart={(e) => {
|
|
4435
|
+
draggedFromHere = true;
|
|
4436
|
+
pictureDragStarted(e);
|
|
4437
|
+
}}
|
|
4438
|
+
ondragend={() => {
|
|
4439
|
+
draggedFromHere = false;
|
|
4440
|
+
draggedPicture = null;
|
|
4441
|
+
}}
|
|
3453
4442
|
ondragover={handleDragOver}
|
|
3454
4443
|
ondrop={handleDrop}
|
|
4444
|
+
oncontextmenu={openFontMenu}
|
|
3455
4445
|
data-placeholder={placeholder}
|
|
3456
4446
|
></div>
|
|
3457
4447
|
|
|
4448
|
+
{#if fontMenuAt}
|
|
4449
|
+
<FontMenu
|
|
4450
|
+
x={fontMenuAt.x}
|
|
4451
|
+
y={fontMenuAt.y}
|
|
4452
|
+
choices={fontMenuChoices}
|
|
4453
|
+
current={currentFont}
|
|
4454
|
+
onpick={pickFont}
|
|
4455
|
+
onclose={closeFontMenu}
|
|
4456
|
+
/>
|
|
4457
|
+
{/if}
|
|
4458
|
+
|
|
3458
4459
|
{#if copySourceBox}
|
|
3459
4460
|
<div
|
|
3460
4461
|
class="ink-copy-format-source"
|
|
@@ -3484,10 +4485,15 @@
|
|
|
3484
4485
|
target={selectedImage}
|
|
3485
4486
|
bounds={contentWrapEl}
|
|
3486
4487
|
within={editorEl}
|
|
3487
|
-
maxWidth={
|
|
4488
|
+
maxWidth={pictureRoom}
|
|
3488
4489
|
onchange={handleImageEdited}
|
|
3489
4490
|
onremove={removeSelectedImage}
|
|
3490
4491
|
oncaption={focusCaption}
|
|
4492
|
+
place={picturePlace}
|
|
4493
|
+
onmove={moveSelectedImage}
|
|
4494
|
+
onbeside={toggleBeside}
|
|
4495
|
+
ongap={setPictureGap}
|
|
4496
|
+
onresizing={pictureResizing}
|
|
3491
4497
|
/>
|
|
3492
4498
|
{/if}
|
|
3493
4499
|
</div>
|
|
@@ -3530,6 +4536,26 @@
|
|
|
3530
4536
|
{/snippet}
|
|
3531
4537
|
</Modal>
|
|
3532
4538
|
|
|
4539
|
+
<!-- Table Modal -->
|
|
4540
|
+
<Modal title="Insert Table" open={tableModalOpen} onclose={() => (tableModalOpen = false)}>
|
|
4541
|
+
<div class="ink-table-size">
|
|
4542
|
+
<label>
|
|
4543
|
+
<span>Rows</span>
|
|
4544
|
+
<input type="number" min="1" max={MOST_TABLE_ROWS} step="1" data-autofocus bind:value={tableRows} onkeydown={(e) => onEnter(e, applyTable)} />
|
|
4545
|
+
</label>
|
|
4546
|
+
<label>
|
|
4547
|
+
<span>Columns</span>
|
|
4548
|
+
<input type="number" min="1" max={MOST_TABLE_COLUMNS} step="1" bind:value={tableColumns} onkeydown={(e) => onEnter(e, applyTable)} />
|
|
4549
|
+
</label>
|
|
4550
|
+
</div>
|
|
4551
|
+
<label class="ink-check"><input type="checkbox" bind:checked={tableBorders} /> <span>Borders</span></label>
|
|
4552
|
+
<label class="ink-check"><input type="checkbox" bind:checked={tableHeader} /> <span>First row is a header</span></label>
|
|
4553
|
+
{#snippet actions()}
|
|
4554
|
+
<button class="ink-btn-ghost" onclick={() => (tableModalOpen = false)}>Cancel</button>
|
|
4555
|
+
<button class="ink-btn-primary" onclick={applyTable}>Insert</button>
|
|
4556
|
+
{/snippet}
|
|
4557
|
+
</Modal>
|
|
4558
|
+
|
|
3533
4559
|
<!-- Image Modal -->
|
|
3534
4560
|
<Modal title="Insert Image" open={imageModalOpen} onclose={() => (imageModalOpen = false)}>
|
|
3535
4561
|
<button class="ink-file-pick" onclick={chooseImageFiles}>
|
|
@@ -3866,10 +4892,54 @@
|
|
|
3866
4892
|
margin-bottom: 12px;
|
|
3867
4893
|
}
|
|
3868
4894
|
|
|
4895
|
+
/* The bullets and the numbers are said here, and not left to the browser's own style
|
|
4896
|
+
sheet: the page an editor is put in often takes them off every list it has
|
|
4897
|
+
(`ol, ul { list-style: none }`, as Tailwind's base styles and most CSS resets do), and
|
|
4898
|
+
a list in the editor was then indented lines with nothing in front of them. A list
|
|
4899
|
+
that says its own marker (type="A", or a list-style-type of its own) keeps it. */
|
|
4900
|
+
.ink-content :global(ul:not([type])) {
|
|
4901
|
+
list-style-type: disc;
|
|
4902
|
+
}
|
|
4903
|
+
|
|
4904
|
+
.ink-content :global(:is(ul, ol) ul:not([type])) {
|
|
4905
|
+
list-style-type: circle;
|
|
4906
|
+
}
|
|
4907
|
+
|
|
4908
|
+
.ink-content :global(:is(ul, ol) :is(ul, ol) ul:not([type])) {
|
|
4909
|
+
list-style-type: square;
|
|
4910
|
+
}
|
|
4911
|
+
|
|
4912
|
+
.ink-content :global(ol:not([type])) {
|
|
4913
|
+
list-style-type: decimal;
|
|
4914
|
+
}
|
|
4915
|
+
|
|
4916
|
+
.ink-content :global(li) {
|
|
4917
|
+
display: list-item;
|
|
4918
|
+
}
|
|
4919
|
+
|
|
3869
4920
|
.ink-content :global(li) {
|
|
3870
4921
|
margin-bottom: 4px;
|
|
3871
4922
|
}
|
|
3872
4923
|
|
|
4924
|
+
/* A list with a gap after its markers (its own padding-left: see markerGapTwips) has its
|
|
4925
|
+
markers drawn by the editor, at the list's edge, where Word has its number: the text is
|
|
4926
|
+
the gap further in. The browser's own marker is drawn against the text, by how wide it
|
|
4927
|
+
is, and could not be put there. */
|
|
4928
|
+
.ink-content :global(:is(ul, ol):has(> li[data-ink-marker])) {
|
|
4929
|
+
position: relative;
|
|
4930
|
+
}
|
|
4931
|
+
|
|
4932
|
+
.ink-content :global(li[data-ink-marker]) {
|
|
4933
|
+
list-style: none;
|
|
4934
|
+
}
|
|
4935
|
+
|
|
4936
|
+
.ink-content :global(li[data-ink-marker]::before) {
|
|
4937
|
+
content: attr(data-ink-marker);
|
|
4938
|
+
position: absolute;
|
|
4939
|
+
left: 0;
|
|
4940
|
+
white-space: nowrap;
|
|
4941
|
+
}
|
|
4942
|
+
|
|
3873
4943
|
.ink-content :global(a) {
|
|
3874
4944
|
color: var(--accent, #d4622b);
|
|
3875
4945
|
text-decoration: underline;
|
|
@@ -3884,8 +4954,31 @@
|
|
|
3884
4954
|
}
|
|
3885
4955
|
|
|
3886
4956
|
/* ── Figures (image + description) ── */
|
|
4957
|
+
/* A picture has the space of a paragraph, which is what the RTF gives it: none of its own.
|
|
4958
|
+
The space before and after it is set from the Spacing list, and shows as it is set.
|
|
4959
|
+
(It had 20px here that no reader of the RTF showed, and a space that was set was lost
|
|
4960
|
+
in it.) */
|
|
3887
4961
|
.ink-content :global(figure) {
|
|
3888
|
-
margin:
|
|
4962
|
+
margin: 0 0 0.2em;
|
|
4963
|
+
}
|
|
4964
|
+
|
|
4965
|
+
/* A cell with no border has none in the RTF. While the document is edited it is drawn with
|
|
4966
|
+
a faint line, or a table of empty cells could not be seen at all. (Not a cell that holds
|
|
4967
|
+
a picture: pictures side by side are a row of such cells, and show where they are.) */
|
|
4968
|
+
/* (Nor the empty cell between two pictures with a set gap: it is the gap.) */
|
|
4969
|
+
.ink-content[contenteditable='true'] :global(:is(td, th):not([style*='border']):not(:has(> figure, > img)):not(table:not([style*='width']) tr:has(> td > :is(figure, img)) > td:empty)) {
|
|
4970
|
+
outline: 1px dashed var(--border, #e5e2dc);
|
|
4971
|
+
outline-offset: -1px;
|
|
4972
|
+
}
|
|
4973
|
+
|
|
4974
|
+
/* A picture in a cell starts at the top of it, where the RTF has it. */
|
|
4975
|
+
.ink-content :global(:is(td, th):has(> figure, > img)) {
|
|
4976
|
+
vertical-align: top;
|
|
4977
|
+
}
|
|
4978
|
+
|
|
4979
|
+
.ink-content :global(:is(td, th) > img:only-child) {
|
|
4980
|
+
margin: 0;
|
|
4981
|
+
vertical-align: bottom;
|
|
3889
4982
|
}
|
|
3890
4983
|
|
|
3891
4984
|
.ink-content :global(figure img) {
|
|
@@ -3999,6 +5092,45 @@
|
|
|
3999
5092
|
color: var(--text-muted, #8a7e72);
|
|
4000
5093
|
}
|
|
4001
5094
|
|
|
5095
|
+
.ink-table-size {
|
|
5096
|
+
display: flex;
|
|
5097
|
+
gap: 16px;
|
|
5098
|
+
margin-bottom: 14px;
|
|
5099
|
+
}
|
|
5100
|
+
|
|
5101
|
+
.ink-table-size label {
|
|
5102
|
+
display: flex;
|
|
5103
|
+
flex-direction: column;
|
|
5104
|
+
gap: 4px;
|
|
5105
|
+
flex: 1;
|
|
5106
|
+
font-size: 13px;
|
|
5107
|
+
color: var(--text-muted, #8a7e72);
|
|
5108
|
+
}
|
|
5109
|
+
|
|
5110
|
+
.ink-table-size input {
|
|
5111
|
+
padding: 9px 12px;
|
|
5112
|
+
border: 1px solid var(--border, #e5e2dc);
|
|
5113
|
+
border-radius: var(--radius-sm, 5px);
|
|
5114
|
+
font: inherit;
|
|
5115
|
+
font-size: 14px;
|
|
5116
|
+
color: var(--text, #2c2520);
|
|
5117
|
+
outline: none;
|
|
5118
|
+
}
|
|
5119
|
+
|
|
5120
|
+
.ink-table-size input:focus {
|
|
5121
|
+
border-color: var(--accent, #d4622b);
|
|
5122
|
+
}
|
|
5123
|
+
|
|
5124
|
+
.ink-check {
|
|
5125
|
+
display: flex;
|
|
5126
|
+
align-items: center;
|
|
5127
|
+
gap: 8px;
|
|
5128
|
+
margin-bottom: 8px;
|
|
5129
|
+
font-size: 14px;
|
|
5130
|
+
color: var(--text, #2c2520);
|
|
5131
|
+
cursor: pointer;
|
|
5132
|
+
}
|
|
5133
|
+
|
|
4002
5134
|
.ink-modal-error {
|
|
4003
5135
|
margin: 0 0 12px;
|
|
4004
5136
|
font-size: 12px;
|