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,4 +1,4 @@
1
- import { strokePolygon } from "../../internal/penStroke.internal.js";
1
+ import { strokePolygon } from "../../../internal/penStroke.internal.js";
2
2
  //#region src/react/features/ink/inkPaint.internal.ts
3
3
  const INK_THINNING = .25;
4
4
  /**
@@ -13,5 +13,81 @@ const inkOutline = (samples, brush, { dx = 0, dy = 0, last = true } = {}) => str
13
13
  size: brush.size / 1.25,
14
14
  thinning: INK_THINNING
15
15
  }, last);
16
+ const HALO = [
17
+ "data-picked",
18
+ "stroke",
19
+ "stroke-opacity",
20
+ "stroke-width"
21
+ ];
22
+ const createInkSurface = (into) => {
23
+ const outlines = /* @__PURE__ */ new WeakMap();
24
+ const paths = /* @__PURE__ */ new Map();
25
+ let hidden = false;
26
+ let ink = [];
27
+ let picked = [];
28
+ const outlineOf = (mark) => {
29
+ let outline = outlines.get(mark);
30
+ if (outline === void 0) {
31
+ outline = inkOutline(mark.samples, mark.brush);
32
+ outlines.set(mark, outline);
33
+ }
34
+ return outline;
35
+ };
36
+ const draw = () => {
37
+ const shown = [];
38
+ const left = new Set(paths.keys());
39
+ for (const { dx, dy, mark } of hidden ? [] : ink) {
40
+ const { color, opacity, size } = mark.brush;
41
+ const halo = picked.includes(mark.id);
42
+ const kept = paths.get(mark.id);
43
+ const inked = kept?.mark !== mark;
44
+ const moved = kept?.dx !== dx || kept?.dy !== dy;
45
+ const path = kept?.path ?? into.ownerDocument.createElementNS("http://www.w3.org/2000/svg", "path");
46
+ if (!kept) path.setAttribute("data-ink-mark", mark.id);
47
+ if (inked) {
48
+ path.setAttribute("d", outlineOf(mark));
49
+ path.setAttribute("fill", color);
50
+ path.setAttribute("opacity", String(opacity));
51
+ }
52
+ if (halo && (inked || !kept?.halo)) {
53
+ path.setAttribute("data-picked", "true");
54
+ path.setAttribute("stroke", color);
55
+ path.setAttribute("stroke-opacity", "0.35");
56
+ path.setAttribute("stroke-width", String(size * 2));
57
+ } else if (!halo && kept?.halo) for (const name of HALO) path.removeAttribute(name);
58
+ if (moved) path.setAttribute("transform", `translate(${dx} ${dy})`);
59
+ if (inked || moved || kept?.halo !== halo) paths.set(mark.id, {
60
+ dx,
61
+ dy,
62
+ halo,
63
+ mark,
64
+ path
65
+ });
66
+ left.delete(mark.id);
67
+ shown.push(path);
68
+ }
69
+ for (const id of left) {
70
+ paths.get(id)?.path.remove();
71
+ paths.delete(id);
72
+ }
73
+ let previous = null;
74
+ for (const path of shown) {
75
+ if (path.parentNode !== into || path.previousSibling !== previous) if (previous) previous.after(path);
76
+ else into.prepend(path);
77
+ previous = path;
78
+ }
79
+ };
80
+ return {
81
+ paint: (placed) => {
82
+ ink = placed;
83
+ draw();
84
+ },
85
+ setLook: (nextHidden, nextPicked) => {
86
+ hidden = nextHidden;
87
+ picked = nextPicked;
88
+ draw();
89
+ }
90
+ };
91
+ };
16
92
  //#endregion
17
- export { inkOutline };
93
+ export { createInkSurface, inkOutline };
@@ -1,21 +1,26 @@
1
+ import { blockAbove } from "../../../features/ink/lib/inkGone.internal.js";
1
2
  import { placeMark } from "../../../features/ink/lib/inkMatch.internal.js";
2
- import { InkPlugin } from "./InkPlugin.js";
3
- import { inkBoxOf, inkTextBlockOf, inkWordBoxes } from "./inkMeasure.internal.js";
3
+ import { byMark, readInkDeletedSigns, readInkIndexedMark, readInkSign } from "../../../features/ink/lib/inkPlaces.internal.js";
4
+ import { inkBoxOf, inkTextBlockOf, inkWordBoxes, sameInkBox } from "./inkMeasure.internal.js";
4
5
  import { createInkLayouts } from "./inkLayout.internal.js";
6
+ import { InkPlugin } from "./InkPlugin.js";
5
7
  //#region src/react/features/ink/inkPlacement.internal.ts
6
8
  const NOTHING_PLACED = {
7
9
  bottom: 0,
10
+ contentOrigin: {
11
+ left: 0,
12
+ top: 0
13
+ },
8
14
  ink: [],
9
15
  signs: []
10
16
  };
11
- const DELETED = {
12
- reason: "deleted",
13
- type: "sign"
14
- };
15
- const WORDS_CHANGED = {
16
- reason: "changed",
17
- type: "sign"
17
+ const metrics = {
18
+ arrivalLookups: 0,
19
+ decided: 0,
20
+ shownIds: 0,
21
+ signReads: 0
18
22
  };
23
+ const crosses = (box, { from, to }) => box.top <= to && box.top + box.height >= from;
19
24
  const inkInputs = (mark, copies, places, measure, candidatesBetween) => {
20
25
  const live = places.map((place) => place && place.kind !== "gone" ? measure(place.key) : void 0);
21
26
  const at = live.findIndex((block) => block !== void 0);
@@ -23,25 +28,23 @@ const inkInputs = (mark, copies, places, measure, candidatesBetween) => {
23
28
  const reach = mark.brush.size / 2 + Math.max(0, ...copies.flatMap((copy) => copy.lines.map((line) => line.height)));
24
29
  const shift = first ? first.box.top - mark.blocks[at].top : 0;
25
30
  const ys = mark.samples.map(([, y]) => y + shift);
26
- const from = Math.min(...ys) - reach;
27
- const to = Math.max(...ys) + reach;
31
+ const band = {
32
+ from: Math.min(...ys) - reach,
33
+ to: Math.max(...ys) + reach
34
+ };
28
35
  const marked = new Set(places.flatMap((place) => place && place.kind !== "gone" ? [place.key] : []));
29
- const near = candidatesBetween(from, to).flatMap(({ box, key }) => {
30
- if (marked.has(key) || box.top > to || box.top + box.height < from) return [];
31
- return measure(key) ?? [];
32
- });
36
+ const crossing = candidatesBetween(band.from, band.to).filter(({ box }) => crosses(box, band));
37
+ const others = crossing.flatMap(({ key }) => marked.has(key) ? [] : [key]);
33
38
  return {
34
- band: {
35
- from,
36
- to
37
- },
39
+ band,
40
+ crossing: crossing.length,
38
41
  first,
42
+ keys: [...marked, ...others],
39
43
  live,
40
- near
44
+ near: others.flatMap((key) => measure(key) ?? [])
41
45
  };
42
46
  };
43
- const placeInkMarks = (view, editable, origin, marks, decide) => {
44
- const { read } = view.plugin(InkPlugin);
47
+ const placeInkMarks = (view, editable, origin, marks, deleted, entryOf, signOf, decide) => {
45
48
  const editableBox = inkBoxOf(editable, origin);
46
49
  const style = getComputedStyle(editable);
47
50
  const rails = {
@@ -49,6 +52,22 @@ const placeInkMarks = (view, editable, origin, marks, decide) => {
49
52
  rtl: editableBox.left
50
53
  };
51
54
  const startTop = editableBox.top + (Number.parseFloat(style.paddingTop) || 0);
55
+ const pins = /* @__PURE__ */ new Map();
56
+ const pinAt = (element) => {
57
+ let pin = pins.get(element);
58
+ if (!pin) {
59
+ const { left, top } = inkBoxOf(element, origin);
60
+ pin = {
61
+ at: {
62
+ left,
63
+ top
64
+ },
65
+ element
66
+ };
67
+ pins.set(element, pin);
68
+ }
69
+ return pin;
70
+ };
52
71
  const anchors = /* @__PURE__ */ new Map();
53
72
  const anchorBox = (key) => {
54
73
  if (!anchors.has(key)) {
@@ -57,6 +76,15 @@ const placeInkMarks = (view, editable, origin, marks, decide) => {
57
76
  }
58
77
  return anchors.get(key);
59
78
  };
79
+ const above = /* @__PURE__ */ new Map();
80
+ const aboveOf = (key) => {
81
+ if (!above.has(key)) above.set(key, view.read((state) => {
82
+ const [, path] = state.nodes.get(key) ?? [];
83
+ const id = path && blockAbove(state.value(), path);
84
+ return id === void 0 ? void 0 : state.unique.holder("id", id)?.key;
85
+ }));
86
+ return above.get(key);
87
+ };
60
88
  const anchorOf = (place) => {
61
89
  if (place?.kind === "live" || place?.kind === "changed") return {
62
90
  id: place.key,
@@ -67,29 +95,43 @@ const placeInkMarks = (view, editable, origin, marks, decide) => {
67
95
  id: "start",
68
96
  top: startTop
69
97
  };
70
- const box = anchorBox(beside.key);
98
+ const upper = beside.at === "before" ? aboveOf(beside.key) : void 0;
99
+ const { at, key } = upper === void 0 ? beside : {
100
+ at: "after",
101
+ key: upper
102
+ };
103
+ const box = anchorBox(key);
71
104
  return {
72
- id: `${beside.at}:${beside.key}`,
73
- top: box ? box.top + (beside.at === "after" ? box.height : 0) : startTop
105
+ id: `${at}:${key}`,
106
+ top: box ? box.top + (at === "after" ? box.height : 0) : startTop
74
107
  };
75
108
  };
76
109
  const ink = [];
77
110
  const signs = /* @__PURE__ */ new Map();
78
111
  for (const mark of marks) {
79
- const copies = mark.blocks.flatMap((block) => read.copy(block.copy) ?? []);
80
- if (copies.length !== mark.blocks.length) continue;
81
- const places = copies.map((copy) => read.place(copy.id));
82
- const placement = places.some((place) => place?.kind === "gone") ? DELETED : decide(mark, copies, places);
83
- if (placement.type === "ink") ink.push({
84
- dx: placement.dx,
85
- dy: placement.dy,
86
- mark
87
- });
88
- const state = placement.type === "sign" ? placement.reason : placement.sign;
112
+ const entry = entryOf(mark);
113
+ if (!entry) continue;
114
+ const kept = signOf(mark);
115
+ let state = kept?.state;
116
+ let place = kept?.place;
117
+ if (!kept) {
118
+ const decided = decide(mark, entry);
119
+ if (!decided) continue;
120
+ const { pin, placement } = decided;
121
+ if (placement.type === "ink") ink.push({
122
+ dx: placement.dx,
123
+ dy: placement.dy,
124
+ mark,
125
+ ...pin && { pin: pinAt(pin) }
126
+ });
127
+ state = placement.type === "sign" ? placement.reason : placement.sign;
128
+ ({place} = decided);
129
+ }
89
130
  if (!state) continue;
90
- const anchor = anchorOf(places[Math.max(0, places.findIndex((place) => place?.kind !== "live"))]);
131
+ const anchor = anchorOf(place);
91
132
  const id = `${mark.direction}|${anchor.id}|${state}`;
92
133
  const sign = signs.get(id) ?? {
134
+ first: mark,
93
135
  left: rails[mark.direction],
94
136
  marks: [],
95
137
  state,
@@ -98,16 +140,46 @@ const placeInkMarks = (view, editable, origin, marks, decide) => {
98
140
  sign.marks.push(mark.id);
99
141
  signs.set(id, sign);
100
142
  }
143
+ const gone = /* @__PURE__ */ new Map();
144
+ for (const { ids, marks: of, place } of deleted) {
145
+ const anchor = anchorOf(place);
146
+ const id = `${of[0].direction}|${anchor.id}|deleted`;
147
+ const there = gone.get(id);
148
+ const merged = there && [...there.marks, ...of].sort(byMark);
149
+ gone.set(id, {
150
+ ids: merged ? merged.map((mark) => mark.id) : ids,
151
+ marks: merged ?? of,
152
+ top: anchor.top
153
+ });
154
+ }
101
155
  return {
102
156
  bottom: editable.offsetTop + editable.offsetHeight,
157
+ contentOrigin: {
158
+ left: editableBox.left - editable.scrollLeft,
159
+ top: editableBox.top - editable.scrollTop
160
+ },
103
161
  ink,
104
- signs: [...signs].map(([id, { marks: ofSign, state, ...place }]) => ({
105
- ...place,
162
+ signs: [...[...signs].map(([id, { marks: ofSign, state, ...at }]) => ({
163
+ ...at,
106
164
  id,
107
165
  sign: {
108
166
  marks: ofSign,
109
167
  state
110
168
  }
169
+ })), ...[...gone].map(([id, { ids, marks: of, top }]) => ({
170
+ first: of[0],
171
+ id,
172
+ left: rails[of[0].direction],
173
+ sign: {
174
+ marks: ids,
175
+ state: "deleted"
176
+ },
177
+ top
178
+ }))].sort((a, b) => byMark(a.first, b.first)).map(({ id, left, sign, top }) => ({
179
+ id,
180
+ left,
181
+ sign,
182
+ top
111
183
  }))
112
184
  };
113
185
  };
@@ -131,20 +203,29 @@ const createInkPlacer = ({ editable, layer, view }) => {
131
203
  const layouts = createInkLayouts();
132
204
  const lowestOf = /* @__PURE__ */ new WeakMap();
133
205
  const textBlocks = /* @__PURE__ */ new WeakMap();
134
- const held = /* @__PURE__ */ new WeakMap();
135
- const ids = /* @__PURE__ */ new WeakMap();
136
- let nextId = 0;
206
+ const held = /* @__PURE__ */ new Map();
137
207
  let index = null;
138
- let gone = null;
139
- const idOf = (value) => {
140
- let id = ids.get(value);
141
- if (id === void 0) {
142
- id = nextId;
143
- nextId += 1;
144
- ids.set(value, id);
145
- }
146
- return id;
208
+ let heading = 1;
209
+ let contentTop;
210
+ const entryOf = (mark) => view.read((state) => readInkIndexedMark({
211
+ copy: read.copy,
212
+ editor: view,
213
+ state
214
+ }, mark));
215
+ const signOf = (mark) => {
216
+ metrics.signReads += 1;
217
+ return view.read((state) => readInkSign({
218
+ copy: read.copy,
219
+ editor: view,
220
+ state
221
+ }, mark));
147
222
  };
223
+ const deletedSigns = () => view.read((state) => readInkDeletedSigns({
224
+ copy: read.copy,
225
+ editor: view,
226
+ marks: read.marks,
227
+ state
228
+ }));
148
229
  const textBlock = (key) => {
149
230
  const node = view.read.nodes.get(key)?.[0];
150
231
  if (!node) return void 0;
@@ -169,8 +250,10 @@ const createInkPlacer = ({ editable, layer, view }) => {
169
250
  const indexed = (marks) => {
170
251
  if (index?.marks === marks) return index;
171
252
  const byBlock = /* @__PURE__ */ new Map();
253
+ const saved = new Set(marks.map((mark) => mark.id));
172
254
  let reachAbove = 0;
173
255
  let reachBelow = 0;
256
+ for (const id of held.keys()) if (!saved.has(id)) held.delete(id);
174
257
  for (const mark of marks) {
175
258
  for (const block of mark.blocks) {
176
259
  const copy = read.copy(block.copy);
@@ -192,16 +275,11 @@ const createInkPlacer = ({ editable, layer, view }) => {
192
275
  reachAbove,
193
276
  reachBelow
194
277
  };
195
- gone = null;
196
278
  return index;
197
279
  };
198
- const placeWithin = (span) => {
280
+ const openPass = (span, fresh) => {
199
281
  const marks = read.marks();
200
- if (marks.length === 0) return {
201
- bottom: editable.offsetTop + editable.offsetHeight,
202
- ink: [],
203
- signs: []
204
- };
282
+ if (marks.length === 0) return void 0;
205
283
  const rect = layer.getBoundingClientRect();
206
284
  const origin = {
207
285
  left: rect.left,
@@ -236,19 +314,22 @@ const createInkPlacer = ({ editable, layer, view }) => {
236
314
  const blocks = blocksBetween(viewportLow, viewportHigh);
237
315
  const byKey = new Map(blocks.map((block) => [block.key, block]));
238
316
  const measured = /* @__PURE__ */ new Map();
239
- const resolve = (key) => {
240
- const element = view.api.dom.resolveDOMNode(key);
241
- const found = textBlock(key);
242
- return element && found ? {
243
- box: inkBoxOf(element, origin),
244
- element,
245
- text: found.text
246
- } : void 0;
317
+ const blockAt = (key) => {
318
+ if (!byKey.has(key)) {
319
+ const element = view.api.dom.resolveDOMNode(key);
320
+ const found = textBlock(key);
321
+ byKey.set(key, element && found ? {
322
+ box: inkBoxOf(element, origin),
323
+ element,
324
+ text: found.text
325
+ } : void 0);
326
+ }
327
+ return byKey.get(key);
247
328
  };
248
329
  const measure = (key) => {
249
330
  if (!measured.has(key)) {
250
- const block = byKey.get(key) ?? resolve(key);
251
- measured.set(key, block && layouts.measure(block.element, block.text, block.box, origin));
331
+ const block = blockAt(key);
332
+ measured.set(key, block && layouts.measure(block.element, block.text, block.box, origin, fresh));
252
333
  }
253
334
  return measured.get(key);
254
335
  };
@@ -258,30 +339,51 @@ const createInkPlacer = ({ editable, layer, view }) => {
258
339
  for (const block of band) if (!byKey.has(block.key)) byKey.set(block.key, block);
259
340
  return band;
260
341
  };
261
- const decide = (mark, copies, places) => {
262
- if (!mark.margin && places.some((place) => place?.kind === "changed")) return WORDS_CHANGED;
263
- const inputs = inkInputs(mark, copies, places, measure, candidatesBetween);
264
- const top = inputs.first?.box.top ?? 0;
265
- const part = (block) => block ? [
266
- idOf(block.lines),
267
- block.box.left,
268
- block.box.top - top
269
- ].join(":") : "-";
270
- const key = [
271
- rect.width,
272
- inputs.live.map(part).join(","),
273
- inputs.near.map(part).join(",")
274
- ].join("|");
275
- const kept = held.get(mark);
276
- if (kept?.key === key) {
277
- const { placement } = kept;
278
- if (placement.type !== "ink") return placement;
279
- return {
280
- ...placement,
281
- dy: placement.dy + top - kept.top
282
- };
342
+ const pinsOf = /* @__PURE__ */ new Map();
343
+ const pinOf = (element) => {
344
+ if (!pinsOf.has(element)) {
345
+ let pin = element;
346
+ while (pin && pin !== editable && !/sticky|fixed/.test(getComputedStyle(pin).position)) pin = pin.parentElement;
347
+ pinsOf.set(element, pin && pin !== editable ? pin : void 0);
283
348
  }
284
- const { band, first, live, near } = inputs;
349
+ return pinsOf.get(element);
350
+ };
351
+ const candidatesMovingWith = (pin) => pin ? (from, to) => candidatesBetween(from, to).filter(({ element }) => pinOf(element) === pin) : candidatesBetween;
352
+ const stillHeld = (mark, entry) => {
353
+ const kept = held.get(mark.id);
354
+ if (kept?.entry !== entry || kept.width !== rect.width) return;
355
+ const top = blockAt(kept.firstKey)?.box.top;
356
+ if (top === void 0) return void 0;
357
+ const band = {
358
+ from: kept.bandFromFirst.from + top,
359
+ to: kept.bandFromFirst.to + top
360
+ };
361
+ if (candidatesMovingWith(kept.pin)(band.from, band.to).filter(({ box }) => crosses(box, band)).length !== kept.crossing || !kept.witnesses.every(({ boxFromFirst, key, lines }) => {
362
+ const now = blockAt(key);
363
+ return now !== void 0 && layouts.lines(now.element) === lines && sameInkBox({
364
+ ...now.box,
365
+ top: now.box.top - top
366
+ }, boxFromFirst);
367
+ })) return;
368
+ const { pin, place, placement } = kept;
369
+ return {
370
+ pin,
371
+ place,
372
+ placement: placement.type === "ink" ? {
373
+ ...placement,
374
+ dy: placement.dy + top - kept.firstTop
375
+ } : placement
376
+ };
377
+ };
378
+ const decide = (mark, entry) => {
379
+ const kept = fresh ? void 0 : stillHeld(mark, entry);
380
+ if (kept) return kept;
381
+ const { copies } = entry;
382
+ const places = copies.map((copy) => read.place(copy.id));
383
+ const [start] = places.flatMap((at) => at && at.kind !== "gone" ? [at.key] : []);
384
+ const anchor = start === void 0 ? void 0 : blockAt(start)?.element;
385
+ const pin = anchor && pinOf(anchor);
386
+ const { band, crossing, first, keys, live, near } = inkInputs(mark, copies, places, measure, candidatesMovingWith(pin));
285
387
  const placement = placeMark({
286
388
  copies,
287
389
  live,
@@ -295,22 +397,150 @@ const createInkPlacer = ({ editable, layer, view }) => {
295
397
  mark,
296
398
  nearby: near.flatMap((block) => inkWordBoxes(block, band))
297
399
  });
298
- held.set(mark, {
299
- key,
400
+ const place = places.find((at) => at?.kind !== "live") ?? places[0];
401
+ const firstTop = first?.box.top ?? 0;
402
+ const witnesses = keys.flatMap((key) => {
403
+ const block = blockAt(key);
404
+ const lines = measure(key)?.lines;
405
+ return block && lines ? [{
406
+ boxFromFirst: {
407
+ ...block.box,
408
+ top: block.box.top - firstTop
409
+ },
410
+ key,
411
+ lines
412
+ }] : [];
413
+ });
414
+ const everyBlockMeasured = live[0] !== void 0 && witnesses.length === keys.length;
415
+ metrics.decided += 1;
416
+ if (everyBlockMeasured) held.set(mark.id, {
417
+ bandFromFirst: {
418
+ from: band.from - firstTop,
419
+ to: band.to - firstTop
420
+ },
421
+ crossing,
422
+ entry,
423
+ firstKey: keys[0],
424
+ firstTop,
425
+ pin,
426
+ place,
300
427
  placement,
301
- top
428
+ width: rect.width,
429
+ witnesses
302
430
  });
303
- return placement;
431
+ else held.delete(mark.id);
432
+ return {
433
+ pin,
434
+ place,
435
+ placement
436
+ };
437
+ };
438
+ return {
439
+ blocks,
440
+ decide,
441
+ deleted: deletedSigns(),
442
+ marksOn: (block) => markIndex.byBlock.get(block) ?? [],
443
+ origin,
444
+ shown: [...new Set(blocks.flatMap((block) => markIndex.byBlock.get(block.id) ?? []))].filter((mark) => signOf(mark)?.state !== "deleted").sort(byMark),
445
+ stillHeld
446
+ };
447
+ };
448
+ const placeWithin = (span) => {
449
+ const pass = openPass(span, false);
450
+ return pass ? placeInkMarks(view, editable, pass.origin, pass.shown, pass.deleted, entryOf, signOf, pass.decide) : {
451
+ ...NOTHING_PLACED,
452
+ bottom: editable.offsetTop + editable.offsetHeight
304
453
  };
305
- gone ??= marks.filter((mark) => mark.blocks.some((block) => {
306
- const copy = read.copy(block.copy);
307
- return copy && read.place(copy.id)?.kind === "gone";
308
- }));
309
- const shown = new Set(gone);
310
- for (const block of blocks) for (const mark of markIndex.byBlock.get(block.id) ?? []) shown.add(mark);
311
- return placeInkMarks(view, editable, origin, marks.filter((mark) => shown.has(mark)), decide);
312
454
  };
313
455
  return {
456
+ arrive: (shown, expired) => {
457
+ const pass = openPass(nearView, false);
458
+ if (!pass) return {
459
+ placed: placeWithin(nearView),
460
+ undecided: void 0
461
+ };
462
+ const screen = layer.ownerDocument.defaultView?.innerHeight ?? 0;
463
+ const ranks = /* @__PURE__ */ new Map();
464
+ for (const { box, id } of pass.blocks) {
465
+ const top = box.top + pass.origin.top;
466
+ const ahead = top >= screen === heading > 0 ? 1 : 2;
467
+ const rank = top < screen && top + box.height > 0 ? 0 : ahead;
468
+ for (const mark of pass.marksOn(id)) ranks.set(mark, Math.min(rank, ranks.get(mark) ?? rank));
469
+ }
470
+ const showing = new Set(pass.deleted.map(({ ids }) => ids));
471
+ let before;
472
+ const shownBefore = (id) => {
473
+ metrics.arrivalLookups += 1;
474
+ if (!before) {
475
+ before = /* @__PURE__ */ new Set([...shown.ink.map(({ mark }) => mark.id), ...shown.signs.flatMap(({ sign }) => showing.has(sign.marks) ? [] : sign.marks)]);
476
+ metrics.shownIds += before.size;
477
+ }
478
+ return before.has(id);
479
+ };
480
+ const decided = /* @__PURE__ */ new Map();
481
+ let undecided;
482
+ let arrivalsDecided = 0;
483
+ for (const [mark, rank] of [...ranks].sort(([, a], [, b]) => a - b)) {
484
+ const entry = entryOf(mark);
485
+ if (!entry || signOf(mark)) continue;
486
+ const kept = pass.stillHeld(mark, entry);
487
+ if (!kept && !shownBefore(mark.id)) {
488
+ if (undecided || arrivalsDecided > 0 && expired(rank === 0)) {
489
+ undecided ??= rank === 0 ? "screen" : "band";
490
+ continue;
491
+ }
492
+ arrivalsDecided += 1;
493
+ }
494
+ decided.set(mark, kept ?? pass.decide(mark, entry));
495
+ }
496
+ return {
497
+ placed: placeInkMarks(view, editable, pass.origin, pass.shown, pass.deleted, entryOf, signOf, (mark) => decided.get(mark)),
498
+ undecided
499
+ };
500
+ },
501
+ audit: () => {
502
+ const pass = openPass(nearView, true);
503
+ let at = 0;
504
+ return (expired) => {
505
+ if (!pass) return false;
506
+ while (at < pass.shown.length) {
507
+ const mark = pass.shown[at];
508
+ const entry = entryOf(mark);
509
+ at += 1;
510
+ if (entry && !signOf(mark)) pass.decide(mark, entry);
511
+ if (expired()) break;
512
+ }
513
+ return at < pass.shown.length;
514
+ };
515
+ },
516
+ follow: ({ contentOrigin, ink }) => {
517
+ const rect = layer.getBoundingClientRect();
518
+ const box = inkBoxOf(editable, rect);
519
+ const top = box.top - editable.scrollTop;
520
+ const moved = /* @__PURE__ */ new Map([[void 0, {
521
+ left: box.left - editable.scrollLeft - contentOrigin.left,
522
+ top: top - contentOrigin.top
523
+ }]]);
524
+ if (contentTop !== void 0 && top + rect.top !== contentTop) heading = top + rect.top < contentTop ? 1 : -1;
525
+ contentTop = top + rect.top;
526
+ const next = ink.map((each) => {
527
+ let by = moved.get(each.pin?.element);
528
+ if (!by && each.pin) {
529
+ const now = inkBoxOf(each.pin.element, rect);
530
+ by = {
531
+ left: now.left - each.pin.at.left,
532
+ top: now.top - each.pin.at.top
533
+ };
534
+ moved.set(each.pin.element, by);
535
+ }
536
+ return by && (by.left !== 0 || by.top !== 0) ? {
537
+ ...each,
538
+ dx: each.dx + by.left,
539
+ dy: each.dy + by.top
540
+ } : each;
541
+ });
542
+ return next.every((each, n) => each === ink[n]) ? ink : next;
543
+ },
314
544
  layoutChanged: (change, within) => {
315
545
  layouts.changed(change, within);
316
546
  if (change !== "words") return;
@@ -319,9 +549,6 @@ const createInkPlacer = ({ editable, layer, view }) => {
319
549
  break;
320
550
  }
321
551
  },
322
- nodesChanged: () => {
323
- gone = null;
324
- },
325
552
  place: () => placeWithin(nearView),
326
553
  placeEvery: () => placeWithin(everywhere)
327
554
  };