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,26 +1,121 @@
1
- import { ElementApi, afterActiveTransactionEnds, getActiveEditorUpdateId, getDocumentValueNode, getEditorRuntimeOwner } from "../../../facade.js";
1
+ import { DocumentChange, ElementApi, afterActiveTransactionEnds, canonicalRecordJson, getActiveEditorUpdateId, getDocumentValueNode, getEditorRuntimeOwner, getRecordSetVersion, 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) => tx.unique.holder("id", 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) => tx.unique.holder("id", 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 = tx.unique.holder("id", id);
270
+ if (holder?.root !== "main" || 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 (tx.unique.holder("id", 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 };