@aionbuilders/nabu 0.1.0-beta.0 → 0.1.1
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 +531 -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 +195 -55
- package/dist/behaviors/text/text.behavior.svelte.js +531 -103
- 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 +32 -18
- 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 +1753 -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 +35 -14
- 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 +55 -8
- 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/composition.d.ts +28 -0
- package/dist/utils/composition.js +51 -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 +48 -20
- 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 +42 -0
- package/dist/utils/words.js +147 -0
- package/dist/vite.d.ts +9 -3
- package/dist/vite.js +19 -8
- package/package.json +56 -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
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
<script>
|
|
2
2
|
import RichText from '../../behaviors/text/RichText.svelte';
|
|
3
|
-
import { getPlaceholder } from '../../utils/placeholders.js';
|
|
4
3
|
/** @type {{block: import('./paragraph.svelte.js').Paragraph}}*/
|
|
5
4
|
let {block} = $props();
|
|
6
5
|
</script>
|
|
@@ -9,7 +8,7 @@
|
|
|
9
8
|
bind:this={block.element}
|
|
10
9
|
data-block-id={block.id}
|
|
11
10
|
data-block-type="paragraph"
|
|
12
|
-
data-placeholder={block.
|
|
11
|
+
data-placeholder={block.placeholder}
|
|
13
12
|
class="nabu-paragraph"
|
|
14
13
|
class:debug={block.nabu.debugging}
|
|
15
14
|
class:selected={block.selected}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* The paragraph block, the default text block: the `paragraph` action (turn a block back into text) and the "Text" slash entry.
|
|
3
3
|
*/
|
|
4
|
-
export const ParagraphExtension: import("
|
|
4
|
+
export const ParagraphExtension: import("../../extend.js").Extension;
|
|
5
5
|
import ParagraphComponent from "./Paragraph.svelte";
|
|
6
6
|
import { Paragraph } from "./paragraph.svelte.js";
|
|
7
7
|
export { ParagraphComponent, Paragraph };
|
|
@@ -6,9 +6,35 @@ import ParagraphComponent from "./Paragraph.svelte";
|
|
|
6
6
|
* @import { Nabu } from "../nabu.svelte.js";
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* Whether the pending (uncommitted) transaction holds a tree `delete` or `move`: the only ops that can leave the root without a node.
|
|
11
|
+
* @param {Nabu} nabu
|
|
12
|
+
* @returns {boolean}
|
|
13
|
+
*/
|
|
14
|
+
function mayEmptyRoot(nabu) {
|
|
15
|
+
const pending = nabu.doc.getUncommittedOpsAsJson();
|
|
16
|
+
if (!pending) return false;
|
|
17
|
+
const treeId = nabu.tree.id;
|
|
18
|
+
for (const change of pending.changes) {
|
|
19
|
+
for (const op of change.ops) {
|
|
20
|
+
if (op.container !== treeId) continue;
|
|
21
|
+
const type = /** @type {any} */ (op.content).type;
|
|
22
|
+
if (type === "delete" || type === "move") return true;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The paragraph block, the default text block: the `paragraph` action (turn a block back into text) and the "Text" slash entry.
|
|
30
|
+
*/
|
|
9
31
|
const ParagraphExtension = extension("paragraph", {
|
|
10
32
|
block: Paragraph,
|
|
33
|
+
defaultBlock: true,
|
|
11
34
|
component: ParagraphComponent,
|
|
35
|
+
slashCommands: [
|
|
36
|
+
{ title: 'Text', icon: '¶', aliases: ['paragraph', 'p', 'plain'], group: 'text', order: 0, run: (e) => e.exec('paragraph') },
|
|
37
|
+
],
|
|
12
38
|
actions: {
|
|
13
39
|
'paragraph': (nabu) => nabu.exec('block:transform', { type: 'paragraph' }),
|
|
14
40
|
'gap:click': (nabu, data) => {
|
|
@@ -29,20 +55,16 @@ const ParagraphExtension = extension("paragraph", {
|
|
|
29
55
|
|
|
30
56
|
}
|
|
31
57
|
},
|
|
32
|
-
/**
|
|
58
|
+
/**
|
|
59
|
+
* "The document is never empty". Only a transaction that DELETES or MOVES a node can have emptied the root, so the
|
|
60
|
+
* check (`tree.roots()`, O(roots)) is skipped for everything else: a keystroke commits text ops only, and pays nothing.
|
|
61
|
+
* @param {Nabu} nabu
|
|
62
|
+
*/
|
|
33
63
|
onBeforeTransaction: (nabu) => {
|
|
34
|
-
|
|
35
|
-
roots.
|
|
36
|
-
const type = root.data.get("type");
|
|
37
|
-
root.children()?.forEach(child => {
|
|
38
|
-
const childType = child.data.get("type");
|
|
39
|
-
});
|
|
40
|
-
})
|
|
41
|
-
if (roots.length === 0) {
|
|
42
|
-
nabu.insert("paragraph", { text: "" });
|
|
43
|
-
}
|
|
64
|
+
if (!mayEmptyRoot(nabu)) return;
|
|
65
|
+
if (nabu.tree.roots().length === 0) nabu.insert("paragraph", { text: "" });
|
|
44
66
|
},
|
|
45
|
-
/** @param {Nabu} nabu @param {Paragraph} block @param {Event} event @param {
|
|
67
|
+
/** @param {Nabu} nabu @param {Paragraph} block @param {Event | null} event @param {import('../../utils/extensions.js').SplitData} data */
|
|
46
68
|
onSplit: (nabu, block, event, data) => {
|
|
47
69
|
const { offset, delta } = data;
|
|
48
70
|
|
|
@@ -52,7 +74,7 @@ const ParagraphExtension = extension("paragraph", {
|
|
|
52
74
|
const parent = block.node.parent();
|
|
53
75
|
const parentId = parent?.id.toString() || null;
|
|
54
76
|
|
|
55
|
-
const newBlock = nabu.insert("paragraph", { delta }, parentId, currentIndex + 1);
|
|
77
|
+
const newBlock = nabu.insert("paragraph", { delta }, parentId, /** @type {number} */ (currentIndex) + 1);
|
|
56
78
|
|
|
57
79
|
block.commit().then(() => nabu.selection.setCursor(newBlock, 0));
|
|
58
80
|
|
|
@@ -5,36 +5,68 @@
|
|
|
5
5
|
* @typedef {NabuNode<{type: "paragraph", text: LoroText}>} ParagraphNode
|
|
6
6
|
*/
|
|
7
7
|
export class Paragraph extends TextBlock {
|
|
8
|
+
static placeholder: string;
|
|
8
9
|
/** @param {import('../../utils/extensions.js').PasteBlock} pb */
|
|
9
10
|
static toMarkdown(pb: import("../../utils/extensions.js").PasteBlock): string;
|
|
10
11
|
/** @param {import('../../utils/extensions.js').PasteBlock} pb */
|
|
11
12
|
static toHtml(pb: import("../../utils/extensions.js").PasteBlock): string;
|
|
12
|
-
|
|
13
|
+
/**
|
|
14
|
+
* The paragraph is the catch-all (priority 0): every line no other rule claims, so also an indented line (4 spaces: NOT
|
|
15
|
+
* code, the indent is trimmed), a footnote (`[^1]`, `[^1]: text` stay literal text) and a line of HTML (`<div>`: literal
|
|
16
|
+
* text, never interpreted). The GFM pipe table is a rule just above it (see table.markdown.js).
|
|
17
|
+
*/
|
|
18
|
+
static markdownRules: ({
|
|
13
19
|
priority: number;
|
|
14
20
|
detect: () => boolean;
|
|
15
|
-
consume(lines: string[], i: number): number;
|
|
16
|
-
|
|
21
|
+
consume(lines: string[], i: number, { joinLines, interrupts }?: any): number;
|
|
22
|
+
table?: undefined;
|
|
23
|
+
interruptsParagraph?: undefined;
|
|
24
|
+
} | {
|
|
25
|
+
priority: number;
|
|
26
|
+
table: boolean;
|
|
27
|
+
interruptsParagraph: boolean;
|
|
28
|
+
detect: (line: string, lines: string[], i: number) => boolean;
|
|
29
|
+
consume: (lines: string[], i: number, helpers: any) => number;
|
|
30
|
+
})[];
|
|
17
31
|
/**
|
|
32
|
+
* Imported lines join into one paragraph: with a space (soft break; `helpers.softBreak` overrides it), or with a newline
|
|
33
|
+
* when the line ends with a hard break (two trailing spaces or a backslash). An image inside the text splits the
|
|
34
|
+
* paragraph: text before, image block, text after (needs `helpers.parseInlineSegments`). The leading indent of every
|
|
35
|
+
* line is trimmed. With the table rule, the lines are a table: one paragraph per row.
|
|
18
36
|
* @param {string[]} lines
|
|
19
|
-
* @param {{ parseInline: (text: string) => import('loro-crdt').Delta<string>[] }} helpers
|
|
20
|
-
* @
|
|
37
|
+
* @param {{ parseInline: (text: string) => import('loro-crdt').Delta<string>[], parseInlineSegments?: (text: string) => Array<{ delta?: import('loro-crdt').Delta<string>[], image?: { alt: string, ref: string } }>, joinLines?: boolean, softBreak?: string }} helpers
|
|
38
|
+
* @param {import('../../utils/extensions.js').MarkdownRule} [rule]
|
|
39
|
+
* @returns {import('../../utils/extensions.js').PasteBlock | import('../../utils/extensions.js').PasteBlock[] | null}
|
|
21
40
|
*/
|
|
22
|
-
static fromMarkdown(lines: string[], { parseInline }: {
|
|
41
|
+
static fromMarkdown(lines: string[], { parseInline, parseInlineSegments, joinLines, softBreak }: {
|
|
23
42
|
parseInline: (text: string) => import("loro-crdt").Delta<string>[];
|
|
24
|
-
|
|
25
|
-
|
|
43
|
+
parseInlineSegments?: (text: string) => Array<{
|
|
44
|
+
delta?: import("loro-crdt").Delta<string>[];
|
|
45
|
+
image?: {
|
|
46
|
+
alt: string;
|
|
47
|
+
ref: string;
|
|
48
|
+
};
|
|
49
|
+
}>;
|
|
50
|
+
joinLines?: boolean;
|
|
51
|
+
softBreak?: string;
|
|
52
|
+
}, rule?: import("../../utils/extensions.js").MarkdownRule): import("../../utils/extensions.js").PasteBlock | import("../../utils/extensions.js").PasteBlock[] | null;
|
|
53
|
+
static htmlRules: ({
|
|
26
54
|
selector: string;
|
|
27
|
-
|
|
55
|
+
priority?: undefined;
|
|
56
|
+
} | {
|
|
57
|
+
selector: string;
|
|
58
|
+
priority: number;
|
|
59
|
+
})[];
|
|
28
60
|
/**
|
|
29
61
|
* @param {Element} el
|
|
30
62
|
* @param {{ parseInline: (el: Element) => import('loro-crdt').Delta<string>[] }} helpers
|
|
31
|
-
* @returns {import('../../utils/extensions.js').PasteBlock | null}
|
|
63
|
+
* @returns {import('../../utils/extensions.js').PasteBlock | import('../../utils/extensions.js').PasteBlock[] | null}
|
|
32
64
|
*/
|
|
33
|
-
static fromHTML(el: Element,
|
|
65
|
+
static fromHTML(el: Element, helpers: {
|
|
34
66
|
parseInline: (el: Element) => import("loro-crdt").Delta<string>[];
|
|
35
|
-
}): import("../../utils/extensions.js").PasteBlock | null;
|
|
36
|
-
/** @param {Nabu} nabu @param {string} type @param {
|
|
37
|
-
static create(nabu: Nabu, type: string, props?:
|
|
67
|
+
}): import("../../utils/extensions.js").PasteBlock | import("../../utils/extensions.js").PasteBlock[] | null;
|
|
68
|
+
/** @param {Nabu} nabu @param {string} type @param {Record<string, any>} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
|
|
69
|
+
static create(nabu: Nabu, type: string, props?: Record<string, any>, parentId?: string | null, index?: number | null): Paragraph;
|
|
38
70
|
/** @param {Nabu} nabu @param {ParagraphNode} node */
|
|
39
71
|
constructor(nabu: Nabu, node: ParagraphNode);
|
|
40
72
|
component: import("svelte").Component<any, any, string> | import("svelte").Component<{
|
|
@@ -2,6 +2,8 @@ import { TextBlock } from "../text-block.svelte.js";
|
|
|
2
2
|
import { LoroText } from "loro-crdt";
|
|
3
3
|
import ParagraphComponent from "./Paragraph.svelte";
|
|
4
4
|
import { deltaToHtml, deltaToMarkdown } from "../../behaviors/text";
|
|
5
|
+
import { tableToRowParagraphs } from "./table.html.js";
|
|
6
|
+
import { isTableStart, tableLength, markdownTableToRowParagraphs } from "./table.markdown.js";
|
|
5
7
|
|
|
6
8
|
/**
|
|
7
9
|
* @import { Nabu, NabuNode } from "../nabu.svelte.js";
|
|
@@ -25,6 +27,8 @@ export class Paragraph extends TextBlock {
|
|
|
25
27
|
this.serializers.set('application/x-nabu+json', (ctx = {}) => this.behavior.toClipboardBlock(ctx));
|
|
26
28
|
}
|
|
27
29
|
|
|
30
|
+
static placeholder = 'Start writing...';
|
|
31
|
+
|
|
28
32
|
component = $derived(this.nabu.components.get("paragraph") || ParagraphComponent);
|
|
29
33
|
|
|
30
34
|
/** @param {import('../../utils/extensions.js').PasteBlock} pb */
|
|
@@ -37,53 +41,110 @@ export class Paragraph extends TextBlock {
|
|
|
37
41
|
return `<p>${deltaToHtml(pb.delta || [])}</p>`;
|
|
38
42
|
}
|
|
39
43
|
|
|
44
|
+
/**
|
|
45
|
+
* The paragraph is the catch-all (priority 0): every line no other rule claims, so also an indented line (4 spaces: NOT
|
|
46
|
+
* code, the indent is trimmed), a footnote (`[^1]`, `[^1]: text` stay literal text) and a line of HTML (`<div>`: literal
|
|
47
|
+
* text, never interpreted). The GFM pipe table is a rule just above it (see table.markdown.js).
|
|
48
|
+
*/
|
|
40
49
|
static markdownRules = [
|
|
41
50
|
{
|
|
42
51
|
priority: 0, // catch-all — must be lowest
|
|
43
52
|
detect: () => true,
|
|
44
|
-
consume(/** @type {string[]} */ lines, /** @type {number} */ i) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
53
|
+
consume(/** @type {string[]} */ lines, /** @type {number} */ i, /** @type {any} */ { joinLines, interrupts } = {}) {
|
|
54
|
+
// Paste: one line = one block. Import (CommonMark): lines up to a blank one are one paragraph, unless a line
|
|
55
|
+
// starts a block of its own (a fence, a heading, a quote, a list, a divider, a table).
|
|
56
|
+
if (!joinLines) return 1;
|
|
57
|
+
let j = i + 1;
|
|
58
|
+
while (j < lines.length && lines[j].trim() && !interrupts?.(lines, j)) j++;
|
|
59
|
+
return j - i;
|
|
48
60
|
},
|
|
49
|
-
}
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
// Just above the catch-all, below every real block: a future table block (priority >= 10) takes over.
|
|
64
|
+
priority: 1,
|
|
65
|
+
table: true,
|
|
66
|
+
interruptsParagraph: true,
|
|
67
|
+
detect: (/** @type {string} */ line, /** @type {string[]} */ lines, /** @type {number} */ i) => isTableStart(lines, i),
|
|
68
|
+
consume: (/** @type {string[]} */ lines, /** @type {number} */ i, /** @type {any} */ helpers) => tableLength(lines, i, helpers),
|
|
69
|
+
},
|
|
50
70
|
];
|
|
51
71
|
|
|
52
72
|
/**
|
|
73
|
+
* Imported lines join into one paragraph: with a space (soft break; `helpers.softBreak` overrides it), or with a newline
|
|
74
|
+
* when the line ends with a hard break (two trailing spaces or a backslash). An image inside the text splits the
|
|
75
|
+
* paragraph: text before, image block, text after (needs `helpers.parseInlineSegments`). The leading indent of every
|
|
76
|
+
* line is trimmed. With the table rule, the lines are a table: one paragraph per row.
|
|
53
77
|
* @param {string[]} lines
|
|
54
|
-
* @param {{ parseInline: (text: string) => import('loro-crdt').Delta<string>[] }} helpers
|
|
55
|
-
* @
|
|
78
|
+
* @param {{ parseInline: (text: string) => import('loro-crdt').Delta<string>[], parseInlineSegments?: (text: string) => Array<{ delta?: import('loro-crdt').Delta<string>[], image?: { alt: string, ref: string } }>, joinLines?: boolean, softBreak?: string }} helpers
|
|
79
|
+
* @param {import('../../utils/extensions.js').MarkdownRule} [rule]
|
|
80
|
+
* @returns {import('../../utils/extensions.js').PasteBlock | import('../../utils/extensions.js').PasteBlock[] | null}
|
|
56
81
|
*/
|
|
57
|
-
static fromMarkdown(lines, { parseInline }) {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
82
|
+
static fromMarkdown(lines, { parseInline, parseInlineSegments, joinLines, softBreak }, rule) {
|
|
83
|
+
if (rule?.table) return markdownTableToRowParagraphs(lines, { parseInline });
|
|
84
|
+
let text = '';
|
|
85
|
+
lines.forEach((line, k) => {
|
|
86
|
+
if (k === 0) {
|
|
87
|
+
// A paste is a fragment of text (` pasted` keeps its space); on import, and for an indent that looks like code
|
|
88
|
+
// (4 columns or a TAB), the indent goes: indented text is a paragraph, not code.
|
|
89
|
+
text = joinLines || /^(?: {4}|\s*\t)/.test(line) ? line.replace(/^\s+/, '') : line;
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
text += (/(?: {2,}|\\)$/.test(lines[k - 1]) ? '\n' : (softBreak ?? ' ')) + line.replace(/^\s+/, '');
|
|
93
|
+
});
|
|
94
|
+
// Pasted, a bare paragraph is plain text ('both'): it must not retype an empty block
|
|
95
|
+
// (heading, quote…). Imported, it is a whole block.
|
|
96
|
+
const partial = joinLines ? false : 'both';
|
|
97
|
+
const segments = parseInlineSegments ? parseInlineSegments(text) : [{ delta: parseInline(text) }];
|
|
98
|
+
if (!segments.some((seg) => seg.image)) {
|
|
99
|
+
const delta = segments[0]?.delta ?? [];
|
|
100
|
+
return delta.length ? { type: 'paragraph', delta, partial } : null;
|
|
101
|
+
}
|
|
102
|
+
/** @type {import('../../utils/extensions.js').PasteBlock[]} */
|
|
103
|
+
const blocks = [];
|
|
104
|
+
segments.forEach((seg, k) => {
|
|
105
|
+
if (seg.image) { blocks.push({ type: 'image', props: { alt: seg.image.alt, ref: seg.image.ref }, partial: false }); return; }
|
|
106
|
+
// The block boundary replaces the space that touched the image.
|
|
107
|
+
const delta = (seg.delta ?? []).map((op) => ({ ...op }));
|
|
108
|
+
const first = /** @type {any} */ (delta[0]);
|
|
109
|
+
const last = /** @type {any} */ (delta[delta.length - 1]);
|
|
110
|
+
if (k > 0 && first) first.insert = first.insert.replace(/^\s+/, '');
|
|
111
|
+
if (k < segments.length - 1 && last) last.insert = last.insert.replace(/\s+$/, '');
|
|
112
|
+
const clean = delta.filter((op) => /** @type {any} */ (op).insert);
|
|
113
|
+
if (clean.length) blocks.push({ type: 'paragraph', delta: clean, partial });
|
|
114
|
+
});
|
|
115
|
+
return blocks.length ? blocks : null;
|
|
61
116
|
}
|
|
62
117
|
|
|
63
118
|
static htmlRules = [
|
|
64
119
|
{ selector: 'p' },
|
|
65
120
|
{ selector: 'blockquote' },
|
|
121
|
+
// No table block yet: a pasted table gives one paragraph per row, cells separated by a TAB. LOWEST priority (below
|
|
122
|
+
// every block rule): an extension with a real table block declares its own rule for `table` at a higher priority
|
|
123
|
+
// and takes over, nothing else to change. See table.html.js.
|
|
124
|
+
{ selector: 'table', priority: -100 },
|
|
66
125
|
];
|
|
67
126
|
|
|
68
127
|
/**
|
|
69
128
|
* @param {Element} el
|
|
70
129
|
* @param {{ parseInline: (el: Element) => import('loro-crdt').Delta<string>[] }} helpers
|
|
71
|
-
* @returns {import('../../utils/extensions.js').PasteBlock | null}
|
|
130
|
+
* @returns {import('../../utils/extensions.js').PasteBlock | import('../../utils/extensions.js').PasteBlock[] | null}
|
|
72
131
|
*/
|
|
73
|
-
static fromHTML(el,
|
|
132
|
+
static fromHTML(el, helpers) {
|
|
133
|
+
const { parseInline } = helpers;
|
|
134
|
+
if (el.localName === 'table') return tableToRowParagraphs(el, helpers);
|
|
74
135
|
const delta = parseInline(el);
|
|
75
136
|
if (!delta.length) return null;
|
|
76
137
|
return { type: 'paragraph', delta, partial: false };
|
|
77
138
|
}
|
|
78
139
|
|
|
79
|
-
/** @param {Nabu} nabu @param {string} type @param {
|
|
140
|
+
/** @param {Nabu} nabu @param {string} type @param {Record<string, any>} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
|
|
80
141
|
static create(nabu, type, props = {}, parentId = null, index = null) {
|
|
81
|
-
const node = nabu.tree.createNode(parentId || undefined, index
|
|
142
|
+
const node = nabu.tree.createNode(/** @type {import('loro-crdt').TreeID | undefined} */ (parentId || undefined), index ?? undefined);
|
|
82
143
|
node.data.set("type", "paragraph");
|
|
83
144
|
const container = node.data.setContainer("text", new LoroText());
|
|
84
145
|
if (props.text) container.insert(0, props.text || "Start writing...");
|
|
85
146
|
if (props.delta) container.applyDelta([...props.delta]);
|
|
86
|
-
const block = new Paragraph(nabu, node);
|
|
147
|
+
const block = new Paragraph(nabu, /** @type {ParagraphNode} */ (node));
|
|
87
148
|
return block;
|
|
88
149
|
}
|
|
89
150
|
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param {Element} table
|
|
3
|
+
* @param {{ parseInline: (el: Element) => import('loro-crdt').Delta<string>[] }} helpers
|
|
4
|
+
* @returns {PasteBlock[]}
|
|
5
|
+
*/
|
|
6
|
+
export function tableToRowParagraphs(table: Element, { parseInline }: {
|
|
7
|
+
parseInline: (el: Element) => import("loro-crdt").Delta<string>[];
|
|
8
|
+
}): PasteBlock[];
|
|
9
|
+
import type { PasteBlock } from "../../utils/extensions.js";
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pasting an HTML `<table>` while the editor has no table block (yet): EACH ROW becomes ONE paragraph, its cells
|
|
3
|
+
* separated by a TAB character. The same lines a spreadsheet puts in `text/plain` (TSV), with the inline marks of the cells
|
|
4
|
+
* kept.
|
|
5
|
+
*
|
|
6
|
+
* - blocks nested in a cell (paragraphs, lists, another table…) are flattened to text, joined with spaces;
|
|
7
|
+
* - a header row is a row like the others; a `colspan` adds the empty cells it covers (the columns stay aligned);
|
|
8
|
+
* - a row without any text is dropped; a `<caption>` is a paragraph of its own, before the rows.
|
|
9
|
+
*
|
|
10
|
+
* It is registered by the paragraph block as a LOW-PRIORITY html rule (see `Paragraph.htmlRules`): a future table block
|
|
11
|
+
* declares its own rule for `table` at a higher priority and takes over without anything else to change.
|
|
12
|
+
*
|
|
13
|
+
* @import { PasteBlock } from "../../utils/extensions.js";
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/** Elements of a cell that carry structure the paragraph cannot hold: replaced by their content, then a space. */
|
|
17
|
+
const NESTED_STRUCTURE = 'table, thead, tbody, tfoot, tr, td, th, caption, ul, ol, li, dl, dt, dd';
|
|
18
|
+
/** Largest `colspan` honoured (a hostile value must not allocate a huge row). */
|
|
19
|
+
const MAX_COLSPAN = 50;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* A cell with its nested structure unwrapped: nested tables / lists become spans of text followed by a space. The result is
|
|
23
|
+
* a detached `<div>` holding a `<span>` that carries the cell's own style (a `font-weight:bold` on a `<td>` bolds its text):
|
|
24
|
+
* `parseInline` reads the children of the element it is given, not the element itself.
|
|
25
|
+
* @param {Element} cell
|
|
26
|
+
* @returns {Element}
|
|
27
|
+
*/
|
|
28
|
+
function flattenCell(cell) {
|
|
29
|
+
const doc = cell.ownerDocument;
|
|
30
|
+
const copy = /** @type {Element} */ (cell.cloneNode(true));
|
|
31
|
+
// Document order lists an element before its descendants: walk it backwards so inner elements go first.
|
|
32
|
+
for (const nested of [...copy.querySelectorAll(NESTED_STRUCTURE)].reverse()) {
|
|
33
|
+
const span = doc.createElement('span');
|
|
34
|
+
while (nested.firstChild) span.appendChild(nested.firstChild);
|
|
35
|
+
span.appendChild(doc.createTextNode(' '));
|
|
36
|
+
nested.replaceWith(span);
|
|
37
|
+
}
|
|
38
|
+
const root = doc.createElement('div');
|
|
39
|
+
const own = doc.createElement('span');
|
|
40
|
+
const style = cell.getAttribute('style');
|
|
41
|
+
if (style) own.setAttribute('style', style);
|
|
42
|
+
while (copy.firstChild) own.appendChild(copy.firstChild);
|
|
43
|
+
root.appendChild(own);
|
|
44
|
+
return root;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The delta of a cell on ONE line: line breaks (`<br>`, block boundaries) become spaces, runs of spaces collapse, the ends
|
|
49
|
+
* are trimmed. Marks are kept.
|
|
50
|
+
* @param {import('loro-crdt').Delta<string>[]} delta
|
|
51
|
+
* @returns {import('loro-crdt').Delta<string>[]}
|
|
52
|
+
*/
|
|
53
|
+
function oneLine(delta) {
|
|
54
|
+
/** @type {import('loro-crdt').Delta<string>[]} */
|
|
55
|
+
const out = [];
|
|
56
|
+
for (const op of delta) {
|
|
57
|
+
if (typeof op.insert !== 'string') continue;
|
|
58
|
+
const text = op.insert.replace(/[\r\n\t]+/g, ' ');
|
|
59
|
+
out.push(op.attributes ? { insert: text, attributes: op.attributes } : { insert: text });
|
|
60
|
+
}
|
|
61
|
+
// collapse spaces across op boundaries, then trim both ends
|
|
62
|
+
let previousSpace = true; // a leading space is dropped
|
|
63
|
+
for (const op of out) {
|
|
64
|
+
let collapsed = '';
|
|
65
|
+
for (const ch of /** @type {string} */ (op.insert)) {
|
|
66
|
+
const space = ch === ' ';
|
|
67
|
+
if (space && previousSpace) continue;
|
|
68
|
+
collapsed += ch;
|
|
69
|
+
previousSpace = space;
|
|
70
|
+
}
|
|
71
|
+
op.insert = collapsed;
|
|
72
|
+
}
|
|
73
|
+
for (let i = out.length - 1; i >= 0; i--) {
|
|
74
|
+
out[i].insert = /** @type {string} */ (out[i].insert).replace(/ +$/, '');
|
|
75
|
+
if (out[i].insert) break;
|
|
76
|
+
}
|
|
77
|
+
return out.filter((op) => op.insert);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* @param {Element} table
|
|
82
|
+
* @param {{ parseInline: (el: Element) => import('loro-crdt').Delta<string>[] }} helpers
|
|
83
|
+
* @returns {PasteBlock[]}
|
|
84
|
+
*/
|
|
85
|
+
export function tableToRowParagraphs(table, { parseInline }) {
|
|
86
|
+
/** @type {PasteBlock[]} */
|
|
87
|
+
const blocks = [];
|
|
88
|
+
const caption = [...table.children].find((c) => c.localName === 'caption');
|
|
89
|
+
if (caption) {
|
|
90
|
+
const delta = oneLine(parseInline(flattenCell(caption)));
|
|
91
|
+
if (delta.length) blocks.push({ type: 'paragraph', delta, partial: false });
|
|
92
|
+
}
|
|
93
|
+
// Rows of THIS table: its own rows (in a section or not), never those of a table nested in a cell.
|
|
94
|
+
const rows = [...table.querySelectorAll('tr')].filter((tr) => tr.closest('table') === table);
|
|
95
|
+
for (const tr of rows) {
|
|
96
|
+
/** @type {import('loro-crdt').Delta<string>[]} */
|
|
97
|
+
const delta = [];
|
|
98
|
+
let cells = 0;
|
|
99
|
+
for (const cell of tr.children) {
|
|
100
|
+
const tag = cell.tagName.toLowerCase();
|
|
101
|
+
if (tag !== 'td' && tag !== 'th') continue;
|
|
102
|
+
if (cells++) delta.push({ insert: '\t' });
|
|
103
|
+
delta.push(...oneLine(parseInline(flattenCell(cell))));
|
|
104
|
+
const span = Math.min(MAX_COLSPAN, Math.max(1, parseInt(cell.getAttribute('colspan') ?? '1', 10) || 1));
|
|
105
|
+
for (let i = 1; i < span; i++) { delta.push({ insert: '\t' }); cells++; }
|
|
106
|
+
}
|
|
107
|
+
if (!delta.some((op) => typeof op.insert === 'string' && /\S/.test(op.insert))) continue; // no text in the row
|
|
108
|
+
blocks.push({ type: 'paragraph', delta, partial: false });
|
|
109
|
+
}
|
|
110
|
+
return blocks;
|
|
111
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The cells of a table row: split on the pipes that are not escaped, the edge pipes dropped, each cell trimmed.
|
|
3
|
+
* @param {string} line
|
|
4
|
+
* @returns {string[]}
|
|
5
|
+
*/
|
|
6
|
+
export function splitRow(line: string): string[];
|
|
7
|
+
/**
|
|
8
|
+
* Whether a table starts at line `i`: a header row with a pipe, then a delimiter row (with a pipe) of the same width.
|
|
9
|
+
* @param {string[]} lines
|
|
10
|
+
* @param {number} i
|
|
11
|
+
* @returns {boolean}
|
|
12
|
+
*/
|
|
13
|
+
export function isTableStart(lines: string[], i: number): boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Number of lines of the table that starts at `i`.
|
|
16
|
+
* @param {string[]} lines
|
|
17
|
+
* @param {number} i
|
|
18
|
+
* @param {{ interrupts?: (lines: string[], k: number) => boolean }} [helpers]
|
|
19
|
+
* @returns {number}
|
|
20
|
+
*/
|
|
21
|
+
export function tableLength(lines: string[], i: number, helpers?: {
|
|
22
|
+
interrupts?: (lines: string[], k: number) => boolean;
|
|
23
|
+
}): number;
|
|
24
|
+
/**
|
|
25
|
+
* @param {string[]} lines the table lines: header, delimiter, rows
|
|
26
|
+
* @param {{ parseInline: (text: string) => import('loro-crdt').Delta<string>[] }} helpers
|
|
27
|
+
* @returns {PasteBlock[] | null}
|
|
28
|
+
*/
|
|
29
|
+
export function markdownTableToRowParagraphs(lines: string[], { parseInline }: {
|
|
30
|
+
parseInline: (text: string) => import("loro-crdt").Delta<string>[];
|
|
31
|
+
}): PasteBlock[] | null;
|
|
32
|
+
import type { PasteBlock } from "../../utils/extensions.js";
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pasting / importing a GFM pipe table while the editor has no table block (yet): EACH ROW becomes ONE paragraph, its
|
|
3
|
+
* cells separated by a TAB character, the inline markdown of the cells parsed. The header row is a row like the others,
|
|
4
|
+
* the delimiter row (`|---|:--:|`, whose colons only say how a cell is aligned) is dropped, the alignment is lost.
|
|
5
|
+
* The same shape as a pasted HTML table (see table.html.js).
|
|
6
|
+
*
|
|
7
|
+
* - a table is a header row, a delimiter row with the SAME number of cells, then the rows (a line with a pipe) up to a
|
|
8
|
+
* blank line or the start of another block; without a delimiter row it is plain text;
|
|
9
|
+
* - a row with fewer cells is padded with empty ones, extra cells are dropped (GFM); `\|` is a pipe inside a cell;
|
|
10
|
+
* - a row without any text is dropped.
|
|
11
|
+
*
|
|
12
|
+
* It is registered by the paragraph block as a LOW-PRIORITY markdown rule (see `Paragraph.markdownRules`): a future table
|
|
13
|
+
* block declares its own rule at a higher priority and takes over without anything else to change.
|
|
14
|
+
*
|
|
15
|
+
* @import { PasteBlock } from "../../utils/extensions.js";
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const DELIMITER_ROW = /^ {0,3}\|?[ \t]*:?-+:?[ \t]*(?:\|[ \t]*:?-+:?[ \t]*)*\|?[ \t]*$/;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The cells of a table row: split on the pipes that are not escaped, the edge pipes dropped, each cell trimmed.
|
|
22
|
+
* @param {string} line
|
|
23
|
+
* @returns {string[]}
|
|
24
|
+
*/
|
|
25
|
+
export function splitRow(line) {
|
|
26
|
+
let row = line.trim();
|
|
27
|
+
if (row.startsWith('|')) row = row.slice(1);
|
|
28
|
+
if (row.endsWith('|') && !row.endsWith('\\|')) row = row.slice(0, -1);
|
|
29
|
+
/** @type {string[]} */
|
|
30
|
+
const cells = [];
|
|
31
|
+
let cell = '';
|
|
32
|
+
for (let i = 0; i < row.length; i++) {
|
|
33
|
+
if (row[i] === '\\' && i + 1 < row.length) { cell += row[i] + row[i + 1]; i++; continue; }
|
|
34
|
+
if (row[i] === '|') { cells.push(cell.trim()); cell = ''; continue; }
|
|
35
|
+
cell += row[i];
|
|
36
|
+
}
|
|
37
|
+
cells.push(cell.trim());
|
|
38
|
+
return cells;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Whether a table starts at line `i`: a header row with a pipe, then a delimiter row (with a pipe) of the same width.
|
|
43
|
+
* @param {string[]} lines
|
|
44
|
+
* @param {number} i
|
|
45
|
+
* @returns {boolean}
|
|
46
|
+
*/
|
|
47
|
+
export function isTableStart(lines, i) {
|
|
48
|
+
const header = lines[i];
|
|
49
|
+
const delimiter = lines[i + 1];
|
|
50
|
+
if (header === undefined || delimiter === undefined) return false;
|
|
51
|
+
if (!header.includes('|') || !delimiter.includes('|') || !DELIMITER_ROW.test(delimiter)) return false;
|
|
52
|
+
return splitRow(header).length === splitRow(delimiter).length;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Number of lines of the table that starts at `i`.
|
|
57
|
+
* @param {string[]} lines
|
|
58
|
+
* @param {number} i
|
|
59
|
+
* @param {{ interrupts?: (lines: string[], k: number) => boolean }} [helpers]
|
|
60
|
+
* @returns {number}
|
|
61
|
+
*/
|
|
62
|
+
export function tableLength(lines, i, helpers) {
|
|
63
|
+
let j = i + 2;
|
|
64
|
+
while (j < lines.length && lines[j].trim() && lines[j].includes('|') && !helpers?.interrupts?.(lines, j)) j++;
|
|
65
|
+
return j - i;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* @param {string[]} lines the table lines: header, delimiter, rows
|
|
70
|
+
* @param {{ parseInline: (text: string) => import('loro-crdt').Delta<string>[] }} helpers
|
|
71
|
+
* @returns {PasteBlock[] | null}
|
|
72
|
+
*/
|
|
73
|
+
export function markdownTableToRowParagraphs(lines, { parseInline }) {
|
|
74
|
+
const width = splitRow(lines[0]).length;
|
|
75
|
+
/** @type {PasteBlock[]} */
|
|
76
|
+
const blocks = [];
|
|
77
|
+
for (const line of [lines[0], ...lines.slice(2)]) {
|
|
78
|
+
const cells = splitRow(line).slice(0, width);
|
|
79
|
+
while (cells.length < width) cells.push('');
|
|
80
|
+
/** @type {import('loro-crdt').Delta<string>[]} */
|
|
81
|
+
const delta = [];
|
|
82
|
+
cells.forEach((cell, k) => {
|
|
83
|
+
if (k) delta.push({ insert: '\t' });
|
|
84
|
+
delta.push(...parseInline(cell));
|
|
85
|
+
});
|
|
86
|
+
if (!delta.some((op) => typeof op.insert === 'string' && /\S/.test(op.insert))) continue; // no text in the row
|
|
87
|
+
blocks.push({ type: 'paragraph', delta, partial: false });
|
|
88
|
+
}
|
|
89
|
+
return blocks.length ? blocks : null;
|
|
90
|
+
}
|