@quillmark/svelte 0.29.3 → 0.30.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 CHANGED
@@ -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` (USV) */
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 */
@@ -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 USV, the coordinate shared on both sides of the boundary; a `ContentHit` is a
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 in USV. */
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 in USV, absent on the placement rung. */
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. */
@@ -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
- /** Code points of `s` (USV units): the iteration granularity the content speaks. */
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
@@ -1,19 +1,15 @@
1
- // Decode: content → PM, a pure function (CODEC §Decode). Fold the flat lines into the tree: group a
2
- // `continues` run into one block (para hard breaks → `hard_break`; a fence's lines → one
3
- // `code_block`), nest by shared `containers` prefix, select the block node by `kind`, apply marks
4
- // over their `[start,end)` USV ranges, and lower island slots to leaf nodes. Anchors are
5
- // decorations (field.ts). Positions are USV; `Array.from` iterates by code point so an astral char
6
- // is one unit.
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
- /** Code points of `s` (USV units): the iteration granularity the content speaks. */
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,39 +24,30 @@ 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 lineTexts = rt.text.split('\n');
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.filter((m) => m.type !== 'anchor') : [];
43
+ const marks = hasMarks(schema) ? (rt.marks ?? []) : [];
44
44
  const cursor = { i: 0, rt };
45
- if (isInlineSchema(schema)) {
46
- return decodeInline(schema, lineTexts, starts, marks, cursor);
47
- }
48
- if (!isBlockSchema(schema)) {
49
- return decodePlain(rt, schema, lineTexts, starts, marks, cursor);
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
  /**
@@ -71,12 +58,12 @@ export function decode(rt, schema) {
71
58
  export function fitsInline(rt) {
72
59
  if (core().isInline(rt))
73
60
  return true;
74
- const plain = (line) => line.kind === 'para' && line.containers.length === 0;
75
- return (rt.islands.length === 0 &&
76
- rt.lines.length === 2 &&
77
- rt.lines.every(plain) &&
78
- !rt.lines[1].continues &&
79
- rt.text.endsWith('\n'));
61
+ const plain = (b) => b.kind === 'para' && !b.path?.length;
62
+ return (!rt.islands?.length &&
63
+ rt.blocks.length === 2 &&
64
+ rt.blocks.every(plain) &&
65
+ rt.text.endsWith('\n') &&
66
+ !rt.text.includes(HARD_BREAK));
80
67
  }
81
68
  /**
82
69
  * Whether `rt` decodes under `plainSchema` losing nothing but its marks, which the next commit
@@ -97,186 +84,127 @@ export function fitsLeaf(rt, leaf) {
97
84
  return fitsInline(rt);
98
85
  if (leaf.plaintext)
99
86
  return fitsPlain(rt);
100
- if (!leaf.islands && rt.islands.length)
87
+ const islands = rt.islands ?? [];
88
+ if (!leaf.islands && islands.length)
101
89
  return false;
102
- return rt.islands.every((isl) => isEditedIsland(isl.type));
90
+ return islands.every((isl) => isEditedIsland(isl.type));
103
91
  }
104
92
  /** Inline decode: one paragraph, containers and islands stripped. */
105
- function decodeInline(schema, lineTexts, starts, marks, cursor) {
106
- // An inline field is single-line: stray lines join with a space. The empty line a trailing `\n`
107
- // opens is dropped, so a first edit stores no space nobody typed. Islands are not representable
108
- // inline: the slot char is dropped and its entry skipped.
93
+ function decodeInline(schema, segments, marks, cursor) {
94
+ // An inline field is single-line: stray blocks and breaks join with a space. The empty block a
95
+ // trailing `\n` opens is dropped, so a first edit stores no space nobody typed. Islands are not
96
+ // representable inline: the slot char is dropped and its entry skipped.
109
97
  const inline = [];
110
- const last = lineTexts.length - 1;
111
- for (let i = 0; i < lineTexts.length; i++) {
112
- if (i > 0 && i === last && lineTexts[i] === '')
113
- break;
98
+ const last = segments.length - 1;
99
+ segments.forEach((seg, i) => {
100
+ if (i > 0 && i === last && seg.text === '')
101
+ return;
114
102
  if (i > 0)
115
103
  inline.push(schema.text(' '));
116
- inline.push(...buildInline(schema, lineTexts[i], starts[i], marks, cursor, true));
117
- }
104
+ inline.push(...buildInline(schema, seg, marks, cursor, 'inline'));
105
+ });
118
106
  const para = schema.nodes.paragraph.create(null, inline);
119
107
  return schema.nodes.doc.create(null, para);
120
108
  }
121
109
  /**
122
- * Plain decode: a `continues` line is a `hard_break` in the paragraph above and every other line
123
- * opens one. Line kinds, containers and islands are dropped, so content {@link fitsPlain} refuses
124
- * opens flattened.
110
+ * Plain decode: every block a paragraph, a break a `hard_break` in it. Block kinds, containers and
111
+ * islands are dropped, so content {@link fitsPlain} refuses opens flattened.
125
112
  */
126
- function decodePlain(rt, schema, lineTexts, starts, marks, cursor) {
127
- const paras = [];
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));
113
+ function decodePlain(schema, segments, marks, cursor) {
114
+ const blocks = segments.map((seg) => schema.nodes.paragraph.create(null, buildInline(schema, seg, marks, cursor, 'plain')));
137
115
  return schema.nodes.doc.create(null, blocks.length ? blocks : schema.nodes.paragraph.create());
138
116
  }
139
- /** Nest a run of leaves that share a `depth`-length container prefix into blocks. */
140
- function groupBlocks(schema, leaves, depth, marks, cursor) {
117
+ /** Nest the blocks whose paths agree to `depth` into nodes, one per child of that prefix. */
118
+ function groupBlocks(schema, segments, depth, containers, marks, cursor) {
141
119
  const out = [];
142
120
  let i = 0;
143
- while (i < leaves.length) {
144
- const path = leaves[i].containers;
121
+ while (i < segments.length) {
122
+ const path = segments[i].block.path ?? [];
145
123
  if (path.length <= depth) {
146
- out.push(makeLeaf(schema, leaves[i], marks, cursor));
124
+ out.push(makeLeaf(schema, segments[i], marks, cursor));
147
125
  i++;
148
126
  continue;
149
127
  }
150
- const here = path[depth];
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);
128
+ const id = path[depth];
190
129
  let j = i + 1;
191
- while (j < leaves.length) {
192
- const c = atDepth(leaves[j], depth);
193
- if (!c || containerKey(c) !== key)
194
- break;
130
+ while (j < segments.length && segments[j].block.path?.[depth] === id)
195
131
  j++;
132
+ const run = segments.slice(i, j);
133
+ const container = containers.get(id);
134
+ const inner = (d) => groupBlocks(schema, run, d, containers, marks, cursor);
135
+ switch (container?.kind) {
136
+ case 'list': {
137
+ const items = groupItems(schema, run, depth + 1, containers, marks, cursor);
138
+ out.push(container.attrs.ordered
139
+ ? schema.nodes.ordered_list.create({ id, start: container.attrs.start }, items)
140
+ : schema.nodes.bullet_list.create({ id }, items));
141
+ break;
142
+ }
143
+ case 'quote':
144
+ out.push(schema.nodes.blockquote.create({ id }, inner(depth + 1)));
145
+ break;
146
+ default:
147
+ // An item outside a list or a container no record names: a value nothing renders, so its
148
+ // blocks decode in place and the projection settles it.
149
+ out.push(...inner(depth + 1));
196
150
  }
197
- const inner = groupBlocks(schema, leaves.slice(i, j), depth + 1, marks, cursor);
198
- out.push(schema.nodes.blockquote.create(null, inner));
199
151
  i = j;
200
152
  }
201
153
  return out;
202
154
  }
203
- function atDepth(leaf, depth) {
204
- return leaf.containers[depth];
205
- }
206
- /**
207
- * The `ordinal` of a leaf's `list_item` at `depth`, only called inside a run the discriminant
208
- * established.
209
- */
210
- function ordinalAt(leaf, depth) {
211
- const c = atDepth(leaf, depth);
212
- return c?.container === 'list_item' ? c.attrs.ordinal : -1;
213
- }
214
- /** A task item's `checked` at `depth`, `null` on any other item: what the item's lines
215
- * carry alike, so its first line answers for it. */
216
- function checkedAt(leaf, depth) {
217
- const c = atDepth(leaf, depth);
218
- return c?.container === 'list_item' ? (c.attrs.checked ?? null) : null;
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)}`;
155
+ /** The items of one list: a `list_item` per run of blocks sharing the item id at `depth`. */
156
+ function groupItems(schema, run, depth, containers, marks, cursor) {
157
+ const items = [];
158
+ let i = 0;
159
+ while (i < run.length) {
160
+ const id = run[i].block.path?.[depth];
161
+ let j = i + 1;
162
+ while (j < run.length && run[j].block.path?.[depth] === id)
163
+ j++;
164
+ const item = id === undefined ? undefined : containers.get(id);
165
+ const checked = item?.kind === 'item' ? (item.attrs?.checked ?? null) : null;
166
+ const body = groupBlocks(schema, run.slice(i, j), depth + 1, containers, marks, cursor);
167
+ items.push(schema.nodes.list_item.create({ id: id ?? null, checked }, body));
168
+ i = j;
169
+ }
170
+ return items;
232
171
  }
233
172
  /**
234
- * A single leaf block node from its segments, exhaustive over the closed line vocabulary so a kind
235
- * added upstream is a compile error.
173
+ * A single leaf block node from its record, exhaustive over the closed block vocabulary so a kind
174
+ * added upstream is a compile error. A kind spelled `x-` reads as a paragraph that holds its kind.
236
175
  */
237
- function makeLeaf(schema, leaf, marks, cursor) {
238
- const line = leaf.line;
239
- switch (line.kind) {
176
+ function makeLeaf(schema, seg, marks, cursor) {
177
+ const block = seg.block;
178
+ const id = block.id || null;
179
+ switch (block.kind) {
240
180
  case 'rule':
241
- return schema.nodes.horizontal_rule.create();
181
+ return schema.nodes.horizontal_rule.create({ id });
242
182
  case 'code': {
243
- // One code_block: the segments' texts joined by literal `\n`, no marks.
244
- const text = leaf.segments.map((s) => s.text).join('\n');
183
+ const text = seg.text.replaceAll(HARD_BREAK, '\n');
245
184
  const content = text.length ? [schema.text(text)] : [];
246
- return schema.nodes.code_block.create({ lang: line.attrs?.lang ?? null }, content);
185
+ return schema.nodes.code_block.create({ id, lang: block.attrs?.lang ?? null }, content);
247
186
  }
248
- case 'para':
249
- case 'heading': {
250
- if (line.kind !== 'heading' && blockIslandLine(leaf, cursor))
251
- return schema.nodes.island_block.create(islandAttrs(cursor));
252
- // Inline content: the segment boundary is a `hard_break` in a paragraph and a space in
253
- // a heading, which takes none (`schema.ts` §`takesLineBreak`). A stored heading carries
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);
187
+ case 'heading':
188
+ return schema.nodes.heading.create({ id, level: block.attrs.level }, buildInline(schema, seg, marks, cursor, 'heading'));
189
+ case 'para': {
190
+ if (blockIslandSegment(seg, cursor))
191
+ return schema.nodes.island_block.create({ ...islandAttrs(cursor), block: id });
192
+ return schema.nodes.paragraph.create({ id }, buildInline(schema, seg, marks, cursor, 'paragraph'));
265
193
  }
266
194
  default: {
267
- const unreached = line;
268
- return unreached;
195
+ const ext = { kind: block.kind, attrs: block.attrs ?? {} };
196
+ return schema.nodes.paragraph.create({ id, ext }, buildInline(schema, seg, marks, cursor, 'paragraph'));
269
197
  }
270
198
  }
271
199
  }
272
200
  /**
273
- * Whether a `para` leaf is one block island: a single line holding its slot alone, backed by an
274
- * island whose markup is a block.
201
+ * Whether a `para` block is one block island: its slot alone, backed by an island whose markup is a
202
+ * block.
275
203
  */
276
- function blockIslandLine(leaf, cursor) {
277
- if (leaf.segments.length !== 1 || leaf.segments[0].text !== ISLAND_SLOT)
204
+ function blockIslandSegment(seg, cursor) {
205
+ if (seg.text !== ISLAND_SLOT)
278
206
  return false;
279
- const isl = cursor.rt.islands[cursor.i];
207
+ const isl = cursor.rt.islands?.[cursor.i];
280
208
  return isl != null && isBlockIsland(isl.type);
281
209
  }
282
210
  /**
@@ -285,17 +213,21 @@ function blockIslandLine(leaf, cursor) {
285
213
  * content): the slot draws nothing, keeping the decode total.
286
214
  */
287
215
  function islandAttrs(cursor) {
288
- const isl = cursor.rt.islands[cursor.i++];
289
- return isl ? { id: isl.id, islandType: isl.type, props: isl.props } : null;
216
+ const isl = cursor.rt.islands?.[cursor.i++];
217
+ return isl
218
+ ? { id: isl.id, islandType: isl.type, props: isl.props, slots: isl.slots ?? null }
219
+ : null;
290
220
  }
291
221
  /**
292
222
  * 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. `stripIslands` drops slots. `pos = segStartUSV + k` indexes
294
- * into the global mark ranges.
223
+ * `U+FFFC` to an inline island node and each `U+2028` to a `hard_break`, or to a space in a host
224
+ * that takes none (`schema.ts` §`takesLineBreak`). An inline host drops slots, keeping the island
225
+ * cursor in step.
295
226
  */
296
- function buildInline(schema, segText, segStartUSV, marks, cursor, stripIslands) {
297
- const cps = codePoints(segText);
227
+ function buildInline(schema, seg, marks, cursor, host) {
298
228
  const out = [];
229
+ const breaks = host !== 'inline' &&
230
+ takesLineBreak(host === 'heading' ? schema.nodes.heading : schema.nodes.paragraph);
299
231
  let runText = '';
300
232
  let runKey = '';
301
233
  let runMarks = [];
@@ -305,30 +237,37 @@ function buildInline(schema, segText, segStartUSV, marks, cursor, stripIslands)
305
237
  runText = '';
306
238
  };
307
239
  // A mark set changes only where a mark starts or ends; `marks` is the whole list, so resolving
308
- // per code point would cost the text against every mark.
309
- const segEndUSV = segStartUSV + cps.length;
310
- const spanning = marks.filter((m) => m.start < segEndUSV && m.end > segStartUSV);
240
+ // per character would cost the text against every mark.
241
+ const segStart = seg.start;
242
+ const segEnd = segStart + seg.text.length;
243
+ const spanning = marks.filter((m) => m.start < segEnd && m.end > segStart);
311
244
  const edges = new Set();
312
245
  for (const m of spanning) {
313
- if (m.start > segStartUSV)
246
+ if (m.start > segStart)
314
247
  edges.add(m.start);
315
- if (m.end < segEndUSV)
248
+ if (m.end < segEnd)
316
249
  edges.add(m.end);
317
250
  }
318
251
  let active = [];
319
252
  let key = '';
320
253
  let unresolved = true;
321
- for (let k = 0; k < cps.length; k++) {
322
- const cp = cps[k];
323
- const pos = segStartUSV + k;
324
- if (cp === ISLAND_SLOT) {
254
+ for (let k = 0; k < seg.text.length; k++) {
255
+ const ch = seg.text[k];
256
+ const pos = segStart + k;
257
+ if (ch === ISLAND_SLOT) {
325
258
  flush();
326
259
  const attrs = islandAttrs(cursor);
327
- if (attrs && !stripIslands && schema.nodes.island_inline) {
260
+ if (attrs && host !== 'inline' && host !== 'plain' && schema.nodes.island_inline)
328
261
  out.push(schema.nodes.island_inline.create(attrs));
329
- }
330
- runKey = '\0slot'; // force a fresh run after a slot
331
- unresolved = true; // the slot's own position is skipped, edge or not
262
+ runKey = '\0slot';
263
+ unresolved = true;
264
+ continue;
265
+ }
266
+ if (ch === HARD_BREAK && breaks) {
267
+ flush();
268
+ out.push(schema.nodes.hard_break.create());
269
+ runKey = '\0break';
270
+ unresolved = true;
332
271
  continue;
333
272
  }
334
273
  if (unresolved || edges.has(pos)) {
@@ -342,7 +281,7 @@ function buildInline(schema, segText, segStartUSV, marks, cursor, stripIslands)
342
281
  runKey = key;
343
282
  runMarks = buildMarkSet(schema, active);
344
283
  }
345
- runText += cp;
284
+ runText += ch === HARD_BREAK ? ' ' : ch;
346
285
  }
347
286
  flush();
348
287
  return out;
@@ -355,7 +294,7 @@ function markSetKey(active) {
355
294
  // groups by. JSON-joined so a `link:a|strong` url stays distinct from `{link:a} + {strong}`.
356
295
  return JSON.stringify(active.map((m) => markKey(descriptorOf(m))).sort());
357
296
  }
358
- /** The PM mark array for an active content mark set (anchors already excluded). */
297
+ /** The PM mark array for an active content mark set. */
359
298
  function buildMarkSet(schema, active) {
360
299
  let set = [];
361
300
  for (const m of active) {
@@ -1,28 +1,27 @@
1
1
  import type { Node as PMNode } from 'prosemirror-model';
2
- import type { ChangeBundle, Delta, Content, ContentIsland, ContentLine, ContentMark } from '@quillmark/wasm';
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
- usvStart: number;
6
+ at: number;
7
7
  s: string;
8
8
  } | {
9
9
  kind: 'nl';
10
10
  pmStart: number;
11
11
  pmEnd: number;
12
- usvStart: number;
12
+ at: number;
13
13
  } | {
14
14
  kind: 'atom';
15
15
  pmStart: number;
16
- usvStart: number;
16
+ at: number;
17
17
  };
18
18
  export interface Scan {
19
19
  text: string;
20
- lines: ContentLine[];
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 otherwise walk
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
- * One old→new content edit with the text splice it implies. Both sides are content, never a PM doc:
49
- * a doc-taking overload would re-walk the tree per keystroke for a projection the caller holds. The
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 interface ContentEdit {
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;