@sixb/app 0.2.4 → 0.2.6

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 (306) hide show
  1. package/assets/connector-icons/README.md +1 -0
  2. package/assets/connector-icons/ctm.png +0 -0
  3. package/dist/agent/activity-label.d.ts.map +1 -1
  4. package/dist/agent/agent-chat.d.ts +3 -1
  5. package/dist/agent/agent-chat.d.ts.map +1 -1
  6. package/dist/agent/agent-panel.d.ts.map +1 -1
  7. package/dist/agent/artifact-panel.d.ts +3 -1
  8. package/dist/agent/artifact-panel.d.ts.map +1 -1
  9. package/dist/agent/components/agent-threads.d.ts.map +1 -1
  10. package/dist/agent/components/composer-place.d.ts.map +1 -1
  11. package/dist/agent/components/composer-settings.d.ts +1 -1
  12. package/dist/agent/components/composer-settings.d.ts.map +1 -1
  13. package/dist/agent/components/composer.d.ts +22 -5
  14. package/dist/agent/components/composer.d.ts.map +1 -1
  15. package/dist/agent/components/computer-place.d.ts +24 -6
  16. package/dist/agent/components/computer-place.d.ts.map +1 -1
  17. package/dist/agent/components/context-meter.d.ts +1 -1
  18. package/dist/agent/components/context-meter.d.ts.map +1 -1
  19. package/dist/agent/components/conversation-panel.d.ts +5 -2
  20. package/dist/agent/components/conversation-panel.d.ts.map +1 -1
  21. package/dist/agent/components/message-parts.d.ts +2 -1
  22. package/dist/agent/components/message-parts.d.ts.map +1 -1
  23. package/dist/agent/components/message-view.d.ts +13 -3
  24. package/dist/agent/components/message-view.d.ts.map +1 -1
  25. package/dist/agent/components/pending-messages.d.ts +20 -0
  26. package/dist/agent/components/pending-messages.d.ts.map +1 -0
  27. package/dist/agent/components/sandbox-recovery.d.ts +11 -2
  28. package/dist/agent/components/sandbox-recovery.d.ts.map +1 -1
  29. package/dist/agent/components/threads-button.d.ts +1 -1
  30. package/dist/agent/components/threads-button.d.ts.map +1 -1
  31. package/dist/agent/components/transcript.d.ts +13 -0
  32. package/dist/agent/components/transcript.d.ts.map +1 -1
  33. package/dist/agent/conversation.d.ts +7 -6
  34. package/dist/agent/conversation.d.ts.map +1 -1
  35. package/dist/agent/delegation/interpret.d.ts +5 -0
  36. package/dist/agent/delegation/interpret.d.ts.map +1 -1
  37. package/dist/agent/delegation/use-children.d.ts.map +1 -1
  38. package/dist/agent/format.d.ts +7 -0
  39. package/dist/agent/format.d.ts.map +1 -1
  40. package/dist/agent/hooks/thread-queries.d.ts +1548 -0
  41. package/dist/agent/hooks/thread-queries.d.ts.map +1 -0
  42. package/dist/agent/hooks/use-agent-conversation.d.ts +51 -6
  43. package/dist/agent/hooks/use-agent-conversation.d.ts.map +1 -1
  44. package/dist/agent/hooks/use-thread-lists.d.ts +80 -3
  45. package/dist/agent/hooks/use-thread-lists.d.ts.map +1 -1
  46. package/dist/agent/hooks/use-thread-place.d.ts +49 -0
  47. package/dist/agent/hooks/use-thread-place.d.ts.map +1 -0
  48. package/dist/agent/hooks/use-thread-stream.d.ts.map +1 -1
  49. package/dist/agent/i18n/en.d.ts +107 -6
  50. package/dist/agent/i18n/en.d.ts.map +1 -1
  51. package/dist/agent/i18n/fr.d.ts.map +1 -1
  52. package/dist/agent/i18n/index.d.ts +180 -12
  53. package/dist/agent/i18n/index.d.ts.map +1 -1
  54. package/dist/agent/inbox-handoff.d.ts +10 -0
  55. package/dist/agent/inbox-handoff.d.ts.map +1 -0
  56. package/dist/agent/live-run.d.ts +8 -1
  57. package/dist/agent/live-run.d.ts.map +1 -1
  58. package/dist/agent/model-selection.d.ts +3 -2
  59. package/dist/agent/model-selection.d.ts.map +1 -1
  60. package/dist/agent/moves.d.ts +40 -0
  61. package/dist/agent/moves.d.ts.map +1 -0
  62. package/dist/agent/search/interpret.d.ts +101 -0
  63. package/dist/agent/search/interpret.d.ts.map +1 -0
  64. package/dist/agent/search/search-tool-view.d.ts +10 -0
  65. package/dist/agent/search/search-tool-view.d.ts.map +1 -0
  66. package/dist/agent/thread-reader-state.d.ts +30 -0
  67. package/dist/agent/thread-reader-state.d.ts.map +1 -0
  68. package/dist/agent/thread-state.d.ts +26 -0
  69. package/dist/agent/thread-state.d.ts.map +1 -0
  70. package/dist/agent/tool-views/built-in.d.ts.map +1 -1
  71. package/dist/agent/tool-views/registry.d.ts +5 -6
  72. package/dist/agent/tool-views/registry.d.ts.map +1 -1
  73. package/dist/agent/types.d.ts +9 -1
  74. package/dist/agent/types.d.ts.map +1 -1
  75. package/dist/agent/view-file/view-file-tool-view.d.ts +2 -1
  76. package/dist/agent/view-file/view-file-tool-view.d.ts.map +1 -1
  77. package/dist/artifacts/card-preview.d.ts.map +1 -1
  78. package/dist/artifacts/editor/block-handle.d.ts +20 -0
  79. package/dist/artifacts/editor/block-handle.d.ts.map +1 -0
  80. package/dist/artifacts/editor/blocks.d.ts +34 -0
  81. package/dist/artifacts/editor/blocks.d.ts.map +1 -0
  82. package/dist/artifacts/editor/callout-view.d.ts +7 -0
  83. package/dist/artifacts/editor/callout-view.d.ts.map +1 -0
  84. package/dist/artifacts/editor/document-editor.d.ts +30 -0
  85. package/dist/artifacts/editor/document-editor.d.ts.map +1 -0
  86. package/dist/artifacts/editor/figure-view.d.ts +12 -0
  87. package/dist/artifacts/editor/figure-view.d.ts.map +1 -0
  88. package/dist/artifacts/editor/find-blocks.d.ts +6 -0
  89. package/dist/artifacts/editor/find-blocks.d.ts.map +1 -0
  90. package/dist/artifacts/editor/markdown-document.d.ts +12 -0
  91. package/dist/artifacts/editor/markdown-document.d.ts.map +1 -0
  92. package/dist/artifacts/editor/messages.d.ts +4 -0
  93. package/dist/artifacts/editor/messages.d.ts.map +1 -0
  94. package/dist/artifacts/editor/schema.d.ts +21 -0
  95. package/dist/artifacts/editor/schema.d.ts.map +1 -0
  96. package/dist/artifacts/editor/selection-toolbar.d.ts +35 -0
  97. package/dist/artifacts/editor/selection-toolbar.d.ts.map +1 -0
  98. package/dist/artifacts/editor/slash-menu.d.ts +43 -0
  99. package/dist/artifacts/editor/slash-menu.d.ts.map +1 -0
  100. package/dist/artifacts/editor/upload.d.ts +12 -0
  101. package/dist/artifacts/editor/upload.d.ts.map +1 -0
  102. package/dist/artifacts/file-source.d.ts +5 -0
  103. package/dist/artifacts/file-source.d.ts.map +1 -1
  104. package/dist/artifacts/frame-editor/bundle.d.ts +7 -0
  105. package/dist/artifacts/frame-editor/bundle.d.ts.map +1 -0
  106. package/dist/artifacts/frame-editor/commands.d.ts +93 -0
  107. package/dist/artifacts/frame-editor/commands.d.ts.map +1 -0
  108. package/dist/artifacts/frame-editor/entry.d.ts +2 -0
  109. package/dist/artifacts/frame-editor/entry.d.ts.map +1 -0
  110. package/dist/artifacts/frame-editor/frame.d.ts +5 -0
  111. package/dist/artifacts/frame-editor/frame.d.ts.map +1 -0
  112. package/dist/artifacts/frame-editor/keys.d.ts +8 -0
  113. package/dist/artifacts/frame-editor/keys.d.ts.map +1 -0
  114. package/dist/artifacts/frame-editor/protocol.d.ts +186 -0
  115. package/dist/artifacts/frame-editor/protocol.d.ts.map +1 -0
  116. package/dist/artifacts/frame-editor/schema.d.ts +34 -0
  117. package/dist/artifacts/frame-editor/schema.d.ts.map +1 -0
  118. package/dist/artifacts/frame-editor/style.d.ts +2 -0
  119. package/dist/artifacts/frame-editor/style.d.ts.map +1 -0
  120. package/dist/artifacts/frame.d.ts +2 -2
  121. package/dist/artifacts/frame.d.ts.map +1 -1
  122. package/dist/artifacts/live/boards.d.ts +2 -0
  123. package/dist/artifacts/live/boards.d.ts.map +1 -1
  124. package/dist/artifacts/live/document.d.ts +32 -0
  125. package/dist/artifacts/live/document.d.ts.map +1 -0
  126. package/dist/artifacts/live/header.d.ts +4 -4
  127. package/dist/artifacts/live/header.d.ts.map +1 -1
  128. package/dist/artifacts/live/presence.d.ts.map +1 -1
  129. package/dist/artifacts/live/session.d.ts +2 -0
  130. package/dist/artifacts/live/session.d.ts.map +1 -1
  131. package/dist/artifacts/live/view.d.ts +4 -3
  132. package/dist/artifacts/live/view.d.ts.map +1 -1
  133. package/dist/artifacts/messages.d.ts +226 -2
  134. package/dist/artifacts/messages.d.ts.map +1 -1
  135. package/dist/artifacts/pdf.d.ts +8 -0
  136. package/dist/artifacts/pdf.d.ts.map +1 -0
  137. package/dist/artifacts/queries.d.ts +80 -0
  138. package/dist/artifacts/queries.d.ts.map +1 -1
  139. package/dist/artifacts/references/references.d.ts +3 -3
  140. package/dist/artifacts/references/references.d.ts.map +1 -1
  141. package/dist/artifacts/renderers/canvas/board-editor.d.ts.map +1 -1
  142. package/dist/artifacts/renderers/canvas/comment-pins.d.ts +4 -2
  143. package/dist/artifacts/renderers/canvas/comment-pins.d.ts.map +1 -1
  144. package/dist/artifacts/renderers/canvas/comments-panel.d.ts.map +1 -1
  145. package/dist/artifacts/renderers/canvas/comments.d.ts +2 -1
  146. package/dist/artifacts/renderers/canvas/comments.d.ts.map +1 -1
  147. package/dist/artifacts/renderers/designed-document.d.ts +28 -0
  148. package/dist/artifacts/renderers/designed-document.d.ts.map +1 -0
  149. package/dist/artifacts/renderers/document-tree.d.ts +2 -0
  150. package/dist/artifacts/renderers/document-tree.d.ts.map +1 -1
  151. package/dist/artifacts/renderers/document.d.ts +9 -1
  152. package/dist/artifacts/renderers/document.d.ts.map +1 -1
  153. package/dist/artifacts/renderers/live-frame.d.ts +145 -0
  154. package/dist/artifacts/renderers/live-frame.d.ts.map +1 -0
  155. package/dist/artifacts/renderers/page-editor.d.ts +16 -0
  156. package/dist/artifacts/renderers/page-editor.d.ts.map +1 -0
  157. package/dist/artifacts/renderers/page.d.ts +10 -1
  158. package/dist/artifacts/renderers/page.d.ts.map +1 -1
  159. package/dist/artifacts/viewer.d.ts.map +1 -1
  160. package/dist/{chunk-pxd07kvx.js → chunk-096d52h2.js} +1 -1
  161. package/dist/{chunk-yrcm1pbg.js → chunk-94d7enwc.js} +491 -2128
  162. package/dist/chunk-94d7enwc.js.map +27 -0
  163. package/dist/{chunk-970s5nkn.js → chunk-ezntd3yy.js} +1 -1
  164. package/dist/{chunk-kymx9y5e.js → chunk-qjw5r2vk.js} +12729 -5044
  165. package/dist/chunk-qjw5r2vk.js.map +242 -0
  166. package/dist/chunk-y51c4tsz.js +11 -0
  167. package/dist/chunk-y51c4tsz.js.map +10 -0
  168. package/dist/createCustomApp.d.ts +8 -1
  169. package/dist/createCustomApp.d.ts.map +1 -1
  170. package/dist/index.d.ts +4 -2
  171. package/dist/index.d.ts.map +1 -1
  172. package/dist/index.js +13 -6
  173. package/dist/index.js.map +5 -5
  174. package/dist/pages/console/data/accounts.d.ts +94 -0
  175. package/dist/pages/console/data/accounts.d.ts.map +1 -0
  176. package/dist/pages/console/data/connectors.d.ts.map +1 -1
  177. package/dist/pages/console/data/live-updates.d.ts.map +1 -1
  178. package/dist/pages/console/data/messages.d.ts +52 -0
  179. package/dist/pages/console/data/messages.d.ts.map +1 -1
  180. package/dist/pages/console/data/shared.d.ts.map +1 -1
  181. package/dist/pages/console/data/sync-button.d.ts.map +1 -1
  182. package/dist/pages/console/data/syncs.d.ts.map +1 -1
  183. package/dist/pages/settings/usage/breakdown.d.ts.map +1 -1
  184. package/dist/pages/settings/usage/page.d.ts +4 -1
  185. package/dist/pages/settings/usage/page.d.ts.map +1 -1
  186. package/dist/pages/settings/usage/spend-model.d.ts +43 -19
  187. package/dist/pages/settings/usage/spend-model.d.ts.map +1 -1
  188. package/dist/pages.js +23044 -22570
  189. package/dist/pages.js.map +17 -16
  190. package/dist/server.d.ts +1 -1
  191. package/dist/server.d.ts.map +1 -1
  192. package/dist/server.js +40 -3
  193. package/dist/server.js.map +3 -3
  194. package/dist/shell/search-command.d.ts.map +1 -1
  195. package/package.json +22 -5
  196. package/src/agent/activity-label.ts +3 -0
  197. package/src/agent/agent-chat.tsx +58 -39
  198. package/src/agent/agent-execution-trace.tsx +2 -2
  199. package/src/agent/agent-panel.tsx +6 -4
  200. package/src/agent/agent.css +254 -0
  201. package/src/agent/artifact-panel.tsx +77 -22
  202. package/src/agent/components/agent-threads.tsx +42 -2
  203. package/src/agent/components/composer-place.tsx +2 -7
  204. package/src/agent/components/composer-settings.tsx +20 -10
  205. package/src/agent/components/composer.tsx +342 -104
  206. package/src/agent/components/computer-place.tsx +415 -70
  207. package/src/agent/components/context-meter.tsx +7 -3
  208. package/src/agent/components/conversation-panel.tsx +99 -54
  209. package/src/agent/components/message-parts.tsx +367 -59
  210. package/src/agent/components/message-view.tsx +428 -9
  211. package/src/agent/components/pending-messages.tsx +342 -0
  212. package/src/agent/components/sandbox-recovery.tsx +36 -17
  213. package/src/agent/components/threads-button.tsx +3 -2
  214. package/src/agent/components/transcript.tsx +198 -38
  215. package/src/agent/conversation.ts +219 -55
  216. package/src/agent/delegation/interpret.ts +31 -7
  217. package/src/agent/format.ts +52 -0
  218. package/src/agent/hooks/thread-queries.ts +75 -0
  219. package/src/agent/hooks/use-agent-conversation.ts +801 -211
  220. package/src/agent/hooks/use-thread-lists.ts +72 -7
  221. package/src/agent/hooks/use-thread-place.ts +239 -0
  222. package/src/agent/hooks/use-thread-stream.ts +10 -0
  223. package/src/agent/i18n/en.ts +150 -27
  224. package/src/agent/i18n/fr.ts +148 -31
  225. package/src/agent/inbox-handoff.ts +47 -0
  226. package/src/agent/live-run.ts +61 -2
  227. package/src/agent/model-selection.ts +3 -2
  228. package/src/agent/moves.ts +174 -0
  229. package/src/agent/react-router.tsx +2 -2
  230. package/src/agent/search/interpret.ts +367 -0
  231. package/src/agent/search/search-tool-view.tsx +235 -0
  232. package/src/agent/thread-reader-state.ts +75 -0
  233. package/src/agent/thread-state.ts +114 -0
  234. package/src/agent/tool-views/built-in.tsx +16 -0
  235. package/src/agent/tool-views/registry.tsx +28 -2
  236. package/src/agent/types.ts +13 -0
  237. package/src/agent/view-file/view-file-tool-view.tsx +33 -19
  238. package/src/artifacts/artifacts.css +398 -0
  239. package/src/artifacts/card-preview.tsx +6 -1
  240. package/src/artifacts/editor/block-handle.tsx +278 -0
  241. package/src/artifacts/editor/blocks.ts +216 -0
  242. package/src/artifacts/editor/callout-view.tsx +31 -0
  243. package/src/artifacts/editor/document-editor.tsx +484 -0
  244. package/src/artifacts/editor/figure-view.tsx +66 -0
  245. package/src/artifacts/editor/find-blocks.ts +19 -0
  246. package/src/artifacts/editor/markdown-document.tsx +72 -0
  247. package/src/artifacts/editor/messages.ts +4 -0
  248. package/src/artifacts/editor/schema.ts +159 -0
  249. package/src/artifacts/editor/selection-toolbar.tsx +364 -0
  250. package/src/artifacts/editor/slash-menu.tsx +251 -0
  251. package/src/artifacts/editor/upload.ts +40 -0
  252. package/src/artifacts/file-source.ts +11 -0
  253. package/src/artifacts/frame-editor/bundle.ts +8 -0
  254. package/src/artifacts/frame-editor/commands.ts +565 -0
  255. package/src/artifacts/frame-editor/entry.ts +6 -0
  256. package/src/artifacts/frame-editor/frame.ts +847 -0
  257. package/src/artifacts/frame-editor/generated/editor.txt +149 -0
  258. package/src/artifacts/frame-editor/generated.d.ts +5 -0
  259. package/src/artifacts/frame-editor/keys.ts +323 -0
  260. package/src/artifacts/frame-editor/protocol.ts +151 -0
  261. package/src/artifacts/frame-editor/schema.ts +293 -0
  262. package/src/artifacts/frame-editor/style.ts +58 -0
  263. package/src/artifacts/frame.tsx +11 -3
  264. package/src/artifacts/live/boards.ts +46 -3
  265. package/src/artifacts/live/document.ts +89 -0
  266. package/src/artifacts/live/header.tsx +9 -14
  267. package/src/artifacts/live/presence.ts +5 -1
  268. package/src/artifacts/live/session.ts +9 -2
  269. package/src/artifacts/live/view.ts +14 -8
  270. package/src/artifacts/messages.ts +138 -2
  271. package/src/artifacts/pdf.ts +50 -0
  272. package/src/artifacts/references/references.tsx +46 -11
  273. package/src/artifacts/renderers/canvas/board-editor.tsx +11 -144
  274. package/src/artifacts/renderers/canvas/comment-pins.tsx +5 -0
  275. package/src/artifacts/renderers/canvas/comments-panel.tsx +58 -1
  276. package/src/artifacts/renderers/canvas/comments.ts +8 -4
  277. package/src/artifacts/renderers/designed-document.tsx +277 -0
  278. package/src/artifacts/renderers/document-tree.ts +9 -0
  279. package/src/artifacts/renderers/document.tsx +13 -102
  280. package/src/artifacts/renderers/live-frame.ts +163 -0
  281. package/src/artifacts/renderers/page-editor.tsx +613 -0
  282. package/src/artifacts/renderers/page.tsx +21 -4
  283. package/src/artifacts/viewer.tsx +87 -41
  284. package/src/createCustomApp.ts +57 -3
  285. package/src/index.ts +10 -0
  286. package/src/pages/console/data/accounts.tsx +509 -0
  287. package/src/pages/console/data/connectors.tsx +7 -15
  288. package/src/pages/console/data/live-updates.ts +2 -0
  289. package/src/pages/console/data/messages.ts +79 -0
  290. package/src/pages/console/data/projections.tsx +4 -0
  291. package/src/pages/console/data/shared.tsx +1 -0
  292. package/src/pages/console/data/sync-button.tsx +56 -16
  293. package/src/pages/console/data/syncs.tsx +66 -41
  294. package/src/pages/console/ontology/lineage/trail.tsx +3 -0
  295. package/src/pages/section-ui.tsx +1 -1
  296. package/src/pages/settings/usage/breakdown.tsx +5 -2
  297. package/src/pages/settings/usage/filter-menu.tsx +1 -1
  298. package/src/pages/settings/usage/page.tsx +32 -13
  299. package/src/pages/settings/usage/spend-chart.tsx +2 -2
  300. package/src/pages/settings/usage/spend-model.ts +118 -50
  301. package/src/server.ts +1 -0
  302. package/src/shell/search-command.tsx +2 -0
  303. package/dist/chunk-kymx9y5e.js.map +0 -211
  304. package/dist/chunk-yrcm1pbg.js.map +0 -34
  305. /package/dist/{chunk-pxd07kvx.js.map → chunk-096d52h2.js.map} +0 -0
  306. /package/dist/{chunk-970s5nkn.js.map → chunk-ezntd3yy.js.map} +0 -0
@@ -0,0 +1,565 @@
1
+ import { PAGE_MARK_TAGS } from "@sixb/core/artifacts/live"
2
+ import type { Editor } from "@tiptap/core"
3
+ import type { Fragment, Node as ProseMirrorNode, ResolvedPos, Schema } from "@tiptap/pm/model"
4
+ import { NodeSelection, Selection, TextSelection, type Transaction } from "@tiptap/pm/state"
5
+ import type { EditorView } from "@tiptap/pm/view"
6
+ import type { EditorRect, PageBlockId } from "./protocol"
7
+ import { startTag, tagAttributes } from "./schema"
8
+
9
+ // What the editor does to a page's blocks: make one, turn one into another, move, copy and delete
10
+ // them, and find where a dragged one lands. A page is HTML, so what is made is always markup that
11
+ // can stand where it goes: a new block next to a list item goes after the list, not into it.
12
+
13
+ /** Blocks of words where Enter starts a new line rather than a new block: a cell, a caption. */
14
+ export const SINGLE_BLOCK_TAGS: ReadonlySet<string> = new Set([
15
+ "td",
16
+ "th",
17
+ "caption",
18
+ "figcaption",
19
+ "summary",
20
+ "legend",
21
+ "label",
22
+ ...PAGE_MARK_TAGS,
23
+ ])
24
+
25
+ export const LIST_TAGS: ReadonlySet<string> = new Set(["ul", "ol", "menu"])
26
+
27
+ /** Elements only some elements may stand in: a new block goes after them, not into them. */
28
+ const HOLDERS: ReadonlySet<string> = new Set([
29
+ ...LIST_TAGS,
30
+ "dl",
31
+ "table",
32
+ "thead",
33
+ "tbody",
34
+ "tfoot",
35
+ "tr",
36
+ ])
37
+
38
+ const HEADING_LEVEL: Readonly<Record<string, string>> = {
39
+ heading1: "h1",
40
+ heading2: "h2",
41
+ heading3: "h3",
42
+ }
43
+
44
+ /** The block of words the selection is in, and where it starts. */
45
+ export function wordsAt(selection: Selection): { node: ProseMirrorNode; pos: number } | null {
46
+ const { $from } = selection
47
+ if ($from.parent.type.name !== "textBlock" || $from.depth === 0) return null
48
+ return { node: $from.parent, pos: $from.before() }
49
+ }
50
+
51
+ export function tagOf(node: ProseMirrorNode | null | undefined): string {
52
+ return typeof node?.attrs.tag === "string" ? node.attrs.tag : ""
53
+ }
54
+
55
+ /** A block of words, made by the editor: spaced as the block before it when written back. */
56
+ export function words(
57
+ schema: Schema,
58
+ tag: string,
59
+ open?: string,
60
+ content?: Fragment | null
61
+ ): ProseMirrorNode {
62
+ return schema.nodes.textBlock.create(
63
+ { tag, open: open ?? `<${tag}>`, close: `</${tag}>` },
64
+ content ?? null
65
+ )
66
+ }
67
+
68
+ export function box(
69
+ schema: Schema,
70
+ tag: string,
71
+ children: readonly ProseMirrorNode[],
72
+ open?: string
73
+ ): ProseMirrorNode {
74
+ return schema.nodes.box.create({ tag, open: open ?? `<${tag}>`, close: `</${tag}>` }, children)
75
+ }
76
+
77
+ function raw(schema: Schema, source: string): ProseMirrorNode {
78
+ return schema.nodes.raw.create({ source })
79
+ }
80
+
81
+ /** A start tag without its `id`, which a copy must not repeat. */
82
+ export function withoutId(open: string, tag: string): string {
83
+ const attrs = { ...tagAttributes(open) }
84
+ if (!("id" in attrs)) return open
85
+ delete attrs.id
86
+ return startTag(tag, attrs)
87
+ }
88
+
89
+ /**
90
+ * The start tag the page wrote for `tag` nearest before `pos`, else first after it, so a new
91
+ * heading or table looks like the page's others; a plain one where it wrote none.
92
+ */
93
+ export function likeOpen(doc: ProseMirrorNode, pos: number, tag: string): string {
94
+ let before: string | null = null
95
+ let after: string | null = null
96
+ doc.descendants((node, at) => {
97
+ if (tagOf(node) !== tag || typeof node.attrs.open !== "string") return true
98
+ if (at < pos) before = node.attrs.open
99
+ else after ??= node.attrs.open
100
+ return true
101
+ })
102
+ const found = before ?? after
103
+ return found ? withoutId(found, tag) : `<${tag}>`
104
+ }
105
+
106
+ /** A new block of the kind `id`, as it is made where `pos` is. */
107
+ export function makeBlock(
108
+ schema: Schema,
109
+ doc: ProseMirrorNode,
110
+ pos: number,
111
+ id: PageBlockId
112
+ ): ProseMirrorNode | null {
113
+ const heading = HEADING_LEVEL[id]
114
+ if (heading) return words(schema, heading, likeOpen(doc, pos, heading))
115
+ switch (id) {
116
+ case "paragraph":
117
+ return words(schema, "p")
118
+ case "quote":
119
+ return words(schema, "blockquote")
120
+ case "bulletList":
121
+ return box(schema, "ul", [words(schema, "li")])
122
+ case "orderedList":
123
+ return box(schema, "ol", [words(schema, "li")])
124
+ case "table": {
125
+ const row = (cell: string) =>
126
+ box(
127
+ schema,
128
+ "tr",
129
+ [1, 2, 3].map(() => words(schema, cell))
130
+ )
131
+ return box(
132
+ schema,
133
+ "table",
134
+ [box(schema, "thead", [row("th")]), box(schema, "tbody", [row("td"), row("td")])],
135
+ likeOpen(doc, pos, "table")
136
+ )
137
+ }
138
+ case "divider":
139
+ return raw(schema, "<hr>")
140
+ case "pageBreak":
141
+ return raw(schema, '<div style="break-after: page"></div>')
142
+ default:
143
+ return null
144
+ }
145
+ }
146
+
147
+ /**
148
+ * Where a block goes after the one at `pos`: right after it, or after the list or table it is
149
+ * part of, where a block of any kind may stand.
150
+ */
151
+ export function placeAfter(doc: ProseMirrorNode, pos: number): number {
152
+ const node = doc.nodeAt(pos)
153
+ const $pos = doc.resolve(pos)
154
+ let at = pos + (node?.nodeSize ?? 0)
155
+ for (let depth = $pos.depth; depth > 0; depth -= 1) {
156
+ if (HOLDERS.has(tagOf($pos.node(depth)))) at = $pos.after(depth)
157
+ }
158
+ return at
159
+ }
160
+
161
+ /**
162
+ * Puts `made` where the block of words at `pos` is: in its place when it is empty and may be
163
+ * replaced, else after it. Returns where `made` starts.
164
+ */
165
+ export function placeBlock(tr: Transaction, pos: number, made: ProseMirrorNode): number {
166
+ const node = tr.doc.nodeAt(pos)
167
+ const after = placeAfter(tr.doc, pos)
168
+ const replaceable =
169
+ node?.type.name === "textBlock" &&
170
+ node.content.size === 0 &&
171
+ after === pos + node.nodeSize &&
172
+ !SINGLE_BLOCK_TAGS.has(tagOf(node))
173
+ if (node && replaceable) {
174
+ tr.replaceWith(pos, pos + node.nodeSize, made)
175
+ return pos
176
+ }
177
+ tr.insert(after, made)
178
+ return after
179
+ }
180
+
181
+ /** Puts the caret in the first words of the block at `at`, or on a new paragraph after it. */
182
+ export function caretInto(tr: Transaction, at: number): void {
183
+ const made = tr.doc.nodeAt(at)
184
+ if (!made) return
185
+ if (!made.isAtom) {
186
+ tr.setSelection(TextSelection.near(tr.doc.resolve(at + 1)))
187
+ return
188
+ }
189
+ const after = at + made.nodeSize
190
+ if (tr.doc.resolve(after).nodeAfter?.type.name !== "textBlock") {
191
+ tr.insert(after, words(tr.doc.type.schema, "p"))
192
+ }
193
+ tr.setSelection(TextSelection.create(tr.doc, after + 1))
194
+ }
195
+
196
+ /** Makes a block of the kind `id` where the caret is, in place of `range`: what `/` typed. */
197
+ export function insertBlock(
198
+ editor: Editor,
199
+ id: PageBlockId,
200
+ range?: { readonly from: number; readonly to: number }
201
+ ): boolean {
202
+ const tr = editor.state.tr
203
+ if (range) tr.delete(range.from, range.to)
204
+ const at = wordsAt(tr.selection)
205
+ if (!at) return false
206
+ const made = makeBlock(editor.schema, tr.doc, at.pos, id)
207
+ if (!made) return false
208
+ caretInto(tr, placeBlock(tr, at.pos, made))
209
+ editor.view.dispatch(tr.scrollIntoView())
210
+ editor.view.focus()
211
+ return true
212
+ }
213
+
214
+ /**
215
+ * Turns the block of words at `pos`, or the one the caret is in, into a block of the kind `id`,
216
+ * keeping its words: a paragraph into a heading, a list item out of its list into a paragraph.
217
+ */
218
+ export function turnInto(editor: Editor, id: PageBlockId, pos?: number): boolean {
219
+ const { state } = editor
220
+ const target =
221
+ pos === undefined
222
+ ? wordsAt(state.selection)
223
+ : { node: state.doc.nodeAt(pos) as ProseMirrorNode, pos }
224
+ if (!target || !turnable(state.doc, target.pos)) return false
225
+ const { node } = target
226
+ const $pos = state.doc.resolve(target.pos)
227
+ const parent = $pos.parent
228
+ const inList = tagOf(node) === "li" && LIST_TAGS.has(tagOf(parent))
229
+ const tr = state.tr
230
+ // The caret stays where it was among the block's words, wherever the block goes.
231
+ const offset = Math.min(Math.max(state.selection.from - target.pos - 1, 0), node.content.size)
232
+ let at = target.pos
233
+
234
+ if (id === "bulletList" || id === "orderedList") {
235
+ const list = id === "bulletList" ? "ul" : "ol"
236
+ if (inList) {
237
+ if (tagOf(parent) === list) return true
238
+ tr.setNodeMarkup($pos.before(), undefined, {
239
+ ...parent.attrs,
240
+ tag: list,
241
+ open: `<${list}>`,
242
+ close: `</${list}>`,
243
+ })
244
+ } else {
245
+ tr.replaceWith(
246
+ target.pos,
247
+ target.pos + node.nodeSize,
248
+ box(editor.schema, list, [words(editor.schema, "li", undefined, node.content)])
249
+ )
250
+ at = target.pos + 1
251
+ }
252
+ } else {
253
+ const tag =
254
+ HEADING_LEVEL[id] ?? (id === "paragraph" ? "p" : id === "quote" ? "blockquote" : null)
255
+ if (!tag) return false
256
+ const open = HEADING_LEVEL[id] ? likeOpen(state.doc, target.pos, tag) : `<${tag}>`
257
+ if (inList) {
258
+ at = liftOut(tr, target.pos, words(editor.schema, tag, open, node.content))
259
+ } else {
260
+ tr.setNodeMarkup(target.pos, undefined, { ...node.attrs, tag, open, close: `</${tag}>` })
261
+ }
262
+ }
263
+ tr.setSelection(TextSelection.create(tr.doc, at + 1 + offset))
264
+ editor.view.dispatch(tr.scrollIntoView())
265
+ editor.view.focus()
266
+ return true
267
+ }
268
+
269
+ /**
270
+ * Whether the block at `pos` may be turned into another kind: a block of words that stands where
271
+ * any block may. A cell, a caption, or words in a table's row cannot: a heading there is not HTML.
272
+ */
273
+ export function turnable(doc: ProseMirrorNode, pos: number): boolean {
274
+ const node = doc.nodeAt(pos)
275
+ if (node?.type.name !== "textBlock" || SINGLE_BLOCK_TAGS.has(tagOf(node))) return false
276
+ const $pos = doc.resolve(pos)
277
+ for (let depth = $pos.depth; depth > 0; depth -= 1) {
278
+ const tag = tagOf($pos.node(depth))
279
+ if (tag === "table" || tag === "tr" || tag === "thead" || tag === "tbody" || tag === "tfoot") {
280
+ return false
281
+ }
282
+ }
283
+ return true
284
+ }
285
+
286
+ /**
287
+ * Takes the list item at `pos` out of its list as `replacement`: before the list when it is the
288
+ * first item, after it when the last, and between the two halves of the list otherwise. The caret
289
+ * goes into it. Returns where it starts.
290
+ */
291
+ export function liftOut(tr: Transaction, pos: number, replacement: ProseMirrorNode): number {
292
+ const item = tr.doc.nodeAt(pos)
293
+ if (!item) return pos
294
+ const $pos = tr.doc.resolve(pos)
295
+ const list = $pos.parent
296
+ const listPos = $pos.before()
297
+ const index = $pos.index()
298
+ let at: number
299
+ if (list.childCount === 1) {
300
+ tr.replaceWith(listPos, listPos + list.nodeSize, replacement)
301
+ at = listPos
302
+ } else if (index === 0) {
303
+ tr.delete(pos, pos + item.nodeSize)
304
+ tr.insert(listPos, replacement)
305
+ at = listPos
306
+ } else if (index === list.childCount - 1) {
307
+ tr.delete(pos, pos + item.nodeSize)
308
+ at = tr.mapping.map(listPos + list.nodeSize)
309
+ tr.insert(at, replacement)
310
+ } else {
311
+ tr.delete(pos, pos + item.nodeSize)
312
+ tr.split(pos, 1)
313
+ at = pos + 1
314
+ tr.insert(at, replacement)
315
+ }
316
+ tr.setSelection(TextSelection.near(tr.doc.resolve(at + 1)))
317
+ return at
318
+ }
319
+
320
+ /** A copy of the block at `pos` right after it, without the ids that would repeat. */
321
+ export function duplicate(editor: Editor, pos: number): boolean {
322
+ const node = editor.state.doc.nodeAt(pos)
323
+ if (!node) return false
324
+ const tag = tagOf(node)
325
+ // A cell or a caption is one of its kind where it stands: a second would break its table.
326
+ if (SINGLE_BLOCK_TAGS.has(tag)) return false
327
+ const open = node.attrs.open
328
+ const copy =
329
+ typeof open === "string" && tag
330
+ ? node.type.create({ ...node.attrs, open: withoutId(open, tag) }, node.content, node.marks)
331
+ : node
332
+ const at = pos + node.nodeSize
333
+ const tr = editor.state.tr.insert(at, copy)
334
+ tr.setSelection(
335
+ copy.isAtom
336
+ ? NodeSelection.create(tr.doc, at)
337
+ : Selection.near(tr.doc.resolve(at + copy.nodeSize - 1), -1)
338
+ )
339
+ editor.view.dispatch(tr.scrollIntoView())
340
+ editor.view.focus()
341
+ return true
342
+ }
343
+
344
+ /** Deletes the block at `pos`, and a box it leaves empty with it. */
345
+ export function remove(editor: Editor, pos: number): boolean {
346
+ const { doc } = editor.state
347
+ let from = pos
348
+ let node = doc.nodeAt(pos)
349
+ if (!node) return false
350
+ // A box holds a block at least: one left empty goes too.
351
+ let $pos = doc.resolve(from)
352
+ while ($pos.depth > 0 && $pos.parent.childCount === 1) {
353
+ from = $pos.before()
354
+ node = $pos.parent
355
+ $pos = doc.resolve(from)
356
+ }
357
+ const tr = editor.state.tr
358
+ if ($pos.depth === 0 && doc.childCount === 1) {
359
+ tr.replaceWith(0, doc.content.size, words(editor.schema, "p"))
360
+ } else {
361
+ tr.delete(from, from + node.nodeSize)
362
+ }
363
+ tr.setSelection(Selection.near(tr.doc.resolve(Math.min(from, tr.doc.content.size)), -1))
364
+ editor.view.dispatch(tr.scrollIntoView())
365
+ editor.view.focus()
366
+ return true
367
+ }
368
+
369
+ /** Moves the block at `pos` past its sibling before (`-1`) or after (`1`) it. Returns where it is. */
370
+ export function move(editor: Editor, pos: number, by: -1 | 1): number | null {
371
+ const { doc } = editor.state
372
+ const node = doc.nodeAt(pos)
373
+ if (!node) return null
374
+ const $pos = doc.resolve(pos)
375
+ const sibling = $pos.parent.maybeChild($pos.index() + by)
376
+ if (!sibling) return null
377
+ const offset = editor.state.selection.from - pos
378
+ const target = by === 1 ? pos + sibling.nodeSize : pos - sibling.nodeSize
379
+ const tr = editor.state.tr.delete(pos, pos + node.nodeSize).insert(target, node)
380
+ const inside = target + Math.min(Math.max(offset, 0), node.nodeSize)
381
+ tr.setSelection(
382
+ node.isAtom ? NodeSelection.create(tr.doc, target) : Selection.near(tr.doc.resolve(inside))
383
+ )
384
+ editor.view.dispatch(tr.scrollIntoView())
385
+ editor.view.focus()
386
+ return target
387
+ }
388
+
389
+ /**
390
+ * The block the pointer is beside: the innermost block under it, or, between blocks, the nearer
391
+ * of the two; never the box around them, whose handle would be far from the pointer. In the margin
392
+ * beside a block, where its handle is, the block on that line. A table is pointed at whole.
393
+ */
394
+ export function blockAt(
395
+ view: EditorView,
396
+ x: number,
397
+ y: number
398
+ ): { node: ProseMirrorNode; pos: number } | null {
399
+ const under = blockUnder(view, x, y)
400
+ if (under?.node.type.name !== "box" || tagOf(under.node) === "table") return under
401
+ // Beside the blocks, over a box's padding or the page's margin: the block on this line.
402
+ for (let probe = x + BESIDE_STEP; probe <= x + BESIDE_REACH; probe += BESIDE_STEP) {
403
+ const beside = blockUnder(view, probe, y)
404
+ if (beside && (beside.node.type.name !== "box" || tagOf(beside.node) === "table")) {
405
+ const rect = rectOf(view.nodeDOM(beside.pos))
406
+ if (rect && y >= rect.top && y <= rect.bottom) return beside
407
+ }
408
+ }
409
+ return under
410
+ }
411
+
412
+ /** How far right of the pointer, and by what steps, a block beside it is looked for. */
413
+ const BESIDE_REACH = 240
414
+ const BESIDE_STEP = 12
415
+
416
+ function blockUnder(
417
+ view: EditorView,
418
+ x: number,
419
+ y: number
420
+ ): { node: ProseMirrorNode; pos: number } | null {
421
+ const found = view.posAtCoords({ left: x, top: y })
422
+ if (!found) return null
423
+ const { doc } = view.state
424
+ let pos = found.inside
425
+ let node = pos >= 0 ? doc.nodeAt(pos) : null
426
+ if (!node || node.isInline) {
427
+ const $pos = doc.resolve(found.pos)
428
+ if ($pos.parent.isTextblock) {
429
+ pos = $pos.before($pos.depth)
430
+ node = $pos.parent
431
+ } else {
432
+ // Between blocks: the nearer of the one before and the one after.
433
+ const before = $pos.nodeBefore
434
+ const after = $pos.nodeAfter
435
+ const distance = (child: ProseMirrorNode | null, at: number) => {
436
+ if (!child) return Number.POSITIVE_INFINITY
437
+ const rect = rectOf(view.nodeDOM(at))
438
+ if (!rect) return Number.POSITIVE_INFINITY
439
+ return y < rect.top ? rect.top - y : y > rect.bottom ? y - rect.bottom : 0
440
+ }
441
+ const beforePos = before ? found.pos - before.nodeSize : -1
442
+ const nearer =
443
+ distance(before, beforePos) <= distance(after, found.pos)
444
+ ? { node: before, pos: beforePos }
445
+ : { node: after, pos: found.pos }
446
+ if (nearer.node) {
447
+ node = nearer.node
448
+ pos = nearer.pos
449
+ } else if ($pos.depth > 0) {
450
+ pos = $pos.before($pos.depth)
451
+ node = $pos.parent
452
+ } else {
453
+ return null
454
+ }
455
+ }
456
+ }
457
+ // The outermost table around it, if any.
458
+ const $pos = doc.resolve(pos)
459
+ for (let depth = 1; depth <= $pos.depth; depth += 1) {
460
+ if (tagOf($pos.node(depth)) === "table")
461
+ return { node: $pos.node(depth), pos: $pos.before(depth) }
462
+ }
463
+ return node ? { node, pos } : null
464
+ }
465
+
466
+ /** What a node draws as, measured: a node drawn through its children is as big as they are. */
467
+ export function rectOf(dom: globalThis.Node | null | undefined): EditorRect | null {
468
+ if (!(dom instanceof Element)) return null
469
+ const own = dom.getBoundingClientRect()
470
+ if (own.width > 0 || own.height > 0) {
471
+ return { left: own.left, top: own.top, right: own.right, bottom: own.bottom }
472
+ }
473
+ let rect: EditorRect | null = null
474
+ for (const child of Array.from(dom.children)) {
475
+ const inner = rectOf(child)
476
+ if (!inner) continue
477
+ rect = rect
478
+ ? {
479
+ left: Math.min(rect.left, inner.left),
480
+ top: Math.min(rect.top, inner.top),
481
+ right: Math.max(rect.right, inner.right),
482
+ bottom: Math.max(rect.bottom, inner.bottom),
483
+ }
484
+ : inner
485
+ }
486
+ return rect
487
+ }
488
+
489
+ /**
490
+ * Where the block at `dragged` would land, dropped at a point: before or after the block there,
491
+ * whichever half of it the point is in. A list item lands among list items; anything else beside
492
+ * a list, not in it. Null where it cannot land: on itself, or inside itself.
493
+ */
494
+ export function dropTarget(
495
+ view: EditorView,
496
+ dragged: number,
497
+ x: number,
498
+ y: number
499
+ ): { at: number; line: EditorRect } | null {
500
+ const node = view.state.doc.nodeAt(dragged)
501
+ let target = blockAt(view, x, y)
502
+ if (!node || !target) return null
503
+ const item = (each: ProseMirrorNode) => tagOf(each) === "li"
504
+ if (item(node) && !item(target.node)) return null
505
+ if (!item(node) && item(target.node)) {
506
+ const $pos = view.state.doc.resolve(target.pos)
507
+ target = { node: $pos.parent, pos: $pos.before() }
508
+ }
509
+ if (target.pos >= dragged && target.pos < dragged + node.nodeSize) return null
510
+ const rect = rectOf(view.nodeDOM(target.pos))
511
+ if (!rect) return null
512
+ const before = y < (rect.top + rect.bottom) / 2
513
+ const edge = before ? rect.top : rect.bottom
514
+ return {
515
+ at: before ? target.pos : target.pos + target.node.nodeSize,
516
+ line: { left: rect.left, right: rect.right, top: edge, bottom: edge },
517
+ }
518
+ }
519
+
520
+ /**
521
+ * Moves the block at `dragged` to `at`, as a drop does. A box it was all of goes with it, as
522
+ * `remove` takes it: a list whose only item moves into another list leaves no empty list behind.
523
+ */
524
+ export function moveTo(editor: Editor, dragged: number, at: number): boolean {
525
+ const { doc } = editor.state
526
+ const node = doc.nodeAt(dragged)
527
+ if (!node || at === dragged || at === dragged + node.nodeSize) return false
528
+ let from = dragged
529
+ let size = node.nodeSize
530
+ let $pos = doc.resolve(from)
531
+ while ($pos.depth > 0 && $pos.parent.childCount === 1) {
532
+ from = $pos.before()
533
+ size = $pos.parent.nodeSize
534
+ $pos = doc.resolve(from)
535
+ }
536
+ if (at > from && at < from + size) return false
537
+ const tr = editor.state.tr.delete(from, from + size)
538
+ const target = tr.mapping.map(at)
539
+ tr.insert(target, node)
540
+ tr.setSelection(
541
+ node.isAtom
542
+ ? NodeSelection.create(tr.doc, target)
543
+ : Selection.near(tr.doc.resolve(target + node.nodeSize - 1), -1)
544
+ )
545
+ editor.view.dispatch(tr.scrollIntoView())
546
+ editor.view.focus()
547
+ return true
548
+ }
549
+
550
+ /** The kind of block the block of words at `$pos` is, as "Turn into" names it. */
551
+ export function blockKind($pos: ResolvedPos): PageBlockId | null {
552
+ const node = $pos.parent
553
+ if (node.type.name !== "textBlock") return null
554
+ const tag = tagOf(node)
555
+ if (tag === "p") return "paragraph"
556
+ if (tag === "h1") return "heading1"
557
+ if (tag === "h2") return "heading2"
558
+ if (tag === "h3") return "heading3"
559
+ if (tag === "blockquote") return "quote"
560
+ if (tag === "li") {
561
+ const list = $pos.depth > 1 ? tagOf($pos.node($pos.depth - 1)) : ""
562
+ return list === "ol" ? "orderedList" : list === "ul" ? "bulletList" : null
563
+ }
564
+ return null
565
+ }
@@ -0,0 +1,6 @@
1
+ import { startFrameEditor } from "./frame"
2
+ import { FRAME_EDITOR_GLOBAL } from "./protocol"
3
+
4
+ // The editor's script, as the frame's runtime runs it: it leaves its start where the runtime takes
5
+ // it at once, before anything else can.
6
+ ;(globalThis as Record<string, unknown>)[FRAME_EDITOR_GLOBAL] = startFrameEditor