@open-docex/shared 0.0.0-stage → 0.1.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 (111) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +101 -2
  3. package/dist/errors/catalogue.d.ts +30 -0
  4. package/dist/errors/catalogue.d.ts.map +1 -0
  5. package/dist/errors/catalogue.js +52 -0
  6. package/dist/errors/catalogue.js.map +1 -0
  7. package/dist/errors/index.d.ts +9 -0
  8. package/dist/errors/index.d.ts.map +1 -0
  9. package/dist/errors/index.js +9 -0
  10. package/dist/errors/index.js.map +1 -0
  11. package/dist/errors/open-docex-error.d.ts +81 -0
  12. package/dist/errors/open-docex-error.d.ts.map +1 -0
  13. package/dist/errors/open-docex-error.js +127 -0
  14. package/dist/errors/open-docex-error.js.map +1 -0
  15. package/dist/ids/index.d.ts +41 -0
  16. package/dist/ids/index.d.ts.map +1 -0
  17. package/dist/ids/index.js +70 -0
  18. package/dist/ids/index.js.map +1 -0
  19. package/dist/index.d.ts +16 -0
  20. package/dist/index.d.ts.map +1 -0
  21. package/dist/index.js +16 -0
  22. package/dist/index.js.map +1 -0
  23. package/dist/ot/index.d.ts +4 -0
  24. package/dist/ot/index.d.ts.map +1 -0
  25. package/dist/ot/index.js +3 -0
  26. package/dist/ot/index.js.map +1 -0
  27. package/dist/ot/types.d.ts +55 -0
  28. package/dist/ot/types.d.ts.map +1 -0
  29. package/dist/ot/types.js +11 -0
  30. package/dist/ot/types.js.map +1 -0
  31. package/dist/protocol/close-codes.d.ts +39 -0
  32. package/dist/protocol/close-codes.d.ts.map +1 -0
  33. package/dist/protocol/close-codes.js +47 -0
  34. package/dist/protocol/close-codes.js.map +1 -0
  35. package/dist/protocol/codec.d.ts +32 -0
  36. package/dist/protocol/codec.d.ts.map +1 -0
  37. package/dist/protocol/codec.js +270 -0
  38. package/dist/protocol/codec.js.map +1 -0
  39. package/dist/protocol/frames.d.ts +45 -0
  40. package/dist/protocol/frames.d.ts.map +1 -0
  41. package/dist/protocol/frames.js +125 -0
  42. package/dist/protocol/frames.js.map +1 -0
  43. package/dist/protocol/index.d.ts +12 -0
  44. package/dist/protocol/index.d.ts.map +1 -0
  45. package/dist/protocol/index.js +12 -0
  46. package/dist/protocol/index.js.map +1 -0
  47. package/dist/protocol/messages.d.ts +250 -0
  48. package/dist/protocol/messages.d.ts.map +1 -0
  49. package/dist/protocol/messages.js +7 -0
  50. package/dist/protocol/messages.js.map +1 -0
  51. package/dist/protocol/validate.d.ts +61 -0
  52. package/dist/protocol/validate.d.ts.map +1 -0
  53. package/dist/protocol/validate.js +139 -0
  54. package/dist/protocol/validate.js.map +1 -0
  55. package/dist/text/apply.d.ts +27 -0
  56. package/dist/text/apply.d.ts.map +1 -0
  57. package/dist/text/apply.js +151 -0
  58. package/dist/text/apply.js.map +1 -0
  59. package/dist/text/chunk.d.ts +33 -0
  60. package/dist/text/chunk.d.ts.map +1 -0
  61. package/dist/text/chunk.js +152 -0
  62. package/dist/text/chunk.js.map +1 -0
  63. package/dist/text/equal.d.ts +4 -0
  64. package/dist/text/equal.d.ts.map +1 -0
  65. package/dist/text/equal.js +16 -0
  66. package/dist/text/equal.js.map +1 -0
  67. package/dist/text/href.d.ts +13 -0
  68. package/dist/text/href.d.ts.map +1 -0
  69. package/dist/text/href.js +31 -0
  70. package/dist/text/href.js.map +1 -0
  71. package/dist/text/index.d.ts +18 -0
  72. package/dist/text/index.d.ts.map +1 -0
  73. package/dist/text/index.js +14 -0
  74. package/dist/text/index.js.map +1 -0
  75. package/dist/text/ot.d.ts +7 -0
  76. package/dist/text/ot.d.ts.map +1 -0
  77. package/dist/text/ot.js +136 -0
  78. package/dist/text/ot.js.map +1 -0
  79. package/dist/text/page.d.ts +35 -0
  80. package/dist/text/page.d.ts.map +1 -0
  81. package/dist/text/page.js +31 -0
  82. package/dist/text/page.js.map +1 -0
  83. package/dist/text/rebase.d.ts +50 -0
  84. package/dist/text/rebase.d.ts.map +1 -0
  85. package/dist/text/rebase.js +59 -0
  86. package/dist/text/rebase.js.map +1 -0
  87. package/dist/text/schema.d.ts +43 -0
  88. package/dist/text/schema.d.ts.map +1 -0
  89. package/dist/text/schema.js +482 -0
  90. package/dist/text/schema.js.map +1 -0
  91. package/dist/text/steps.d.ts +17 -0
  92. package/dist/text/steps.d.ts.map +1 -0
  93. package/dist/text/steps.js +14 -0
  94. package/dist/text/steps.js.map +1 -0
  95. package/dist/util/canonical-json.d.ts +16 -0
  96. package/dist/util/canonical-json.d.ts.map +1 -0
  97. package/dist/util/canonical-json.js +28 -0
  98. package/dist/util/canonical-json.js.map +1 -0
  99. package/dist/util/index.d.ts +10 -0
  100. package/dist/util/index.d.ts.map +1 -0
  101. package/dist/util/index.js +10 -0
  102. package/dist/util/index.js.map +1 -0
  103. package/dist/util/random.d.ts +17 -0
  104. package/dist/util/random.d.ts.map +1 -0
  105. package/dist/util/random.js +40 -0
  106. package/dist/util/random.js.map +1 -0
  107. package/dist/util/utf8.d.ts +13 -0
  108. package/dist/util/utf8.d.ts.map +1 -0
  109. package/dist/util/utf8.js +38 -0
  110. package/dist/util/utf8.js.map +1 -0
  111. package/package.json +57 -4
@@ -0,0 +1,136 @@
1
+ /**
2
+ * `TextOtType`: the OT Strategy for text documents (LLD Part 1 §8.5, §7, §0 S-20,
3
+ * S-21, S-27). Snapshots are ProseMirror `doc` nodes of {@link textSchema}; ops are
4
+ * `Step.toJSON()` output.
5
+ */
6
+ import { Node as PMNode } from 'prosemirror-model';
7
+ import { Step, Transform } from 'prosemirror-transform';
8
+ import { OpenDocexError } from '../errors/index.js';
9
+ import { applyStepsWithDrop, colwidthFits } from './apply.js';
10
+ import { assembleText, chunkText } from './chunk.js';
11
+ import { getPageSettings, pageSettingsFit } from './page.js';
12
+ import { rebaseSteps } from './rebase.js';
13
+ import { textSchema } from './schema.js';
14
+ import { parseTextOps } from './steps.js';
15
+ const STEP_TYPES = new Set([
16
+ 'replace',
17
+ 'replaceAround',
18
+ 'addMark',
19
+ 'removeMark',
20
+ 'markSet',
21
+ 'addNodeMark',
22
+ 'removeNodeMark',
23
+ 'attr',
24
+ 'docAttr',
25
+ ]);
26
+ /** Position fields that must be safe integers ≥ 0 when present (§8.2). */
27
+ const POSITION_FIELDS = ['from', 'to', 'gapFrom', 'gapTo', 'insert', 'pos'];
28
+ /** `attr` steps may name any node attribute of the schema (§8.2). */
29
+ const NODE_ATTRS = new Set(Object.values(textSchema.nodes).flatMap((t) => Object.keys(t.spec.attrs ?? {})));
30
+ /** `docAttr` steps may change only the page settings (§8.2, §0 S-18). */
31
+ const DOC_ATTRS = new Set(['size', 'orientation', 'margins']);
32
+ function isPlainObject(v) {
33
+ if (typeof v !== 'object' || v === null || Array.isArray(v))
34
+ return false;
35
+ const proto = Object.getPrototypeOf(v);
36
+ return proto === Object.prototype || proto === null;
37
+ }
38
+ function rejectOp(detail) {
39
+ throw new OpenDocexError('PROTOCOL_OP_REJECTED', `Invalid text op: ${detail}`);
40
+ }
41
+ /**
42
+ * Checks untrusted ops without the document and returns them normalized (unknown fields
43
+ * dropped). Content validity, `attr` values and positions need the document and are
44
+ * checked in `apply`. Throws `PROTOCOL_OP_REJECTED`.
45
+ */
46
+ function validateTextOps(x) {
47
+ if (!Array.isArray(x) || x.length === 0)
48
+ rejectOp('not a non-empty array');
49
+ const out = [];
50
+ for (const j of x) {
51
+ if (!isPlainObject(j) || typeof j.stepType !== 'string' || !STEP_TYPES.has(j.stepType))
52
+ rejectOp('stepType');
53
+ for (const f of POSITION_FIELDS) {
54
+ const v = j[f];
55
+ if (v !== undefined && !(Number.isSafeInteger(v) && v >= 0))
56
+ rejectOp(f);
57
+ }
58
+ if (j.stepType === 'attr' || j.stepType === 'docAttr') {
59
+ const allowed = j.stepType === 'attr' ? NODE_ATTRS : DOC_ATTRS;
60
+ if (typeof j.attr !== 'string' || !allowed.has(j.attr))
61
+ rejectOp('attr');
62
+ }
63
+ let step;
64
+ try {
65
+ // Unknown node and mark types and attribute validators throw here.
66
+ step = Step.fromJSON(textSchema, j);
67
+ }
68
+ catch {
69
+ rejectOp('step');
70
+ }
71
+ out.push(step.toJSON());
72
+ }
73
+ return out;
74
+ }
75
+ /**
76
+ * Applies ops to a document. A step that fails is dropped with its dependents (§8.4).
77
+ * An op that does not parse is dropped too, so `apply` never throws for a bad op.
78
+ */
79
+ function applyTextOps(doc, ops) {
80
+ const steps = [];
81
+ let unparsed = 0;
82
+ for (const op of ops) {
83
+ try {
84
+ steps.push(Step.fromJSON(textSchema, op));
85
+ }
86
+ catch {
87
+ unparsed += 1;
88
+ }
89
+ }
90
+ const tr = new Transform(doc);
91
+ const r = applyStepsWithDrop(tr, steps);
92
+ return { snapshot: tr.doc, applied: r.applied.map((s) => s.toJSON()), dropped: r.dropped + unparsed };
93
+ }
94
+ /**
95
+ * Rebuilds a snapshot from JSON. Every failure throws `DOCUMENT_FAULTED` (a corrupt
96
+ * local snapshot): bad JSON, content or attributes, a root that is not `doc`, and the
97
+ * cross-attribute rules of §0 S-27 (`colwidth` length = `colspan`, margins fit the page).
98
+ */
99
+ function deserializeText(j) {
100
+ let doc;
101
+ try {
102
+ doc = PMNode.fromJSON(textSchema, j);
103
+ doc.check();
104
+ }
105
+ catch (err) {
106
+ throw new OpenDocexError('DOCUMENT_FAULTED', 'Corrupt text snapshot', { cause: err });
107
+ }
108
+ if (doc.type !== textSchema.topNodeType)
109
+ throw new OpenDocexError('DOCUMENT_FAULTED', 'Text snapshot root is not doc');
110
+ if (!pageSettingsFit(getPageSettings(doc))) {
111
+ throw new OpenDocexError('DOCUMENT_FAULTED', 'Text snapshot margins do not fit the page');
112
+ }
113
+ let cellsFit = true;
114
+ doc.descendants((node) => {
115
+ if (!cellsFit)
116
+ return false;
117
+ if ((node.type.name === 'table_cell' || node.type.name === 'table_header') && !colwidthFits(node))
118
+ cellsFit = false;
119
+ return cellsFit;
120
+ });
121
+ if (!cellsFit)
122
+ throw new OpenDocexError('DOCUMENT_FAULTED', 'Text snapshot colwidth does not match colspan');
123
+ return doc;
124
+ }
125
+ /** The OT Strategy for text documents (§8.5). Every method is pure and deterministic. */
126
+ export const TextOtType = Object.freeze({
127
+ kind: 'text',
128
+ apply: applyTextOps,
129
+ rebase: (ops, over) => rebaseSteps(parseTextOps(ops), parseTextOps(over)).map((s) => s.toJSON()),
130
+ validateOps: validateTextOps,
131
+ serializeSnapshot: (doc) => doc.toJSON(),
132
+ deserializeSnapshot: deserializeText,
133
+ chunk: chunkText,
134
+ assemble: assembleText,
135
+ });
136
+ //# sourceMappingURL=ot.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ot.js","sourceRoot":"","sources":["../../src/text/ot.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAE,IAAI,IAAI,MAAM,EAAE,MAAM,mBAAmB,CAAC;AACnD,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAEpD,OAAO,EAAE,kBAAkB,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAC9D,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAErD,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AAC7D,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAG1C,MAAM,UAAU,GAAwB,IAAI,GAAG,CAAe;IAC5D,SAAS;IACT,eAAe;IACf,SAAS;IACT,YAAY;IACZ,SAAS;IACT,aAAa;IACb,gBAAgB;IAChB,MAAM;IACN,SAAS;CACV,CAAC,CAAC;AAEH,0EAA0E;AAC1E,MAAM,eAAe,GAAG,CAAC,MAAM,EAAE,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE,QAAQ,EAAE,KAAK,CAAU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,IAAI,CAAC,IAAI,CAAC,IAAI,KAAK,YAAY,IAAI,IAAI,CAAC,IAAI,CAAC,IAAI,KAAK,cAAc,CAAC,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC;YAAE,QAAQ,GAAG,KAAK,CAAC;QACpH,OAAO,QAAQ,CAAC;IAClB,CAAC,CAAC,CAAC;IACH,IAAI,CAAC,QAAQ;QAAE,MAAM,IAAI,cAAc,CAAC,kBAAkB,EAAE,+CAA+C,CAAC,CAAC;IAC7G,OAAO,GAAG,CAAC;AACb,CAAC;AAED,yFAAyF;AACzF,MAAM,CAAC,MAAM,UAAU,GAAgD,MAAM,CAAC,MAAM,CAAC;IACnF,IAAI,EAAE,MAAe;IACrB,KAAK,EAAE,YAAY;IACnB,MAAM,EAAE,CAAC,GAAa,EAAE,IAAc,EAAY,EAAE,CAClD,WAAW,CAAC,YAAY,CAAC,GAAG,CAAC,EAAE,YAAY,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,EAAY,CAAC;IACrF,WAAW,EAAE,eAAe;IAC5B,iBAAiB,EAAE,CAAC,GAAiB,EAAW,EAAE,CAAC,GAAG,CAAC,MAAM,EAAE;IAC/D,mBAAmB,EAAE,eAAe;IACpC,KAAK,EAAE,SAAS;IAChB,QAAQ,EAAE,YAAY;CACvB,CAAC,CAAC"}
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Page settings of a text document (LLD Part 1 §8.1, CL-9).
3
+ *
4
+ * There is one page style per document, held in the `doc` attributes `size`,
5
+ * `orientation` and `margins`. Page settings change through `DocAttrStep`, one attribute
6
+ * at a time, so concurrent changes to different attributes all survive. All lengths are
7
+ * in points.
8
+ */
9
+ import { DocAttrStep } from 'prosemirror-transform';
10
+ import type { TextSnapshot } from './schema.js';
11
+ /** The page style of a text document. Lengths in pt. */
12
+ export interface PageSettings {
13
+ size: {
14
+ width: number;
15
+ height: number;
16
+ };
17
+ orientation: 'portrait' | 'landscape';
18
+ margins: {
19
+ top: number;
20
+ right: number;
21
+ bottom: number;
22
+ left: number;
23
+ };
24
+ }
25
+ /** Reads the page settings from a `doc` node's attributes. */
26
+ export declare function getPageSettings(doc: TextSnapshot): PageSettings;
27
+ /** A step that sets one page attribute. Its value is validated when the step applies. */
28
+ export declare function pageSettingStep(key: keyof PageSettings, value: PageSettings[keyof PageSettings]): DocAttrStep;
29
+ /**
30
+ * True when the margins leave at least 36 pt of body in each direction:
31
+ * `left + right ≤ width − 36` and `top + bottom ≤ height − 36` (§8.1, `doc` attributes).
32
+ * This check spans two attributes, so the per-attribute validators can't make it.
33
+ */
34
+ export declare function pageSettingsFit(p: PageSettings): boolean;
35
+ //# sourceMappingURL=page.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"page.d.ts","sourceRoot":"","sources":["../../src/text/page.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAEhD,wDAAwD;AACxD,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,CAAC;IACxC,WAAW,EAAE,UAAU,GAAG,WAAW,CAAC;IACtC,OAAO,EAAE;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;CACvE;AAED,8DAA8D;AAC9D,wBAAgB,eAAe,CAAC,GAAG,EAAE,YAAY,GAAG,YAAY,CAO/D;AAED,yFAAyF;AACzF,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,YAAY,EAAE,KAAK,EAAE,YAAY,CAAC,MAAM,YAAY,CAAC,GAAG,WAAW,CAE7G;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,CAAC,EAAE,YAAY,GAAG,OAAO,CAIxD"}
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Page settings of a text document (LLD Part 1 §8.1, CL-9).
3
+ *
4
+ * There is one page style per document, held in the `doc` attributes `size`,
5
+ * `orientation` and `margins`. Page settings change through `DocAttrStep`, one attribute
6
+ * at a time, so concurrent changes to different attributes all survive. All lengths are
7
+ * in points.
8
+ */
9
+ import { DocAttrStep } from 'prosemirror-transform';
10
+ /** Reads the page settings from a `doc` node's attributes. */
11
+ export function getPageSettings(doc) {
12
+ const a = doc.attrs;
13
+ return {
14
+ size: a.size,
15
+ orientation: a.orientation,
16
+ margins: a.margins,
17
+ };
18
+ }
19
+ /** A step that sets one page attribute. Its value is validated when the step applies. */
20
+ export function pageSettingStep(key, value) {
21
+ return new DocAttrStep(key, value);
22
+ }
23
+ /**
24
+ * True when the margins leave at least 36 pt of body in each direction:
25
+ * `left + right ≤ width − 36` and `top + bottom ≤ height − 36` (§8.1, `doc` attributes).
26
+ * This check spans two attributes, so the per-attribute validators can't make it.
27
+ */
28
+ export function pageSettingsFit(p) {
29
+ return (p.margins.left + p.margins.right <= p.size.width - 36 && p.margins.top + p.margins.bottom <= p.size.height - 36);
30
+ }
31
+ //# sourceMappingURL=page.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"page.js","sourceRoot":"","sources":["../../src/text/page.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAUpD,8DAA8D;AAC9D,MAAM,UAAU,eAAe,CAAC,GAAiB;IAC/C,MAAM,CAAC,GAAG,GAAG,CAAC,KAAK,CAAC;IACpB,OAAO;QACL,IAAI,EAAE,CAAC,CAAC,IAA4B;QACpC,WAAW,EAAE,CAAC,CAAC,WAA0C;QACzD,OAAO,EAAE,CAAC,CAAC,OAAkC;KAC9C,CAAC;AACJ,CAAC;AAED,yFAAyF;AACzF,MAAM,UAAU,eAAe,CAAC,GAAuB,EAAE,KAAuC;IAC9F,OAAO,IAAI,WAAW,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;AACrC,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,eAAe,CAAC,CAAe;IAC7C,OAAO,CACL,CAAC,CAAC,OAAO,CAAC,IAAI,GAAG,CAAC,CAAC,OAAO,CAAC,KAAK,IAAI,CAAC,CAAC,IAAI,CAAC,KAAK,GAAG,EAAE,IAAI,CAAC,CAAC,OAAO,CAAC,GAAG,GAAG,CAAC,CAAC,OAAO,CAAC,MAAM,IAAI,CAAC,CAAC,IAAI,CAAC,MAAM,GAAG,EAAE,CAChH,CAAC;AACJ,CAAC"}
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Mapping-only rebase of text steps (LLD Part 1 §8.3, §0 R-6).
3
+ *
4
+ * `rebaseSteps` needs no document, so the server and every client compute the same
5
+ * result from the same `(steps, over)`. It uses only `getMap`, `StepMap.invert`,
6
+ * `Step.map`, `Mapping.slice` and `Mapping.appendMap`. Unlike prosemirror-collab, a
7
+ * mirror is recorded for every step that maps, so content of earlier own steps is
8
+ * recovered even when a later step fails to apply; failures are handled by
9
+ * {@link applyStepsWithDrop}.
10
+ *
11
+ * Prefix consistency: `rebaseSteps([...A, ...B], R)` starts with `rebaseSteps(A, R)`.
12
+ */
13
+ import type { Node as PMNode } from 'prosemirror-model';
14
+ import type { Step, StepMap } from 'prosemirror-transform';
15
+ /** Rebased steps plus, for each, the index of its source in the input. */
16
+ export interface IndexedSteps {
17
+ steps: Step[];
18
+ index: number[];
19
+ }
20
+ /**
21
+ * {@link rebaseOverMaps} with index tracking. `index[j]` is `inputIndex[k]` for the
22
+ * input step `k` that produced `steps[j]`. Internal.
23
+ */
24
+ export declare function rebaseOverMapsIndexed(steps: readonly Step[], overMaps: readonly StepMap[], inputIndex: readonly number[]): IndexedSteps;
25
+ /**
26
+ * Rebases `steps` (made on some document D) over the step maps `overMaps` (also made on
27
+ * D, in order). Steps the mapping deletes are left out. Internal: callers outside the
28
+ * text module use {@link rebaseSteps}.
29
+ */
30
+ export declare function rebaseOverMaps(steps: readonly Step[], overMaps: readonly StepMap[]): Step[];
31
+ /**
32
+ * Rebases `steps` over `over`, both made on the same document and each in FIFO order.
33
+ * Pure and document-free: the result depends only on the inputs. Steps whose range was
34
+ * deleted by `over` are left out. This is the form behind `TextOtType.rebase`.
35
+ */
36
+ export declare function rebaseSteps(steps: readonly Step[], over: readonly Step[]): Step[];
37
+ /**
38
+ * Rebases `steps` over `over` and applies the result to `doc` with the
39
+ * {@link applyStepsWithDrop} semantics (§0 R-6). `doc` is the document with `over`
40
+ * already applied. A step that fails to apply is dropped, and so are its dependents.
41
+ *
42
+ * @returns `steps`: the surviving rebased steps; `kept[j]`: the index in the input
43
+ * `steps` of `steps[j]`; `doc`: `doc` with the surviving steps applied.
44
+ */
45
+ export declare function rebaseStepsOnDoc(steps: readonly Step[], over: readonly Step[], doc: PMNode): {
46
+ steps: Step[];
47
+ kept: number[];
48
+ doc: PMNode;
49
+ };
50
+ //# sourceMappingURL=rebase.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rebase.d.ts","sourceRoot":"","sources":["../../src/text/rebase.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,KAAK,EAAE,IAAI,IAAI,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAExD,OAAO,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAG3D,0EAA0E;AAC1E,MAAM,WAAW,YAAY;IAC3B,KAAK,EAAE,IAAI,EAAE,CAAC;IACd,KAAK,EAAE,MAAM,EAAE,CAAC;CACjB;AAED;;;GAGG;AACH,wBAAgB,qBAAqB,CACnC,KAAK,EAAE,SAAS,IAAI,EAAE,EACtB,QAAQ,EAAE,SAAS,OAAO,EAAE,EAC5B,UAAU,EAAE,SAAS,MAAM,EAAE,GAC5B,YAAY,CAmBd;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,SAAS,IAAI,EAAE,EAAE,QAAQ,EAAE,SAAS,OAAO,EAAE,GAAG,IAAI,EAAE,CAM3F;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,SAAS,IAAI,EAAE,EAAE,IAAI,EAAE,SAAS,IAAI,EAAE,GAAG,IAAI,EAAE,CAKjF;AAED;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,SAAS,IAAI,EAAE,EACtB,IAAI,EAAE,SAAS,IAAI,EAAE,EACrB,GAAG,EAAE,MAAM,GACV;IAAE,KAAK,EAAE,IAAI,EAAE,CAAC;IAAC,IAAI,EAAE,MAAM,EAAE,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,CAShD"}
@@ -0,0 +1,59 @@
1
+ import { Mapping, Transform } from 'prosemirror-transform';
2
+ import { applyIndexedWithDrop } from './apply.js';
3
+ /**
4
+ * {@link rebaseOverMaps} with index tracking. `index[j]` is `inputIndex[k]` for the
5
+ * input step `k` that produced `steps[j]`. Internal.
6
+ */
7
+ export function rebaseOverMapsIndexed(steps, overMaps, inputIndex) {
8
+ const n = steps.length;
9
+ const mapping = new Mapping();
10
+ // steps[i]⁻¹ sits at index n-1-i.
11
+ for (let i = n - 1; i >= 0; i--)
12
+ mapping.appendMap(steps[i].getMap().invert());
13
+ for (const m of overMaps)
14
+ mapping.appendMap(m);
15
+ const out = [];
16
+ const index = [];
17
+ for (let i = 0; i < n; i++) {
18
+ // slice(n - i) skips the inverses of steps[i..n-1].
19
+ const mapped = steps[i].map(mapping.slice(n - i));
20
+ if (mapped) {
21
+ // Mirror of steps[i]⁻¹, so positions inside earlier own content are recovered.
22
+ mapping.appendMap(mapped.getMap(), n - 1 - i);
23
+ out.push(mapped);
24
+ index.push(inputIndex[i]);
25
+ }
26
+ }
27
+ return { steps: out, index };
28
+ }
29
+ /**
30
+ * Rebases `steps` (made on some document D) over the step maps `overMaps` (also made on
31
+ * D, in order). Steps the mapping deletes are left out. Internal: callers outside the
32
+ * text module use {@link rebaseSteps}.
33
+ */
34
+ export function rebaseOverMaps(steps, overMaps) {
35
+ return rebaseOverMapsIndexed(steps, overMaps, steps.map((_, i) => i)).steps;
36
+ }
37
+ /**
38
+ * Rebases `steps` over `over`, both made on the same document and each in FIFO order.
39
+ * Pure and document-free: the result depends only on the inputs. Steps whose range was
40
+ * deleted by `over` are left out. This is the form behind `TextOtType.rebase`.
41
+ */
42
+ export function rebaseSteps(steps, over) {
43
+ return rebaseOverMaps(steps, over.map((s) => s.getMap()));
44
+ }
45
+ /**
46
+ * Rebases `steps` over `over` and applies the result to `doc` with the
47
+ * {@link applyStepsWithDrop} semantics (§0 R-6). `doc` is the document with `over`
48
+ * already applied. A step that fails to apply is dropped, and so are its dependents.
49
+ *
50
+ * @returns `steps`: the surviving rebased steps; `kept[j]`: the index in the input
51
+ * `steps` of `steps[j]`; `doc`: `doc` with the surviving steps applied.
52
+ */
53
+ export function rebaseStepsOnDoc(steps, over, doc) {
54
+ const rebased = rebaseOverMapsIndexed(steps, over.map((s) => s.getMap()), steps.map((_, i) => i));
55
+ const tr = new Transform(doc);
56
+ const r = applyIndexedWithDrop(tr, rebased.steps, rebased.index);
57
+ return { steps: r.applied, kept: r.index, doc: tr.doc };
58
+ }
59
+ //# sourceMappingURL=rebase.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rebase.js","sourceRoot":"","sources":["../../src/text/rebase.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAE3D,OAAO,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAQlD;;;GAGG;AACH,MAAM,UAAU,qBAAqB,CACnC,KAAsB,EACtB,QAA4B,EAC5B,UAA6B;IAE7B,MAAM,CAAC,GAAG,KAAK,CAAC,MAAM,CAAC;IACvB,MAAM,OAAO,GAAG,IAAI,OAAO,EAA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CAAC,KAAsB,EAAE,IAAqB;IACvE,OAAO,cAAc,CACnB,KAAK,EACL,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAC5B,CAAC;AACJ,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,gBAAgB,CAC9B,KAAsB,EACtB,IAAqB,EACrB,GAAW;IAEX,MAAM,OAAO,GAAG,qBAAqB,CACnC,KAAK,EACL,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,EAC3B,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CACvB,CAAC;IACF,MAAM,EAAE,GAAG,IAAI,SAAS,CAAC,GAAG,CAAC,CAAC;IAC9B,MAAM,CAAC,GAAG,oBAAoB,CAAC,EAAE,EAAE,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,KAAK,CAAC,CAAC;IACjE,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE,CAAC,GAAG,EAAE,CAAC;AAC1D,CAAC"}
@@ -0,0 +1,43 @@
1
+ /**
2
+ * The ProseMirror schema for text documents (LLD Part 1 §8.1; §0 R-14, R-17, L-2,
3
+ * S-18, S-19, S-20).
4
+ *
5
+ * Every attribute has a validator, which ProseMirror runs on `create` and again in
6
+ * `Node.check()`. Every `toDOM` output is built only from validated attribute values,
7
+ * and ProseMirror's `DOMSerializer` sets attributes, never HTML. Specs have no
8
+ * `parseDOM` unless the LLD lists one, so pasted HTML can never create the
9
+ * `header`/`body`/`footer` regions or a `page_field`.
10
+ *
11
+ * Schema evolution: node types, mark types and attributes are only ever added, and
12
+ * every new attribute has a default, so older snapshots and journals keep parsing.
13
+ */
14
+ import { Schema } from 'prosemirror-model';
15
+ import type { Node as PMNode } from 'prosemirror-model';
16
+ /** Every node type name in {@link textSchema}. */
17
+ export type TextNodeName = 'doc' | 'header' | 'body' | 'footer' | 'paragraph' | 'heading' | 'bullet_list' | 'ordered_list' | 'list_item' | 'table' | 'table_row' | 'table_cell' | 'table_header' | 'image' | 'page_break' | 'text' | 'hard_break' | 'page_field';
18
+ /** Every mark type name in {@link textSchema}, in rank order. */
19
+ export type TextMarkName = 'link' | 'bold' | 'italic' | 'underline' | 'strike' | 'text_style';
20
+ /** A text document: a prosemirror-model `Node` of {@link textSchema} with type `doc`. */
21
+ export type TextSnapshot = PMNode;
22
+ /** Paragraph alignment, used by `paragraph.align` and `heading.align`. */
23
+ export type TextAlign = 'left' | 'center' | 'right' | 'justify';
24
+ /** `paragraph.lineHeight` and `heading.lineHeight` (§0 S-19). `pt` values are points. */
25
+ export type LineHeight = {
26
+ kind: 'multiple';
27
+ value: number;
28
+ } | {
29
+ kind: 'exact';
30
+ pt: number;
31
+ } | {
32
+ kind: 'atLeast';
33
+ pt: number;
34
+ };
35
+ /** The `doc` attribute `textDefaults` (§0 R-14, S-18): the document's default run style. */
36
+ export interface TextDefaults {
37
+ fontFamily: string | null;
38
+ fontSize: number | null;
39
+ color: string | null;
40
+ }
41
+ /** The text document schema (LLD Part 1 §8.1). */
42
+ export declare const textSchema: Schema<TextNodeName, TextMarkName>;
43
+ //# sourceMappingURL=schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/text/schema.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAC3C,OAAO,KAAK,EAAgD,IAAI,IAAI,MAAM,EAAY,MAAM,mBAAmB,CAAC;AAKhH,kDAAkD;AAClD,MAAM,MAAM,YAAY,GACpB,KAAK,GACL,QAAQ,GACR,MAAM,GACN,QAAQ,GACR,WAAW,GACX,SAAS,GACT,aAAa,GACb,c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