platejs 54.0.0-beta.3 → 54.0.0-beta.4

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 (150) hide show
  1. package/dist/{AIChatRequestContext-DY104TqL.d.ts → AIChatRequestContext-DrUahdTc.d.ts} +2 -2
  2. package/dist/{BaseSuggestionPlugin-Deo6n6Fs.d.ts → BaseSuggestionPlugin-_7Ka6nY3.d.ts} +4 -4
  3. package/dist/{PlateStatic-CJX5bKuv.d.ts → PlateStatic-D3Cb-QwH.d.ts} +3 -3
  4. package/dist/ai/index.d.ts +1 -1
  5. package/dist/ai/react/index.d.ts +9 -9
  6. package/dist/authored/index.d.ts +3 -3
  7. package/dist/code-block/codemirror/index.d.ts +1 -1
  8. package/dist/{combobox-DAOYWuvC.d.ts → combobox-Dr0OYZIE.d.ts} +2 -2
  9. package/dist/{comparison-types-Dt-unJZY-DCddmbSD.d.ts → comparison-types-DdwE6mYq-BogsHoUO.d.ts} +1 -1
  10. package/dist/compiler/index.d.ts +2 -2
  11. package/dist/{core-ClHasORr.d.ts → core-BqR-fEDp.d.ts} +65 -26
  12. package/dist/diff/index.d.ts +2 -2
  13. package/dist/docs/api/core/plate-controller.md +1 -1
  14. package/dist/docs/api/core.md +45 -5
  15. package/dist/docs/api/dom.md +4 -7
  16. package/dist/docs/api/react-hooks.md +40 -22
  17. package/dist/docs/authored-changes.md +4 -2
  18. package/dist/docs/block-placeholder.md +4 -2
  19. package/dist/docs/board.md +57 -4
  20. package/dist/docs/comment.md +2 -2
  21. package/dist/docs/document-model.md +15 -3
  22. package/dist/docs/editor.md +50 -4
  23. package/dist/docs/llms.txt +1 -1
  24. package/dist/docs/markdown.md +97 -9
  25. package/dist/docs/plugin-input-rules.md +62 -3
  26. package/dist/docx/export/index.d.ts +3 -3
  27. package/dist/docx/export/lib/exportDocx.js +18 -13
  28. package/dist/docx/export/lib/internal/xml-builder.js +11 -16
  29. package/dist/docx/export/lib/reviewProjection.js +43 -3
  30. package/dist/docx/import/index.d.ts +4 -4
  31. package/dist/docx/paste/index.d.ts +4 -4
  32. package/dist/dom/index.d.ts +1 -1
  33. package/dist/excalidraw/index.d.ts +3 -3
  34. package/dist/excalidraw/react/index.d.ts +3 -3
  35. package/dist/facade.js +2 -2
  36. package/dist/features/board/index.d.ts +1 -1
  37. package/dist/features/callout/index.d.ts +3 -3
  38. package/dist/features/combobox/index.d.ts +2 -2
  39. package/dist/features/comments/BaseCommentsPlugin.js +33 -17
  40. package/dist/features/comments/index.d.ts +1 -1
  41. package/dist/features/date/index.d.ts +3 -3
  42. package/dist/features/details/index.d.ts +3 -3
  43. package/dist/features/details/lib/BaseDetailsPlugin.js +15 -2
  44. package/dist/features/field/index.d.ts +2 -2
  45. package/dist/features/field/index.js +4 -2
  46. package/dist/features/field/lib/BaseFieldPlugin.js +51 -105
  47. package/dist/features/field/lib/applyFieldValues.js +188 -0
  48. package/dist/features/field/lib/fieldRules.internal.js +169 -7
  49. package/dist/features/find/index.d.ts +3 -3
  50. package/dist/features/footnote/index.d.ts +1 -1
  51. package/dist/features/ink/index.d.ts +1 -1
  52. package/dist/features/ink/lib/inkGone.internal.js +6 -6
  53. package/dist/features/ink/lib/inkPlaces.internal.js +8 -9
  54. package/dist/features/layout/index.d.ts +1 -1
  55. package/dist/features/lint/index.d.ts +3 -3
  56. package/dist/features/media/index.d.ts +2 -2
  57. package/dist/features/media/index.js +2 -2
  58. package/dist/features/media/lib/media-embed/BaseMediaEmbedPlugin.js +200 -2
  59. package/dist/features/mention/index.d.ts +5 -5
  60. package/dist/features/suggestion/index.d.ts +1 -1
  61. package/dist/features/table/index.d.ts +1 -1
  62. package/dist/features/table/lib/BaseTablePlugin.js +50 -7
  63. package/dist/features/toc/index.d.ts +1 -1
  64. package/dist/features/upload/index.d.ts +1 -1
  65. package/dist/history/index.d.ts +2 -2
  66. package/dist/html/index.d.ts +3 -3
  67. package/dist/html/server/index.d.ts +1 -1
  68. package/dist/{htmlTypes-cg_cWsaN.d.ts → htmlTypes-eT2ZGDAj.d.ts} +1 -1
  69. package/dist/hyperscript/index.d.ts +1 -1
  70. package/dist/{index-D3-2iM1b.d.ts → index-B33E1CRJ.d.ts} +291 -4
  71. package/dist/{index-DKOFvteF.d.ts → index-B7moSod7.d.ts} +1 -1
  72. package/dist/{index-C9DiGDVj.d.ts → index-B8-5Sr7I.d.ts} +2 -2
  73. package/dist/{index-CzLwBZpY.d.ts → index-B8QJDAmG.d.ts} +2 -2
  74. package/dist/{index-Cmuluh6f.d.ts → index-BTx5oK7k.d.ts} +1 -1
  75. package/dist/{index-DPWyk3lG-DOYwohXO.d.ts → index-BWPcE4ac-Cto_IsNs.d.ts} +7 -1
  76. package/dist/{index-DpltOCot-A2b3p3Eb.d.ts → index-BXjLmx1n-CXk17buG.d.ts} +75 -27
  77. package/dist/{index-C6ql9pKw.d.ts → index-BXwU9QH6.d.ts} +2 -2
  78. package/dist/{index-CLsEe3LY.d.ts → index-BkwR-5hH.d.ts} +3 -3
  79. package/dist/{index-BZWFoUXt.d.ts → index-BsV2McRr.d.ts} +5 -5
  80. package/dist/{index-DQnjqtQa.d.ts → index-C9Fu-P4E.d.ts} +116 -21
  81. package/dist/{index-oIJAZxc2.d.ts → index-Ca48r1ng.d.ts} +3 -3
  82. package/dist/{index-Do9pj1GK.d.ts → index-Cob_iBdu.d.ts} +47 -6
  83. package/dist/{index-C1_JIhL4.d.ts → index-CpY7Xnxw.d.ts} +4 -4
  84. package/dist/{index-baQOK89E.d.ts → index-DNQXzU-X.d.ts} +1 -1
  85. package/dist/{index-8WO2vI0U.d.ts → index-DPowErHa.d.ts} +18 -20
  86. package/dist/{index-B5mYEawV.d.ts → index-DShbzec2.d.ts} +5 -6
  87. package/dist/{index-C35V_hj0.d.ts → index-DYdObeFQ.d.ts} +3 -3
  88. package/dist/{index-HPYawTae-BCUQLEWF.d.ts → index-Dq3Jlj8O-BJ6NxZm0.d.ts} +1 -1
  89. package/dist/{index-CdOLsPaW.d.ts → index-DwWQVwQt.d.ts} +3 -5
  90. package/dist/{index-r7yb77Wy.d.ts → index-JWgwL5l3.d.ts} +5 -3
  91. package/dist/{index-CktYvOgk.d.ts → index-M781pyx9.d.ts} +4 -4
  92. package/dist/{index-C0POz7rg.d.ts → index-d7io3MzC.d.ts} +3 -3
  93. package/dist/index.d.ts +4 -4
  94. package/dist/lib/plugins/element-id/ElementIdPlugin.js +6 -16
  95. package/dist/lib/plugins/input-rules/InputRulesPlugin.js +73 -14
  96. package/dist/markdown/index.d.ts +2 -2
  97. package/dist/markdown/index.js +2 -1
  98. package/dist/markdown/lib/MarkdownPlugin.js +8 -12
  99. package/dist/markdown/lib/RawHtmlPlugin.js +4 -2
  100. package/dist/markdown/lib/internal/markdownConversion.js +7 -4
  101. package/dist/markdown/lib/internal/markdownFootnoteWriter.js +35 -0
  102. package/dist/markdown/lib/internal/markdownRawHtml.js +40 -1
  103. package/dist/markdown/lib/internal/markdownTags.js +7 -1
  104. package/dist/math/index.d.ts +3 -3
  105. package/dist/math/react/index.d.ts +4 -4
  106. package/dist/migrations/index.d.ts +3 -3
  107. package/dist/migrations/migratePlateV54.js +4 -6
  108. package/dist/migrations/migratePlateV54RetiredMarks.internal.js +23 -8
  109. package/dist/migrations/migratePlateV54Suggestions.internal.js +2 -2
  110. package/dist/migrations/v53-manifest.internal.js +4 -0
  111. package/dist/pagination/index.d.ts +1 -1
  112. package/dist/pagination/react/index.d.ts +2 -2
  113. package/dist/react/components/EditorContentView.internal.js +26 -16
  114. package/dist/react/components/Plate.js +13 -20
  115. package/dist/react/features/board/boardExport.internal.js +470 -0
  116. package/dist/react/features/board/boardPictureStyles.internal.js +415 -0
  117. package/dist/react/features/board/boardView.internal.js +11 -6
  118. package/dist/react/features/board/index.d.ts +50 -6
  119. package/dist/react/features/callout/index.d.ts +3 -3
  120. package/dist/react/features/combobox/index.d.ts +5 -5
  121. package/dist/react/features/comments/index.d.ts +6 -6
  122. package/dist/react/features/date/index.d.ts +4 -4
  123. package/dist/react/features/details/index.d.ts +4 -4
  124. package/dist/react/features/field/FieldPlugin.js +5 -4
  125. package/dist/react/features/field/index.d.ts +19 -16
  126. package/dist/react/features/footnote/index.d.ts +7 -7
  127. package/dist/react/features/format-painter/index.d.ts +4 -4
  128. package/dist/react/features/ink/index.d.ts +5 -9
  129. package/dist/react/features/ink/inkPlacement.internal.js +2 -1
  130. package/dist/react/features/layout/index.d.ts +5 -5
  131. package/dist/react/features/media/index.d.ts +5 -5
  132. package/dist/react/features/mention/index.d.ts +6 -6
  133. package/dist/react/features/suggestion/index.d.ts +6 -6
  134. package/dist/react/features/table/index.d.ts +7 -6
  135. package/dist/react/features/table/useTableResize.js +10 -1
  136. package/dist/react/features/toc/index.d.ts +2 -2
  137. package/dist/react/features/upload/index.d.ts +5 -5
  138. package/dist/react/index.d.ts +6 -6
  139. package/dist/react/internal/plate-context.js +20 -11
  140. package/dist/react/stores/plate/useEditorSelector.js +22 -9
  141. package/dist/react/utils/BlockPlaceholderPlugin.js +5 -2
  142. package/dist/react/virtualized.d.ts +2 -2
  143. package/dist/{source-aUwpNnAw.d.ts → source-BGohy8vH.d.ts} +3 -3
  144. package/dist/static/index.d.ts +5 -5
  145. package/dist/tabbable/index.d.ts +1 -1
  146. package/dist/tabbable/react/index.d.ts +3 -3
  147. package/dist/testing/index.d.ts +2 -2
  148. package/dist/yjs/index.d.ts +1 -1
  149. package/dist/yjs/react/index.d.ts +5 -5
  150. package/package.json +2 -2
@@ -22,6 +22,18 @@ const getDetailsOfTitle = (state, { path, type }) => {
22
22
  const title = container?.[0].children[0];
23
23
  return title && state.nodes.isContent(title) ? container : void 0;
24
24
  };
25
+ const opensTagOrBreaks = (text) => /<[!/?A-Za-z]/.test(text) || text.includes("\n");
26
+ const breaksHeadedLine = (nodes) => nodes.some((node) => {
27
+ if (node.type === "break") return true;
28
+ if (node.type === "html") return node.value.includes("\n");
29
+ if ("value" in node) return opensTagOrBreaks(node.value);
30
+ if ((node.type === "link" || node.type === "image") && (!node.url || /[\0- \u007F]/.test(node.url) || [
31
+ node.url,
32
+ node.title ?? "",
33
+ node.type === "image" ? node.alt ?? "" : ""
34
+ ].some(opensTagOrBreaks) || node.type === "link" && node.children.length === 1 && node.children[0]?.type === "text" && [node.url, node.url.replace(/^mailto:/i, "")].includes(node.children[0].value))) return true;
35
+ return "children" in node && breaksHeadedLine(node.children);
36
+ });
25
37
  const isSummaryHeadingLevel = (value) => typeof value === "number" && Number.isInteger(value) && value >= 1 && value <= 6;
26
38
  const getReleasedTitleProps = (editor, level) => {
27
39
  const heading = editor.plugin(BaseHeadingPlugin);
@@ -88,7 +100,8 @@ const BaseDetailsSummaryPlugin = definePlugin(PLUGINS.detailsSummary, {
88
100
  },
89
101
  encode: ({ encodeNodeAttributes, encodePhrasing, node, preserve, readPlainInline }) => {
90
102
  const content = readPlainInline(node.children) === "" ? [] : encodePhrasing(node.children);
91
- const phrasing = node.level ? [
103
+ const headed = Boolean(node.level) && !breaksHeadedLine(content);
104
+ const phrasing = headed ? [
92
105
  {
93
106
  type: "html",
94
107
  value: `<h${node.level}>`
@@ -101,7 +114,7 @@ const BaseDetailsSummaryPlugin = definePlugin(PLUGINS.detailsSummary, {
101
114
  ] : content;
102
115
  if (node.level) preserve("level");
103
116
  return {
104
- attributes: encodeNodeAttributes().filter((attribute) => !("name" in attribute && attribute.name === "level")),
117
+ attributes: encodeNodeAttributes().filter((attribute) => !headed || !("name" in attribute && attribute.name === "level")),
105
118
  children: phrasing.length > 0 ? [{
106
119
  children: phrasing,
107
120
  type: "paragraph"
@@ -1,2 +1,2 @@
1
- import { a as FieldKind, c as FieldProblem, d as FieldTest, f as FieldValue, i as FieldElement, l as FieldRead, n as FIELD_TESTS, o as FieldOption, p as fitsField, r as FieldCondition, s as FieldPatch, t as BaseFieldPlugin, u as FieldSettings } from "../../index-DQnjqtQa.js";
2
- export { BaseFieldPlugin, FIELD_TESTS, FieldCondition, FieldElement, FieldKind, FieldOption, FieldPatch, FieldProblem, FieldRead, FieldSettings, FieldTest, FieldValue, fitsField };
1
+ import { _ as fitsField, a as FieldCondition, c as FieldOption, d as FieldRead, f as FieldSettings, g as FIELD_TESTS, h as FieldValue, i as BaseFieldPlugin, l as FieldPatch, m as FieldTest, n as FieldRefusal, o as FieldElement, p as FieldSubmission, r as applyFieldValues, s as FieldKind, t as FieldApplyResult, u as FieldProblem } from "../../index-C9Fu-P4E.js";
2
+ export { BaseFieldPlugin, FIELD_TESTS, FieldApplyResult, FieldCondition, FieldElement, FieldKind, FieldOption, FieldPatch, FieldProblem, FieldRead, FieldRefusal, FieldSettings, FieldSubmission, FieldTest, FieldValue, applyFieldValues, fitsField };
@@ -1,2 +1,4 @@
1
- import { BaseFieldPlugin, FIELD_TESTS, fitsField } from "./lib/BaseFieldPlugin.js";
2
- export { BaseFieldPlugin, FIELD_TESTS, fitsField };
1
+ import { FIELD_TESTS, fitsField } from "./lib/fieldRules.internal.js";
2
+ import { BaseFieldPlugin } from "./lib/BaseFieldPlugin.js";
3
+ import { applyFieldValues } from "./lib/applyFieldValues.js";
4
+ export { BaseFieldPlugin, FIELD_TESTS, applyFieldValues, fitsField };
@@ -2,67 +2,9 @@ import { EditorRefusalError, claimsHistoryReplay, getActiveEditorUpdateId, getAc
2
2
  import { failInvariant } from "../../../internal/failInvariant.js";
3
3
  import { definePlugin } from "../../../lib/plugin/definePlugin.js";
4
4
  import { ElementApi, documentReplacement, locateAt, property, schema, target, txLocate } from "../../../core.js";
5
- import { fieldLinks, sharedFieldLinks } from "./fieldRules.internal.js";
5
+ import { FIELD_KINDS, FIELD_TESTS, conditionProblem, fieldLinks, fieldProblems, fitsField, isFieldCondition, isFieldKind, isFieldOptions, isFieldValue, sharedFieldLinks, unfitSettings } from "./fieldRules.internal.js";
6
6
  import { nanoid } from "nanoid";
7
7
  //#region src/features/field/lib/BaseFieldPlugin.ts
8
- const FIELD_KINDS = [
9
- "checkbox",
10
- "choice",
11
- "date",
12
- "number",
13
- "signature",
14
- "text"
15
- ];
16
- /**
17
- * The tests a condition may use on a field of each kind. `insert` and `update`
18
- * refuse any other test, and a read reports it as `unsupported-test`.
19
- */
20
- const FIELD_TESTS = {
21
- checkbox: ["equals", "not-equals"],
22
- choice: [
23
- "filled",
24
- "blank",
25
- "equals",
26
- "not-equals"
27
- ],
28
- date: [
29
- "filled",
30
- "blank",
31
- "equals",
32
- "not-equals",
33
- "above",
34
- "below"
35
- ],
36
- number: [
37
- "filled",
38
- "blank",
39
- "equals",
40
- "not-equals",
41
- "above",
42
- "below"
43
- ],
44
- signature: ["filled", "blank"],
45
- text: [
46
- "filled",
47
- "blank",
48
- "equals",
49
- "not-equals"
50
- ]
51
- };
52
- const ISO_DATE_PATTERN = /^\d{4}-\d{2}-\d{2}$/;
53
- const isIsoDate = (value) => {
54
- if (!ISO_DATE_PATTERN.test(value)) return false;
55
- const date = /* @__PURE__ */ new Date(`${value}T00:00:00Z`);
56
- return !Number.isNaN(date.getTime()) && date.toISOString().startsWith(value);
57
- };
58
- const isFieldValue = (value) => typeof value === "string" || typeof value === "boolean" || typeof value === "number" && Number.isFinite(value);
59
- const isFieldOptions = (value) => Array.isArray(value) && value.every((option) => typeof option?.label === "string" && typeof option.value === "string") && new Set(value.map((option) => option.value)).size === value.length;
60
- const isFieldCondition = (value) => {
61
- if (!value || typeof value !== "object") return false;
62
- const { ref, test, value: compared } = value;
63
- return typeof ref === "string" && Object.values(FIELD_TESTS).some((tests) => tests.some((known) => known === test)) && (compared === void 0 || isFieldValue(compared));
64
- };
65
- const isFieldKind = (value) => FIELD_KINDS.some((kind) => kind === value);
66
8
  const fieldText = ({ node: { label, options, value } }) => {
67
9
  return (options?.find((option) => option.value === value)?.label ?? String(value ?? "")) || `[${label ?? ""}]`;
68
10
  };
@@ -203,39 +145,6 @@ const fieldSchemaPlugin = definePlugin(FIELD_TYPE, {
203
145
  }
204
146
  }
205
147
  });
206
- const SHAPED_SETTINGS = [
207
- "defaultValue",
208
- "options",
209
- "value"
210
- ];
211
- /**
212
- * Whether a value fits a field's kind: a boolean for a checkbox, one of its
213
- * options' values for a choice, an ISO date string for a date, a finite number
214
- * other than -0 for a number, a string with no line break for a text field,
215
- * and a string for a signature field.
216
- */
217
- const fitsField = ({ kind, options = [] }, value) => {
218
- switch (kind) {
219
- case "checkbox": return typeof value === "boolean";
220
- case "choice": return options.some((option) => option.value === value);
221
- case "date": return typeof value === "string" && isIsoDate(value);
222
- case "number": return typeof value === "number" && Number.isFinite(value) && !Object.is(value, -0);
223
- case "text": return typeof value === "string" && !/[\n\r]/.test(value);
224
- default: return typeof value === "string";
225
- }
226
- };
227
- const fitsSetting = (field, setting) => field[setting] === void 0 || (setting === "options" ? field.kind === "choice" : fitsField(field, field[setting]));
228
- const conditionProblem = ({ test, value }, named) => {
229
- if (!named) return "missing-target";
230
- if (!FIELD_TESTS[named.kind].includes(test)) return "unsupported-test";
231
- return (test === "filled" || test === "blank" ? value === void 0 : fitsField(named, value)) ? void 0 : "invalid-value";
232
- };
233
- const fieldProblems = (field, link) => {
234
- const own = SHAPED_SETTINGS.every((setting) => fitsSetting(field, setting)) ? [] : ["invalid-value"];
235
- const conditioned = field.condition && conditionProblem(field.condition, link.target);
236
- const problems = conditioned && !own.includes(conditioned) ? [...own, conditioned] : own;
237
- return link.loop ? [...problems, "condition-loop"] : problems;
238
- };
239
148
  const refusal = (reason) => /* @__PURE__ */ new TypeError(`Field write refused: ${reason}.`);
240
149
  const UNKNOWN_FILL_MODE = Symbol("unknown fill mode");
241
150
  /**
@@ -315,6 +224,22 @@ const BaseFieldPlugin = fieldSchemaPlugin.extend(({ editor, schema: { properties
315
224
  if (isField(node)) return [node];
316
225
  return ElementApi.isElement(node) ? fieldsIn(node.children) : [];
317
226
  });
227
+ const listed = () => {
228
+ const roots = getActiveEditorUpdateId(reader) ? readRootEntries(reader) : reader.read.roots();
229
+ const nodes = [readViewPage(state) ?? state.children(), ...roots.map(({ root }) => state.root(root))].flatMap(fieldsIn);
230
+ const byId = /* @__PURE__ */ new Map();
231
+ const repeated = /* @__PURE__ */ new Set();
232
+ for (const node of nodes) {
233
+ if (typeof node.fieldId !== "string") continue;
234
+ if (byId.has(node.fieldId)) repeated.add(node.fieldId);
235
+ else byId.set(node.fieldId, node);
236
+ }
237
+ const lookup = (id) => repeated.has(id) ? held(id) : byId.get(id);
238
+ return {
239
+ lookup,
240
+ nodes
241
+ };
242
+ };
318
243
  return {
319
244
  /**
320
245
  * The field whose `fieldId` is `fieldId`, in any root, or null when
@@ -323,21 +248,42 @@ const BaseFieldPlugin = fieldSchemaPlugin.extend(({ editor, schema: { properties
323
248
  */
324
249
  field: (fieldId) => {
325
250
  const node = held(fieldId);
326
- return node ? {
251
+ if (!node) return null;
252
+ const link = sharedFieldLinks(reader).link(node, held);
253
+ return {
254
+ applies: link.applies,
327
255
  fieldId,
328
256
  node,
329
- problems: fieldProblems(node, sharedFieldLinks(reader)(node, held))
330
- } : null;
257
+ problems: fieldProblems(node, link)
258
+ };
331
259
  },
332
260
  /** Every field of the document, in reading order. */
333
261
  fields: () => {
334
- const fields = new Map([readViewPage(state) ?? state.children(), ...readRootEntries(reader).map(({ root }) => state.root(root))].flatMap(fieldsIn).map((node) => [node.fieldId, node]));
335
- const link = fieldLinks();
336
- return [...fields].map(([fieldId, node]) => ({
337
- fieldId,
338
- node,
339
- problems: fieldProblems(node, link(node, (id) => fields.get(id)))
340
- }));
262
+ const { lookup, nodes } = listed();
263
+ const links = fieldLinks();
264
+ return nodes.map((node) => {
265
+ const link = lookup(node.fieldId) === node ? links.link(node, lookup) : links.linkCopy(node, lookup);
266
+ return {
267
+ applies: link.applies,
268
+ fieldId: node.fieldId,
269
+ node,
270
+ problems: fieldProblems(node, link)
271
+ };
272
+ });
273
+ },
274
+ /**
275
+ * The `value` of each field of `role` that holds one, keyed by
276
+ * `fieldId` and read from the field that id names, to pass to
277
+ * `applyFieldValues`.
278
+ */
279
+ submission: (role) => {
280
+ const { lookup, nodes } = listed();
281
+ const values = Object.create(null);
282
+ for (const node of nodes) if (node.role === role && isFieldValue(node.value) && lookup(node.fieldId) === node) values[node.fieldId] = node.value;
283
+ return {
284
+ role,
285
+ values
286
+ };
341
287
  }
342
288
  };
343
289
  },
@@ -360,7 +306,7 @@ const BaseFieldPlugin = fieldSchemaPlugin.extend(({ editor, schema: { properties
360
306
  */
361
307
  insert: txLocate(locateAt(1), (settings, options = {}) => {
362
308
  if ("fieldId" in settings) throw refusal("fieldId is not a setting");
363
- const unfit = SHAPED_SETTINGS.find((setting) => !fitsSetting(settings, setting));
309
+ const [unfit] = unfitSettings(settings);
364
310
  if (unfit) throw refusal(`${unfit} does not fit a ${settings.kind} field`);
365
311
  if (settings.condition) checkCondition(settings.condition);
366
312
  const field = tx.schema.create(type, settings);
@@ -393,12 +339,12 @@ const BaseFieldPlugin = fieldSchemaPlugin.extend(({ editor, schema: { properties
393
339
  ...field,
394
340
  ...patch
395
341
  };
396
- const unfit = SHAPED_SETTINGS.filter((setting) => !fitsSetting(next, setting));
342
+ const unfit = unfitSettings(next);
397
343
  const named = unfit.find((setting) => setting in patch);
398
344
  if (named) throw refusalForRole(`${named} does not fit a ${next.kind} field`);
399
345
  if (patch.condition) {
400
346
  checkCondition(patch.condition, next);
401
- if (patch.condition.ref !== field.condition?.ref && fieldLinks()(next, (id) => id === fieldId ? next : held(id)[0]).loop) throw refusal("its condition closes a loop");
347
+ if (patch.condition.ref !== field.condition?.ref && fieldLinks().link(next, (id) => id === fieldId ? next : held(id)[0]).loop) throw refusal("its condition closes a loop");
402
348
  }
403
349
  const reshaped = "kind" in patch || "options" in patch;
404
350
  tx.nodes.set({
@@ -0,0 +1,188 @@
1
+ import { fieldProblems, fitsField, isFieldCondition, isFieldKind, isFieldOptions, isFieldValue, isFilled, submittedFieldLinks } from "./fieldRules.internal.js";
2
+ //#region src/features/field/lib/applyFieldValues.ts
3
+ const isChildren = (template) => Array.isArray(template);
4
+ const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
5
+ const isText = (value) => isRecord(value) && typeof value.text === "string" && !("children" in value);
6
+ const placesIn = (nodes, type, root, path = []) => nodes.flatMap((node, index) => {
7
+ if (!isRecord(node) || isText(node)) return [];
8
+ const at = [...path, index];
9
+ if (node.type === type) return [{
10
+ node,
11
+ path: at,
12
+ ...root === void 0 ? {} : { root }
13
+ }];
14
+ return Array.isArray(node.children) ? placesIn(node.children, type, root, at) : [];
15
+ });
16
+ const isWellFormed = (node) => !("text" in node) && Array.isArray(node.children) && node.children.length === 1 && isText(node.children[0]) && typeof node.fieldId === "string" && isFieldKind(node.kind) && typeof node.label === "string" && typeof node.role === "string" && (node.required === void 0 || typeof node.required === "boolean") && (node.condition === void 0 || isFieldCondition(node.condition)) && (node.defaultValue === void 0 || isFieldValue(node.defaultValue)) && (node.options === void 0 || isFieldOptions(node.options)) && (node.value === void 0 || isFieldValue(node.value));
17
+ const fillNodes = (nodes, type, held) => {
18
+ let changed = false;
19
+ const next = nodes.map((node) => {
20
+ const filled = fillNode(node, type, held);
21
+ changed ||= filled !== node;
22
+ return filled;
23
+ });
24
+ return changed ? next : nodes;
25
+ };
26
+ const fillNode = (node, type, held) => {
27
+ if (!isRecord(node) || !Array.isArray(node.children)) return node;
28
+ if (node.type === type) {
29
+ const answer = typeof node.fieldId === "string" ? held.get(node.fieldId)?.value : void 0;
30
+ return answer === void 0 ? node : {
31
+ ...node,
32
+ value: answer
33
+ };
34
+ }
35
+ const children = fillNodes(node.children, type, held);
36
+ return children === node.children ? node : {
37
+ ...node,
38
+ children
39
+ };
40
+ };
41
+ /**
42
+ * Check each role's answers against a frozen template and build the filled
43
+ * document from the answers alone. `type` names the field element's type when
44
+ * the app renamed it with `schema.override`. `submissions` are in the order
45
+ * the roles submitted, and each is checked against the roles before it plus
46
+ * its own answers. A submitted role's checkbox with no answer reads as `false`
47
+ * while it applies.
48
+ *
49
+ * Refuses a template whose fields hold an answer, lack or repeat a `fieldId`,
50
+ * break the schema or have a problem a read reports, a condition loop
51
+ * included. Then refuses, at the first submission that has any: a malformed
52
+ * submission, a role that submitted before or owns no field, an answer for a
53
+ * field that is missing, of another role or does not fit it, a field of the
54
+ * role that waits on another role, a filled answer on a skipped field, and a
55
+ * required field that applies without a filled answer.
56
+ */
57
+ function applyFieldValues(template, submissions, options) {
58
+ const type = options?.type ?? "field";
59
+ const document = isChildren(template) ? { children: template } : template;
60
+ const roots = Object.entries(document.roots ?? {});
61
+ const places = [...placesIn(document.children, type, void 0), ...roots.flatMap(([root, nodes]) => placesIn(nodes, type, root))];
62
+ const first = /* @__PURE__ */ new Map();
63
+ for (const place of places) {
64
+ const { fieldId } = place.node;
65
+ if (typeof fieldId === "string" && !first.has(fieldId)) first.set(fieldId, place);
66
+ }
67
+ const held = /* @__PURE__ */ new Map();
68
+ for (const [fieldId, { node }] of first) if (isWellFormed(node)) held.set(fieldId, node);
69
+ const lookup = (fieldId) => held.get(fieldId);
70
+ const templateLinks = submittedFieldLinks(/* @__PURE__ */ new Set());
71
+ const [templateRefusal, ...templateRefusals] = places.flatMap((place) => {
72
+ const { node, path, root } = place;
73
+ const invalid = (problem) => ({
74
+ code: "invalid-template",
75
+ ...typeof node.fieldId === "string" ? { fieldId: node.fieldId } : {},
76
+ path,
77
+ problem,
78
+ ...root === void 0 ? {} : { root }
79
+ });
80
+ const duplicate = typeof node.fieldId === "string" && first.get(node.fieldId) !== place ? [invalid("duplicate-id")] : [];
81
+ if (!isWellFormed(node)) return [invalid("malformed"), ...duplicate];
82
+ const answered = node.value === void 0 ? [] : [invalid("answered")];
83
+ if (duplicate.length > 0) return [...answered, ...duplicate];
84
+ return [...answered, ...fieldProblems(node, templateLinks.link(node, lookup)).map((problem) => invalid(problem))];
85
+ });
86
+ if (templateRefusal) return {
87
+ ok: false,
88
+ refusals: [templateRefusal, ...templateRefusals]
89
+ };
90
+ const fields = [...held.values()];
91
+ const roles = [...new Set(fields.map(({ role }) => role))];
92
+ const settled = /* @__PURE__ */ new Set();
93
+ for (const [index, submission] of submissions.entries()) {
94
+ if (!isRecord(submission) || typeof submission.role !== "string" || !isRecord(submission.values)) return {
95
+ ok: false,
96
+ refusals: [{
97
+ code: "invalid-submission",
98
+ index
99
+ }]
100
+ };
101
+ const { role, values } = submission;
102
+ if (settled.has(role)) return {
103
+ ok: false,
104
+ refusals: [{
105
+ code: "duplicate-role",
106
+ role
107
+ }]
108
+ };
109
+ if (!roles.includes(role)) return {
110
+ ok: false,
111
+ refusals: [{
112
+ code: "unknown-role",
113
+ role
114
+ }]
115
+ };
116
+ const refusals = [];
117
+ const refusedIds = /* @__PURE__ */ new Set();
118
+ for (const [fieldId, value] of Object.entries(values)) {
119
+ const field = lookup(fieldId);
120
+ const code = !field ? "unknown-field" : field.role !== role ? "wrong-role" : fitsField(field, value) ? void 0 : "invalid-value";
121
+ if (code) {
122
+ refusals.push({
123
+ code,
124
+ fieldId,
125
+ role
126
+ });
127
+ refusedIds.add(fieldId);
128
+ } else if (field) held.set(fieldId, {
129
+ ...field,
130
+ value
131
+ });
132
+ }
133
+ settled.add(role);
134
+ const links = submittedFieldLinks(settled);
135
+ for (const field of fields) {
136
+ if (field.role !== role) continue;
137
+ const verdict = links.link(field, lookup);
138
+ const { fieldId } = field;
139
+ if (verdict.applies === null) {
140
+ refusals.push({
141
+ code: "waiting",
142
+ fieldId,
143
+ role,
144
+ waitsOn: verdict.waitsOn
145
+ });
146
+ continue;
147
+ }
148
+ if (refusedIds.has(fieldId)) continue;
149
+ const filled = isFilled(field, held.get(fieldId)?.value);
150
+ if (!verdict.applies && filled) refusals.push({
151
+ code: "skipped",
152
+ fieldId,
153
+ role
154
+ });
155
+ else if (verdict.applies && field.required && !filled) refusals.push({
156
+ code: "missing-required",
157
+ fieldId,
158
+ role
159
+ });
160
+ }
161
+ const [refusal, ...rest] = refusals;
162
+ if (refusal) return {
163
+ ok: false,
164
+ refusals: [refusal, ...rest]
165
+ };
166
+ }
167
+ const links = submittedFieldLinks(settled);
168
+ const pending = /* @__PURE__ */ new Set();
169
+ for (const field of fields) {
170
+ if (settled.has(field.role) || !field.required) continue;
171
+ const verdict = links.link(field, lookup);
172
+ if (verdict.applies === false) continue;
173
+ pending.add(field.role);
174
+ if (verdict.applies === null) pending.add(verdict.waitsOn);
175
+ }
176
+ const children = fillNodes(document.children, type, held);
177
+ return {
178
+ document: isChildren(template) ? children : {
179
+ ...document,
180
+ children,
181
+ ...document.roots ? { roots: Object.fromEntries(roots.map(([root, nodes]) => [root, fillNodes(nodes, type, held)])) } : {}
182
+ },
183
+ ok: true,
184
+ pending: roles.filter((role) => pending.has(role))
185
+ };
186
+ }
187
+ //#endregion
188
+ export { applyFieldValues };
@@ -1,25 +1,187 @@
1
1
  import { getActiveEditorUpdateId, getEditorRuntimeOwner, getSnapshotVersion, readShownDocument } from "../../../facade.js";
2
2
  import { failInvariant } from "../../../internal/failInvariant.js";
3
3
  //#region src/features/field/lib/fieldRules.internal.ts
4
- const fieldLinks = () => {
5
- const links = /* @__PURE__ */ new Map();
6
- return (field, held) => {
4
+ const FIELD_KINDS = [
5
+ "checkbox",
6
+ "choice",
7
+ "date",
8
+ "number",
9
+ "signature",
10
+ "text"
11
+ ];
12
+ /**
13
+ * The tests a condition may use on a field of each kind. `insert` and `update`
14
+ * refuse any other test, and a read reports it as `unsupported-test`.
15
+ */
16
+ const FIELD_TESTS = {
17
+ checkbox: ["equals", "not-equals"],
18
+ choice: [
19
+ "filled",
20
+ "blank",
21
+ "equals",
22
+ "not-equals"
23
+ ],
24
+ date: [
25
+ "filled",
26
+ "blank",
27
+ "equals",
28
+ "not-equals",
29
+ "above",
30
+ "below"
31
+ ],
32
+ number: [
33
+ "filled",
34
+ "blank",
35
+ "equals",
36
+ "not-equals",
37
+ "above",
38
+ "below"
39
+ ],
40
+ signature: ["filled", "blank"],
41
+ text: [
42
+ "filled",
43
+ "blank",
44
+ "equals",
45
+ "not-equals"
46
+ ]
47
+ };
48
+ const isFieldValue = (value) => typeof value === "string" || typeof value === "boolean" || typeof value === "number" && Number.isFinite(value);
49
+ const isFieldOptions = (value) => Array.isArray(value) && value.every((option) => typeof option?.label === "string" && typeof option.value === "string") && new Set(value.map((option) => option.value)).size === value.length;
50
+ const isFieldCondition = (value) => {
51
+ if (!value || typeof value !== "object") return false;
52
+ const { ref, test, value: compared } = value;
53
+ return typeof ref === "string" && Object.values(FIELD_TESTS).some((tests) => tests.some((known) => known === test)) && (compared === void 0 || isFieldValue(compared));
54
+ };
55
+ const isFieldKind = (value) => FIELD_KINDS.some((kind) => kind === value);
56
+ const ISO_DATE_PATTERN = /^\d{4}-\d{2}-\d{2}$/;
57
+ const isIsoDate = (value) => {
58
+ if (!ISO_DATE_PATTERN.test(value)) return false;
59
+ const date = /* @__PURE__ */ new Date(`${value}T00:00:00Z`);
60
+ return !Number.isNaN(date.getTime()) && date.toISOString().startsWith(value);
61
+ };
62
+ /**
63
+ * Whether a value fits a field's kind: a boolean for a checkbox, one of its
64
+ * options' values for a choice, an ISO date string for a date, a finite number
65
+ * other than -0 for a number, a string with no line break for a text field,
66
+ * and a string for a signature field.
67
+ */
68
+ const fitsField = ({ kind, options = [] }, value) => {
69
+ switch (kind) {
70
+ case "checkbox": return typeof value === "boolean";
71
+ case "choice": return options.some((option) => option.value === value);
72
+ case "date": return typeof value === "string" && isIsoDate(value);
73
+ case "number": return typeof value === "number" && Number.isFinite(value) && !Object.is(value, -0);
74
+ case "text": return typeof value === "string" && !/[\n\r]/.test(value);
75
+ default: return typeof value === "string";
76
+ }
77
+ };
78
+ const conditionProblem = ({ test, value }, named) => {
79
+ if (!named) return "missing-target";
80
+ if (!FIELD_TESTS[named.kind].includes(test)) return "unsupported-test";
81
+ return (test === "filled" || test === "blank" ? value === void 0 : fitsField(named, value)) ? void 0 : "invalid-value";
82
+ };
83
+ const isFilled = (field, answer) => field.kind === "checkbox" ? answer === true : fitsField(field, answer) && (field.kind !== "text" && field.kind !== "signature" || /\S/.test(String(answer)));
84
+ const holds = ({ test, value }, target, answer) => {
85
+ const before = (a, b) => target.kind === "number" ? Number(a) < Number(b) : String(a) < String(b);
86
+ switch (test) {
87
+ case "above":
88
+ case "below":
89
+ if (answer === void 0) return false;
90
+ return test === "above" ? before(value, answer) : before(answer, value);
91
+ case "blank": return !isFilled(target, answer);
92
+ case "equals": return answer === value;
93
+ case "filled": return isFilled(target, answer);
94
+ default: return answer !== value;
95
+ }
96
+ };
97
+ const APPLIES = { applies: true };
98
+ const SKIPS = { applies: false };
99
+ const verdictOf = (applies) => applies ? APPLIES : SKIPS;
100
+ const SHAPED_SETTINGS = [
101
+ "defaultValue",
102
+ "options",
103
+ "value"
104
+ ];
105
+ const fitsSetting = (field, setting) => field[setting] === void 0 || (setting === "options" ? field.kind === "choice" : fitsField(field, field[setting]));
106
+ const unfitSettings = (field) => SHAPED_SETTINGS.filter((setting) => !fitsSetting(field, setting));
107
+ const fieldProblems = (field, link) => {
108
+ const own = unfitSettings(field).length === 0 ? [] : ["invalid-value"];
109
+ const conditioned = field.condition && conditionProblem(field.condition, link.target);
110
+ const problems = conditioned && !own.includes(conditioned) ? [...own, conditioned] : own;
111
+ return link.loop ? [...problems, "condition-loop"] : problems;
112
+ };
113
+ const judge = (condition, target, reached, settled) => {
114
+ if (reached.applies === null) return reached;
115
+ if (!reached.applies) return verdictOf(holds(condition, target, void 0));
116
+ if (fitsField(target, target.value)) return verdictOf(holds(condition, target, target.value));
117
+ if (!settled) return {
118
+ applies: null,
119
+ waitsOn: target.role
120
+ };
121
+ return verdictOf(holds(condition, target, target.kind === "checkbox" ? false : void 0));
122
+ };
123
+ const linksWith = (settles) => {
124
+ const chains = /* @__PURE__ */ new Map();
125
+ const byReadingRole = /* @__PURE__ */ new Map();
126
+ const chainOf = (field, held) => {
7
127
  const path = [];
8
128
  const places = /* @__PURE__ */ new Map();
9
129
  let next = field;
10
- while (next && !links.has(next.fieldId) && !places.has(next.fieldId)) {
130
+ while (next && !chains.has(next.fieldId) && !places.has(next.fieldId)) {
11
131
  places.set(next.fieldId, path.length);
12
132
  path.push(next);
13
133
  next = next.condition && held(next.condition.ref);
14
134
  }
15
135
  const loopStart = next ? places.get(next.fieldId) : void 0;
16
- for (let index = path.length - 1; index >= 0; index--) links.set(path[index].fieldId, {
136
+ for (let index = path.length - 1; index >= 0; index--) chains.set(path[index].fieldId, {
17
137
  loop: loopStart !== void 0 && index >= loopStart,
18
138
  target: path[index + 1] ?? next
19
139
  });
20
- return links.get(field.fieldId) ?? failInvariant("A field was read before its link.");
140
+ return chains.get(field.fieldId) ?? failInvariant("A field was read before its link.");
141
+ };
142
+ const appliesOf = (field, held, readingRole = field.role) => {
143
+ const known = byReadingRole.get(readingRole) ?? /* @__PURE__ */ new Map();
144
+ byReadingRole.set(readingRole, known);
145
+ const waiting = [];
146
+ let at = field;
147
+ let reached = known.get(at.fieldId);
148
+ while (reached === void 0) {
149
+ const { loop, target } = chainOf(at, held);
150
+ if (loop || !at.condition || !target || conditionProblem(at.condition, target)) {
151
+ reached = APPLIES;
152
+ known.set(at.fieldId, reached);
153
+ break;
154
+ }
155
+ waiting.push([at, at.condition]);
156
+ at = target;
157
+ reached = known.get(at.fieldId);
158
+ }
159
+ for (let index = waiting.length - 1; index >= 0; index--) {
160
+ const [next, condition] = waiting[index];
161
+ reached = judge(condition, at, reached, settles(at.role, readingRole));
162
+ known.set(next.fieldId, reached);
163
+ at = next;
164
+ }
165
+ return reached;
166
+ };
167
+ return {
168
+ link: (field, held) => ({
169
+ ...chainOf(field, held),
170
+ ...appliesOf(field, held)
171
+ }),
172
+ linkCopy: (field, held) => {
173
+ const { condition } = field;
174
+ const target = condition && held(condition.ref);
175
+ return {
176
+ ...!condition || !target || conditionProblem(condition, target) ? APPLIES : judge(condition, target, appliesOf(target, held, field.role), settles(target.role, field.role)),
177
+ loop: false,
178
+ target
179
+ };
180
+ }
21
181
  };
22
182
  };
183
+ const fieldLinks = () => linksWith((role, readingRole) => role === readingRole);
184
+ const submittedFieldLinks = (roles) => linksWith((role) => roles.has(role));
23
185
  const shared = /* @__PURE__ */ new WeakMap();
24
186
  const linksOf = (key, version) => {
25
187
  const kept = shared.get(key);
@@ -39,4 +201,4 @@ const sharedFieldLinks = (editor) => {
39
201
  return linksOf(owner, getSnapshotVersion(owner));
40
202
  };
41
203
  //#endregion
42
- export { fieldLinks, sharedFieldLinks };
204
+ export { FIELD_KINDS, FIELD_TESTS, conditionProblem, fieldLinks, fieldProblems, fitsField, isFieldCondition, isFieldKind, isFieldOptions, isFieldValue, isFilled, sharedFieldLinks, submittedFieldLinks, unfitSettings };
@@ -1,6 +1,6 @@
1
- import { eu as TxDeclaredMethod, us as Range } from "../../index-DpltOCot-A2b3p3Eb.js";
2
- import "../../htmlTypes-cg_cWsaN.js";
3
- import { Ao as PluginSelectorMethods, _ as BasePlugin, mo as NormalizePluginSelectors } from "../../core-ClHasORr.js";
1
+ import { iu as TxDeclaredMethod, ps as Range } from "../../index-BXjLmx1n-CXk17buG.js";
2
+ import "../../htmlTypes-eT2ZGDAj.js";
3
+ import { Mo as PluginSelectorMethods, _ as BasePlugin, go as NormalizePluginSelectors } from "../../core-BqR-fEDp.js";
4
4
  type FindMatch = Readonly<{
5
5
  id: string;
6
6
  range: Range;
@@ -1,2 +1,2 @@
1
- import { a as FootnoteDefinition, c as FootnotePluginState, i as FootnoteAnchor, l as FootnoteReferenceElement, n as BaseFootnotePlugin, o as FootnoteDefinitionElement, r as CreateFootnoteDefinitionOptions, s as FootnoteElement, t as BaseFootnoteDefinitionPlugin } from "../../index-BZWFoUXt.js";
1
+ import { a as FootnoteDefinition, c as FootnotePluginState, i as FootnoteAnchor, l as FootnoteReferenceElement, n as BaseFootnotePlugin, o as FootnoteDefinitionElement, r as CreateFootnoteDefinitionOptions, s as FootnoteElement, t as BaseFootnoteDefinitionPlugin } from "../../index-BsV2McRr.js";
2
2
  export { BaseFootnoteDefinitionPlugin, BaseFootnotePlugin, CreateFootnoteDefinitionOptions, FootnoteAnchor, FootnoteDefinition, FootnoteDefinitionElement, FootnoteElement, FootnotePluginState, FootnoteReferenceElement };
@@ -1,2 +1,2 @@
1
- import { a as InkCopyPlace, c as InkMargin, d as InkMarkInput, f as InkRun, g as InkViewState, h as InkTool, i as InkCopy, l as InkMark, m as InkSignPlace, n as InkBlockInput, o as InkDirection, p as InkSample, r as InkBrush, s as InkLine, t as BaseInkPlugin, u as InkMarkBlock } from "../../index-CdOLsPaW.js";
1
+ import { a as InkCopyPlace, c as InkMargin, d as InkMarkInput, f as InkRun, g as InkViewState, h as InkTool, i as InkCopy, l as InkMark, m as InkSignPlace, n as InkBlockInput, o as InkDirection, p as InkSample, r as InkBrush, s as InkLine, t as BaseInkPlugin, u as InkMarkBlock } from "../../index-DwWQVwQt.js";
2
2
  export { BaseInkPlugin, InkBlockInput, InkBrush, InkCopy, InkCopyPlace, InkDirection, InkLine, InkMargin, InkMark, InkMarkBlock, InkMarkInput, InkRun, InkSample, InkSignPlace, InkTool, InkViewState };