@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.
Files changed (63) hide show
  1. package/README.md +3 -3
  2. package/dist/core/address.d.ts +3 -3
  3. package/dist/core/address.js +1 -1
  4. package/dist/core/codec/decode.d.ts +4 -5
  5. package/dist/core/codec/decode.js +151 -208
  6. package/dist/core/codec/encode.d.ts +13 -35
  7. package/dist/core/codec/encode.js +127 -408
  8. package/dist/core/codec/field.d.ts +11 -10
  9. package/dist/core/codec/field.js +190 -92
  10. package/dist/core/codec/index.d.ts +2 -2
  11. package/dist/core/codec/index.js +4 -4
  12. package/dist/core/codec/islands.d.ts +13 -9
  13. package/dist/core/codec/islands.js +54 -49
  14. package/dist/core/codec/marks.d.ts +12 -18
  15. package/dist/core/codec/marks.js +16 -26
  16. package/dist/core/codec/positions.d.ts +25 -7
  17. package/dist/core/codec/positions.js +43 -50
  18. package/dist/core/codec/prose.css +0 -8
  19. package/dist/core/codec/schema.d.ts +3 -3
  20. package/dist/core/codec/schema.js +29 -11
  21. package/dist/core/codec/slash.d.ts +4 -2
  22. package/dist/core/codec/slash.js +13 -3
  23. package/dist/core/codec/table-copy.d.ts +3 -3
  24. package/dist/core/codec/table-copy.js +6 -4
  25. package/dist/core/codec/table-paste.js +13 -9
  26. package/dist/core/codec/table-view.js +19 -12
  27. package/dist/core/codec/table.d.ts +77 -43
  28. package/dist/core/codec/table.js +157 -48
  29. package/dist/core/index.d.ts +1 -0
  30. package/dist/core/theme.css +14 -6
  31. package/dist/visual/ArrayField.svelte +168 -83
  32. package/dist/visual/BooleanField.svelte +2 -2
  33. package/dist/visual/Card.svelte +65 -44
  34. package/dist/visual/DateField.svelte +16 -1
  35. package/dist/visual/DiagnosticList.svelte +34 -2
  36. package/dist/visual/EnumField.svelte +58 -44
  37. package/dist/visual/Field.svelte +13 -3
  38. package/dist/visual/Field.svelte.d.ts +3 -1
  39. package/dist/visual/FieldHint.svelte +10 -3
  40. package/dist/visual/FormatPopover.svelte +1 -1
  41. package/dist/visual/MatrixAdd.svelte +3 -18
  42. package/dist/visual/MatrixField.svelte +20 -12
  43. package/dist/visual/ObjectField.svelte +164 -145
  44. package/dist/visual/ProseField.svelte +6 -1
  45. package/dist/visual/ProseValue.svelte +8 -8
  46. package/dist/visual/SlashMenu.svelte +24 -12
  47. package/dist/visual/SlashMenu.svelte.d.ts +2 -0
  48. package/dist/visual/VisualEditor.svelte +1 -1
  49. package/dist/visual/VisualEditor.svelte.d.ts +1 -1
  50. package/dist/visual/VisualEditorInner.svelte +159 -18
  51. package/dist/visual/VisualEditorInner.svelte.d.ts +1 -1
  52. package/dist/visual/controls.css +42 -4
  53. package/dist/visual/leaves.d.ts +1 -1
  54. package/dist/visual/leaves.js +1 -1
  55. package/dist/visual/strings.d.ts +27 -10
  56. package/dist/visual/strings.js +40 -6
  57. package/dist/visual/structure.d.ts +17 -9
  58. package/dist/visual/structure.js +20 -26
  59. package/dist/visual/undo.svelte.d.ts +36 -0
  60. package/dist/visual/undo.svelte.js +119 -0
  61. package/package.json +2 -2
  62. package/dist/visual/hold.d.ts +0 -17
  63. package/dist/visual/hold.js +0 -39
@@ -1,13 +1,16 @@
1
1
  // Islands: the `U+FFFC` slot + `ContentIsland` entry ↔ a PM leaf node. Every slot sits in a `para`
2
- // line; the island's type says which node it is, a `table` a block alone on its line and every
3
- // other type inline. The node carries `id`, `islandType` and `props` verbatim.
2
+ // block; the island's type says which node it is, a `table` a block alone in its block and every
3
+ // other type inline. The node carries `id`, `islandType`, `props` and `slots` verbatim, and a block
4
+ // island the id of the block it fills.
4
5
  //
5
6
  // `ContentIsland.props` is the typed union at the boundary (DOCUMENT_MODEL §Stability seams), so a
6
7
  // discriminant check narrows it; `props` still crosses the DOM as opaque JSON, which
7
- // §`tablePropsOfNode` guards.
8
+ // `table.ts` §`tableOfNode` guards.
8
9
  import { Fragment, Slice } from 'prosemirror-model';
9
10
  import { Plugin } from 'prosemirror-state';
10
11
  import { storableUrl } from './urls.js';
12
+ /** The `U+2028` line separator that is a break inside a block's text. */
13
+ export const HARD_BREAK = '\u2028';
11
14
  /** The `U+FFFC` object-replacement char that occupies one island slot in `text`. */
12
15
  export const ISLAND_SLOT = '';
13
16
  // The three island node attributes carry the content entry verbatim: `id` is the stable identity
@@ -17,7 +20,8 @@ export const ISLAND_SLOT = '';
17
20
  const islandAttrs = {
18
21
  id: { default: '' },
19
22
  islandType: {},
20
- props: {}
23
+ props: {},
24
+ slots: { default: null }
21
25
  };
22
26
  // All three attributes cross the DOM, since a copy and a paste inside one body run the document
23
27
  // through `toDOM`/`parseDOM` (CODEC §"Markdown at the edges") and an island's tags alone say only
@@ -28,54 +32,56 @@ function islandDOM(node) {
28
32
  return {
29
33
  'data-qm-island': node.attrs.islandType,
30
34
  'data-qm-island-id': node.attrs.id,
31
- 'data-qm-island-props': JSON.stringify(node.attrs.props ?? null)
35
+ 'data-qm-island-props': JSON.stringify(node.attrs.props ?? null),
36
+ 'data-qm-island-slots': JSON.stringify(node.attrs.slots ?? null)
32
37
  };
33
38
  }
34
- /**
35
- * The island types the content model names, each with whether its markup is a block (deciding the
36
- * node its slot decodes to) and whether a leaf edits it. A `Record` over the boundary's union, so a
37
- * member added upstream is a missing key here. A footnote and an equation are read, not edited
38
- * (CODEC §Decode): a leaf holding one is held.
39
- */
40
39
  const ISLANDS = {
41
40
  table: { block: true, edited: true },
42
41
  image: { block: false, edited: true },
43
42
  footnote: { block: false, edited: false },
44
43
  math: { block: false, edited: false }
45
44
  };
46
- /** Whether an island of `type` is block markup: a `table` stands alone on its line. */
45
+ /** Whether an island of `type` is block markup: a `table` stands alone in its block. */
47
46
  export function isBlockIsland(type) {
48
- return ISLANDS[type].block;
47
+ return Object.hasOwn(ISLANDS, type) && ISLANDS[type].block;
49
48
  }
50
49
  /** Whether a leaf edits an island of `type`; one holding any other is held (`fitsLeaf`). */
51
50
  export function isEditedIsland(type) {
52
- return ISLANDS[type].edited;
51
+ return Object.hasOwn(ISLANDS, type) && ISLANDS[type].edited;
53
52
  }
54
53
  /**
55
54
  * An island node's attributes off that DOM, or `false` where the id is absent or the type is one no
56
55
  * leaf edits (an unknown name, a footnote, an equation), so the element's text arrives as text. A
57
56
  * `props` that is not the JSON written above reads as `null`, one of the wrong shape `undefined` at
58
- * its reader (§`tablePropsOfNode`): the entry survives as an island of its type.
57
+ * its reader (`table.ts` §`tableOfNode`): the entry survives as an island of its type.
59
58
  */
60
59
  function islandAttrsFromDOM(el) {
61
60
  const id = el.getAttribute('data-qm-island-id');
62
61
  // Asserted off an untrusted attribute and checked against the set on the next line.
63
62
  const type = el.getAttribute('data-qm-island');
64
- if (id == null || type == null || !Object.hasOwn(ISLANDS, type) || !isEditedIsland(type))
63
+ if (id == null || type == null || !isEditedIsland(type))
65
64
  return false;
66
- const raw = el.getAttribute('data-qm-island-props');
67
- let props = null;
68
- try {
69
- props = raw == null ? null : JSON.parse(raw);
70
- }
71
- catch {
72
- props = null;
73
- }
74
- return { id, islandType: type, props: storableProps(type, props) };
65
+ const json = (name) => {
66
+ const raw = el.getAttribute(name);
67
+ try {
68
+ return raw == null ? null : JSON.parse(raw);
69
+ }
70
+ catch {
71
+ return null;
72
+ }
73
+ };
74
+ const slots = json('data-qm-island-slots');
75
+ return {
76
+ id,
77
+ islandType: type,
78
+ props: storableProps(type, json('data-qm-island-props')),
79
+ slots: typeof slots === 'object' && slots !== null ? slots : null
80
+ };
75
81
  }
76
82
  /**
77
83
  * `props` with the one key the authored lane refuses made storable: an `image`'s `url` carrying a
78
- * line ending, which `IslandOp` and `overwrite` throw on.
84
+ * line ending, which a write refuses.
79
85
  */
80
86
  function storableProps(type, props) {
81
87
  if (type !== 'image' || typeof props !== 'object' || props === null)
@@ -83,16 +89,16 @@ function storableProps(type, props) {
83
89
  const url = props.url;
84
90
  return typeof url === 'string' ? { ...props, url: storableUrl(url) } : props;
85
91
  }
86
- /** Block island node (a table): a `U+FFFC` slot alone on its `para` line. Atom. */
92
+ /** Block island node (a table): a `U+FFFC` slot alone in its `para` block. Atom. */
87
93
  export const islandBlockSpec = {
88
94
  group: 'block',
89
95
  atom: true,
90
96
  selectable: true,
91
- attrs: islandAttrs,
97
+ attrs: { ...islandAttrs, block: { default: null } },
92
98
  parseDOM: [{ tag: 'div[data-qm-island]', getAttrs: (el) => islandAttrsFromDOM(el) }],
93
99
  toDOM: (node) => ['div', islandDOM(node), `[${node.attrs.islandType || 'island'}]`]
94
100
  };
95
- /** Inline island node (an image, a footnote, an equation): a `U+FFFC` slot inside a `para` line. */
101
+ /** Inline island node (an image, a footnote, an equation): a `U+FFFC` slot inside a `para` block. */
96
102
  export const islandInlineSpec = {
97
103
  group: 'inline',
98
104
  inline: true,
@@ -110,8 +116,10 @@ function inlineIslandDOM(node) {
110
116
  const attrs = node.attrs;
111
117
  const dom = islandDOM(node);
112
118
  const props = attrs.props;
113
- if (attrs.islandType === 'footnote')
114
- return ['sup', typeof props?.text === 'string' ? { ...dom, title: props.text } : dom];
119
+ if (attrs.islandType === 'footnote') {
120
+ const note = attrs.slots?.note?.text;
121
+ return ['sup', typeof note === 'string' ? { ...dom, title: note } : dom];
122
+ }
115
123
  if (attrs.islandType === 'math' && typeof props?.tex === 'string') {
116
124
  const fence = props.display ? '$$' : '$';
117
125
  return ['span', dom, `${fence}${props.tex}${fence}`];
@@ -122,35 +130,32 @@ function inlineIslandDOM(node) {
122
130
  * Build a content island entry from a PM island node's attrs. The cast pairs a closed `type` with a
123
131
  * `props` that crossed the DOM as opaque JSON. The store is lenient about a table's shape, so a
124
132
  * payload the arm does not hold is stored, and `undefined` at its reader draws the placeholder
125
- * (§`tablePropsOfNode`).
133
+ * (`table.ts` §`tableOfNode`).
126
134
  */
127
135
  export function islandEntryFromNode(attrs) {
128
136
  return {
129
137
  id: attrs.id,
130
138
  type: attrs.islandType,
131
- props: attrs.props
139
+ props: attrs.props,
140
+ ...(attrs.slots && Object.keys(attrs.slots).length ? { slots: attrs.slots } : {})
132
141
  };
133
142
  }
134
143
  /**
135
- * The `TableProps` shape over a value that reached the node off the DOM: the rectangle every reader
136
- * indexes (`table.ts`), down to the `text` a row's emptiness is read off.
144
+ * The `TableProps` shape over a value that reached the node off the DOM: the column ids and the
145
+ * rows mapping each to a slot id, which every reader indexes (`table.ts`).
137
146
  */
138
- function isTableProps(props) {
147
+ export function isTableProps(props) {
139
148
  if (typeof props !== 'object' || props === null)
140
149
  return false;
141
- const { header, rows, aligns } = props;
142
- const cells = (v) => Array.isArray(v) &&
143
- v.every((c) => typeof c === 'object' && c !== null && typeof c.text === 'string');
144
- return (cells(header) && Array.isArray(aligns) && Array.isArray(rows) && rows.every((row) => cells(row)));
145
- }
146
- /**
147
- * A node's `TableProps`, or `undefined` for any other island and for a payload of the wrong shape:
148
- * the boundary's guard, so a `props` that crossed the DOM is read once here. Every caller draws the
149
- * placeholder for `undefined` (`table-view.ts` §`render`).
150
- */
151
- export function tablePropsOfNode(node) {
152
- const attrs = node.attrs;
153
- return attrs.islandType === 'table' && isTableProps(attrs.props) ? attrs.props : undefined;
150
+ const { columns, rows } = props;
151
+ const ids = (v) => Array.isArray(v) && v.every((c) => typeof c === 'string');
152
+ return (ids(columns) &&
153
+ Array.isArray(rows) &&
154
+ rows.every((row) => typeof row === 'object' &&
155
+ row !== null &&
156
+ typeof row.id === 'string' &&
157
+ typeof row.cells === 'object' &&
158
+ row.cells !== null));
154
159
  }
155
160
  /**
156
161
  * A minter over one document: the positional `isl-{n}` sequence continued past the highest id the
@@ -1,9 +1,7 @@
1
1
  import type { Mark, Schema } from 'prosemirror-model';
2
2
  import type { ContentMark, ContentMarkKind } from '@quillmark/wasm';
3
- /** A PM mark from a content formatting mark, or `null` for an anchor, which no PM mark
4
- * draws (`field.ts` holds it as a decoration). Exhaustive over
5
- * the closed mark vocabulary, so a member added upstream is a compile error here rather
6
- * than an index into a PM mark this schema does not declare. */
3
+ /** A PM mark from a content formatting mark, or `null` where the schema declares none for it. An
4
+ * `x-` type rides `ext`. */
7
5
  export declare function pmMarkFromContent(schema: Schema, m: ContentMark): Mark | null;
8
6
  /**
9
7
  * A content mark descriptor from a PM mark (range-free): the `{ type, … }` half
@@ -14,29 +12,25 @@ export declare function pmMarkFromContent(schema: Schema, m: ContentMark): Mark
14
12
  export declare function contentDescriptorFromPM(mark: Mark): ContentMarkKind;
15
13
  /**
16
14
  * The range-free `{ type, … }` half of a content mark: `contentDescriptorFromPM`'s
17
- * content-side twin, and what a `MarkOp` carries beside its range. Lifted off the
15
+ * content-side twin, and what a splice's mark carries beside its range. Lifted off the
18
16
  * range envelope rather than rebuilt arm by arm, so a payload crosses whole and an
19
17
  * arm added upstream reaches this by construction.
20
18
  */
21
19
  export declare function descriptorOf(m: ContentMark): ContentMarkKind;
22
20
  /**
23
- * A stable grouping key for the mark diff: marks sharing a key union into one
24
- * coverage set. Payload-free formatting keys on its type, everything else on
25
- * type+attrs, because `applyChange`'s `remove` matches type and attrs (verified),
26
- * so two links differing in url are independent mark families. This is the seam's
27
- * own `(type, attrs)` tie-break, which is why no arm needs a case of its own.
21
+ * A stable grouping key for marks: marks sharing a key union into one coverage set.
22
+ * Payload-free formatting keys on its type, everything else on type+attrs, so two
23
+ * links differing in url are independent mark families: the store's own `(type,
24
+ * attrs)` union rule, which is why no arm needs a case of its own.
28
25
  */
29
26
  export declare function markKey(descriptor: ContentMarkKind): string;
30
- /** A held anchor position: an identity id at a USV content offset (zero-width). */
27
+ /** A held anchor: an identity id over `[start, end)` content offsets, a point where they meet. */
31
28
  export interface AnchorPos {
32
29
  id: string;
33
- pos: number;
30
+ start: number;
31
+ end: number;
34
32
  }
35
- /**
36
- * The identity anchors of a `Content` as `{ id, pos }` in USV: the seed for the
37
- * field's anchor-position plugin and the `oldAnchors` the mark diff rebases.
38
- * Anchors are zero-width, so `start` is the position.
39
- */
33
+ /** The identity anchors of a `Content`: the seed for the field's anchor plugin. */
40
34
  export declare function anchorsFromContent(rt: {
41
- marks: ContentMark[];
35
+ anchors?: AnchorPos[];
42
36
  }): AnchorPos[];
@@ -1,12 +1,8 @@
1
1
  import { canonicalJson } from './reconcile.js';
2
- /** A PM mark from a content formatting mark, or `null` for an anchor, which no PM mark
3
- * draws (`field.ts` holds it as a decoration). Exhaustive over
4
- * the closed mark vocabulary, so a member added upstream is a compile error here rather
5
- * than an index into a PM mark this schema does not declare. */
2
+ /** A PM mark from a content formatting mark, or `null` where the schema declares none for it. An
3
+ * `x-` type rides `ext`. */
6
4
  export function pmMarkFromContent(schema, m) {
7
5
  switch (m.type) {
8
- case 'anchor':
9
- return null;
10
6
  case 'font_family':
11
7
  return schema.marks.font_family.create({ name: m.attrs.name });
12
8
  case 'emph':
@@ -18,10 +14,8 @@ export function pmMarkFromContent(schema, m) {
18
14
  case 'strike':
19
15
  case 'code':
20
16
  return schema.marks[m.type].create();
21
- default: {
22
- const unreached = m;
23
- return unreached;
24
- }
17
+ default:
18
+ return schema.marks.ext?.create({ type: m.type, attrs: m.attrs ?? null }) ?? null;
25
19
  }
26
20
  }
27
21
  /**
@@ -38,11 +32,16 @@ export function contentDescriptorFromPM(mark) {
38
32
  return { type: 'link', attrs: { url: mark.attrs.href } };
39
33
  if (name === 'font_family')
40
34
  return { type: 'font_family', attrs: { name: mark.attrs.name } };
35
+ if (name === 'ext') {
36
+ const type = mark.attrs.type;
37
+ const attrs = mark.attrs.attrs;
38
+ return attrs && Object.keys(attrs).length ? { type, attrs } : { type };
39
+ }
41
40
  return { type: name };
42
41
  }
43
42
  /**
44
43
  * The range-free `{ type, … }` half of a content mark: `contentDescriptorFromPM`'s
45
- * content-side twin, and what a `MarkOp` carries beside its range. Lifted off the
44
+ * content-side twin, and what a splice's mark carries beside its range. Lifted off the
46
45
  * range envelope rather than rebuilt arm by arm, so a payload crosses whole and an
47
46
  * arm added upstream reaches this by construction.
48
47
  */
@@ -51,26 +50,17 @@ export function descriptorOf(m) {
51
50
  return kind;
52
51
  }
53
52
  /**
54
- * A stable grouping key for the mark diff: marks sharing a key union into one
55
- * coverage set. Payload-free formatting keys on its type, everything else on
56
- * type+attrs, because `applyChange`'s `remove` matches type and attrs (verified),
57
- * so two links differing in url are independent mark families. This is the seam's
58
- * own `(type, attrs)` tie-break, which is why no arm needs a case of its own.
53
+ * A stable grouping key for marks: marks sharing a key union into one coverage set.
54
+ * Payload-free formatting keys on its type, everything else on type+attrs, so two
55
+ * links differing in url are independent mark families: the store's own `(type,
56
+ * attrs)` union rule, which is why no arm needs a case of its own.
59
57
  */
60
58
  export function markKey(descriptor) {
61
59
  if ('attrs' in descriptor)
62
60
  return `${descriptor.type}\u0000${canonicalJson(descriptor.attrs)}`;
63
61
  return descriptor.type;
64
62
  }
65
- /**
66
- * The identity anchors of a `Content` as `{ id, pos }` in USV: the seed for the
67
- * field's anchor-position plugin and the `oldAnchors` the mark diff rebases.
68
- * Anchors are zero-width, so `start` is the position.
69
- */
63
+ /** The identity anchors of a `Content`: the seed for the field's anchor plugin. */
70
64
  export function anchorsFromContent(rt) {
71
- const out = [];
72
- for (const m of rt.marks)
73
- if (m.type === 'anchor')
74
- out.push({ id: m.attrs.id, pos: m.start });
75
- return out;
65
+ return (rt.anchors ?? []).map(({ id, start, end }) => ({ id, start, end }));
76
66
  }
@@ -1,12 +1,25 @@
1
1
  import type { Node as PMNode } from 'prosemirror-model';
2
2
  import { type PosRun, type Scan } from './encode.js';
3
- /** A prebuilt line→position index for a PM doc, rebuilt on structural change. */
3
+ /** A text change as one pass over the text before, offsets in UTF-16 code units: what a host
4
+ * hands `applyExternal` for a field another writer changed, such as `@quillmark/collab`'s
5
+ * `RemoteChange` delta. */
6
+ export interface Delta {
7
+ ops: ({
8
+ retain: number;
9
+ } | {
10
+ insert: string;
11
+ } | {
12
+ delete: number;
13
+ })[];
14
+ }
15
+ /** A prebuilt position index for a PM doc, rebuilt on structural change. */
4
16
  export interface LineIndex {
5
17
  runs: PosRun[];
6
- usvEnd: number;
18
+ /** Length of the content text. */
19
+ end: number;
7
20
  /** PM position at the end of the last content run (the caret-max). */
8
21
  pmEndContent: number;
9
- /** PM content position of the first line (the caret for an empty doc). */
22
+ /** PM content position of the first block (the caret for an empty doc). */
10
23
  firstContentStart: number;
11
24
  }
12
25
  /** Build the position index for `doc` (walks it once via the shared scanner). */
@@ -14,7 +27,12 @@ export declare function buildLineIndex(doc: PMNode): LineIndex;
14
27
  /** The index a scan's runs make. The commit path takes this one: it wants the
15
28
  * content projection off the same walk (`encode.ts`, `scanContent`). */
16
29
  export declare function lineIndexOf(s: Scan): LineIndex;
17
- /** USV content offset → PM position. */
18
- export declare function usvToPM(index: LineIndex, usvPos: number): number;
19
- /** PM position → USV content offset. */
20
- export declare function pmToUsv(index: LineIndex, pmPos: number): number;
30
+ /** Content offset → PM position. */
31
+ export declare function offsetToPM(index: LineIndex, offset: number): number;
32
+ /** PM position → content offset. */
33
+ export declare function pmToOffset(index: LineIndex, pmPos: number): number;
34
+ /**
35
+ * Where content offset `pos` lands after `delta`, its ops counting UTF-16 code units: text
36
+ * inserted at `pos` lands before it, so a caret at an edit's edge follows the text typed there.
37
+ */
38
+ export declare function mapThroughDelta(delta: Delta, pos: number): number;
@@ -15,7 +15,7 @@ export function lineIndexOf(s) {
15
15
  const last = runs[runs.length - 1];
16
16
  pmEndContent = runEndPm(last);
17
17
  }
18
- return { runs, usvEnd: s.usvEnd, pmEndContent, firstContentStart };
18
+ return { runs, end: s.text.length, pmEndContent, firstContentStart };
19
19
  }
20
20
  /** PM position just past a run (its exclusive end). */
21
21
  function runEndPm(run) {
@@ -25,13 +25,13 @@ function runEndPm(run) {
25
25
  return run.pmEnd;
26
26
  return run.pmStart + 1;
27
27
  }
28
- const usvStartOf = (run) => run.usvStart;
28
+ const atOf = (run) => run.at;
29
29
  const pmStartOf = (run) => run.pmStart;
30
30
  /**
31
31
  * Index of the last run whose `coord` is ≤ `pos`, or `-1` when `pos` precedes the
32
32
  * first run. The walk emits runs in document order and every run owns at least one
33
33
  * position in each space, so both coordinates increase strictly across the list and
34
- * the lookup bisects. Bulk conversion is what wants it: `readAnchorsUsv` converts
34
+ * the lookup bisects. Bulk conversion is what wants it: `readAnchors` converts
35
35
  * once per anchor on every commit, and a scan would make that O(anchors × runs).
36
36
  */
37
37
  function lastRunAtOrBefore(runs, pos, coord) {
@@ -50,63 +50,56 @@ function lastRunAtOrBefore(runs, pos, coord) {
50
50
  }
51
51
  return found;
52
52
  }
53
- /** USV content offset → PM position. */
54
- export function usvToPM(index, usvPos) {
55
- const usv = clamp(usvPos, 0, index.usvEnd);
56
- if (usv >= index.usvEnd)
57
- return index.usvEnd === 0 ? index.firstContentStart : index.pmEndContent;
58
- // The runs tile the USV space from 0 (every text the walk appends pushes its run
59
- // first), so below `usvEnd` the run at or before `usv` is the run that owns it.
60
- const run = index.runs[lastRunAtOrBefore(index.runs, usv, usvStartOf)];
53
+ /** Content offset → PM position. */
54
+ export function offsetToPM(index, offset) {
55
+ const at = clamp(offset, 0, index.end);
56
+ if (at >= index.end)
57
+ return index.end === 0 ? index.firstContentStart : index.pmEndContent;
58
+ // The runs tile the content from 0 (every text the walk appends pushes its run first), so
59
+ // below `end` the run at or before `at` is the run that owns it.
60
+ const run = index.runs[lastRunAtOrBefore(index.runs, at, atOf)];
61
61
  if (run.kind === 'text')
62
- return run.pmStart + utf16OfCodePoints(run.s, usv - run.usvStart);
62
+ return run.pmStart + (at - run.at);
63
63
  return run.pmStart; // nl / atom: the run's single owned position
64
64
  }
65
- /** PM position → USV content offset. */
66
- export function pmToUsv(index, pmPos) {
65
+ /** PM position → content offset. */
66
+ export function pmToOffset(index, pmPos) {
67
67
  const i = lastRunAtOrBefore(index.runs, pmPos, pmStartOf);
68
68
  if (i < 0)
69
69
  return 0; // before the first run's PM position (or no runs at all)
70
70
  const run = index.runs[i];
71
- // PM coverage, unlike USV, has gaps no run owns (a rule's own token). A position
72
- // in one is past every run that could answer it → the end of the content text.
71
+ // PM coverage has gaps no run owns (a rule's own token). A position in one is past every run
72
+ // that could answer it → the end of the content text.
73
73
  if (pmPos >= runEndPm(run))
74
- return index.usvEnd;
74
+ return index.end;
75
75
  if (run.kind === 'text')
76
- return run.usvStart + codePointsOfUtf16(run.s, pmPos - run.pmStart);
77
- return run.usvStart;
78
- }
79
- // ── UTF-16 ↔ USV conversions within one text run ────────────────────────────
80
- /** UTF-16 length of the first `n` code points of `s`. */
81
- function utf16OfCodePoints(s, n) {
82
- let cp = 0;
83
- let i = 0;
84
- while (cp < n && i < s.length) {
85
- i += charUnitLen(s, i);
86
- cp++;
87
- }
88
- return i;
89
- }
90
- /** Number of whole code points within the first `k` UTF-16 units of `s`. */
91
- function codePointsOfUtf16(s, k) {
92
- let cp = 0;
93
- let i = 0;
94
- while (i < k && i < s.length) {
95
- i += charUnitLen(s, i);
96
- cp++;
97
- }
98
- return cp;
99
- }
100
- /** UTF-16 unit length (1 or 2) of the code point starting at `i`. */
101
- function charUnitLen(s, i) {
102
- const code = s.charCodeAt(i);
103
- if (code >= 0xd800 && code <= 0xdbff && i + 1 < s.length) {
104
- const next = s.charCodeAt(i + 1);
105
- if (next >= 0xdc00 && next <= 0xdfff)
106
- return 2;
107
- }
108
- return 1;
76
+ return run.at + (pmPos - run.pmStart);
77
+ return run.at;
109
78
  }
110
79
  function clamp(n, lo, hi) {
111
80
  return n < lo ? lo : n > hi ? hi : n;
112
81
  }
82
+ /**
83
+ * Where content offset `pos` lands after `delta`, its ops counting UTF-16 code units: text
84
+ * inserted at `pos` lands before it, so a caret at an edit's edge follows the text typed there.
85
+ */
86
+ export function mapThroughDelta(delta, pos) {
87
+ let at = 0;
88
+ let out = pos;
89
+ for (const op of delta.ops) {
90
+ if (at > pos)
91
+ break;
92
+ if ('retain' in op)
93
+ at += op.retain;
94
+ else if ('insert' in op)
95
+ out += op.insert.length;
96
+ else {
97
+ const gone = Math.min(op.delete, Math.max(0, pos - at));
98
+ out -= gone;
99
+ if (pos < at + op.delete)
100
+ break;
101
+ at += op.delete;
102
+ }
103
+ }
104
+ return out;
105
+ }
@@ -72,14 +72,6 @@ li.ProseMirror-selectednode::after {
72
72
  margin-top: var(--_qm-space-3);
73
73
  }
74
74
 
75
- /* A plain leaf's paragraph is a line of literal text (`schema.ts` §`plainSchema`), and
76
- a boundary between two stores the one `\n` a hard break does. So no rhythm: a gap
77
- would draw a blank line the value does not hold, and the two spellings of one string
78
- would read apart. */
79
- .ProseMirror > [data-qm-line] + [data-qm-line] {
80
- margin-top: 0;
81
- }
82
-
83
75
  /* A blockquote's UA indent was its only mark, and the reset above takes it. The rule
84
76
  and the inset carry it instead: the same hairline every surface reads, so a quote
85
77
  reads as set apart without a fill or a second box. */
@@ -22,10 +22,10 @@ export declare const plaintextSchema: Schema<string, never>;
22
22
  export declare const cellSchema: Schema<"hard_break", string>;
23
23
  /**
24
24
  * A `text` field declaring `multiline: true`: paragraphs and hard breaks, no marks, islands or
25
- * containers, the shape `Content::is_plain` admits. A paragraph and a break both store one `\n`, so
26
- * a paragraph here is a line (`data-qm-line`): `prose.css` draws a run of them at the leading.
25
+ * containers, the shape `Content::is_plain` admits. Its literal text spells a break as a `\n` and a
26
+ * paragraph boundary as a blank line.
27
27
  */
28
- export declare const plainSchema: Schema<"text" | "doc" | "paragraph" | "hard_break", never>;
28
+ export declare const plainSchema: Schema<"text" | "paragraph" | "hard_break" | "doc", never>;
29
29
  /** The schema a leaf of the declared type mounts: `inline` narrows either type to one
30
30
  * textblock, and `plaintext` takes its marks away. */
31
31
  export declare function leafSchema(opts: {
@@ -95,6 +95,13 @@ const marks = {
95
95
  ];
96
96
  }
97
97
  },
98
+ // A content mark this build names no PM mark for, by its `x-` type and attributes as written. It
99
+ // formats nothing, and several stack on one character.
100
+ ext: {
101
+ attrs: { type: {}, attrs: { default: null } },
102
+ excludes: '',
103
+ toDOM: () => ['span', 0]
104
+ },
98
105
  link: {
99
106
  attrs: { href: { default: '' } },
100
107
  inclusive: false,
@@ -134,22 +141,26 @@ function fenceLang(pre) {
134
141
  return null;
135
142
  }
136
143
  // ── Block nodes ─────────────────────────────────────────────────────────────
144
+ // Every block and container carries the id of the content part it decodes from, `null` on one a
145
+ // command or a paste made. No `toDOM` writes it, so a copy carries none, and the projection keeps the
146
+ // first holder of an id a split copied (`encode.ts`).
147
+ const ID = { default: null };
137
148
  const blockNodes = {
138
149
  doc: { content: 'block+' },
139
150
  paragraph: {
140
151
  content: 'inline*',
141
152
  group: 'block',
153
+ attrs: { id: ID, ext: { default: null } },
142
154
  parseDOM: [{ tag: 'p' }],
143
155
  toDOM: () => ['p', 0]
144
156
  },
145
- // A heading holds no `hard_break`: the store clears a `continues` after one and markdown has no
146
- // syntax for a break inside it. Declaring it out makes that structural rather than a rule each
147
- // door holds.
157
+ // A heading holds no `hard_break`: the store refuses a break in one and markdown has no syntax
158
+ // for it. Declaring it out makes that structural rather than a rule each door holds.
148
159
  heading: {
149
160
  content: '(text | island_inline)*',
150
161
  group: 'block',
151
162
  defining: true,
152
- attrs: { level: { default: 1 } },
163
+ attrs: { id: ID, level: { default: 1 } },
153
164
  parseDOM: [1, 2, 3, 4, 5, 6].map((level) => ({ tag: `h${level}`, attrs: { level } })),
154
165
  toDOM: (node) => [`h${node.attrs.level}`, 0]
155
166
  },
@@ -160,7 +171,7 @@ const blockNodes = {
160
171
  defining: true,
161
172
  marks: '',
162
173
  whitespace: 'pre',
163
- attrs: { lang: { default: null } },
174
+ attrs: { id: ID, lang: { default: null } },
164
175
  parseDOM: [
165
176
  {
166
177
  tag: 'pre',
@@ -178,14 +189,20 @@ const blockNodes = {
178
189
  content: 'block+',
179
190
  group: 'block',
180
191
  defining: true,
192
+ attrs: { id: ID },
181
193
  parseDOM: [{ tag: 'blockquote' }],
182
194
  toDOM: () => ['blockquote', 0]
183
195
  },
184
- horizontal_rule: { group: 'block', parseDOM: [{ tag: 'hr' }], toDOM: () => ['hr'] },
196
+ horizontal_rule: {
197
+ group: 'block',
198
+ attrs: { id: ID },
199
+ parseDOM: [{ tag: 'hr' }],
200
+ toDOM: () => ['hr']
201
+ },
185
202
  ordered_list: {
186
203
  content: 'list_item+',
187
204
  group: 'block',
188
- attrs: { start: { default: 1 } },
205
+ attrs: { id: ID, start: { default: 1 } },
189
206
  parseDOM: [
190
207
  {
191
208
  tag: 'ol',
@@ -206,6 +223,7 @@ const blockNodes = {
206
223
  bullet_list: {
207
224
  content: 'list_item+',
208
225
  group: 'block',
226
+ attrs: { id: ID },
209
227
  parseDOM: [{ tag: 'ul' }],
210
228
  toDOM: () => ['ul', 0]
211
229
  },
@@ -213,7 +231,7 @@ const blockNodes = {
213
231
  list_item: {
214
232
  content: 'block+',
215
233
  defining: true,
216
- attrs: { checked: { default: null } },
234
+ attrs: { id: ID, checked: { default: null } },
217
235
  parseDOM: [
218
236
  {
219
237
  tag: 'li',
@@ -275,13 +293,13 @@ export const cellSchema = new Schema({
275
293
  });
276
294
  /**
277
295
  * A `text` field declaring `multiline: true`: paragraphs and hard breaks, no marks, islands or
278
- * containers, the shape `Content::is_plain` admits. A paragraph and a break both store one `\n`, so
279
- * a paragraph here is a line (`data-qm-line`): `prose.css` draws a run of them at the leading.
296
+ * containers, the shape `Content::is_plain` admits. Its literal text spells a break as a `\n` and a
297
+ * paragraph boundary as a blank line.
280
298
  */
281
299
  export const plainSchema = new Schema({
282
300
  nodes: {
283
301
  doc: { content: 'paragraph+' },
284
- paragraph: { ...blockNodes.paragraph, toDOM: () => ['p', { 'data-qm-line': '' }, 0] },
302
+ paragraph: blockNodes.paragraph,
285
303
  text: inlineLeafNodes.text,
286
304
  hard_break: inlineLeafNodes.hard_break
287
305
  },