@quillmark/svelte 0.29.3 → 0.31.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 +3 -3
- package/dist/core/address.d.ts +3 -3
- package/dist/core/address.js +1 -1
- package/dist/core/codec/decode.d.ts +4 -5
- package/dist/core/codec/decode.js +151 -208
- package/dist/core/codec/encode.d.ts +13 -35
- package/dist/core/codec/encode.js +127 -408
- package/dist/core/codec/field.d.ts +11 -10
- package/dist/core/codec/field.js +190 -92
- package/dist/core/codec/index.d.ts +2 -2
- package/dist/core/codec/index.js +4 -4
- package/dist/core/codec/islands.d.ts +13 -9
- package/dist/core/codec/islands.js +54 -49
- package/dist/core/codec/marks.d.ts +12 -18
- package/dist/core/codec/marks.js +16 -26
- package/dist/core/codec/positions.d.ts +25 -7
- package/dist/core/codec/positions.js +43 -50
- package/dist/core/codec/prose.css +0 -8
- package/dist/core/codec/schema.d.ts +3 -3
- package/dist/core/codec/schema.js +29 -11
- package/dist/core/codec/slash.d.ts +4 -2
- package/dist/core/codec/slash.js +13 -3
- package/dist/core/codec/table-copy.d.ts +3 -3
- package/dist/core/codec/table-copy.js +6 -4
- package/dist/core/codec/table-paste.js +13 -9
- package/dist/core/codec/table-view.js +19 -12
- package/dist/core/codec/table.d.ts +77 -43
- package/dist/core/codec/table.js +157 -48
- package/dist/core/index.d.ts +1 -0
- package/dist/core/theme.css +14 -6
- package/dist/visual/ArrayField.svelte +168 -83
- package/dist/visual/BooleanField.svelte +2 -2
- package/dist/visual/Card.svelte +65 -44
- package/dist/visual/DateField.svelte +16 -1
- package/dist/visual/DiagnosticList.svelte +34 -2
- package/dist/visual/EnumField.svelte +58 -44
- package/dist/visual/Field.svelte +13 -3
- package/dist/visual/Field.svelte.d.ts +3 -1
- package/dist/visual/FieldHint.svelte +10 -3
- package/dist/visual/FormatPopover.svelte +1 -1
- package/dist/visual/MatrixAdd.svelte +3 -18
- package/dist/visual/MatrixField.svelte +20 -12
- package/dist/visual/ObjectField.svelte +164 -145
- package/dist/visual/ProseField.svelte +6 -1
- package/dist/visual/ProseValue.svelte +8 -8
- package/dist/visual/SlashMenu.svelte +24 -12
- package/dist/visual/SlashMenu.svelte.d.ts +2 -0
- package/dist/visual/VisualEditor.svelte +1 -1
- package/dist/visual/VisualEditor.svelte.d.ts +1 -1
- package/dist/visual/VisualEditorInner.svelte +159 -18
- package/dist/visual/VisualEditorInner.svelte.d.ts +1 -1
- package/dist/visual/controls.css +42 -4
- package/dist/visual/leaves.d.ts +1 -1
- package/dist/visual/leaves.js +1 -1
- package/dist/visual/strings.d.ts +27 -10
- package/dist/visual/strings.js +40 -6
- package/dist/visual/structure.d.ts +17 -9
- package/dist/visual/structure.js +20 -26
- package/dist/visual/undo.svelte.d.ts +36 -0
- package/dist/visual/undo.svelte.js +119 -0
- package/package.json +2 -2
- package/dist/visual/hold.d.ts +0 -17
- package/dist/visual/hold.js +0 -39
package/README.md
CHANGED
|
@@ -39,7 +39,7 @@ const quill = Quill.fromTree(tree); // tree: Map<string, Uint8Array> of the quil
|
|
|
39
39
|
const doc = quill.seedDocument();
|
|
40
40
|
|
|
41
41
|
// An EXISTING document: parsed back from what you stored.
|
|
42
|
-
const doc = Document.
|
|
42
|
+
const doc = Document.parse(markdown); // canonical Quillmark markdown
|
|
43
43
|
const doc = Document.fromStored(json); // the versioned storage DTO (`doc.toStored()`)
|
|
44
44
|
|
|
45
45
|
const session = await new Engine().open(quill, doc);
|
|
@@ -53,7 +53,7 @@ const session = await new Engine().open(quill, doc);
|
|
|
53
53
|
A stored document carries none of its quill's bytes, only a reference: `doc.quillRef` is `name@version`, persisted in the markdown itself. Resolution is **host code**: read the ref, map it to a `Quill`, open. No surface resolves, so one resolution per document holds by construction.
|
|
54
54
|
|
|
55
55
|
```ts
|
|
56
|
-
const doc = Document.
|
|
56
|
+
const doc = Document.parse(stored); // Document, from the gate above
|
|
57
57
|
const quill = await registry.getQuill(doc.quillRef); // your ref → Quill mapping
|
|
58
58
|
const session = await new Engine().open(quill, doc);
|
|
59
59
|
```
|
|
@@ -112,7 +112,7 @@ In Svelte, `<Preview {session} onPick={…} />` exposes the same verbs (`refresh
|
|
|
112
112
|
/* the active leaf: `active.field` (a DocPath), and `active.cardId` for its card */
|
|
113
113
|
}}
|
|
114
114
|
onCaretMove={(at) => {
|
|
115
|
-
/* the caret moved to `at.field` (a DocPath) at `at.pos` (
|
|
115
|
+
/* the caret moved to `at.field` (a DocPath) at `at.pos` (UTF-16) */
|
|
116
116
|
}}
|
|
117
117
|
onError={(err) => {
|
|
118
118
|
/* a failure the editor recovered from; editing continues */
|
package/dist/core/address.d.ts
CHANGED
|
@@ -9,12 +9,12 @@ export type DocPath = string;
|
|
|
9
9
|
/**
|
|
10
10
|
* A place in the document: a field and a caret within it. What the editor reports when its caret
|
|
11
11
|
* moves and what the preview scrolls to, so `onCaretMove={preview.focusPosition}` translates
|
|
12
|
-
* nothing. `pos` is
|
|
12
|
+
* nothing. `pos` is a UTF-16 offset, the coordinate shared on both sides of the boundary; a `ContentHit` is a
|
|
13
13
|
* `Place` with extras.
|
|
14
14
|
*/
|
|
15
15
|
export interface Place {
|
|
16
16
|
field: DocPath;
|
|
17
|
-
/** The caret
|
|
17
|
+
/** The caret, a UTF-16 offset. */
|
|
18
18
|
pos: number;
|
|
19
19
|
}
|
|
20
20
|
/**
|
|
@@ -25,7 +25,7 @@ export interface Place {
|
|
|
25
25
|
*/
|
|
26
26
|
export interface Landing {
|
|
27
27
|
field: DocPath;
|
|
28
|
-
/** The caret
|
|
28
|
+
/** The caret, a UTF-16 offset, absent on the placement rung. */
|
|
29
29
|
pos?: number;
|
|
30
30
|
/** Whether `pos` is cluster-exact or floored to a segment start; the boundary's
|
|
31
31
|
* own marker, carried through untouched. */
|
package/dist/core/address.js
CHANGED
|
@@ -126,7 +126,7 @@ function addrForSegs(segs) {
|
|
|
126
126
|
function cardHead(index, kinds) {
|
|
127
127
|
if (index == null)
|
|
128
128
|
return { seg: 'main' };
|
|
129
|
-
if (!Number.isInteger(index) || index < 0 || index >= kinds.length)
|
|
129
|
+
if (typeof index !== 'number' || !Number.isInteger(index) || index < 0 || index >= kinds.length)
|
|
130
130
|
return undefined;
|
|
131
131
|
return { seg: 'card', kind: kinds[index] || null, index };
|
|
132
132
|
}
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { type Node as PMNode, type Schema } from 'prosemirror-model';
|
|
2
2
|
import type { Content } from '@quillmark/wasm';
|
|
3
|
-
/**
|
|
4
|
-
export declare function codePoints(s: string): string[];
|
|
5
|
-
/** USV length of `s` (code points, not UTF-16 units). */
|
|
3
|
+
/** USV length of `s` (code points, not UTF-16 units): what a character cap counts. */
|
|
6
4
|
export declare function usvLength(s: string): number;
|
|
7
5
|
/**
|
|
8
6
|
* A `Content` as read-only DOM: decode under `schema`, then the nodes' own `toDOM`, for chrome that
|
|
@@ -14,8 +12,9 @@ export declare function renderContent(rt: Content, schema: Schema): Node;
|
|
|
14
12
|
export declare function decode(rt: Content, schema: Schema): PMNode;
|
|
15
13
|
/**
|
|
16
14
|
* Whether `rt` decodes under an inline schema with nothing but whitespace dropped: upstream's
|
|
17
|
-
* one-line rule, or one plain line and the
|
|
18
|
-
* scalar keeps in a `text` value). Marks are the
|
|
15
|
+
* one-line rule, or one plain line and the break a trailing `\n` spells (the one a YAML `|`
|
|
16
|
+
* scalar keeps in a `text` value), or the empty line a trailing blank line opens. Marks are the
|
|
17
|
+
* schema's business.
|
|
19
18
|
*/
|
|
20
19
|
export declare function fitsInline(rt: Content): boolean;
|
|
21
20
|
/**
|
|
@@ -1,19 +1,15 @@
|
|
|
1
|
-
// Decode: content → PM, a pure function (CODEC §Decode).
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
// over their `[start,end)`
|
|
5
|
-
// decorations (field.ts).
|
|
6
|
-
//
|
|
1
|
+
// Decode: content → PM, a pure function (CODEC §Decode). Each `\n`-separated segment is one
|
|
2
|
+
// block, its record's `kind` selecting the node; blocks nest by the containers their paths name,
|
|
3
|
+
// a run of blocks sharing a container id being that container; a `U+2028` is a break inside a
|
|
4
|
+
// block; marks apply over their `[start,end)` ranges; island slots lower to leaf nodes. Anchors are
|
|
5
|
+
// decorations (field.ts). Offsets are UTF-16 code units, JavaScript string indices, so a PM text
|
|
6
|
+
// run and its content span advance together.
|
|
7
7
|
import { DOMSerializer } from 'prosemirror-model';
|
|
8
|
-
import { ISLAND_SLOT, isBlockIsland, isEditedIsland } from './islands.js';
|
|
8
|
+
import { HARD_BREAK, ISLAND_SLOT, isBlockIsland, isEditedIsland } from './islands.js';
|
|
9
9
|
import { descriptorOf, markKey, pmMarkFromContent } from './marks.js';
|
|
10
10
|
import { core } from '../lifecycle.js';
|
|
11
11
|
import { hasMarks, isBlockSchema, isInlineSchema, takesLineBreak } from './schema.js';
|
|
12
|
-
/**
|
|
13
|
-
export function codePoints(s) {
|
|
14
|
-
return Array.from(s);
|
|
15
|
-
}
|
|
16
|
-
/** USV length of `s` (code points, not UTF-16 units). */
|
|
12
|
+
/** USV length of `s` (code points, not UTF-16 units): what a character cap counts. */
|
|
17
13
|
export function usvLength(s) {
|
|
18
14
|
let n = 0;
|
|
19
15
|
for (const _ of s)
|
|
@@ -28,55 +24,48 @@ export function usvLength(s) {
|
|
|
28
24
|
export function renderContent(rt, schema) {
|
|
29
25
|
return DOMSerializer.fromSchema(schema).serializeFragment(decode(rt, schema).content);
|
|
30
26
|
}
|
|
27
|
+
/** Each block of `rt` with its text and start. */
|
|
28
|
+
function segmentsOf(rt) {
|
|
29
|
+
const texts = rt.text.split('\n');
|
|
30
|
+
const out = [];
|
|
31
|
+
let at = 0;
|
|
32
|
+
texts.forEach((text, i) => {
|
|
33
|
+
out.push({ block: rt.blocks[i] ?? { id: '', kind: 'para' }, text, start: at });
|
|
34
|
+
at += text.length + 1;
|
|
35
|
+
});
|
|
36
|
+
return out;
|
|
37
|
+
}
|
|
31
38
|
/** Decode a `Content` to a PM document under `schema`. */
|
|
32
39
|
export function decode(rt, schema) {
|
|
33
|
-
const
|
|
34
|
-
// Per-line USV start: line i begins after all earlier lines and their `\n`s.
|
|
35
|
-
const starts = [];
|
|
36
|
-
let acc = 0;
|
|
37
|
-
for (let i = 0; i < lineTexts.length; i++) {
|
|
38
|
-
starts.push(acc);
|
|
39
|
-
acc += usvLength(lineTexts[i]) + 1; // +1 for the `\n` boundary
|
|
40
|
-
}
|
|
40
|
+
const segments = segmentsOf(rt);
|
|
41
41
|
// A mark-free schema has nothing to apply marks with, and a `text` field that acquired one
|
|
42
42
|
// anyway (a hand-written document) must open and heal on the next commit, not throw.
|
|
43
|
-
const marks = hasMarks(schema) ? rt.marks
|
|
43
|
+
const marks = hasMarks(schema) ? (rt.marks ?? []) : [];
|
|
44
44
|
const cursor = { i: 0, rt };
|
|
45
|
-
if (isInlineSchema(schema))
|
|
46
|
-
return decodeInline(schema,
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
// Build leaves (fold `continues` runs), then nest by container prefix.
|
|
52
|
-
const leaves = [];
|
|
53
|
-
for (let i = 0; i < rt.lines.length; i++) {
|
|
54
|
-
const line = rt.lines[i];
|
|
55
|
-
const seg = { text: lineTexts[i] ?? '', startUSV: starts[i] };
|
|
56
|
-
if (line.continues && leaves.length > 0) {
|
|
57
|
-
leaves[leaves.length - 1].segments.push(seg);
|
|
58
|
-
}
|
|
59
|
-
else {
|
|
60
|
-
leaves.push({ line, containers: line.containers, segments: [seg] });
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
const blocks = groupBlocks(schema, leaves, 0, marks, cursor);
|
|
45
|
+
if (isInlineSchema(schema))
|
|
46
|
+
return decodeInline(schema, segments, marks, cursor);
|
|
47
|
+
if (!isBlockSchema(schema))
|
|
48
|
+
return decodePlain(schema, segments, marks, cursor);
|
|
49
|
+
const containers = new Map((rt.containers ?? []).map((c) => [c.id, c]));
|
|
50
|
+
const blocks = groupBlocks(schema, segments, 0, containers, marks, cursor);
|
|
64
51
|
return schema.nodes.doc.create(null, blocks.length ? blocks : schema.nodes.paragraph.create());
|
|
65
52
|
}
|
|
66
53
|
/**
|
|
67
54
|
* Whether `rt` decodes under an inline schema with nothing but whitespace dropped: upstream's
|
|
68
|
-
* one-line rule, or one plain line and the
|
|
69
|
-
* scalar keeps in a `text` value). Marks are the
|
|
55
|
+
* one-line rule, or one plain line and the break a trailing `\n` spells (the one a YAML `|`
|
|
56
|
+
* scalar keeps in a `text` value), or the empty line a trailing blank line opens. Marks are the
|
|
57
|
+
* schema's business.
|
|
70
58
|
*/
|
|
71
59
|
export function fitsInline(rt) {
|
|
72
60
|
if (core().isInline(rt))
|
|
73
61
|
return true;
|
|
74
|
-
const plain = (
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
rt.text.endsWith(
|
|
62
|
+
const plain = (b) => b.kind === 'para' && !b.path?.length;
|
|
63
|
+
if (rt.islands?.length || !rt.blocks.every(plain))
|
|
64
|
+
return false;
|
|
65
|
+
const breaks = rt.text.split(HARD_BREAK).length - 1;
|
|
66
|
+
if (rt.blocks.length === 1)
|
|
67
|
+
return breaks === 1 && rt.text.endsWith(HARD_BREAK);
|
|
68
|
+
return rt.blocks.length === 2 && rt.text.endsWith('\n') && breaks === 0;
|
|
80
69
|
}
|
|
81
70
|
/**
|
|
82
71
|
* Whether `rt` decodes under `plainSchema` losing nothing but its marks, which the next commit
|
|
@@ -97,186 +86,129 @@ export function fitsLeaf(rt, leaf) {
|
|
|
97
86
|
return fitsInline(rt);
|
|
98
87
|
if (leaf.plaintext)
|
|
99
88
|
return fitsPlain(rt);
|
|
100
|
-
|
|
89
|
+
const islands = rt.islands ?? [];
|
|
90
|
+
if (!leaf.islands && islands.length)
|
|
101
91
|
return false;
|
|
102
|
-
return
|
|
92
|
+
return islands.every((isl) => isEditedIsland(isl.type));
|
|
103
93
|
}
|
|
104
94
|
/** Inline decode: one paragraph, containers and islands stripped. */
|
|
105
|
-
function decodeInline(schema,
|
|
106
|
-
// An inline field is single-line: stray
|
|
107
|
-
//
|
|
108
|
-
// inline: the slot char is
|
|
95
|
+
function decodeInline(schema, segments, marks, cursor) {
|
|
96
|
+
// An inline field is single-line: stray blocks and breaks join with a space. The break a
|
|
97
|
+
// trailing `\n` spells, and the empty block a trailing blank line opens, are dropped, so a first
|
|
98
|
+
// edit stores no space nobody typed. Islands are not representable inline: the slot char is
|
|
99
|
+
// dropped and its entry skipped.
|
|
109
100
|
const inline = [];
|
|
110
|
-
const last =
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
101
|
+
const last = segments.length - 1;
|
|
102
|
+
segments.forEach((at, i) => {
|
|
103
|
+
const seg = i === last && at.text.endsWith(HARD_BREAK) ? { ...at, text: at.text.slice(0, -1) } : at;
|
|
104
|
+
if (i > 0 && i === last && seg.text === '')
|
|
105
|
+
return;
|
|
114
106
|
if (i > 0)
|
|
115
107
|
inline.push(schema.text(' '));
|
|
116
|
-
inline.push(...buildInline(schema,
|
|
117
|
-
}
|
|
108
|
+
inline.push(...buildInline(schema, seg, marks, cursor, 'inline'));
|
|
109
|
+
});
|
|
118
110
|
const para = schema.nodes.paragraph.create(null, inline);
|
|
119
111
|
return schema.nodes.doc.create(null, para);
|
|
120
112
|
}
|
|
121
113
|
/**
|
|
122
|
-
* Plain decode: a
|
|
123
|
-
*
|
|
124
|
-
* opens flattened.
|
|
114
|
+
* Plain decode: every block a paragraph, a break a `hard_break` in it. Block kinds, containers and
|
|
115
|
+
* islands are dropped, so content {@link fitsPlain} refuses opens flattened.
|
|
125
116
|
*/
|
|
126
|
-
function decodePlain(
|
|
127
|
-
const
|
|
128
|
-
for (let i = 0; i < rt.lines.length; i++) {
|
|
129
|
-
const inline = buildInline(schema, lineTexts[i] ?? '', starts[i], marks, cursor, true);
|
|
130
|
-
const open = paras[paras.length - 1];
|
|
131
|
-
if (rt.lines[i].continues && open)
|
|
132
|
-
open.push(schema.nodes.hard_break.create(), ...inline);
|
|
133
|
-
else
|
|
134
|
-
paras.push(inline);
|
|
135
|
-
}
|
|
136
|
-
const blocks = paras.map((inline) => schema.nodes.paragraph.create(null, inline));
|
|
117
|
+
function decodePlain(schema, segments, marks, cursor) {
|
|
118
|
+
const blocks = segments.map((seg) => schema.nodes.paragraph.create(null, buildInline(schema, seg, marks, cursor, 'plain')));
|
|
137
119
|
return schema.nodes.doc.create(null, blocks.length ? blocks : schema.nodes.paragraph.create());
|
|
138
120
|
}
|
|
139
|
-
/** Nest
|
|
140
|
-
function groupBlocks(schema,
|
|
121
|
+
/** Nest the blocks whose paths agree to `depth` into nodes, one per child of that prefix. */
|
|
122
|
+
function groupBlocks(schema, segments, depth, containers, marks, cursor) {
|
|
141
123
|
const out = [];
|
|
142
124
|
let i = 0;
|
|
143
|
-
while (i <
|
|
144
|
-
const path =
|
|
125
|
+
while (i < segments.length) {
|
|
126
|
+
const path = segments[i].block.path ?? [];
|
|
145
127
|
if (path.length <= depth) {
|
|
146
|
-
out.push(makeLeaf(schema,
|
|
128
|
+
out.push(makeLeaf(schema, segments[i], marks, cursor));
|
|
147
129
|
i++;
|
|
148
130
|
continue;
|
|
149
131
|
}
|
|
150
|
-
const
|
|
151
|
-
if (here.container === 'list_item') {
|
|
152
|
-
// Gather the maximal run of sibling `list_item` leaves at this depth
|
|
153
|
-
// (same ordered/start), then split it into items by `ordinal`.
|
|
154
|
-
const { ordered, start } = here.attrs;
|
|
155
|
-
const instance = instanceOf(here);
|
|
156
|
-
let j = i + 1;
|
|
157
|
-
while (j < leaves.length) {
|
|
158
|
-
const c = atDepth(leaves[j], depth);
|
|
159
|
-
if (!c || c.container !== 'list_item')
|
|
160
|
-
break;
|
|
161
|
-
if (c.attrs.ordered !== ordered || c.attrs.start !== start)
|
|
162
|
-
break;
|
|
163
|
-
// `instance` is the boundary between two adjacent lists; the normalizer
|
|
164
|
-
// numbers a run's `ordinal`s gaplessly from 0, so a reset carries none.
|
|
165
|
-
if (instanceOf(c) !== instance)
|
|
166
|
-
break;
|
|
167
|
-
j++;
|
|
168
|
-
}
|
|
169
|
-
const run = leaves.slice(i, j);
|
|
170
|
-
const items = [];
|
|
171
|
-
let k = 0;
|
|
172
|
-
while (k < run.length) {
|
|
173
|
-
const ord = ordinalAt(run[k], depth);
|
|
174
|
-
let l = k + 1;
|
|
175
|
-
while (l < run.length && ordinalAt(run[l], depth) === ord)
|
|
176
|
-
l++;
|
|
177
|
-
items.push(schema.nodes.list_item.create({ checked: checkedAt(run[k], depth) }, groupBlocks(schema, run.slice(k, l), depth + 1, marks, cursor)));
|
|
178
|
-
k = l;
|
|
179
|
-
}
|
|
180
|
-
const listType = ordered ? schema.nodes.ordered_list : schema.nodes.bullet_list;
|
|
181
|
-
out.push(listType.create(ordered ? { start } : null, items));
|
|
182
|
-
i = j;
|
|
183
|
-
continue;
|
|
184
|
-
}
|
|
185
|
-
// The quote, and the whole of what is left: `satisfies` makes a container added upstream a
|
|
186
|
-
// compile error. A wrapper over the run of leaves carrying the identical container
|
|
187
|
-
// (`containerKey`).
|
|
188
|
-
here.container;
|
|
189
|
-
const key = containerKey(here);
|
|
132
|
+
const id = path[depth];
|
|
190
133
|
let j = i + 1;
|
|
191
|
-
while (j <
|
|
192
|
-
const c = atDepth(leaves[j], depth);
|
|
193
|
-
if (!c || containerKey(c) !== key)
|
|
194
|
-
break;
|
|
134
|
+
while (j < segments.length && segments[j].block.path?.[depth] === id)
|
|
195
135
|
j++;
|
|
136
|
+
const run = segments.slice(i, j);
|
|
137
|
+
const container = containers.get(id);
|
|
138
|
+
const inner = (d) => groupBlocks(schema, run, d, containers, marks, cursor);
|
|
139
|
+
switch (container?.kind) {
|
|
140
|
+
case 'list': {
|
|
141
|
+
const items = groupItems(schema, run, depth + 1, containers, marks, cursor);
|
|
142
|
+
out.push(container.attrs.ordered
|
|
143
|
+
? schema.nodes.ordered_list.create({ id, start: container.attrs.start }, items)
|
|
144
|
+
: schema.nodes.bullet_list.create({ id }, items));
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
case 'quote':
|
|
148
|
+
out.push(schema.nodes.blockquote.create({ id }, inner(depth + 1)));
|
|
149
|
+
break;
|
|
150
|
+
default:
|
|
151
|
+
// An item outside a list or a container no record names: a value nothing renders, so its
|
|
152
|
+
// blocks decode in place and the projection settles it.
|
|
153
|
+
out.push(...inner(depth + 1));
|
|
196
154
|
}
|
|
197
|
-
const inner = groupBlocks(schema, leaves.slice(i, j), depth + 1, marks, cursor);
|
|
198
|
-
out.push(schema.nodes.blockquote.create(null, inner));
|
|
199
155
|
i = j;
|
|
200
156
|
}
|
|
201
157
|
return out;
|
|
202
158
|
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
return
|
|
219
|
-
}
|
|
220
|
-
/**
|
|
221
|
-
* A container's `instance`, absent meaning the zero a canonical read omits. It tells one container
|
|
222
|
-
* from an adjacent sibling of identical shape, so absent and `0` answer alike.
|
|
223
|
-
*/
|
|
224
|
-
function instanceOf(c) {
|
|
225
|
-
return c.instance ?? 0;
|
|
226
|
-
}
|
|
227
|
-
/** Identity of a container for run gathering: its name and its `instance`, NUL-joined
|
|
228
|
-
* as `markKey` joins a mark's. `list_item` carries its own run rule, so the rest are what
|
|
229
|
-
* key here. */
|
|
230
|
-
function containerKey(c) {
|
|
231
|
-
return `${c.container}\u0000${instanceOf(c)}`;
|
|
159
|
+
/** The items of one list: a `list_item` per run of blocks sharing the item id at `depth`. */
|
|
160
|
+
function groupItems(schema, run, depth, containers, marks, cursor) {
|
|
161
|
+
const items = [];
|
|
162
|
+
let i = 0;
|
|
163
|
+
while (i < run.length) {
|
|
164
|
+
const id = run[i].block.path?.[depth];
|
|
165
|
+
let j = i + 1;
|
|
166
|
+
while (j < run.length && run[j].block.path?.[depth] === id)
|
|
167
|
+
j++;
|
|
168
|
+
const item = id === undefined ? undefined : containers.get(id);
|
|
169
|
+
const checked = item?.kind === 'item' ? (item.attrs?.checked ?? null) : null;
|
|
170
|
+
const body = groupBlocks(schema, run.slice(i, j), depth + 1, containers, marks, cursor);
|
|
171
|
+
items.push(schema.nodes.list_item.create({ id: id ?? null, checked }, body));
|
|
172
|
+
i = j;
|
|
173
|
+
}
|
|
174
|
+
return items;
|
|
232
175
|
}
|
|
233
176
|
/**
|
|
234
|
-
* A single leaf block node from its
|
|
235
|
-
* added upstream is a compile error.
|
|
177
|
+
* A single leaf block node from its record, exhaustive over the closed block vocabulary so a kind
|
|
178
|
+
* added upstream is a compile error. A kind spelled `x-` reads as a paragraph that holds its kind.
|
|
236
179
|
*/
|
|
237
|
-
function makeLeaf(schema,
|
|
238
|
-
const
|
|
239
|
-
|
|
180
|
+
function makeLeaf(schema, seg, marks, cursor) {
|
|
181
|
+
const block = seg.block;
|
|
182
|
+
const id = block.id || null;
|
|
183
|
+
switch (block.kind) {
|
|
240
184
|
case 'rule':
|
|
241
|
-
return schema.nodes.horizontal_rule.create();
|
|
185
|
+
return schema.nodes.horizontal_rule.create({ id });
|
|
242
186
|
case 'code': {
|
|
243
|
-
|
|
244
|
-
const text = leaf.segments.map((s) => s.text).join('\n');
|
|
187
|
+
const text = seg.text.replaceAll(HARD_BREAK, '\n');
|
|
245
188
|
const content = text.length ? [schema.text(text)] : [];
|
|
246
|
-
return schema.nodes.code_block.create({ lang:
|
|
189
|
+
return schema.nodes.code_block.create({ id, lang: block.attrs?.lang ?? null }, content);
|
|
247
190
|
}
|
|
248
|
-
case '
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
// no continuation, so the space keeps hand-built content decodable.
|
|
255
|
-
const type = line.kind === 'heading' ? schema.nodes.heading : schema.nodes.paragraph;
|
|
256
|
-
const inline = [];
|
|
257
|
-
leaf.segments.forEach((seg, idx) => {
|
|
258
|
-
if (idx > 0)
|
|
259
|
-
inline.push(takesLineBreak(type) ? schema.nodes.hard_break.create() : schema.text(' '));
|
|
260
|
-
inline.push(...buildInline(schema, seg.text, seg.startUSV, marks, cursor, false));
|
|
261
|
-
});
|
|
262
|
-
return line.kind === 'heading'
|
|
263
|
-
? type.create({ level: line.attrs.level }, inline)
|
|
264
|
-
: type.create(null, inline);
|
|
191
|
+
case 'heading':
|
|
192
|
+
return schema.nodes.heading.create({ id, level: block.attrs.level }, buildInline(schema, seg, marks, cursor, 'heading'));
|
|
193
|
+
case 'para': {
|
|
194
|
+
if (blockIslandSegment(seg, cursor))
|
|
195
|
+
return schema.nodes.island_block.create({ ...islandAttrs(cursor), block: id });
|
|
196
|
+
return schema.nodes.paragraph.create({ id }, buildInline(schema, seg, marks, cursor, 'paragraph'));
|
|
265
197
|
}
|
|
266
198
|
default: {
|
|
267
|
-
const
|
|
268
|
-
return
|
|
199
|
+
const ext = { kind: block.kind, attrs: block.attrs ?? {} };
|
|
200
|
+
return schema.nodes.paragraph.create({ id, ext }, buildInline(schema, seg, marks, cursor, 'paragraph'));
|
|
269
201
|
}
|
|
270
202
|
}
|
|
271
203
|
}
|
|
272
204
|
/**
|
|
273
|
-
* Whether a `para`
|
|
274
|
-
*
|
|
205
|
+
* Whether a `para` block is one block island: its slot alone, backed by an island whose markup is a
|
|
206
|
+
* block.
|
|
275
207
|
*/
|
|
276
|
-
function
|
|
277
|
-
if (
|
|
208
|
+
function blockIslandSegment(seg, cursor) {
|
|
209
|
+
if (seg.text !== ISLAND_SLOT)
|
|
278
210
|
return false;
|
|
279
|
-
const isl = cursor.rt.islands[cursor.i];
|
|
211
|
+
const isl = cursor.rt.islands?.[cursor.i];
|
|
280
212
|
return isl != null && isBlockIsland(isl.type);
|
|
281
213
|
}
|
|
282
214
|
/**
|
|
@@ -285,17 +217,21 @@ function blockIslandLine(leaf, cursor) {
|
|
|
285
217
|
* content): the slot draws nothing, keeping the decode total.
|
|
286
218
|
*/
|
|
287
219
|
function islandAttrs(cursor) {
|
|
288
|
-
const isl = cursor.rt.islands[cursor.i++];
|
|
289
|
-
return isl
|
|
220
|
+
const isl = cursor.rt.islands?.[cursor.i++];
|
|
221
|
+
return isl
|
|
222
|
+
? { id: isl.id, islandType: isl.type, props: isl.props, slots: isl.slots ?? null }
|
|
223
|
+
: null;
|
|
290
224
|
}
|
|
291
225
|
/**
|
|
292
226
|
* Inline nodes for one segment: split text into maximal runs of a constant mark set, lower each
|
|
293
|
-
* `U+FFFC` to an inline island node
|
|
294
|
-
*
|
|
227
|
+
* `U+FFFC` to an inline island node and each `U+2028` to a `hard_break`, or to a space in a host
|
|
228
|
+
* that takes none (`schema.ts` §`takesLineBreak`). An inline host drops slots, keeping the island
|
|
229
|
+
* cursor in step.
|
|
295
230
|
*/
|
|
296
|
-
function buildInline(schema,
|
|
297
|
-
const cps = codePoints(segText);
|
|
231
|
+
function buildInline(schema, seg, marks, cursor, host) {
|
|
298
232
|
const out = [];
|
|
233
|
+
const breaks = host !== 'inline' &&
|
|
234
|
+
takesLineBreak(host === 'heading' ? schema.nodes.heading : schema.nodes.paragraph);
|
|
299
235
|
let runText = '';
|
|
300
236
|
let runKey = '';
|
|
301
237
|
let runMarks = [];
|
|
@@ -305,30 +241,37 @@ function buildInline(schema, segText, segStartUSV, marks, cursor, stripIslands)
|
|
|
305
241
|
runText = '';
|
|
306
242
|
};
|
|
307
243
|
// A mark set changes only where a mark starts or ends; `marks` is the whole list, so resolving
|
|
308
|
-
// per
|
|
309
|
-
const
|
|
310
|
-
const
|
|
244
|
+
// per character would cost the text against every mark.
|
|
245
|
+
const segStart = seg.start;
|
|
246
|
+
const segEnd = segStart + seg.text.length;
|
|
247
|
+
const spanning = marks.filter((m) => m.start < segEnd && m.end > segStart);
|
|
311
248
|
const edges = new Set();
|
|
312
249
|
for (const m of spanning) {
|
|
313
|
-
if (m.start >
|
|
250
|
+
if (m.start > segStart)
|
|
314
251
|
edges.add(m.start);
|
|
315
|
-
if (m.end <
|
|
252
|
+
if (m.end < segEnd)
|
|
316
253
|
edges.add(m.end);
|
|
317
254
|
}
|
|
318
255
|
let active = [];
|
|
319
256
|
let key = '';
|
|
320
257
|
let unresolved = true;
|
|
321
|
-
for (let k = 0; k <
|
|
322
|
-
const
|
|
323
|
-
const pos =
|
|
324
|
-
if (
|
|
258
|
+
for (let k = 0; k < seg.text.length; k++) {
|
|
259
|
+
const ch = seg.text[k];
|
|
260
|
+
const pos = segStart + k;
|
|
261
|
+
if (ch === ISLAND_SLOT) {
|
|
325
262
|
flush();
|
|
326
263
|
const attrs = islandAttrs(cursor);
|
|
327
|
-
if (attrs &&
|
|
264
|
+
if (attrs && host !== 'inline' && host !== 'plain' && schema.nodes.island_inline)
|
|
328
265
|
out.push(schema.nodes.island_inline.create(attrs));
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
266
|
+
runKey = '\0slot';
|
|
267
|
+
unresolved = true;
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
if (ch === HARD_BREAK && breaks) {
|
|
271
|
+
flush();
|
|
272
|
+
out.push(schema.nodes.hard_break.create());
|
|
273
|
+
runKey = '\0break';
|
|
274
|
+
unresolved = true;
|
|
332
275
|
continue;
|
|
333
276
|
}
|
|
334
277
|
if (unresolved || edges.has(pos)) {
|
|
@@ -342,7 +285,7 @@ function buildInline(schema, segText, segStartUSV, marks, cursor, stripIslands)
|
|
|
342
285
|
runKey = key;
|
|
343
286
|
runMarks = buildMarkSet(schema, active);
|
|
344
287
|
}
|
|
345
|
-
runText +=
|
|
288
|
+
runText += ch === HARD_BREAK ? ' ' : ch;
|
|
346
289
|
}
|
|
347
290
|
flush();
|
|
348
291
|
return out;
|
|
@@ -355,7 +298,7 @@ function markSetKey(active) {
|
|
|
355
298
|
// groups by. JSON-joined so a `link:a|strong` url stays distinct from `{link:a} + {strong}`.
|
|
356
299
|
return JSON.stringify(active.map((m) => markKey(descriptorOf(m))).sort());
|
|
357
300
|
}
|
|
358
|
-
/** The PM mark array for an active content mark set
|
|
301
|
+
/** The PM mark array for an active content mark set. */
|
|
359
302
|
function buildMarkSet(schema, active) {
|
|
360
303
|
let set = [];
|
|
361
304
|
for (const m of active) {
|
|
@@ -1,28 +1,27 @@
|
|
|
1
1
|
import type { Node as PMNode } from 'prosemirror-model';
|
|
2
|
-
import type {
|
|
2
|
+
import type { Change, Content, ContentAnchor, ContentBlock, ContentContainer, ContentIsland, ContentMark } from '@quillmark/wasm';
|
|
3
3
|
export type PosRun = {
|
|
4
4
|
kind: 'text';
|
|
5
5
|
pmStart: number;
|
|
6
|
-
|
|
6
|
+
at: number;
|
|
7
7
|
s: string;
|
|
8
8
|
} | {
|
|
9
9
|
kind: 'nl';
|
|
10
10
|
pmStart: number;
|
|
11
11
|
pmEnd: number;
|
|
12
|
-
|
|
12
|
+
at: number;
|
|
13
13
|
} | {
|
|
14
14
|
kind: 'atom';
|
|
15
15
|
pmStart: number;
|
|
16
|
-
|
|
16
|
+
at: number;
|
|
17
17
|
};
|
|
18
18
|
export interface Scan {
|
|
19
19
|
text: string;
|
|
20
|
-
|
|
20
|
+
blocks: ContentBlock[];
|
|
21
|
+
containers: ContentContainer[];
|
|
21
22
|
marks: ContentMark[];
|
|
22
23
|
islands: ContentIsland[];
|
|
23
24
|
runs: PosRun[];
|
|
24
|
-
/** Total USV length of `text`. */
|
|
25
|
-
usvEnd: number;
|
|
26
25
|
}
|
|
27
26
|
/**
|
|
28
27
|
* Walk a PM document once, producing the content projection and the position-map runs; sharing the
|
|
@@ -30,35 +29,14 @@ export interface Scan {
|
|
|
30
29
|
*/
|
|
31
30
|
export declare function scanDoc(doc: PMNode): Scan;
|
|
32
31
|
/**
|
|
33
|
-
* The content projection a scan carries, for a caller holding a `Scan` that would
|
|
34
|
-
* again.
|
|
32
|
+
* The content projection a scan carries, with `anchors`, for a caller holding a `Scan` that would
|
|
33
|
+
* otherwise walk again. An empty part is left out, as a read leaves it.
|
|
35
34
|
*/
|
|
36
|
-
export declare function scanContent(s: Scan): Content;
|
|
35
|
+
export declare function scanContent(s: Scan, anchors?: ContentAnchor[]): Content;
|
|
37
36
|
/** `pmToContent`: the pure inverse of decode (drops the position runs). */
|
|
38
|
-
export declare function pmToContent(doc: PMNode): Content;
|
|
39
|
-
interface LowerOpts {
|
|
40
|
-
/** Anchor decoration positions after the edit, in final (new) USV coords. The
|
|
41
|
-
* pre-edit set is `oldRt`'s own, so only the projection's is passed in. */
|
|
42
|
-
newAnchors?: {
|
|
43
|
-
id: string;
|
|
44
|
-
pos: number;
|
|
45
|
-
}[];
|
|
46
|
-
}
|
|
37
|
+
export declare function pmToContent(doc: PMNode, anchors?: ContentAnchor[]): Content;
|
|
47
38
|
/**
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
* splice rides along because `diffText` allocates a code-point array per side. `contentEdit` is its
|
|
51
|
-
* only constructor, which keeps a `delta` agreeing with the contents it was diffed from; `lower`
|
|
52
|
-
* moves a slot inside the splice to the island channel, so this `delta` is the raw splice.
|
|
39
|
+
* The change taking `oldRt` to `newRt`, or `null` where the two are one value: the store's own
|
|
40
|
+
* `diff`, so what lands is the projection, ids and anchors and all.
|
|
53
41
|
*/
|
|
54
|
-
export
|
|
55
|
-
readonly oldRt: Content;
|
|
56
|
-
readonly newRt: Content;
|
|
57
|
-
/** The minimal single splice, or absent when the text is unchanged. */
|
|
58
|
-
readonly delta?: Delta;
|
|
59
|
-
}
|
|
60
|
-
/** The edit `oldRt` → `newRt`: diffs the text once. */
|
|
61
|
-
export declare function contentEdit(oldRt: Content, newRt: Content): ContentEdit;
|
|
62
|
-
/** Lower an edit to a `ChangeBundle`: the old→new content diff, as ops. */
|
|
63
|
-
export declare function lower(edit: ContentEdit, opts?: LowerOpts): ChangeBundle;
|
|
64
|
-
export {};
|
|
42
|
+
export declare function lower(oldRt: Content, newRt: Content): Change | null;
|