@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
|
@@ -1,64 +1,7 @@
|
|
|
1
1
|
import { Extension } from '../utils/extensions.js';
|
|
2
|
+
import { parseMarkdownInline, parseMarkdownSegments, extractLinkDefinitions } from './markdown-inline.js';
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
// Each entry: { re (non-global, used for leftmost-match search), marks }
|
|
5
|
-
const INLINE_PATTERNS = [
|
|
6
|
-
{ re: /\*\*\*(.+?)\*\*\*/s, marks: { bold: true, italic: true } },
|
|
7
|
-
{ re: /\*\*(.+?)\*\*/s, marks: { bold: true } },
|
|
8
|
-
{ re: /\*(.+?)\*/s, marks: { italic: true } },
|
|
9
|
-
{ re: /~~(.+?)~~/s, marks: { strikethrough: true } },
|
|
10
|
-
{ re: /`([^`]+)`/, marks: { code: true } },
|
|
11
|
-
];
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Parse inline markdown text into a Loro-compatible delta array.
|
|
15
|
-
* Uses a leftmost-match recursive strategy so nested marks work naturally:
|
|
16
|
-
* **bold *italic* still bold** → [bold, bold+italic, bold]
|
|
17
|
-
*
|
|
18
|
-
* @param {string} text
|
|
19
|
-
* @param {Record<string, true>} [marks]
|
|
20
|
-
* @returns {import('loro-crdt').Delta<string>[]}
|
|
21
|
-
*/
|
|
22
|
-
export function parseMarkdownInline(text, marks = {}) {
|
|
23
|
-
if (!text) return [];
|
|
24
|
-
|
|
25
|
-
// Find the leftmost (earliest index) pattern match across all patterns
|
|
26
|
-
let earliest = /** @type {RegExpExecArray | null} */ (null);
|
|
27
|
-
let earliestMarks = /** @type {Record<string, true>} */ ({});
|
|
28
|
-
|
|
29
|
-
for (const pat of INLINE_PATTERNS) {
|
|
30
|
-
const m = pat.re.exec(text);
|
|
31
|
-
if (m && (earliest === null || m.index < earliest.index)) {
|
|
32
|
-
earliest = m;
|
|
33
|
-
earliestMarks = pat.marks;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
if (!earliest) {
|
|
38
|
-
// No marks — emit plain text (with inherited marks)
|
|
39
|
-
return [
|
|
40
|
-
Object.keys(marks).length
|
|
41
|
-
? { insert: text, attributes: { ...marks } }
|
|
42
|
-
: { insert: text }
|
|
43
|
-
];
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const ops = /** @type {import('loro-crdt').Delta<string>[]} */ ([]);
|
|
47
|
-
|
|
48
|
-
// Plain text before the match
|
|
49
|
-
if (earliest.index > 0) {
|
|
50
|
-
ops.push(...parseMarkdownInline(text.slice(0, earliest.index), marks));
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// The matched span — recurse with merged marks
|
|
54
|
-
ops.push(...parseMarkdownInline(earliest[1], { ...marks, ...earliestMarks }));
|
|
55
|
-
|
|
56
|
-
// Text after the match
|
|
57
|
-
const after = text.slice(earliest.index + earliest[0].length);
|
|
58
|
-
if (after) ops.push(...parseMarkdownInline(after, marks));
|
|
59
|
-
|
|
60
|
-
return ops;
|
|
61
|
-
}
|
|
4
|
+
export { parseMarkdownInline, parseMarkdownSegments } from './markdown-inline.js';
|
|
62
5
|
|
|
63
6
|
/**
|
|
64
7
|
* Build the ordered markdown ruleset from all BlockClasses registered in nabu.
|
|
@@ -71,7 +14,7 @@ function buildMarkdownRuleset(nabu) {
|
|
|
71
14
|
/** @type {Array<{ BlockClass: any, rule: any }>} */
|
|
72
15
|
const ruleset = [];
|
|
73
16
|
for (const [, BlockClass] of nabu.registry) {
|
|
74
|
-
const rules = BlockClass.markdownRules;
|
|
17
|
+
const rules = /** @type {{ markdownRules?: unknown }} */ (BlockClass).markdownRules;
|
|
75
18
|
if (!Array.isArray(rules)) continue;
|
|
76
19
|
for (const rule of rules) ruleset.push({ BlockClass, rule });
|
|
77
20
|
}
|
|
@@ -79,6 +22,9 @@ function buildMarkdownRuleset(nabu) {
|
|
|
79
22
|
return ruleset;
|
|
80
23
|
}
|
|
81
24
|
|
|
25
|
+
/**
|
|
26
|
+
* Paste of markdown typed as `text/plain`: parses it into blocks and inline marks through the rules the registered block classes declare (`markdownRules`). It also powers `importMarkdown()`.
|
|
27
|
+
*/
|
|
82
28
|
export const MarkdownPasteExtension = new Extension('markdown-paste', {
|
|
83
29
|
pasteInterpreters: [
|
|
84
30
|
{
|
|
@@ -87,49 +33,87 @@ export const MarkdownPasteExtension = new Extension('markdown-paste', {
|
|
|
87
33
|
/**
|
|
88
34
|
* @param {string} raw
|
|
89
35
|
* @param {import('../blocks/nabu.svelte.js').Nabu} nabu
|
|
36
|
+
* @param {import('../utils/extensions.js').PasteInterpretOptions} [options]
|
|
90
37
|
* @returns {import('../utils/extensions.js').PasteFragment | null}
|
|
91
38
|
*/
|
|
92
|
-
interpret(raw, nabu) {
|
|
39
|
+
interpret(raw, nabu, options) {
|
|
93
40
|
try {
|
|
94
|
-
|
|
41
|
+
// BOM stripped, CRLF / CR normalised to LF. Link reference definitions (`[ref]: url`) are collected
|
|
42
|
+
// first and produce no block.
|
|
43
|
+
const { lines, resolveRef } = extractLinkDefinitions(raw.replace(/^\uFEFF/, '').split(/\r\n|\r|\n/));
|
|
95
44
|
const ruleset = buildMarkdownRuleset(nabu);
|
|
96
45
|
if (!ruleset.length) return null;
|
|
97
46
|
|
|
98
|
-
const
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
47
|
+
const inlineOptions = { resolveRef };
|
|
48
|
+
const joinLines = !!options?.joinLines;
|
|
49
|
+
// The rules that start a block in the middle of a paragraph (CommonMark): a fence, a heading, a quote, a list…
|
|
50
|
+
const interrupters = ruleset.filter(({ rule }) => rule.interruptsParagraph);
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The helpers a rule receives. `overrides` change the parsing mode for a nested parse (`parseBlocks`).
|
|
54
|
+
* @param {{ joinLines?: boolean, softBreak?: string }} overrides
|
|
55
|
+
* @returns {any}
|
|
56
|
+
*/
|
|
57
|
+
const makeHelpers = (overrides) => {
|
|
58
|
+
/** @type {any} */
|
|
59
|
+
const helpers = {
|
|
60
|
+
parseInline: (/** @type {string} */ text) => parseMarkdownInline(text, {}, inlineOptions),
|
|
61
|
+
parseInlineSegments: (/** @type {string} */ text) => parseMarkdownSegments(text, inlineOptions),
|
|
62
|
+
joinLines,
|
|
63
|
+
...overrides,
|
|
64
|
+
};
|
|
65
|
+
// Does line `k` start a block that ends the paragraph above it?
|
|
66
|
+
helpers.interrupts = (/** @type {string[]} */ ls, /** @type {number} */ k) => interrupters.some(({ rule }) =>
|
|
67
|
+
rule.interruptsParagraph === true ? detects(rule, ls, k, helpers) : rule.interruptsParagraph(ls[k], ls, k, helpers));
|
|
68
|
+
// Parse lines as a document of their own (the content of a quote…).
|
|
69
|
+
helpers.parseBlocks = (/** @type {string[]} */ ls, /** @type {object} */ over = {}) => parseBlocks(ls, makeHelpers({ ...overrides, ...over }));
|
|
70
|
+
return helpers;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
/** @returns {boolean} */
|
|
74
|
+
const detects = (/** @type {any} */ rule, /** @type {string[]} */ ls, /** @type {number} */ k, /** @type {any} */ helpers) => {
|
|
75
|
+
// Normalize detect: regex or function
|
|
76
|
+
if (rule.detect instanceof RegExp) { rule.detect.lastIndex = 0; return rule.detect.test(ls[k]); }
|
|
77
|
+
return !!rule.detect(ls[k], ls, k, helpers);
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* @param {string[]} ls
|
|
82
|
+
* @param {any} helpers
|
|
83
|
+
* @returns {import('../utils/extensions.js').PasteBlock[]}
|
|
84
|
+
*/
|
|
85
|
+
const parseBlocks = (ls, helpers) => {
|
|
86
|
+
/** @type {import('../utils/extensions.js').PasteBlock[]} */
|
|
87
|
+
const blocks = [];
|
|
88
|
+
let i = 0;
|
|
89
|
+
|
|
90
|
+
while (i < ls.length) {
|
|
91
|
+
// Skip blank lines between blocks
|
|
92
|
+
if (!ls[i].trim()) { i++; continue; }
|
|
93
|
+
|
|
94
|
+
let matched = false;
|
|
95
|
+
for (const { BlockClass, rule } of ruleset) {
|
|
96
|
+
if (!detects(rule, ls, i, helpers)) continue;
|
|
97
|
+
|
|
98
|
+
const count = Math.max(1, rule.consume(ls, i, helpers));
|
|
99
|
+
const consumed = ls.slice(i, i + count);
|
|
100
|
+
const pb = BlockClass.fromMarkdown(consumed, helpers, rule);
|
|
101
|
+
|
|
102
|
+
if (pb) {
|
|
103
|
+
if (Array.isArray(pb)) blocks.push(...pb); // a paragraph split around inline images, a table, several lists
|
|
104
|
+
else blocks.push(pb);
|
|
105
|
+
i += count;
|
|
106
|
+
matched = true;
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
127
109
|
}
|
|
128
|
-
}
|
|
129
110
|
|
|
130
|
-
|
|
131
|
-
|
|
111
|
+
if (!matched) i++; // safety: skip unrecognized line
|
|
112
|
+
}
|
|
113
|
+
return blocks;
|
|
114
|
+
};
|
|
132
115
|
|
|
116
|
+
const blocks = parseBlocks(lines, makeHelpers({}));
|
|
133
117
|
return blocks.length ? { blocks } : null;
|
|
134
118
|
} catch {
|
|
135
119
|
return null;
|
package/dist/paste/nabu.paste.js
CHANGED
|
@@ -1,15 +1,41 @@
|
|
|
1
|
-
import { Extension } from '../utils/extensions.js';
|
|
1
|
+
import { Extension, CLIPBOARD_FORMAT_VERSION } from '../utils/extensions.js';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* The version of a fragment: the `version` field (a number, or its string form "1"); a fragment without one is
|
|
5
|
+
* a version-1 fragment (the format shipped without it). NaN for anything else.
|
|
6
|
+
* @param {any} fragment
|
|
7
|
+
* @returns {number}
|
|
8
|
+
*/
|
|
9
|
+
const versionOf = (fragment) => {
|
|
10
|
+
const v = fragment?.version;
|
|
11
|
+
if (v === undefined || v === null) return 1;
|
|
12
|
+
return typeof v === 'number' || typeof v === 'string' ? Number(v) : NaN;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Paste of `application/x-nabu+json`, the format Nabu writes on copy and cut: copying between Nabu editors keeps the block structure, marks and files.
|
|
17
|
+
*/
|
|
3
18
|
export const NabuPasteExtension = new Extension('nabu-paste', {
|
|
4
19
|
pasteInterpreters: [
|
|
5
20
|
{
|
|
6
21
|
format: 'application/x-nabu+json',
|
|
7
22
|
priority: 100,
|
|
8
|
-
/**
|
|
9
|
-
|
|
23
|
+
/**
|
|
24
|
+
* Accepts format version 1 (written as `version: 1`, or without `version`). A fragment from a NEWER
|
|
25
|
+
* format is not mis-parsed: the interpreter declines (`null` + `nabu.warn`), and the paste falls back
|
|
26
|
+
* to the next interpreter (`text/html`, then `text/plain`) of the same clipboard.
|
|
27
|
+
* @param {string} raw
|
|
28
|
+
* @param {import('../blocks/nabu.svelte.js').Nabu} [nabu]
|
|
29
|
+
*/
|
|
30
|
+
interpret(raw, nabu) {
|
|
10
31
|
try {
|
|
11
32
|
const fragment = JSON.parse(raw);
|
|
12
33
|
if (!fragment?.blocks?.length) return null;
|
|
34
|
+
const version = versionOf(fragment);
|
|
35
|
+
if (version !== CLIPBOARD_FORMAT_VERSION) {
|
|
36
|
+
nabu?.warn(`paste: unsupported application/x-nabu+json version "${fragment.version}" (this editor reads version ${CLIPBOARD_FORMAT_VERSION}): falling back to the next format`);
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
13
39
|
return fragment;
|
|
14
40
|
} catch {
|
|
15
41
|
return null;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Interprète text/plain comme du texte pur.
|
|
3
|
-
* -
|
|
4
|
-
* -
|
|
3
|
+
* - Chaque ligne non vide → un bloc (une ligne = un bloc)
|
|
4
|
+
* - Les lignes vides ne sont que des séparateurs (aucun paragraphe vide)
|
|
5
5
|
* Priorité 0 (fallback de dernier recours).
|
|
6
6
|
*/
|
|
7
7
|
export const PlainTextPasteExtension: Extension;
|
|
@@ -2,8 +2,8 @@ import { Extension } from '../utils/extensions.js';
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Interprète text/plain comme du texte pur.
|
|
5
|
-
* -
|
|
6
|
-
* -
|
|
5
|
+
* - Chaque ligne non vide → un bloc (une ligne = un bloc)
|
|
6
|
+
* - Les lignes vides ne sont que des séparateurs (aucun paragraphe vide)
|
|
7
7
|
* Priorité 0 (fallback de dernier recours).
|
|
8
8
|
*/
|
|
9
9
|
export const PlainTextPasteExtension = new Extension('plain-text-paste', {
|
|
@@ -16,7 +16,7 @@ export const PlainTextPasteExtension = new Extension('plain-text-paste', {
|
|
|
16
16
|
if (!raw) return null;
|
|
17
17
|
|
|
18
18
|
const normalized = raw.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
|
|
19
|
-
const paragraphs = normalized.split(
|
|
19
|
+
const paragraphs = normalized.split('\n').filter(line => line.trim());
|
|
20
20
|
if (paragraphs.length === 0) return null;
|
|
21
21
|
|
|
22
22
|
if (paragraphs.length === 1) {
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {import('../files/store.js').AssetStore} AssetStore
|
|
3
|
+
* @typedef {import('../files/store.js').AssetMeta} AssetMeta
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* The {@link AssetStore} of a persisted editor (ADR 014 §3.3): database `nabu`, object store `assets` (`{ hash, blob, mime,
|
|
7
|
+
* name, size, savedAt }`), SHARED by every document of the origin — the same image used in two documents is stored once.
|
|
8
|
+
* A second object store, `docRefs` (`{ docId, hashes }`), records which files each document references; the persisted
|
|
9
|
+
* editor refreshes it on every save, and the cleanup (compaction, `clearPersistence()`) only deletes a file no document references.
|
|
10
|
+
*
|
|
11
|
+
* Bytes are stored as `Blob`s (the browser keeps them on disk, not in memory).
|
|
12
|
+
* @implements {AssetStore}
|
|
13
|
+
*/
|
|
14
|
+
export class IndexedDBAssetStore implements AssetStore {
|
|
15
|
+
/** @param {string} hash @returns {Promise<boolean>} */
|
|
16
|
+
has(hash: string): Promise<boolean>;
|
|
17
|
+
/** @param {string} hash @returns {Promise<Blob | null>} */
|
|
18
|
+
get(hash: string): Promise<Blob | null>;
|
|
19
|
+
/** @param {string} hash @param {Blob} blob @param {AssetMeta} meta @returns {Promise<void>} */
|
|
20
|
+
put(hash: string, blob: Blob, meta: AssetMeta): Promise<void>;
|
|
21
|
+
/** @param {string} hash @returns {Promise<void>} */
|
|
22
|
+
delete(hash: string): Promise<void>;
|
|
23
|
+
/** @returns {AsyncGenerator<string>} */
|
|
24
|
+
keys(): AsyncGenerator<string>;
|
|
25
|
+
/** @param {string} hash @returns {Promise<AssetMeta | null>} */
|
|
26
|
+
stat(hash: string): Promise<AssetMeta | null>;
|
|
27
|
+
/**
|
|
28
|
+
* When the file was last stored or re-added (`touch`), `null` if unknown (no record, or a record without `savedAt`).
|
|
29
|
+
* @param {string} hash @returns {Promise<number | null>}
|
|
30
|
+
*/
|
|
31
|
+
savedAt(hash: string): Promise<number | null>;
|
|
32
|
+
/**
|
|
33
|
+
* A file that is already stored is added again (deduplication): refresh its `savedAt`, so that a release in another tab keeps
|
|
34
|
+
* it during the grace period even though that tab does not see the new reference yet. One transaction.
|
|
35
|
+
* @param {string} hash @returns {Promise<void>}
|
|
36
|
+
*/
|
|
37
|
+
touch(hash: string): Promise<void>;
|
|
38
|
+
/**
|
|
39
|
+
* Record the files `docId` references (replaces the previous list).
|
|
40
|
+
* @param {string} docId @param {Iterable<string>} hashes @returns {Promise<void>}
|
|
41
|
+
*/
|
|
42
|
+
setDocRefs(docId: string, hashes: Iterable<string>): Promise<void>;
|
|
43
|
+
/** @param {string} docId @returns {Promise<Set<string>>} */
|
|
44
|
+
getDocRefs(docId: string): Promise<Set<string>>;
|
|
45
|
+
/** @param {string} docId @returns {Promise<void>} */
|
|
46
|
+
clearDocRefs(docId: string): Promise<void>;
|
|
47
|
+
/** Every file referenced by at least one document. @returns {Promise<Set<string>>} */
|
|
48
|
+
referencedHashes(): Promise<Set<string>>;
|
|
49
|
+
}
|
|
50
|
+
export type AssetStore = import("../files/store.js").AssetStore;
|
|
51
|
+
export type AssetMeta = import("../files/store.js").AssetMeta;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { ASSETS_STORE, DOC_REFS_STORE, withStore, openDB } from './indexeddb.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @typedef {import('../files/store.js').AssetStore} AssetStore
|
|
5
|
+
* @typedef {import('../files/store.js').AssetMeta} AssetMeta
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The {@link AssetStore} of a persisted editor (ADR 014 §3.3): database `nabu`, object store `assets` (`{ hash, blob, mime,
|
|
10
|
+
* name, size, savedAt }`), SHARED by every document of the origin — the same image used in two documents is stored once.
|
|
11
|
+
* A second object store, `docRefs` (`{ docId, hashes }`), records which files each document references; the persisted
|
|
12
|
+
* editor refreshes it on every save, and the cleanup (compaction, `clearPersistence()`) only deletes a file no document references.
|
|
13
|
+
*
|
|
14
|
+
* Bytes are stored as `Blob`s (the browser keeps them on disk, not in memory).
|
|
15
|
+
* @implements {AssetStore}
|
|
16
|
+
*/
|
|
17
|
+
export class IndexedDBAssetStore {
|
|
18
|
+
/** @param {string} hash @returns {Promise<boolean>} */
|
|
19
|
+
async has(hash) {
|
|
20
|
+
return (await withStore(ASSETS_STORE, 'readonly', (s) => s.count(hash))) > 0;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** @param {string} hash @returns {Promise<Blob | null>} */
|
|
24
|
+
async get(hash) {
|
|
25
|
+
const record = await withStore(ASSETS_STORE, 'readonly', (s) => s.get(hash));
|
|
26
|
+
return record?.blob ?? null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** @param {string} hash @param {Blob} blob @param {AssetMeta} meta @returns {Promise<void>} */
|
|
30
|
+
async put(hash, blob, meta) {
|
|
31
|
+
await withStore(ASSETS_STORE, 'readwrite', (s) => s.put({ hash, blob, mime: meta.mime, name: meta.name, size: meta.size, savedAt: Date.now() }));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** @param {string} hash @returns {Promise<void>} */
|
|
35
|
+
async delete(hash) {
|
|
36
|
+
await withStore(ASSETS_STORE, 'readwrite', (s) => s.delete(hash));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** @returns {AsyncGenerator<string>} */
|
|
40
|
+
async *keys() {
|
|
41
|
+
const all = /** @type {IDBValidKey[]} */ (await withStore(ASSETS_STORE, 'readonly', (s) => s.getAllKeys()));
|
|
42
|
+
for (const key of all) yield String(key);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** @param {string} hash @returns {Promise<AssetMeta | null>} */
|
|
46
|
+
async stat(hash) {
|
|
47
|
+
const record = await withStore(ASSETS_STORE, 'readonly', (s) => s.get(hash));
|
|
48
|
+
return record ? { mime: record.mime, name: record.name, size: record.size } : null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* When the file was last stored or re-added (`touch`), `null` if unknown (no record, or a record without `savedAt`).
|
|
53
|
+
* @param {string} hash @returns {Promise<number | null>}
|
|
54
|
+
*/
|
|
55
|
+
async savedAt(hash) {
|
|
56
|
+
const record = await withStore(ASSETS_STORE, 'readonly', (s) => s.get(hash));
|
|
57
|
+
return typeof record?.savedAt === 'number' ? record.savedAt : null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* A file that is already stored is added again (deduplication): refresh its `savedAt`, so that a release in another tab keeps
|
|
62
|
+
* it during the grace period even though that tab does not see the new reference yet. One transaction.
|
|
63
|
+
* @param {string} hash @returns {Promise<void>}
|
|
64
|
+
*/
|
|
65
|
+
async touch(hash) {
|
|
66
|
+
const db = await openDB();
|
|
67
|
+
await new Promise((resolve, reject) => {
|
|
68
|
+
const tx = db.transaction(ASSETS_STORE, 'readwrite');
|
|
69
|
+
const store = tx.objectStore(ASSETS_STORE);
|
|
70
|
+
const get = store.get(hash);
|
|
71
|
+
get.onsuccess = () => { if (get.result) store.put({ ...get.result, savedAt: Date.now() }); };
|
|
72
|
+
tx.oncomplete = () => { db.close(); resolve(undefined); };
|
|
73
|
+
tx.onabort = tx.onerror = () => { db.close(); reject(tx.error); };
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// ── docRefs: which files each document references ─────────────────────────────────────────
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Record the files `docId` references (replaces the previous list).
|
|
81
|
+
* @param {string} docId @param {Iterable<string>} hashes @returns {Promise<void>}
|
|
82
|
+
*/
|
|
83
|
+
async setDocRefs(docId, hashes) {
|
|
84
|
+
await withStore(DOC_REFS_STORE, 'readwrite', (s) => s.put({ docId, hashes: [...hashes], savedAt: Date.now() }));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** @param {string} docId @returns {Promise<Set<string>>} */
|
|
88
|
+
async getDocRefs(docId) {
|
|
89
|
+
const record = await withStore(DOC_REFS_STORE, 'readonly', (s) => s.get(docId));
|
|
90
|
+
return new Set(record?.hashes ?? []);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** @param {string} docId @returns {Promise<void>} */
|
|
94
|
+
async clearDocRefs(docId) {
|
|
95
|
+
await withStore(DOC_REFS_STORE, 'readwrite', (s) => s.delete(docId));
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Every file referenced by at least one document. @returns {Promise<Set<string>>} */
|
|
99
|
+
async referencedHashes() {
|
|
100
|
+
const records = /** @type {Array<{ hashes: string[] }>} */ (await withStore(DOC_REFS_STORE, 'readonly', (s) => s.getAll()));
|
|
101
|
+
return new Set(records.flatMap((r) => r.hashes));
|
|
102
|
+
}
|
|
103
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Normalise the `maxWait` option: a positive number of ms, never shorter than `debounce` (like lodash: the maximum wait cannot be
|
|
3
|
+
* shorter than the wait). `0` / `Infinity` / a non-number other than `undefined` disable it (the old, trailing-only behaviour);
|
|
4
|
+
* `undefined` gives {@link DEFAULT_MAX_WAIT}.
|
|
5
|
+
* @param {unknown} maxWait
|
|
6
|
+
* @param {number} debounce
|
|
7
|
+
* @returns {number} ms, or `Infinity` (no maximum)
|
|
8
|
+
*/
|
|
9
|
+
export function resolveMaxWait(maxWait: unknown, debounce: number): number;
|
|
10
|
+
/**
|
|
11
|
+
* @typedef {object} Autosave
|
|
12
|
+
* @property {() => void} touch a change happened: (re)arm the timer
|
|
13
|
+
* @property {() => void} cancel drop the pending save (nothing is saved by this scheduler)
|
|
14
|
+
* @property {() => boolean} pending whether a save is scheduled and has not started yet
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Debounce with a maximum wait: `touch()` re-arms a timer of `debounce` ms, but never later than `maxWait` ms after the FIRST
|
|
18
|
+
* `touch()` of the current burst, so a long uninterrupted burst of changes is still saved every `maxWait` ms. Once `run` is
|
|
19
|
+
* called, the next `touch()` starts a new burst. Pure (injectable clock and timers) so it can be unit tested.
|
|
20
|
+
*
|
|
21
|
+
* @param {object} options
|
|
22
|
+
* @param {number} options.debounce ms of quiet before a save
|
|
23
|
+
* @param {number} [options.maxWait] ms; `Infinity` / omitted: no maximum
|
|
24
|
+
* @param {() => void} options.run the save (must not throw: report its own errors)
|
|
25
|
+
* @param {() => number} [options.now]
|
|
26
|
+
* @param {(fn: () => void, ms: number) => any} [options.setTimer]
|
|
27
|
+
* @param {(handle: any) => void} [options.clearTimer]
|
|
28
|
+
* @returns {Autosave}
|
|
29
|
+
*/
|
|
30
|
+
export function createAutosave({ debounce, maxWait, run, now, setTimer, clearTimer }: {
|
|
31
|
+
debounce: number;
|
|
32
|
+
maxWait?: number | undefined;
|
|
33
|
+
run: () => void;
|
|
34
|
+
now?: (() => number) | undefined;
|
|
35
|
+
setTimer?: ((fn: () => void, ms: number) => any) | undefined;
|
|
36
|
+
clearTimer?: ((handle: any) => void) | undefined;
|
|
37
|
+
}): Autosave;
|
|
38
|
+
/** Default `maxWait` of `createPersistedEditor` (ms): during a continuous burst of edits, a save happens at least this often. */
|
|
39
|
+
export const DEFAULT_MAX_WAIT: 3000;
|
|
40
|
+
export type Autosave = {
|
|
41
|
+
/**
|
|
42
|
+
* a change happened: (re)arm the timer
|
|
43
|
+
*/
|
|
44
|
+
touch: () => void;
|
|
45
|
+
/**
|
|
46
|
+
* drop the pending save (nothing is saved by this scheduler)
|
|
47
|
+
*/
|
|
48
|
+
cancel: () => void;
|
|
49
|
+
/**
|
|
50
|
+
* whether a save is scheduled and has not started yet
|
|
51
|
+
*/
|
|
52
|
+
pending: () => boolean;
|
|
53
|
+
};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/** Default `maxWait` of `createPersistedEditor` (ms): during a continuous burst of edits, a save happens at least this often. */
|
|
2
|
+
export const DEFAULT_MAX_WAIT = 3000;
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Normalise the `maxWait` option: a positive number of ms, never shorter than `debounce` (like lodash: the maximum wait cannot be
|
|
6
|
+
* shorter than the wait). `0` / `Infinity` / a non-number other than `undefined` disable it (the old, trailing-only behaviour);
|
|
7
|
+
* `undefined` gives {@link DEFAULT_MAX_WAIT}.
|
|
8
|
+
* @param {unknown} maxWait
|
|
9
|
+
* @param {number} debounce
|
|
10
|
+
* @returns {number} ms, or `Infinity` (no maximum)
|
|
11
|
+
*/
|
|
12
|
+
export function resolveMaxWait(maxWait, debounce) {
|
|
13
|
+
if (maxWait === undefined) return Math.max(DEFAULT_MAX_WAIT, debounce);
|
|
14
|
+
if (typeof maxWait !== 'number' || !(maxWait > 0) || !Number.isFinite(maxWait)) return Infinity;
|
|
15
|
+
return Math.max(maxWait, debounce);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @typedef {object} Autosave
|
|
20
|
+
* @property {() => void} touch a change happened: (re)arm the timer
|
|
21
|
+
* @property {() => void} cancel drop the pending save (nothing is saved by this scheduler)
|
|
22
|
+
* @property {() => boolean} pending whether a save is scheduled and has not started yet
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Debounce with a maximum wait: `touch()` re-arms a timer of `debounce` ms, but never later than `maxWait` ms after the FIRST
|
|
27
|
+
* `touch()` of the current burst, so a long uninterrupted burst of changes is still saved every `maxWait` ms. Once `run` is
|
|
28
|
+
* called, the next `touch()` starts a new burst. Pure (injectable clock and timers) so it can be unit tested.
|
|
29
|
+
*
|
|
30
|
+
* @param {object} options
|
|
31
|
+
* @param {number} options.debounce ms of quiet before a save
|
|
32
|
+
* @param {number} [options.maxWait] ms; `Infinity` / omitted: no maximum
|
|
33
|
+
* @param {() => void} options.run the save (must not throw: report its own errors)
|
|
34
|
+
* @param {() => number} [options.now]
|
|
35
|
+
* @param {(fn: () => void, ms: number) => any} [options.setTimer]
|
|
36
|
+
* @param {(handle: any) => void} [options.clearTimer]
|
|
37
|
+
* @returns {Autosave}
|
|
38
|
+
*/
|
|
39
|
+
export function createAutosave({ debounce, maxWait = Infinity, run, now = Date.now, setTimer = setTimeout, clearTimer = clearTimeout }) {
|
|
40
|
+
/** @type {any} */
|
|
41
|
+
let timer = null;
|
|
42
|
+
/** When the current burst started (first `touch` since the last save). @type {number | null} */
|
|
43
|
+
let burstStart = null;
|
|
44
|
+
|
|
45
|
+
const fire = () => { timer = null; burstStart = null; run(); };
|
|
46
|
+
|
|
47
|
+
return {
|
|
48
|
+
touch() {
|
|
49
|
+
const t = now();
|
|
50
|
+
if (burstStart === null) burstStart = t;
|
|
51
|
+
if (timer !== null) clearTimer(timer);
|
|
52
|
+
const remaining = burstStart + maxWait - t;
|
|
53
|
+
timer = setTimer(fire, Math.max(0, Math.min(debounce, remaining)));
|
|
54
|
+
},
|
|
55
|
+
cancel() {
|
|
56
|
+
if (timer !== null) clearTimer(timer);
|
|
57
|
+
timer = null;
|
|
58
|
+
burstStart = null;
|
|
59
|
+
},
|
|
60
|
+
pending: () => timer !== null,
|
|
61
|
+
};
|
|
62
|
+
}
|