@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.
- package/CHANGELOG.md +523 -0
- package/LICENSE +21 -0
- package/README.md +72 -341
- package/dist/behaviors/text/RichText.svelte +24 -4
- package/dist/behaviors/text/markdown-shortcuts.extension.d.ts +3 -0
- package/dist/behaviors/text/markdown-shortcuts.extension.js +35 -10
- package/dist/behaviors/text/markdown-syntax.d.ts +42 -0
- package/dist/behaviors/text/markdown-syntax.js +81 -0
- package/dist/behaviors/text/rich-text.extension.d.ts +3 -0
- package/dist/behaviors/text/rich-text.extension.js +95 -38
- package/dist/behaviors/text/text.behavior.svelte.d.ts +199 -48
- package/dist/behaviors/text/text.behavior.svelte.js +549 -83
- package/dist/behaviors/text/typography.extension.d.ts +2 -1
- package/dist/behaviors/text/typography.extension.js +3 -2
- package/dist/blocks/BlockList.svelte +48 -0
- package/dist/blocks/BlockList.svelte.d.ts +17 -0
- package/dist/blocks/Nabu.svelte +169 -25
- package/dist/blocks/Nabu.svelte.d.ts +13 -0
- package/dist/blocks/block.extension.d.ts +3 -0
- package/dist/blocks/block.extension.js +4 -7
- package/dist/blocks/block.svelte.d.ts +350 -86
- package/dist/blocks/block.svelte.js +370 -59
- package/dist/blocks/blockquote/Blockquote.svelte +1 -2
- package/dist/blocks/blockquote/blockquote.extension.d.ts +4 -1
- package/dist/blocks/blockquote/blockquote.extension.js +14 -5
- package/dist/blocks/blockquote/blockquote.svelte.d.ts +19 -11
- package/dist/blocks/blockquote/blockquote.svelte.js +58 -13
- package/dist/blocks/callout/Callout.svelte +6 -8
- package/dist/blocks/callout/callout.extension.d.ts +4 -1
- package/dist/blocks/callout/callout.extension.js +19 -3
- package/dist/blocks/callout/callout.svelte.d.ts +5 -3
- package/dist/blocks/callout/callout.svelte.js +11 -6
- package/dist/blocks/chunks.d.ts +49 -0
- package/dist/blocks/chunks.js +90 -0
- package/dist/blocks/code/Code.svelte +7 -8
- package/dist/blocks/code/code.extension.d.ts +4 -1
- package/dist/blocks/code/code.extension.js +22 -2
- package/dist/blocks/code/code.svelte.d.ts +9 -2
- package/dist/blocks/code/code.svelte.js +49 -25
- package/dist/blocks/container.utils.d.ts +85 -13
- package/dist/blocks/container.utils.js +338 -178
- package/dist/blocks/dialogue/Dialogue.svelte +8 -4
- package/dist/blocks/dialogue/dialogue.svelte.d.ts +33 -2
- package/dist/blocks/dialogue/dialogue.svelte.js +58 -4
- package/dist/blocks/dialogue/hooks/onBeforeInput.hook.d.ts +2 -2
- package/dist/blocks/dialogue/hooks/onBeforeInput.hook.js +6 -4
- package/dist/blocks/dialogue/index.d.ts +12 -1
- package/dist/blocks/dialogue/index.js +51 -35
- package/dist/blocks/divider/divider.extension.d.ts +23 -1
- package/dist/blocks/divider/divider.extension.js +85 -19
- package/dist/blocks/divider/divider.svelte.d.ts +7 -2
- package/dist/blocks/divider/divider.svelte.js +9 -4
- package/dist/blocks/file/File.svelte +13 -6
- package/dist/blocks/file/file.extension.d.ts +4 -1
- package/dist/blocks/file/file.extension.js +9 -1
- package/dist/blocks/file/file.svelte.d.ts +3 -3
- package/dist/blocks/file/file.svelte.js +20 -14
- package/dist/blocks/heading/Heading.svelte +1 -2
- package/dist/blocks/heading/heading.svelte.d.ts +23 -11
- package/dist/blocks/heading/heading.svelte.js +68 -13
- package/dist/blocks/heading/hooks/onBeforeInput.hook.js +7 -5
- package/dist/blocks/heading/index.d.ts +2 -2
- package/dist/blocks/heading/index.js +33 -8
- package/dist/blocks/image/Image.svelte +5 -2
- package/dist/blocks/image/image.extension.d.ts +4 -1
- package/dist/blocks/image/image.extension.js +9 -1
- package/dist/blocks/image/image.svelte.d.ts +17 -6
- package/dist/blocks/image/image.svelte.js +33 -11
- package/dist/blocks/index.d.ts +1 -1
- package/dist/blocks/index.js +1 -1
- package/dist/blocks/list/List.svelte +1 -0
- package/dist/blocks/list/ListItem.svelte +1 -0
- package/dist/blocks/list/convert.d.ts +20 -0
- package/dist/blocks/list/convert.js +92 -0
- package/dist/blocks/list/hooks/onBeforeInput.hook.js +49 -28
- package/dist/blocks/list/html-flat.d.ts +78 -0
- package/dist/blocks/list/html-flat.js +244 -0
- package/dist/blocks/list/indent.d.ts +86 -0
- package/dist/blocks/list/indent.js +205 -0
- package/dist/blocks/list/index.d.ts +6 -3
- package/dist/blocks/list/index.js +42 -31
- package/dist/blocks/list/list-item.svelte.d.ts +31 -17
- package/dist/blocks/list/list-item.svelte.js +99 -100
- package/dist/blocks/list/list.behavior.svelte.d.ts +4 -1
- package/dist/blocks/list/list.behavior.svelte.js +6 -1
- package/dist/blocks/list/list.svelte.d.ts +89 -18
- package/dist/blocks/list/list.svelte.js +129 -125
- package/dist/blocks/list/markdown.d.ts +149 -0
- package/dist/blocks/list/markdown.js +442 -0
- package/dist/blocks/list/normalize.d.ts +11 -0
- package/dist/blocks/list/normalize.js +396 -0
- package/dist/blocks/list/paste.d.ts +45 -0
- package/dist/blocks/list/paste.js +242 -0
- package/dist/blocks/list/step-back.d.ts +24 -0
- package/dist/blocks/list/step-back.js +87 -0
- package/dist/blocks/list/to-list.d.ts +7 -0
- package/dist/blocks/list/to-list.js +44 -0
- package/dist/blocks/list/todos-toggle.d.ts +20 -0
- package/dist/blocks/list/todos-toggle.js +48 -0
- package/dist/blocks/media/markdown.d.ts +12 -0
- package/dist/blocks/media/markdown.js +26 -0
- package/dist/blocks/media/media-block.svelte.d.ts +77 -2
- package/dist/blocks/media/media-block.svelte.js +124 -12
- package/dist/blocks/media/media.helpers.d.ts +6 -1
- package/dist/blocks/media/media.helpers.js +43 -6
- package/dist/blocks/megablock.svelte.d.ts +50 -19
- package/dist/blocks/megablock.svelte.js +114 -74
- package/dist/blocks/move.d.ts +23 -0
- package/dist/blocks/move.js +92 -0
- package/dist/blocks/nabu.svelte.d.ts +764 -71
- package/dist/blocks/nabu.svelte.js +1746 -189
- package/dist/blocks/paragraph/Paragraph.svelte +1 -2
- package/dist/blocks/paragraph/index.d.ts +2 -2
- package/dist/blocks/paragraph/index.js +35 -13
- package/dist/blocks/paragraph/paragraph.svelte.d.ts +46 -14
- package/dist/blocks/paragraph/paragraph.svelte.js +77 -16
- package/dist/blocks/paragraph/table.html.d.ts +9 -0
- package/dist/blocks/paragraph/table.html.js +111 -0
- package/dist/blocks/paragraph/table.markdown.d.ts +32 -0
- package/dist/blocks/paragraph/table.markdown.js +90 -0
- package/dist/blocks/region/Region.svelte +264 -0
- package/dist/blocks/region/Region.svelte.d.ts +11 -0
- package/dist/blocks/region/actions.d.ts +135 -0
- package/dist/blocks/region/actions.js +402 -0
- package/dist/blocks/region/close.d.ts +35 -0
- package/dist/blocks/region/close.js +130 -0
- package/dist/blocks/region/commands.d.ts +6 -0
- package/dist/blocks/region/commands.js +277 -0
- package/dist/blocks/region/create.d.ts +72 -0
- package/dist/blocks/region/create.js +208 -0
- package/dist/blocks/region/end-marker.d.ts +35 -0
- package/dist/blocks/region/end-marker.js +51 -0
- package/dist/blocks/region/facts.d.ts +69 -0
- package/dist/blocks/region/facts.js +152 -0
- package/dist/blocks/region/format.d.ts +91 -0
- package/dist/blocks/region/format.js +260 -0
- package/dist/blocks/region/index.d.ts +14 -0
- package/dist/blocks/region/index.js +14 -0
- package/dist/blocks/region/insert.d.ts +59 -0
- package/dist/blocks/region/insert.js +102 -0
- package/dist/blocks/region/messages.d.ts +254 -0
- package/dist/blocks/region/messages.js +131 -0
- package/dist/blocks/region/normalize.d.ts +7 -0
- package/dist/blocks/region/normalize.js +170 -0
- package/dist/blocks/region/region.extension.d.ts +58 -0
- package/dist/blocks/region/region.extension.js +119 -0
- package/dist/blocks/region/region.svelte.d.ts +221 -0
- package/dist/blocks/region/region.svelte.js +582 -0
- package/dist/blocks/region/regions.svelte.d.ts +141 -0
- package/dist/blocks/region/regions.svelte.js +276 -0
- package/dist/blocks/region/serialize.d.ts +57 -0
- package/dist/blocks/region/serialize.js +112 -0
- package/dist/blocks/region/types.d.ts +255 -0
- package/dist/blocks/region/types.js +204 -0
- package/dist/blocks/selection.svelte.d.ts +106 -8
- package/dist/blocks/selection.svelte.js +622 -74
- package/dist/blocks/subthread.d.ts +80 -0
- package/dist/blocks/subthread.js +167 -0
- package/dist/blocks/text-block.svelte.d.ts +34 -13
- package/dist/blocks/text-block.svelte.js +42 -8
- package/dist/blocks/toggle/Toggle.svelte +4 -15
- package/dist/blocks/toggle/markdown.d.ts +20 -0
- package/dist/blocks/toggle/markdown.js +112 -0
- package/dist/blocks/toggle/toggle.extension.d.ts +4 -1
- package/dist/blocks/toggle/toggle.extension.js +6 -0
- package/dist/blocks/toggle/toggle.svelte.d.ts +54 -7
- package/dist/blocks/toggle/toggle.svelte.js +134 -34
- package/dist/blocks/undo-selection.d.ts +175 -0
- package/dist/blocks/undo-selection.js +360 -0
- package/dist/blocks/video/Video.svelte +5 -2
- package/dist/blocks/video/video.extension.d.ts +4 -1
- package/dist/blocks/video/video.extension.js +9 -1
- package/dist/blocks/video/video.svelte.d.ts +6 -5
- package/dist/blocks/video/video.svelte.js +18 -9
- package/dist/extend.d.ts +55 -0
- package/dist/extend.js +88 -0
- package/dist/files/assets.clipboard.d.ts +77 -0
- package/dist/files/assets.clipboard.js +152 -0
- package/dist/files/files.svelte.d.ts +321 -19
- package/dist/files/files.svelte.js +847 -43
- package/dist/files/index.d.ts +3 -1
- package/dist/files/index.js +3 -1
- package/dist/files/outbox.d.ts +69 -0
- package/dist/files/outbox.js +48 -0
- package/dist/files/status.d.ts +65 -0
- package/dist/files/status.js +87 -0
- package/dist/files/store.d.ts +97 -0
- package/dist/files/store.js +65 -0
- package/dist/files/uploads.svelte.d.ts +174 -0
- package/dist/files/uploads.svelte.js +504 -0
- package/dist/index.d.ts +68 -9
- package/dist/index.js +117 -10
- package/dist/nabu-theme.css +21 -1
- package/dist/paste/html.paste.d.ts +74 -3
- package/dist/paste/html.paste.js +550 -86
- package/dist/paste/markdown-inline.d.ts +86 -0
- package/dist/paste/markdown-inline.js +528 -0
- package/dist/paste/markdown.paste.d.ts +2 -8
- package/dist/paste/markdown.paste.js +78 -94
- package/dist/paste/nabu.paste.d.ts +3 -0
- package/dist/paste/nabu.paste.js +29 -3
- package/dist/paste/plain-text.paste.d.ts +2 -2
- package/dist/paste/plain-text.paste.js +3 -3
- package/dist/persistence/asset-store.d.ts +51 -0
- package/dist/persistence/asset-store.js +103 -0
- package/dist/persistence/autosave.d.ts +53 -0
- package/dist/persistence/autosave.js +62 -0
- package/dist/persistence/compact.d.ts +162 -0
- package/dist/persistence/compact.js +141 -0
- package/dist/persistence/history.d.ts +77 -0
- package/dist/persistence/history.js +176 -0
- package/dist/persistence/indexeddb.d.ts +190 -4
- package/dist/persistence/indexeddb.js +333 -29
- package/dist/persistence/merge.d.ts +38 -0
- package/dist/persistence/merge.js +36 -0
- package/dist/persistence/outbox.d.ts +25 -0
- package/dist/persistence/outbox.js +40 -0
- package/dist/persistence/persistence.d.ts +70 -9
- package/dist/persistence/persistence.js +507 -35
- package/dist/persistence/recovery.d.ts +79 -0
- package/dist/persistence/recovery.js +79 -0
- package/dist/persistence/release.d.ts +34 -0
- package/dist/persistence/release.js +64 -0
- package/dist/persistence/state.svelte.d.ts +53 -0
- package/dist/persistence/state.svelte.js +48 -0
- package/dist/persistence/sync.d.ts +39 -0
- package/dist/persistence/sync.js +138 -0
- package/dist/persistence/tail.d.ts +42 -0
- package/dist/persistence/tail.js +103 -0
- package/dist/presets.d.ts +29 -26
- package/dist/presets.js +35 -13
- package/dist/themes/atelier.css +318 -0
- package/dist/ui/CommandChooser.svelte +153 -0
- package/dist/ui/CommandChooser.svelte.d.ts +13 -0
- package/dist/ui/CommandInvite.svelte +99 -0
- package/dist/ui/CommandInvite.svelte.d.ts +12 -0
- package/dist/ui/DragHandle.svelte +97 -21
- package/dist/ui/FloatingToolbar.svelte +75 -27
- package/dist/ui/FloatingToolbar.svelte.d.ts +10 -0
- package/dist/ui/SlashMenu.svelte +147 -15
- package/dist/ui/SlashMenu.svelte.d.ts +20 -0
- package/dist/ui/anchor.d.ts +112 -0
- package/dist/ui/anchor.js +136 -0
- package/dist/ui/index.d.ts +7 -1
- package/dist/ui/index.js +12 -1
- package/dist/ui/slash-menu.svelte.d.ts +79 -7
- package/dist/ui/slash-menu.svelte.js +85 -51
- package/dist/utils/command-confirm.svelte.d.ts +349 -0
- package/dist/utils/command-confirm.svelte.js +673 -0
- package/dist/utils/commands.d.ts +328 -0
- package/dist/utils/commands.js +416 -0
- package/dist/utils/dom-scope.d.ts +14 -0
- package/dist/utils/dom-scope.js +20 -0
- package/dist/utils/drop.d.ts +160 -0
- package/dist/utils/drop.js +305 -0
- package/dist/utils/extensions.d.ts +439 -45
- package/dist/utils/extensions.js +271 -29
- package/dist/utils/html.d.ts +44 -0
- package/dist/utils/html.js +183 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.js +3 -0
- package/dist/utils/keys.d.ts +120 -0
- package/dist/utils/keys.js +248 -0
- package/dist/utils/links.d.ts +22 -0
- package/dist/utils/links.js +34 -0
- package/dist/utils/placeholders.d.ts +81 -3
- package/dist/utils/placeholders.js +73 -3
- package/dist/utils/selection.svelte.d.ts +47 -19
- package/dist/utils/selection.svelte.js +60 -10
- package/dist/utils/spellcheck.d.ts +9 -0
- package/dist/utils/spellcheck.js +17 -0
- package/dist/utils/tab.d.ts +31 -0
- package/dist/utils/tab.js +117 -0
- package/dist/utils/words.d.ts +7 -0
- package/dist/utils/words.js +62 -0
- package/dist/vite.d.ts +9 -3
- package/dist/vite.js +19 -8
- package/package.json +54 -20
- package/dist/blocks/variable.svelte.d.ts +0 -47
- package/dist/blocks/variable.svelte.js +0 -51
- package/dist/blocks/zone/TemplateEditor.svelte +0 -308
- package/dist/blocks/zone/TemplateEditor.svelte.d.ts +0 -11
- package/dist/blocks/zone/Zone.svelte +0 -272
- package/dist/blocks/zone/Zone.svelte.d.ts +0 -11
- package/dist/blocks/zone/ZoneEditor.svelte +0 -289
- package/dist/blocks/zone/ZoneEditor.svelte.d.ts +0 -11
- package/dist/blocks/zone/ZoneTemplatesList.svelte +0 -188
- package/dist/blocks/zone/ZoneTemplatesList.svelte.d.ts +0 -11
- package/dist/blocks/zone/hooks/onBeforeInput.hook.d.ts +0 -3
- package/dist/blocks/zone/hooks/onBeforeInput.hook.js +0 -30
- package/dist/blocks/zone/index.d.ts +0 -2
- package/dist/blocks/zone/index.js +0 -2
- package/dist/blocks/zone/zone.extension.d.ts +0 -2
- package/dist/blocks/zone/zone.extension.js +0 -237
- package/dist/blocks/zone/zone.svelte.d.ts +0 -221
- package/dist/blocks/zone/zone.svelte.js +0 -608
- package/dist/overlays/context/Context.svelte +0 -182
- package/dist/overlays/context/Context.svelte.d.ts +0 -17
- package/dist/overlays/context/collect.d.ts +0 -17
- package/dist/overlays/context/collect.js +0 -33
- package/dist/overlays/context/context.extension.d.ts +0 -2
- package/dist/overlays/context/context.extension.js +0 -8
- package/dist/overlays/context/context.svelte.d.ts +0 -82
- package/dist/overlays/context/context.svelte.js +0 -159
- package/dist/overlays/context/index.d.ts +0 -3
- package/dist/overlays/context/index.js +0 -3
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Indent / unindent of list items (Tab / Shift+Tab), for one item or for every item a selection touches.
|
|
3
|
+
*
|
|
4
|
+
* Everything works on Loro tree NODES, never on the Block instances: inside one batch of moves the
|
|
5
|
+
* Block state (`children`, `sublists`, `parent`) is stale until the commit, and the later moves of the
|
|
6
|
+
* batch depend on the earlier ones. All the moves belong to ONE transaction (one undo step); the
|
|
7
|
+
* selection is captured before and restored after.
|
|
8
|
+
*
|
|
9
|
+
* @import { Nabu, NabuNode } from "../nabu.svelte.js";
|
|
10
|
+
* @import { Block } from "../block.svelte.js";
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { List } from "./list.svelte.js";
|
|
14
|
+
|
|
15
|
+
/** @param {NabuNode | null | undefined} node */
|
|
16
|
+
export const typeOf = (node) => /** @type {any} */ (node?.data.get("type"));
|
|
17
|
+
|
|
18
|
+
/** @param {NabuNode} node @returns {NabuNode[]} */
|
|
19
|
+
export const kids = (node) => /** @type {NabuNode[]} */ (node.children() ?? []);
|
|
20
|
+
|
|
21
|
+
/** `listType` of a list node (`bullet` when unset). @param {NabuNode | null | undefined} list */
|
|
22
|
+
export const listTypeOf = (list) => /** @type {any} */ (list?.data)?.get("listType") || "bullet";
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Sublists of an item, in order, read from the tree (not from the possibly stale Block). An item may hold
|
|
26
|
+
* SEVERAL, of different types (P-28): `a > [ul[s1], ol[s2], ul[s3]]`.
|
|
27
|
+
* @param {NabuNode} item
|
|
28
|
+
* @returns {NabuNode[]}
|
|
29
|
+
*/
|
|
30
|
+
export const sublistsOf = (item) => kids(item).filter((child) => typeOf(child) === "list");
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The sublist of `item` that receives a child of type `listType`: its LAST sublist when that one has the
|
|
34
|
+
* same type, otherwise a new sublist of that type appended after the existing ones. No `nabu.insert()`: it
|
|
35
|
+
* commits, and the normalizer would delete the still-empty list before the caller fills it.
|
|
36
|
+
* @param {Nabu} nabu
|
|
37
|
+
* @param {NabuNode} item
|
|
38
|
+
* @param {string} listType
|
|
39
|
+
* @returns {NabuNode}
|
|
40
|
+
*/
|
|
41
|
+
function sublistForType(nabu, item, listType) {
|
|
42
|
+
const last = kids(item).at(-1);
|
|
43
|
+
if (last && typeOf(last) === "list" && listTypeOf(last) === listType) return last;
|
|
44
|
+
return List.create(nabu, "list", { listType }, item.id.toString()).node;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Deletes a list this batch just emptied. The normalizer would do it too, but it spares a list created by
|
|
49
|
+
* the same transaction (it may still be "under construction"), and a batch of moves creates then empties
|
|
50
|
+
* such lists (an item unindented, then its adopted follower unindented in turn).
|
|
51
|
+
* @param {Nabu} nabu
|
|
52
|
+
* @param {NabuNode | null | undefined} list
|
|
53
|
+
*/
|
|
54
|
+
export function dropIfEmpty(nabu, list) {
|
|
55
|
+
if (list && typeOf(list) === "list" && !kids(list).length) nabu.tree.delete(list.id);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Indent one item: it goes under its previous sibling, at the end of that sibling's LAST sublist when that
|
|
60
|
+
* sublist has the same type as the item's current list, otherwise in a new sublist of that type appended
|
|
61
|
+
* after the sibling's sublists. Its whole subtree moves along (its own sublists stay attached to it). Moves
|
|
62
|
+
* only; no commit.
|
|
63
|
+
* @param {Nabu} nabu
|
|
64
|
+
* @param {NabuNode} item
|
|
65
|
+
* @returns {boolean} whether the item moved
|
|
66
|
+
*/
|
|
67
|
+
export function indentNode(nabu, item) {
|
|
68
|
+
const index = item.index();
|
|
69
|
+
if (!index) return false;
|
|
70
|
+
const parentList = item.parent();
|
|
71
|
+
const previous = parentList && kids(parentList)[index - 1];
|
|
72
|
+
if (!previous || typeOf(previous) !== "list-item") return false;
|
|
73
|
+
|
|
74
|
+
nabu.tree.move(item.id, sublistForType(nabu, previous, listTypeOf(parentList)).id);
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Unindent one item: it goes right after its parent item, and the siblings that followed it become its
|
|
80
|
+
* children, in a sublist of THEIR list type appended after the sublists it already has (its own subtree
|
|
81
|
+
* stays attached). To keep the document order, the sublists of the parent item that came after the item's
|
|
82
|
+
* list follow them. Moves only; no commit.
|
|
83
|
+
* @param {Nabu} nabu
|
|
84
|
+
* @param {NabuNode} item
|
|
85
|
+
* @returns {boolean} whether the item moved
|
|
86
|
+
*/
|
|
87
|
+
export function unindentNode(nabu, item) {
|
|
88
|
+
const parentList = item.parent();
|
|
89
|
+
const parentItem = parentList?.parent();
|
|
90
|
+
if (!parentList || !parentItem || typeOf(parentItem) !== "list-item") return false;
|
|
91
|
+
const targetList = parentItem.parent();
|
|
92
|
+
if (!targetList) return false;
|
|
93
|
+
|
|
94
|
+
const followers = kids(parentList).slice((item.index() ?? 0) + 1);
|
|
95
|
+
const laterSublists = kids(parentItem).slice((parentList.index() ?? 0) + 1);
|
|
96
|
+
if (followers.length) {
|
|
97
|
+
const sublistId = sublistForType(nabu, item, listTypeOf(parentList)).id;
|
|
98
|
+
for (const follower of followers) nabu.tree.move(follower.id, sublistId);
|
|
99
|
+
}
|
|
100
|
+
for (const later of laterSublists) nabu.tree.move(later.id, item.id);
|
|
101
|
+
nabu.tree.move(item.id, targetList.id, (parentItem.index() ?? 0) + 1);
|
|
102
|
+
dropIfEmpty(nabu, parentList);
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* @typedef {{ block: Block, offset: number }} CaretPoint
|
|
108
|
+
* @typedef {{ anchor: CaretPoint, focus: CaretPoint }} SelectionSnapshot
|
|
109
|
+
*/
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Model position (block + text offset) of the live selection's anchor and focus, or null when one of
|
|
113
|
+
* them is not in a text block.
|
|
114
|
+
* @param {Nabu} nabu
|
|
115
|
+
* @returns {SelectionSnapshot | null}
|
|
116
|
+
*/
|
|
117
|
+
export function captureSelection(nabu) {
|
|
118
|
+
const sel = nabu.selection;
|
|
119
|
+
/** @param {Node | null} node @param {number} offset @param {Block | null | undefined} block @returns {CaretPoint | null} */
|
|
120
|
+
const point = (node, offset, block) => {
|
|
121
|
+
const text = /** @type {any} */ (block?.behaviors?.get("text"));
|
|
122
|
+
if (!node || !block?.element || !text) return null;
|
|
123
|
+
return { block, offset: text.calculateOffset(node, offset, block.element) };
|
|
124
|
+
};
|
|
125
|
+
const anchor = point(sel.anchorNode, sel.anchorOffset, sel.anchorBlock);
|
|
126
|
+
const focus = point(sel.focusNode, sel.focusOffset, sel.focusBlock);
|
|
127
|
+
return anchor && focus ? { anchor, focus } : null;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Put the selection back after a re-render: same blocks (looked up again by id), same offsets, so a
|
|
132
|
+
* collapsed caret stays collapsed and a range stays a range.
|
|
133
|
+
* @param {Nabu} nabu
|
|
134
|
+
* @param {SelectionSnapshot | null} snapshot
|
|
135
|
+
* @returns {Promise<void>}
|
|
136
|
+
*/
|
|
137
|
+
export async function restoreSelection(nabu, snapshot) {
|
|
138
|
+
if (!snapshot) return;
|
|
139
|
+
const anchor = nabu.blocks.get(snapshot.anchor.block.id);
|
|
140
|
+
const focus = nabu.blocks.get(snapshot.focus.block.id);
|
|
141
|
+
if (!anchor || !focus) return;
|
|
142
|
+
await nabu.focus({
|
|
143
|
+
from: { block: anchor, offset: snapshot.anchor.offset },
|
|
144
|
+
to: { block: focus, offset: snapshot.focus.offset },
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Indent (`unindent = false`) or unindent every given list item, in document order, as ONE transaction.
|
|
150
|
+
* Items that cannot move (e.g. the first item of a list on indent) stay. The selection is restored.
|
|
151
|
+
* @param {Nabu} nabu
|
|
152
|
+
* @param {NabuNode[]} items list-item nodes, in document order
|
|
153
|
+
* @param {boolean} unindent
|
|
154
|
+
* @returns {Promise<boolean>} whether at least one item moved
|
|
155
|
+
*/
|
|
156
|
+
export async function moveItems(nabu, items, unindent) {
|
|
157
|
+
const snapshot = captureSelection(nabu);
|
|
158
|
+
// Ancestors read BEFORE any move of the batch: an item whose original ancestor is selected AND moved
|
|
159
|
+
// goes along with it as part of the subtree, so it must not be moved a second time. (Later moves
|
|
160
|
+
// re-parent items: s2 adopted by an unindented s1 is still processed, it was no descendant of s1.)
|
|
161
|
+
const ancestors = items.map((item) => {
|
|
162
|
+
/** @type {string[]} */
|
|
163
|
+
const ids = [];
|
|
164
|
+
for (let a = item.parent(); a; a = a.parent()) ids.push(a.id.toString());
|
|
165
|
+
return ids;
|
|
166
|
+
});
|
|
167
|
+
/** @type {Set<string>} */
|
|
168
|
+
const moved = new Set();
|
|
169
|
+
items.forEach((item, i) => {
|
|
170
|
+
if (ancestors[i].some((id) => moved.has(id))) return;
|
|
171
|
+
if (unindent ? unindentNode(nabu, item) : indentNode(nabu, item)) moved.add(item.id.toString());
|
|
172
|
+
});
|
|
173
|
+
if (!moved.size) return false;
|
|
174
|
+
await nabu.commit();
|
|
175
|
+
await restoreSelection(nabu, snapshot);
|
|
176
|
+
return true;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* The list items a selection touches, in document order: the anchor block alone when the selection is
|
|
181
|
+
* inside one block, otherwise every list item between the start and the end block.
|
|
182
|
+
* @param {Nabu} nabu
|
|
183
|
+
* @returns {NabuNode[]}
|
|
184
|
+
*/
|
|
185
|
+
export function selectedItems(nabu) {
|
|
186
|
+
const sel = nabu.selection;
|
|
187
|
+
/** @type {Block[]} */
|
|
188
|
+
const blocks = sel.anchorBlock && sel.anchorBlock === sel.focusBlock ? [sel.anchorBlock] : [...sel.blocks];
|
|
189
|
+
return blocks.filter((block) => block.type === "list-item").map((block) => /** @type {NabuNode} */ (block.node));
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Tab / Shift+Tab on the current selection. Returns true (= the key is handled, the default prevented)
|
|
194
|
+
* whenever the selection touches list items, even when none of them could move: focus never leaves the editor.
|
|
195
|
+
* @param {Nabu} nabu
|
|
196
|
+
* @param {KeyboardEvent} event
|
|
197
|
+
* @returns {boolean}
|
|
198
|
+
*/
|
|
199
|
+
export function handleTab(nabu, event) {
|
|
200
|
+
if (event.key !== "Tab" || event.ctrlKey || event.altKey || event.metaKey) return false;
|
|
201
|
+
const items = selectedItems(nabu);
|
|
202
|
+
if (!items.length) return false;
|
|
203
|
+
moveItems(nabu, items, event.shiftKey);
|
|
204
|
+
return true;
|
|
205
|
+
}
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
export { Todos } from "./todos.svelte.js";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* The list container (bulleted, numbered or to-do): the `list:*` actions, Tab and Shift+Tab indentation over a selection, and the list slash entries. Always loaded together with `ListItemExtension`.
|
|
4
4
|
*/
|
|
5
|
-
export const ListExtension: import("
|
|
6
|
-
|
|
5
|
+
export const ListExtension: import("../../extend.js").Extension;
|
|
6
|
+
/**
|
|
7
|
+
* The list item block: Enter, Backspace and Tab behaviour inside lists, the to-do checkbox and `todo:toggle` (`Mod-Enter`). Always loaded together with `ListExtension`.
|
|
8
|
+
*/
|
|
9
|
+
export const ListItemExtension: import("../../extend.js").Extension;
|
|
7
10
|
import { List } from "./list.svelte.js";
|
|
8
11
|
import { ListItem } from "./list-item.svelte.js";
|
|
9
12
|
import ListComponent from "./List.svelte";
|
|
@@ -1,54 +1,65 @@
|
|
|
1
1
|
import { extension } from "../../utils/extensions";
|
|
2
2
|
import { List } from "./list.svelte.js";
|
|
3
3
|
import ListComponent from "./List.svelte";
|
|
4
|
-
import { ListItem
|
|
4
|
+
import { ListItem } from "./list-item.svelte.js";
|
|
5
5
|
import ListItemComponent from "./ListItem.svelte";
|
|
6
|
-
import {
|
|
6
|
+
import { normalizeLists } from "./normalize.js";
|
|
7
|
+
import { moveItems, selectedItems, handleTab } from "./indent.js";
|
|
8
|
+
import { convertToList } from "./to-list.js";
|
|
9
|
+
import { toggleSelectedTodos, selectedTodoItems } from "./todos-toggle.js";
|
|
7
10
|
import { onBeforeInput } from "./hooks/onBeforeInput.hook.js";
|
|
8
11
|
|
|
9
12
|
/**
|
|
10
|
-
*
|
|
13
|
+
* The list container (bulleted, numbered or to-do): the `list:*` actions, Tab and Shift+Tab indentation over a selection, and the list slash entries. Always loaded together with `ListItemExtension`.
|
|
11
14
|
*/
|
|
12
|
-
|
|
13
15
|
const ListExtension = extension("list", {
|
|
14
16
|
block: List,
|
|
15
17
|
component: ListComponent,
|
|
18
|
+
slashCommands: [
|
|
19
|
+
{ title: 'Bulleted list', icon: '•', aliases: ['bullet', 'ul', 'list'], group: 'lists', order: 0, run: (e) => e.exec('list:convert', { listType: 'bullet' }) },
|
|
20
|
+
{ title: 'Numbered list', icon: '1.', aliases: ['numbered', 'ol', 'ordered'], group: 'lists', order: 10, run: (e) => e.exec('list:convert', { listType: 'ordered' }) },
|
|
21
|
+
{ title: 'To-do list', icon: '✓', aliases: ['todo', 'task', 'checkbox', 'checklist'], group: 'lists', order: 20, run: (e) => e.exec('list:convert', { listType: 'todo' }) },
|
|
22
|
+
],
|
|
16
23
|
hooks: {
|
|
17
24
|
onBeforeInput,
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
previousBehavior.absorbs(list);
|
|
30
|
-
list.destroy();
|
|
31
|
-
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
}
|
|
25
|
+
// Tab / Shift+Tab with a selection spanning several blocks: the keydown would otherwise resolve to
|
|
26
|
+
// the lowest common ancestor (a List, a container, the root), which does not know about indenting.
|
|
27
|
+
// A selection inside one block is handled by ListItem.keydown.
|
|
28
|
+
onKeyDown: (nabu, event) => {
|
|
29
|
+
const sel = nabu.selection;
|
|
30
|
+
if (sel.anchorBlock === sel.focusBlock) return nabu.CONTINUE;
|
|
31
|
+
return handleTab(nabu, event) ? nabu.BREAK : nabu.CONTINUE;
|
|
32
|
+
},
|
|
33
|
+
// Runs before doc.commit(): the fixes belong to the same transaction (same undo step) as the
|
|
34
|
+
// operation that broke the structure. See normalize.js for the invariants.
|
|
35
|
+
onBeforeTransaction: normalizeLists,
|
|
35
36
|
}
|
|
36
37
|
});
|
|
37
38
|
|
|
39
|
+
/**
|
|
40
|
+
* The list item block: Enter, Backspace and Tab behaviour inside lists, the to-do checkbox and `todo:toggle` (`Mod-Enter`). Always loaded together with `ListExtension`.
|
|
41
|
+
*/
|
|
38
42
|
const ListItemExtension = extension("list-item", {
|
|
39
43
|
block: ListItem,
|
|
40
44
|
component: ListItemComponent,
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
unindentListItem(/** @type {ListItem} */ (block));
|
|
45
|
+
commands: {
|
|
46
|
+
// Mod-Enter on a to-do item: toggle the to-do item(s) the selection touches. `when` limits it to to-do items and
|
|
47
|
+
// `scope` makes it win over a generic Mod-Enter command (e.g. the regions' "split here") with the caret in an item.
|
|
48
|
+
'todo:toggle': {
|
|
49
|
+
label: 'Toggle to-do',
|
|
50
|
+
keys: ['Mod-Enter'],
|
|
51
|
+
scope: 'list-item',
|
|
52
|
+
when: (ctx) => selectedTodoItems(ctx.nabu).length > 0,
|
|
53
|
+
run: (nabu) => toggleSelectedTodos(nabu),
|
|
51
54
|
},
|
|
55
|
+
},
|
|
56
|
+
actions: {
|
|
57
|
+
// Every list item the selection touches (the anchor item for a caret).
|
|
58
|
+
'list:indent': (nabu) => { moveItems(nabu, selectedItems(nabu), false); },
|
|
59
|
+
'list:unindent': (nabu) => { moveItems(nabu, selectedItems(nabu), true); },
|
|
60
|
+
// Make the block holding the caret a list of that type: a list item changes type ON ITS OWN (the
|
|
61
|
+
// surrounding list is split around it), any other text block is replaced by a new list.
|
|
62
|
+
'list:convert': (nabu, data) => convertToList(nabu, data?.listType),
|
|
52
63
|
}
|
|
53
64
|
});
|
|
54
65
|
|
|
@@ -1,17 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* @typedef {NabuNode<{type: "list-item", text: LoroText}>} ListItemNode
|
|
6
|
-
*/
|
|
7
|
-
/**
|
|
8
|
-
* Indent a list item (Tab behavior): move into previous sibling's sublist, hoist own children.
|
|
2
|
+
* Indent a list item (Tab behavior): move under the previous sibling (into its last sublist when of the same type, else a new one).
|
|
3
|
+
* The selection (collapsed caret or range) is restored afterwards. See indent.js.
|
|
9
4
|
* @param {ListItem} item
|
|
10
5
|
* @returns {boolean}
|
|
11
6
|
*/
|
|
12
7
|
export function indentListItem(item: ListItem): boolean;
|
|
13
8
|
/**
|
|
14
9
|
* Unindent a list item (Shift+Tab behavior): move out of parent sublist, carry followers.
|
|
10
|
+
* The selection (collapsed caret or range) is restored afterwards. See indent.js.
|
|
15
11
|
* @param {ListItem} item
|
|
16
12
|
* @returns {boolean}
|
|
17
13
|
*/
|
|
@@ -19,13 +15,14 @@ export function unindentListItem(item: ListItem): boolean;
|
|
|
19
15
|
export class ListItem extends MegaBlock {
|
|
20
16
|
/**
|
|
21
17
|
* @param {import('../../utils/extensions.js').PasteBlock} pb
|
|
22
|
-
* @param {
|
|
18
|
+
* @param {Partial<import('../../utils/extensions.js').SerializeContext> & { listType?: string, index?: number, start?: number, run?: number, indent?: string }} helpers `run`: position of the list among adjacent lists (marker alternation), `indent`: what the item is nested under
|
|
23
19
|
*/
|
|
24
|
-
static toMarkdown(pb: import("../../utils/extensions.js").PasteBlock, {
|
|
25
|
-
recurse: (child: import("../../utils/extensions.js").PasteBlock, ctx?: object) => string;
|
|
20
|
+
static toMarkdown(pb: import("../../utils/extensions.js").PasteBlock, { listType, index, start, run, indent }: Partial<import("../../utils/extensions.js").SerializeContext> & {
|
|
26
21
|
listType?: string;
|
|
27
22
|
index?: number;
|
|
28
|
-
|
|
23
|
+
start?: number;
|
|
24
|
+
run?: number;
|
|
25
|
+
indent?: string;
|
|
29
26
|
}): string;
|
|
30
27
|
/**
|
|
31
28
|
* @param {import('../../utils/extensions.js').PasteBlock} pb
|
|
@@ -46,8 +43,8 @@ export class ListItem extends MegaBlock {
|
|
|
46
43
|
parseInline: (el: Element) => import("loro-crdt").Delta<string>[];
|
|
47
44
|
parseBlock: (el: Element) => import("../../utils/extensions.js").PasteBlock | null;
|
|
48
45
|
}): import("../../utils/extensions.js").PasteBlock | null;
|
|
49
|
-
/** @param {Nabu} nabu @param {string} type @param {
|
|
50
|
-
static create(nabu: Nabu, type: string, props?:
|
|
46
|
+
/** @param {Nabu} nabu @param {string} type @param {Record<string, any>} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
|
|
47
|
+
static create(nabu: Nabu, type: string, props?: Record<string, any>, parentId?: string | null, index?: number | null): ListItem;
|
|
51
48
|
/** @param {Nabu} nabu @param {ListItemNode} node */
|
|
52
49
|
constructor(nabu: Nabu, node: ListItemNode);
|
|
53
50
|
container: LoroText;
|
|
@@ -59,13 +56,24 @@ export class ListItem extends MegaBlock {
|
|
|
59
56
|
block: import("./list-item.svelte.js").ListItem;
|
|
60
57
|
}, {}, "">;
|
|
61
58
|
isTodo: boolean;
|
|
59
|
+
/**
|
|
60
|
+
* This item and its sublists as PasteBlocks (the shape the static serializers read).
|
|
61
|
+
* @returns {import('../../utils/extensions.js').PasteBlock}
|
|
62
|
+
*/
|
|
63
|
+
toPasteBlock(): import("../../utils/extensions.js").PasteBlock;
|
|
64
|
+
/** A to-do item carries its `checked` state onto the clipboard. */
|
|
65
|
+
get clipboardProps(): {
|
|
66
|
+
checked: boolean;
|
|
67
|
+
} | {
|
|
68
|
+
checked?: undefined;
|
|
69
|
+
};
|
|
62
70
|
/**
|
|
63
71
|
* Set the checked state and persist to Loro.
|
|
64
72
|
* @param {boolean} v
|
|
65
73
|
*/
|
|
66
74
|
setChecked(v: boolean): void;
|
|
67
75
|
/**
|
|
68
|
-
* Toggle the checked state.
|
|
76
|
+
* Toggle the checked state (the user's gesture: a click on the box, the key). Does nothing in a read-only editor.
|
|
69
77
|
*/
|
|
70
78
|
toggleChecked(): void;
|
|
71
79
|
get text(): string;
|
|
@@ -76,7 +84,10 @@ export class ListItem extends MegaBlock {
|
|
|
76
84
|
isCollapsed: boolean;
|
|
77
85
|
direction: "forward" | "backward" | "none";
|
|
78
86
|
} | null;
|
|
79
|
-
sublist:
|
|
87
|
+
/** Every sublist, in order: an item may hold several, of different types (`a > [ul[…], ol[…], ul[…]]`). */
|
|
88
|
+
sublists: Block[];
|
|
89
|
+
/** The first sublist (see `sublists`). */
|
|
90
|
+
sublist: Block;
|
|
80
91
|
/**
|
|
81
92
|
* A ListItem must always live inside a List.
|
|
82
93
|
* wrapOrphan() uses this to auto-create a List wrapper when a ListItem
|
|
@@ -90,10 +101,11 @@ export class ListItem extends MegaBlock {
|
|
|
90
101
|
props: () => Record<string, any>;
|
|
91
102
|
};
|
|
92
103
|
/**
|
|
93
|
-
*
|
|
104
|
+
* The sublist that receives a child of type `listType`: the LAST sublist when it has that type, otherwise
|
|
105
|
+
* a new one appended after the existing sublists (an item may hold several sublists of different types).
|
|
94
106
|
* @param {"bullet" | "ordered" | "todo"} [listType]
|
|
95
107
|
*/
|
|
96
|
-
getOrCreateSublist(listType?: "bullet" | "ordered" | "todo"): Block;
|
|
108
|
+
getOrCreateSublist(listType?: "bullet" | "ordered" | "todo"): Block | List;
|
|
97
109
|
/** @param {KeyboardEvent} event */
|
|
98
110
|
keydown(event: KeyboardEvent): boolean;
|
|
99
111
|
/** @param {import('../block.svelte.js').Block} block */
|
|
@@ -106,10 +118,12 @@ export class ListItem extends MegaBlock {
|
|
|
106
118
|
export type ListItemNode = NabuNode<{
|
|
107
119
|
type: "list-item";
|
|
108
120
|
text: LoroText;
|
|
121
|
+
checked?: boolean;
|
|
109
122
|
}>;
|
|
110
123
|
import { MegaBlock } from "../megablock.svelte.js";
|
|
111
124
|
import { LoroText } from "loro-crdt";
|
|
112
125
|
import { TextBehavior } from "../../behaviors/text";
|
|
113
126
|
import type { Block } from "..";
|
|
127
|
+
import { List } from "./list.svelte.js";
|
|
114
128
|
import type { Nabu } from "..";
|
|
115
129
|
import type { NabuNode } from "..";
|