foldkit-richtext 0.3.0 → 0.5.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 +163 -32
- package/dist/index.d.mts +784 -1785
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1284 -106
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -5
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { t as __exportAll } from "./rolldown-runtime-D7D4PA-g.mjs";
|
|
2
|
-
import { Equal, Schema } from "effect";
|
|
2
|
+
import { Equal, Match, Option, Schema } from "effect";
|
|
3
|
+
import { ImageAsset } from "foldkit-media";
|
|
3
4
|
//#region src/document.ts
|
|
4
5
|
/** Caller-assigned identity, unique across blocks and text runs in a document. */
|
|
5
6
|
const NodeId = Schema.NonEmptyString.pipe(Schema.brand("foldkit-richtext/NodeId"));
|
|
@@ -221,6 +222,22 @@ const locateBlock = (document, node) => {
|
|
|
221
222
|
});
|
|
222
223
|
return found;
|
|
223
224
|
};
|
|
225
|
+
/**
|
|
226
|
+
* The blocks either side of a block in its own container, which a handle's up and down move it
|
|
227
|
+
* past; both absent when the document does not hold it.
|
|
228
|
+
*/
|
|
229
|
+
const siblingsOf = (document, node) => {
|
|
230
|
+
const found = locateBlock(document, node);
|
|
231
|
+
if (found === void 0) return {};
|
|
232
|
+
const index = found.path[found.path.length - 1];
|
|
233
|
+
const container = found.path.slice(0, -1);
|
|
234
|
+
const parent = container.length === 0 ? void 0 : blockAtPath(document, container);
|
|
235
|
+
const blocks = parent?.type === "Node" ? parent.blocks ?? [] : document.children;
|
|
236
|
+
return {
|
|
237
|
+
previous: blocks[index - 1],
|
|
238
|
+
next: blocks[index + 1]
|
|
239
|
+
};
|
|
240
|
+
};
|
|
224
241
|
/** The run with this identity, wherever it sits. */
|
|
225
242
|
const locateRun = (document, node) => {
|
|
226
243
|
let found;
|
|
@@ -602,31 +619,75 @@ const sameMarkSet = (left, right) => left.length === right.length && left.every(
|
|
|
602
619
|
* for `apply` to diagnose. Never invents runs: mixed-mark edges stay put.
|
|
603
620
|
*/
|
|
604
621
|
const resolveInsertion = (document, position, registry = shippedRegistry) => {
|
|
605
|
-
|
|
622
|
+
let resolved = position;
|
|
623
|
+
eachBlock(document.children, (block) => {
|
|
606
624
|
const index = block.children.findIndex((run) => run.id === position.node);
|
|
607
625
|
const run = block.children[index];
|
|
608
|
-
if (run === void 0)
|
|
626
|
+
if (run === void 0) return;
|
|
609
627
|
const leavingLeft = position.affinity === "before" && position.offset === 0;
|
|
610
628
|
const leavingRight = position.affinity === "after" && position.offset === run.text.length;
|
|
611
|
-
if (!leavingLeft && !leavingRight) return
|
|
629
|
+
if (!leavingLeft && !leavingRight) return;
|
|
612
630
|
const neighbor = leavingLeft ? block.children[index - 1] : block.children[index + 1];
|
|
613
|
-
if (neighbor === void 0) return
|
|
614
|
-
const
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
return expand === "both" || expand === direction;
|
|
618
|
-
});
|
|
619
|
-
if (!sameMarkSet(desired, neighbor.marks)) return position;
|
|
620
|
-
return {
|
|
631
|
+
if (neighbor === void 0) return;
|
|
632
|
+
const desired = extending(run.marks, leavingLeft ? "before" : "after", registry);
|
|
633
|
+
if (!sameMarkSet(desired, neighbor.marks)) return;
|
|
634
|
+
resolved = {
|
|
621
635
|
node: neighbor.id,
|
|
622
636
|
offset: leavingLeft ? neighbor.text.length : 0,
|
|
623
637
|
affinity: position.affinity
|
|
624
638
|
};
|
|
625
|
-
}
|
|
626
|
-
return
|
|
639
|
+
});
|
|
640
|
+
return resolved;
|
|
641
|
+
};
|
|
642
|
+
/** The marks of a run that extend past its edge in this direction. */
|
|
643
|
+
const extending = (marks, direction, registry) => marks.filter((mark) => {
|
|
644
|
+
const expand = registry.expansionOf(markName(mark));
|
|
645
|
+
return expand === "both" || expand === direction;
|
|
646
|
+
});
|
|
647
|
+
/**
|
|
648
|
+
* The marks text typed at the outer edge of a block should carry, when they differ from its run's:
|
|
649
|
+
* a mark that does not extend past that edge (a link, a footnote reference) stops there. There is
|
|
650
|
+
* no neighbouring run to type into instead, as `resolveInsertion` finds inside a block, so the
|
|
651
|
+
* text needs a run of its own. Undefined anywhere else, and where every mark extends.
|
|
652
|
+
*/
|
|
653
|
+
const edgeMarks = (document, position, registry = shippedRegistry) => {
|
|
654
|
+
let found;
|
|
655
|
+
eachBlock(document.children, (block) => {
|
|
656
|
+
const index = block.children.findIndex((run) => run.id === position.node);
|
|
657
|
+
const run = block.children[index];
|
|
658
|
+
if (run === void 0 || run.text.length === 0) return;
|
|
659
|
+
const atEnd = index === block.children.length - 1 && position.offset === run.text.length;
|
|
660
|
+
const atStart = index === 0 && position.offset === 0;
|
|
661
|
+
if (!atEnd && !atStart) return;
|
|
662
|
+
const desired = extending(run.marks, atEnd ? "after" : "before", registry);
|
|
663
|
+
if (desired.length !== run.marks.length) found = desired;
|
|
664
|
+
});
|
|
665
|
+
return found;
|
|
627
666
|
};
|
|
628
667
|
//#endregion
|
|
629
668
|
//#region src/rendering.ts
|
|
669
|
+
/** The attribute that marks an element as an adornment, which every interpreter skips as content. */
|
|
670
|
+
const ADORNMENT_ATTRIBUTE = "data-adornment";
|
|
671
|
+
/** The attribute naming the prop an adornment's click flips. */
|
|
672
|
+
const TOGGLES_ATTRIBUTE = "data-toggles";
|
|
673
|
+
/** The attribute naming what an adornment's click asks the application for. */
|
|
674
|
+
const ACTION_ATTRIBUTE = "data-action";
|
|
675
|
+
/** An adornment's attributes as every interpreter writes them: its own, then the markers. */
|
|
676
|
+
const adornmentAttributes = (adornment) => [
|
|
677
|
+
...Object.entries(adornment.attributes).filter(([name]) => name !== "data-adornment" && name !== "data-toggles" && name !== "data-action"),
|
|
678
|
+
[ADORNMENT_ATTRIBUTE, ""],
|
|
679
|
+
...adornment.toggles === void 0 ? [] : [[TOGGLES_ATTRIBUTE, adornment.toggles]],
|
|
680
|
+
...adornment.action === void 0 ? [] : [[ACTION_ATTRIBUTE, adornment.action]]
|
|
681
|
+
];
|
|
682
|
+
/** Whether two renderings draw the same element: tag, attributes, holder and adornments. */
|
|
683
|
+
const sameElementRendering = (left, right) => left.tag === right.tag && left.inner === right.inner && sameRecord(left.attributes, right.attributes) && (left.adornments ?? []).length === (right.adornments ?? []).length && (left.adornments ?? []).every((adornment, at) => {
|
|
684
|
+
const other = right.adornments[at];
|
|
685
|
+
return adornment.tag === other.tag && adornment.text === other.text && adornment.toggles === other.toggles && adornment.action === other.action && sameRecord(adornment.attributes, other.attributes);
|
|
686
|
+
});
|
|
687
|
+
const sameRecord = (left, right) => {
|
|
688
|
+
const names = Object.keys(left);
|
|
689
|
+
return names.length === Object.keys(right).length && names.every((name) => Object.hasOwn(right, name) && left[name] === right[name]);
|
|
690
|
+
};
|
|
630
691
|
/** The nesting the shipped marks get, innermost first. */
|
|
631
692
|
const BUILT_IN_MARK_TAGS = [
|
|
632
693
|
["Bold", "strong"],
|
|
@@ -766,7 +827,11 @@ const renderBlock = (block, renderer) => {
|
|
|
766
827
|
tag: blockTag(block),
|
|
767
828
|
attributes: block.type === "Node" ? { "data-node": block.kind } : {}
|
|
768
829
|
};
|
|
769
|
-
|
|
830
|
+
const adornment = (element.adornments ?? []).map((piece) => renderElement({
|
|
831
|
+
tag: piece.tag,
|
|
832
|
+
attributes: Object.fromEntries(adornmentAttributes(piece))
|
|
833
|
+
}, escapeText(piece.text ?? ""))).join("");
|
|
834
|
+
return renderElement(element, element.inner === void 0 ? `${adornment}${runs}${nested}` : `${adornment}${runs}${renderElement({
|
|
770
835
|
tag: element.inner,
|
|
771
836
|
attributes: {}
|
|
772
837
|
}, nested)}`);
|
|
@@ -1040,7 +1105,9 @@ const node = (name, options = {}) => ({
|
|
|
1040
1105
|
props: options.Props,
|
|
1041
1106
|
isolating: options.isolating ?? false,
|
|
1042
1107
|
splitProps: options.splitProps,
|
|
1043
|
-
within: options.within
|
|
1108
|
+
within: options.within,
|
|
1109
|
+
lineBreaks: options.lineBreaks ?? false,
|
|
1110
|
+
indents: options.indents ?? false
|
|
1044
1111
|
});
|
|
1045
1112
|
const kit = (definition) => Object.freeze({
|
|
1046
1113
|
nodes: [...definition.nodes],
|
|
@@ -2275,6 +2342,410 @@ const apply = (state, transaction, transforms = defaultTransforms) => {
|
|
|
2275
2342
|
};
|
|
2276
2343
|
};
|
|
2277
2344
|
//#endregion
|
|
2345
|
+
//#region src/indent.ts
|
|
2346
|
+
/**
|
|
2347
|
+
* Indentation in a kind whose lines are indented, as code's are (`node(…, { indents })`):
|
|
2348
|
+
* Tab indents, Shift+Tab outdents, and Enter starts the new line as indented as the one it
|
|
2349
|
+
* broke. A line is the text between two breaks of one block.
|
|
2350
|
+
*/
|
|
2351
|
+
/** What one level of indentation is: two spaces. */
|
|
2352
|
+
const INDENT = " ";
|
|
2353
|
+
const textOf$1 = (block) => block.children.map((run) => run.text).join("");
|
|
2354
|
+
/** A position's offset into its block's whole text, or undefined when it is not the block's. */
|
|
2355
|
+
const offsetIn = (block, position) => {
|
|
2356
|
+
let consumed = 0;
|
|
2357
|
+
for (const run of block.children) {
|
|
2358
|
+
if (run.id === position.node) return consumed + position.offset;
|
|
2359
|
+
consumed += run.text.length;
|
|
2360
|
+
}
|
|
2361
|
+
};
|
|
2362
|
+
const lineStart = (text, offset) => text.lastIndexOf("\n", offset - 1) + 1;
|
|
2363
|
+
/** The leading spaces and tabs of the line `offset` is on, up to `offset`. */
|
|
2364
|
+
const indentationAt = (text, offset) => /^[ \t]*/.exec(text.slice(lineStart(text, offset), offset))?.[0] ?? "";
|
|
2365
|
+
/**
|
|
2366
|
+
* The starts of the lines a range covers, last first, so editing each leaves the offsets of the
|
|
2367
|
+
* ones before it as they were. A line the range only reaches the start of is not covered.
|
|
2368
|
+
*/
|
|
2369
|
+
const linesCovered = (text, from, to) => {
|
|
2370
|
+
const starts = [lineStart(text, from)];
|
|
2371
|
+
for (let at = text.indexOf("\n", from); at !== -1 && at + 1 < to; at = text.indexOf("\n", at + 1)) starts.push(at + 1);
|
|
2372
|
+
return starts.reverse();
|
|
2373
|
+
};
|
|
2374
|
+
/** The selection's ends as offsets into the block's text, in order, or undefined. */
|
|
2375
|
+
const span = (state, block) => {
|
|
2376
|
+
const selection = state.selection;
|
|
2377
|
+
if (selection?.type !== "Range") return void 0;
|
|
2378
|
+
const anchor = offsetIn(block, selection.anchor);
|
|
2379
|
+
const focus = offsetIn(block, selection.focus);
|
|
2380
|
+
if (anchor === void 0 || focus === void 0) return void 0;
|
|
2381
|
+
return {
|
|
2382
|
+
from: Math.min(anchor, focus),
|
|
2383
|
+
to: Math.max(anchor, focus)
|
|
2384
|
+
};
|
|
2385
|
+
};
|
|
2386
|
+
/**
|
|
2387
|
+
* Tab: at a caret, a level of indentation where the caret is; over a range, a level at the start
|
|
2388
|
+
* of every line it covers, so a block of code moves right together.
|
|
2389
|
+
*/
|
|
2390
|
+
const indent = (state, block) => {
|
|
2391
|
+
const ends = span(state, block);
|
|
2392
|
+
if (ends === void 0) return {
|
|
2393
|
+
ok: false,
|
|
2394
|
+
error: "InvalidSelection"
|
|
2395
|
+
};
|
|
2396
|
+
const text = textOf$1(block);
|
|
2397
|
+
const at = (offset) => positionInBlock(block, offset);
|
|
2398
|
+
if (ends.from === ends.to) {
|
|
2399
|
+
const caret = at(ends.from);
|
|
2400
|
+
if (caret === void 0) return {
|
|
2401
|
+
ok: false,
|
|
2402
|
+
error: "InvalidSelection"
|
|
2403
|
+
};
|
|
2404
|
+
const after = {
|
|
2405
|
+
node: caret.node,
|
|
2406
|
+
offset: caret.offset + 2,
|
|
2407
|
+
affinity: "after"
|
|
2408
|
+
};
|
|
2409
|
+
return apply(state, [Edit.insertText(caret, " "), Edit.setSelection({
|
|
2410
|
+
type: "Range",
|
|
2411
|
+
anchor: after,
|
|
2412
|
+
focus: after
|
|
2413
|
+
})]);
|
|
2414
|
+
}
|
|
2415
|
+
const operations = [];
|
|
2416
|
+
for (const start of linesCovered(text, ends.from, ends.to)) {
|
|
2417
|
+
const position = at(start);
|
|
2418
|
+
if (position !== void 0) operations.push(Edit.insertText(position, " "));
|
|
2419
|
+
}
|
|
2420
|
+
return apply(state, operations);
|
|
2421
|
+
};
|
|
2422
|
+
/** Shift+Tab: up to a level of indentation off the start of every line the selection is on. */
|
|
2423
|
+
const outdent = (state, block) => {
|
|
2424
|
+
const ends = span(state, block);
|
|
2425
|
+
if (ends === void 0) return {
|
|
2426
|
+
ok: false,
|
|
2427
|
+
error: "InvalidSelection"
|
|
2428
|
+
};
|
|
2429
|
+
const text = textOf$1(block);
|
|
2430
|
+
const starts = ends.from === ends.to ? [lineStart(text, ends.from)] : linesCovered(text, ends.from, ends.to);
|
|
2431
|
+
const operations = [];
|
|
2432
|
+
for (const start of starts) {
|
|
2433
|
+
const leading = /^(?: {1,2}|\t)/.exec(text.slice(start))?.[0].length ?? 0;
|
|
2434
|
+
const from = positionInBlock(block, start);
|
|
2435
|
+
if (leading === 0 || from === void 0 || from.offset + leading > runLength(block, from)) continue;
|
|
2436
|
+
operations.push(Edit.deleteText(from.node, from.offset, from.offset + leading));
|
|
2437
|
+
}
|
|
2438
|
+
return apply(state, operations);
|
|
2439
|
+
};
|
|
2440
|
+
const runLength = (block, position) => block.children.find((run) => run.id === position.node)?.text.length ?? 0;
|
|
2441
|
+
//#endregion
|
|
2442
|
+
//#region src/search.ts
|
|
2443
|
+
/**
|
|
2444
|
+
* Search: every occurrence of a query in a document's text, as matches a find bar steps through
|
|
2445
|
+
* and as decorations a renderer draws, without changing anything. The producer lives here rather
|
|
2446
|
+
* than in an application because it is a pure read of a document and nothing else, and the
|
|
2447
|
+
* interpreter that draws the result is the caller's choice — the read-only view renders these as
|
|
2448
|
+
* `span[data-decoration="search"]` (§126), and the editable one takes them when its overlay lands.
|
|
2449
|
+
*
|
|
2450
|
+
* A match never spans two blocks, because what a document has between blocks is a structural
|
|
2451
|
+
* boundary rather than text. It may span two *runs*, though, which is why a block's runs are
|
|
2452
|
+
* read as one string and the endpoints are mapped back to positions.
|
|
2453
|
+
*/
|
|
2454
|
+
/** The kind a search decoration carries, which is what a stylesheet reaches it by. */
|
|
2455
|
+
const SEARCH_DECORATION = "search";
|
|
2456
|
+
/** The kind the match a find bar is on carries instead, so it can be drawn apart. */
|
|
2457
|
+
const SEARCH_CURRENT_DECORATION = "search-current";
|
|
2458
|
+
/**
|
|
2459
|
+
* Text as it is compared: lowercased when case does not count, unless lowercasing would change
|
|
2460
|
+
* its length (a dotted capital I does), which would put every offset after it out of step.
|
|
2461
|
+
*/
|
|
2462
|
+
const comparable = (text, caseSensitive) => {
|
|
2463
|
+
if (caseSensitive) return text;
|
|
2464
|
+
const lower = text.toLowerCase();
|
|
2465
|
+
return lower.length === text.length ? lower : text;
|
|
2466
|
+
};
|
|
2467
|
+
/** Every occurrence of `query`, in document order; none for an empty query. */
|
|
2468
|
+
const findMatches = (document, query, options = {}) => {
|
|
2469
|
+
const found = [];
|
|
2470
|
+
if (query.length === 0) return found;
|
|
2471
|
+
const caseSensitive = options.caseSensitive === true;
|
|
2472
|
+
const needle = comparable(query, caseSensitive);
|
|
2473
|
+
eachBlock(document.children, (block) => {
|
|
2474
|
+
if (block.type === "Unknown" || block.children.length === 0) return;
|
|
2475
|
+
const text = comparable(block.children.map((run) => run.text).join(""), caseSensitive);
|
|
2476
|
+
for (let at = text.indexOf(needle); at >= 0; at = text.indexOf(needle, at + needle.length)) {
|
|
2477
|
+
const from = positionInBlock(block, at);
|
|
2478
|
+
const to = positionInBlock(block, at + needle.length);
|
|
2479
|
+
if (from !== void 0 && to !== void 0) found.push({
|
|
2480
|
+
block: block.id,
|
|
2481
|
+
at,
|
|
2482
|
+
length: needle.length,
|
|
2483
|
+
from,
|
|
2484
|
+
to
|
|
2485
|
+
});
|
|
2486
|
+
}
|
|
2487
|
+
});
|
|
2488
|
+
return found;
|
|
2489
|
+
};
|
|
2490
|
+
/**
|
|
2491
|
+
* Every occurrence of `query` in the document, as decorations a renderer can draw; the one at
|
|
2492
|
+
* index `current`, when given, carries `search-current` instead of `search`.
|
|
2493
|
+
*/
|
|
2494
|
+
const searchDecorations = (document, query, options = {}) => findMatches(document, query, options).map((match, index) => ({
|
|
2495
|
+
from: match.from,
|
|
2496
|
+
to: match.to,
|
|
2497
|
+
kind: index === options.current ? SEARCH_CURRENT_DECORATION : SEARCH_DECORATION
|
|
2498
|
+
}));
|
|
2499
|
+
//#endregion
|
|
2500
|
+
//#region src/footnote.ts
|
|
2501
|
+
/**
|
|
2502
|
+
* Footnotes: a reference in the text and a definition at the end of the document, tied by a
|
|
2503
|
+
* label. The reference is a mark on the run that shows the label, so it moves, splits and undoes
|
|
2504
|
+
* with the text; the definition is a block holding blocks, so its note is ordinary editable text.
|
|
2505
|
+
* Nothing else ties them: a definition no reference names, or a reference with no definition, is
|
|
2506
|
+
* what `footnotesOf` reports, not an invalid document.
|
|
2507
|
+
*/
|
|
2508
|
+
const Label = Schema.Struct({ label: Schema.String });
|
|
2509
|
+
/** Where a footnote is referred to: the run showing its label carries this mark. */
|
|
2510
|
+
const FootnoteRef = mark("FootnoteRef", {
|
|
2511
|
+
Props: Label,
|
|
2512
|
+
expand: "none"
|
|
2513
|
+
});
|
|
2514
|
+
/** A footnote's note, under the label its references carry. */
|
|
2515
|
+
const FootnoteDefinition = node("FootnoteDefinition", {
|
|
2516
|
+
Props: Label,
|
|
2517
|
+
children: blockContent
|
|
2518
|
+
});
|
|
2519
|
+
const labelOf = (props) => {
|
|
2520
|
+
if (typeof props !== "object" || props === null || !("label" in props)) return void 0;
|
|
2521
|
+
return typeof props.label === "string" ? props.label : void 0;
|
|
2522
|
+
};
|
|
2523
|
+
/** The label of the footnote a run refers to, if it carries a reference. */
|
|
2524
|
+
const referenceLabel = (run) => {
|
|
2525
|
+
const reference = run.marks.find((each) => markName(each) === FootnoteRef.name);
|
|
2526
|
+
return reference === void 0 ? void 0 : labelOf(markProps(reference));
|
|
2527
|
+
};
|
|
2528
|
+
const textOf = (blocks) => {
|
|
2529
|
+
const lines = [];
|
|
2530
|
+
eachBlock(blocks, (block) => {
|
|
2531
|
+
if (block.children.length > 0) lines.push(block.children.map((run) => run.text).join(""));
|
|
2532
|
+
});
|
|
2533
|
+
return lines.join("\n");
|
|
2534
|
+
};
|
|
2535
|
+
/**
|
|
2536
|
+
* Every footnote, in the order its first reference appears; a definition nothing refers to comes
|
|
2537
|
+
* after them, in document order.
|
|
2538
|
+
*/
|
|
2539
|
+
const footnotesOf = (document) => {
|
|
2540
|
+
const references = /* @__PURE__ */ new Map();
|
|
2541
|
+
const definitions = /* @__PURE__ */ new Map();
|
|
2542
|
+
eachBlock(document.children, (block) => {
|
|
2543
|
+
if (block.type === "Node" && block.kind === FootnoteDefinition.name) {
|
|
2544
|
+
const label = labelOf(block.props);
|
|
2545
|
+
if (label !== void 0 && !definitions.has(label)) definitions.set(label, {
|
|
2546
|
+
node: block.id,
|
|
2547
|
+
text: textOf(block.blocks ?? [])
|
|
2548
|
+
});
|
|
2549
|
+
}
|
|
2550
|
+
for (const run of block.children) {
|
|
2551
|
+
const label = referenceLabel(run);
|
|
2552
|
+
if (label === void 0) continue;
|
|
2553
|
+
const held = references.get(label);
|
|
2554
|
+
if (held === void 0) references.set(label, [run.id]);
|
|
2555
|
+
else held.push(run.id);
|
|
2556
|
+
}
|
|
2557
|
+
});
|
|
2558
|
+
return [...references.keys(), ...[...definitions.keys()].filter((l) => !references.has(l))].map((label) => ({
|
|
2559
|
+
label,
|
|
2560
|
+
definition: definitions.get(label)?.node,
|
|
2561
|
+
references: references.get(label) ?? [],
|
|
2562
|
+
text: definitions.get(label)?.text ?? ""
|
|
2563
|
+
}));
|
|
2564
|
+
};
|
|
2565
|
+
/**
|
|
2566
|
+
* The label of the footnote whose reference the selection starts in or right beside. A reference
|
|
2567
|
+
* is a character or two, and its mark does not extend, so a click on one usually leaves the caret
|
|
2568
|
+
* at its edge, in the run next to it; that caret counts as at the reference.
|
|
2569
|
+
*/
|
|
2570
|
+
const footnoteAt = (document, selection) => {
|
|
2571
|
+
if (selection?.type !== "Range") return void 0;
|
|
2572
|
+
const start = rangeStart(document, selection);
|
|
2573
|
+
if (start === void 0) return void 0;
|
|
2574
|
+
let found;
|
|
2575
|
+
eachBlock(document.children, (block) => {
|
|
2576
|
+
const index = block.children.findIndex((each) => each.id === start.node);
|
|
2577
|
+
if (index < 0) return;
|
|
2578
|
+
const run = block.children[index];
|
|
2579
|
+
let beside;
|
|
2580
|
+
if (start.offset === 0) beside = block.children[index - 1];
|
|
2581
|
+
else if (start.offset === run.text.length) beside = block.children[index + 1];
|
|
2582
|
+
found = referenceLabel(run) ?? (beside === void 0 ? void 0 : referenceLabel(beside));
|
|
2583
|
+
});
|
|
2584
|
+
return found;
|
|
2585
|
+
};
|
|
2586
|
+
/** The smallest whole number no footnote uses as its label, as text: the next note's label. */
|
|
2587
|
+
const nextFootnoteLabel = (document) => {
|
|
2588
|
+
const used = new Set(footnotesOf(document).map((each) => each.label));
|
|
2589
|
+
let n = 1;
|
|
2590
|
+
while (used.has(String(n))) n++;
|
|
2591
|
+
return String(n);
|
|
2592
|
+
};
|
|
2593
|
+
//#endregion
|
|
2594
|
+
//#region src/table.ts
|
|
2595
|
+
/**
|
|
2596
|
+
* Editing a table as a grid: rows and columns added and removed beside the caret's cell, and the
|
|
2597
|
+
* caret moved from cell to cell, as Tab and Shift+Tab do. A table is the standard vocabulary's
|
|
2598
|
+
* `Table` of `TableRow`s of `TableCell`s, each cell holding blocks; a column is the cells at one
|
|
2599
|
+
* index across the rows, which may be ragged, so a column that a short row lacks is skipped
|
|
2600
|
+
* rather than refused. Each change is one transaction, so it is one undo step.
|
|
2601
|
+
*/
|
|
2602
|
+
const isKind = (block, kind) => block?.type === "Node" && block.kind === kind;
|
|
2603
|
+
/** The cells of a row, which are its nested blocks. */
|
|
2604
|
+
const cellsOf = (row) => row.blocks ?? [];
|
|
2605
|
+
/**
|
|
2606
|
+
* The table cell the selection starts in, the innermost one when tables nest, or undefined
|
|
2607
|
+
* outside a table. A range is read from its start, as the other block commands read it.
|
|
2608
|
+
*/
|
|
2609
|
+
const tableAt = (document, selection) => {
|
|
2610
|
+
if (selection?.type !== "Range") return void 0;
|
|
2611
|
+
const start = rangeStart(document, selection);
|
|
2612
|
+
const run = start === void 0 ? void 0 : locateRun(document, start.node);
|
|
2613
|
+
if (run === void 0) return void 0;
|
|
2614
|
+
for (let depth = run.path.length - 1; depth >= 3; depth--) {
|
|
2615
|
+
const cell = blockAtPath(document, run.path.slice(0, depth));
|
|
2616
|
+
const row = blockAtPath(document, run.path.slice(0, depth - 1));
|
|
2617
|
+
const table = blockAtPath(document, run.path.slice(0, depth - 2));
|
|
2618
|
+
if (isKind(cell, "TableCell") && isKind(row, "TableRow") && isKind(table, "Table")) return {
|
|
2619
|
+
table,
|
|
2620
|
+
rows: (table.blocks ?? []).filter((candidate) => candidate.type === "Node"),
|
|
2621
|
+
row: run.path[depth - 2],
|
|
2622
|
+
column: run.path[depth - 1]
|
|
2623
|
+
};
|
|
2624
|
+
}
|
|
2625
|
+
};
|
|
2626
|
+
/** An empty cell, under placeholder identities a caller freshens. */
|
|
2627
|
+
const emptyCell = () => ({
|
|
2628
|
+
type: "Node",
|
|
2629
|
+
kind: "TableCell",
|
|
2630
|
+
id: NodeId.make("cell"),
|
|
2631
|
+
props: {},
|
|
2632
|
+
children: [],
|
|
2633
|
+
blocks: [{
|
|
2634
|
+
type: "Paragraph",
|
|
2635
|
+
id: NodeId.make("paragraph"),
|
|
2636
|
+
children: [{
|
|
2637
|
+
type: "Text",
|
|
2638
|
+
id: NodeId.make("text"),
|
|
2639
|
+
text: "",
|
|
2640
|
+
marks: []
|
|
2641
|
+
}]
|
|
2642
|
+
}]
|
|
2643
|
+
});
|
|
2644
|
+
const fresh = (block, mint) => withFreshIds({
|
|
2645
|
+
version: 1,
|
|
2646
|
+
blocks: [block]
|
|
2647
|
+
}, mint).blocks[0];
|
|
2648
|
+
const firstRun = (block) => {
|
|
2649
|
+
if (block === void 0) return void 0;
|
|
2650
|
+
let found;
|
|
2651
|
+
eachBlock([block], (inner) => {
|
|
2652
|
+
found ??= inner.children[0];
|
|
2653
|
+
});
|
|
2654
|
+
return found;
|
|
2655
|
+
};
|
|
2656
|
+
/** The selection at the start of a cell's first text, or the selection as it was. */
|
|
2657
|
+
const caretIn = (cell, otherwise) => {
|
|
2658
|
+
const run = firstRun(cell);
|
|
2659
|
+
if (run === void 0) return otherwise;
|
|
2660
|
+
const at = {
|
|
2661
|
+
node: run.id,
|
|
2662
|
+
offset: 0,
|
|
2663
|
+
affinity: "after"
|
|
2664
|
+
};
|
|
2665
|
+
return {
|
|
2666
|
+
type: "Range",
|
|
2667
|
+
anchor: at,
|
|
2668
|
+
focus: at
|
|
2669
|
+
};
|
|
2670
|
+
};
|
|
2671
|
+
/** The widest row's cell count: what a new row is given so the grid stays as wide. */
|
|
2672
|
+
const widthOf = (rows) => Math.max(1, ...rows.map((row) => cellsOf(row).length));
|
|
2673
|
+
/** A row as wide as the table, under fresh identities, and the operation that puts it there. */
|
|
2674
|
+
const newRow = (at, index, mint) => {
|
|
2675
|
+
const row = fresh({
|
|
2676
|
+
type: "Node",
|
|
2677
|
+
kind: "TableRow",
|
|
2678
|
+
id: NodeId.make("row"),
|
|
2679
|
+
props: {},
|
|
2680
|
+
children: [],
|
|
2681
|
+
blocks: Array.from({ length: widthOf(at.rows) }, emptyCell)
|
|
2682
|
+
}, mint);
|
|
2683
|
+
return {
|
|
2684
|
+
row,
|
|
2685
|
+
operation: Edit.insertBlock(row, index, at.table.id)
|
|
2686
|
+
};
|
|
2687
|
+
};
|
|
2688
|
+
/** A row above or below the caret's, as wide as the table; the caret goes to it, same column. */
|
|
2689
|
+
const insertRow = (state, at, where, mint) => {
|
|
2690
|
+
const { row, operation } = newRow(at, at.row + (where === "after" ? 1 : 0), mint);
|
|
2691
|
+
const cells = row.type === "Node" ? row.blocks ?? [] : [];
|
|
2692
|
+
return apply(state, [operation, Edit.setSelection(caretIn(cells[Math.min(at.column, cells.length - 1)], state.selection))]);
|
|
2693
|
+
};
|
|
2694
|
+
/**
|
|
2695
|
+
* A cell beside the caret's in every row, before or after it; a row too short to have that
|
|
2696
|
+
* column gets its cell at its end. The caret goes to the new cell in its own row.
|
|
2697
|
+
*/
|
|
2698
|
+
const insertColumn = (state, at, where, mint) => {
|
|
2699
|
+
const index = at.column + (where === "after" ? 1 : 0);
|
|
2700
|
+
let caretCell;
|
|
2701
|
+
const operations = at.rows.map((row, rowIndex) => {
|
|
2702
|
+
const cell = fresh(emptyCell(), mint);
|
|
2703
|
+
if (rowIndex === at.row) caretCell = cell;
|
|
2704
|
+
return Edit.insertBlock(cell, Math.min(index, cellsOf(row).length), row.id);
|
|
2705
|
+
});
|
|
2706
|
+
return apply(state, [...operations, Edit.setSelection(caretIn(caretCell, state.selection))]);
|
|
2707
|
+
};
|
|
2708
|
+
/** The caret's row, gone; the caret goes to the same column of the row after it, or before. */
|
|
2709
|
+
const deleteRow = (state, at) => {
|
|
2710
|
+
if (at.rows.length === 1) return { deleteTable: at.table.id };
|
|
2711
|
+
const next = at.rows[at.row + 1] ?? at.rows[at.row - 1];
|
|
2712
|
+
const cells = cellsOf(next);
|
|
2713
|
+
return apply(state, [Edit.deleteBlock(at.rows[at.row].id), Edit.setSelection(caretIn(cells[Math.min(at.column, cells.length - 1)], state.selection))]);
|
|
2714
|
+
};
|
|
2715
|
+
/**
|
|
2716
|
+
* The caret's column, gone from every row that has it; a row it leaves empty goes too. The
|
|
2717
|
+
* caret goes to the cell that takes the column's place in its row, or the one before it.
|
|
2718
|
+
*/
|
|
2719
|
+
const deleteColumn = (state, at) => {
|
|
2720
|
+
if (at.rows.every((row) => cellsOf(row).length <= 1)) return { deleteTable: at.table.id };
|
|
2721
|
+
const operations = [];
|
|
2722
|
+
for (const row of at.rows) {
|
|
2723
|
+
const cells = cellsOf(row);
|
|
2724
|
+
const cell = cells[at.column];
|
|
2725
|
+
if (cell === void 0) continue;
|
|
2726
|
+
operations.push(Edit.deleteBlock(cells.length === 1 ? row.id : cell.id));
|
|
2727
|
+
}
|
|
2728
|
+
const own = cellsOf(at.rows[at.row]);
|
|
2729
|
+
const landing = own.length === 1 ? void 0 : own[at.column + 1] ?? own[at.column - 1];
|
|
2730
|
+
return apply(state, [...operations, ...landing === void 0 ? [] : [Edit.setSelection(caretIn(landing, state.selection))]]);
|
|
2731
|
+
};
|
|
2732
|
+
/**
|
|
2733
|
+
* The caret to the start of the next cell, across rows, or the previous one: Tab and Shift+Tab.
|
|
2734
|
+
* Tab in the last cell adds a row and goes to it; Shift+Tab in the first stays where it is.
|
|
2735
|
+
*/
|
|
2736
|
+
const moveToCell = (state, at, direction, mint) => {
|
|
2737
|
+
const order = at.rows.flatMap((row, rowIndex) => cellsOf(row).map((cell, column) => ({
|
|
2738
|
+
cell,
|
|
2739
|
+
rowIndex,
|
|
2740
|
+
column
|
|
2741
|
+
})));
|
|
2742
|
+
const target = order[order.findIndex((entry) => entry.rowIndex === at.row && entry.column === at.column) + (direction === "next" ? 1 : -1)];
|
|
2743
|
+
if (target !== void 0) return apply(state, [Edit.setSelection(caretIn(target.cell, state.selection))]);
|
|
2744
|
+
if (direction === "previous") return apply(state, []);
|
|
2745
|
+
const { row, operation } = newRow(at, at.rows.length, mint);
|
|
2746
|
+
return apply(state, [operation, Edit.setSelection(caretIn(row, state.selection))]);
|
|
2747
|
+
};
|
|
2748
|
+
//#endregion
|
|
2278
2749
|
//#region src/command.ts
|
|
2279
2750
|
/** Where `MoveBlock` puts a block: before or after a sibling, named by identity. */
|
|
2280
2751
|
const Beside = Schema.Union([Schema.Struct({
|
|
@@ -2332,6 +2803,15 @@ const declaresText = (nodes, kind) => {
|
|
|
2332
2803
|
const declared = nodes?.definitionFor(kind);
|
|
2333
2804
|
return declared?.kind === "node" && declared.children === "text";
|
|
2334
2805
|
};
|
|
2806
|
+
/**
|
|
2807
|
+
* Whether a block is an atom, holding neither text nor blocks, as an image is. Without a
|
|
2808
|
+
* vocabulary, a node with nothing in it is one.
|
|
2809
|
+
*/
|
|
2810
|
+
const isAtom = (nodes, block) => {
|
|
2811
|
+
if (block.type !== "Node") return false;
|
|
2812
|
+
const declared = nodes?.definitionFor(block.kind);
|
|
2813
|
+
return declared === void 0 ? block.blocks === void 0 && block.children.length === 0 : declared.kind === "atom";
|
|
2814
|
+
};
|
|
2335
2815
|
/** Whether the vocabulary names `itemKind` among the kinds `listKind` holds, as `List` names `ListItem`. */
|
|
2336
2816
|
const holdsItem = (nodes, listKind, itemKind) => {
|
|
2337
2817
|
const declared = nodes.definitionFor(listKind);
|
|
@@ -2586,6 +3066,117 @@ const blocksAt = (document, selection) => {
|
|
|
2586
3066
|
const path = startPath(document, selection) ?? [];
|
|
2587
3067
|
return path.map((_, depth) => blockAtPath(document, path.slice(0, depth + 1)));
|
|
2588
3068
|
};
|
|
3069
|
+
/** Whether `prefix` is a path to one of the blocks `path` lies inside, or `path` itself. */
|
|
3070
|
+
const startsWith = (path, prefix) => prefix.length <= path.length && prefix.every((index, at) => path[at] === index);
|
|
3071
|
+
/** Document order of two block paths: a container comes before what it holds. */
|
|
3072
|
+
const pathOrder = (left, right) => {
|
|
3073
|
+
for (let at = 0; at < Math.min(left.length, right.length); at++) if (left[at] !== right[at]) return left[at] - right[at];
|
|
3074
|
+
return left.length - right.length;
|
|
3075
|
+
};
|
|
3076
|
+
/** Whether two blocks can be joined into one: what `JoinNode` accepts. */
|
|
3077
|
+
const joinable = (survivor, removed) => {
|
|
3078
|
+
if (survivor.type === "Unknown" || removed.type === "Unknown") return false;
|
|
3079
|
+
if (nestedBlocks(survivor) !== void 0 || nestedBlocks(removed) !== void 0) return false;
|
|
3080
|
+
if (survivor.type !== "Node" && removed.type !== "Node") return true;
|
|
3081
|
+
return survivor.type === "Node" && removed.type === "Node" && survivor.kind === removed.kind && Equal.equals(survivor.props, removed.props);
|
|
3082
|
+
};
|
|
3083
|
+
const nestedBlocks = (block) => block.type === "Node" && block.blocks !== void 0 ? block.blocks : void 0;
|
|
3084
|
+
/** Whether going from one block to another leaves an isolating container, as a table cell is. */
|
|
3085
|
+
const crossesIsolating = (document, from, to, nodes) => [from, to].some((path, side) => {
|
|
3086
|
+
const other = side === 0 ? to : from;
|
|
3087
|
+
for (let depth = 1; depth < path.length; depth++) {
|
|
3088
|
+
const container = path.slice(0, depth);
|
|
3089
|
+
if (startsWith(other, container)) continue;
|
|
3090
|
+
const block = blockAtPath(document, container);
|
|
3091
|
+
const declared = block?.type === "Node" ? nodes?.definitionFor(block.kind) : void 0;
|
|
3092
|
+
if (declared?.kind === "node" && declared.isolating === true) return true;
|
|
3093
|
+
}
|
|
3094
|
+
return false;
|
|
3095
|
+
});
|
|
3096
|
+
/** The innermost first or last block inside the block at `path`: itself when it holds none. */
|
|
3097
|
+
const edgeLeaf = (document, path, last) => {
|
|
3098
|
+
let at = path;
|
|
3099
|
+
for (;;) {
|
|
3100
|
+
const held = nestedBlocks(blockAtPath(document, at));
|
|
3101
|
+
if (held === void 0 || held.length === 0) return at;
|
|
3102
|
+
at = [...at, last ? held.length - 1 : 0];
|
|
3103
|
+
}
|
|
3104
|
+
};
|
|
3105
|
+
/** The first block after the one at `path` that holds no blocks, in document order. */
|
|
3106
|
+
const nextLeaf = (document, path) => {
|
|
3107
|
+
let found;
|
|
3108
|
+
eachBlock(document.children, (block, at) => {
|
|
3109
|
+
if (found !== void 0 || pathOrder(at, path) <= 0 || startsWith(path, at)) return;
|
|
3110
|
+
if ((nestedBlocks(block) ?? []).length === 0) found = at;
|
|
3111
|
+
});
|
|
3112
|
+
return found;
|
|
3113
|
+
};
|
|
3114
|
+
/** One result followed by another, as one: what `runAction` does for two commands. */
|
|
3115
|
+
const followedBy = (first, second) => !first.ok || !second.ok ? second.ok ? first : second : {
|
|
3116
|
+
ok: true,
|
|
3117
|
+
state: second.state,
|
|
3118
|
+
changeSet: unionChangeSet(first.changeSet, second.changeSet),
|
|
3119
|
+
positionMap: [...first.positionMap, ...second.positionMap],
|
|
3120
|
+
transactions: [...first.transactions, ...second.transactions]
|
|
3121
|
+
};
|
|
3122
|
+
/**
|
|
3123
|
+
* Deletes a range that leaves its container, as selecting from one kind of block into another
|
|
3124
|
+
* (or everything) and pressing Delete does: the text after `start` in its block and before
|
|
3125
|
+
* `end` in its block goes, every block wholly between them goes, and the block `end` is in
|
|
3126
|
+
* comes up beside the one `start` is in, joining it when the two can be one. A container it
|
|
3127
|
+
* leaves empty goes too. A range that leaves a table cell (an isolating container) only loses
|
|
3128
|
+
* its text: the cells stay, as a table's structure is not a selection's to remove. The caret
|
|
3129
|
+
* ends at `start`.
|
|
3130
|
+
*/
|
|
3131
|
+
const clearRange = (state, start, end, options) => {
|
|
3132
|
+
const document = state.document;
|
|
3133
|
+
const startAt = locate(document, start.node);
|
|
3134
|
+
const endAt = locate(document, end.node);
|
|
3135
|
+
if (startAt === void 0 || endAt === void 0) return failure("MissingText");
|
|
3136
|
+
const startPath = startAt.path;
|
|
3137
|
+
const endPath = endAt.path;
|
|
3138
|
+
const spans = covered(document, start, end);
|
|
3139
|
+
if (crossesIsolating(document, startPath, endPath, options.nodes)) return apply(state, [...spans.map((span) => Edit.deleteText(span.run.id, span.from, span.to)), Edit.setSelection(caretAt(start))]);
|
|
3140
|
+
const ends = spans.filter((span) => pathKey$1(span.run.path) === pathKey$1(startPath) || pathKey$1(span.run.path) === pathKey$1(endPath));
|
|
3141
|
+
const between = [];
|
|
3142
|
+
eachBlock(document.children, (block, path) => {
|
|
3143
|
+
if (pathOrder(path, startPath) <= 0 || pathOrder(path, endPath) >= 0) return;
|
|
3144
|
+
if (startsWith(endPath, path) || startsWith(startPath, path)) return;
|
|
3145
|
+
if (between.some((outer) => startsWith(path, outer.path))) return;
|
|
3146
|
+
between.push({
|
|
3147
|
+
id: block.id,
|
|
3148
|
+
path
|
|
3149
|
+
});
|
|
3150
|
+
});
|
|
3151
|
+
const first = apply(state, [
|
|
3152
|
+
...ends.map((span) => Edit.deleteText(span.run.id, span.from, span.to)),
|
|
3153
|
+
...between.map((block) => Edit.deleteBlock(block.id)),
|
|
3154
|
+
Edit.setSelection(caretAt(start))
|
|
3155
|
+
]);
|
|
3156
|
+
if (!first.ok) return first;
|
|
3157
|
+
const startBlock = blockAtPath(document, startPath);
|
|
3158
|
+
const endBlock = blockAtPath(document, endPath);
|
|
3159
|
+
const now = first.state.document;
|
|
3160
|
+
const survivorAt = locateBlock(now, startBlock.id);
|
|
3161
|
+
const removedAt = locateBlock(now, endBlock.id);
|
|
3162
|
+
if (survivorAt === void 0 || removedAt === void 0) return first;
|
|
3163
|
+
const operations = [];
|
|
3164
|
+
const parentPath = survivorAt.path.slice(0, -1);
|
|
3165
|
+
const parent = parentPath.length === 0 ? void 0 : blockAtPath(now, parentPath);
|
|
3166
|
+
const index = survivorAt.path[survivorAt.path.length - 1];
|
|
3167
|
+
const siblings = pathKey$1(removedAt.path.slice(0, -1)) === pathKey$1(parentPath);
|
|
3168
|
+
if (!siblings && parentPath.length > 0 && !acceptsChild(now, parentPath, blockKind(removedAt.block), options.nodes)) return first;
|
|
3169
|
+
if (!siblings) operations.push(Edit.moveBlock(endBlock.id, index + 1, parent?.id));
|
|
3170
|
+
if ((siblings ? removedAt.path[removedAt.path.length - 1] === index + 1 : true) && joinable(survivorAt.block, removedAt.block)) operations.push(Edit.joinBlocks(startBlock.id, endBlock.id));
|
|
3171
|
+
if (!siblings) for (let depth = removedAt.path.length - 1; depth > 0; depth--) {
|
|
3172
|
+
const container = blockAtPath(now, removedAt.path.slice(0, depth));
|
|
3173
|
+
if (container === void 0 || startsWith(survivorAt.path, removedAt.path.slice(0, depth))) break;
|
|
3174
|
+
if ((nestedBlocks(container) ?? []).length > 1) break;
|
|
3175
|
+
operations.push(Edit.deleteBlock(container.id));
|
|
3176
|
+
}
|
|
3177
|
+
if (operations.length === 0) return first;
|
|
3178
|
+
return followedBy(first, apply(first.state, [...operations, Edit.setSelection(caretAt(start))]));
|
|
3179
|
+
};
|
|
2589
3180
|
/** The last run in a block's subtree, or undefined when it holds none. */
|
|
2590
3181
|
const lastRunOf = (block) => {
|
|
2591
3182
|
if (block.type === "Node" && block.blocks !== void 0) for (let index = block.blocks.length - 1; index >= 0; index--) {
|
|
@@ -2619,10 +3210,12 @@ const deleteRange = (document, start, end) => {
|
|
|
2619
3210
|
const blocks = containerBlocks(document, containerPath);
|
|
2620
3211
|
const from = startAt.path[startAt.path.length - 1];
|
|
2621
3212
|
const to = endAt.path[endAt.path.length - 1];
|
|
2622
|
-
const survivor = blocks[from]
|
|
3213
|
+
const survivor = blocks[from];
|
|
2623
3214
|
for (let index = from + 1; index <= to; index++) {
|
|
2624
3215
|
const block = blocks[index];
|
|
2625
|
-
if (block
|
|
3216
|
+
if (block === void 0) continue;
|
|
3217
|
+
if (!joinable(survivor, block)) return void 0;
|
|
3218
|
+
deletions.push(Edit.joinBlocks(survivor.id, block.id));
|
|
2626
3219
|
}
|
|
2627
3220
|
return deletions;
|
|
2628
3221
|
};
|
|
@@ -2883,6 +3476,15 @@ const runBlockCommand = (state, command, starting, ids, options) => {
|
|
|
2883
3476
|
*/
|
|
2884
3477
|
const run = (state, command, ids, options = {}) => {
|
|
2885
3478
|
const declared = options.marks ?? shippedRegistry;
|
|
3479
|
+
const range = state.selection;
|
|
3480
|
+
if ((command.type === "InsertText" || command.type === "DeleteBackward" || command.type === "DeleteForward" || command.type === "SplitBlock" || command.type === "Paste") && range?.type === "Range" && !isCollapsed(range)) {
|
|
3481
|
+
const span = ordered(state.document, range);
|
|
3482
|
+
if (span !== void 0 && deleteRange(state.document, span.start, span.end) === void 0) {
|
|
3483
|
+
const cleared = clearRange(state, span.start, span.end, options);
|
|
3484
|
+
if (!cleared.ok || command.type === "DeleteBackward" || command.type === "DeleteForward") return cleared;
|
|
3485
|
+
return followedBy(cleared, run(cleared.state, command, ids, options));
|
|
3486
|
+
}
|
|
3487
|
+
}
|
|
2886
3488
|
if (command.type === "SetSelection") return apply(state, [Edit.setSelection(command.selection)]);
|
|
2887
3489
|
if (command.type === "SetProps") {
|
|
2888
3490
|
const target = locateBlock(state.document, command.node)?.block;
|
|
@@ -2897,6 +3499,109 @@ const run = (state, command, ids, options = {}) => {
|
|
|
2897
3499
|
}) && !refuses(target.props)) return failure("InvalidInput");
|
|
2898
3500
|
return apply(state, [Edit.setProps(command.node, command.props)]);
|
|
2899
3501
|
}
|
|
3502
|
+
if (command.type === "InsertRow" || command.type === "InsertColumn" || command.type === "DeleteRow" || command.type === "DeleteColumn") {
|
|
3503
|
+
const at = tableAt(state.document, state.selection);
|
|
3504
|
+
if (at === void 0) return failure("InvalidSelection");
|
|
3505
|
+
const edited = Match.value(command).pipe(Match.discriminatorsExhaustive("type")({
|
|
3506
|
+
InsertRow: ({ where }) => insertRow(state, at, where, ids.mint),
|
|
3507
|
+
InsertColumn: ({ where }) => insertColumn(state, at, where, ids.mint),
|
|
3508
|
+
DeleteRow: () => deleteRow(state, at),
|
|
3509
|
+
DeleteColumn: () => deleteColumn(state, at)
|
|
3510
|
+
}));
|
|
3511
|
+
return "deleteTable" in edited ? run(state, {
|
|
3512
|
+
type: "DeleteBlock",
|
|
3513
|
+
node: edited.deleteTable
|
|
3514
|
+
}, ids, options) : edited;
|
|
3515
|
+
}
|
|
3516
|
+
if (command.type === "Indent" || command.type === "Outdent") {
|
|
3517
|
+
const code = linesHolder(state, options.nodes);
|
|
3518
|
+
if (code !== void 0) return command.type === "Indent" ? indent(state, code) : outdent(state, code);
|
|
3519
|
+
const item = itemAtCaret(state, options.nodes);
|
|
3520
|
+
const at = tableAt(state.document, state.selection);
|
|
3521
|
+
const tableDepth = at === void 0 ? -1 : locateBlock(state.document, at.table.id)?.path.length ?? -1;
|
|
3522
|
+
if (item !== void 0 && item.containerPath.length > tableDepth) return command.type === "Indent" ? sinkItem(state, item, ids, options) : liftItem(state, item, ids, options);
|
|
3523
|
+
if (at === void 0) return failure("InvalidSelection");
|
|
3524
|
+
return moveToCell(state, at, command.type === "Indent" ? "next" : "previous", ids.mint);
|
|
3525
|
+
}
|
|
3526
|
+
if (command.type === "Replace") {
|
|
3527
|
+
const matches = findMatches(state.document, command.query, command);
|
|
3528
|
+
const chosen = command.at === void 0 ? matches : matches.slice(command.at, command.at + 1);
|
|
3529
|
+
return replaceMatches(state, chosen, command.replacement, ids, options);
|
|
3530
|
+
}
|
|
3531
|
+
if (command.type === "InsertFootnote") return insertFootnote(state, command.text ?? "", ids, options);
|
|
3532
|
+
if (command.type === "RenameFootnote") return renameFootnote(state, command.from, command.to.trim(), ids, options);
|
|
3533
|
+
if (command.type === "InsertBlock") {
|
|
3534
|
+
const to = command.to ?? besideCaret(state);
|
|
3535
|
+
if (to === void 0) return failure("InvalidSelection");
|
|
3536
|
+
const beside = locateBlock(state.document, "before" in to ? to.before : to.after);
|
|
3537
|
+
if (beside === void 0) return failure("InvalidInput");
|
|
3538
|
+
const containerPath = beside.path.slice(0, -1);
|
|
3539
|
+
const holder = containerPath.length === 0 ? void 0 : blockAtPath(state.document, containerPath);
|
|
3540
|
+
const kind = blockKind(command.block);
|
|
3541
|
+
if (holder !== void 0 && !acceptsChild(state.document, containerPath, kind, options.nodes) || !standsWithin(options.nodes?.definitionFor(kind), holder === void 0 ? void 0 : blockKind(holder))) return failure("UnexpectedChild");
|
|
3542
|
+
const [inserted] = withFreshIds({
|
|
3543
|
+
version: 1,
|
|
3544
|
+
blocks: [command.block]
|
|
3545
|
+
}, ids.mint).blocks;
|
|
3546
|
+
const at = beside.path[beside.path.length - 1] + ("after" in to ? 1 : 0);
|
|
3547
|
+
const first = firstRunIn([inserted]);
|
|
3548
|
+
return apply(state, [Edit.insertBlock(inserted, at, holder?.id), ...first === void 0 ? [] : [Edit.setSelection(caretAt({
|
|
3549
|
+
node: first.id,
|
|
3550
|
+
offset: 0,
|
|
3551
|
+
affinity: "after"
|
|
3552
|
+
}))]]);
|
|
3553
|
+
}
|
|
3554
|
+
if (command.type === "DeleteBlock") {
|
|
3555
|
+
const deleting = locateBlock(state.document, command.node);
|
|
3556
|
+
if (deleting === void 0) return failure("InvalidInput");
|
|
3557
|
+
const target = emptiedBy(state.document, deleting.path.slice(0, -1), [-1]) ?? command.node;
|
|
3558
|
+
const gone = locateBlock(state.document, target);
|
|
3559
|
+
const inside = (path) => gone.path.every((index, depth) => path[depth] === index);
|
|
3560
|
+
const runs = [];
|
|
3561
|
+
eachBlock(state.document.children, (block, path) => {
|
|
3562
|
+
for (const run of block.children) runs.push({
|
|
3563
|
+
run,
|
|
3564
|
+
inside: inside(path),
|
|
3565
|
+
after: pathOrder(path, gone.path) > 0
|
|
3566
|
+
});
|
|
3567
|
+
});
|
|
3568
|
+
const selection = state.selection;
|
|
3569
|
+
const holds = selection !== null && (selection.type === "Node" ? locateBlock(state.document, selection.node) !== void 0 && inside(locateBlock(state.document, selection.node).path) : [selection.anchor, selection.focus].some((end) => runs.some((entry) => entry.inside && entry.run.id === end.node)));
|
|
3570
|
+
const following = runs.find((entry) => entry.after && !entry.inside);
|
|
3571
|
+
const preceding = [...runs].reverse().find((entry) => !entry.after && !entry.inside);
|
|
3572
|
+
const landing = following !== void 0 ? {
|
|
3573
|
+
node: following.run.id,
|
|
3574
|
+
offset: 0,
|
|
3575
|
+
affinity: "after"
|
|
3576
|
+
} : preceding !== void 0 ? {
|
|
3577
|
+
node: preceding.run.id,
|
|
3578
|
+
offset: preceding.run.text.length,
|
|
3579
|
+
affinity: "after"
|
|
3580
|
+
} : void 0;
|
|
3581
|
+
if (gone.path.length === 1 && state.document.children.length === 1) {
|
|
3582
|
+
const textId = NodeId.make(ids.mint());
|
|
3583
|
+
const paragraph = {
|
|
3584
|
+
type: "Paragraph",
|
|
3585
|
+
id: NodeId.make(ids.mint()),
|
|
3586
|
+
children: [{
|
|
3587
|
+
type: "Text",
|
|
3588
|
+
id: textId,
|
|
3589
|
+
text: "",
|
|
3590
|
+
marks: []
|
|
3591
|
+
}]
|
|
3592
|
+
};
|
|
3593
|
+
return apply(state, [
|
|
3594
|
+
Edit.insertBlock(paragraph, 1),
|
|
3595
|
+
Edit.deleteBlock(target),
|
|
3596
|
+
Edit.setSelection(caretAt({
|
|
3597
|
+
node: textId,
|
|
3598
|
+
offset: 0,
|
|
3599
|
+
affinity: "after"
|
|
3600
|
+
}))
|
|
3601
|
+
]);
|
|
3602
|
+
}
|
|
3603
|
+
return apply(state, [Edit.deleteBlock(target), ...holds ? [Edit.setSelection(landing === void 0 ? null : caretAt(landing))] : []]);
|
|
3604
|
+
}
|
|
2900
3605
|
if (command.type === "MoveBlock") {
|
|
2901
3606
|
const moving = locateBlock(state.document, command.node);
|
|
2902
3607
|
const beside = locateBlock(state.document, "before" in command.to ? command.to.before : command.to.after);
|
|
@@ -2925,9 +3630,39 @@ const run = (state, command, ids, options = {}) => {
|
|
|
2925
3630
|
]);
|
|
2926
3631
|
}
|
|
2927
3632
|
const selection = state.selection;
|
|
3633
|
+
if (selection?.type === "Node") {
|
|
3634
|
+
const after = {
|
|
3635
|
+
type: "InsertBlock",
|
|
3636
|
+
block: {
|
|
3637
|
+
type: "Paragraph",
|
|
3638
|
+
id: NodeId.make("paragraph"),
|
|
3639
|
+
children: [{
|
|
3640
|
+
type: "Text",
|
|
3641
|
+
id: NodeId.make("text"),
|
|
3642
|
+
text: "",
|
|
3643
|
+
marks: []
|
|
3644
|
+
}]
|
|
3645
|
+
},
|
|
3646
|
+
to: { after: selection.node }
|
|
3647
|
+
};
|
|
3648
|
+
if (command.type === "DeleteBackward" || command.type === "DeleteForward") return run(state, {
|
|
3649
|
+
type: "DeleteBlock",
|
|
3650
|
+
node: selection.node
|
|
3651
|
+
}, ids, options);
|
|
3652
|
+
if (command.type === "SplitBlock") return run(state, after, ids, options);
|
|
3653
|
+
if (command.type === "InsertText") return runAction(state, [
|
|
3654
|
+
after,
|
|
3655
|
+
{
|
|
3656
|
+
type: "DeleteBlock",
|
|
3657
|
+
node: selection.node
|
|
3658
|
+
},
|
|
3659
|
+
command
|
|
3660
|
+
], ids, options);
|
|
3661
|
+
}
|
|
2928
3662
|
if (selection === null || selection.type === "Node") return failure("InvalidSelection");
|
|
2929
3663
|
if (command.type === "InsertText") {
|
|
2930
|
-
const
|
|
3664
|
+
const edge = command.marks === void 0 && isCollapsed(selection) ? edgeMarks(state.document, selection.anchor, options.marks) : void 0;
|
|
3665
|
+
const stored = command.marks ?? edge;
|
|
2931
3666
|
const target = isCollapsed(selection) ? stored === void 0 ? resolveInsertion(state.document, selection.anchor, options.marks) : selection.anchor : ordered(state.document, selection)?.start;
|
|
2932
3667
|
if (target === void 0) return failure("InvalidSelection");
|
|
2933
3668
|
const at = locate(state.document, target.node);
|
|
@@ -2935,7 +3670,7 @@ const run = (state, command, ids, options = {}) => {
|
|
|
2935
3670
|
const marks = [];
|
|
2936
3671
|
if (stored !== void 0) {
|
|
2937
3672
|
for (const mark of stored) {
|
|
2938
|
-
if (!declared.accepts(mark)) return failure("InvalidInput");
|
|
3673
|
+
if (edge === void 0 && !declared.accepts(mark)) return failure("InvalidInput");
|
|
2939
3674
|
marks.push(mark);
|
|
2940
3675
|
}
|
|
2941
3676
|
if (marks.length > 0 && forbidsMarks(state.document, at.path, options.nodes)) return failure("ForbiddenMark");
|
|
@@ -2987,14 +3722,44 @@ const run = (state, command, ids, options = {}) => {
|
|
|
2987
3722
|
type: "RetypeBlock",
|
|
2988
3723
|
to: { type: "Paragraph" }
|
|
2989
3724
|
}, ids, options);
|
|
3725
|
+
if (backward && block.type === "Heading") return run(state, {
|
|
3726
|
+
type: "RetypeBlock",
|
|
3727
|
+
to: { type: "Paragraph" }
|
|
3728
|
+
}, ids, options);
|
|
2990
3729
|
const containerPath = at.path.slice(0, -1);
|
|
2991
3730
|
const siblings = containerBlocks(state.document, containerPath);
|
|
2992
3731
|
const index = at.path[at.path.length - 1];
|
|
2993
3732
|
const neighborBlock = siblings[backward ? index - 1 : index + 1];
|
|
2994
|
-
if (neighborBlock
|
|
2995
|
-
|
|
3733
|
+
if (neighborBlock !== void 0 && isAtom(options.nodes, neighborBlock)) return apply(state, [...textOfBlock(block) === "" ? [Edit.deleteBlock(block.id)] : [], Edit.setSelection({
|
|
3734
|
+
type: "Node",
|
|
3735
|
+
node: neighborBlock.id
|
|
3736
|
+
})]);
|
|
3737
|
+
if (neighborBlock === void 0 && backward) {
|
|
3738
|
+
const lifted = containerPath.length > 0 && options.nodes !== void 0 ? liftOperations(state.document, siblings[index].id, ids, options.nodes) : void 0;
|
|
2996
3739
|
return apply(state, lifted ?? []);
|
|
2997
3740
|
}
|
|
3741
|
+
if (neighborBlock === void 0 || !joinable(backward ? neighborBlock : block, backward ? block : neighborBlock)) {
|
|
3742
|
+
const leafPath = neighborBlock === void 0 ? nextLeaf(state.document, at.path) : edgeLeaf(state.document, [...containerPath, index + (backward ? -1 : 1)], backward);
|
|
3743
|
+
const leaf = leafPath === void 0 ? void 0 : blockAtPath(state.document, leafPath);
|
|
3744
|
+
if (leafPath === void 0 || leaf === void 0) return apply(state, []);
|
|
3745
|
+
const empty = textOfBlock(block) === "";
|
|
3746
|
+
if (isAtom(options.nodes, leaf)) return apply(state, [Edit.setSelection({
|
|
3747
|
+
type: "Node",
|
|
3748
|
+
node: leaf.id
|
|
3749
|
+
})]);
|
|
3750
|
+
const first = leaf.children[0];
|
|
3751
|
+
const last = leaf.children[leaf.children.length - 1];
|
|
3752
|
+
const target = backward ? last : first;
|
|
3753
|
+
if (target === void 0) return apply(state, []);
|
|
3754
|
+
const edge = {
|
|
3755
|
+
node: target.id,
|
|
3756
|
+
offset: backward ? target.text.length : 0,
|
|
3757
|
+
affinity: "after"
|
|
3758
|
+
};
|
|
3759
|
+
if (!(empty && neighborBlock !== void 0) && (backward ? joinable(leaf, block) : joinable(block, leaf))) return backward ? clearRange(state, edge, caret, options) : clearRange(state, caret, edge, options);
|
|
3760
|
+
if (neighborBlock === void 0 || !backward && !empty) return apply(state, []);
|
|
3761
|
+
return apply(state, [...empty ? [Edit.deleteBlock(block.id)] : [], Edit.setSelection(caretAt(edge))]);
|
|
3762
|
+
}
|
|
2998
3763
|
const survivor = backward ? neighborBlock : siblings[index];
|
|
2999
3764
|
const removed = backward ? siblings[index] : neighborBlock;
|
|
3000
3765
|
const lastRun = lastRunOf(survivor) ?? lastRunOf(removed);
|
|
@@ -3016,6 +3781,28 @@ const run = (state, command, ids, options = {}) => {
|
|
|
3016
3781
|
if (at === void 0) return failure("MissingText");
|
|
3017
3782
|
const deletions = span === void 0 ? [] : deleteRange(state.document, span.start, span.end);
|
|
3018
3783
|
if (deletions === void 0) return failure("InvalidParent");
|
|
3784
|
+
const holder = blockAtPath(state.document, at.path);
|
|
3785
|
+
const definition = holder?.type === "Node" ? options.nodes?.definitionFor(holder.kind) : void 0;
|
|
3786
|
+
if (holder !== void 0 && definition?.kind === "node" && definition.lineBreaks === true) {
|
|
3787
|
+
const last = holder.children[holder.children.length - 1];
|
|
3788
|
+
return span === void 0 && last?.id === at.id && caret.offset === at.text.length && holder.children.map((run) => run.text).join("").endsWith("\n") ? runAction(state, [{ type: "DeleteBackward" }, {
|
|
3789
|
+
type: "InsertBlock",
|
|
3790
|
+
block: {
|
|
3791
|
+
type: "Paragraph",
|
|
3792
|
+
id: NodeId.make("paragraph"),
|
|
3793
|
+
children: [{
|
|
3794
|
+
type: "Text",
|
|
3795
|
+
id: NodeId.make("text"),
|
|
3796
|
+
text: "",
|
|
3797
|
+
marks: []
|
|
3798
|
+
}]
|
|
3799
|
+
},
|
|
3800
|
+
to: { after: holder.id }
|
|
3801
|
+
}], ids, options) : run(state, {
|
|
3802
|
+
type: "InsertText",
|
|
3803
|
+
text: definition.indents === true ? `\n${indentationAt(textOfBlock(holder), offsetIn(holder, caret) ?? 0)}` : "\n"
|
|
3804
|
+
}, ids, options);
|
|
3805
|
+
}
|
|
3019
3806
|
const item = itemAround(state.document, at.path, options.nodes);
|
|
3020
3807
|
if (item !== void 0) return span === void 0 ? splitItem(state, at, caret.offset, item, ids, options.nodes) : runAction(state, [{ type: "DeleteBackward" }, { type: "SplitBlock" }], ids, options);
|
|
3021
3808
|
const textId = ids.mint();
|
|
@@ -3041,7 +3828,7 @@ const run = (state, command, ids, options = {}) => {
|
|
|
3041
3828
|
span = ordered(state.document, extent.selection);
|
|
3042
3829
|
}
|
|
3043
3830
|
if (span === void 0) return failure("InvalidSelection");
|
|
3044
|
-
const spans = covered(state.document, span.start, span.end);
|
|
3831
|
+
const spans = covered(state.document, span.start, span.end).filter((entry) => command.type !== "ClearMark" || typeof mark === "string" || entry.run.marks.some((held) => sameMark(held, mark)));
|
|
3045
3832
|
if (spans.length === 0) return apply(state, []);
|
|
3046
3833
|
const adding = command.type === "SetMark" || command.type === "ToggleMark" && !spans.every((entry) => entry.run.marks.some((held) => markName(held) === name));
|
|
3047
3834
|
const operations = [];
|
|
@@ -3066,12 +3853,19 @@ const run = (state, command, ids, options = {}) => {
|
|
|
3066
3853
|
return apply(state, operations);
|
|
3067
3854
|
}
|
|
3068
3855
|
if (command.type === "Paste") {
|
|
3069
|
-
|
|
3856
|
+
const content = command.slice.blocks;
|
|
3857
|
+
if (content.length === 0) return apply(state, []);
|
|
3070
3858
|
const span = isCollapsed(selection) ? void 0 : ordered(state.document, selection);
|
|
3071
3859
|
if (!isCollapsed(selection) && span === void 0) return failure("InvalidSelection");
|
|
3072
3860
|
const caret = span?.start ?? selection.anchor;
|
|
3073
3861
|
const at = locate(state.document, caret.node);
|
|
3074
3862
|
if (at === void 0) return failure("MissingText");
|
|
3863
|
+
const into = blockAtPath(state.document, at.path);
|
|
3864
|
+
const holds = into?.type === "Node" ? options.nodes?.definitionFor(into.kind) : void 0;
|
|
3865
|
+
if (holds?.kind === "node" && holds.lineBreaks === true) return run(state, {
|
|
3866
|
+
type: "InsertText",
|
|
3867
|
+
text: toText(content)
|
|
3868
|
+
}, ids, options);
|
|
3075
3869
|
const index = at.path[at.path.length - 1];
|
|
3076
3870
|
const containerPath = at.path.slice(0, -1);
|
|
3077
3871
|
const parent = containerPath.length === 0 ? void 0 : blockAtPath(state.document, containerPath)?.id;
|
|
@@ -3083,47 +3877,254 @@ const run = (state, command, ids, options = {}) => {
|
|
|
3083
3877
|
if (inserted.some((piece) => containerPath.length > 0 && !acceptsChild(state.document, containerPath, blockKind(piece), options.nodes) || !standsWithin(options.nodes?.definitionFor(blockKind(piece)), holder === void 0 ? void 0 : blockKind(holder)))) return failure("UnexpectedChild");
|
|
3084
3878
|
const block = blockAtPath(state.document, at.path);
|
|
3085
3879
|
if (block === void 0) return failure("MissingText");
|
|
3086
|
-
const
|
|
3087
|
-
const
|
|
3088
|
-
|
|
3089
|
-
|
|
3090
|
-
|
|
3091
|
-
|
|
3092
|
-
|
|
3093
|
-
|
|
3094
|
-
|
|
3095
|
-
|
|
3096
|
-
|
|
3097
|
-
|
|
3098
|
-
|
|
3880
|
+
const end = span?.end ?? caret;
|
|
3881
|
+
const endBlock = blockAtPath(state.document, locate(state.document, end.node)?.path ?? at.path);
|
|
3882
|
+
const beforeEmpty = offsetIn(block, caret) === 0;
|
|
3883
|
+
const afterEmpty = endBlock === void 0 || offsetIn(endBlock, end) === textOfBlock(endBlock).length;
|
|
3884
|
+
const tailText = NodeId.make(ids.mint());
|
|
3885
|
+
const tail = NodeId.make(ids.mint());
|
|
3886
|
+
operations.push(Edit.splitBlock(at.blockId, at.id, caret.offset, tail, tailText));
|
|
3887
|
+
for (const [offset, piece] of inserted.entries()) operations.push(Edit.insertBlock(piece, index + 1 + offset, parent));
|
|
3888
|
+
const first = inserted[0];
|
|
3889
|
+
const last = inserted[inserted.length - 1];
|
|
3890
|
+
const lastRun = last.children[last.children.length - 1];
|
|
3891
|
+
operations.push(Edit.setSelection(caretAt(lastRun === void 0 ? {
|
|
3892
|
+
node: tailText,
|
|
3893
|
+
offset: 0,
|
|
3894
|
+
affinity: "after"
|
|
3895
|
+
} : {
|
|
3896
|
+
node: lastRun.id,
|
|
3897
|
+
offset: lastRun.text.length,
|
|
3898
|
+
affinity: "after"
|
|
3899
|
+
})));
|
|
3900
|
+
const joinsHead = joinable(block, first);
|
|
3901
|
+
const joinsTail = joinable(last, block);
|
|
3902
|
+
if (joinsHead) operations.push(Edit.joinBlocks(at.blockId, first.id));
|
|
3903
|
+
if (joinsTail) operations.push(Edit.joinBlocks(joinsHead && last === first ? at.blockId : last.id, tail));
|
|
3904
|
+
if (!joinsHead && beforeEmpty) operations.push(Edit.deleteBlock(at.blockId));
|
|
3905
|
+
if (!joinsTail && afterEmpty && lastRun !== void 0) operations.push(Edit.deleteBlock(tail));
|
|
3099
3906
|
return apply(state, operations);
|
|
3100
3907
|
}
|
|
3101
3908
|
return apply(state, []);
|
|
3102
3909
|
};
|
|
3910
|
+
/** Where an `InsertBlock` without a place goes: beside the caret's block, before it when empty. */
|
|
3911
|
+
const besideCaret = (state) => {
|
|
3912
|
+
if (state.selection?.type !== "Range") return void 0;
|
|
3913
|
+
const run = locateRun(state.document, ordered(state.document, state.selection)?.start.node ?? state.selection.anchor.node);
|
|
3914
|
+
const block = run === void 0 ? void 0 : blockAtPath(state.document, run.path);
|
|
3915
|
+
if (block === void 0) return void 0;
|
|
3916
|
+
return block.children.every((child) => child.text === "") ? { before: block.id } : { after: block.id };
|
|
3917
|
+
};
|
|
3918
|
+
const textOfBlock = (block) => block.children.map((run) => run.text).join("");
|
|
3919
|
+
/** The block the selection starts in when its kind indents its lines, as code does. */
|
|
3920
|
+
const linesHolder = (state, nodes) => {
|
|
3921
|
+
if (state.selection?.type !== "Range") return void 0;
|
|
3922
|
+
const start = ordered(state.document, state.selection)?.start;
|
|
3923
|
+
const run = start === void 0 ? void 0 : locateRun(state.document, start.node);
|
|
3924
|
+
const block = run === void 0 ? void 0 : blockAtPath(state.document, run.path);
|
|
3925
|
+
if (block?.type !== "Node") return void 0;
|
|
3926
|
+
const definition = nodes?.definitionFor(block.kind);
|
|
3927
|
+
return definition?.kind === "node" && definition.indents === true ? block : void 0;
|
|
3928
|
+
};
|
|
3103
3929
|
/**
|
|
3104
|
-
*
|
|
3105
|
-
*
|
|
3106
|
-
* trailing half a split created, then the next sibling.
|
|
3930
|
+
* Whether Tab means something where the selection is: in code, or in a table cell. An editor
|
|
3931
|
+
* takes Tab only there, so anywhere else it still moves focus out of the editor.
|
|
3107
3932
|
*/
|
|
3108
|
-
const
|
|
3109
|
-
|
|
3110
|
-
|
|
3111
|
-
|
|
3112
|
-
|
|
3113
|
-
|
|
3114
|
-
|
|
3115
|
-
|
|
3116
|
-
|
|
3117
|
-
|
|
3118
|
-
|
|
3119
|
-
|
|
3120
|
-
|
|
3121
|
-
|
|
3122
|
-
|
|
3123
|
-
|
|
3124
|
-
|
|
3125
|
-
|
|
3933
|
+
const takesTab = (document, selection, nodes) => linesHolder({
|
|
3934
|
+
document,
|
|
3935
|
+
selection
|
|
3936
|
+
}, nodes) !== void 0 || itemAtCaret({
|
|
3937
|
+
document,
|
|
3938
|
+
selection
|
|
3939
|
+
}, nodes) !== void 0 || tableAt(document, selection) !== void 0;
|
|
3940
|
+
/** The list item the selection starts in, when the vocabulary says its list holds it. */
|
|
3941
|
+
const itemAtCaret = (state, nodes) => {
|
|
3942
|
+
if (state.selection?.type !== "Range") return void 0;
|
|
3943
|
+
const start = ordered(state.document, state.selection)?.start;
|
|
3944
|
+
const run = start === void 0 ? void 0 : locateRun(state.document, start.node);
|
|
3945
|
+
return run === void 0 ? void 0 : itemAround(state.document, run.path, nodes);
|
|
3946
|
+
};
|
|
3947
|
+
/** Whether two props hold the same values, for a list that two items may share. */
|
|
3948
|
+
const sameProps = (left, right) => {
|
|
3949
|
+
const keys = Object.keys(left);
|
|
3950
|
+
return keys.length === Object.keys(right).length && keys.every((key) => Object.hasOwn(right, key) && Equal.equals(left[key], right[key]));
|
|
3951
|
+
};
|
|
3952
|
+
/**
|
|
3953
|
+
* Tab in a list item: the item moves under the item before it, as the last item of the list
|
|
3954
|
+
* that item ends with, or of a new one like its own when it ends with none. It keeps its
|
|
3955
|
+
* identity, and the caret with it. The first item has nothing to go under, and stays.
|
|
3956
|
+
*/
|
|
3957
|
+
const sinkItem = (state, item, ids, options) => {
|
|
3958
|
+
if (item.list.type !== "Node") return apply(state, []);
|
|
3959
|
+
const list = item.list;
|
|
3960
|
+
const index = item.containerPath[item.containerPath.length - 1];
|
|
3961
|
+
const previous = (list.blocks ?? [])[index - 1];
|
|
3962
|
+
if (previous?.type !== "Node") return apply(state, []);
|
|
3963
|
+
const nested = previous.blocks?.at(-1);
|
|
3964
|
+
const last = nested?.type === "Node" ? nested.blocks?.at(-1) : void 0;
|
|
3965
|
+
if (nested?.type === "Node" && nested.kind === list.kind && sameProps(nested.props, list.props) && last !== void 0) return run(state, {
|
|
3966
|
+
type: "MoveBlock",
|
|
3967
|
+
node: item.container.id,
|
|
3968
|
+
to: { after: last.id }
|
|
3969
|
+
}, ids, options);
|
|
3970
|
+
const created = NodeId.make(ids.mint());
|
|
3971
|
+
return apply(state, [Edit.insertBlock({
|
|
3972
|
+
type: "Node",
|
|
3973
|
+
kind: list.kind,
|
|
3974
|
+
id: created,
|
|
3975
|
+
props: list.props,
|
|
3976
|
+
children: [],
|
|
3977
|
+
blocks: []
|
|
3978
|
+
}, (previous.blocks ?? []).length, previous.id), Edit.moveBlock(item.container.id, 0, created)]);
|
|
3979
|
+
};
|
|
3980
|
+
/**
|
|
3981
|
+
* Shift+Tab in a list item: a nested item moves out to follow the item its list is in, and an
|
|
3982
|
+
* item at the top level leaves its list, as `LiftBlock` takes it. A list the move empties goes.
|
|
3983
|
+
*/
|
|
3984
|
+
const liftItem = (state, item, ids, options) => {
|
|
3985
|
+
const listPath = item.containerPath.slice(0, -1);
|
|
3986
|
+
const outerItem = blockAtPath(state.document, listPath.slice(0, -1));
|
|
3987
|
+
const outerList = blockAtPath(state.document, listPath.slice(0, -2));
|
|
3988
|
+
return outerItem?.type === "Node" && outerList?.type === "Node" && options.nodes !== void 0 && holdsItem(options.nodes, outerList.kind, outerItem.kind) ? run(state, {
|
|
3989
|
+
type: "MoveBlock",
|
|
3990
|
+
node: item.container.id,
|
|
3991
|
+
to: { after: outerItem.id }
|
|
3992
|
+
}, ids, options) : run(state, { type: "LiftBlock" }, ids, options);
|
|
3993
|
+
};
|
|
3994
|
+
/**
|
|
3995
|
+
* Replaces matches, last first, each read again from its block's offset in the state the one
|
|
3996
|
+
* after it left: a replacement later in a block leaves an earlier match's offset alone, though
|
|
3997
|
+
* the runs it touched may have merged or split, so positions found before it would not do.
|
|
3998
|
+
*/
|
|
3999
|
+
const replaceMatches = (state, matches, replacement, ids, options) => {
|
|
4000
|
+
let done = apply(state, []);
|
|
4001
|
+
for (const match of [...matches].reverse()) {
|
|
4002
|
+
if (!done.ok) return done;
|
|
4003
|
+
const block = locateBlock(done.state.document, match.block)?.block;
|
|
4004
|
+
const from = block === void 0 ? void 0 : positionInBlock(block, match.at);
|
|
4005
|
+
const to = block === void 0 ? void 0 : positionInBlock(block, match.at + match.length);
|
|
4006
|
+
if (from === void 0 || to === void 0) return failure("MissingText");
|
|
4007
|
+
const step = runAction(done.state, [{
|
|
4008
|
+
type: "SetSelection",
|
|
4009
|
+
selection: {
|
|
4010
|
+
type: "Range",
|
|
4011
|
+
anchor: from,
|
|
4012
|
+
focus: to
|
|
4013
|
+
}
|
|
4014
|
+
}, {
|
|
4015
|
+
type: "InsertText",
|
|
4016
|
+
text: replacement
|
|
4017
|
+
}], ids, options);
|
|
4018
|
+
if (!step.ok) return step;
|
|
4019
|
+
done = {
|
|
4020
|
+
ok: true,
|
|
4021
|
+
state: step.state,
|
|
4022
|
+
changeSet: unionChangeSet(done.changeSet, step.changeSet),
|
|
4023
|
+
positionMap: [...done.positionMap, ...step.positionMap],
|
|
4024
|
+
transactions: [...done.transactions, ...step.transactions]
|
|
4025
|
+
};
|
|
4026
|
+
}
|
|
4027
|
+
return done;
|
|
4028
|
+
};
|
|
4029
|
+
const insertFootnote = (state, text, ids, options) => {
|
|
4030
|
+
const selection = state.selection;
|
|
4031
|
+
if (selection?.type !== "Range") return failure("InvalidSelection");
|
|
4032
|
+
const start = rangeStart(state.document, selection);
|
|
4033
|
+
const last = state.document.children[state.document.children.length - 1];
|
|
4034
|
+
if (start === void 0 || last === void 0) return failure("InvalidSelection");
|
|
4035
|
+
const end = start === selection.anchor ? selection.focus : selection.anchor;
|
|
4036
|
+
const label = nextFootnoteLabel(state.document);
|
|
4037
|
+
const definition = {
|
|
4038
|
+
type: "Node",
|
|
4039
|
+
kind: FootnoteDefinition.name,
|
|
4040
|
+
id: NodeId.make("footnote"),
|
|
4041
|
+
props: { label },
|
|
4042
|
+
children: [],
|
|
4043
|
+
blocks: [{
|
|
4044
|
+
type: "Paragraph",
|
|
4045
|
+
id: NodeId.make("footnote-text"),
|
|
4046
|
+
children: [{
|
|
4047
|
+
type: "Text",
|
|
4048
|
+
id: NodeId.make("footnote-run"),
|
|
4049
|
+
text,
|
|
4050
|
+
marks: []
|
|
4051
|
+
}]
|
|
4052
|
+
}]
|
|
3126
4053
|
};
|
|
4054
|
+
return runAction(state, [
|
|
4055
|
+
{
|
|
4056
|
+
type: "SetSelection",
|
|
4057
|
+
selection: {
|
|
4058
|
+
type: "Range",
|
|
4059
|
+
anchor: end,
|
|
4060
|
+
focus: end
|
|
4061
|
+
}
|
|
4062
|
+
},
|
|
4063
|
+
{
|
|
4064
|
+
type: "InsertText",
|
|
4065
|
+
text: label,
|
|
4066
|
+
marks: [FootnoteRef.of({ label })]
|
|
4067
|
+
},
|
|
4068
|
+
{
|
|
4069
|
+
type: "InsertBlock",
|
|
4070
|
+
block: definition,
|
|
4071
|
+
to: { after: last.id }
|
|
4072
|
+
}
|
|
4073
|
+
], ids, options);
|
|
4074
|
+
};
|
|
4075
|
+
const renameFootnote = (state, from, to, ids, options) => {
|
|
4076
|
+
const notes = footnotesOf(state.document);
|
|
4077
|
+
const note = notes.find((each) => each.label === from);
|
|
4078
|
+
if (note === void 0 || to === "" || to !== from && notes.some((each) => each.label === to)) return failure("InvalidInput");
|
|
4079
|
+
const relabelled = FootnoteRef.of({ label: to });
|
|
4080
|
+
const references = [];
|
|
4081
|
+
eachBlock(state.document.children, (block) => {
|
|
4082
|
+
for (const run of block.children) {
|
|
4083
|
+
if (referenceLabel(run) !== from) continue;
|
|
4084
|
+
references.push({
|
|
4085
|
+
type: "SetSelection",
|
|
4086
|
+
selection: {
|
|
4087
|
+
type: "Range",
|
|
4088
|
+
anchor: {
|
|
4089
|
+
node: run.id,
|
|
4090
|
+
offset: 0,
|
|
4091
|
+
affinity: "after"
|
|
4092
|
+
},
|
|
4093
|
+
focus: {
|
|
4094
|
+
node: run.id,
|
|
4095
|
+
offset: run.text.length,
|
|
4096
|
+
affinity: "before"
|
|
4097
|
+
}
|
|
4098
|
+
}
|
|
4099
|
+
}, {
|
|
4100
|
+
type: "InsertText",
|
|
4101
|
+
text: run.text === from ? to : run.text,
|
|
4102
|
+
marks: run.marks.map((each) => markName(each) === FootnoteRef.name ? relabelled : each)
|
|
4103
|
+
});
|
|
4104
|
+
}
|
|
4105
|
+
});
|
|
4106
|
+
const selection = state.selection;
|
|
4107
|
+
const inReference = selection?.type === "Range" && (note.references.includes(selection.anchor.node) || note.references.includes(selection.focus.node));
|
|
4108
|
+
return runAction(state, [
|
|
4109
|
+
...note.definition === void 0 ? [] : [{
|
|
4110
|
+
type: "SetProps",
|
|
4111
|
+
node: note.definition,
|
|
4112
|
+
props: { label: to }
|
|
4113
|
+
}],
|
|
4114
|
+
...references,
|
|
4115
|
+
...inReference || selection === null ? [] : [{
|
|
4116
|
+
type: "SetSelection",
|
|
4117
|
+
selection
|
|
4118
|
+
}]
|
|
4119
|
+
], ids, options);
|
|
4120
|
+
};
|
|
4121
|
+
/** The first run anywhere in these blocks, nested ones included, in document order. */
|
|
4122
|
+
const firstRunIn = (blocks) => {
|
|
4123
|
+
let found;
|
|
4124
|
+
eachBlock(blocks, (block) => {
|
|
4125
|
+
found ??= block.children[0];
|
|
4126
|
+
});
|
|
4127
|
+
return found;
|
|
3127
4128
|
};
|
|
3128
4129
|
/**
|
|
3129
4130
|
* A composed action's invalidation summary is the union of its commands': an identity
|
|
@@ -3181,20 +4182,28 @@ const runAction = (state, commands, ids, options = {}) => {
|
|
|
3181
4182
|
* A rule that claims to consume more than it was shown throws: those deletes would run past
|
|
3182
4183
|
* the block's start and join it to the one before, which no rule means.
|
|
3183
4184
|
*/
|
|
3184
|
-
const applyInputRules = (rules, input) =>
|
|
4185
|
+
const applyInputRules = (rules, input) => resolveInputRules(rules, input).action;
|
|
4186
|
+
/**
|
|
4187
|
+
* `applyInputRules`, with the marks the matching rule says the next typed text carries, which an
|
|
4188
|
+
* editor keeps as its stored marks.
|
|
4189
|
+
*/
|
|
4190
|
+
const resolveInputRules = (rules, input) => {
|
|
3185
4191
|
const typed = `${input.textBefore}${input.text}`;
|
|
3186
4192
|
for (const rule of rules) {
|
|
3187
4193
|
const matched = rule.match(typed, input.within ?? []);
|
|
3188
4194
|
if (matched === void 0) continue;
|
|
3189
4195
|
if (!Number.isInteger(matched.remove) || matched.remove < 0 || matched.remove > typed.length) throw new RangeError(`Input rule "${rule.name}" removes ${matched.remove} characters of the ${typed.length} it was shown`);
|
|
3190
4196
|
const consumed = Array.from({ length: matched.remove }, () => ({ type: "DeleteBackward" }));
|
|
3191
|
-
return
|
|
3192
|
-
|
|
3193
|
-
|
|
3194
|
-
|
|
3195
|
-
|
|
4197
|
+
return {
|
|
4198
|
+
action: [
|
|
4199
|
+
input.insertion,
|
|
4200
|
+
...consumed,
|
|
4201
|
+
...matched.commands
|
|
4202
|
+
],
|
|
4203
|
+
storedMarks: matched.storedMarks
|
|
4204
|
+
};
|
|
3196
4205
|
}
|
|
3197
|
-
return [input.insertion];
|
|
4206
|
+
return { action: [input.insertion] };
|
|
3198
4207
|
};
|
|
3199
4208
|
//#endregion
|
|
3200
4209
|
//#region src/history.ts
|
|
@@ -3367,43 +4376,6 @@ const runPieces = (text, spans) => {
|
|
|
3367
4376
|
return pieces;
|
|
3368
4377
|
};
|
|
3369
4378
|
//#endregion
|
|
3370
|
-
//#region src/search.ts
|
|
3371
|
-
/**
|
|
3372
|
-
* Search highlights, as decorations (§64): every occurrence of a query in a document's text,
|
|
3373
|
-
* without changing it. The producer lives here rather than in an application because it is a
|
|
3374
|
-
* pure read of a document and nothing else, and the interpreter that draws the result is the
|
|
3375
|
-
* caller's choice — the read-only view renders these as `span[data-decoration="search"]`
|
|
3376
|
-
* (§126), and the editable one takes them when its overlay lands.
|
|
3377
|
-
*
|
|
3378
|
-
* A match is exact: a caller that wants case-insensitive search lowercases both sides. It
|
|
3379
|
-
* never spans two blocks, because what a document has between blocks is a structural
|
|
3380
|
-
* boundary rather than text. It may span two *runs*, though, which is why a block's runs are
|
|
3381
|
-
* read as one string and the endpoints are mapped back to positions.
|
|
3382
|
-
*/
|
|
3383
|
-
/** The kind a search decoration carries, which is what a stylesheet reaches it by. */
|
|
3384
|
-
const SEARCH_DECORATION = "search";
|
|
3385
|
-
/** Every occurrence of `query` in the document, as decorations a renderer can draw. */
|
|
3386
|
-
const searchDecorations = (document, query) => {
|
|
3387
|
-
const found = [];
|
|
3388
|
-
if (query.length === 0) return found;
|
|
3389
|
-
eachBlock(document.children, (block) => {
|
|
3390
|
-
if (block.type === "Unknown" || block.children.length === 0) return;
|
|
3391
|
-
const text = block.children.map((run) => run.text).join("");
|
|
3392
|
-
let at = text.indexOf(query);
|
|
3393
|
-
while (at >= 0) {
|
|
3394
|
-
const anchor = positionInBlock(block, at);
|
|
3395
|
-
const focus = positionInBlock(block, at + query.length);
|
|
3396
|
-
if (anchor !== void 0 && focus !== void 0) found.push({
|
|
3397
|
-
from: anchor,
|
|
3398
|
-
to: focus,
|
|
3399
|
-
kind: SEARCH_DECORATION
|
|
3400
|
-
});
|
|
3401
|
-
at = text.indexOf(query, at + query.length);
|
|
3402
|
-
}
|
|
3403
|
-
});
|
|
3404
|
-
return found;
|
|
3405
|
-
};
|
|
3406
|
-
//#endregion
|
|
3407
4379
|
//#region src/code.ts
|
|
3408
4380
|
/**
|
|
3409
4381
|
* Code highlighting, as decorations (§124 §7, §130): the document holds a `CodeBlock`'s
|
|
@@ -3563,12 +4535,22 @@ const standardNodes = [
|
|
|
3563
4535
|
node("CodeBlock", {
|
|
3564
4536
|
Props: Schema.Struct({ language: Schema.optional(Schema.String) }),
|
|
3565
4537
|
children: textContent,
|
|
3566
|
-
marks: "none"
|
|
4538
|
+
marks: "none",
|
|
4539
|
+
lineBreaks: true,
|
|
4540
|
+
indents: true
|
|
3567
4541
|
}),
|
|
3568
4542
|
atom("ThematicBreak"),
|
|
3569
4543
|
atom("Image", { Props: Schema.Struct({
|
|
3570
4544
|
src: Schema.String,
|
|
3571
|
-
alt: Schema.optional(Schema.String)
|
|
4545
|
+
alt: Schema.optional(Schema.String),
|
|
4546
|
+
caption: Schema.optional(Schema.String),
|
|
4547
|
+
width: Schema.optional(Schema.Number.check(Schema.isGreaterThan(0))),
|
|
4548
|
+
align: Schema.optional(Schema.Literals([
|
|
4549
|
+
"start",
|
|
4550
|
+
"center",
|
|
4551
|
+
"end"
|
|
4552
|
+
])),
|
|
4553
|
+
asset: Schema.optional(ImageAsset)
|
|
3572
4554
|
}) }),
|
|
3573
4555
|
node("Table", { children: blocksOf("TableRow") }),
|
|
3574
4556
|
node("TableRow", {
|
|
@@ -3582,6 +4564,24 @@ const standardNodes = [
|
|
|
3582
4564
|
within: ["TableRow"]
|
|
3583
4565
|
})
|
|
3584
4566
|
];
|
|
4567
|
+
const decodeAsset = Schema.decodeUnknownOption(ImageAsset);
|
|
4568
|
+
/**
|
|
4569
|
+
* An `Image`'s `width` and `height` attributes from its props' `asset`, at its drawn `width`
|
|
4570
|
+
* when it has one, so a page keeps the box before the file arrives; none without facts. The
|
|
4571
|
+
* standard rendering writes them; a rendering of its own (a figure) spreads them on its `img`.
|
|
4572
|
+
* The facts are trusted to be `src`'s: whatever changes `src` replaces or clears `asset`.
|
|
4573
|
+
*/
|
|
4574
|
+
const imageBox = (props) => Option.match(decodeAsset(props["asset"]), {
|
|
4575
|
+
onNone: () => ({}),
|
|
4576
|
+
onSome: ({ width, height }) => {
|
|
4577
|
+
const drawn = props["width"];
|
|
4578
|
+
const shown = typeof drawn === "number" && drawn > 0 ? drawn : width;
|
|
4579
|
+
return {
|
|
4580
|
+
width: String(Math.round(shown)),
|
|
4581
|
+
height: String(Math.round(shown * height / width))
|
|
4582
|
+
};
|
|
4583
|
+
}
|
|
4584
|
+
});
|
|
3585
4585
|
/**
|
|
3586
4586
|
* A URL attribute, or none when the value fails the URL policy. Import already applies the
|
|
3587
4587
|
* policy, but a document also arrives decoded, synchronized, or edited through `SetMark`,
|
|
@@ -3646,7 +4646,8 @@ const standardRendering = rendering({
|
|
|
3646
4646
|
tag: "img",
|
|
3647
4647
|
attributes: {
|
|
3648
4648
|
...urlAttribute("src", block.props.src),
|
|
3649
|
-
alt: String(block.props.alt ?? "")
|
|
4649
|
+
alt: String(block.props.alt ?? ""),
|
|
4650
|
+
...imageBox(block.props)
|
|
3650
4651
|
}
|
|
3651
4652
|
}),
|
|
3652
4653
|
Table: {
|
|
@@ -5528,6 +6529,183 @@ const resolve = (state, selection) => {
|
|
|
5528
6529
|
};
|
|
5529
6530
|
};
|
|
5530
6531
|
//#endregion
|
|
5531
|
-
|
|
6532
|
+
//#region src/outline.ts
|
|
6533
|
+
/**
|
|
6534
|
+
* The document's outline: its headings in document order, which a table of contents draws, and
|
|
6535
|
+
* the section a selection is in. Both are reads; nothing here changes the document.
|
|
6536
|
+
*/
|
|
6537
|
+
/** Every heading with text, in document order, inside containers too. */
|
|
6538
|
+
const outlineOf = (document) => {
|
|
6539
|
+
const entries = [];
|
|
6540
|
+
eachBlock(document.children, (block) => {
|
|
6541
|
+
if (block.type !== "Heading") return;
|
|
6542
|
+
const text = block.children.map((run) => run.text).join("");
|
|
6543
|
+
if (text.trim() !== "") entries.push({
|
|
6544
|
+
node: block.id,
|
|
6545
|
+
level: block.level,
|
|
6546
|
+
text
|
|
6547
|
+
});
|
|
6548
|
+
});
|
|
6549
|
+
return entries;
|
|
6550
|
+
};
|
|
6551
|
+
/**
|
|
6552
|
+
* The heading of the section the selection starts in: the last heading with text at or before
|
|
6553
|
+
* its block. Undefined with no selection, or before the first heading.
|
|
6554
|
+
*/
|
|
6555
|
+
const sectionAt = (document, selection) => {
|
|
6556
|
+
if (selection?.type !== "Range") return void 0;
|
|
6557
|
+
const start = rangeStart(document, selection);
|
|
6558
|
+
if (start === void 0) return void 0;
|
|
6559
|
+
let last;
|
|
6560
|
+
let found;
|
|
6561
|
+
eachBlock(document.children, (block) => {
|
|
6562
|
+
if (block.type === "Heading" && block.children.some((run) => run.text.trim() !== "")) last = block.id;
|
|
6563
|
+
if (block.children.some((run) => run.id === start.node)) found = last;
|
|
6564
|
+
});
|
|
6565
|
+
return found;
|
|
6566
|
+
};
|
|
6567
|
+
//#endregion
|
|
6568
|
+
//#region src/media.ts
|
|
6569
|
+
/**
|
|
6570
|
+
* Media beside the standard `Image`: a video and a sound played with the browser's own controls,
|
|
6571
|
+
* and a file to download. Opt-in vocabulary like footnotes: add `mediaNodes` to a kit and draw
|
|
6572
|
+
* them with `mediaRendering` under the rendering. Each is an atom holding where its media is
|
|
6573
|
+
* (`src`); where that is, a `data:` URL read from a file or a URL an upload returned, is the
|
|
6574
|
+
* application's to decide.
|
|
6575
|
+
*/
|
|
6576
|
+
const Width = Schema.optional(Schema.Number.check(Schema.isGreaterThan(0)));
|
|
6577
|
+
/** A video, played with the browser's controls, at `width` CSS pixels when it says. */
|
|
6578
|
+
const Video = atom("Video", { Props: Schema.Struct({
|
|
6579
|
+
src: Schema.String,
|
|
6580
|
+
title: Schema.optional(Schema.String),
|
|
6581
|
+
width: Width
|
|
6582
|
+
}) });
|
|
6583
|
+
/** A sound, played with the browser's controls. */
|
|
6584
|
+
const Audio = atom("Audio", { Props: Schema.Struct({
|
|
6585
|
+
src: Schema.String,
|
|
6586
|
+
title: Schema.optional(Schema.String)
|
|
6587
|
+
}) });
|
|
6588
|
+
/** A file to download, under its name, with its size and type when they are known. */
|
|
6589
|
+
const Attachment = atom("Attachment", { Props: Schema.Struct({
|
|
6590
|
+
src: Schema.String,
|
|
6591
|
+
name: Schema.String,
|
|
6592
|
+
size: Schema.optional(Schema.Number.check(Schema.isGreaterThanOrEqualTo(0))),
|
|
6593
|
+
type: Schema.optional(Schema.String)
|
|
6594
|
+
}) });
|
|
6595
|
+
const mediaNodes = [
|
|
6596
|
+
Video,
|
|
6597
|
+
Audio,
|
|
6598
|
+
Attachment
|
|
6599
|
+
];
|
|
6600
|
+
/** Types a page could run as script or markup if one were ever opened rather than saved. */
|
|
6601
|
+
const ACTIVE = /^(text\/html|application\/xhtml\+xml|image\/svg\+xml|text\/xml|application\/xml)\b/i;
|
|
6602
|
+
/**
|
|
6603
|
+
* Where a media element may load from: what the URL policy allows, or a `data:` URL whose type
|
|
6604
|
+
* `accepts` admits. The policy refuses `data:` in general, since a link to one could carry a
|
|
6605
|
+
* page; a video's own bytes cannot.
|
|
6606
|
+
*/
|
|
6607
|
+
const mediaUrl = (value, accepts) => {
|
|
6608
|
+
if (typeof value !== "string") return void 0;
|
|
6609
|
+
const data = /^data:([^;,]*)[;,]/i.exec(value.trim());
|
|
6610
|
+
if (data === null) return safeUrl(value);
|
|
6611
|
+
return accepts(data[1] ?? "") ? value.trim() : void 0;
|
|
6612
|
+
};
|
|
6613
|
+
const text = (value) => typeof value === "string" ? value : "";
|
|
6614
|
+
/** A size as a reader says it: `820 B`, `12 KB`, `3.4 MB`. */
|
|
6615
|
+
const fileSize = (bytes) => {
|
|
6616
|
+
if (bytes < 1024) return `${bytes} B`;
|
|
6617
|
+
if (bytes < 1048576) return `${Math.round(bytes / 1024)} KB`;
|
|
6618
|
+
return `${(bytes / 1048576).toFixed(1)} MB`;
|
|
6619
|
+
};
|
|
6620
|
+
/**
|
|
6621
|
+
* How media draws: a `video` or `audio` with `controls`, which loads only its metadata until it
|
|
6622
|
+
* is played, and an attachment as a download link showing its name and size. A source the policy
|
|
6623
|
+
* refuses leaves the element without one, so it draws nothing playable.
|
|
6624
|
+
*/
|
|
6625
|
+
const mediaRendering = rendering({ nodes: {
|
|
6626
|
+
Video: (block) => {
|
|
6627
|
+
const src = mediaUrl(block.props.src, (type) => type.toLowerCase().startsWith("video/"));
|
|
6628
|
+
const width = block.props.width;
|
|
6629
|
+
return {
|
|
6630
|
+
tag: "video",
|
|
6631
|
+
attributes: {
|
|
6632
|
+
...src === void 0 ? {} : { src },
|
|
6633
|
+
controls: "",
|
|
6634
|
+
preload: "metadata",
|
|
6635
|
+
title: text(block.props.title),
|
|
6636
|
+
...typeof width === "number" ? { width: String(width) } : {}
|
|
6637
|
+
}
|
|
6638
|
+
};
|
|
6639
|
+
},
|
|
6640
|
+
Audio: (block) => {
|
|
6641
|
+
const src = mediaUrl(block.props.src, (type) => type.toLowerCase().startsWith("audio/"));
|
|
6642
|
+
return {
|
|
6643
|
+
tag: "audio",
|
|
6644
|
+
attributes: {
|
|
6645
|
+
...src === void 0 ? {} : { src },
|
|
6646
|
+
controls: "",
|
|
6647
|
+
preload: "metadata",
|
|
6648
|
+
title: text(block.props.title)
|
|
6649
|
+
}
|
|
6650
|
+
};
|
|
6651
|
+
},
|
|
6652
|
+
Attachment: (block) => {
|
|
6653
|
+
const href = mediaUrl(block.props.src, (type) => !ACTIVE.test(type));
|
|
6654
|
+
const name = text(block.props.name);
|
|
6655
|
+
const size = block.props.size;
|
|
6656
|
+
return {
|
|
6657
|
+
tag: "a",
|
|
6658
|
+
attributes: {
|
|
6659
|
+
...href === void 0 ? {} : { href },
|
|
6660
|
+
download: name,
|
|
6661
|
+
"data-attachment": ""
|
|
6662
|
+
},
|
|
6663
|
+
adornments: [{
|
|
6664
|
+
tag: "span",
|
|
6665
|
+
attributes: { "data-attachment-name": "" },
|
|
6666
|
+
text: name
|
|
6667
|
+
}, ...typeof size === "number" ? [{
|
|
6668
|
+
tag: "span",
|
|
6669
|
+
attributes: { "data-attachment-size": "" },
|
|
6670
|
+
text: fileSize(size)
|
|
6671
|
+
}] : []]
|
|
6672
|
+
};
|
|
6673
|
+
}
|
|
6674
|
+
} });
|
|
6675
|
+
/**
|
|
6676
|
+
* The block for a file a reader added: an `Image`, `Video` or `Audio` when its type is one, and
|
|
6677
|
+
* an `Attachment` otherwise. Its identity is a placeholder, which `InsertBlock` replaces.
|
|
6678
|
+
*/
|
|
6679
|
+
const mediaBlock = (file) => {
|
|
6680
|
+
const family = file.type.toLowerCase().split("/")[0];
|
|
6681
|
+
const title = file.name.replace(/\.[^.]+$/, "");
|
|
6682
|
+
const node = (kind, props) => ({
|
|
6683
|
+
type: "Node",
|
|
6684
|
+
kind,
|
|
6685
|
+
id: NodeId.make("media"),
|
|
6686
|
+
props,
|
|
6687
|
+
children: []
|
|
6688
|
+
});
|
|
6689
|
+
if (family === "image") return node("Image", {
|
|
6690
|
+
src: file.src,
|
|
6691
|
+
alt: title
|
|
6692
|
+
});
|
|
6693
|
+
if (family === "video") return node("Video", {
|
|
6694
|
+
src: file.src,
|
|
6695
|
+
title
|
|
6696
|
+
});
|
|
6697
|
+
if (family === "audio") return node("Audio", {
|
|
6698
|
+
src: file.src,
|
|
6699
|
+
title
|
|
6700
|
+
});
|
|
6701
|
+
return node("Attachment", {
|
|
6702
|
+
src: file.src,
|
|
6703
|
+
name: file.name,
|
|
6704
|
+
size: file.size,
|
|
6705
|
+
...file.type === "" ? {} : { type: file.type }
|
|
6706
|
+
});
|
|
6707
|
+
};
|
|
6708
|
+
//#endregion
|
|
6709
|
+
export { ACTION_ATTRIBUTE, ADORNMENT_ATTRIBUTE, Attachment, Audio, Beside, Block, Bold, Code, Container, DefaultDocumentLimits, Document, DocumentLimits, Edit, EditorState, FootnoteDefinition, FootnoteRef, Heading, History, INDENT, Italic, Link, MAX_NORMALIZATION_PASSES, Mark, MarkValue, Node, NodeBlock, NodeId, Operation, Paragraph, Position, replicated_exports as Replicated, RetypeTarget, RunMark, SEARCH_CURRENT_DECORATION, SEARCH_DECORATION, Selection, Slice, Strikethrough, TOGGLES_ATTRIBUTE, Text, TextBlock, Transaction, UnknownBlock, Video, adornmentAttributes, apply, applyInputRules, atom, block, blockAtPath, blockContent, blockKind, blocksAt, blocksOf, canRedo, canUndo, codeDecorations, commit, count, coversText, decodeDocument, decorationsIn, defaultTransforms, deserializeSlice, documentToHtml, documentToText, eachBlock, emptyHistory, emptySlice, fileSize, findMatches, findUnknownMarks, findUnknownNodes, footnoteAt, footnotesOf, groupFor, imageBox, inspect, inspectHistory, inspectKit, isAtom, isBlank, kit, linkAt, locateBlock, locateRun, mapPosition, mark, markExtent, markName, markProps, markRegistry, marksInRange, mediaBlock, mediaNodes, mediaRendering, mergeAdjacentRuns, migrate, migration, moveTargets, nextFootnoteLabel, noRendering, node, nodeProps, nodeRegistry, nodeRendering, outlineOf, plainTextOf, positionInBlock, promoteUnknown, rangeStart, redo, referenceLabel, rendering, renderingOver, resolveInputRules, resolveInsertion, run, runAction, runPieces, runRendering, safeUrl, sameElementRendering, sameMark, sameMarkSet, searchDecorations, sectionAt, selectionIsValid, serializeSlice, shippedMarks, shippedRegistry, siblingsOf, sliceFromText, sliceOf, standardMarks, standardNodes, standardRendering, tableAt, takesTab, textBefore, textBlockAt, textContent, textRangeBefore, toHtml, toText, unchanged, undo, unionChangeSet, validate, withFreshIds };
|
|
5532
6710
|
|
|
5533
6711
|
//# sourceMappingURL=index.mjs.map
|