platejs 54.0.0-beta.7 → 54.0.0-beta.8

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 (218) hide show
  1. package/dist/{AIChatRequestContext-DVGOeWhQ.d.ts → AIChatRequestContext-PbXTTovP.d.ts} +4 -2
  2. package/dist/{BaseSuggestionPlugin-BRFMMyEo.d.ts → BaseSuggestionPlugin-Dg-cdX26.d.ts} +10 -4
  3. package/dist/{PlateStatic-DIwBAVqF.d.ts → PlateStatic-CggiBCuD.d.ts} +3 -3
  4. package/dist/ai/index.d.ts +40 -2
  5. package/dist/ai/index.js +2 -1
  6. package/dist/ai/lib/lockedContext.internal.js +105 -0
  7. package/dist/ai/lib/lockedContext.js +179 -0
  8. package/dist/ai/react/AIChatPlugin.js +498 -103
  9. package/dist/ai/react/CopilotPlugin.js +37 -6
  10. package/dist/ai/react/aiOutputLanding.internal.js +19 -4
  11. package/dist/ai/react/index.d.ts +42 -9
  12. package/dist/authored/index.d.ts +4 -4
  13. package/dist/authored/index.js +2 -2
  14. package/dist/code-block/codemirror/index.d.ts +1 -1
  15. package/dist/{combobox-CfCJYKT0.d.ts → combobox-CUUSZR07.d.ts} +2 -2
  16. package/dist/compiler/index.d.ts +2 -2
  17. package/dist/{core-Cqc_KzpG.d.ts → core-D8i_gkA6.d.ts} +173 -106
  18. package/dist/diff/index.d.ts +3 -30
  19. package/dist/docs/api/core/plate-plugin.md +10 -9
  20. package/dist/docs/api/core.md +10 -5
  21. package/dist/docs/api/document-change.md +17 -10
  22. package/dist/docs/api/react-hooks.md +11 -5
  23. package/dist/docs/authored-changes.md +9 -6
  24. package/dist/docs/board.md +5 -4
  25. package/dist/docs/bold.md +0 -2
  26. package/dist/docs/document-model.md +3 -3
  27. package/dist/docs/docx.md +44 -0
  28. package/dist/docs/editor-methods.md +3 -3
  29. package/dist/docs/examples/version-history.md +1 -1
  30. package/dist/docs/footnote.md +80 -28
  31. package/dist/docs/html.md +10 -5
  32. package/dist/docs/indent.md +7 -5
  33. package/dist/docs/ink.md +18 -20
  34. package/dist/docs/installation/node.md +1 -1
  35. package/dist/docs/list.md +23 -3
  36. package/dist/docs/llms.txt +4 -3
  37. package/dist/docs/lock.md +264 -0
  38. package/dist/docs/markdown.md +13 -96
  39. package/dist/docs/plugin-context.md +14 -10
  40. package/dist/docs/serializing.md +26 -3
  41. package/dist/docs/static.md +19 -5
  42. package/dist/docx/export/index.d.ts +3 -3
  43. package/dist/docx/export/lib/exportDocx.js +58 -12
  44. package/dist/docx/import/index.d.ts +4 -4
  45. package/dist/docx/paste/index.d.ts +4 -4
  46. package/dist/dom/index.d.ts +1 -1
  47. package/dist/excalidraw/index.d.ts +3 -3
  48. package/dist/excalidraw/react/index.d.ts +3 -3
  49. package/dist/facade.js +2 -2
  50. package/dist/features/basic-nodes/lib/BaseBlockPlugins.js +1 -1
  51. package/dist/features/basic-nodes/lib/BaseHeadingPlugins.js +27 -6
  52. package/dist/features/basic-nodes/lib/BaseMarkPlugins.js +64 -12
  53. package/dist/features/board/index.d.ts +1 -1
  54. package/dist/features/board/lib/boardTypes.internal.js +3 -0
  55. package/dist/features/callout/index.d.ts +3 -3
  56. package/dist/features/callout/lib/BaseCalloutPlugin.js +1 -4
  57. package/dist/features/code-block/lib/BaseCodeBlockPlugin.js +1 -2
  58. package/dist/features/combobox/index.d.ts +2 -2
  59. package/dist/features/combobox/lib/filterWords.js +7 -11
  60. package/dist/features/comments/index.d.ts +1 -1
  61. package/dist/features/date/index.d.ts +3 -3
  62. package/dist/features/details/index.d.ts +4 -3
  63. package/dist/features/details/lib/BaseDetailsPlugin.js +228 -122
  64. package/dist/features/field/index.d.ts +1 -1
  65. package/dist/features/field/lib/BaseFieldPlugin.js +18 -2
  66. package/dist/features/find/index.d.ts +3 -3
  67. package/dist/features/footnote/index.d.ts +1 -1
  68. package/dist/features/footnote/lib/BaseFootnotePlugin.js +9 -2
  69. package/dist/features/ink/index.d.ts +2 -2
  70. package/dist/features/ink/lib/BaseInkPlugin.js +118 -82
  71. package/dist/features/ink/lib/inkMatch.internal.js +79 -78
  72. package/dist/features/ink/lib/inkPlaces.internal.js +166 -535
  73. package/dist/features/layout/index.d.ts +1 -1
  74. package/dist/features/layout/lib/BaseColumnPlugin.js +37 -14
  75. package/dist/features/link/lib/BaseLinkPlugin.js +38 -3
  76. package/dist/features/lint/index.d.ts +3 -3
  77. package/dist/features/list/lib/BaseListPlugin.js +30 -11
  78. package/dist/features/list/lib/listFamily.internal.js +2 -2
  79. package/dist/features/list/lib/listHtml.internal.js +1 -0
  80. package/dist/features/lock/index.d.ts +2 -0
  81. package/dist/features/lock/index.js +2 -0
  82. package/dist/features/lock/lib/BaseLockPlugin.js +526 -0
  83. package/dist/features/media/index.d.ts +1 -1
  84. package/dist/features/mention/index.d.ts +5 -5
  85. package/dist/features/suggestion/index.d.ts +1 -1
  86. package/dist/features/table/index.d.ts +1 -1
  87. package/dist/features/toc/index.d.ts +1 -1
  88. package/dist/features/upload/index.d.ts +1 -1
  89. package/dist/features/upload/lib/BaseUploadPlugin.js +5 -4
  90. package/dist/history/index.d.ts +2 -2
  91. package/dist/html/index.d.ts +3 -3
  92. package/dist/html/server/index.d.ts +1 -1
  93. package/dist/html/server/index.js +2 -1
  94. package/dist/html/server/serverCssParser.internal.js +500 -0
  95. package/dist/html/server/serverCssStyle.internal.js +208 -0
  96. package/dist/{htmlTypes-i_FBCMf2.d.ts → htmlTypes-zTLU3aTb.d.ts} +9 -13
  97. package/dist/hyperscript/index.d.ts +1 -1
  98. package/dist/index-0YxPsHwd.d.ts +1 -0
  99. package/dist/{index-Bv13FX9o.d.ts → index-3CExu_fj.d.ts} +3 -3
  100. package/dist/{index-CTTjpWgb-Fdrn8NP4.d.ts → index-4PpawhYe-yelJvgk1.d.ts} +1 -1
  101. package/dist/{index-B4ZhSQdk.d.ts → index-BCYBYfGM.d.ts} +3 -3
  102. package/dist/{index-CJGkjkH6.d.ts → index-BODDOPEd.d.ts} +1 -1
  103. package/dist/{index-DNXr_SN-.d.ts → index-BPOa4Z5n.d.ts} +9 -3
  104. package/dist/{index-Cc2ajXAd.d.ts → index-BSBkt2bm.d.ts} +44 -48
  105. package/dist/{index-CLNem74G.d.ts → index-BeyI3RUg.d.ts} +42 -39
  106. package/dist/{index-CmISVYmz.d.ts → index-BisOdLXI.d.ts} +14 -49
  107. package/dist/{index-CVAHIQ3p.d.ts → index-Bt5YI_JN.d.ts} +4 -4
  108. package/dist/{index-CrJ9Km8W.d.ts → index-CD_Ym9WQ.d.ts} +2 -2
  109. package/dist/{index-KteL56rA.d.ts → index-CX_JaDRY.d.ts} +4 -3
  110. package/dist/{index-BI0NS7kx.d.ts → index-CfK4uv4I.d.ts} +3 -3
  111. package/dist/{index-By7BkzIN.d.ts → index-CfRUKYCM.d.ts} +2 -2
  112. package/dist/{index-OMa7dlcS.d.ts → index-Cil7kghY.d.ts} +5 -5
  113. package/dist/index-D11C0iLb.d.ts +97 -0
  114. package/dist/{index-CS4G_W_R.d.ts → index-DJW6AjJ6.d.ts} +1 -1
  115. package/dist/{comparison-types-CsmDG5r2-z1s4gX06.d.ts → index-DM-b6jEe-BqkdOlav.d.ts} +19 -57
  116. package/dist/{index-1PlYsibA.d.ts → index-DoZhM7nD.d.ts} +3 -3
  117. package/dist/{index-DfkFmApH.d.ts → index-DsNwo8dd.d.ts} +35 -14
  118. package/dist/{index-CxG1Atvg.d.ts → index-DuElD_q6.d.ts} +3 -3
  119. package/dist/{index-BWCC4DCP.d.ts → index-f3aV3Kt-.d.ts} +5 -5
  120. package/dist/{index-uVbqCpDa.d.ts → index-u_h6ow4e.d.ts} +3 -3
  121. package/dist/{index-1rcWTB-o.d.ts → index-yZDmK6ab.d.ts} +3 -3
  122. package/dist/index.d.ts +3 -3
  123. package/dist/internal/plugin/OverridePlugin.js +22 -3
  124. package/dist/internal/plugin/blockInsertion.js +4 -4
  125. package/dist/internal/plugin/collectPlateNodeMappings.js +11 -4
  126. package/dist/internal/plugin/compilePlainTextMappings.js +2 -0
  127. package/dist/internal/plugin/plateRuntime.js +7 -1
  128. package/dist/internal/plugin/resolvePlugin.js +4 -1
  129. package/dist/internal/plugin/resolvePlugins.js +5 -3
  130. package/dist/lib/plugins/html/HtmlPlugin.js +32 -13
  131. package/dist/lib/plugins/html/htmlAst.js +5 -3
  132. package/dist/lib/plugins/html/htmlDom.js +1 -10
  133. package/dist/markdown/index.d.ts +2 -2
  134. package/dist/markdown/index.js +1 -2
  135. package/dist/markdown/lib/MarkdownPlugin.js +5 -7
  136. package/dist/markdown/lib/deserializer/convertNodesDeserialize.js +27 -3
  137. package/dist/markdown/lib/internal/markdownConversion.js +308 -340
  138. package/dist/markdown/lib/internal/markdownIntrinsics.js +36 -71
  139. package/dist/markdown/lib/internal/markdownLine.js +2 -4
  140. package/dist/markdown/lib/internal/markdownMappings.js +21 -3
  141. package/dist/markdown/lib/internal/markdownSafety.js +101 -1
  142. package/dist/markdown/lib/internal/markdownTags.js +2 -26
  143. package/dist/markdown/lib/serializer/convertNodesSerialize.js +5 -3
  144. package/dist/markdown/lib/serializer/convertPhrasingSerialize.js +1 -10
  145. package/dist/math/index.d.ts +3 -3
  146. package/dist/math/react/index.d.ts +4 -4
  147. package/dist/migrations/documentMigrationSelection.internal.js +107 -2
  148. package/dist/migrations/documentMigrations.js +17 -112
  149. package/dist/migrations/index.d.ts +3 -3
  150. package/dist/migrations/migratePlateV54.js +41 -40
  151. package/dist/migrations/migratePlateV54Ast.internal.js +143 -55
  152. package/dist/migrations/migratePlateV54CodeBlocks.internal.js +24 -29
  153. package/dist/migrations/migratePlateV54Inputs.internal.js +39 -32
  154. package/dist/migrations/migratePlateV54Profile.internal.js +52 -22
  155. package/dist/migrations/migratePlateV54Stage.internal.js +127 -0
  156. package/dist/migrations/migratePlateV54Urls.internal.js +77 -57
  157. package/dist/pagination/index.d.ts +1 -1
  158. package/dist/pagination/react/index.d.ts +2 -2
  159. package/dist/react/components/NodeSelection.js +8 -1
  160. package/dist/react/components/Plate.js +198 -152
  161. package/dist/react/components/PlateView.js +1 -1
  162. package/dist/{static/internal/getPlainText.js → react/components/getPlainText.internal.js} +1 -4
  163. package/dist/react/components/plate-nodes.js +48 -40
  164. package/dist/{static/internal/writeStaticSelectionClipboardData.js → react/components/writeStaticSelectionClipboardData.internal.js} +3 -4
  165. package/dist/react/features/board/BoardShape.js +48 -22
  166. package/dist/react/features/board/boardConnect.internal.js +0 -9
  167. package/dist/react/features/board/boardKeys.internal.js +41 -12
  168. package/dist/react/features/board/boardPicking.internal.js +0 -17
  169. package/dist/react/features/board/boardResize.internal.js +13 -18
  170. package/dist/react/features/board/boardView.internal.js +37 -8
  171. package/dist/react/features/board/boardVoice.internal.js +17 -4
  172. package/dist/react/features/board/index.d.ts +13 -11
  173. package/dist/react/features/callout/index.d.ts +3 -3
  174. package/dist/react/features/combobox/comboboxOwner.internal.js +10 -4
  175. package/dist/react/features/combobox/index.d.ts +5 -5
  176. package/dist/react/features/comments/index.d.ts +5 -5
  177. package/dist/react/features/date/index.d.ts +4 -4
  178. package/dist/react/features/details/index.d.ts +5 -5
  179. package/dist/react/features/field/index.d.ts +6 -5
  180. package/dist/react/features/footnote/index.d.ts +7 -7
  181. package/dist/react/features/format-painter/index.d.ts +4 -4
  182. package/dist/react/features/ink/InkContent.js +28 -100
  183. package/dist/react/features/ink/InkPlugin.js +12 -23
  184. package/dist/react/features/ink/index.d.ts +29 -102
  185. package/dist/react/features/ink/index.js +1 -2
  186. package/dist/react/features/ink/inkCapture.internal.js +97 -61
  187. package/dist/react/features/ink/inkImages.internal.js +20 -5
  188. package/dist/react/features/ink/inkMeasure.internal.js +35 -22
  189. package/dist/react/features/ink/inkPlacement.internal.js +78 -203
  190. package/dist/react/features/ink/inkWatch.internal.js +1 -32
  191. package/dist/react/features/layout/index.d.ts +5 -5
  192. package/dist/react/features/lock/LockPlugin.js +66 -0
  193. package/dist/react/features/lock/index.d.ts +58 -0
  194. package/dist/react/features/lock/index.js +2 -0
  195. package/dist/react/features/media/index.d.ts +5 -5
  196. package/dist/react/features/mention/index.d.ts +6 -6
  197. package/dist/react/features/suggestion/index.d.ts +6 -6
  198. package/dist/react/features/table/index.d.ts +5 -5
  199. package/dist/react/features/toc/index.d.ts +2 -2
  200. package/dist/react/features/upload/index.d.ts +5 -5
  201. package/dist/react/index.d.ts +4 -4
  202. package/dist/react/utils/pipeRenderElement.internal.js +32 -30
  203. package/dist/react/virtualized.d.ts +2 -2
  204. package/dist/{source-Dcgqj5Mf.d.ts → source-B1rLozUs.d.ts} +3 -3
  205. package/dist/static/components/plite-nodes.js +1 -1
  206. package/dist/static/index.d.ts +8 -10
  207. package/dist/static/pluginRenderLeafStatic.internal.js +1 -2
  208. package/dist/tabbable/index.d.ts +1 -1
  209. package/dist/tabbable/react/index.d.ts +3 -3
  210. package/dist/testing/index.d.ts +2 -2
  211. package/dist/{tx-only-1FM45FSj-D40jacZv.d.ts → tx-only-D8lpB-sT-ZiAbZRMU.d.ts} +133 -59
  212. package/dist/yjs/index.d.ts +1 -1
  213. package/dist/yjs/react/index.d.ts +5 -5
  214. package/package.json +16 -3
  215. package/dist/features/ink/lib/inkGone.internal.js +0 -290
  216. package/dist/markdown/lib/RawHtmlPlugin.js +0 -26
  217. package/dist/markdown/lib/internal/markdownRawHtml.js +0 -353
  218. package/dist/react/features/ink/InkCopyView.js +0 -214
@@ -1,5 +1,5 @@
1
- import { Wa as NodeSelectionMode, Yc as Selection, bs as Range, oo as Path, xu as Value } from "./tx-only-1FM45FSj-D40jacZv.js";
2
- import "./core-Cqc_KzpG.js";
1
+ import { Wa as NodeSelectionMode, Yc as Selection, bs as Range, oo as Path, xu as Value } from "./tx-only-D8lpB-sT-ZiAbZRMU.js";
2
+ import "./core-D8i_gkA6.js";
3
3
  type AIToolName = 'comment' | 'edit' | 'generate' | null;
4
4
  /** Request-local reference into `AIChatRequestContext.children`. */
5
5
  type AIChatRequestNodeRef = Readonly<{
@@ -8,6 +8,8 @@ type AIChatRequestNodeRef = Readonly<{
8
8
  }>;
9
9
  type AIChatRequestRefs = Readonly<{
10
10
  blocks: readonly AIChatRequestNodeRef[];
11
+ /** Maximal non-writable blocks inside the active selection, in document order. */
12
+ lockedBlocks?: readonly AIChatRequestNodeRef[];
11
13
  tableCells: readonly AIChatRequestNodeRef[];
12
14
  }>;
13
15
  /** Exact directional membership alongside the representative request range. */
@@ -1,7 +1,9 @@
1
- import { Aa as NodeKey, bu as TxOnlyMethod, po as Plugin, ta as Location, vu as TxDeclaredMethod } from "./tx-only-1FM45FSj-D40jacZv.js";
2
- import "./htmlTypes-i_FBCMf2.js";
3
- import { $i as BasePluginDependencyReferenceFor, H as InternalBasePluginRuntimeExtension, go as PluginReference, mo as PluginDependency, v as BasePlugin } from "./core-Cqc_KzpG.js";
4
- import { S as AuthoredView, _ as AuthoredPlugin, b as AuthoredResult, c as AuthoredDecision, h as AuthoredPage, m as AuthoredOutcome, n as AuthoredChangeDetails, o as AuthoredChangesDelta, r as AuthoredChangeGroup, t as AuthoredChange, u as AuthoredGroupDetails, v as AuthoredQuery, x as AuthoredReview, y as AuthoredRefusal } from "./index-CLNem74G.js";
1
+ import { Aa as NodeKey, bu as TxOnlyMethod, po as Plugin, ta as Location, vu as TxDeclaredMethod } from "./tx-only-D8lpB-sT-ZiAbZRMU.js";
2
+ import "./htmlTypes-zTLU3aTb.js";
3
+ import { $i as BasePluginDependencyReferenceFor, H as InternalBasePluginRuntimeExtension, go as PluginReference, mo as PluginDependency, v as BasePlugin } from "./core-D8i_gkA6.js";
4
+ import { n as Comparison } from "./index-DM-b6jEe-BqkdOlav.js";
5
+ import "./index-0YxPsHwd.js";
6
+ import { S as AuthoredView, _ as AuthoredPlugin, b as AuthoredResult, c as AuthoredDecision, h as AuthoredPage, m as AuthoredOutcome, n as AuthoredChangeDetails, o as AuthoredChangesDelta, r as AuthoredChangeGroup, s as AuthoredComparisonImportResult, t as AuthoredChange, u as AuthoredGroupDetails, v as AuthoredQuery, x as AuthoredReview, y as AuthoredRefusal } from "./index-BeyI3RUg.js";
5
7
  /** Paints each node's pending authored changes as `data-editor-authored-*` attributes. */
6
8
  declare const BaseSuggestionPlugin: BasePlugin<Readonly<{
7
9
  inject: true;
@@ -13,6 +15,10 @@ declare const BaseSuggestionPlugin: BasePlugin<Readonly<{
13
15
  }> & Readonly<{
14
16
  __pluginWritableProperties: never;
15
17
  }> & Readonly<Record<"api", {
18
+ proposeComparison: (input: Readonly<{
19
+ comparison: Comparison;
20
+ groupIds?: readonly string[];
21
+ }>) => AuthoredComparisonImportResult;
16
22
  setView: (view: AuthoredView) => void;
17
23
  subscribeChanges: (listener: (delta: AuthoredChangesDelta) => void) => () => void;
18
24
  subscribeRefusals: (listener: (refusal: AuthoredRefusal) => void) => () => void;
@@ -1,6 +1,6 @@
1
- import { Tt as EditorDocumentValue } from "./tx-only-1FM45FSj-D40jacZv.js";
2
- import "./htmlTypes-i_FBCMf2.js";
3
- import { u as Editor } from "./core-Cqc_KzpG.js";
1
+ import { Tt as EditorDocumentValue } from "./tx-only-D8lpB-sT-ZiAbZRMU.js";
2
+ import "./htmlTypes-zTLU3aTb.js";
3
+ import { u as Editor } from "./core-D8i_gkA6.js";
4
4
  import React$1 from "react";
5
5
  type EditorStaticProps<E = Editor> = {
6
6
  /**
@@ -1,2 +1,40 @@
1
- import { a as AIToolName, i as AIChatRequestRefs, n as AIChatRequestNodeRef, o as resolveAIChatRequestContext, r as AIChatRequestNodeSelection, t as AIChatRequestContext } from "../AIChatRequestContext-DVGOeWhQ.js";
2
- export { AIChatRequestContext, AIChatRequestNodeRef, AIChatRequestNodeSelection, AIChatRequestRefs, AIToolName, resolveAIChatRequestContext };
1
+ import { u as Editor } from "../core-D8i_gkA6.js";
2
+ import { a as AIToolName, i as AIChatRequestRefs, n as AIChatRequestNodeRef, o as resolveAIChatRequestContext, r as AIChatRequestNodeSelection, t as AIChatRequestContext } from "../AIChatRequestContext-PbXTTovP.js";
3
+ import { r as MarkdownEditor } from "../index-BSBkt2bm.js";
4
+ /**
5
+ * Self-closing Markdown block tag emitted for locked blocks inside AI edit
6
+ * prompts and parsed back at the Markdown AST stage.
7
+ */
8
+ declare const LOCKED_BLOCK_PLACEHOLDER_TAG = "Locked";
9
+ /**
10
+ * Return `true` when the active selection contains at least one writable block
11
+ * or table cell that AI can edit in place.
12
+ */
13
+ declare const hasEditableAISelection: (editor: Editor, refs?: Partial<Pick<AIChatRequestRefs, 'lockedBlocks' | 'tableCells'>>) => boolean;
14
+ /** Result of serializing an AI multi-block edit context with locked placeholders. */
15
+ type SerializedAIEditContext = Readonly<{
16
+ /** Read-only plain-text summaries of each locked block, one per line. */
17
+ lockedContext: string;
18
+ /** Markdown prompt context with `<Locked ref="lN" />` replacing locked blocks. */
19
+ markdown: string;
20
+ /** Request-local locked block references in document order. */
21
+ refs: readonly AIChatRequestNodeRef[];
22
+ }>;
23
+ /**
24
+ * Serialize the active selection for multi-block AI edits, replacing each
25
+ * non-writable block with a request-local `<Locked ref="lN" />` placeholder
26
+ * and collecting read-only `<locked-clauses>` context lines.
27
+ */
28
+ declare const serializeAIEditContext: (editor: MarkdownEditor, options?: Readonly<{
29
+ lockedBlocks?: readonly AIChatRequestNodeRef[];
30
+ }>) => SerializedAIEditContext;
31
+ /**
32
+ * Serialize the active selection to Markdown without mutating the editor.
33
+ * When `includeSelectionMarkers` is `true`, `<Selection>` and `</Selection>`
34
+ * are spliced into a cloned JSON copy of the selected blocks so locked blocks
35
+ * stay readable by single-block edit and generate prompts.
36
+ */
37
+ declare const serializeAISelectionMarkdown: (editor: MarkdownEditor, options?: Readonly<{
38
+ includeSelectionMarkers?: boolean;
39
+ }>) => string;
40
+ export { AIChatRequestContext, AIChatRequestNodeRef, AIChatRequestNodeSelection, AIChatRequestRefs, AIToolName, LOCKED_BLOCK_PLACEHOLDER_TAG, SerializedAIEditContext, hasEditableAISelection, resolveAIChatRequestContext, serializeAIEditContext, serializeAISelectionMarkdown };
package/dist/ai/index.js CHANGED
@@ -1,2 +1,3 @@
1
1
  import { resolveAIChatRequestContext } from "./lib/AIChatRequestContext.js";
2
- export { resolveAIChatRequestContext };
2
+ import { LOCKED_BLOCK_PLACEHOLDER_TAG, hasEditableAISelection, serializeAIEditContext, serializeAISelectionMarkdown } from "./lib/lockedContext.js";
3
+ export { LOCKED_BLOCK_PLACEHOLDER_TAG, hasEditableAISelection, resolveAIChatRequestContext, serializeAIEditContext, serializeAISelectionMarkdown };
@@ -0,0 +1,105 @@
1
+ import { hasNonWritableAt } from "../../facade.js";
2
+ import { ElementApi, PathApi, RangeApi, SelectionApi } from "../../core.js";
3
+ //#region src/ai/lib/lockedContext.internal.ts
4
+ /**
5
+ * Return `true` when the block at `path` is non-writable via a contributor
6
+ * (such as `BaseLockPlugin`) on itself, an ancestor, an owned root, or any
7
+ * descendant, excluding static `schema.isReadOnly` atoms.
8
+ *
9
+ * @internal
10
+ */
11
+ const isAIPathNonWritable = (editor, path) => {
12
+ const root = editor.read.view.root();
13
+ return hasNonWritableAt(editor, SelectionApi.nodes([path], {
14
+ mode: "block",
15
+ ...root ? { root } : {}
16
+ }), { includeReadOnly: false });
17
+ };
18
+ const isElementAt = (editor, path) => {
19
+ const entry = editor.read.nodes.get(path, { match: ElementApi.isElement });
20
+ return entry ? [entry[0], entry[1]] : null;
21
+ };
22
+ const isContainerFullyCoveredByRange = (editor, containerPath, startPath, endPath) => {
23
+ const firstLeaf = editor.read.nodes.first(containerPath);
24
+ const lastLeaf = editor.read.nodes.last(containerPath);
25
+ if (!firstLeaf || !lastLeaf) return true;
26
+ const startsAtOrBeforeFirst = PathApi.compare(startPath, firstLeaf[1]) <= 0;
27
+ const endsAtOrAfterLast = PathApi.compare(endPath, lastLeaf[1]) >= 0;
28
+ return startsAtOrBeforeFirst && endsAtOrAfterLast;
29
+ };
30
+ /**
31
+ * Resolve the active AI selection into sibling target block entries shared by
32
+ * client target capture, request ref collection, and server edit serialization.
33
+ *
34
+ * @internal
35
+ */
36
+ const resolveAISelectedBlocks = (editor, explicitSelection) => {
37
+ const selectedNodes = explicitSelection === void 0 ? editor.read.selection.nodes() : [];
38
+ if (selectedNodes.length > 0) {
39
+ const entries = [];
40
+ for (const [node, path] of selectedNodes) if (ElementApi.isElement(node)) entries.push([node, path]);
41
+ entries.sort(([, left], [, right]) => PathApi.compare(left, right));
42
+ return {
43
+ entries,
44
+ partialLockedContainer: false
45
+ };
46
+ }
47
+ const selection = explicitSelection === void 0 ? editor.read.selection() : explicitSelection;
48
+ if (!selection) return {
49
+ entries: [],
50
+ partialLockedContainer: false
51
+ };
52
+ const [start, end] = RangeApi.edges(selection);
53
+ if (PathApi.equals(start.path, end.path)) {
54
+ const block = editor.read.nodes.block({ at: start });
55
+ return {
56
+ entries: block ? [block] : [],
57
+ partialLockedContainer: false
58
+ };
59
+ }
60
+ let containerPath = PathApi.common(start.path, end.path);
61
+ while (containerPath.length > 0) {
62
+ const entry = editor.read.nodes.get(containerPath);
63
+ if (entry && ElementApi.isElement(entry[0]) && !editor.read.schema.isInline(entry[0]) && entry[0].children.every((child) => ElementApi.isElement(child))) break;
64
+ containerPath = PathApi.parent(containerPath);
65
+ }
66
+ const depth = containerPath.length;
67
+ const startIndex = start.path[depth];
68
+ const endIndex = end.path[depth];
69
+ if (startIndex === void 0 || endIndex === void 0) return {
70
+ entries: [...editor.read.nodes.blocks({ at: selection })],
71
+ partialLockedContainer: false
72
+ };
73
+ const siblings = [];
74
+ for (let index = startIndex; index <= endIndex; index += 1) {
75
+ const path = [...containerPath, index];
76
+ const entry = isElementAt(editor, path);
77
+ if (entry) siblings.push(entry);
78
+ }
79
+ if (siblings.length === 0) return {
80
+ entries: [],
81
+ partialLockedContainer: false
82
+ };
83
+ let hasPartial = false;
84
+ let partialLockedContainer = false;
85
+ for (const [node, path] of siblings) {
86
+ if (!(node.children.length > 0 && node.children.every((child) => ElementApi.isElement(child)))) continue;
87
+ if (isContainerFullyCoveredByRange(editor, path, start.path, end.path)) continue;
88
+ hasPartial = true;
89
+ if (isAIPathNonWritable(editor, path)) partialLockedContainer = true;
90
+ }
91
+ if (partialLockedContainer) return {
92
+ entries: siblings,
93
+ partialLockedContainer: true
94
+ };
95
+ if (hasPartial) return {
96
+ entries: [...editor.read.nodes.blocks({ at: selection })],
97
+ partialLockedContainer: false
98
+ };
99
+ return {
100
+ entries: siblings,
101
+ partialLockedContainer: false
102
+ };
103
+ };
104
+ //#endregion
105
+ export { isAIPathNonWritable, resolveAISelectedBlocks };
@@ -0,0 +1,179 @@
1
+ import { ElementApi, NodeApi, PathApi, RangeApi, TextApi } from "../../core.js";
2
+ import { BaseTableCellPlugin, BaseTablePlugin } from "../../features/table/lib/BaseTablePlugin.js";
3
+ import { isAIPathNonWritable, resolveAISelectedBlocks } from "./lockedContext.internal.js";
4
+ //#region src/ai/lib/lockedContext.ts
5
+ /**
6
+ * Self-closing Markdown block tag emitted for locked blocks inside AI edit
7
+ * prompts and parsed back at the Markdown AST stage.
8
+ */
9
+ const LOCKED_BLOCK_PLACEHOLDER_TAG = "Locked";
10
+ const SELECTION_START = "<Selection>";
11
+ const SELECTION_END = "</Selection>";
12
+ const MAX_LOCKED_SUMMARY_CHARS = 240;
13
+ const getSelectedTableCells = (editor) => {
14
+ const tablePlugin = editor.plugin(BaseTablePlugin);
15
+ if (tablePlugin.installed) {
16
+ const selected = tablePlugin.read.selection()?.cells ?? [];
17
+ if (selected.length > 0) return selected;
18
+ }
19
+ if (!editor.read.selection()) return [];
20
+ if (!editor.read.nodes.block({ type: BaseTablePlugin })) return [];
21
+ return editor.read.nodes.toArray({ type: BaseTableCellPlugin });
22
+ };
23
+ /**
24
+ * Return `true` when the active selection contains at least one writable block
25
+ * or table cell that AI can edit in place.
26
+ */
27
+ const hasEditableAISelection = (editor, refs) => {
28
+ const selectedCells = getSelectedTableCells(editor);
29
+ if (selectedCells.length > 1) {
30
+ if (refs?.tableCells !== void 0) return refs.tableCells.some(({ path }) => !isAIPathNonWritable(editor, path));
31
+ return selectedCells.some(([, path]) => !isAIPathNonWritable(editor, path));
32
+ }
33
+ const { entries, partialLockedContainer } = resolveAISelectedBlocks(editor);
34
+ if (entries.length === 0 || partialLockedContainer) return false;
35
+ const lockedPaths = new Set((refs?.lockedBlocks ?? []).map(({ path }) => path.join(",")));
36
+ return entries.some(([, path]) => !lockedPaths.has(path.join(",")) && !isAIPathNonWritable(editor, path));
37
+ };
38
+ const summarizeLockedBlock = (node) => {
39
+ const normalized = NodeApi.string(node).replaceAll(/\s+/g, " ").trim();
40
+ if (!normalized) return "(empty locked block)";
41
+ if (normalized.length <= MAX_LOCKED_SUMMARY_CHARS) return normalized;
42
+ return `${normalized.slice(0, MAX_LOCKED_SUMMARY_CHARS - 3)}...`;
43
+ };
44
+ const serializeUnlockedRun = (editor, nodes) => {
45
+ if (nodes.length === 0) return "";
46
+ const result = editor.api.markdown.serialize({
47
+ document: { children: [...nodes] },
48
+ lossPolicy: "allow"
49
+ });
50
+ if (!result.ok) throw new Error(result.diagnostics[0]?.message ?? "Failed to serialize AI edit context.");
51
+ return result.data.trim();
52
+ };
53
+ /**
54
+ * Serialize the active selection for multi-block AI edits, replacing each
55
+ * non-writable block with a request-local `<Locked ref="lN" />` placeholder
56
+ * and collecting read-only `<locked-clauses>` context lines.
57
+ */
58
+ const serializeAIEditContext = (editor, options = {}) => {
59
+ const { entries } = resolveAISelectedBlocks(editor);
60
+ const providedByPath = new Map((options.lockedBlocks ?? []).map((entry) => [entry.path.join(","), entry.ref]));
61
+ const usedRefs = new Set(providedByPath.values());
62
+ let nextRefIndex = 1;
63
+ const allocateRef = () => {
64
+ while (usedRefs.has(`l${nextRefIndex}`)) nextRefIndex += 1;
65
+ const ref = `l${nextRefIndex}`;
66
+ usedRefs.add(ref);
67
+ nextRefIndex += 1;
68
+ return ref;
69
+ };
70
+ const parts = [];
71
+ const lockedSummaries = [];
72
+ const lockedRefs = [];
73
+ let unlockedRun = [];
74
+ const flushUnlocked = () => {
75
+ if (unlockedRun.length === 0) return;
76
+ const serialized = serializeUnlockedRun(editor, unlockedRun);
77
+ if (serialized) parts.push(serialized);
78
+ unlockedRun = [];
79
+ };
80
+ for (const [node, path] of entries) {
81
+ const pathKey = path.join(",");
82
+ const providedRef = providedByPath.get(pathKey);
83
+ if (!(providedRef !== void 0 || isAIPathNonWritable(editor, path))) {
84
+ unlockedRun.push(node);
85
+ continue;
86
+ }
87
+ flushUnlocked();
88
+ const ref = providedRef ?? allocateRef();
89
+ lockedRefs.push({
90
+ path: [...path],
91
+ ref
92
+ });
93
+ parts.push(`<${LOCKED_BLOCK_PLACEHOLDER_TAG} ref="${ref}" />`);
94
+ lockedSummaries.push(`<${LOCKED_BLOCK_PLACEHOLDER_TAG} ref="${ref}" />: ${summarizeLockedBlock(node)}`);
95
+ }
96
+ flushUnlocked();
97
+ return Object.freeze({
98
+ lockedContext: lockedSummaries.join("\n"),
99
+ markdown: parts.join("\n\n"),
100
+ refs: Object.freeze(lockedRefs)
101
+ });
102
+ };
103
+ const spliceSelectionInNode = (node, relativePath, offset, marker) => {
104
+ if (relativePath.length === 0) {
105
+ if (!TextApi.isText(node)) return node;
106
+ const clamped = Math.max(0, Math.min(offset, node.text.length));
107
+ return {
108
+ ...node,
109
+ text: `${node.text.slice(0, clamped)}${marker}${node.text.slice(clamped)}`
110
+ };
111
+ }
112
+ if (!ElementApi.isElement(node)) return node;
113
+ const [childIndex, ...rest] = relativePath;
114
+ if (childIndex === void 0 || childIndex < 0 || childIndex >= node.children.length) return node;
115
+ const nextChildren = node.children.map((child, index) => index === childIndex ? spliceSelectionInNode(child, rest, offset, marker) : child);
116
+ return {
117
+ ...node,
118
+ children: nextChildren
119
+ };
120
+ };
121
+ const unescapeSelectionMarkers = (editor, text, includeSelectionMarkers) => {
122
+ let output = text.replace(`\\${SELECTION_START}`, SELECTION_START).replace(`\\${SELECTION_END}`, SELECTION_END);
123
+ if (!includeSelectionMarkers || output.includes(SELECTION_END)) return output;
124
+ const selection = editor.read.selection();
125
+ if (!selection || RangeApi.isCollapsed(selection)) return output;
126
+ const [, end] = RangeApi.edges(selection);
127
+ const block = editor.read.nodes.block({ at: end.path });
128
+ if (!block || !editor.read.schema.isVoid(block[0])) return output;
129
+ const result = editor.api.markdown.serialize({
130
+ document: { children: [block[0]] },
131
+ lossPolicy: "allow"
132
+ });
133
+ if (!result.ok) return output;
134
+ const voidString = result.data;
135
+ const idx = output.lastIndexOf(voidString);
136
+ if (idx !== -1) output = output.slice(0, idx) + voidString.trimEnd() + SELECTION_END + output.slice(idx + voidString.length);
137
+ return output;
138
+ };
139
+ /**
140
+ * Serialize the active selection to Markdown without mutating the editor.
141
+ * When `includeSelectionMarkers` is `true`, `<Selection>` and `</Selection>`
142
+ * are spliced into a cloned JSON copy of the selected blocks so locked blocks
143
+ * stay readable by single-block edit and generate prompts.
144
+ */
145
+ const serializeAISelectionMarkdown = (editor, options = {}) => {
146
+ const { includeSelectionMarkers = false } = options;
147
+ const entries = [...editor.read.nodes.blocks()];
148
+ if (entries.length === 0) return "";
149
+ const selection = editor.read.selection();
150
+ const blocks = entries.map(([node]) => structuredClone(node));
151
+ if (includeSelectionMarkers && selection && RangeApi.isExpanded(selection)) {
152
+ const [start, end] = RangeApi.edges(selection);
153
+ const points = [{
154
+ marker: SELECTION_END,
155
+ point: end
156
+ }, {
157
+ marker: SELECTION_START,
158
+ point: start
159
+ }];
160
+ for (const { marker, point } of points) {
161
+ const entryIndex = entries.findIndex(([, blockPath]) => PathApi.equals(blockPath, point.path) || PathApi.isAncestor(blockPath, point.path));
162
+ if (entryIndex === -1) continue;
163
+ const entry = entries[entryIndex];
164
+ const currentBlock = blocks[entryIndex];
165
+ if (!entry || !currentBlock) continue;
166
+ const relativePath = PathApi.relative(point.path, entry[1]);
167
+ const updated = spliceSelectionInNode(currentBlock, relativePath, point.offset, marker);
168
+ if (ElementApi.isElement(updated)) blocks[entryIndex] = updated;
169
+ }
170
+ }
171
+ const result = editor.api.markdown.serialize({
172
+ document: { children: blocks },
173
+ lossPolicy: "allow"
174
+ });
175
+ if (!result.ok) throw new Error(result.diagnostics[0]?.message ?? "Failed to serialize AI selection.");
176
+ return unescapeSelectionMarkers(editor, result.data, includeSelectionMarkers);
177
+ };
178
+ //#endregion
179
+ export { LOCKED_BLOCK_PLACEHOLDER_TAG, hasEditableAISelection, serializeAIEditContext, serializeAISelectionMarkdown };