platejs 54.0.0-beta.2 → 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 (255) hide show
  1. package/README.md +1 -1
  2. package/THIRD_PARTY_NOTICES.md +56 -0
  3. package/dist/{AIChatRequestContext-BMVWhfPV.d.ts → AIChatRequestContext-DrUahdTc.d.ts} +2 -2
  4. package/dist/{BaseSuggestionPlugin-DBl3fqnm.d.ts → BaseSuggestionPlugin-_7Ka6nY3.d.ts} +7 -3
  5. package/dist/{PlateStatic-DhNv2ejp.d.ts → PlateStatic-D3Cb-QwH.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-Dr0OYZIE.d.ts} +2 -2
  14. package/dist/{comparison-types-CYnJ35qU.d.ts → comparison-types-DdwE6mYq-BogsHoUO.d.ts} +10 -2
  15. package/dist/compiler/index.d.ts +2 -2
  16. package/dist/{core-BJG8l_mk.d.ts → core-BqR-fEDp.d.ts} +493 -116
  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 +5 -3
  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/core.md +45 -5
  24. package/dist/docs/api/dom.md +11 -4
  25. package/dist/docs/api/editor-transforms.md +2 -1
  26. package/dist/docs/api/react-hooks.md +45 -13
  27. package/dist/docs/authored-changes.md +9 -4
  28. package/dist/docs/basic-blocks.md +42 -29
  29. package/dist/docs/block-placeholder.md +4 -2
  30. package/dist/docs/blockquote.md +5 -3
  31. package/dist/docs/board.md +118 -10
  32. package/dist/docs/code-block.md +47 -0
  33. package/dist/docs/comment.md +2 -2
  34. package/dist/docs/discussion.md +1 -1
  35. package/dist/docs/document-model.md +15 -3
  36. package/dist/docs/docx.md +1 -1
  37. package/dist/docs/dom-coverage.md +5 -0
  38. package/dist/docs/editor.md +50 -4
  39. package/dist/docs/examples/editable-voids.md +2 -3
  40. package/dist/docs/external-text.md +2 -0
  41. package/dist/docs/field.md +370 -0
  42. package/dist/docs/find.md +9 -6
  43. package/dist/docs/form.md +2 -0
  44. package/dist/docs/heading.md +6 -0
  45. package/dist/docs/installation/manual.md +0 -4
  46. package/dist/docs/link.md +3 -3
  47. package/dist/docs/llms.txt +4 -2
  48. package/dist/docs/markdown.md +270 -14
  49. package/dist/docs/plugin-components.md +13 -10
  50. package/dist/docs/plugin-input-rules.md +66 -15
  51. package/dist/docs/prompt-editor.md +205 -0
  52. package/dist/docs/roots.md +8 -6
  53. package/dist/docs/rtl.md +6 -4
  54. package/dist/docs/selection.md +7 -4
  55. package/dist/docs/suggestion.md +1 -1
  56. package/dist/docs/toolbar.md +26 -30
  57. package/dist/docs/upload.md +106 -17
  58. package/dist/docx/export/index.d.ts +3 -3
  59. package/dist/docx/export/lib/docxStaticComponents.js +12 -2
  60. package/dist/docx/export/lib/exportDocx.js +46 -17
  61. package/dist/docx/export/lib/internal/xml-builder.js +11 -16
  62. package/dist/docx/export/lib/reviewPart.js +1 -1
  63. package/dist/docx/export/lib/reviewProjection.js +52 -10
  64. package/dist/docx/import/index.d.ts +4 -4
  65. package/dist/docx/paste/index.d.ts +4 -4
  66. package/dist/dom/index.d.ts +1 -6
  67. package/dist/dom/plite-dom.internal.js +2 -2
  68. package/dist/excalidraw/index.d.ts +3 -2
  69. package/dist/excalidraw/react/index.d.ts +3 -2
  70. package/dist/facade.js +2 -2
  71. package/dist/features/basic-nodes/lib/BaseBlockPlugins.js +13 -5
  72. package/dist/features/basic-nodes/lib/BaseHeadingPlugins.js +32 -10
  73. package/dist/features/board/index.d.ts +2 -2
  74. package/dist/features/board/lib/BaseBoardPlugin.js +14 -1
  75. package/dist/features/board/lib/boardStroke.internal.js +13 -1
  76. package/dist/features/board/lib/boardTypes.internal.js +16 -0
  77. package/dist/features/callout/index.d.ts +3 -2
  78. package/dist/features/code-block/lib/BaseCodeBlockPlugin.js +14 -5
  79. package/dist/features/combobox/index.d.ts +2 -2
  80. package/dist/features/comments/BaseCommentsPlugin.js +33 -17
  81. package/dist/features/comments/index.d.ts +1 -1
  82. package/dist/features/date/index.d.ts +3 -2
  83. package/dist/features/details/index.d.ts +3 -2
  84. package/dist/features/details/lib/BaseDetailsPlugin.js +25 -12
  85. package/dist/features/field/index.d.ts +2 -0
  86. package/dist/features/field/index.js +4 -0
  87. package/dist/features/field/lib/BaseFieldPlugin.js +360 -0
  88. package/dist/features/field/lib/applyFieldValues.js +188 -0
  89. package/dist/features/field/lib/fieldRules.internal.js +204 -0
  90. package/dist/features/find/index.d.ts +3 -2
  91. package/dist/features/footnote/index.d.ts +1 -1
  92. package/dist/features/ink/index.d.ts +1 -1
  93. package/dist/features/ink/lib/BaseInkPlugin.js +126 -103
  94. package/dist/features/ink/lib/inkCodec.internal.js +130 -0
  95. package/dist/features/ink/lib/inkGone.internal.js +229 -48
  96. package/dist/features/ink/lib/inkPlaces.internal.js +534 -64
  97. package/dist/features/layout/index.d.ts +1 -1
  98. package/dist/features/link/lib/BaseLinkPlugin.js +6 -4
  99. package/dist/features/lint/index.d.ts +3 -2
  100. package/dist/features/media/index.d.ts +2 -2
  101. package/dist/features/media/index.js +2 -2
  102. package/dist/features/media/lib/media/parseMediaUrl.js +11 -1
  103. package/dist/features/media/lib/media-embed/BaseMediaEmbedPlugin.js +206 -4
  104. package/dist/features/mention/index.d.ts +5 -4
  105. package/dist/features/suggestion/index.d.ts +1 -1
  106. package/dist/features/table/index.d.ts +1 -1
  107. package/dist/features/table/lib/BaseTablePlugin.js +64 -15
  108. package/dist/features/table/lib/internal/selection.js +4 -2
  109. package/dist/features/toc/index.d.ts +1 -1
  110. package/dist/features/upload/index.d.ts +2 -2
  111. package/dist/features/upload/lib/BaseUploadPlugin.js +464 -250
  112. package/dist/history/index.d.ts +2 -2
  113. package/dist/html/index.d.ts +3 -3
  114. package/dist/html/server/index.d.ts +1 -1
  115. package/dist/{htmlTypes-DjeFG3P8.d.ts → htmlTypes-eT2ZGDAj.d.ts} +2 -31
  116. package/dist/hyperscript/index.d.ts +1 -1
  117. package/dist/{index-DzD6BIFJ.d.ts → index-B33E1CRJ.d.ts} +295 -6
  118. package/dist/{index-Cl77E9yr.d.ts → index-B7moSod7.d.ts} +2 -2
  119. package/dist/{index-hsC_sOn2.d.ts → index-B8-5Sr7I.d.ts} +35 -5
  120. package/dist/{index-BiQerPJS.d.ts → index-B8QJDAmG.d.ts} +2 -2
  121. package/dist/{index-BBKGrsG3.d.ts → index-BTx5oK7k.d.ts} +1 -1
  122. package/dist/{index-iaZOhA76.d.ts → index-BWPcE4ac-Cto_IsNs.d.ts} +7 -1
  123. package/dist/{index-BU6k1Aev.d.ts → index-BXjLmx1n-CXk17buG.d.ts} +4781 -4584
  124. package/dist/{index-kLKMc-pI.d.ts → index-BXwU9QH6.d.ts} +2 -2
  125. package/dist/{index-DTgaSxKr.d.ts → index-BkwR-5hH.d.ts} +40 -4
  126. package/dist/{index-Bq3sdm3S.d.ts → index-BsV2McRr.d.ts} +5 -4
  127. package/dist/index-C9Fu-P4E.d.ts +773 -0
  128. package/dist/{index-D5W-p9bS.d.ts → index-Ca48r1ng.d.ts} +49 -4
  129. package/dist/{index-CcZbqPqk.d.ts → index-Cob_iBdu.d.ts} +116 -41
  130. package/dist/{index-Bxfvicyy.d.ts → index-CpY7Xnxw.d.ts} +4 -3
  131. package/dist/{index-BkR7-GLI.d.ts → index-DNQXzU-X.d.ts} +2 -2
  132. package/dist/{index-D1CFXA-i.d.ts → index-DPowErHa.d.ts} +471 -141
  133. package/dist/{index-DaM37gF6.d.ts → index-DShbzec2.d.ts} +38 -23
  134. package/dist/{index-DC_MNGyu.d.ts → index-DYdObeFQ.d.ts} +3 -3
  135. package/dist/{index-Bc2Yg_2j.d.ts → index-Dq3Jlj8O-BJ6NxZm0.d.ts} +2 -2
  136. package/dist/{index-CT7miqQN.d.ts → index-DwWQVwQt.d.ts} +29 -11
  137. package/dist/{index-xdmCBvlT.d.ts → index-JWgwL5l3.d.ts} +5 -2
  138. package/dist/{index-DLA2DUR2.d.ts → index-M781pyx9.d.ts} +4 -3
  139. package/dist/{index-DdIRduDG.d.ts → index-d7io3MzC.d.ts} +3 -2
  140. package/dist/index.d.ts +4 -4
  141. package/dist/{react/internal → internal}/penStroke.internal.js +1 -1
  142. package/dist/internal/plugin/OverridePlugin.js +101 -119
  143. package/dist/internal/plugin/collectPlateNodeMappings.js +5 -1
  144. package/dist/internal/plugin/pastedUploadFiles.js +4 -0
  145. package/dist/internal/plugin/resolvePlugin.js +1 -1
  146. package/dist/internal/plugin/resolvePlugins.js +3 -0
  147. package/dist/internal/plugin/uploadDrafts.js +6 -0
  148. package/dist/internal/utils/mergePlugins.js +1 -0
  149. package/dist/lib/plugins/element-id/ElementIdPlugin.js +14 -18
  150. package/dist/lib/plugins/html/HtmlPlugin.js +9 -0
  151. package/dist/lib/plugins/html/htmlSafety.js +1 -1
  152. package/dist/lib/plugins/input-rules/InputRulesPlugin.js +77 -28
  153. package/dist/lib/plugins/input-rules/inputRuleVeto.internal.js +16 -0
  154. package/dist/markdown/index.d.ts +2 -2
  155. package/dist/markdown/index.js +3 -1
  156. package/dist/markdown/lib/MarkdownPlugin.js +14 -11
  157. package/dist/markdown/lib/RawHtmlPlugin.js +26 -0
  158. package/dist/markdown/lib/deserializer/mdastToSlate.js +12 -2
  159. package/dist/markdown/lib/internal/markdownConversion.js +776 -74
  160. package/dist/markdown/lib/internal/markdownDiagnostics.js +18 -7
  161. package/dist/markdown/lib/internal/markdownFootnoteWriter.js +35 -0
  162. package/dist/markdown/lib/internal/markdownFrontmatter.js +102 -0
  163. package/dist/markdown/lib/internal/markdownIntrinsics.js +125 -43
  164. package/dist/markdown/lib/internal/markdownLine.js +5 -3
  165. package/dist/markdown/lib/internal/markdownMappings.js +19 -7
  166. package/dist/markdown/lib/internal/markdownOpenTail.js +381 -0
  167. package/dist/markdown/lib/internal/markdownRawHtml.js +353 -0
  168. package/dist/markdown/lib/internal/markdownSegments.js +57 -48
  169. package/dist/markdown/lib/internal/markdownTags.js +29 -9
  170. package/dist/markdown/lib/serializer/convertNodesSerialize.js +10 -8
  171. package/dist/markdown/lib/serializer/convertPhrasingSerialize.js +226 -0
  172. package/dist/markdown/lib/serializer/listToMdastTree.js +2 -0
  173. package/dist/math/index.d.ts +3 -2
  174. package/dist/math/react/index.d.ts +4 -3
  175. package/dist/migrations/index.d.ts +3 -3
  176. package/dist/migrations/migratePlateV54.js +4 -6
  177. package/dist/migrations/migratePlateV54RetiredMarks.internal.js +23 -8
  178. package/dist/migrations/migratePlateV54Suggestions.internal.js +2 -2
  179. package/dist/migrations/v53-manifest.internal.js +4 -0
  180. package/dist/pagination/index.d.ts +1 -1
  181. package/dist/pagination/react/index.d.ts +4 -4
  182. package/dist/react/components/EditorContentView.internal.js +42 -23
  183. package/dist/react/components/EditorProvider.js +5 -5
  184. package/dist/react/components/Plate.js +102 -128
  185. package/dist/react/components/PlateContainer.js +40 -32
  186. package/dist/react/components/PlateController.js +28 -16
  187. package/dist/react/components/PlateRoot.internal.js +3 -2
  188. package/dist/react/core.js +4 -4
  189. package/dist/react/features/board/BoardSurface.js +116 -87
  190. package/dist/react/features/board/boardClipboard.internal.js +15 -5
  191. package/dist/react/features/board/boardExport.internal.js +470 -0
  192. package/dist/react/features/board/boardGesture.internal.js +6 -3
  193. package/dist/react/features/board/boardIndex.internal.js +7 -18
  194. package/dist/react/features/board/boardPictureStyles.internal.js +415 -0
  195. package/dist/react/features/board/boardPictures.internal.js +19 -13
  196. package/dist/react/features/board/boardView.internal.js +190 -50
  197. package/dist/react/features/board/boardVoice.internal.js +10 -11
  198. package/dist/react/features/board/index.d.ts +108 -18
  199. package/dist/react/features/callout/index.d.ts +3 -2
  200. package/dist/react/features/combobox/index.d.ts +8 -8
  201. package/dist/react/features/comments/index.d.ts +6 -5
  202. package/dist/react/features/date/index.d.ts +4 -3
  203. package/dist/react/features/details/index.d.ts +4 -3
  204. package/dist/react/features/field/FieldPlugin.js +35 -0
  205. package/dist/react/features/field/index.d.ts +315 -0
  206. package/dist/react/features/field/index.js +2 -0
  207. package/dist/react/features/footnote/index.d.ts +7 -6
  208. package/dist/react/features/format-painter/index.d.ts +4 -3
  209. package/dist/react/features/ink/InkContent.js +313 -0
  210. package/dist/react/features/ink/index.d.ts +57 -33
  211. package/dist/react/features/ink/index.js +2 -2
  212. package/dist/react/features/ink/inkCapture.internal.js +39 -17
  213. package/dist/react/features/ink/inkInput.internal.js +1 -1
  214. package/dist/react/features/ink/inkLayout.internal.js +6 -3
  215. package/dist/react/features/ink/inkPaint.internal.js +78 -2
  216. package/dist/react/features/ink/inkPlacement.internal.js +332 -104
  217. package/dist/react/features/ink/inkWatch.internal.js +7 -5
  218. package/dist/react/features/layout/index.d.ts +5 -4
  219. package/dist/react/features/media/index.d.ts +5 -4
  220. package/dist/react/features/mention/index.d.ts +6 -5
  221. package/dist/react/features/resizable/Resizable.js +4 -6
  222. package/dist/react/features/resizable/index.d.ts +5 -5
  223. package/dist/react/features/suggestion/index.d.ts +6 -5
  224. package/dist/react/features/table/TablePlugin.js +4 -11
  225. package/dist/react/features/table/index.d.ts +10 -8
  226. package/dist/react/features/table/useTableResize.js +10 -1
  227. package/dist/react/features/toc/index.d.ts +2 -2
  228. package/dist/react/features/upload/index.d.ts +11 -6
  229. package/dist/react/index.d.ts +6 -6
  230. package/dist/react/index.js +4 -4
  231. package/dist/react/internal/plate-context.js +434 -188
  232. package/dist/react/plite-react.js +2 -2
  233. package/dist/react/plugins/navigation-feedback/NavigationFeedbackPlugin.js +12 -10
  234. package/dist/react/stores/plate/useEditor.js +32 -14
  235. package/dist/react/stores/plate/useEditorHistory.js +55 -0
  236. package/dist/react/stores/plate/useEditorSelector.js +79 -21
  237. package/dist/react/utils/BlockPlaceholderPlugin.js +5 -2
  238. package/dist/react/utils/pipeRenderElement.internal.js +1 -1
  239. package/dist/react/utils/pluginRenderElement.internal.js +1 -1
  240. package/dist/react/virtualized.d.ts +4 -4
  241. package/dist/{source-CNKy99Th.d.ts → source-BGohy8vH.d.ts} +4 -4
  242. package/dist/static/index.d.ts +18 -18
  243. package/dist/static/internal/renderStaticHtmlWithOverrides.js +2 -2
  244. package/dist/static/internal/staticPresentation.js +4 -4
  245. package/dist/static/internal/writeStaticSelectionClipboardData.js +7 -2
  246. package/dist/tabbable/index.d.ts +1 -1
  247. package/dist/tabbable/react/index.d.ts +3 -3
  248. package/dist/testing/index.d.ts +3 -3
  249. package/dist/yjs/index.d.ts +1 -1
  250. package/dist/yjs/react/index.d.ts +6 -6
  251. package/package.json +20 -2
  252. package/dist/markdown/lib/serializer/convertTextsSerialize.js +0 -163
  253. package/dist/react/components/EditorSelectionView.js +0 -40
  254. package/dist/react/features/ink/InkLayer.js +0 -218
  255. package/dist/react/internal/createPlateTargetScope.js +0 -62
@@ -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 };
@@ -1,26 +1,121 @@
1
- import { ElementApi, afterActiveTransactionEnds, getActiveEditorUpdateId, getDocumentValueNode, getEditorRuntimeOwner } from "../../../facade.js";
1
+ import { DocumentChange, ElementApi, afterActiveTransactionEnds, canonicalRecordJson, getActiveEditorUpdateId, getDocumentValueNode, getEditorRuntimeOwner, getRecordSetVersion, readElementById, readRecord, readShownRecords, recordSetChanges } from "../../../facade.js";
2
2
  //#region src/features/ink/lib/inkGone.internal.ts
3
+ const COPIES = "ink-copies";
3
4
  const GONE = "ink-gone";
5
+ const MARKS = "ink-marks";
4
6
  const ADD_LOOKBACK_UPDATES = 4;
5
7
  const sessions = /* @__PURE__ */ new WeakMap();
6
- const goneIn = (records, id) => {
7
- const record = records?.[GONE]?.[id];
8
- return record && {
9
- ...typeof record.next === "string" ? { next: record.next } : {},
10
- ...typeof record.previous === "string" ? { previous: record.previous } : {}
8
+ const sessionOf = (owner) => {
9
+ let session = sessions.get(owner);
10
+ if (!session) {
11
+ session = {
12
+ departed: /* @__PURE__ */ new Map(),
13
+ holdCounts: /* @__PURE__ */ new Map(),
14
+ kept: /* @__PURE__ */ new Map(),
15
+ pending: /* @__PURE__ */ new Map(),
16
+ queued: /* @__PURE__ */ new Map(),
17
+ recent: [],
18
+ runs: 0,
19
+ update: void 0
20
+ };
21
+ sessions.set(owner, session);
22
+ }
23
+ return session;
24
+ };
25
+ const keepHeldChains = ({ holdCounts, kept }, departed, order) => {
26
+ let grew = holdCounts.size > 0;
27
+ while (grew) {
28
+ grew = false;
29
+ const named = new Set([...kept.values()].flatMap(({ place }) => [place.next, place.previous]));
30
+ for (const [id, place] of departed) {
31
+ const saved = kept.get(id);
32
+ if (saved && saved.order >= order) continue;
33
+ if (saved || holdCounts.has(id) || named.has(id)) {
34
+ kept.set(id, {
35
+ order,
36
+ place: gapPlace(...place)
37
+ });
38
+ grew = true;
39
+ }
40
+ }
41
+ }
42
+ };
43
+ const holdInkBlock = (editor, block) => {
44
+ const session = sessionOf(getEditorRuntimeOwner(editor));
45
+ session.holdCounts.set(block, (session.holdCounts.get(block) ?? 0) + 1);
46
+ let released = false;
47
+ return () => {
48
+ if (released) return;
49
+ released = true;
50
+ const count = (session.holdCounts.get(block) ?? 1) - 1;
51
+ if (count > 0) session.holdCounts.set(block, count);
52
+ else session.holdCounts.delete(block);
53
+ if (session.holdCounts.size === 0) session.kept.clear();
11
54
  };
12
55
  };
56
+ const goneOf = (record) => record && {
57
+ ...typeof record.next === "string" ? { next: record.next } : {},
58
+ ...typeof record.previous === "string" ? { previous: record.previous } : {}
59
+ };
60
+ const goneIn = (version, id) => goneOf(readRecord(version, id));
61
+ const NAMING_FIELDS = [[COPIES, ["block"]], [GONE, [
62
+ "id",
63
+ "next",
64
+ "previous"
65
+ ]]];
66
+ const namedBlocks = /* @__PURE__ */ new WeakMap();
67
+ const namedInkBlocks = (editor) => {
68
+ const owner = getEditorRuntimeOwner(editor);
69
+ const store = readShownRecords(editor);
70
+ let named = namedBlocks.get(owner);
71
+ if (!named) {
72
+ named = {
73
+ names: /* @__PURE__ */ new Map(),
74
+ versions: /* @__PURE__ */ new Map()
75
+ };
76
+ namedBlocks.set(owner, named);
77
+ }
78
+ const { names, versions } = named;
79
+ for (const [set, fields] of NAMING_FIELDS) {
80
+ const before = versions.get(set) ?? null;
81
+ const after = getRecordSetVersion(store, set);
82
+ for (const id of recordSetChanges(before, after)) for (const [version, step] of [[before, -1], [after, 1]]) {
83
+ const record = readRecord(version, id);
84
+ for (const field of fields) {
85
+ const block = field === "id" && record ? id : record?.[field];
86
+ if (typeof block !== "string") continue;
87
+ const key = `${field} ${block}`;
88
+ const ids = names.get(key) ?? /* @__PURE__ */ new Set();
89
+ if (step > 0) names.set(key, ids.add(id));
90
+ else if (ids.delete(id) && ids.size === 0) names.delete(key);
91
+ }
92
+ }
93
+ versions.set(set, after);
94
+ }
95
+ return names;
96
+ };
13
97
  const sameGone = (a, b) => a !== void 0 && a.next === b.next && a.previous === b.previous;
14
98
  const idOf = (node) => ElementApi.isElement(node) && typeof node.id === "string" ? node.id : void 0;
15
- const beside = (document, gap, step) => {
99
+ const metrics = {
100
+ besides: 0,
101
+ chains: 0,
102
+ hops: 0,
103
+ reads: 0
104
+ };
105
+ const besideAt = (document, gap, step, skips) => {
106
+ metrics.besides += 1;
16
107
  const parent = gap.slice(0, -1);
17
108
  let [index] = gap.slice(-1);
18
109
  if (step === -1) index -= 1;
19
110
  while (true) {
20
- const node = index >= 0 ? getDocumentValueNode(document, [...parent, index]) : null;
111
+ const path = [...parent, index];
112
+ const node = index >= 0 ? getDocumentValueNode(document, path) : null;
21
113
  if (node) {
22
114
  const id = idOf(node);
23
- if (id) return id;
115
+ if (id && !skips?.(path)) return {
116
+ id,
117
+ path
118
+ };
24
119
  index += step;
25
120
  } else if (parent.length > 0) {
26
121
  const [up] = parent.splice(-1);
@@ -28,25 +123,67 @@ const beside = (document, gap, step) => {
28
123
  } else return;
29
124
  }
30
125
  };
126
+ const beside = (document, gap, step) => besideAt(document, gap, step)?.id;
127
+ const goneFrom = (previous, next) => ({
128
+ ...next ? { next } : {},
129
+ ...previous ? { previous } : {}
130
+ });
131
+ const gapPlace = (after, gap) => goneFrom(beside(after, gap, -1), beside(after, gap, 1));
132
+ const blockAbove = (document, gap) => beside(document, gap, -1);
133
+ const reachGone = (walk, from, side, memo, count) => {
134
+ const seen = /* @__PURE__ */ new Set([from]);
135
+ let last = from;
136
+ let found;
137
+ count.chains += 1;
138
+ while (true) {
139
+ const to = memo?.[side].get(last);
140
+ if (to !== void 0 && !seen.has(to)) {
141
+ last = to;
142
+ seen.add(to);
143
+ }
144
+ const at = walk.record(last)?.[side];
145
+ if (at === void 0 || seen.has(at)) break;
146
+ count.hops += 1;
147
+ const holder = walk.holder(at);
148
+ if (holder !== void 0) {
149
+ found = {
150
+ holder,
151
+ id: at,
152
+ side
153
+ };
154
+ break;
155
+ }
156
+ if (!walk.record(at)) break;
157
+ last = at;
158
+ seen.add(at);
159
+ }
160
+ if (memo && "walked" in memo) for (const block of seen) {
161
+ memo.walked.add(block);
162
+ if (block !== last) memo[side].set(block, last);
163
+ }
164
+ return {
165
+ found,
166
+ last
167
+ };
168
+ };
169
+ const walkGone = (walk, id, memo, count) => {
170
+ const { found: above, last } = reachGone(walk, id, "previous", memo, count);
171
+ const next = (from) => reachGone(walk, from, "next", memo, count).found;
172
+ const belowTop = () => next(last);
173
+ const belowBlock = () => last === id ? void 0 : next(id);
174
+ return {
175
+ found: above ?? belowTop() ?? belowBlock(),
176
+ last
177
+ };
178
+ };
31
179
  const currentSession = (editor) => {
32
180
  const session = sessions.get(getEditorRuntimeOwner(editor));
33
181
  return session?.update?.deref() === getActiveEditorUpdateId(editor) ? session : void 0;
34
182
  };
35
- const captureGone = (editor, tags, after, { gap, id }) => {
183
+ const captureGone = (editor, tx, after, [gap, id], memo) => {
36
184
  const update = getActiveEditorUpdateId(editor);
37
185
  if (!update) return;
38
- const owner = getEditorRuntimeOwner(editor);
39
- let session = sessions.get(owner);
40
- if (!session) {
41
- session = {
42
- departed: /* @__PURE__ */ new Map(),
43
- pending: /* @__PURE__ */ new Set(),
44
- recent: [],
45
- runs: 0,
46
- update: void 0
47
- };
48
- sessions.set(owner, session);
49
- }
186
+ const session = sessionOf(getEditorRuntimeOwner(editor));
50
187
  if (session.update?.deref() !== update) {
51
188
  const departed = /* @__PURE__ */ new Map();
52
189
  session.runs += 1;
@@ -54,7 +191,8 @@ const captureGone = (editor, tags, after, { gap, id }) => {
54
191
  const { recent } = session;
55
192
  session.update = new WeakRef(update);
56
193
  session.departed = departed;
57
- session.pending = /* @__PURE__ */ new Set();
194
+ session.pending = /* @__PURE__ */ new Map();
195
+ session.queued = /* @__PURE__ */ new Map();
58
196
  afterActiveTransactionEnds(editor, () => {
59
197
  const at = recent.findIndex((run) => run.order > order);
60
198
  recent.splice(at === -1 ? recent.length : at, 0, {
@@ -62,46 +200,89 @@ const captureGone = (editor, tags, after, { gap, id }) => {
62
200
  order
63
201
  });
64
202
  if (recent.length > ADD_LOOKBACK_UPDATES) recent.shift();
65
- });
203
+ keepHeldChains(session, departed, order);
204
+ }, { beforeListeners: true });
66
205
  }
67
- const next = beside(after, gap, 1);
68
- const previous = beside(after, gap, -1);
69
- session.departed.set(id, {
70
- ...next ? { next } : {},
71
- ...previous ? { previous } : {}
72
- });
73
- if (!tags.has("collaboration")) session.pending.add(id);
206
+ session.departed.set(id, [after, gap]);
207
+ if (tx.tags.has("collaboration")) return;
208
+ const names = namedInkBlocks(editor);
209
+ const { pending, queued } = session;
210
+ const walk = {
211
+ holder: (block) => readElementById(tx.nodes, block) ?? void 0,
212
+ record: (block) => pending.get(block) ?? savedGone(editor, block)
213
+ };
214
+ const gone = (block) => !walk.holder(block);
215
+ const named = (side) => [...new Set([names, queued].flatMap((map) => [...map.get(`${side} ${id}`) ?? []]))].filter((block) => gone(block) && (!pending.has(block) || pending.get(block)?.[side] === id));
216
+ const asNext = named("next");
217
+ const namers = named("previous");
218
+ const watched = names.has(`block ${id}`) || session.holdCounts.has(id) || names.has(`id ${id}`) || pending.has(id);
219
+ if (!watched && asNext.length + namers.length === 0) return;
220
+ const place = gapPlace(after, gap);
221
+ const atGap = (block) => {
222
+ const { found } = walkGone(walk, block, memo, metrics);
223
+ return found ? found.id === place[found.side] : place.previous === void 0 && place.next === void 0;
224
+ };
225
+ const saved = walk.record(id);
226
+ const was = saved?.previous;
227
+ if (watched ? was !== void 0 && (was === place.previous || gone(was) && walk.record(was) !== void 0 && atGap(was)) && (asNext.length === 0 || saved?.next === place.next || reachGone(walk, id, "next", memo, metrics).found?.id === place.next) : [...asNext, ...namers].every(atGap)) return;
228
+ pending.set(id, place);
229
+ for (const key of Object.entries(place).map((pair) => pair.join(" "))) queued.set(key, (queued.get(key) ?? /* @__PURE__ */ new Set()).add(id));
74
230
  };
75
231
  const hasPendingGone = (editor) => !!currentSession(editor)?.pending.size;
76
- const writeGone = (tx, ids, placeOf, writes) => {
77
- const { records } = tx.value();
232
+ const savedGone = (editor, id) => {
233
+ metrics.reads += 1;
234
+ return goneIn(getRecordSetVersion(readShownRecords(editor), GONE), id);
235
+ };
236
+ const writeGone = (editor, tx, ids, placeOf, writes) => {
78
237
  const queue = [...ids];
79
238
  const done = /* @__PURE__ */ new Set();
239
+ const texts = /* @__PURE__ */ new Map();
80
240
  for (const id of queue) {
81
241
  if (done.has(id)) continue;
82
242
  done.add(id);
83
243
  const place = placeOf(id);
84
- if (!place || tx.unique.holder("id", id)) continue;
85
- const saved = goneIn(records, id);
86
- if (!writes(saved, place)) continue;
87
- if (saved) tx.records.drop(GONE, id);
88
- tx.records.put(GONE, id, place);
244
+ if (!place) continue;
245
+ if (!writes(savedGone(editor, id), place)) continue;
246
+ texts.set(id, canonicalRecordJson(place));
89
247
  for (const neighbor of [place.next, place.previous]) if (neighbor !== void 0 && !done.has(neighbor)) queue.push(neighbor);
90
248
  }
249
+ if (texts.size === 0) return;
250
+ tx.changes.apply(DocumentChange.fromJSON({
251
+ records: { [GONE]: Object.fromEntries(texts) },
252
+ version: 3
253
+ }));
91
254
  };
92
- const flushGone = (editor, tx, named) => {
255
+ const flushGone = (editor, tx) => {
93
256
  const session = currentSession(editor);
94
257
  if (!session) return;
95
- const { records } = tx.value();
96
- const ids = [...session.pending].filter((id) => named(id) || goneIn(records, id) !== void 0);
97
- const { departed } = session;
98
- session.pending = /* @__PURE__ */ new Set();
99
- writeGone(tx, ids, (id) => departed.get(id), (saved, place) => !sameGone(saved, place));
258
+ const { pending } = session;
259
+ session.pending = /* @__PURE__ */ new Map();
260
+ writeGone(editor, tx, pending.keys(), (id) => readElementById(tx.nodes, id) ? void 0 : pending.get(id), (saved, place) => !sameGone(saved, place));
100
261
  };
101
- const addGone = (editor, tx, block) => {
262
+ const addGone = (editor, tx, copies) => {
263
+ const blocks = copies.map(({ record }) => record.block);
102
264
  const current = currentSession(editor)?.departed;
103
- const recent = sessions.get(getEditorRuntimeOwner(editor))?.recent ?? [];
104
- writeGone(tx, [block], (id) => current?.get(id) ?? recent.findLast((run) => run.departed.has(id))?.departed.get(id), (saved) => saved === void 0);
265
+ const session = sessions.get(getEditorRuntimeOwner(editor));
266
+ const recent = session?.recent ?? [];
267
+ const standing = /* @__PURE__ */ new Map();
268
+ const stand = (id) => {
269
+ const holder = readElementById(tx.nodes, id);
270
+ if (!holder || holder.root !== void 0 || standing.has(id)) return;
271
+ const { path } = holder;
272
+ const value = tx.value();
273
+ const past = [...path.slice(0, -1), (path.at(-1) ?? 0) + 1];
274
+ standing.set(id, goneFrom(beside(value, path, -1), beside(value, past, 1)));
275
+ };
276
+ for (const block of blocks) {
277
+ stand(block);
278
+ for (const side of Object.values(standing.get(block) ?? {})) stand(side);
279
+ }
280
+ writeGone(editor, tx, standing.keys(), (id) => standing.get(id), (saved, place) => !sameGone(saved, place));
281
+ writeGone(editor, tx, blocks, (id) => {
282
+ if (readElementById(tx.nodes, id)) return void 0;
283
+ const departure = current?.get(id) ?? (session?.kept.has(id) ? void 0 : recent.findLast((run) => run.departed.has(id))?.departed.get(id));
284
+ return departure ? gapPlace(...departure) : session?.kept.get(id)?.place;
285
+ }, (saved) => saved === void 0);
105
286
  };
106
287
  //#endregion
107
- export { GONE, addGone, captureGone, flushGone, goneIn, hasPendingGone, idOf };
288
+ export { COPIES, GONE, MARKS, addGone, besideAt, blockAbove, captureGone, flushGone, goneIn, hasPendingGone, holdInkBlock, idOf, namedInkBlocks, reachGone, walkGone };