@folijs/core 0.6.0 → 1.0.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 (190) hide show
  1. package/CHANGELOG.md +159 -0
  2. package/README.md +1 -1
  3. package/dist/document.d.ts +0 -7
  4. package/dist/document.d.ts.map +1 -1
  5. package/dist/document.js +5 -1
  6. package/dist/document.js.map +1 -1
  7. package/dist/index.d.ts +4 -4
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +8 -4
  10. package/dist/index.js.map +1 -1
  11. package/dist/internal.d.ts +5 -0
  12. package/dist/internal.d.ts.map +1 -0
  13. package/dist/internal.js +7 -0
  14. package/dist/internal.js.map +1 -0
  15. package/dist/model/attrs.d.ts +0 -10
  16. package/dist/model/attrs.d.ts.map +1 -1
  17. package/dist/model/attrs.js +10 -2
  18. package/dist/model/attrs.js.map +1 -1
  19. package/dist/model/content.d.ts +1 -1
  20. package/dist/model/content.d.ts.map +1 -1
  21. package/dist/model/content.js +1 -1
  22. package/dist/model/content.js.map +1 -1
  23. package/dist/model/errors.d.ts +0 -2
  24. package/dist/model/errors.d.ts.map +1 -1
  25. package/dist/model/errors.js +4 -1
  26. package/dist/model/errors.js.map +1 -1
  27. package/dist/model/fragment.d.ts +1 -1
  28. package/dist/model/fragment.d.ts.map +1 -1
  29. package/dist/model/fragment.js +3 -1
  30. package/dist/model/fragment.js.map +1 -1
  31. package/dist/model/mark.d.ts +1 -1
  32. package/dist/model/mark.d.ts.map +1 -1
  33. package/dist/model/mark.js +1 -1
  34. package/dist/model/mark.js.map +1 -1
  35. package/dist/model/node.d.ts +4 -4
  36. package/dist/model/node.d.ts.map +1 -1
  37. package/dist/model/node.js +8 -4
  38. package/dist/model/node.js.map +1 -1
  39. package/dist/model/schema.d.ts +8 -8
  40. package/dist/model/schema.d.ts.map +1 -1
  41. package/dist/model/schema.js +4 -3
  42. package/dist/model/schema.js.map +1 -1
  43. package/dist/schema.d.ts +0 -11
  44. package/dist/schema.d.ts.map +1 -1
  45. package/dist/schema.js +2 -0
  46. package/dist/schema.js.map +1 -1
  47. package/dist/state/attempt.d.ts +1 -3
  48. package/dist/state/attempt.d.ts.map +1 -1
  49. package/dist/state/comments.d.ts +1 -7
  50. package/dist/state/comments.d.ts.map +1 -1
  51. package/dist/state/comments.js +1 -0
  52. package/dist/state/comments.js.map +1 -1
  53. package/dist/state/controls.d.ts +1 -19
  54. package/dist/state/controls.d.ts.map +1 -1
  55. package/dist/state/controls.js +21 -7
  56. package/dist/state/controls.js.map +1 -1
  57. package/dist/state/editor.d.ts +107 -72
  58. package/dist/state/editor.d.ts.map +1 -1
  59. package/dist/state/editor.js +121 -61
  60. package/dist/state/editor.js.map +1 -1
  61. package/dist/state/fields.d.ts +1 -12
  62. package/dist/state/fields.d.ts.map +1 -1
  63. package/dist/state/fields.js +16 -4
  64. package/dist/state/fields.js.map +1 -1
  65. package/dist/state/fieldupdate.d.ts +1 -47
  66. package/dist/state/fieldupdate.d.ts.map +1 -1
  67. package/dist/state/fieldupdate.js +1 -0
  68. package/dist/state/fieldupdate.js.map +1 -1
  69. package/dist/state/find.d.ts +1 -3
  70. package/dist/state/find.d.ts.map +1 -1
  71. package/dist/state/find.js +10 -2
  72. package/dist/state/find.js.map +1 -1
  73. package/dist/state/history.d.ts +1 -10
  74. package/dist/state/history.d.ts.map +1 -1
  75. package/dist/state/history.js +1 -0
  76. package/dist/state/history.js.map +1 -1
  77. package/dist/state/index.d.ts +4 -4
  78. package/dist/state/index.d.ts.map +1 -1
  79. package/dist/state/index.js +4 -4
  80. package/dist/state/index.js.map +1 -1
  81. package/dist/state/lists.d.ts +3 -3
  82. package/dist/state/lists.d.ts.map +1 -1
  83. package/dist/state/lists.js.map +1 -1
  84. package/dist/state/mark.d.ts +1 -17
  85. package/dist/state/mark.d.ts.map +1 -1
  86. package/dist/state/mark.js +2 -0
  87. package/dist/state/mark.js.map +1 -1
  88. package/dist/state/notes.d.ts +2 -12
  89. package/dist/state/notes.d.ts.map +1 -1
  90. package/dist/state/notes.js +10 -3
  91. package/dist/state/notes.js.map +1 -1
  92. package/dist/state/review.d.ts +1 -6
  93. package/dist/state/review.d.ts.map +1 -1
  94. package/dist/state/review.js +1 -0
  95. package/dist/state/review.js.map +1 -1
  96. package/dist/state/sections.d.ts +1 -11
  97. package/dist/state/sections.d.ts.map +1 -1
  98. package/dist/state/sections.js +10 -3
  99. package/dist/state/sections.js.map +1 -1
  100. package/dist/state/state.d.ts +13 -9
  101. package/dist/state/state.d.ts.map +1 -1
  102. package/dist/state/state.js +5 -2
  103. package/dist/state/state.js.map +1 -1
  104. package/dist/state/stories.d.ts +1 -40
  105. package/dist/state/stories.d.ts.map +1 -1
  106. package/dist/state/stories.js +18 -2
  107. package/dist/state/stories.js.map +1 -1
  108. package/dist/state/tables.d.ts +1 -36
  109. package/dist/state/tables.d.ts.map +1 -1
  110. package/dist/state/tables.js +23 -5
  111. package/dist/state/tables.js.map +1 -1
  112. package/dist/state/track.d.ts +1 -29
  113. package/dist/state/track.d.ts.map +1 -1
  114. package/dist/state/track.js +9 -2
  115. package/dist/state/track.js.map +1 -1
  116. package/dist/state/word.d.ts +8 -78
  117. package/dist/state/word.d.ts.map +1 -1
  118. package/dist/state/word.js +95 -28
  119. package/dist/state/word.js.map +1 -1
  120. package/dist/state/wordclip.d.ts +3 -1
  121. package/dist/state/wordclip.d.ts.map +1 -1
  122. package/dist/state/wordclip.js +67 -8
  123. package/dist/state/wordclip.js.map +1 -1
  124. package/dist/state/wordformat.d.ts +1 -86
  125. package/dist/state/wordformat.d.ts.map +1 -1
  126. package/dist/state/wordformat.js +54 -11
  127. package/dist/state/wordformat.js.map +1 -1
  128. package/dist/transform/stepmap.d.ts +0 -2
  129. package/dist/transform/stepmap.d.ts.map +1 -1
  130. package/dist/v3/check.d.ts +0 -25
  131. package/dist/v3/check.d.ts.map +1 -1
  132. package/dist/v3/check.js +35 -7
  133. package/dist/v3/check.js.map +1 -1
  134. package/dist/v3/document.d.ts +8 -6
  135. package/dist/v3/document.d.ts.map +1 -1
  136. package/dist/v3/document.js +28 -16
  137. package/dist/v3/document.js.map +1 -1
  138. package/dist/v3/extensions.d.ts +43 -0
  139. package/dist/v3/extensions.d.ts.map +1 -0
  140. package/dist/v3/extensions.js +154 -0
  141. package/dist/v3/extensions.js.map +1 -0
  142. package/dist/v3/index.d.ts +2 -1
  143. package/dist/v3/index.d.ts.map +1 -1
  144. package/dist/v3/index.js +2 -1
  145. package/dist/v3/index.js.map +1 -1
  146. package/dist/v3/resolve.d.ts.map +1 -1
  147. package/dist/v3/resolve.js +6 -0
  148. package/dist/v3/resolve.js.map +1 -1
  149. package/dist/v3/schema.d.ts +5 -18
  150. package/dist/v3/schema.d.ts.map +1 -1
  151. package/dist/v3/schema.js +79 -5
  152. package/dist/v3/schema.js.map +1 -1
  153. package/package.json +5 -1
  154. package/src/document.ts +5 -1
  155. package/src/index.ts +330 -4
  156. package/src/internal.ts +50 -0
  157. package/src/model/attrs.ts +10 -2
  158. package/src/model/content.ts +1 -1
  159. package/src/model/errors.ts +4 -1
  160. package/src/model/fragment.ts +2 -1
  161. package/src/model/mark.ts +1 -1
  162. package/src/model/node.ts +6 -4
  163. package/src/model/schema.ts +9 -3
  164. package/src/schema.ts +2 -0
  165. package/src/state/comments.ts +1 -0
  166. package/src/state/controls.ts +25 -9
  167. package/src/state/editor.ts +251 -130
  168. package/src/state/fields.ts +16 -4
  169. package/src/state/fieldupdate.ts +1 -0
  170. package/src/state/find.ts +12 -4
  171. package/src/state/history.ts +2 -0
  172. package/src/state/index.ts +5 -4
  173. package/src/state/lists.ts +2 -2
  174. package/src/state/mark.ts +2 -0
  175. package/src/state/notes.ts +27 -17
  176. package/src/state/review.ts +1 -0
  177. package/src/state/sections.ts +10 -3
  178. package/src/state/state.ts +16 -3
  179. package/src/state/stories.ts +32 -5
  180. package/src/state/tables.ts +27 -6
  181. package/src/state/track.ts +9 -2
  182. package/src/state/word.ts +105 -32
  183. package/src/state/wordclip.ts +76 -8
  184. package/src/state/wordformat.ts +55 -11
  185. package/src/v3/check.ts +35 -7
  186. package/src/v3/document.ts +39 -16
  187. package/src/v3/extensions.ts +201 -0
  188. package/src/v3/index.ts +9 -0
  189. package/src/v3/resolve.ts +5 -0
  190. package/src/v3/schema.ts +83 -5
@@ -11,9 +11,13 @@ export interface AttributeSpec {
11
11
 
12
12
  export type AttributeSpecs = Readonly<Record<string, AttributeSpec>>;
13
13
 
14
+ /** @internal */
14
15
  export const NO_ATTRS: Attrs = Object.freeze({});
15
16
 
16
- /** Fills defaults and validates. Unknown or missing required attributes are errors. */
17
+ /**
18
+ * Fills defaults and validates. Unknown or missing required attributes are errors.
19
+ * @internal
20
+ */
17
21
  export function computeAttrs(
18
22
  specs: AttributeSpecs | undefined,
19
23
  given: Attrs | undefined,
@@ -40,6 +44,7 @@ export function computeAttrs(
40
44
  /**
41
45
  * The persisted form of `attrs`: only attributes that differ from their default (required ones
42
46
  * always). Undefined when none remain, so a paragraph with default alignment has no `attrs`.
47
+ * @internal
43
48
  */
44
49
  export function attrsToJSON(
45
50
  specs: AttributeSpecs | undefined,
@@ -56,7 +61,10 @@ export function attrsToJSON(
56
61
  return json;
57
62
  }
58
63
 
59
- /** Structural equality for JSON-like attribute values. */
64
+ /**
65
+ * Structural equality for JSON-like attribute values.
66
+ * @internal
67
+ */
60
68
  export function deepEqual(a: unknown, b: unknown): boolean {
61
69
  if (a === b) return true;
62
70
  if (typeof a !== "object" || typeof b !== "object" || a === null || b === null) return false;
@@ -49,7 +49,7 @@ export class ContentMatch {
49
49
  /** True when the content may end here. */
50
50
  readonly validEnd: boolean;
51
51
 
52
- /** @internal Use NodeType.contentMatch. */
52
+ /** Use NodeType.contentMatch. */
53
53
  constructor(
54
54
  private readonly program: Program,
55
55
  private readonly states: readonly number[],
@@ -8,7 +8,10 @@ export class ReplaceError extends Error {
8
8
  override name = "ReplaceError";
9
9
  }
10
10
 
11
- /** Throws a FoliDocumentError whose message starts with a JSON path. */
11
+ /**
12
+ * Throws a FoliDocumentError whose message starts with a JSON path.
13
+ * @internal
14
+ */
12
15
  export function fail(path: string, message: string): never {
13
16
  throw new FoliDocumentError(`${path}: ${message}`);
14
17
  }
@@ -76,7 +76,8 @@ export class Fragment {
76
76
  nodesBetween(
77
77
  from: number,
78
78
  to: number,
79
- f: (node: Node, pos: number, parent: Node | null, index: number) => boolean | undefined,
79
+ // biome-ignore lint/suspicious/noConfusingVoidType: a callback without a return (TypeScript 5 refuses one for `undefined`)
80
+ f: (node: Node, pos: number, parent: Node | null, index: number) => boolean | void,
80
81
  nodeStart = 0,
81
82
  parent: Node | null = null,
82
83
  ): void {
package/src/model/mark.ts CHANGED
@@ -13,7 +13,7 @@ export interface MarkJSON {
13
13
  export class Mark {
14
14
  static readonly none: readonly Mark[] = Object.freeze([]);
15
15
 
16
- /** @internal Use MarkType.create or Schema.mark. */
16
+ /** Use MarkType.create or Schema.mark. */
17
17
  constructor(
18
18
  readonly type: MarkType,
19
19
  readonly attrs: Attrs,
package/src/model/node.ts CHANGED
@@ -28,7 +28,7 @@ export class Node {
28
28
  /** Set only on text nodes. */
29
29
  declare readonly text?: string;
30
30
 
31
- /** @internal Use NodeType.create, Schema.node/text or Schema.nodeFromJSON. */
31
+ /** Use NodeType.create, Schema.node/text or Schema.nodeFromJSON. */
32
32
  constructor(
33
33
  readonly type: NodeType,
34
34
  readonly attrs: Attrs,
@@ -103,14 +103,16 @@ export class Node {
103
103
  nodesBetween(
104
104
  from: number,
105
105
  to: number,
106
- f: (node: Node, pos: number, parent: Node | null, index: number) => boolean | undefined,
106
+ // biome-ignore lint/suspicious/noConfusingVoidType: a callback without a return (TypeScript 5 refuses one for `undefined`)
107
+ f: (node: Node, pos: number, parent: Node | null, index: number) => boolean | void,
107
108
  ): void {
108
109
  this.content.nodesBetween(from, to, f, 0, this);
109
110
  }
110
111
 
111
112
  /** Calls `f` for every descendant. Return false to skip a node's children. */
112
113
  descendants(
113
- f: (node: Node, pos: number, parent: Node | null, index: number) => boolean | undefined,
114
+ // biome-ignore lint/suspicious/noConfusingVoidType: a callback without a return (TypeScript 5 refuses one for `undefined`)
115
+ f: (node: Node, pos: number, parent: Node | null, index: number) => boolean | void,
114
116
  ): void {
115
117
  this.nodesBetween(0, this.content.size, f);
116
118
  }
@@ -254,7 +256,7 @@ export class Node {
254
256
  }
255
257
 
256
258
  export class TextNode extends Node {
257
- /** @internal Use Schema.text. */
259
+ /** Use Schema.text. */
258
260
  constructor(
259
261
  type: NodeType,
260
262
  override readonly text: string,
@@ -26,6 +26,11 @@ export interface NodeSpec {
26
26
  atom?: boolean;
27
27
  /** Whether a NodeSelection may select this node (default true; text is never selectable). */
28
28
  selectable?: boolean;
29
+ /**
30
+ * The text a leaf stands for in plain text, find, the clipboard and `.docx` (custom nodes,
31
+ * ADR-0008). Not saved: the attributes are.
32
+ */
33
+ text?: (attrs: Attrs) => string;
29
34
  }
30
35
 
31
36
  export interface MarkSpec {
@@ -57,6 +62,7 @@ const NODE_KEYS = ["type", "attrs", "content", "text", "marks"];
57
62
  /**
58
63
  * How deep parsed JSON may nest: far beyond real documents (a table in a cell is three levels;
59
64
  * Word nests tables a few dozen deep at most), well within the call stack.
65
+ * @internal
60
66
  */
61
67
  export const MAX_NODE_DEPTH = 300;
62
68
  const MARK_KEYS = ["type", "attrs"];
@@ -73,7 +79,7 @@ export class NodeType {
73
79
  /** Allowed child marks; null means all. */
74
80
  markSet!: readonly MarkType[] | null;
75
81
 
76
- /** @internal Created by Schema. */
82
+ /** Created by Schema. */
77
83
  constructor(
78
84
  readonly name: string,
79
85
  readonly schema: Schema,
@@ -123,7 +129,7 @@ export class NodeType {
123
129
 
124
130
  /**
125
131
  * Creates a node without checking its content. Attributes are still validated.
126
- * @internal For transforms whose node ends up complete: a wrapper the wrapped blocks fill
132
+ * For transforms whose node ends up complete: a wrapper the wrapped blocks fill
127
133
  * (validated when they are inserted) or the open half of a split (validated when the replace
128
134
  * joins it). Steps do not validate closed nodes inside a slice: a node left incomplete here
129
135
  * would make an invalid document.
@@ -185,7 +191,7 @@ export class MarkType {
185
191
  readonly groups: readonly string[];
186
192
  private readonly instance: Mark | null;
187
193
 
188
- /** @internal Created by Schema. */
194
+ /** Created by Schema. */
189
195
  constructor(
190
196
  readonly name: string,
191
197
  readonly schema: Schema,
package/src/schema.ts CHANGED
@@ -45,6 +45,7 @@ export function isSafeHref(href: unknown): href is string {
45
45
  * Whether a link's attributes may come in from outside the document (a command, a paste from
46
46
  * elsewhere): a safe `href`, or none with a bookmark (`anchor`, schema v3: Word's table of
47
47
  * contents and cross-references).
48
+ * @internal
48
49
  */
49
50
  export function isSafeLink(
50
51
  attrs: { href?: unknown; anchor?: unknown } | null | undefined,
@@ -71,6 +72,7 @@ const color = {
71
72
  isColor(value) ? undefined : "color must be a lowercase #rrggbb value",
72
73
  };
73
74
 
75
+ /** @internal */
74
76
  export const schemaV2 = new Schema({
75
77
  nodes: {
76
78
  doc: { content: "block+" },
@@ -327,6 +327,7 @@ function removesReference(tr: Transaction): boolean {
327
327
  * Keeps comments with their text: a comment whose reference mark an edit removed (its range
328
328
  * deleted) is removed with its replies and the rest of their anchors, in the same undo step.
329
329
  * Only references the edit removed count: a comment without one from the start stays.
330
+ * @internal
330
331
  */
331
332
  export function commentsPlugin(): Plugin {
332
333
  return new Plugin({
@@ -21,6 +21,7 @@ import { Mapping } from "../transform/stepmap.js";
21
21
  import { TransformError } from "../transform/transform.js";
22
22
  import type { ControlJSON } from "../v3/document.js";
23
23
  import { parseContent } from "../v3/document.js";
24
+ import { writeCustom } from "../v3/extensions.js";
24
25
  import { attempt } from "./attempt.js";
25
26
  import { insertHardBreak, insertText } from "./commands.js";
26
27
  import { formatDatePicture } from "./fieldupdate.js";
@@ -55,6 +56,7 @@ export interface ControlRange {
55
56
  /**
56
57
  * On a transaction: `value` fills a control, `accept` and `reject` resolve tracked changes, `fields`
57
58
  * updates fields: what Word lets through where typing is refused.
59
+ * @internal
58
60
  */
59
61
  export const controlEdit = "controlEdit";
60
62
 
@@ -222,6 +224,7 @@ export function controlSpot(tree: Node, a: number, b: number): boolean {
222
224
  /**
223
225
  * Whether a change at `from..to` is in content that may not be edited: it touches such a control and
224
226
  * is not all inside an editable one within it. One whose own deletion is tracked does not count.
227
+ * @internal
225
228
  */
226
229
  export function readOnlyAt(
227
230
  tree: Node,
@@ -889,7 +892,10 @@ const spaceKey: Command = (state, dispatch) => {
889
892
  return true;
890
893
  };
891
894
 
892
- /** The rules of content controls (ADR-0007), in every editor of a document. */
895
+ /**
896
+ * The rules of content controls (ADR-0007), in every editor of a document.
897
+ * @internal
898
+ */
893
899
  export function controlsPlugin(): Plugin {
894
900
  return new Plugin({
895
901
  filterTransaction: allowed,
@@ -992,7 +998,9 @@ export function controlValues(state: EditorState): ControlValue[] {
992
998
  content: (range.level === "cell"
993
999
  ? state.doc.slice(range.from + 1, range.to - 1).content
994
1000
  : contentOf(state.doc, range)
995
- ).toJSON(),
1001
+ )
1002
+ .toJSON()
1003
+ .map(writeCustom),
996
1004
  }
997
1005
  : {}),
998
1006
  });
@@ -1094,7 +1102,7 @@ function valueFor(
1094
1102
  }
1095
1103
  const nodes = value.content.every((json) => state.schema.nodes[json.type]?.isInline)
1096
1104
  ? value.content.map((json) => state.schema.nodeFromJSON(json))
1097
- : blocksFrom(value.content);
1105
+ : blocksFrom(state, value.content);
1098
1106
  if (nodes.every((n) => n.isInline)) return { nodes, control: shown };
1099
1107
  if (range.level === "inline") {
1100
1108
  const only = nodes.length === 1 ? nodes[0] : undefined;
@@ -1166,11 +1174,13 @@ function takesText(tree: Node, range: ControlRange): boolean {
1166
1174
  const isBlocks = (value: ControlValueInput) =>
1167
1175
  typeof value === "object" && value !== null && "content" in value;
1168
1176
 
1169
- function blocksFrom(content: readonly NodeJSON[]): Node[] {
1177
+ function blocksFrom(state: EditorState, content: readonly NodeJSON[]): Node[] {
1170
1178
  const blocks: Node[] = [];
1171
- parseContent({ type: "doc", content }, "content", true).forEach((node) => {
1172
- blocks.push(node);
1173
- });
1179
+ parseContent({ type: "doc", content }, { fragment: true, schema: state.schema }).forEach(
1180
+ (node) => {
1181
+ blocks.push(node);
1182
+ },
1183
+ );
1174
1184
  return blocks;
1175
1185
  }
1176
1186
 
@@ -1312,14 +1322,20 @@ export interface ControlInsertOptions {
1312
1322
  multiLine?: boolean;
1313
1323
  }
1314
1324
 
1315
- /** A control's properties for a copy of it: Word gives the copy a `w:id` of its own. */
1325
+ /**
1326
+ * A control's properties for a copy of it: Word gives the copy a `w:id` of its own.
1327
+ * @internal
1328
+ */
1316
1329
  export function copiedControl(control: ControlJSON): ControlJSON {
1317
1330
  return control.ext
1318
1331
  ? { ...control, ext: control.ext.replace(/<[\w.-]+:id\b[^>]*\/>/, "") }
1319
1332
  : control;
1320
1333
  }
1321
1334
 
1322
- /** An id no control of `controls` has. */
1335
+ /**
1336
+ * An id no control of `controls` has.
1337
+ * @internal
1338
+ */
1323
1339
  export function freshControlId(controls: Controls | undefined): string {
1324
1340
  let n = Object.keys(controls ?? {}).length + 1;
1325
1341
  while (Object.hasOwn(controls ?? {}, `control-${n}`)) n++;