platejs 54.0.0-beta.2 → 54.0.0-beta.3

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 (237) hide show
  1. package/README.md +1 -1
  2. package/THIRD_PARTY_NOTICES.md +56 -0
  3. package/dist/{AIChatRequestContext-BMVWhfPV.d.ts → AIChatRequestContext-DY104TqL.d.ts} +2 -2
  4. package/dist/{BaseSuggestionPlugin-DBl3fqnm.d.ts → BaseSuggestionPlugin-Deo6n6Fs.d.ts} +7 -3
  5. package/dist/{PlateStatic-DhNv2ejp.d.ts → PlateStatic-CJX5bKuv.d.ts} +7 -7
  6. package/dist/ai/index.d.ts +1 -1
  7. package/dist/ai/react/AIChatPlugin.js +113 -78
  8. package/dist/ai/react/CopilotPlugin.js +1 -1
  9. package/dist/ai/react/index.d.ts +23 -16
  10. package/dist/authored/index.d.ts +4 -4
  11. package/dist/code-block/codemirror/createCodeMirrorAdapter.js +12 -9
  12. package/dist/code-block/codemirror/index.d.ts +1 -1
  13. package/dist/{combobox-DkBGgyd3.d.ts → combobox-DAOYWuvC.d.ts} +2 -2
  14. package/dist/{comparison-types-CYnJ35qU.d.ts → comparison-types-Dt-unJZY-DCddmbSD.d.ts} +10 -2
  15. package/dist/compiler/index.d.ts +2 -2
  16. package/dist/{core-BJG8l_mk.d.ts → core-ClHasORr.d.ts} +433 -95
  17. package/dist/diff/index.d.ts +2 -2
  18. package/dist/docs/ai.md +4 -3
  19. package/dist/docs/api/core/plate-components.md +0 -19
  20. package/dist/docs/api/core/plate-controller.md +4 -2
  21. package/dist/docs/api/core/plate-plugin.md +3 -0
  22. package/dist/docs/api/core/plate-store.md +3 -3
  23. package/dist/docs/api/dom.md +14 -4
  24. package/dist/docs/api/editor-transforms.md +2 -1
  25. package/dist/docs/api/react-hooks.md +25 -11
  26. package/dist/docs/authored-changes.md +5 -2
  27. package/dist/docs/basic-blocks.md +42 -29
  28. package/dist/docs/blockquote.md +5 -3
  29. package/dist/docs/board.md +63 -8
  30. package/dist/docs/code-block.md +47 -0
  31. package/dist/docs/discussion.md +1 -1
  32. package/dist/docs/docx.md +1 -1
  33. package/dist/docs/dom-coverage.md +5 -0
  34. package/dist/docs/examples/editable-voids.md +2 -3
  35. package/dist/docs/external-text.md +2 -0
  36. package/dist/docs/field.md +370 -0
  37. package/dist/docs/find.md +9 -6
  38. package/dist/docs/form.md +2 -0
  39. package/dist/docs/heading.md +6 -0
  40. package/dist/docs/installation/manual.md +0 -4
  41. package/dist/docs/link.md +3 -3
  42. package/dist/docs/llms.txt +4 -2
  43. package/dist/docs/markdown.md +176 -8
  44. package/dist/docs/plugin-components.md +13 -10
  45. package/dist/docs/plugin-input-rules.md +4 -12
  46. package/dist/docs/prompt-editor.md +205 -0
  47. package/dist/docs/roots.md +8 -6
  48. package/dist/docs/rtl.md +6 -4
  49. package/dist/docs/selection.md +7 -4
  50. package/dist/docs/suggestion.md +1 -1
  51. package/dist/docs/toolbar.md +26 -30
  52. package/dist/docs/upload.md +106 -17
  53. package/dist/docx/export/index.d.ts +3 -3
  54. package/dist/docx/export/lib/docxStaticComponents.js +12 -2
  55. package/dist/docx/export/lib/exportDocx.js +29 -5
  56. package/dist/docx/export/lib/reviewPart.js +1 -1
  57. package/dist/docx/export/lib/reviewProjection.js +10 -8
  58. package/dist/docx/import/index.d.ts +4 -4
  59. package/dist/docx/paste/index.d.ts +4 -4
  60. package/dist/dom/index.d.ts +1 -6
  61. package/dist/dom/plite-dom.internal.js +2 -2
  62. package/dist/excalidraw/index.d.ts +3 -2
  63. package/dist/excalidraw/react/index.d.ts +3 -2
  64. package/dist/facade.js +2 -2
  65. package/dist/features/basic-nodes/lib/BaseBlockPlugins.js +13 -5
  66. package/dist/features/basic-nodes/lib/BaseHeadingPlugins.js +32 -10
  67. package/dist/features/board/index.d.ts +2 -2
  68. package/dist/features/board/lib/BaseBoardPlugin.js +14 -1
  69. package/dist/features/board/lib/boardStroke.internal.js +13 -1
  70. package/dist/features/board/lib/boardTypes.internal.js +16 -0
  71. package/dist/features/callout/index.d.ts +3 -2
  72. package/dist/features/code-block/lib/BaseCodeBlockPlugin.js +14 -5
  73. package/dist/features/combobox/index.d.ts +2 -2
  74. package/dist/features/comments/index.d.ts +1 -1
  75. package/dist/features/date/index.d.ts +3 -2
  76. package/dist/features/details/index.d.ts +3 -2
  77. package/dist/features/details/lib/BaseDetailsPlugin.js +10 -10
  78. package/dist/features/field/index.d.ts +2 -0
  79. package/dist/features/field/index.js +2 -0
  80. package/dist/features/field/lib/BaseFieldPlugin.js +414 -0
  81. package/dist/features/field/lib/fieldRules.internal.js +42 -0
  82. package/dist/features/find/index.d.ts +3 -2
  83. package/dist/features/footnote/index.d.ts +1 -1
  84. package/dist/features/ink/index.d.ts +1 -1
  85. package/dist/features/ink/lib/BaseInkPlugin.js +126 -103
  86. package/dist/features/ink/lib/inkCodec.internal.js +130 -0
  87. package/dist/features/ink/lib/inkGone.internal.js +229 -48
  88. package/dist/features/ink/lib/inkPlaces.internal.js +533 -62
  89. package/dist/features/layout/index.d.ts +1 -1
  90. package/dist/features/link/lib/BaseLinkPlugin.js +6 -4
  91. package/dist/features/lint/index.d.ts +3 -2
  92. package/dist/features/media/index.d.ts +1 -1
  93. package/dist/features/media/lib/media/parseMediaUrl.js +11 -1
  94. package/dist/features/media/lib/media-embed/BaseMediaEmbedPlugin.js +6 -2
  95. package/dist/features/mention/index.d.ts +5 -4
  96. package/dist/features/suggestion/index.d.ts +1 -1
  97. package/dist/features/table/index.d.ts +1 -1
  98. package/dist/features/table/lib/BaseTablePlugin.js +14 -8
  99. package/dist/features/table/lib/internal/selection.js +4 -2
  100. package/dist/features/toc/index.d.ts +1 -1
  101. package/dist/features/upload/index.d.ts +2 -2
  102. package/dist/features/upload/lib/BaseUploadPlugin.js +464 -250
  103. package/dist/history/index.d.ts +2 -2
  104. package/dist/html/index.d.ts +3 -3
  105. package/dist/html/server/index.d.ts +1 -1
  106. package/dist/{htmlTypes-DjeFG3P8.d.ts → htmlTypes-cg_cWsaN.d.ts} +2 -31
  107. package/dist/hyperscript/index.d.ts +1 -1
  108. package/dist/{index-D1CFXA-i.d.ts → index-8WO2vI0U.d.ts} +475 -143
  109. package/dist/{index-DaM37gF6.d.ts → index-B5mYEawV.d.ts} +37 -21
  110. package/dist/{index-Bq3sdm3S.d.ts → index-BZWFoUXt.d.ts} +5 -4
  111. package/dist/{index-DdIRduDG.d.ts → index-C0POz7rg.d.ts} +3 -2
  112. package/dist/{index-Bxfvicyy.d.ts → index-C1_JIhL4.d.ts} +4 -3
  113. package/dist/{index-DC_MNGyu.d.ts → index-C35V_hj0.d.ts} +3 -3
  114. package/dist/{index-kLKMc-pI.d.ts → index-C6ql9pKw.d.ts} +2 -2
  115. package/dist/{index-hsC_sOn2.d.ts → index-C9DiGDVj.d.ts} +35 -5
  116. package/dist/{index-DTgaSxKr.d.ts → index-CLsEe3LY.d.ts} +40 -4
  117. package/dist/{index-CT7miqQN.d.ts → index-CdOLsPaW.d.ts} +29 -9
  118. package/dist/{index-DLA2DUR2.d.ts → index-CktYvOgk.d.ts} +4 -3
  119. package/dist/{index-BBKGrsG3.d.ts → index-Cmuluh6f.d.ts} +1 -1
  120. package/dist/{index-BiQerPJS.d.ts → index-CzLwBZpY.d.ts} +2 -2
  121. package/dist/{index-DzD6BIFJ.d.ts → index-D3-2iM1b.d.ts} +7 -5
  122. package/dist/{index-Cl77E9yr.d.ts → index-DKOFvteF.d.ts} +2 -2
  123. package/dist/{index-iaZOhA76.d.ts → index-DPWyk3lG-DOYwohXO.d.ts} +1 -1
  124. package/dist/index-DQnjqtQa.d.ts +678 -0
  125. package/dist/{index-CcZbqPqk.d.ts → index-Do9pj1GK.d.ts} +73 -39
  126. package/dist/{index-BU6k1Aev.d.ts → index-DpltOCot-A2b3p3Eb.d.ts} +4735 -4586
  127. package/dist/{index-Bc2Yg_2j.d.ts → index-HPYawTae-BCUQLEWF.d.ts} +2 -2
  128. package/dist/{index-BkR7-GLI.d.ts → index-baQOK89E.d.ts} +2 -2
  129. package/dist/{index-D5W-p9bS.d.ts → index-oIJAZxc2.d.ts} +49 -4
  130. package/dist/{index-xdmCBvlT.d.ts → index-r7yb77Wy.d.ts} +3 -2
  131. package/dist/index.d.ts +4 -4
  132. package/dist/{react/internal → internal}/penStroke.internal.js +1 -1
  133. package/dist/internal/plugin/OverridePlugin.js +101 -119
  134. package/dist/internal/plugin/collectPlateNodeMappings.js +5 -1
  135. package/dist/internal/plugin/pastedUploadFiles.js +4 -0
  136. package/dist/internal/plugin/resolvePlugin.js +1 -1
  137. package/dist/internal/plugin/resolvePlugins.js +3 -0
  138. package/dist/internal/plugin/uploadDrafts.js +6 -0
  139. package/dist/internal/utils/mergePlugins.js +1 -0
  140. package/dist/lib/plugins/element-id/ElementIdPlugin.js +8 -2
  141. package/dist/lib/plugins/html/HtmlPlugin.js +9 -0
  142. package/dist/lib/plugins/html/htmlSafety.js +1 -1
  143. package/dist/lib/plugins/input-rules/InputRulesPlugin.js +5 -15
  144. package/dist/lib/plugins/input-rules/inputRuleVeto.internal.js +16 -0
  145. package/dist/markdown/index.d.ts +2 -2
  146. package/dist/markdown/index.js +2 -1
  147. package/dist/markdown/lib/MarkdownPlugin.js +16 -9
  148. package/dist/markdown/lib/RawHtmlPlugin.js +24 -0
  149. package/dist/markdown/lib/deserializer/mdastToSlate.js +12 -2
  150. package/dist/markdown/lib/internal/markdownConversion.js +773 -74
  151. package/dist/markdown/lib/internal/markdownDiagnostics.js +18 -7
  152. package/dist/markdown/lib/internal/markdownFrontmatter.js +102 -0
  153. package/dist/markdown/lib/internal/markdownIntrinsics.js +125 -43
  154. package/dist/markdown/lib/internal/markdownLine.js +5 -3
  155. package/dist/markdown/lib/internal/markdownMappings.js +19 -7
  156. package/dist/markdown/lib/internal/markdownOpenTail.js +381 -0
  157. package/dist/markdown/lib/internal/markdownRawHtml.js +314 -0
  158. package/dist/markdown/lib/internal/markdownSegments.js +57 -48
  159. package/dist/markdown/lib/internal/markdownTags.js +22 -8
  160. package/dist/markdown/lib/serializer/convertNodesSerialize.js +10 -8
  161. package/dist/markdown/lib/serializer/convertPhrasingSerialize.js +226 -0
  162. package/dist/markdown/lib/serializer/listToMdastTree.js +2 -0
  163. package/dist/math/index.d.ts +3 -2
  164. package/dist/math/react/index.d.ts +4 -3
  165. package/dist/migrations/index.d.ts +3 -3
  166. package/dist/pagination/index.d.ts +1 -1
  167. package/dist/pagination/react/index.d.ts +4 -4
  168. package/dist/react/components/EditorContentView.internal.js +17 -8
  169. package/dist/react/components/EditorProvider.js +5 -5
  170. package/dist/react/components/Plate.js +103 -122
  171. package/dist/react/components/PlateContainer.js +40 -32
  172. package/dist/react/components/PlateController.js +28 -16
  173. package/dist/react/components/PlateRoot.internal.js +3 -2
  174. package/dist/react/core.js +4 -4
  175. package/dist/react/features/board/BoardSurface.js +116 -87
  176. package/dist/react/features/board/boardClipboard.internal.js +15 -5
  177. package/dist/react/features/board/boardGesture.internal.js +6 -3
  178. package/dist/react/features/board/boardIndex.internal.js +7 -18
  179. package/dist/react/features/board/boardPictures.internal.js +19 -13
  180. package/dist/react/features/board/boardView.internal.js +184 -49
  181. package/dist/react/features/board/boardVoice.internal.js +10 -11
  182. package/dist/react/features/board/index.d.ts +64 -18
  183. package/dist/react/features/callout/index.d.ts +3 -2
  184. package/dist/react/features/combobox/index.d.ts +8 -8
  185. package/dist/react/features/comments/index.d.ts +6 -5
  186. package/dist/react/features/date/index.d.ts +4 -3
  187. package/dist/react/features/details/index.d.ts +4 -3
  188. package/dist/react/features/field/FieldPlugin.js +34 -0
  189. package/dist/react/features/field/index.d.ts +312 -0
  190. package/dist/react/features/field/index.js +2 -0
  191. package/dist/react/features/footnote/index.d.ts +7 -6
  192. package/dist/react/features/format-painter/index.d.ts +4 -3
  193. package/dist/react/features/ink/InkContent.js +313 -0
  194. package/dist/react/features/ink/index.d.ts +57 -29
  195. package/dist/react/features/ink/index.js +2 -2
  196. package/dist/react/features/ink/inkCapture.internal.js +39 -17
  197. package/dist/react/features/ink/inkInput.internal.js +1 -1
  198. package/dist/react/features/ink/inkLayout.internal.js +6 -3
  199. package/dist/react/features/ink/inkPaint.internal.js +78 -2
  200. package/dist/react/features/ink/inkPlacement.internal.js +331 -104
  201. package/dist/react/features/ink/inkWatch.internal.js +7 -5
  202. package/dist/react/features/layout/index.d.ts +5 -4
  203. package/dist/react/features/media/index.d.ts +5 -4
  204. package/dist/react/features/mention/index.d.ts +6 -5
  205. package/dist/react/features/resizable/Resizable.js +4 -6
  206. package/dist/react/features/resizable/index.d.ts +5 -5
  207. package/dist/react/features/suggestion/index.d.ts +6 -5
  208. package/dist/react/features/table/TablePlugin.js +4 -11
  209. package/dist/react/features/table/index.d.ts +8 -7
  210. package/dist/react/features/toc/index.d.ts +2 -2
  211. package/dist/react/features/upload/index.d.ts +11 -6
  212. package/dist/react/index.d.ts +6 -6
  213. package/dist/react/index.js +4 -4
  214. package/dist/react/internal/plate-context.js +425 -188
  215. package/dist/react/plite-react.js +2 -2
  216. package/dist/react/plugins/navigation-feedback/NavigationFeedbackPlugin.js +12 -10
  217. package/dist/react/stores/plate/useEditor.js +32 -14
  218. package/dist/react/stores/plate/useEditorHistory.js +55 -0
  219. package/dist/react/stores/plate/useEditorSelector.js +66 -21
  220. package/dist/react/utils/pipeRenderElement.internal.js +1 -1
  221. package/dist/react/utils/pluginRenderElement.internal.js +1 -1
  222. package/dist/react/virtualized.d.ts +4 -4
  223. package/dist/{source-CNKy99Th.d.ts → source-aUwpNnAw.d.ts} +4 -4
  224. package/dist/static/index.d.ts +18 -18
  225. package/dist/static/internal/renderStaticHtmlWithOverrides.js +2 -2
  226. package/dist/static/internal/staticPresentation.js +4 -4
  227. package/dist/static/internal/writeStaticSelectionClipboardData.js +7 -2
  228. package/dist/tabbable/index.d.ts +1 -1
  229. package/dist/tabbable/react/index.d.ts +3 -3
  230. package/dist/testing/index.d.ts +3 -3
  231. package/dist/yjs/index.d.ts +1 -1
  232. package/dist/yjs/react/index.d.ts +6 -6
  233. package/package.json +20 -2
  234. package/dist/markdown/lib/serializer/convertTextsSerialize.js +0 -163
  235. package/dist/react/components/EditorSelectionView.js +0 -40
  236. package/dist/react/features/ink/InkLayer.js +0 -218
  237. package/dist/react/internal/createPlateTargetScope.js +0 -62
@@ -1,53 +1,44 @@
1
- import { defineRuntimePlugin, getDocumentValueNode } from "../../../facade.js";
1
+ import { defineRuntimePlugin, getDocumentValueNode, getRecordSetVersion, readRecord, readShownRecords, recordSetIds } from "../../../facade.js";
2
2
  import { definePlugin } from "../../../lib/plugin/definePlugin.js";
3
3
  import { readEditorAuthor } from "../../../lib/editor/editorUser.internal.js";
4
4
  import { ElementIdPlugin } from "../../../lib/plugins/element-id/ElementIdPlugin.js";
5
5
  import { txLocate } from "../../../core.js";
6
- import { GONE, addGone, captureGone, flushGone, hasPendingGone, idOf } from "./inkGone.internal.js";
7
- import { readInkPlace } from "./inkPlaces.internal.js";
6
+ import { countInkDecode, decodePoints, encodePoints, isInkRefusal, refuse, roundSample } from "./inkCodec.internal.js";
7
+ import { COPIES, GONE, MARKS, addGone, captureGone, flushGone, hasPendingGone, idOf } from "./inkGone.internal.js";
8
+ import { advanceInkPlaces, readInkPlace, takeFirstChangeWalkMemo } from "./inkPlaces.internal.js";
8
9
  import { nanoid } from "nanoid";
9
10
  //#region src/features/ink/lib/BaseInkPlugin.ts
10
- const MARKS = "ink-marks";
11
- const COPIES = "ink-copies";
11
+ const copiesOf = ({ copies }) => {
12
+ const ids = typeof copies === "string" ? copies.split(" ") : Array.isArray(copies) ? copies : [];
13
+ return ids.length > 0 && ids.every((id) => typeof id === "string" && id !== "") ? ids : null;
14
+ };
15
+ const beta2MayUndoTo = (id) => id.startsWith("c_");
12
16
  const inkRecords = defineRuntimePlugin("ink-records", {
13
17
  schema: { records: {
14
- [COPIES]: { history: "retain" },
18
+ [COPIES]: {
19
+ history: "retain",
20
+ namedBy: {
21
+ keep: beta2MayUndoTo,
22
+ names: copiesOf,
23
+ set: MARKS
24
+ }
25
+ },
15
26
  [GONE]: { history: "retain" },
16
27
  [MARKS]: {}
17
28
  } },
18
29
  update: ({ editor, tx }) => ({ flush: txLocate(false, () => {
19
- flushGone(editor, tx, (id) => isNamed(tx.value().records, id));
30
+ flushGone(editor, tx);
20
31
  }) })
21
32
  });
22
- const NO_RECORDS = {};
23
- const namedCache = /* @__PURE__ */ new WeakMap();
24
- const namedBy = (set, fields) => {
25
- let ids = namedCache.get(set);
26
- if (!ids) {
27
- const found = /* @__PURE__ */ new Set();
28
- for (const record of Object.values(set)) for (const field of fields) {
29
- const id = record[field];
30
- if (typeof id === "string") found.add(id);
31
- }
32
- ids = found;
33
- namedCache.set(set, ids);
34
- }
35
- return ids;
36
- };
37
- const isNamed = (records, id) => namedBy(records?.[COPIES] ?? NO_RECORDS, ["block"]).has(id) || namedBy(records?.["ink-gone"] ?? NO_RECORDS, ["next", "previous"]).has(id);
38
33
  const decodedCopies = /* @__PURE__ */ new WeakMap();
39
34
  const decodedMarks = /* @__PURE__ */ new WeakMap();
35
+ const markLists = /* @__PURE__ */ new WeakMap();
36
+ const EMPTY_SET_VERSION = {};
40
37
  const frozen = (value) => {
41
38
  for (const field of Object.values(value)) if (typeof field === "object" && field !== null) frozen(field);
42
39
  Object.freeze(value);
43
40
  return value;
44
41
  };
45
- var InkRefusalError = class extends TypeError {
46
- name = "InkRefusalError";
47
- };
48
- const refuse = (message) => {
49
- throw new InkRefusalError(`Ink mark refused: ${message}.`);
50
- };
51
42
  const isNumber = (value) => typeof value === "number" && Number.isFinite(value);
52
43
  const numberIn = (value, name, min = -Infinity, max = Infinity) => {
53
44
  if (!isNumber(value) || value < min || value > max) return refuse(`${name} must be a finite number from ${min} to ${max}`);
@@ -61,19 +52,22 @@ const textIn = (value, name) => typeof value === "string" && value.length > 0 ?
61
52
  const listIn = (value, name) => Array.isArray(value) ? value : refuse(`${name} must be an array`);
62
53
  const directionIn = (value) => value === "ltr" || value === "rtl" ? value : refuse("direction must be 'ltr' or 'rtl'");
63
54
  const sideIn = (value) => value === "left" || value === "right" ? value : refuse("margin.side must be 'left' or 'right'");
64
- const encodeSamples = (input) => {
55
+ const encodeSamples = (input, shape) => {
65
56
  const samples = listIn(input, "samples");
66
- const numbers = [];
67
57
  let time = 0;
68
58
  if (samples.length === 0) refuse("a mark needs at least one sample");
69
- for (const [at, sample] of samples.entries()) {
59
+ const checked = samples.map((sample, at) => {
70
60
  const name = `samples[${at}]`;
71
61
  if (!Array.isArray(sample) || sample.length !== 4) refuse(`${name} must be [x, y, pressure, time]`);
72
- numbers.push(numberIn(sample[0], `${name} x`), numberIn(sample[1], `${name} y`), numberIn(sample[2], `${name} pressure`, 0, 1));
73
62
  time = numberIn(sample[3], `${name} time`, time);
74
- numbers.push(time);
75
- }
76
- return numbers.join(" ");
63
+ return [
64
+ numberIn(sample[0], `${name} x`),
65
+ numberIn(sample[1], `${name} y`),
66
+ numberIn(sample[2], `${name} pressure`, 0, 1),
67
+ time
68
+ ];
69
+ });
70
+ return shape === "packed" ? encodePoints(checked.map(roundSample)) : checked.flat().join(" ");
77
71
  };
78
72
  const copyRecord = (copy, name) => {
79
73
  const text = typeof copy.text === "string" ? copy.text : refuse(`${name}.text must be a string`);
@@ -94,7 +88,8 @@ const copyRecord = (copy, name) => {
94
88
  width: numberIn(copy.width, `${name}.width`, 0)
95
89
  };
96
90
  };
97
- const encodeMark = (input, author) => {
91
+ const encodeMark = (input, author, shape = "packed") => {
92
+ const list = (values) => shape === "packed" ? values.join(" ") : values;
98
93
  if (typeof input !== "object" || input === null) refuse("input must be an object");
99
94
  const blocks = listIn(input.blocks, "blocks");
100
95
  const { margin } = input;
@@ -102,9 +97,16 @@ const encodeMark = (input, author) => {
102
97
  if (margin !== void 0 && blocks.length !== 1) refuse("a margin note sits beside exactly one block");
103
98
  if (new Set(blocks.map((block) => block?.block)).size !== blocks.length) refuse("a mark names each block once");
104
99
  const copies = blocks.map((block, at) => {
105
- const record = typeof block === "object" && block !== null ? copyRecord(block, `blocks[${at}]`) : refuse(`blocks[${at}] must be an object`);
100
+ const { block: id, height, lines, text, width } = typeof block === "object" && block !== null ? copyRecord(block, `blocks[${at}]`) : refuse(`blocks[${at}] must be an object`);
101
+ const record = {
102
+ block: id,
103
+ height,
104
+ lines: list(lines),
105
+ text,
106
+ width
107
+ };
106
108
  return {
107
- id: `c_${digest(JSON.stringify(record))}`,
109
+ id: `${shape === "packed" ? "p" : "c"}_${digest(JSON.stringify(record))}`,
108
110
  record
109
111
  };
110
112
  });
@@ -123,7 +125,7 @@ const encodeMark = (input, author) => {
123
125
  }
124
126
  }
125
127
  const brush = typeof input.brush === "object" && input.brush !== null ? input.brush : refuse("brush must be an object");
126
- const place = margin === void 0 ? { runs } : {
128
+ const place = margin === void 0 ? { runs: list(runs) } : {
127
129
  reach: numberIn(margin.reach, "margin.reach", 0),
128
130
  side: sideIn(margin.side)
129
131
  };
@@ -133,11 +135,10 @@ const encodeMark = (input, author) => {
133
135
  at: numberIn(input.at, "at", 0),
134
136
  author,
135
137
  color: textIn(brush.color, "brush.color"),
136
- copies: copies.map((copy) => copy.id),
138
+ copies: list(copies.map((copy) => copy.id)),
137
139
  direction: directionIn(input.direction),
138
140
  opacity: numberIn(brush.opacity, "brush.opacity", 0, 1),
139
- origins,
140
- points: encodeSamples(input.samples),
141
+ origins: list(origins),
141
142
  size: numberIn(brush.size, "brush.size", 0),
142
143
  ...place
143
144
  }
@@ -162,7 +163,7 @@ const readSaved = (read) => {
162
163
  try {
163
164
  return read();
164
165
  } catch (error) {
165
- if (error instanceof InkRefusalError) return void 0;
166
+ if (isInkRefusal(error)) return void 0;
166
167
  throw error;
167
168
  }
168
169
  };
@@ -170,13 +171,15 @@ const sameFields = (encoded, saved) => Object.entries(encoded).every(([key, valu
170
171
  const other = saved[key];
171
172
  return Array.isArray(value) && Array.isArray(other) ? value.length === other.length && value.every((item, at) => item === other[at]) : value === other;
172
173
  });
174
+ const savedList = (value, name) => typeof value === "string" ? value === "" ? [] : value.split(" ") : listIn(value, name);
173
175
  const savedNumbers = (value, name, stride) => {
174
- const numbers = listIn(value, name);
176
+ const numbers = savedList(value, name).map((number) => typeof number === "string" ? Number(number) : number);
175
177
  if (numbers.length % stride !== 0) refuse(`${name} must hold ${stride} numbers per entry`);
176
178
  return numbers.map((number, at) => numberIn(number, `${name}[${at}]`));
177
179
  };
178
180
  const groupsOf = (numbers, stride, build) => Array.from({ length: numbers.length / stride }, (_, index) => build(index * stride));
179
181
  const decodeCopy = (id, record) => readSaved(() => {
182
+ countInkDecode("copies");
180
183
  const numbers = savedNumbers(record.lines, "lines", 5);
181
184
  const lines = groupsOf(numbers, 5, (at) => ({
182
185
  end: numbers[at],
@@ -185,10 +188,11 @@ const decodeCopy = (id, record) => readSaved(() => {
185
188
  top: numbers[at + 2],
186
189
  width: numbers[at + 3]
187
190
  }));
188
- const { block, height, text, width } = copyRecord({
191
+ const { block, height, text, width, ...written } = copyRecord({
189
192
  ...record,
190
193
  lines
191
194
  }, "copy");
195
+ if (typeof record.lines === "string" && written.lines.join(" ") !== record.lines) refuse("a saved copy must be what add writes");
192
196
  return frozen({
193
197
  block,
194
198
  height,
@@ -199,10 +203,12 @@ const decodeCopy = (id, record) => readSaved(() => {
199
203
  });
200
204
  });
201
205
  const decodeMark = (id, record, copyOf) => readSaved(() => {
202
- const copies = listIn(record.copies, "copies").map((copy, at) => typeof copy === "string" && copyOf(copy) || refuse(`copies[${at}] must name a saved copy`));
206
+ countInkDecode("marks");
207
+ const shape = typeof record.copies === "string" ? "packed" : "beta2";
208
+ const copies = savedList(record.copies, "copies").map((copy, at) => typeof copy === "string" && copyOf(copy) || refuse(`copies[${at}] must name a saved copy`));
203
209
  const origins = savedNumbers(record.origins, "origins", 2);
204
210
  const runs = record.runs === void 0 ? [] : savedNumbers(record.runs, "runs", 5);
205
- const points = savedNumbers(typeof record.points === "string" ? record.points.split(" ").map(Number) : void 0, "points", 4);
211
+ const points = shape === "packed" ? [] : savedNumbers(record.points, "points", 4);
206
212
  const marked = groupsOf(runs, 5, (at) => ({
207
213
  copy: runs[at],
208
214
  run: {
@@ -231,7 +237,7 @@ const decodeMark = (id, record, copyOf) => readSaved(() => {
231
237
  reach: numberIn(record.reach, "reach"),
232
238
  side: sideIn(record.side)
233
239
  } } : {},
234
- samples: groupsOf(points, 4, (at) => [
240
+ samples: shape === "packed" ? decodePoints(textIn(record.points, "points")) : groupsOf(points, 4, (at) => [
235
241
  points[at],
236
242
  points[at + 1],
237
243
  points[at + 2],
@@ -248,8 +254,8 @@ const decodeMark = (id, record, copyOf) => readSaved(() => {
248
254
  text: copies[index].text,
249
255
  width: copies[index].width
250
256
  }))
251
- }, author).mark;
252
- if (!sameFields(written, record)) refuse("a saved mark must be what add writes");
257
+ }, author, shape).mark;
258
+ if (!sameFields(written, record) || shape === "beta2" && encodeSamples(mark.samples, "beta2") !== record.points) refuse("a saved mark must be what add writes");
253
259
  return frozen({
254
260
  ...mark,
255
261
  author,
@@ -257,12 +263,28 @@ const decodeMark = (id, record, copyOf) => readSaved(() => {
257
263
  id
258
264
  });
259
265
  });
266
+ const copyIn = (copies, id) => {
267
+ const record = readRecord(copies, id);
268
+ if (record && !decodedCopies.has(record)) decodedCopies.set(record, decodeCopy(id, record));
269
+ return record && decodedCopies.get(record);
270
+ };
271
+ const markIn = (id, record, copies) => {
272
+ const named = (copiesOf(record) ?? []).map((copy) => readRecord(copies, copy));
273
+ const decoded = decodedMarks.get(record);
274
+ if (decoded?.named.every((copy, at) => copy === named[at])) return decoded.mark;
275
+ const mark = decodeMark(id, record, (copy) => copyIn(copies, copy));
276
+ decodedMarks.set(record, {
277
+ mark,
278
+ named
279
+ });
280
+ return mark;
281
+ };
260
282
  /**
261
283
  * Pen marks on a document's text, saved as records of the document value.
262
284
  * Its updates add and remove marks, and its reads return them decoded and say
263
285
  * where each copy's block stands. It turns on block ids, because a mark names
264
- * its blocks by id, and saves where a deleted block stood in the update that
265
- * deletes it.
286
+ * its blocks by id, and saves where each marked block stands with its stroke,
287
+ * and again in a delete that leaves that place wrong.
266
288
  */
267
289
  const BaseInkPlugin = definePlugin("ink", {
268
290
  corrections: [{
@@ -284,63 +306,55 @@ const BaseInkPlugin = definePlugin("ink", {
284
306
  picked: [],
285
307
  tool: null
286
308
  }),
287
- on: { transactionChange: ({ after, before, changed, editor, tx }) => {
288
- if (!changed.has("structure") && !changed.has("properties")) return;
289
- for (const { node, path } of changed.removed()) {
290
- const id = idOf(node);
291
- if (id) captureGone(editor, tx.tags, after, {
292
- gap: path,
293
- id
294
- });
295
- }
296
- if (changed.has("structure")) return;
297
- for (const path of changed.paths()) {
298
- const id = idOf(getDocumentValueNode(before, path));
299
- if (id && id !== idOf(getDocumentValueNode(after, path))) captureGone(editor, tx.tags, after, {
300
- gap: path,
301
- id
302
- });
309
+ on: {
310
+ commit: ({ commit, editor }) => {
311
+ advanceInkPlaces(editor, commit);
312
+ },
313
+ transactionChange: ({ after, before, changed, editor, tx }) => {
314
+ if (!changed.has("structure") && !changed.has("properties")) return;
315
+ const memo = takeFirstChangeWalkMemo(editor);
316
+ for (const { node, path } of changed.removed()) {
317
+ const id = idOf(node);
318
+ if (id) captureGone(editor, tx, after, [path, id], memo);
319
+ }
320
+ if (changed.has("structure")) return;
321
+ for (const path of changed.paths()) {
322
+ const id = idOf(getDocumentValueNode(before, path));
323
+ if (id && id !== idOf(getDocumentValueNode(after, path))) captureGone(editor, tx, after, [path, id], memo);
324
+ }
303
325
  }
304
- } },
305
- read: ({ state }) => {
306
- const set = (name) => state.value().records?.[name];
307
- const copyReader = (saved = set(COPIES) ?? NO_RECORDS) => {
308
- const decoded = decodedCopies.get(saved) ?? /* @__PURE__ */ new Map();
309
- decodedCopies.set(saved, decoded);
310
- return (id) => {
311
- if (!decoded.has(id)) {
312
- const record = saved[id];
313
- decoded.set(id, record && decodeCopy(id, record));
314
- }
315
- return decoded.get(id);
316
- };
317
- };
318
- const copy = (id) => copyReader()(id);
326
+ },
327
+ read: ({ editor, state }) => {
328
+ const version = (set) => getRecordSetVersion(readShownRecords(editor), set);
329
+ const copy = (id) => copyIn(version(COPIES), id);
319
330
  return {
320
331
  /** The copy saved under `id`, or `undefined`. */
321
332
  copy,
322
333
  /** The mark saved under `id`, or `undefined`. */
323
334
  mark: (id) => {
324
- const record = set(MARKS)?.[id];
325
- return record && decodeMark(id, record, copyReader());
335
+ const record = readRecord(version(MARKS), id);
336
+ return record && markIn(id, record, version("ink-copies"));
326
337
  },
327
338
  /**
328
339
  * Every saved mark, oldest first, frozen, as the same list while the
329
- * saved marks and copies stay the same.
340
+ * saved marks and copies stay the same. Each mark stays the same object
341
+ * while its record and the copies it names stay the same.
330
342
  */
331
343
  marks: () => {
332
- const saved = set(MARKS) ?? NO_RECORDS;
333
- const copies = set(COPIES) ?? NO_RECORDS;
334
- let byCopies = decodedMarks.get(saved);
344
+ const saved = version(MARKS);
345
+ const copies = version(COPIES);
346
+ let byCopies = markLists.get(saved ?? EMPTY_SET_VERSION);
335
347
  if (!byCopies) {
336
348
  byCopies = /* @__PURE__ */ new WeakMap();
337
- decodedMarks.set(saved, byCopies);
349
+ markLists.set(saved ?? EMPTY_SET_VERSION, byCopies);
338
350
  }
339
- let marks = byCopies.get(copies);
351
+ let marks = byCopies.get(copies ?? EMPTY_SET_VERSION);
340
352
  if (!marks) {
341
- const copyOf = copyReader(copies);
342
- marks = Object.freeze(Object.entries(saved).flatMap(([id, record]) => decodeMark(id, record, copyOf) ?? []).sort((a, b) => a.at - b.at || (a.id < b.id ? -1 : 1)));
343
- byCopies.set(copies, marks);
353
+ marks = Object.freeze(recordSetIds(saved).flatMap((id) => {
354
+ const record = readRecord(saved, id);
355
+ return (record && markIn(id, record, copies)) ?? [];
356
+ }).sort((a, b) => a.at - b.at || (a.id < b.id ? -1 : 1)));
357
+ byCopies.set(copies ?? EMPTY_SET_VERSION, marks);
344
358
  }
345
359
  return marks;
346
360
  },
@@ -352,6 +366,7 @@ const BaseInkPlugin = definePlugin("ink", {
352
366
  */
353
367
  place: (copyId) => readInkPlace({
354
368
  copy,
369
+ editor,
355
370
  state
356
371
  }, copyId)
357
372
  };
@@ -359,18 +374,26 @@ const BaseInkPlugin = definePlugin("ink", {
359
374
  update: ({ editor, tx }) => ({
360
375
  /**
361
376
  * Save one mark and each copy not saved yet, as one undo step, and
362
- * return the mark's id. Refuses malformed input, writing nothing. A copy's
363
- * block deleted by this update, or by one of the last four updates that
364
- * deleted blocks, gets its place saved too.
377
+ * return the mark's id. Each sample saves rounded to a hundredth of a
378
+ * pixel, a thousandth of pressure and a whole millisecond. Refuses
379
+ * malformed input, and a sample whose hundredths of a pixel or
380
+ * milliseconds pass a quarter of `Number.MAX_SAFE_INTEGER`, writing
381
+ * nothing. A copy's
382
+ * block deleted by this update, by one of the last four updates that
383
+ * deleted blocks, or while a stroke that entered it was still unsaved,
384
+ * gets its place saved too.
385
+ * Each other copy's block and the block on each side of it get theirs.
365
386
  */
366
387
  add: txLocate(false, (input) => {
367
388
  const { copies, mark } = encodeMark(input, readEditorAuthor(editor));
389
+ const points = encodeSamples(input.samples, "packed");
368
390
  const id = `m_${nanoid()}`;
369
- for (const copy of copies) {
370
- tx.records.put(COPIES, copy.id, copy.record);
371
- addGone(editor, tx, copy.record.block);
372
- }
373
- tx.records.put(MARKS, id, mark);
391
+ for (const copy of copies) tx.records.put(COPIES, copy.id, copy.record);
392
+ addGone(editor, tx, copies);
393
+ tx.records.put(MARKS, id, {
394
+ ...mark,
395
+ points
396
+ });
374
397
  return id;
375
398
  }),
376
399
  /** Drop the marks `ids` names. Their copies stay. */
@@ -0,0 +1,130 @@
1
+ //#region src/features/ink/lib/inkCodec.internal.ts
2
+ var InkRefusalError = class extends TypeError {
3
+ name = "InkRefusalError";
4
+ };
5
+ const isInkRefusal = (error) => error instanceof InkRefusalError;
6
+ const refuse = (message) => {
7
+ throw new InkRefusalError(`Ink mark refused: ${message}.`);
8
+ };
9
+ const decodes = {
10
+ copies: 0,
11
+ marks: 0
12
+ };
13
+ const countInkDecode = (kind) => {
14
+ decodes[kind] += 1;
15
+ };
16
+ const zigzag = (delta) => delta < 0 ? -delta * 2 - 1 : delta * 2;
17
+ const unzigzag = (value) => value % 2 === 0 ? value / 2 : -(value + 1) / 2;
18
+ const SAFE_ZIGZAG_REACH = Number.MAX_SAFE_INTEGER / 4;
19
+ const onGrid = (value, steps) => {
20
+ const grid = Math.round(value * steps);
21
+ if (Math.abs(grid) > SAFE_ZIGZAG_REACH) refuse("a sample lies too far out");
22
+ return grid === 0 ? 0 : grid / steps;
23
+ };
24
+ const roundPixel = (value) => onGrid(value, 100);
25
+ const roundSample = ([x, y, pressure, time]) => [
26
+ roundPixel(x),
27
+ roundPixel(y),
28
+ onGrid(pressure, 1e3),
29
+ onGrid(time, 1)
30
+ ];
31
+ const GRID_STEPS = [
32
+ 100,
33
+ 100,
34
+ 1e3,
35
+ 1
36
+ ];
37
+ const ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
38
+ const VALUES = new Map(ALPHABET.split("").map((char, value) => [char, value]));
39
+ const toUnpaddedBase64 = (bytes) => {
40
+ let text = "";
41
+ for (let at = 0; at < bytes.length; at += 3) {
42
+ const chunk = bytes[at] << 16 | (bytes[at + 1] ?? 0) << 8 | (bytes[at + 2] ?? 0);
43
+ const chars = Math.min(4, Math.ceil((bytes.length - at) * 4 / 3));
44
+ for (let char = 0; char < chars; char++) text += ALPHABET[chunk >> 18 - 6 * char & 63];
45
+ }
46
+ return text;
47
+ };
48
+ const fromUnpaddedBase64 = (text) => {
49
+ const bytes = [];
50
+ let held = 0;
51
+ let bits = 0;
52
+ for (const char of text) {
53
+ const value = VALUES.get(char) ?? refuse("points must be base64 without = padding");
54
+ held = held << 6 | value;
55
+ bits += 6;
56
+ if (bits >= 8) {
57
+ bits -= 8;
58
+ bytes.push(held >> bits);
59
+ held &= (1 << bits) - 1;
60
+ }
61
+ }
62
+ return bytes;
63
+ };
64
+ const writeVarint = (bytes, value) => {
65
+ let rest = value;
66
+ while (rest >= 128) {
67
+ bytes.push(rest % 128 | 128);
68
+ rest = Math.floor(rest / 128);
69
+ }
70
+ bytes.push(rest);
71
+ };
72
+ const encodePoints = (samples) => {
73
+ const bytes = [];
74
+ let last = [
75
+ 0,
76
+ 0,
77
+ 0,
78
+ 0
79
+ ];
80
+ writeVarint(bytes, samples.length);
81
+ for (const sample of samples) {
82
+ const grid = sample.map((value, at) => Math.round(value * GRID_STEPS[at]));
83
+ for (const [at, value] of grid.entries()) writeVarint(bytes, zigzag(value - last[at]));
84
+ last = grid;
85
+ }
86
+ return toUnpaddedBase64(bytes);
87
+ };
88
+ const decodePoints = (text) => {
89
+ const bytes = fromUnpaddedBase64(text);
90
+ let at = 0;
91
+ const readVarint = () => {
92
+ let value = 0;
93
+ let scale = 1;
94
+ for (;;) {
95
+ const byte = bytes[at] ?? refuse("points end inside a sample");
96
+ at += 1;
97
+ value += (byte & 127) * scale;
98
+ if (byte < 128) break;
99
+ scale *= 128;
100
+ }
101
+ return Number.isSafeInteger(value) ? value : refuse("a points number is too large");
102
+ };
103
+ const count = readVarint();
104
+ if (count > bytes.length / 4) refuse("points name more samples than their bytes hold");
105
+ const samples = new Array(count);
106
+ let last = [
107
+ 0,
108
+ 0,
109
+ 0,
110
+ 0
111
+ ];
112
+ for (let sample = 0; sample < count; sample++) {
113
+ const grid = last.map((value) => value + unzigzag(readVarint()));
114
+ if (grid[2] < 0 || grid[2] > 1e3) refuse("a pressure runs from 0 to 1");
115
+ if (grid[3] < last[3]) refuse("a sample's time never goes back");
116
+ if (grid.some((value) => Math.abs(value) > SAFE_ZIGZAG_REACH)) refuse("a sample lies too far out");
117
+ samples[sample] = [
118
+ grid[0] / 100,
119
+ grid[1] / 100,
120
+ grid[2] / 1e3,
121
+ grid[3]
122
+ ];
123
+ last = grid;
124
+ }
125
+ if (at < bytes.length) refuse("points hold bytes after their last sample");
126
+ if (encodePoints(samples) !== text) refuse("points must be what add writes");
127
+ return samples;
128
+ };
129
+ //#endregion
130
+ export { countInkDecode, decodePoints, encodePoints, isInkRefusal, refuse, roundPixel, roundSample };