@folijs/view 0.1.0 → 0.2.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/CHANGELOG.md +26 -0
- package/dist/clipboard.d.ts +9 -2
- package/dist/clipboard.d.ts.map +1 -1
- package/dist/clipboard.js +370 -51
- package/dist/clipboard.js.map +1 -1
- package/dist/render.d.ts +17 -4
- package/dist/render.d.ts.map +1 -1
- package/dist/render.js +104 -8
- package/dist/render.js.map +1 -1
- package/dist/view.d.ts +42 -6
- package/dist/view.d.ts.map +1 -1
- package/dist/view.js +490 -74
- package/dist/view.js.map +1 -1
- package/package.json +2 -2
- package/src/clipboard.ts +399 -60
- package/src/render.ts +109 -10
- package/src/view.ts +491 -69
package/src/view.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
AllSelection,
|
|
3
|
+
arrowAcrossLeaf,
|
|
4
|
+
autolinkHref,
|
|
3
5
|
type Command,
|
|
4
6
|
closeHistory,
|
|
5
7
|
type DocumentSettings,
|
|
@@ -9,12 +11,15 @@ import {
|
|
|
9
11
|
type Editor,
|
|
10
12
|
type EditorState,
|
|
11
13
|
insertHardBreak,
|
|
14
|
+
insertLink,
|
|
12
15
|
insertText,
|
|
13
16
|
type Node as ModelNode,
|
|
14
17
|
marksAt,
|
|
18
|
+
NodeSelection,
|
|
15
19
|
redo,
|
|
16
20
|
Selection,
|
|
17
21
|
Slice,
|
|
22
|
+
setLink,
|
|
18
23
|
splitBlock,
|
|
19
24
|
TextSelection,
|
|
20
25
|
type Transaction,
|
|
@@ -32,7 +37,28 @@ import {
|
|
|
32
37
|
PX_PER_MM,
|
|
33
38
|
type Reuse,
|
|
34
39
|
} from "./layout/flow.js";
|
|
35
|
-
import {
|
|
40
|
+
import {
|
|
41
|
+
type BlockDesc,
|
|
42
|
+
createWrapper,
|
|
43
|
+
isWrapper,
|
|
44
|
+
isWrapperFor,
|
|
45
|
+
PAGE_BREAK_ATTR,
|
|
46
|
+
renderBlock,
|
|
47
|
+
} from "./render.js";
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* A container around blocks (a list, a list item) as rendered: its node, the position before it
|
|
51
|
+
* (which identifies it within one render), its index in its parent and its first block's index.
|
|
52
|
+
*/
|
|
53
|
+
interface Container {
|
|
54
|
+
readonly node: ModelNode;
|
|
55
|
+
readonly pos: number;
|
|
56
|
+
readonly index: number;
|
|
57
|
+
readonly firstBlock: number;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Class of the element of a node-selected block (a rule). */
|
|
61
|
+
const SELECTED_CLASS = "foli-selected-node";
|
|
36
62
|
|
|
37
63
|
interface DomPoint {
|
|
38
64
|
node: Node;
|
|
@@ -100,13 +126,23 @@ function printStyles({ pageSize }: DocumentSettings, id: string): string {
|
|
|
100
126
|
* view cannot prevent (Android keyboards, extensions). A MutationObserver records those; the view
|
|
101
127
|
* then reads the changed text back into a transaction (`readBack`) and re-renders.
|
|
102
128
|
*
|
|
103
|
-
*
|
|
129
|
+
* Blocks are the leaf blocks of the document at any depth (paragraphs, headings, rules); the
|
|
130
|
+
* containers around them (lists, list items) are rebuilt on each page around the blocks it holds
|
|
131
|
+
* (ADR-0004).
|
|
104
132
|
*/
|
|
105
133
|
export class EditorView {
|
|
106
134
|
private state: EditorState;
|
|
107
135
|
private blocks: BlockDesc[] = [];
|
|
108
136
|
/** Position before each block, parallel to `blocks`. */
|
|
109
137
|
private starts: number[] = [];
|
|
138
|
+
/** The containers around each block, outermost first, parallel to `blocks`. */
|
|
139
|
+
private chains: (readonly Container[])[] = [];
|
|
140
|
+
/** The index of each block in `blocks`. */
|
|
141
|
+
private indexOf = new Map<BlockDesc, number>();
|
|
142
|
+
/** The container types around each block at its last render (a change means re-measuring). */
|
|
143
|
+
private chainKeys = new WeakMap<ModelNode, string>();
|
|
144
|
+
/** The element that shows a node selection, if any. */
|
|
145
|
+
private selectedDom: HTMLElement | null = null;
|
|
110
146
|
/** The page of each fragment of each block. */
|
|
111
147
|
private fragmentPages = new Map<BlockDesc, readonly number[]>();
|
|
112
148
|
/** The block each fragment element belongs to. */
|
|
@@ -194,6 +230,8 @@ export class EditorView {
|
|
|
194
230
|
host.addEventListener("copy", this.onCopy);
|
|
195
231
|
host.addEventListener("cut", this.onCut);
|
|
196
232
|
host.addEventListener("paste", this.onPaste);
|
|
233
|
+
host.addEventListener("click", this.onClick);
|
|
234
|
+
host.addEventListener("auxclick", this.onClick);
|
|
197
235
|
host.ownerDocument.addEventListener("selectionchange", this.onSelectionChange);
|
|
198
236
|
host.ownerDocument.fonts?.addEventListener("loadingdone", this.onFontsLoaded);
|
|
199
237
|
this.unsubscribe = editor.subscribe((state) => this.update(state));
|
|
@@ -218,6 +256,8 @@ export class EditorView {
|
|
|
218
256
|
host.removeEventListener("copy", this.onCopy);
|
|
219
257
|
host.removeEventListener("cut", this.onCut);
|
|
220
258
|
host.removeEventListener("paste", this.onPaste);
|
|
259
|
+
host.removeEventListener("click", this.onClick);
|
|
260
|
+
host.removeEventListener("auxclick", this.onClick);
|
|
221
261
|
host.ownerDocument.removeEventListener("selectionchange", this.onSelectionChange);
|
|
222
262
|
const owned = [
|
|
223
263
|
"contenteditable",
|
|
@@ -287,6 +327,7 @@ export class EditorView {
|
|
|
287
327
|
const selectionChanged = !state.selection.eq(this.state.selection);
|
|
288
328
|
this.state = state;
|
|
289
329
|
if (docChanged) this.render();
|
|
330
|
+
this.showSelectedNode();
|
|
290
331
|
this.writeSelection(docChanged);
|
|
291
332
|
// shortcut: scrolls for every change while focused; once remote or plugin changes exist, scroll only for local input (a transaction flag).
|
|
292
333
|
if (docChanged || selectionChanged) this.scrollToSelection();
|
|
@@ -341,7 +382,36 @@ export class EditorView {
|
|
|
341
382
|
}
|
|
342
383
|
let blocks: BlockDesc[] = [];
|
|
343
384
|
const starts: number[] = [];
|
|
344
|
-
|
|
385
|
+
const chains: (readonly Container[])[] = [];
|
|
386
|
+
// Leaf blocks at any depth, in document order, with the containers around each.
|
|
387
|
+
const open: Container[] = [];
|
|
388
|
+
// The chain of the open containers and its key, rebuilt only when they change (blocks in
|
|
389
|
+
// the same container share one array).
|
|
390
|
+
let chain: readonly Container[] = [];
|
|
391
|
+
let chainKey = "";
|
|
392
|
+
const changed = () => {
|
|
393
|
+
chain = open.slice();
|
|
394
|
+
chainKey = open.map((container) => container.node.type.name).join(">");
|
|
395
|
+
};
|
|
396
|
+
doc.descendants((node, pos, _parent, index) => {
|
|
397
|
+
let popped = false;
|
|
398
|
+
while (open.length) {
|
|
399
|
+
const last = open[open.length - 1] as Container;
|
|
400
|
+
if (pos < last.pos + last.node.nodeSize) break;
|
|
401
|
+
open.pop();
|
|
402
|
+
popped = true;
|
|
403
|
+
}
|
|
404
|
+
if (!node.isTextblock && !node.isLeaf) {
|
|
405
|
+
open.push({ node, pos, index, firstBlock: blocks.length });
|
|
406
|
+
changed();
|
|
407
|
+
return true;
|
|
408
|
+
}
|
|
409
|
+
if (popped) changed();
|
|
410
|
+
// A block moved into or out of a container (wrapped in a list, indented, lifted) keeps its
|
|
411
|
+
// node but not its line width or margins: measure it again, and re-flow from it.
|
|
412
|
+
const before = this.chainKeys.get(node);
|
|
413
|
+
if (before !== undefined && before !== chainKey) this.stale.add(node);
|
|
414
|
+
this.chainKeys.set(node, chainKey);
|
|
345
415
|
let reused = previous.get(node);
|
|
346
416
|
// A split block about to be measured again (fonts, CSS) is rendered whole first: each
|
|
347
417
|
// fragment's end breaks its line, so its lines would depend on where it was split before.
|
|
@@ -349,7 +419,9 @@ export class EditorView {
|
|
|
349
419
|
if (reused && reused.fragments.length > 1 && remeasure && !this.composing) reused = undefined;
|
|
350
420
|
if (reused) previous.delete(node);
|
|
351
421
|
blocks.push(reused ?? renderBlock(node, ownerDocument));
|
|
352
|
-
starts.push(
|
|
422
|
+
starts.push(pos);
|
|
423
|
+
chains.push(chain);
|
|
424
|
+
return false;
|
|
353
425
|
});
|
|
354
426
|
for (const stale of previous.values())
|
|
355
427
|
for (const fragment of stale.fragments) fragment.remove();
|
|
@@ -359,7 +431,7 @@ export class EditorView {
|
|
|
359
431
|
const before = previousPages.get(desc);
|
|
360
432
|
return desc.fragments.map((_, f) => (last = Math.max(last, before?.[f] ?? last)));
|
|
361
433
|
});
|
|
362
|
-
this.place(blocks, pages);
|
|
434
|
+
this.place(blocks, pages, chains);
|
|
363
435
|
if (!this.composing) {
|
|
364
436
|
// DOM reads only for blocks without a valid measurement (new, changed or stale ones).
|
|
365
437
|
let scale: number | undefined;
|
|
@@ -406,10 +478,12 @@ export class EditorView {
|
|
|
406
478
|
return same ? desc : renderBlock(desc.node, ownerDocument, splits);
|
|
407
479
|
});
|
|
408
480
|
pages = flowed.map((fragments) => fragments.map((fragment) => fragment.page));
|
|
409
|
-
this.place(blocks, pages);
|
|
481
|
+
this.place(blocks, pages, chains);
|
|
410
482
|
}
|
|
411
483
|
this.blocks = blocks;
|
|
412
484
|
this.starts = starts;
|
|
485
|
+
this.chains = chains;
|
|
486
|
+
this.indexOf = new Map(blocks.map((desc, i) => [desc, i]));
|
|
413
487
|
this.fragmentPages = new Map(blocks.map((desc, i) => [desc, pages[i] ?? []]));
|
|
414
488
|
this.owner = new Map(
|
|
415
489
|
blocks.flatMap((desc) => desc.fragments.map((fragment) => [fragment, desc] as const)),
|
|
@@ -417,6 +491,7 @@ export class EditorView {
|
|
|
417
491
|
this.unflowed = this.composing;
|
|
418
492
|
this.observeSizes();
|
|
419
493
|
this.showPlaceholder();
|
|
494
|
+
this.showSelectedNode();
|
|
420
495
|
// The view's own DOM changes are not input. During a composition, keep what the browser
|
|
421
496
|
// changed before this render: the touched blocks are read back at compositionend.
|
|
422
497
|
const records = this.observer.takeRecords();
|
|
@@ -425,13 +500,19 @@ export class EditorView {
|
|
|
425
500
|
|
|
426
501
|
/**
|
|
427
502
|
* Puts each fragment of each block into its page (pages are non-decreasing in document
|
|
428
|
-
* order),
|
|
429
|
-
*
|
|
503
|
+
* order), inside the elements of the block's containers (lists, list items), creating and
|
|
504
|
+
* removing pages as needed. A page reuses the container elements already at the same place
|
|
505
|
+
* (same tag, in order), so an edit inside a list moves no DOM. Anything else in the host, a
|
|
506
|
+
* page or a container (stray DOM) is removed.
|
|
430
507
|
*/
|
|
431
|
-
private place(
|
|
508
|
+
private place(
|
|
509
|
+
blocks: readonly BlockDesc[],
|
|
510
|
+
pages: readonly (readonly number[])[],
|
|
511
|
+
chains: readonly (readonly Container[])[],
|
|
512
|
+
): void {
|
|
432
513
|
const { host } = this;
|
|
433
514
|
const items = blocks.flatMap((desc, i) =>
|
|
434
|
-
desc.fragments.map((dom, f) => ({ dom, page: pages[i]?.[f] ?? 0 })),
|
|
515
|
+
desc.fragments.map((dom, f) => ({ dom, page: pages[i]?.[f] ?? 0, block: i, fragment: f })),
|
|
435
516
|
);
|
|
436
517
|
const count = (items[items.length - 1]?.page ?? 0) + 1;
|
|
437
518
|
while (this.pages.length < count) this.pages.push(this.createPage());
|
|
@@ -441,25 +522,89 @@ export class EditorView {
|
|
|
441
522
|
if (page === cursor) cursor = cursor.nextSibling;
|
|
442
523
|
else host.insertBefore(page, cursor);
|
|
443
524
|
}
|
|
444
|
-
const
|
|
525
|
+
const ownerDocument = host.ownerDocument;
|
|
526
|
+
/** Removes what is left after the last placed child (strays, and blocks of later pages). */
|
|
527
|
+
const closeLevel = (level: { at: ChildNode | null }) => {
|
|
528
|
+
let at = level.at;
|
|
529
|
+
while (at) {
|
|
530
|
+
const next: ChildNode | null = at.nextSibling;
|
|
531
|
+
at.remove();
|
|
532
|
+
at = next;
|
|
533
|
+
}
|
|
534
|
+
};
|
|
535
|
+
// Block fragments not placed yet, and their blocks. A container element whose first block is
|
|
536
|
+
// still to be placed, in another container at that level, belongs to that later container: a
|
|
537
|
+
// block before it gets a new element instead of taking it (which would move every later block
|
|
538
|
+
// into the next element along, e.g. when one list item moves to the next page).
|
|
539
|
+
const pending = new Map<Node, number>(items.map((item) => [item.dom, item.block]));
|
|
540
|
+
/** Container elements placed in this pass (never taken twice). */
|
|
541
|
+
const claimed = new Set<Node>();
|
|
445
542
|
let i = 0;
|
|
446
543
|
for (let p = 0; p < count; p++) {
|
|
447
544
|
const page = this.pages[p] as HTMLElement;
|
|
448
|
-
|
|
545
|
+
const stack: { el: HTMLElement; at: ChildNode | null; pos: number }[] = [
|
|
546
|
+
{ el: page, at: page.firstChild, pos: -1 },
|
|
547
|
+
];
|
|
449
548
|
for (; i < items.length && items[i]?.page === p; i++) {
|
|
450
|
-
const
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
549
|
+
const item = items[i] as (typeof items)[number];
|
|
550
|
+
const chain = chains[item.block] ?? [];
|
|
551
|
+
// Keep the open containers this block shares with the previous one on this page.
|
|
552
|
+
let common = 0;
|
|
553
|
+
while (
|
|
554
|
+
common < chain.length &&
|
|
555
|
+
common + 1 < stack.length &&
|
|
556
|
+
stack[common + 1]?.pos === (chain[common] as Container).pos
|
|
557
|
+
)
|
|
558
|
+
common++;
|
|
559
|
+
while (stack.length > common + 1) closeLevel(stack.pop() as (typeof stack)[number]);
|
|
560
|
+
for (let k = common; k < chain.length; k++) {
|
|
561
|
+
const container = chain[k] as Container;
|
|
562
|
+
const parent = stack[stack.length - 1] as (typeof stack)[number];
|
|
563
|
+
let element: HTMLElement;
|
|
564
|
+
const candidate = parent.at;
|
|
565
|
+
const later = isWrapperFor(candidate, container.node)
|
|
566
|
+
? pending.get(firstBlockIn(candidate) as Node)
|
|
567
|
+
: undefined;
|
|
568
|
+
// The element that holds this block now, at the innermost level: a list item whose
|
|
569
|
+
// block moves to another page takes its element along (as a paragraph does), instead
|
|
570
|
+
// of a new one replacing it on every page an edit shifts.
|
|
571
|
+
const own = k === chain.length - 1 ? item.dom.parentNode : null;
|
|
572
|
+
if (
|
|
573
|
+
isWrapperFor(candidate, container.node) &&
|
|
574
|
+
!claimed.has(candidate) &&
|
|
575
|
+
(later === undefined || chains[later]?.[k]?.pos === container.pos)
|
|
576
|
+
) {
|
|
577
|
+
element = candidate;
|
|
578
|
+
parent.at = element.nextSibling;
|
|
579
|
+
} else if (
|
|
580
|
+
isWrapperFor(own, container.node) &&
|
|
581
|
+
!claimed.has(own) &&
|
|
582
|
+
!own.contains(parent.el)
|
|
583
|
+
) {
|
|
584
|
+
element = own;
|
|
585
|
+
for (const level of stack) if (level.at === element) level.at = element.nextSibling;
|
|
586
|
+
parent.el.insertBefore(element, parent.at);
|
|
587
|
+
} else {
|
|
588
|
+
element = createWrapper(container.node, ownerDocument);
|
|
589
|
+
parent.el.insertBefore(element, parent.at);
|
|
590
|
+
}
|
|
591
|
+
claimed.add(element);
|
|
592
|
+
styleContainer(element, container, chain[k - 1], item);
|
|
593
|
+
stack.push({ el: element, at: element.firstChild, pos: container.pos });
|
|
594
|
+
}
|
|
595
|
+
const top = stack[stack.length - 1] as (typeof stack)[number];
|
|
596
|
+
if (item.dom === top.at) top.at = top.at.nextSibling;
|
|
597
|
+
else {
|
|
598
|
+
// A cursor of an outer level may point at this block (it moves into a new container):
|
|
599
|
+
// step it past, or it would point into the container.
|
|
600
|
+
for (const level of stack) if (level.at === item.dom) level.at = item.dom.nextSibling;
|
|
601
|
+
top.el.insertBefore(item.dom, top.at);
|
|
602
|
+
}
|
|
603
|
+
pending.delete(item.dom);
|
|
459
604
|
}
|
|
605
|
+
while (stack.length) closeLevel(stack.pop() as (typeof stack)[number]);
|
|
460
606
|
}
|
|
461
|
-
// Pages no longer needed and stray DOM in the host: all after the last page by now.
|
|
462
|
-
// after the block loop, since a block found loose in the host has just moved into a page.
|
|
607
|
+
// Pages no longer needed and stray DOM in the host: all after the last page by now.
|
|
463
608
|
let rest = (this.pages[count - 1] as HTMLElement).nextSibling;
|
|
464
609
|
while (rest) {
|
|
465
610
|
const next: ChildNode | null = rest.nextSibling;
|
|
@@ -579,20 +724,35 @@ export class EditorView {
|
|
|
579
724
|
|
|
580
725
|
// ---------- position mapping ----------
|
|
581
726
|
|
|
582
|
-
/** The block element
|
|
727
|
+
/** The block element (a fragment of a rendered block) that contains `node`, or null. */
|
|
583
728
|
private blockElement(node: Node | null): Node | null {
|
|
584
|
-
let element = node;
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
729
|
+
for (let element = node; element && element !== this.host; element = element.parentNode) {
|
|
730
|
+
if (this.owner.has(element)) return element;
|
|
731
|
+
}
|
|
732
|
+
return null;
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
/** Whether `node` holds blocks rather than content: the host, a page or a container. */
|
|
736
|
+
private holdsBlocks(node: Node): boolean {
|
|
737
|
+
return node === this.host || this.pages.includes(node as HTMLElement) || isWrapper(node);
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
/** Index of the first block that starts at or after `pos` (the block count if none). */
|
|
741
|
+
private blockFrom(pos: number): number {
|
|
742
|
+
let lo = 0;
|
|
743
|
+
let hi = this.starts.length;
|
|
744
|
+
while (lo < hi) {
|
|
745
|
+
const mid = (lo + hi) >> 1;
|
|
746
|
+
if ((this.starts[mid] as number) < pos) lo = mid + 1;
|
|
747
|
+
else hi = mid;
|
|
588
748
|
}
|
|
589
|
-
return
|
|
749
|
+
return lo;
|
|
590
750
|
}
|
|
591
751
|
|
|
592
752
|
/** Model position of a DOM point inside the host, or null if it is outside. */
|
|
593
753
|
posFromDOM(node: Node, offset: number): number | null {
|
|
594
754
|
const { host } = this;
|
|
595
|
-
if (
|
|
755
|
+
if (this.holdsBlocks(node)) {
|
|
596
756
|
// Between fragments: the start of the first one after the point (a block start, or
|
|
597
757
|
// where a continuation starts), else the document end.
|
|
598
758
|
const range = host.ownerDocument.createRange();
|
|
@@ -606,9 +766,14 @@ export class EditorView {
|
|
|
606
766
|
}
|
|
607
767
|
const element = this.blockElement(node);
|
|
608
768
|
const owner = element ? this.owner.get(element) : undefined;
|
|
609
|
-
const index = owner ? this.
|
|
769
|
+
const index = owner ? (this.indexOf.get(owner) ?? -1) : -1;
|
|
610
770
|
const desc = this.blocks[index];
|
|
611
771
|
if (!desc) return null;
|
|
772
|
+
// A point in or at a block leaf (a rule): before it, or after it at its end.
|
|
773
|
+
if (!desc.node.isTextblock) {
|
|
774
|
+
const after = node === desc.dom && offset > 0;
|
|
775
|
+
return (this.starts[index] as number) + (after ? desc.node.nodeSize : 0);
|
|
776
|
+
}
|
|
612
777
|
const contentStart = (this.starts[index] as number) + 1;
|
|
613
778
|
const leaf = desc.leaves.find((l) => l.dom === node);
|
|
614
779
|
if (leaf && node.nodeType === Node.TEXT_NODE)
|
|
@@ -625,10 +790,19 @@ export class EditorView {
|
|
|
625
790
|
/** DOM point for a model position. At a mark boundary the caret stays in the text before it. */
|
|
626
791
|
domFromPos(pos: number): DomPoint {
|
|
627
792
|
const { doc } = this.state;
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
793
|
+
const $pos = doc.resolve(pos);
|
|
794
|
+
if (!$pos.parent.isTextblock) {
|
|
795
|
+
// Between blocks: the start of the next block (before a rule), else the document end.
|
|
796
|
+
const next = this.blocks[this.blockFrom(pos)];
|
|
797
|
+
if (next) return next.node.isTextblock ? fragmentStart(next, 0) : before(next.dom);
|
|
798
|
+
const last = this.blocks[this.blocks.length - 1] as BlockDesc;
|
|
799
|
+
if (!last.node.isTextblock) {
|
|
800
|
+
const point = before(last.dom);
|
|
801
|
+
return { node: point.node, offset: point.offset + 1 };
|
|
802
|
+
}
|
|
803
|
+
return fragmentEnd(last, last.fragments.length - 1);
|
|
804
|
+
}
|
|
805
|
+
const desc = this.blocks[this.blockFrom($pos.before())] as BlockDesc;
|
|
632
806
|
const offset = $pos.parentOffset;
|
|
633
807
|
// Where a fragment starts, show the caret at the start of the next page's fragment, as a
|
|
634
808
|
// browser shows it at the start of the next line at a soft wrap.
|
|
@@ -658,13 +832,50 @@ export class EditorView {
|
|
|
658
832
|
private readSelection(): Selection | null {
|
|
659
833
|
const sel = this.host.ownerDocument.getSelection();
|
|
660
834
|
if (!sel?.anchorNode || !sel.focusNode) return null;
|
|
835
|
+
// A range around exactly one block leaf element (a rule): the node selection that wrote it.
|
|
836
|
+
// (Its end, mapped alone, would be the start of the next block, not the end of the rule.)
|
|
837
|
+
if (sel.anchorNode === sel.focusNode && Math.abs(sel.anchorOffset - sel.focusOffset) === 1) {
|
|
838
|
+
const child = sel.anchorNode.childNodes[Math.min(sel.anchorOffset, sel.focusOffset)];
|
|
839
|
+
const owner = child && this.owner.get(child);
|
|
840
|
+
const index = owner ? (this.indexOf.get(owner) ?? -1) : -1;
|
|
841
|
+
if (owner && !owner.node.isTextblock && NodeSelection.isSelectable(owner.node))
|
|
842
|
+
return NodeSelection.create(this.state.doc, this.starts[index] as number);
|
|
843
|
+
}
|
|
661
844
|
const anchor = this.posFromDOM(sel.anchorNode, sel.anchorOffset);
|
|
662
845
|
const head = this.posFromDOM(sel.focusNode, sel.focusOffset);
|
|
663
846
|
if (anchor === null || head === null) return null;
|
|
664
847
|
const { doc } = this.state;
|
|
848
|
+
// Exactly around a selectable block leaf (a rule): the node selection that wrote it.
|
|
849
|
+
const from = Math.min(anchor, head);
|
|
850
|
+
const node = doc.nodeAt(from);
|
|
851
|
+
if (
|
|
852
|
+
node &&
|
|
853
|
+
node.isBlock &&
|
|
854
|
+
node.isAtom &&
|
|
855
|
+
Math.max(anchor, head) === from + node.nodeSize &&
|
|
856
|
+
NodeSelection.isSelectable(node)
|
|
857
|
+
)
|
|
858
|
+
return NodeSelection.create(doc, from);
|
|
665
859
|
return TextSelection.between(doc.resolve(anchor), doc.resolve(head));
|
|
666
860
|
}
|
|
667
861
|
|
|
862
|
+
/** The rendered block a node selection selects, if it selects one. */
|
|
863
|
+
private selectedBlock(): BlockDesc | null {
|
|
864
|
+
const { selection } = this.state;
|
|
865
|
+
if (!(selection instanceof NodeSelection)) return null;
|
|
866
|
+
const desc = this.blocks[this.blockFrom(selection.from)];
|
|
867
|
+
return desc && this.starts[this.indexOf.get(desc) ?? -1] === selection.from ? desc : null;
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
/** Marks the element of a node-selected block, for styling (the DOM selection is around it). */
|
|
871
|
+
private showSelectedNode(): void {
|
|
872
|
+
const dom = this.selectedBlock()?.dom ?? null;
|
|
873
|
+
if (dom === this.selectedDom) return;
|
|
874
|
+
this.selectedDom?.classList.remove(SELECTED_CLASS);
|
|
875
|
+
dom?.classList.add(SELECTED_CLASS);
|
|
876
|
+
this.selectedDom = dom;
|
|
877
|
+
}
|
|
878
|
+
|
|
668
879
|
/**
|
|
669
880
|
* Brings the model selection up to date with the DOM. `selectionchange` is asynchronous, so
|
|
670
881
|
* every input handler calls this first instead of trusting the last event.
|
|
@@ -704,6 +915,12 @@ export class EditorView {
|
|
|
704
915
|
const current = this.readSelection();
|
|
705
916
|
if (current && this.shows(current)) return;
|
|
706
917
|
}
|
|
918
|
+
const selected = this.selectedBlock();
|
|
919
|
+
if (selected) {
|
|
920
|
+
const point = before(selected.dom);
|
|
921
|
+
sel.setBaseAndExtent(point.node, point.offset, point.node, point.offset + 1);
|
|
922
|
+
return;
|
|
923
|
+
}
|
|
707
924
|
// Never show a caret inside a grapheme cluster (the core snaps only out of surrogate pairs).
|
|
708
925
|
// The model takes the snapped selection back on its next read of the DOM.
|
|
709
926
|
const from = clusterBoundary(doc, selection.from, false);
|
|
@@ -761,7 +978,8 @@ export class EditorView {
|
|
|
761
978
|
this.run(insertText(text));
|
|
762
979
|
return;
|
|
763
980
|
case "insertParagraph":
|
|
764
|
-
|
|
981
|
+
// The same as Enter (a list item splits or ends), for keyboards that send no keydown.
|
|
982
|
+
if (!this.runKey("Enter")) this.run(splitBlock);
|
|
765
983
|
return;
|
|
766
984
|
case "insertLineBreak":
|
|
767
985
|
this.run(insertHardBreak);
|
|
@@ -802,9 +1020,20 @@ export class EditorView {
|
|
|
802
1020
|
return;
|
|
803
1021
|
}
|
|
804
1022
|
const unit = type.startsWith("deleteWord") ? "word" : "char";
|
|
1023
|
+
// A one-character delete goes through the key bindings first (Backspace at a list item's
|
|
1024
|
+
// start lifts it), as the key itself would.
|
|
1025
|
+
if (unit === "char" && this.runKey(type.endsWith("Forward") ? "Delete" : "Backspace")) return;
|
|
805
1026
|
this.run(type.endsWith("Forward") ? deleteForward(unit) : deleteBackward(unit));
|
|
806
1027
|
};
|
|
807
1028
|
|
|
1029
|
+
/** Runs the first command bound to `name` that applies, through the view's dispatch. */
|
|
1030
|
+
private runKey(name: string): boolean {
|
|
1031
|
+
return this.state.plugins.some((plugin) => {
|
|
1032
|
+
const command = plugin.spec.keymap?.[name];
|
|
1033
|
+
return command ? this.run(command) : false;
|
|
1034
|
+
});
|
|
1035
|
+
}
|
|
1036
|
+
|
|
808
1037
|
/**
|
|
809
1038
|
* Triple-click selects the clicked block's content. Chromium and WebKit natively extend the
|
|
810
1039
|
* selection to the start of the next block, so typing over it would merge the two blocks.
|
|
@@ -814,24 +1043,54 @@ export class EditorView {
|
|
|
814
1043
|
// Focus from a mousedown happens in its default action, before this timer. A mousedown that
|
|
815
1044
|
// focuses nothing (prevented, middle button) must not skip the next keyboard focus.
|
|
816
1045
|
if (this.pointerFocus) setTimeout(() => (this.pointerFocus = false));
|
|
817
|
-
if (event.
|
|
1046
|
+
if (event.button !== 0) return;
|
|
818
1047
|
const element = this.blockElement(event.target as Node | null);
|
|
819
1048
|
const owner = element ? this.owner.get(element) : undefined;
|
|
820
|
-
const index = owner ? this.
|
|
1049
|
+
const index = owner ? (this.indexOf.get(owner) ?? -1) : -1;
|
|
821
1050
|
const desc = this.blocks[index];
|
|
822
1051
|
if (!desc) return;
|
|
823
|
-
event.preventDefault();
|
|
824
|
-
const from = (this.starts[index] as number) + 1;
|
|
825
1052
|
const { doc } = this.state;
|
|
1053
|
+
const start = this.starts[index] as number;
|
|
1054
|
+
let selection: Selection;
|
|
1055
|
+
if (!desc.node.isTextblock) {
|
|
1056
|
+
// A click on a rule selects it (the browser would put the caret beside it); Shift+click
|
|
1057
|
+
// extends the selection natively.
|
|
1058
|
+
if (event.shiftKey || !NodeSelection.isSelectable(desc.node)) return;
|
|
1059
|
+
selection = NodeSelection.create(doc, start);
|
|
1060
|
+
} else if (event.detail === 3) {
|
|
1061
|
+
selection = TextSelection.create(doc, start + 1, start + 1 + desc.node.content.size);
|
|
1062
|
+
} else return;
|
|
1063
|
+
event.preventDefault();
|
|
826
1064
|
this.host.focus();
|
|
827
|
-
this.dispatch(
|
|
828
|
-
this.state.tr
|
|
829
|
-
.setSelection(TextSelection.create(doc, from, from + desc.node.content.size))
|
|
830
|
-
.setStoredMarks(null),
|
|
831
|
-
);
|
|
1065
|
+
this.dispatch(this.state.tr.setSelection(selection).setStoredMarks(null));
|
|
832
1066
|
this.writeSelection(true);
|
|
833
1067
|
};
|
|
834
1068
|
|
|
1069
|
+
/**
|
|
1070
|
+
* A click on a link in the document does not navigate (it places the caret, also in read-only
|
|
1071
|
+
* mode); Ctrl+click (Cmd+click on Apple platforms) opens it in a new tab, without `opener`.
|
|
1072
|
+
*/
|
|
1073
|
+
private onClick = (event: MouseEvent): void => {
|
|
1074
|
+
const target = event.target;
|
|
1075
|
+
const element = target instanceof Element ? target.closest("a") : null;
|
|
1076
|
+
if (!element || !this.host.contains(element)) return;
|
|
1077
|
+
event.preventDefault();
|
|
1078
|
+
if (event.type !== "click" || !(IS_MAC ? event.metaKey : event.ctrlKey)) return;
|
|
1079
|
+
const href = this.linkHref(element);
|
|
1080
|
+
if (href) this.host.ownerDocument.defaultView?.open(href, "_blank", "noopener,noreferrer");
|
|
1081
|
+
};
|
|
1082
|
+
|
|
1083
|
+
/** The href of the link an `<a>` shows, read from the model (the DOM is never the truth). */
|
|
1084
|
+
private linkHref(element: Element): string | null {
|
|
1085
|
+
const pos = this.posFromDOM(element, 0);
|
|
1086
|
+
const type = this.state.schema.marks.link;
|
|
1087
|
+
if (pos === null || !type) return null;
|
|
1088
|
+
const $pos = this.state.doc.resolve(pos);
|
|
1089
|
+
const node = $pos.parent.maybeChild($pos.index());
|
|
1090
|
+
const href = node && type.isInSet(node.marks)?.attrs.href;
|
|
1091
|
+
return typeof href === "string" ? href : null;
|
|
1092
|
+
}
|
|
1093
|
+
|
|
835
1094
|
private onKeyDown = (event: KeyboardEvent): void => {
|
|
836
1095
|
if (event.isComposing || this.composing) return;
|
|
837
1096
|
this.syncSelection();
|
|
@@ -843,13 +1102,43 @@ export class EditorView {
|
|
|
843
1102
|
// Ctrl/Cmd+Shift+V: the paste event that follows uses only the plain text.
|
|
844
1103
|
this.plainPaste = name === "Mod-Shift-v";
|
|
845
1104
|
// Like `editor.handleKey`, but through the view's dispatch, so read-only mode drops edits.
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
1105
|
+
// A key no command takes keeps its default (Tab outside a list moves the focus on).
|
|
1106
|
+
if (this.runKey(name)) {
|
|
1107
|
+
event.preventDefault();
|
|
1108
|
+
return;
|
|
1109
|
+
}
|
|
1110
|
+
// Up/Down on a textblock's first/last line next to a rule select it (only the view knows
|
|
1111
|
+
// lines; measured only when a rule is beside the block); else the browser moves the caret.
|
|
1112
|
+
const vertical = name === "ArrowUp" ? -1 : name === "ArrowDown" ? 1 : 0;
|
|
1113
|
+
const across = vertical ? arrowAcrossLeaf(vertical, "line") : null;
|
|
1114
|
+
if (across?.(this.state) && this.caretOnEdgeLine(vertical as -1 | 1) && this.run(across)) {
|
|
1115
|
+
event.preventDefault();
|
|
1116
|
+
}
|
|
851
1117
|
};
|
|
852
1118
|
|
|
1119
|
+
/**
|
|
1120
|
+
* Whether the caret is on the first (`dir` -1) or last (1) line of its textblock, in the
|
|
1121
|
+
* block's first or last fragment (a block split across pages has several).
|
|
1122
|
+
*/
|
|
1123
|
+
private caretOnEdgeLine(dir: -1 | 1): boolean {
|
|
1124
|
+
const sel = this.host.ownerDocument.getSelection();
|
|
1125
|
+
if (!sel?.rangeCount || !sel.isCollapsed || !sel.focusNode) return false;
|
|
1126
|
+
const element = this.blockElement(sel.focusNode);
|
|
1127
|
+
const desc = element ? this.owner.get(element) : undefined;
|
|
1128
|
+
if (!desc?.node.isTextblock) return false;
|
|
1129
|
+
const fragment = desc.fragments[dir < 0 ? 0 : desc.fragments.length - 1];
|
|
1130
|
+
if (!fragment || element !== fragment) return false;
|
|
1131
|
+
if (!desc.node.content.size) return true;
|
|
1132
|
+
const caret = sel.getRangeAt(0).getClientRects()[0];
|
|
1133
|
+
const lines = this.host.ownerDocument.createRange();
|
|
1134
|
+
lines.selectNodeContents(fragment);
|
|
1135
|
+
const rects = lines.getClientRects();
|
|
1136
|
+
const edge = rects[dir < 0 ? 0 : rects.length - 1];
|
|
1137
|
+
// No caret box (some positions next to a line break): leave the move to the browser.
|
|
1138
|
+
if (!caret || !edge) return false;
|
|
1139
|
+
return dir < 0 ? caret.top + 1 < edge.bottom : caret.bottom - 1 > edge.top;
|
|
1140
|
+
}
|
|
1141
|
+
|
|
853
1142
|
/**
|
|
854
1143
|
* Caret affinity at a page break. The end of one fragment and the start of the next are two
|
|
855
1144
|
* DOM caret stops for one model position, so a native Left/Right (or word move) there would
|
|
@@ -873,7 +1162,7 @@ export class EditorView {
|
|
|
873
1162
|
const f = desc.fragments.indexOf(element as HTMLElement);
|
|
874
1163
|
const head = this.posFromDOM(sel.focusNode, sel.focusOffset);
|
|
875
1164
|
if (head === null) return false;
|
|
876
|
-
const offset = head - (this.starts[this.
|
|
1165
|
+
const offset = head - (this.starts[this.indexOf.get(desc) ?? -1] as number) - 1;
|
|
877
1166
|
let target: DomPoint | null = null;
|
|
878
1167
|
// Stepping back over an edge after a hard break lands before the <br>: the point after it
|
|
879
1168
|
// is still the split, and browsers would show it before the <br> anyway. That step is
|
|
@@ -991,10 +1280,15 @@ export class EditorView {
|
|
|
991
1280
|
else node.childNodes.forEach(walk);
|
|
992
1281
|
};
|
|
993
1282
|
let first = true;
|
|
994
|
-
// Block-level nodes in order:
|
|
995
|
-
const blockLevel = [
|
|
996
|
-
|
|
997
|
-
|
|
1283
|
+
// Block-level nodes in order: inside pages and containers, and anything else in the host.
|
|
1284
|
+
const blockLevel: Node[] = [];
|
|
1285
|
+
const collect = (parent: Node) => {
|
|
1286
|
+
for (const child of parent.childNodes) {
|
|
1287
|
+
if (this.holdsBlocks(child)) collect(child);
|
|
1288
|
+
else blockLevel.push(child);
|
|
1289
|
+
}
|
|
1290
|
+
};
|
|
1291
|
+
collect(this.host);
|
|
998
1292
|
let previousOwner: BlockDesc | undefined;
|
|
999
1293
|
for (const child of blockLevel) {
|
|
1000
1294
|
const owner = this.owner.get(child);
|
|
@@ -1010,7 +1304,7 @@ export class EditorView {
|
|
|
1010
1304
|
const { doc } = this.state;
|
|
1011
1305
|
const lineStarts: number[] = [];
|
|
1012
1306
|
let modelText = "";
|
|
1013
|
-
|
|
1307
|
+
this.blocks.forEach(({ node: block }, i) => {
|
|
1014
1308
|
if (i > 0) modelText += BLOCK_SEPARATOR;
|
|
1015
1309
|
lineStarts.push(modelText.length);
|
|
1016
1310
|
modelText += block.textBetween(0, block.content.size, "", HARD_BREAK);
|
|
@@ -1118,6 +1412,7 @@ export class EditorView {
|
|
|
1118
1412
|
const data = event.clipboardData;
|
|
1119
1413
|
this.paste(
|
|
1120
1414
|
plain ? { getData: (type) => (type === "text/plain" ? data.getData(type) : "") } : data,
|
|
1415
|
+
!plain,
|
|
1121
1416
|
);
|
|
1122
1417
|
};
|
|
1123
1418
|
|
|
@@ -1126,31 +1421,66 @@ export class EditorView {
|
|
|
1126
1421
|
* paste starts a new undo step (typing right after it joins that step). Closed content (whole
|
|
1127
1422
|
* blocks) goes into an empty textblock whole, so a pasted heading stays a heading; open
|
|
1128
1423
|
* content, and anything pasted into a non-empty block, joins the text around the selection,
|
|
1129
|
-
* so the target block keeps its type.
|
|
1424
|
+
* so the target block keeps its type. Plain text that is one URL becomes a link (`autolink`,
|
|
1425
|
+
* not for paste as plain text): over selected text inside one block, that text is linked;
|
|
1426
|
+
* otherwise the URL is inserted as linked text. Returns whether anything was inserted.
|
|
1130
1427
|
*/
|
|
1131
|
-
paste(data: Pick<DataTransfer, "getData"
|
|
1428
|
+
paste(data: Pick<DataTransfer, "getData">, autolink = true): boolean {
|
|
1132
1429
|
const { state } = this;
|
|
1430
|
+
const text = autolink && !data.getData(FOLI_MIME) ? data.getData("text/plain") : "";
|
|
1431
|
+
const href = text ? autolinkHref(text) : null;
|
|
1432
|
+
if (href) {
|
|
1433
|
+
const { selection } = state;
|
|
1434
|
+
const inside =
|
|
1435
|
+
!selection.empty &&
|
|
1436
|
+
selection.$from.sameParent(selection.$to) &&
|
|
1437
|
+
selection.$from.parent.inlineContent;
|
|
1438
|
+
let applied = false;
|
|
1439
|
+
const command = inside ? setLink(href) : insertLink(href, text.trim());
|
|
1440
|
+
const dispatch = (tr: Transaction) => {
|
|
1441
|
+
applied = this.dispatch(tr.setMeta(closeHistory, true));
|
|
1442
|
+
};
|
|
1443
|
+
if (command(state, dispatch)) return applied;
|
|
1444
|
+
}
|
|
1133
1445
|
const marks = (state.storedMarks ?? marksAt(state.selection.$from)).map((m) => m.toJSON());
|
|
1134
1446
|
const pasted = docFromClipboard(data, marks);
|
|
1135
|
-
// One empty
|
|
1136
|
-
|
|
1447
|
+
// One empty textblock: nothing to paste (a lone rule is something).
|
|
1448
|
+
const only = pasted?.doc.childCount === 1 ? pasted.doc.firstChild : null;
|
|
1449
|
+
if (!pasted || (only?.isTextblock && !only.content.size)) return false;
|
|
1137
1450
|
const { doc, open } = pasted;
|
|
1138
|
-
|
|
1139
|
-
|
|
1451
|
+
// As a slice (text joins the text around the caret); where the schema refuses that (a
|
|
1452
|
+
// list in a paragraph), as whole blocks fitted at the caret.
|
|
1453
|
+
const asSlice = (tr: Transaction) => {
|
|
1140
1454
|
const $pos = tr.selection.$from;
|
|
1141
1455
|
if (!open && $pos.depth === 1 && $pos.parent.isTextblock && !$pos.parent.content.size) {
|
|
1142
1456
|
const from = $pos.before();
|
|
1143
1457
|
tr.replace(from, $pos.after(), new Slice(doc.content, 0, 0));
|
|
1144
1458
|
tr.setSelection(Selection.near(tr.doc.resolve(from + doc.content.size), -1));
|
|
1145
1459
|
} else {
|
|
1460
|
+
// A slice opens into its first and last blocks: a rule there has no inside.
|
|
1461
|
+
if (doc.firstChild?.isLeaf || doc.lastChild?.isLeaf) {
|
|
1462
|
+
throw new TransformError("A rule at an end is pasted as a block");
|
|
1463
|
+
}
|
|
1146
1464
|
tr.replaceSelection(doc.slice(1, doc.content.size - 1));
|
|
1147
1465
|
}
|
|
1466
|
+
};
|
|
1467
|
+
const asBlocks = (tr: Transaction) => {
|
|
1468
|
+
if (open) throw new TransformError("Open content is pasted as a slice");
|
|
1469
|
+
insertBlocks(tr, doc);
|
|
1470
|
+
};
|
|
1471
|
+
const strategies = [asSlice, asBlocks];
|
|
1472
|
+
for (const strategy of strategies) {
|
|
1473
|
+
const tr = state.tr;
|
|
1474
|
+
try {
|
|
1475
|
+
tr.deleteSelection();
|
|
1476
|
+
strategy(tr);
|
|
1477
|
+
} catch (error) {
|
|
1478
|
+
if (error instanceof TransformError) continue;
|
|
1479
|
+
throw error;
|
|
1480
|
+
}
|
|
1148
1481
|
return this.dispatch(tr.setMeta(closeHistory, true));
|
|
1149
|
-
} catch (error) {
|
|
1150
|
-
// shortcut: content the schema cannot fit at the selection is not pasted; general fitting comes with nested blocks (Phase 7).
|
|
1151
|
-
if (error instanceof TransformError) return false;
|
|
1152
|
-
throw error;
|
|
1153
1482
|
}
|
|
1483
|
+
return false;
|
|
1154
1484
|
}
|
|
1155
1485
|
|
|
1156
1486
|
private onSelectionChange = (): void => {
|
|
@@ -1158,6 +1488,97 @@ export class EditorView {
|
|
|
1158
1488
|
};
|
|
1159
1489
|
}
|
|
1160
1490
|
|
|
1491
|
+
/**
|
|
1492
|
+
* Pastes whole blocks at the caret (after deleting the selection), for content a slice cannot
|
|
1493
|
+
* fit (lists): a single pasted list inside a list item joins that list (its items replace an
|
|
1494
|
+
* empty item, go before or after the item, or between its two halves); other blocks go before
|
|
1495
|
+
* or after the caret's textblock (replacing it when empty) where its parent takes them, else
|
|
1496
|
+
* between its two halves. Throws TransformError where the schema does not allow it.
|
|
1497
|
+
*/
|
|
1498
|
+
function insertBlocks(tr: Transaction, doc: ModelNode): void {
|
|
1499
|
+
const $pos = tr.selection.$from;
|
|
1500
|
+
if (!$pos.parent.isTextblock) throw new TransformError("No textblock at the caret");
|
|
1501
|
+
const atStart = $pos.parentOffset === 0;
|
|
1502
|
+
const atEnd = $pos.parentOffset === $pos.parent.content.size;
|
|
1503
|
+
const only = doc.childCount === 1 ? doc.firstChild : null;
|
|
1504
|
+
const itemType = tr.doc.type.schema.nodes.listItem;
|
|
1505
|
+
const inItem = $pos.depth >= 2 && $pos.node(-1).type === itemType;
|
|
1506
|
+
let at: number;
|
|
1507
|
+
let content = doc.content;
|
|
1508
|
+
if (only && inItem && only.firstChild?.type === itemType) {
|
|
1509
|
+
content = only.content;
|
|
1510
|
+
const depth = $pos.depth - 1;
|
|
1511
|
+
const item = $pos.node(depth);
|
|
1512
|
+
if (atStart && atEnd && item.childCount === 1) {
|
|
1513
|
+
at = $pos.before(depth);
|
|
1514
|
+
tr.delete(at, $pos.after(depth));
|
|
1515
|
+
} else if (atEnd) {
|
|
1516
|
+
// After the item (and its nested list, if it has one): no item left without its text.
|
|
1517
|
+
at = $pos.after(depth);
|
|
1518
|
+
} else if (atStart && $pos.index(depth) === 0) at = $pos.before(depth);
|
|
1519
|
+
else if (atStart) {
|
|
1520
|
+
// At a later block's start: split the item between blocks, not inside the text.
|
|
1521
|
+
const boundary = $pos.before();
|
|
1522
|
+
tr.split(boundary, 1);
|
|
1523
|
+
at = boundary + 1;
|
|
1524
|
+
} else {
|
|
1525
|
+
tr.split($pos.pos, 2);
|
|
1526
|
+
at = $pos.pos + 2;
|
|
1527
|
+
}
|
|
1528
|
+
} else {
|
|
1529
|
+
// Before, after, or in place of the textblock where its parent takes the blocks there (a
|
|
1530
|
+
// list item's first paragraph must stay first); else between its two halves.
|
|
1531
|
+
const parent = $pos.node(-1);
|
|
1532
|
+
const index = $pos.index(-1);
|
|
1533
|
+
if (atStart && atEnd && parent.canReplace(index, index + 1, content)) {
|
|
1534
|
+
at = $pos.before();
|
|
1535
|
+
tr.delete(at, $pos.after());
|
|
1536
|
+
} else if (atStart && parent.canReplace(index, index, content)) at = $pos.before();
|
|
1537
|
+
else if (atEnd && parent.canReplace(index + 1, index + 1, content)) at = $pos.after();
|
|
1538
|
+
else {
|
|
1539
|
+
tr.split($pos.pos);
|
|
1540
|
+
at = $pos.pos + 1;
|
|
1541
|
+
}
|
|
1542
|
+
}
|
|
1543
|
+
tr.insert(at, content);
|
|
1544
|
+
tr.setSelection(Selection.near(tr.doc.resolve(at + content.size), -1));
|
|
1545
|
+
}
|
|
1546
|
+
|
|
1547
|
+
/** The first block element inside a container element (through nested containers), or null. */
|
|
1548
|
+
function firstBlockIn(element: Element): Element | null {
|
|
1549
|
+
let child = element.firstElementChild;
|
|
1550
|
+
while (isWrapper(child)) child = child.firstElementChild;
|
|
1551
|
+
return child;
|
|
1552
|
+
}
|
|
1553
|
+
|
|
1554
|
+
/**
|
|
1555
|
+
* Sets what a container element shows on a page: an ordered list item's number (so numbering
|
|
1556
|
+
* continues across pages), and no marker on an item continued from the previous page (its
|
|
1557
|
+
* first block is on an earlier page, or this is a continued fragment of it).
|
|
1558
|
+
*/
|
|
1559
|
+
function styleContainer(
|
|
1560
|
+
element: HTMLElement,
|
|
1561
|
+
container: Container,
|
|
1562
|
+
parent: Container | undefined,
|
|
1563
|
+
item: { block: number; fragment: number },
|
|
1564
|
+
): void {
|
|
1565
|
+
if (element.localName !== "li") return;
|
|
1566
|
+
const continued = item.block !== container.firstBlock || item.fragment > 0;
|
|
1567
|
+
const start = parent?.node.type.name === "orderedList" ? parent.node.attrs.start : undefined;
|
|
1568
|
+
const value = typeof start === "number" ? String(start + container.index) : null;
|
|
1569
|
+
// Written only when they change (checked against what was written last, without reading the
|
|
1570
|
+
// DOM): this runs for every list item on every render, and every write invalidates style.
|
|
1571
|
+
const state = `${continued}|${value}`;
|
|
1572
|
+
if (containerStates.get(element) === state) return;
|
|
1573
|
+
containerStates.set(element, state);
|
|
1574
|
+
element.style.listStyleType = continued ? "none" : "";
|
|
1575
|
+
if (value === null) element.removeAttribute("value");
|
|
1576
|
+
else element.setAttribute("value", value);
|
|
1577
|
+
}
|
|
1578
|
+
|
|
1579
|
+
/** What `styleContainer` last wrote on each list item element. */
|
|
1580
|
+
const containerStates = new WeakMap<HTMLElement, string>();
|
|
1581
|
+
|
|
1161
1582
|
/**
|
|
1162
1583
|
* Client rects are scaled by CSS `zoom` or a `transform` on an ancestor, while margins and the
|
|
1163
1584
|
* page geometry are in layout px: the ratio between the two for a page. From the page's width
|
|
@@ -1451,9 +1872,10 @@ const IS_MAC = typeof navigator !== "undefined" && /Mac|iP(hone|[oa]d)/.test(nav
|
|
|
1451
1872
|
export function keyName(event: KeyboardEvent): string {
|
|
1452
1873
|
let key = event.key.length === 1 ? event.key.toLowerCase() : event.key;
|
|
1453
1874
|
const mod = IS_MAC ? event.metaKey : event.ctrlKey;
|
|
1454
|
-
// Non-Latin layouts: Ctrl+B still means "b" (use the physical key when the character is not a-z)
|
|
1455
|
-
|
|
1456
|
-
|
|
1875
|
+
// Non-Latin layouts: Ctrl+B still means "b" (use the physical key when the character is not a-z);
|
|
1876
|
+
// with Shift, Ctrl+Shift+7 still means "7" (the character is "&" or "/" depending on layout).
|
|
1877
|
+
if (mod && !/^[a-z0-9]$/.test(key) && /^(Key[A-Z]|Digit[0-9])$/.test(event.code))
|
|
1878
|
+
key = event.code.slice(-1).toLowerCase();
|
|
1457
1879
|
let name = key;
|
|
1458
1880
|
if (event.shiftKey && key !== "Shift") name = `Shift-${name}`;
|
|
1459
1881
|
if (event.altKey) name = `Alt-${name}`;
|