@aionbuilders/nabu 0.1.0-beta.0 → 0.1.0

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/CHANGELOG.md +523 -0
  2. package/LICENSE +21 -0
  3. package/README.md +72 -341
  4. package/dist/behaviors/text/RichText.svelte +24 -4
  5. package/dist/behaviors/text/markdown-shortcuts.extension.d.ts +3 -0
  6. package/dist/behaviors/text/markdown-shortcuts.extension.js +35 -10
  7. package/dist/behaviors/text/markdown-syntax.d.ts +42 -0
  8. package/dist/behaviors/text/markdown-syntax.js +81 -0
  9. package/dist/behaviors/text/rich-text.extension.d.ts +3 -0
  10. package/dist/behaviors/text/rich-text.extension.js +95 -38
  11. package/dist/behaviors/text/text.behavior.svelte.d.ts +199 -48
  12. package/dist/behaviors/text/text.behavior.svelte.js +549 -83
  13. package/dist/behaviors/text/typography.extension.d.ts +2 -1
  14. package/dist/behaviors/text/typography.extension.js +3 -2
  15. package/dist/blocks/BlockList.svelte +48 -0
  16. package/dist/blocks/BlockList.svelte.d.ts +17 -0
  17. package/dist/blocks/Nabu.svelte +169 -25
  18. package/dist/blocks/Nabu.svelte.d.ts +13 -0
  19. package/dist/blocks/block.extension.d.ts +3 -0
  20. package/dist/blocks/block.extension.js +4 -7
  21. package/dist/blocks/block.svelte.d.ts +350 -86
  22. package/dist/blocks/block.svelte.js +370 -59
  23. package/dist/blocks/blockquote/Blockquote.svelte +1 -2
  24. package/dist/blocks/blockquote/blockquote.extension.d.ts +4 -1
  25. package/dist/blocks/blockquote/blockquote.extension.js +14 -5
  26. package/dist/blocks/blockquote/blockquote.svelte.d.ts +19 -11
  27. package/dist/blocks/blockquote/blockquote.svelte.js +58 -13
  28. package/dist/blocks/callout/Callout.svelte +6 -8
  29. package/dist/blocks/callout/callout.extension.d.ts +4 -1
  30. package/dist/blocks/callout/callout.extension.js +19 -3
  31. package/dist/blocks/callout/callout.svelte.d.ts +5 -3
  32. package/dist/blocks/callout/callout.svelte.js +11 -6
  33. package/dist/blocks/chunks.d.ts +49 -0
  34. package/dist/blocks/chunks.js +90 -0
  35. package/dist/blocks/code/Code.svelte +7 -8
  36. package/dist/blocks/code/code.extension.d.ts +4 -1
  37. package/dist/blocks/code/code.extension.js +22 -2
  38. package/dist/blocks/code/code.svelte.d.ts +9 -2
  39. package/dist/blocks/code/code.svelte.js +49 -25
  40. package/dist/blocks/container.utils.d.ts +85 -13
  41. package/dist/blocks/container.utils.js +338 -178
  42. package/dist/blocks/dialogue/Dialogue.svelte +8 -4
  43. package/dist/blocks/dialogue/dialogue.svelte.d.ts +33 -2
  44. package/dist/blocks/dialogue/dialogue.svelte.js +58 -4
  45. package/dist/blocks/dialogue/hooks/onBeforeInput.hook.d.ts +2 -2
  46. package/dist/blocks/dialogue/hooks/onBeforeInput.hook.js +6 -4
  47. package/dist/blocks/dialogue/index.d.ts +12 -1
  48. package/dist/blocks/dialogue/index.js +51 -35
  49. package/dist/blocks/divider/divider.extension.d.ts +23 -1
  50. package/dist/blocks/divider/divider.extension.js +85 -19
  51. package/dist/blocks/divider/divider.svelte.d.ts +7 -2
  52. package/dist/blocks/divider/divider.svelte.js +9 -4
  53. package/dist/blocks/file/File.svelte +13 -6
  54. package/dist/blocks/file/file.extension.d.ts +4 -1
  55. package/dist/blocks/file/file.extension.js +9 -1
  56. package/dist/blocks/file/file.svelte.d.ts +3 -3
  57. package/dist/blocks/file/file.svelte.js +20 -14
  58. package/dist/blocks/heading/Heading.svelte +1 -2
  59. package/dist/blocks/heading/heading.svelte.d.ts +23 -11
  60. package/dist/blocks/heading/heading.svelte.js +68 -13
  61. package/dist/blocks/heading/hooks/onBeforeInput.hook.js +7 -5
  62. package/dist/blocks/heading/index.d.ts +2 -2
  63. package/dist/blocks/heading/index.js +33 -8
  64. package/dist/blocks/image/Image.svelte +5 -2
  65. package/dist/blocks/image/image.extension.d.ts +4 -1
  66. package/dist/blocks/image/image.extension.js +9 -1
  67. package/dist/blocks/image/image.svelte.d.ts +17 -6
  68. package/dist/blocks/image/image.svelte.js +33 -11
  69. package/dist/blocks/index.d.ts +1 -1
  70. package/dist/blocks/index.js +1 -1
  71. package/dist/blocks/list/List.svelte +1 -0
  72. package/dist/blocks/list/ListItem.svelte +1 -0
  73. package/dist/blocks/list/convert.d.ts +20 -0
  74. package/dist/blocks/list/convert.js +92 -0
  75. package/dist/blocks/list/hooks/onBeforeInput.hook.js +49 -28
  76. package/dist/blocks/list/html-flat.d.ts +78 -0
  77. package/dist/blocks/list/html-flat.js +244 -0
  78. package/dist/blocks/list/indent.d.ts +86 -0
  79. package/dist/blocks/list/indent.js +205 -0
  80. package/dist/blocks/list/index.d.ts +6 -3
  81. package/dist/blocks/list/index.js +42 -31
  82. package/dist/blocks/list/list-item.svelte.d.ts +31 -17
  83. package/dist/blocks/list/list-item.svelte.js +99 -100
  84. package/dist/blocks/list/list.behavior.svelte.d.ts +4 -1
  85. package/dist/blocks/list/list.behavior.svelte.js +6 -1
  86. package/dist/blocks/list/list.svelte.d.ts +89 -18
  87. package/dist/blocks/list/list.svelte.js +129 -125
  88. package/dist/blocks/list/markdown.d.ts +149 -0
  89. package/dist/blocks/list/markdown.js +442 -0
  90. package/dist/blocks/list/normalize.d.ts +11 -0
  91. package/dist/blocks/list/normalize.js +396 -0
  92. package/dist/blocks/list/paste.d.ts +45 -0
  93. package/dist/blocks/list/paste.js +242 -0
  94. package/dist/blocks/list/step-back.d.ts +24 -0
  95. package/dist/blocks/list/step-back.js +87 -0
  96. package/dist/blocks/list/to-list.d.ts +7 -0
  97. package/dist/blocks/list/to-list.js +44 -0
  98. package/dist/blocks/list/todos-toggle.d.ts +20 -0
  99. package/dist/blocks/list/todos-toggle.js +48 -0
  100. package/dist/blocks/media/markdown.d.ts +12 -0
  101. package/dist/blocks/media/markdown.js +26 -0
  102. package/dist/blocks/media/media-block.svelte.d.ts +77 -2
  103. package/dist/blocks/media/media-block.svelte.js +124 -12
  104. package/dist/blocks/media/media.helpers.d.ts +6 -1
  105. package/dist/blocks/media/media.helpers.js +43 -6
  106. package/dist/blocks/megablock.svelte.d.ts +50 -19
  107. package/dist/blocks/megablock.svelte.js +114 -74
  108. package/dist/blocks/move.d.ts +23 -0
  109. package/dist/blocks/move.js +92 -0
  110. package/dist/blocks/nabu.svelte.d.ts +764 -71
  111. package/dist/blocks/nabu.svelte.js +1746 -189
  112. package/dist/blocks/paragraph/Paragraph.svelte +1 -2
  113. package/dist/blocks/paragraph/index.d.ts +2 -2
  114. package/dist/blocks/paragraph/index.js +35 -13
  115. package/dist/blocks/paragraph/paragraph.svelte.d.ts +46 -14
  116. package/dist/blocks/paragraph/paragraph.svelte.js +77 -16
  117. package/dist/blocks/paragraph/table.html.d.ts +9 -0
  118. package/dist/blocks/paragraph/table.html.js +111 -0
  119. package/dist/blocks/paragraph/table.markdown.d.ts +32 -0
  120. package/dist/blocks/paragraph/table.markdown.js +90 -0
  121. package/dist/blocks/region/Region.svelte +264 -0
  122. package/dist/blocks/region/Region.svelte.d.ts +11 -0
  123. package/dist/blocks/region/actions.d.ts +135 -0
  124. package/dist/blocks/region/actions.js +402 -0
  125. package/dist/blocks/region/close.d.ts +35 -0
  126. package/dist/blocks/region/close.js +130 -0
  127. package/dist/blocks/region/commands.d.ts +6 -0
  128. package/dist/blocks/region/commands.js +277 -0
  129. package/dist/blocks/region/create.d.ts +72 -0
  130. package/dist/blocks/region/create.js +208 -0
  131. package/dist/blocks/region/end-marker.d.ts +35 -0
  132. package/dist/blocks/region/end-marker.js +51 -0
  133. package/dist/blocks/region/facts.d.ts +69 -0
  134. package/dist/blocks/region/facts.js +152 -0
  135. package/dist/blocks/region/format.d.ts +91 -0
  136. package/dist/blocks/region/format.js +260 -0
  137. package/dist/blocks/region/index.d.ts +14 -0
  138. package/dist/blocks/region/index.js +14 -0
  139. package/dist/blocks/region/insert.d.ts +59 -0
  140. package/dist/blocks/region/insert.js +102 -0
  141. package/dist/blocks/region/messages.d.ts +254 -0
  142. package/dist/blocks/region/messages.js +131 -0
  143. package/dist/blocks/region/normalize.d.ts +7 -0
  144. package/dist/blocks/region/normalize.js +170 -0
  145. package/dist/blocks/region/region.extension.d.ts +58 -0
  146. package/dist/blocks/region/region.extension.js +119 -0
  147. package/dist/blocks/region/region.svelte.d.ts +221 -0
  148. package/dist/blocks/region/region.svelte.js +582 -0
  149. package/dist/blocks/region/regions.svelte.d.ts +141 -0
  150. package/dist/blocks/region/regions.svelte.js +276 -0
  151. package/dist/blocks/region/serialize.d.ts +57 -0
  152. package/dist/blocks/region/serialize.js +112 -0
  153. package/dist/blocks/region/types.d.ts +255 -0
  154. package/dist/blocks/region/types.js +204 -0
  155. package/dist/blocks/selection.svelte.d.ts +106 -8
  156. package/dist/blocks/selection.svelte.js +622 -74
  157. package/dist/blocks/subthread.d.ts +80 -0
  158. package/dist/blocks/subthread.js +167 -0
  159. package/dist/blocks/text-block.svelte.d.ts +34 -13
  160. package/dist/blocks/text-block.svelte.js +42 -8
  161. package/dist/blocks/toggle/Toggle.svelte +4 -15
  162. package/dist/blocks/toggle/markdown.d.ts +20 -0
  163. package/dist/blocks/toggle/markdown.js +112 -0
  164. package/dist/blocks/toggle/toggle.extension.d.ts +4 -1
  165. package/dist/blocks/toggle/toggle.extension.js +6 -0
  166. package/dist/blocks/toggle/toggle.svelte.d.ts +54 -7
  167. package/dist/blocks/toggle/toggle.svelte.js +134 -34
  168. package/dist/blocks/undo-selection.d.ts +175 -0
  169. package/dist/blocks/undo-selection.js +360 -0
  170. package/dist/blocks/video/Video.svelte +5 -2
  171. package/dist/blocks/video/video.extension.d.ts +4 -1
  172. package/dist/blocks/video/video.extension.js +9 -1
  173. package/dist/blocks/video/video.svelte.d.ts +6 -5
  174. package/dist/blocks/video/video.svelte.js +18 -9
  175. package/dist/extend.d.ts +55 -0
  176. package/dist/extend.js +88 -0
  177. package/dist/files/assets.clipboard.d.ts +77 -0
  178. package/dist/files/assets.clipboard.js +152 -0
  179. package/dist/files/files.svelte.d.ts +321 -19
  180. package/dist/files/files.svelte.js +847 -43
  181. package/dist/files/index.d.ts +3 -1
  182. package/dist/files/index.js +3 -1
  183. package/dist/files/outbox.d.ts +69 -0
  184. package/dist/files/outbox.js +48 -0
  185. package/dist/files/status.d.ts +65 -0
  186. package/dist/files/status.js +87 -0
  187. package/dist/files/store.d.ts +97 -0
  188. package/dist/files/store.js +65 -0
  189. package/dist/files/uploads.svelte.d.ts +174 -0
  190. package/dist/files/uploads.svelte.js +504 -0
  191. package/dist/index.d.ts +68 -9
  192. package/dist/index.js +117 -10
  193. package/dist/nabu-theme.css +21 -1
  194. package/dist/paste/html.paste.d.ts +74 -3
  195. package/dist/paste/html.paste.js +550 -86
  196. package/dist/paste/markdown-inline.d.ts +86 -0
  197. package/dist/paste/markdown-inline.js +528 -0
  198. package/dist/paste/markdown.paste.d.ts +2 -8
  199. package/dist/paste/markdown.paste.js +78 -94
  200. package/dist/paste/nabu.paste.d.ts +3 -0
  201. package/dist/paste/nabu.paste.js +29 -3
  202. package/dist/paste/plain-text.paste.d.ts +2 -2
  203. package/dist/paste/plain-text.paste.js +3 -3
  204. package/dist/persistence/asset-store.d.ts +51 -0
  205. package/dist/persistence/asset-store.js +103 -0
  206. package/dist/persistence/autosave.d.ts +53 -0
  207. package/dist/persistence/autosave.js +62 -0
  208. package/dist/persistence/compact.d.ts +162 -0
  209. package/dist/persistence/compact.js +141 -0
  210. package/dist/persistence/history.d.ts +77 -0
  211. package/dist/persistence/history.js +176 -0
  212. package/dist/persistence/indexeddb.d.ts +190 -4
  213. package/dist/persistence/indexeddb.js +333 -29
  214. package/dist/persistence/merge.d.ts +38 -0
  215. package/dist/persistence/merge.js +36 -0
  216. package/dist/persistence/outbox.d.ts +25 -0
  217. package/dist/persistence/outbox.js +40 -0
  218. package/dist/persistence/persistence.d.ts +70 -9
  219. package/dist/persistence/persistence.js +507 -35
  220. package/dist/persistence/recovery.d.ts +79 -0
  221. package/dist/persistence/recovery.js +79 -0
  222. package/dist/persistence/release.d.ts +34 -0
  223. package/dist/persistence/release.js +64 -0
  224. package/dist/persistence/state.svelte.d.ts +53 -0
  225. package/dist/persistence/state.svelte.js +48 -0
  226. package/dist/persistence/sync.d.ts +39 -0
  227. package/dist/persistence/sync.js +138 -0
  228. package/dist/persistence/tail.d.ts +42 -0
  229. package/dist/persistence/tail.js +103 -0
  230. package/dist/presets.d.ts +29 -26
  231. package/dist/presets.js +35 -13
  232. package/dist/themes/atelier.css +318 -0
  233. package/dist/ui/CommandChooser.svelte +153 -0
  234. package/dist/ui/CommandChooser.svelte.d.ts +13 -0
  235. package/dist/ui/CommandInvite.svelte +99 -0
  236. package/dist/ui/CommandInvite.svelte.d.ts +12 -0
  237. package/dist/ui/DragHandle.svelte +97 -21
  238. package/dist/ui/FloatingToolbar.svelte +75 -27
  239. package/dist/ui/FloatingToolbar.svelte.d.ts +10 -0
  240. package/dist/ui/SlashMenu.svelte +147 -15
  241. package/dist/ui/SlashMenu.svelte.d.ts +20 -0
  242. package/dist/ui/anchor.d.ts +112 -0
  243. package/dist/ui/anchor.js +136 -0
  244. package/dist/ui/index.d.ts +7 -1
  245. package/dist/ui/index.js +12 -1
  246. package/dist/ui/slash-menu.svelte.d.ts +79 -7
  247. package/dist/ui/slash-menu.svelte.js +85 -51
  248. package/dist/utils/command-confirm.svelte.d.ts +349 -0
  249. package/dist/utils/command-confirm.svelte.js +673 -0
  250. package/dist/utils/commands.d.ts +328 -0
  251. package/dist/utils/commands.js +416 -0
  252. package/dist/utils/dom-scope.d.ts +14 -0
  253. package/dist/utils/dom-scope.js +20 -0
  254. package/dist/utils/drop.d.ts +160 -0
  255. package/dist/utils/drop.js +305 -0
  256. package/dist/utils/extensions.d.ts +439 -45
  257. package/dist/utils/extensions.js +271 -29
  258. package/dist/utils/html.d.ts +44 -0
  259. package/dist/utils/html.js +183 -0
  260. package/dist/utils/index.d.ts +3 -0
  261. package/dist/utils/index.js +3 -0
  262. package/dist/utils/keys.d.ts +120 -0
  263. package/dist/utils/keys.js +248 -0
  264. package/dist/utils/links.d.ts +22 -0
  265. package/dist/utils/links.js +34 -0
  266. package/dist/utils/placeholders.d.ts +81 -3
  267. package/dist/utils/placeholders.js +73 -3
  268. package/dist/utils/selection.svelte.d.ts +47 -19
  269. package/dist/utils/selection.svelte.js +60 -10
  270. package/dist/utils/spellcheck.d.ts +9 -0
  271. package/dist/utils/spellcheck.js +17 -0
  272. package/dist/utils/tab.d.ts +31 -0
  273. package/dist/utils/tab.js +117 -0
  274. package/dist/utils/words.d.ts +7 -0
  275. package/dist/utils/words.js +62 -0
  276. package/dist/vite.d.ts +9 -3
  277. package/dist/vite.js +19 -8
  278. package/package.json +54 -20
  279. package/dist/blocks/variable.svelte.d.ts +0 -47
  280. package/dist/blocks/variable.svelte.js +0 -51
  281. package/dist/blocks/zone/TemplateEditor.svelte +0 -308
  282. package/dist/blocks/zone/TemplateEditor.svelte.d.ts +0 -11
  283. package/dist/blocks/zone/Zone.svelte +0 -272
  284. package/dist/blocks/zone/Zone.svelte.d.ts +0 -11
  285. package/dist/blocks/zone/ZoneEditor.svelte +0 -289
  286. package/dist/blocks/zone/ZoneEditor.svelte.d.ts +0 -11
  287. package/dist/blocks/zone/ZoneTemplatesList.svelte +0 -188
  288. package/dist/blocks/zone/ZoneTemplatesList.svelte.d.ts +0 -11
  289. package/dist/blocks/zone/hooks/onBeforeInput.hook.d.ts +0 -3
  290. package/dist/blocks/zone/hooks/onBeforeInput.hook.js +0 -30
  291. package/dist/blocks/zone/index.d.ts +0 -2
  292. package/dist/blocks/zone/index.js +0 -2
  293. package/dist/blocks/zone/zone.extension.d.ts +0 -2
  294. package/dist/blocks/zone/zone.extension.js +0 -237
  295. package/dist/blocks/zone/zone.svelte.d.ts +0 -221
  296. package/dist/blocks/zone/zone.svelte.js +0 -608
  297. package/dist/overlays/context/Context.svelte +0 -182
  298. package/dist/overlays/context/Context.svelte.d.ts +0 -17
  299. package/dist/overlays/context/collect.d.ts +0 -17
  300. package/dist/overlays/context/collect.js +0 -33
  301. package/dist/overlays/context/context.extension.d.ts +0 -2
  302. package/dist/overlays/context/context.extension.js +0 -8
  303. package/dist/overlays/context/context.svelte.d.ts +0 -82
  304. package/dist/overlays/context/context.svelte.js +0 -159
  305. package/dist/overlays/context/index.d.ts +0 -3
  306. package/dist/overlays/context/index.js +0 -3
@@ -1,63 +1,82 @@
1
- import { List } from '../blocks/list/list.svelte.js';
2
- import { ListItem } from '../blocks/list/list-item.svelte.js';
3
-
4
1
  /**
5
- * Transform the current anchor block (a paragraph) into a list of the given type.
6
- * Mirrors the "- "/"1. " creation flow. Isolated here so the slash menu doesn't
7
- * depend on internal list actions.
8
- * @param {import('../blocks/nabu.svelte.js').Nabu} nabu
9
- * @param {"bullet"|"ordered"|"todo"} listType
2
+ * The known groups of the slash menu, in display order. A group is a free string: an unknown one is listed after these (in the
3
+ * order its first entry is met), and an entry without a group comes last.
4
+ * @type {readonly string[]}
10
5
  */
11
- function createListInBlock(nabu, listType) {
12
- const block = nabu.selection.anchorBlock;
13
- if (!block) return;
14
- const beh = block.behaviors?.get('text');
15
- const delta = beh ? beh.container.toDelta() : [];
16
- const parentNode = block.node.parent();
17
- const parentId = parentNode?.id.toString() ?? null;
18
- const index = block.node.index();
19
- const list = List.create(nabu, 'list', { listType }, parentId, index);
20
- const itemProps = listType === 'todo' ? { delta, checked: false } : { delta };
21
- const item = ListItem.create(nabu, 'list-item', itemProps, list.node.id.toString());
22
- block.destroy();
23
- nabu.commit().then(() => { const f = nabu.blocks.get(item.id); if (f) f.focus({ start: 0, end: 0 }); });
24
- }
6
+ export const SLASH_GROUP_ORDER = ['text', 'lists', 'blocks', 'media', 'structure'];
25
7
 
26
8
  /**
27
9
  * @typedef {Object} SlashCommand
10
+ * An entry of the slash menu: a title, optional subtitle, icon and aliases to filter on, a group and order for sorting, an optional
11
+ * `when` to limit where it is offered, and the action to run. Extensions contribute theirs with `slashCommands`.
28
12
  * @property {string} title
29
13
  * @property {string} [subtitle]
30
- * @property {string} [icon]
14
+ * @property {string} [icon] A short text glyph (no emoji in the library's own entries). A theme or an integrator overrides an
15
+ * entry's icon by providing its own entry (`commands` of `SlashMenu`, or `slashCommands` of an extension).
31
16
  * @property {string[]} [aliases]
17
+ * @property {'text' | 'lists' | 'blocks' | 'media' | 'structure' | (string & {})} [group] Where the entry is listed. Known
18
+ * groups, in display order: `text`, `lists`, `blocks`, `media`, `structure`; any other string is a group of its own listed
19
+ * after them; no group = last.
20
+ * @property {number} [order] Position inside the group (ascending, default 0); ties keep the declaration order. The menu's
21
+ * order never depends on the load order of the extensions.
22
+ * @property {(engine: import('../blocks/nabu.svelte.js').Nabu, block: any) => boolean} [when]
23
+ * Where the entry applies; `block` is the block holding the caret. Without it the entry is always offered.
32
24
  * @property {(engine: import('../blocks/nabu.svelte.js').Nabu) => void} run
33
25
  */
34
26
 
35
- /** @type {SlashCommand[]} */
36
- export const DEFAULT_SLASH_COMMANDS = [
37
- { title: 'Text', icon: '¶', aliases: ['paragraph','p','plain'], run: (e) => e.exec('paragraph') },
38
- { title: 'Heading 1', icon: 'H1', aliases: ['h1','title','heading'], run: (e) => e.exec('heading:1') },
39
- { title: 'Heading 2', icon: 'H2', aliases: ['h2','subtitle'], run: (e) => e.exec('heading:2') },
40
- { title: 'Heading 3', icon: 'H3', aliases: ['h3'], run: (e) => e.exec('heading:3') },
41
- { title: 'Bulleted list', icon: '•', aliases: ['bullet','ul','list'], run: (e) => createListInBlock(e, 'bullet') },
42
- { title: 'Numbered list', icon: '1.', aliases: ['numbered','ol','ordered'], run: (e) => createListInBlock(e, 'ordered') },
43
- { title: 'To-do list', icon: '☑', aliases: ['todo','task','checkbox','checklist'], run: (e) => createListInBlock(e, 'todo') },
44
- { title: 'Code', icon: '<>', aliases: ['code','snippet'], run: (e) => e.exec('code') },
45
- { title: 'Callout', icon: '💡', aliases: ['callout','note','info','warning'], run: (e) => e.exec('callout') },
46
- { title: 'Quote', icon: '"', aliases: ['quote','blockquote'], run: (e) => e.exec('blockquote') },
47
- { title: 'Toggle', icon: '▸', aliases: ['toggle','collapse','details'], run: (e) => e.exec('toggle') },
48
- { title: 'Divider', icon: '—', aliases: ['divider','hr','separator','rule'], run: (e) => e.exec('divider:insert') },
49
- { title: 'Image', icon: '🖼', aliases: ['image','img','photo','picture'], run: (e) => e.exec('image:pick') },
50
- { title: 'Video', icon: '🎬', aliases: ['video','movie','mp4'], run: (e) => e.exec('video:pick') },
51
- { title: 'File', icon: '📎', aliases: ['file','attachment','upload','document'], run: (e) => e.exec('file:pick') },
52
- ];
27
+ /**
28
+ * Stable sort of slash entries: group (known order, then other groups by first appearance, then ungrouped), then `order`, then
29
+ * declaration order. Returns a new array.
30
+ * @param {SlashCommand[]} commands
31
+ * @returns {SlashCommand[]}
32
+ */
33
+ export function sortSlashCommands(commands) {
34
+ /** @type {Map<string, number>} */
35
+ const rank = new Map(SLASH_GROUP_ORDER.map((g, i) => [g, i]));
36
+ const groupRank = (/** @type {SlashCommand} */ c) => {
37
+ if (!c.group) return Infinity;
38
+ if (!rank.has(c.group)) rank.set(c.group, rank.size);
39
+ return /** @type {number} */ (rank.get(c.group));
40
+ };
41
+ return commands
42
+ .map((c, i) => ({ c, i, g: groupRank(c) }))
43
+ .sort((a, b) => (a.g === b.g ? 0 : a.g < b.g ? -1 : 1) || (a.c.order ?? 0) - (b.c.order ?? 0) || a.i - b.i)
44
+ .map((x) => x.c);
45
+ }
53
46
 
47
+ /**
48
+ * Filter slash commands by a query. Titles match by SUBSTRING (`/head` -> "Heading 1"), aliases
49
+ * by PREFIX (`/bul` -> "Bulleted list" via "bullet"); a substring alias match would make `/ul`
50
+ * also hit Divider through its "r-ul-e" alias. Case-insensitive; an empty query keeps everything.
51
+ * @param {SlashCommand[]} commands @param {string} query
52
+ * @returns {SlashCommand[]}
53
+ */
54
+ export function filterSlashCommands(commands, query) {
55
+ const q = query.toLowerCase();
56
+ if (!q) return commands;
57
+ return commands.filter(c =>
58
+ c.title.toLowerCase().includes(q) ||
59
+ (c.aliases || []).some(a => a.toLowerCase().startsWith(q)));
60
+ }
61
+
62
+ /**
63
+ * The state of the "/" menu for one editor: whether it is open, the query typed after the slash, the filtered and sorted entries
64
+ * and the selected one. `SlashMenu` is its default rendering; use it directly to build your own menu.
65
+ */
54
66
  export class SlashMenuController {
55
67
  /** @type {import('../blocks/nabu.svelte.js').Nabu} */
56
68
  #engine;
57
- /** @param {import('../blocks/nabu.svelte.js').Nabu} engine @param {SlashCommand[]} commands */
58
- constructor(engine, commands) { this.#engine = engine; this.commands = commands ?? DEFAULT_SLASH_COMMANDS; }
69
+ /**
70
+ * @param {import('../blocks/nabu.svelte.js').Nabu} engine
71
+ * @param {SlashCommand[]} [commands] EXTRA entries of the integrator, added to the ones the loaded extensions contribute
72
+ * (`Extension#slashCommands`, collected in `engine.slashCommands`) and sorted with them. The menu has no entry of its own.
73
+ */
74
+ constructor(engine, commands) { this.#engine = engine; this.commands = commands ?? []; }
59
75
 
60
- /** @type {{ block: any, slashStart: number, cursor: number, query: string } | null} */
76
+ /**
77
+ * The pending slash trigger at the caret: its block, where the slash starts, the caret and the query typed after it; `null` when there is none.
78
+ * @type {{ block: any, slashStart: number, cursor: number, query: string } | null}
79
+ */
61
80
  match = $derived.by(() => {
62
81
  const sel = this.#engine.selection;
63
82
  const block = sel?.anchorBlock;
@@ -73,25 +92,38 @@ export class SlashMenuController {
73
92
  return { block, slashStart: bsel.from - query.length - 1, cursor: bsel.from, query };
74
93
  });
75
94
 
95
+ /** The text typed after the slash (empty when no trigger). */
76
96
  query = $derived(this.match?.query ?? '');
77
97
 
98
+ /**
99
+ * The entries of the loaded extensions plus the integrator's extras that apply at the caret (`when`), in the documented
100
+ * order (group, `order`, declaration order).
101
+ */
102
+ available = $derived.by(() => {
103
+ const block = this.match?.block;
104
+ const applies = (/** @type {SlashCommand} */ c) => {
105
+ if (typeof c.when !== 'function') return true;
106
+ try { return !!block && !!c.when(this.#engine, block); } catch { return false; }
107
+ };
108
+ return sortSlashCommands([...(this.#engine.slashCommands ?? []), ...this.commands].filter(applies));
109
+ });
110
+
111
+ /** The available entries matching the query: titles by substring, aliases by prefix. */
78
112
  filteredItems = $derived.by(() => {
79
113
  if (!this.match) return [];
80
- const q = this.query.toLowerCase();
81
- if (!q) return this.commands;
82
- return this.commands.filter(c =>
83
- c.title.toLowerCase().includes(q) ||
84
- (c.aliases || []).some(a => a.toLowerCase().includes(q)));
114
+ return filterSlashCommands(this.available, this.query);
85
115
  });
86
116
 
87
117
  /** @type {number | null} slashStart of a dismissed (Escape) trigger */
88
118
  #dismissedKey = $state(null);
89
119
 
90
- open = $derived(!!this.match && this.filteredItems.length > 0 && this.match.slashStart !== this.#dismissedKey);
120
+ /** Whether the menu should be shown: a trigger at the caret, at least one match, not dismissed, not read-only. */
121
+ open = $derived.by(() => !this.#engine.readonly && !!this.match && this.filteredItems.length > 0 && this.match.slashStart !== this.#dismissedKey);
91
122
 
123
+ /** Index of the highlighted entry in `filteredItems`. */
92
124
  selectedIndex = $state(0);
93
125
 
94
- /** @param {number} delta */
126
+ /** Move the highlight by `delta` entries, wrapping around. @param {number} delta */
95
127
  moveSelection(delta) {
96
128
  const n = this.filteredItems.length;
97
129
  if (!n) return;
@@ -107,6 +139,8 @@ export class SlashMenuController {
107
139
  if (beh) beh.delete({ index: m.slashStart, length: m.cursor - m.slashStart });
108
140
  await this.#engine.commit();
109
141
  this.#engine.selection.setCursor(m.block, m.slashStart);
142
+ // The command reads selection.anchorBlock: don't wait for the async `selectionchange`.
143
+ this.#engine.selection.sync?.();
110
144
  this.close();
111
145
  item.run(this.#engine);
112
146
  }
@@ -0,0 +1,349 @@
1
+ /**
2
+ * The DOM element standing for a confirmation target: a block (its `element`) or an element. `null` when it has none (yet).
3
+ * @param {any} target
4
+ * @returns {HTMLElement | null}
5
+ */
6
+ export function commandTargetElement(target: any): HTMLElement | null;
7
+ /** Default duration of a `hold`, in milliseconds. */
8
+ export const DEFAULT_HOLD_MS: 700;
9
+ /** Default time an `invite` waits for its second press, in milliseconds. */
10
+ export const DEFAULT_INVITE_MS: 5000;
11
+ /**
12
+ * Time source of the controller (injectable, so that tests drive a fake clock).
13
+ * @typedef {Object} ConfirmClock
14
+ * @property {() => number} now monotonic milliseconds
15
+ * @property {(fn: () => void, ms: number) => () => void} timeout run `fn` once after `ms`; returns the canceller
16
+ * @property {(fn: () => void) => () => void} frame run `fn` before the next paint (animation frame); returns the canceller
17
+ */
18
+ /** @type {ConfirmClock} */
19
+ export const REAL_CLOCK: ConfirmClock;
20
+ /**
21
+ * What is waiting for the user, as `nabu.pendingCommand`. The reference changes when a confirmation starts or ends; `progress`
22
+ * and `selectedIndex` are reactive fields of it.
23
+ *
24
+ * @typedef {Object} PendingOption
25
+ * @property {string} id the command id (a tie) or the variant id (explicit `choose`)
26
+ * @property {string} label
27
+ * @property {string} display shortcut (a tie) or hint (explicit `choose`) to show next to the label; may be empty
28
+ * @property {string} [group] explicit `choose` only: the title of the group the option belongs to. Consecutive options with the
29
+ * same `group` are shown under one small header; options without one stay ungrouped.
30
+ * @property {string} [detail] explicit `choose` only: a short muted text shown right-aligned in the option (a number, a hint)
31
+ */
32
+ export class PendingCommand {
33
+ /**
34
+ * @param {Object} init
35
+ * @param {string} init.id
36
+ * @param {'hold' | 'invite' | 'choose'} init.strategy
37
+ * @param {string} init.label
38
+ * @param {string} init.display
39
+ * @param {string} init.message
40
+ * @param {any} [init.target]
41
+ * @param {PendingOption[]} [init.options]
42
+ * @param {string} [init.footer]
43
+ */
44
+ constructor({ id, strategy, label, display, message, target, options, footer }: {
45
+ id: string;
46
+ strategy: "hold" | "invite" | "choose";
47
+ label: string;
48
+ display: string;
49
+ message: string;
50
+ target?: any;
51
+ options?: PendingOption[] | undefined;
52
+ footer?: string | undefined;
53
+ });
54
+ /** command id (`choose:<chord>` for a tie) */
55
+ id: string;
56
+ strategy: "hold" | "invite" | "choose";
57
+ /** label of the command (resolved with the context of the key press) */
58
+ label: string;
59
+ /** the shortcut that started it, formatted for the platform (`'Ctrl+Shift+Backspace'`, `'⇧⌘⌫'`) */
60
+ display: string;
61
+ /** the prompt, i18n-resolved: the text of the live region and of the default hint */
62
+ message: string;
63
+ /** what the confirmation is about (`hold`: `confirm.target(ctx)`, usually a block); `undefined` when none */
64
+ target: any;
65
+ /** `choose` only */
66
+ options: PendingOption[] | undefined;
67
+ /** `choose` only: a short hint shown under the options (`confirm.footer`); empty when none */
68
+ footer: string;
69
+ /** `hold`: 0 → 1 over `holdMs` */
70
+ progress: number;
71
+ /** `choose`: the highlighted option */
72
+ selectedIndex: number;
73
+ }
74
+ /**
75
+ * Texts of the prompts and the outcomes. Each value is a string or a function of the given parameters; override them for all
76
+ * commands with the `commandConfirm.messages` init option, or per command with `confirm.messages` (partial objects).
77
+ * @typedef {Object} ConfirmMessages
78
+ * @property {string | ((p: { keys: string, label: string }) => string)} [hold] prompt of a hold
79
+ * @property {string | ((p: { keys: string, label: string }) => string)} [invite] prompt of an invite
80
+ * @property {string | ((p: { keys: string, label: string, options: PendingOption[] }) => string)} [choose] prompt of a chooser
81
+ * @property {string | ((p: { option: PendingOption, index: number, count: number }) => string)} [option] the highlighted option
82
+ * @property {string | ((p: { label: string }) => string)} [done] after the command ran
83
+ * @property {string | ((p: { label: string }) => string)} [cancelled] after a release / Escape / timeout
84
+ */
85
+ /** @type {Required<ConfirmMessages>} */
86
+ export const DEFAULT_MESSAGES: Required<ConfirmMessages>;
87
+ /**
88
+ * @typedef {Object} Session
89
+ * @property {PendingCommand} pending
90
+ * @property {'hold' | 'invite' | 'choose'} strategy
91
+ * @property {CommandEntry | null} entry the command (a tie / `choose` with variants: the explicit command or `null`)
92
+ * @property {CommandContext} ctx context of the key press that started it
93
+ * @property {string} chord canonical id of the chord that started it
94
+ * @property {ReturnType<typeof eventChord>} chordInfo
95
+ * @property {string} code physical key of the press (`event.code`)
96
+ * @property {boolean} [waiting] `hold` opened from code: nobody holds the key yet; the count starts at the user's press
97
+ * @property {number} startedAt
98
+ * @property {number} ms `hold`: duration; `invite`: timeout
99
+ * @property {(() => void) | null} stopTimer
100
+ * @property {Array<{ id: string, label: string, display: string, group?: string, detail?: string, run: () => void }>} [choices] `choose`
101
+ * @property {{ selection: [Node | null, number, Node | null, number] } | null} dom selection when it started
102
+ */
103
+ export class CommandConfirmation {
104
+ /** @param {Nabu} nabu @param {CommandConfirmOptions} [options] */
105
+ constructor(nabu: Nabu, { holdMs, inviteMs, messages, clock }?: CommandConfirmOptions);
106
+ /** What is waiting for the user (`nabu.pendingCommand`), reactive; `null` when nothing is. @returns {PendingCommand | null} */
107
+ get pending(): PendingCommand | null;
108
+ /** Whether the keys must go through {@link CommandConfirmation#handleKeydown}: something is pending, or the key of a completed hold is still down. */
109
+ get busy(): boolean;
110
+ /** The text of the polite live region: the last prompt or outcome (reactive). */
111
+ get announcement(): string;
112
+ /** The pending confirmation when it is about `target` (reactive), else `null`. @param {any} target @returns {PendingCommand | null} */
113
+ pendingOn(target: any): PendingCommand | null;
114
+ /**
115
+ * A key press selected `entry`, which has a `confirm` strategy. Returns whether the key was consumed (it always is, unless
116
+ * the strategy cannot start). The registry has already checked `when`.
117
+ * @param {CommandEntry} entry
118
+ * @param {CommandContext} ctx
119
+ * @param {KeyboardEvent} event
120
+ * @returns {boolean}
121
+ */
122
+ begin(entry: CommandEntry, ctx: CommandContext, event: KeyboardEvent): boolean;
123
+ /**
124
+ * Several commands apply to the key: let the user pick (§3.4c).
125
+ * @param {CommandEntry[]} ties
126
+ * @param {CommandContext} ctx
127
+ * @param {KeyboardEvent} event
128
+ * @returns {boolean}
129
+ */
130
+ beginTie(ties: CommandEntry[], ctx: CommandContext, event: KeyboardEvent): boolean;
131
+ /**
132
+ * Open the confirmation of a command WITHOUT a key press (a typed trigger, a menu, a button, an input rule): the same
133
+ * prompt a key would open (`nabu.pendingCommand`, the `CommandInvite` / `CommandChooser` components, the keyboard handling,
134
+ * the cancellations). The command's `when` is checked. Prefer the public `nabu.commands.offer(id)`, which also runs a command
135
+ * that has no confirmation.
136
+ *
137
+ * - `choose`: the options come from `confirm.options(ctx)`, the choice runs `run(nabu, ctx, option)`;
138
+ * - `invite`: asks to press the command's first key again (`invite` timeout);
139
+ * - `hold`: asks to hold the command's first key; the count starts at the user's press and the confirmation lapses after the
140
+ * `invite` timeout when no key comes.
141
+ *
142
+ * `hold` and `invite` need a key to confirm with: a command with no key (disabled by the `keymap`) opens nothing.
143
+ * Returns false (nothing opens) when the command does not exist, has no confirmation, does not apply, or offers no option.
144
+ * @param {string} id command id
145
+ * @param {CommandContext} [context] default: the current context
146
+ * @returns {boolean}
147
+ */
148
+ offer(id: string, context?: CommandContext): boolean;
149
+ /**
150
+ * Every key press goes through here before the command resolution, while a confirmation is pending (or the key of a completed
151
+ * hold is still down). Returns `true` when the key is consumed (`preventDefault` already called); `false` lets the registry go
152
+ * on, which is what an unrelated key must get after it cancelled the confirmation ("the key acts normally").
153
+ * @param {KeyboardEvent} event
154
+ * @returns {boolean}
155
+ */
156
+ handleKeydown(event: KeyboardEvent): boolean;
157
+ /**
158
+ * Move the highlight of a pending `choose` (the chooser component calls it on hover).
159
+ * @param {number} index
160
+ */
161
+ highlight(index: number): void;
162
+ /**
163
+ * Run option `index` of a pending `choose` (Enter, a digit, a click).
164
+ * @param {number} index
165
+ * @returns {boolean} whether something ran
166
+ */
167
+ choose(index: number): boolean;
168
+ /**
169
+ * Cancel what is pending (`nabu.cancelPendingCommand()`): nothing runs, no undo step exists. No-op when nothing is pending.
170
+ * @param {string} [reason] `'api'` (default), `'release'`, `'escape'`, `'key'`, `'selection'`, `'click'`, `'blur'`, `'timeout'`
171
+ */
172
+ cancel(reason?: string): void;
173
+ /**
174
+ * The key of the chord went up: a hold that is not finished is cancelled; the key of a completed hold is free again.
175
+ * Releasing a modifier of the chord counts like releasing its key.
176
+ * @param {KeyboardEvent} event
177
+ */
178
+ onKeyup(event: KeyboardEvent): void;
179
+ /** The DOM selection moved away from where the confirmation started. */
180
+ onSelectionChange(): void;
181
+ /** A click outside the confirmation's own UI cancels it. @param {Event} event */
182
+ onPointerDown(event: Event): void;
183
+ /** Focus left the editor (window blur, tab switch, a click elsewhere): nothing can be confirmed any more. */
184
+ onBlur(): void;
185
+ /** Attach the document listeners (the editor component calls it through `nabu.systems`). Idempotent. */
186
+ start(): void;
187
+ /** Detach the listeners and drop what is pending. */
188
+ stop(): void;
189
+ #private;
190
+ }
191
+ /**
192
+ * Time source of the controller (injectable, so that tests drive a fake clock).
193
+ */
194
+ export type ConfirmClock = {
195
+ /**
196
+ * monotonic milliseconds
197
+ */
198
+ now: () => number;
199
+ /**
200
+ * run `fn` once after `ms`; returns the canceller
201
+ */
202
+ timeout: (fn: () => void, ms: number) => () => void;
203
+ /**
204
+ * run `fn` before the next paint (animation frame); returns the canceller
205
+ */
206
+ frame: (fn: () => void) => () => void;
207
+ };
208
+ /**
209
+ * What is waiting for the user, as `nabu.pendingCommand`. The reference changes when a confirmation starts or ends; `progress`
210
+ * and `selectedIndex` are reactive fields of it.
211
+ */
212
+ export type PendingOption = {
213
+ /**
214
+ * the command id (a tie) or the variant id (explicit `choose`)
215
+ */
216
+ id: string;
217
+ label: string;
218
+ /**
219
+ * shortcut (a tie) or hint (explicit `choose`) to show next to the label; may be empty
220
+ */
221
+ display: string;
222
+ /**
223
+ * explicit `choose` only: the title of the group the option belongs to. Consecutive options with the
224
+ * same `group` are shown under one small header; options without one stay ungrouped.
225
+ */
226
+ group?: string | undefined;
227
+ /**
228
+ * explicit `choose` only: a short muted text shown right-aligned in the option (a number, a hint)
229
+ */
230
+ detail?: string | undefined;
231
+ };
232
+ /**
233
+ * Texts of the prompts and the outcomes. Each value is a string or a function of the given parameters; override them for all
234
+ * commands with the `commandConfirm.messages` init option, or per command with `confirm.messages` (partial objects).
235
+ */
236
+ export type ConfirmMessages = {
237
+ /**
238
+ * prompt of a hold
239
+ */
240
+ hold?: string | ((p: {
241
+ keys: string;
242
+ label: string;
243
+ }) => string) | undefined;
244
+ /**
245
+ * prompt of an invite
246
+ */
247
+ invite?: string | ((p: {
248
+ keys: string;
249
+ label: string;
250
+ }) => string) | undefined;
251
+ /**
252
+ * prompt of a chooser
253
+ */
254
+ choose?: string | ((p: {
255
+ keys: string;
256
+ label: string;
257
+ options: PendingOption[];
258
+ }) => string) | undefined;
259
+ /**
260
+ * the highlighted option
261
+ */
262
+ option?: string | ((p: {
263
+ option: PendingOption;
264
+ index: number;
265
+ count: number;
266
+ }) => string) | undefined;
267
+ /**
268
+ * after the command ran
269
+ */
270
+ done?: string | ((p: {
271
+ label: string;
272
+ }) => string) | undefined;
273
+ /**
274
+ * after a release / Escape / timeout
275
+ */
276
+ cancelled?: string | ((p: {
277
+ label: string;
278
+ }) => string) | undefined;
279
+ };
280
+ export type CommandConfirmOptions = {
281
+ /**
282
+ * default duration of a `hold` (700)
283
+ */
284
+ holdMs?: number | undefined;
285
+ /**
286
+ * default time an `invite` waits (5000)
287
+ */
288
+ inviteMs?: number | undefined;
289
+ /**
290
+ * texts, see {@link ConfirmMessages}
291
+ */
292
+ messages?: ConfirmMessages | undefined;
293
+ /**
294
+ * time source (tests)
295
+ */
296
+ clock?: ConfirmClock | undefined;
297
+ };
298
+ export type Session = {
299
+ pending: PendingCommand;
300
+ strategy: "hold" | "invite" | "choose";
301
+ /**
302
+ * the command (a tie / `choose` with variants: the explicit command or `null`)
303
+ */
304
+ entry: CommandEntry | null;
305
+ /**
306
+ * context of the key press that started it
307
+ */
308
+ ctx: CommandContext;
309
+ /**
310
+ * canonical id of the chord that started it
311
+ */
312
+ chord: string;
313
+ chordInfo: ReturnType<typeof eventChord>;
314
+ /**
315
+ * physical key of the press (`event.code`)
316
+ */
317
+ code: string;
318
+ /**
319
+ * `hold` opened from code: nobody holds the key yet; the count starts at the user's press
320
+ */
321
+ waiting?: boolean | undefined;
322
+ startedAt: number;
323
+ /**
324
+ * `hold`: duration; `invite`: timeout
325
+ */
326
+ ms: number;
327
+ stopTimer: (() => void) | null;
328
+ /**
329
+ * `choose`
330
+ */
331
+ choices?: {
332
+ id: string;
333
+ label: string;
334
+ display: string;
335
+ group?: string;
336
+ detail?: string;
337
+ run: () => void;
338
+ }[] | undefined;
339
+ /**
340
+ * selection when it started
341
+ */
342
+ dom: {
343
+ selection: [Node | null, number, Node | null, number];
344
+ } | null;
345
+ };
346
+ import type { CommandEntry } from './commands.js';
347
+ import type { CommandContext } from './commands.js';
348
+ import type { Nabu } from '../blocks/nabu.svelte.js';
349
+ import { eventChord } from './keys.js';