@aionbuilders/nabu 0.1.0-alpha.3 → 0.1.0-alpha.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/README.md +15 -1
  2. package/dist/behaviors/text/rich-text.extension.js +87 -30
  3. package/dist/behaviors/text/text.behavior.svelte.js +14 -1
  4. package/dist/blocks/block.extension.d.ts +2 -0
  5. package/dist/blocks/block.extension.js +31 -0
  6. package/dist/blocks/block.svelte.d.ts +10 -2
  7. package/dist/blocks/block.svelte.js +12 -6
  8. package/dist/blocks/container.utils.d.ts +0 -4
  9. package/dist/blocks/container.utils.js +48 -1
  10. package/dist/blocks/dialogue/Dialogue.svelte +46 -0
  11. package/dist/blocks/dialogue/Dialogue.svelte.d.ts +11 -0
  12. package/dist/blocks/dialogue/dialogue.svelte.d.ts +41 -0
  13. package/dist/blocks/dialogue/dialogue.svelte.js +85 -0
  14. package/dist/blocks/dialogue/hooks/onBeforeInput.hook.d.ts +3 -0
  15. package/dist/blocks/dialogue/hooks/onBeforeInput.hook.js +52 -0
  16. package/dist/blocks/dialogue/index.d.ts +7 -0
  17. package/dist/blocks/dialogue/index.js +51 -0
  18. package/dist/blocks/heading/index.js +8 -0
  19. package/dist/blocks/index.d.ts +2 -0
  20. package/dist/blocks/index.js +2 -0
  21. package/dist/blocks/list/hooks/onBeforeInput.hook.d.ts +3 -0
  22. package/dist/blocks/list/hooks/onBeforeInput.hook.js +77 -0
  23. package/dist/blocks/list/index.js +15 -1
  24. package/dist/blocks/list/list-item.svelte.d.ts +24 -0
  25. package/dist/blocks/list/list-item.svelte.js +88 -83
  26. package/dist/blocks/list/list.svelte.js +1 -1
  27. package/dist/blocks/megablock.svelte.d.ts +1 -3
  28. package/dist/blocks/megablock.svelte.js +8 -14
  29. package/dist/blocks/nabu.svelte.d.ts +8 -0
  30. package/dist/blocks/nabu.svelte.js +25 -1
  31. package/dist/blocks/paragraph/index.js +3 -0
  32. package/dist/blocks/selection.svelte.js +6 -1
  33. package/dist/persistence/indexeddb.d.ts +19 -0
  34. package/dist/persistence/indexeddb.js +71 -0
  35. package/dist/persistence/persistence.d.ts +48 -0
  36. package/dist/persistence/persistence.js +80 -0
  37. package/dist/presets.d.ts +1 -0
  38. package/dist/presets.js +8 -1
  39. package/dist/utils/extensions.d.ts +4 -0
  40. package/dist/utils/extensions.js +3 -0
  41. package/package.json +2 -1
@@ -0,0 +1,71 @@
1
+ const DB_NAME = 'nabu';
2
+ const STORE = 'snapshots';
3
+ const DB_VERSION = 1;
4
+
5
+ /**
6
+ * @returns {Promise<IDBDatabase>}
7
+ */
8
+ function openDB() {
9
+ return new Promise((resolve, reject) => {
10
+ const request = indexedDB.open(DB_NAME, DB_VERSION);
11
+
12
+ request.onupgradeneeded = (event) => {
13
+ const db = /** @type {IDBOpenDBRequest} */ (event.target).result;
14
+ if (!db.objectStoreNames.contains(STORE)) {
15
+ db.createObjectStore(STORE, { keyPath: 'docId' });
16
+ }
17
+ };
18
+
19
+ request.onsuccess = (event) => resolve(/** @type {IDBOpenDBRequest} */ (event.target).result);
20
+ request.onerror = (event) => reject(/** @type {IDBOpenDBRequest} */ (event.target).error);
21
+ });
22
+ }
23
+
24
+ /**
25
+ * Load a snapshot from IndexedDB.
26
+ * @param {string} docId
27
+ * @returns {Promise<Uint8Array | null>}
28
+ */
29
+ export async function loadSnapshot(docId) {
30
+ const db = await openDB();
31
+ return new Promise((resolve, reject) => {
32
+ const tx = db.transaction(STORE, 'readonly');
33
+ const request = tx.objectStore(STORE).get(docId);
34
+ request.onsuccess = (event) => {
35
+ const record = /** @type {IDBRequest} */ (event.target).result;
36
+ resolve(record ? record.snapshot : null);
37
+ };
38
+ request.onerror = (event) => reject(/** @type {IDBRequest} */ (event.target).error);
39
+ });
40
+ }
41
+
42
+ /**
43
+ * Save a snapshot to IndexedDB.
44
+ * @param {string} docId
45
+ * @param {Uint8Array} snapshot
46
+ * @returns {Promise<void>}
47
+ */
48
+ export async function saveSnapshot(docId, snapshot) {
49
+ const db = await openDB();
50
+ return new Promise((resolve, reject) => {
51
+ const tx = db.transaction(STORE, 'readwrite');
52
+ const request = tx.objectStore(STORE).put({ docId, snapshot, savedAt: Date.now() });
53
+ request.onsuccess = () => resolve();
54
+ request.onerror = (event) => reject(/** @type {IDBRequest} */ (event.target).error);
55
+ });
56
+ }
57
+
58
+ /**
59
+ * Delete a snapshot from IndexedDB.
60
+ * @param {string} docId
61
+ * @returns {Promise<void>}
62
+ */
63
+ export async function deleteSnapshot(docId) {
64
+ const db = await openDB();
65
+ return new Promise((resolve, reject) => {
66
+ const tx = db.transaction(STORE, 'readwrite');
67
+ const request = tx.objectStore(STORE).delete(docId);
68
+ request.onsuccess = () => resolve();
69
+ request.onerror = (event) => reject(/** @type {IDBRequest} */ (event.target).error);
70
+ });
71
+ }
@@ -0,0 +1,48 @@
1
+ /**
2
+ * @import { Extension } from '../utils/extensions.js';
3
+ */
4
+ /**
5
+ * Create a Nabu editor instance with automatic IndexedDB persistence.
6
+ *
7
+ * Async counterpart of `createEditor()` — same `{ preset, extensions }` signature,
8
+ * extended with a required `docId` for storage.
9
+ *
10
+ * On first call, the document is empty (only the default paragraph from ParagraphExtension).
11
+ * On subsequent calls, the document state is restored from the last saved snapshot.
12
+ *
13
+ * The instance gains three extra methods:
14
+ * - `nabu.saveNow()` — force an immediate save (returns a Promise)
15
+ * - `nabu.clearPersistence()` — delete the stored snapshot (returns a Promise)
16
+ * - `nabu.isNew` — `true` if no previous snapshot was found (fresh document)
17
+ *
18
+ * @param {object} options
19
+ * @param {string} options.docId - Unique identifier for this document in IndexedDB
20
+ * @param {Extension[]} [options.preset] - Extension preset (default: FullPreset)
21
+ * @param {Extension[]} [options.extensions] - Additional extensions to append
22
+ * @param {number} [options.debounce] - Auto-save debounce delay in ms (default: 2000)
23
+ * @returns {Promise<Nabu>}
24
+ *
25
+ * @example
26
+ * // Fresh or restored editor, full preset
27
+ * const engine = await createPersistedEditor({ docId: 'my-note' });
28
+ *
29
+ * @example
30
+ * // Document preset with custom extension
31
+ * const engine = await createPersistedEditor({
32
+ * docId: 'article-42',
33
+ * preset: DocumentPreset,
34
+ * extensions: [MyCustomExtension],
35
+ * });
36
+ */
37
+ export function createPersistedEditor({ docId, preset, extensions, debounce }?: {
38
+ docId: string;
39
+ preset?: Extension[] | undefined;
40
+ extensions?: Extension[] | undefined;
41
+ debounce?: number | undefined;
42
+ }): Promise<Nabu>;
43
+ import type { Extension } from '../utils/extensions.js';
44
+ import { Nabu } from '../blocks/nabu.svelte.js';
45
+ import { loadSnapshot } from './indexeddb.js';
46
+ import { saveSnapshot } from './indexeddb.js';
47
+ import { deleteSnapshot } from './indexeddb.js';
48
+ export { loadSnapshot, saveSnapshot, deleteSnapshot };
@@ -0,0 +1,80 @@
1
+ import { Nabu } from '../blocks/nabu.svelte.js';
2
+ import { FullPreset } from '../presets.js';
3
+ import { loadSnapshot, saveSnapshot, deleteSnapshot } from './indexeddb.js';
4
+
5
+ export { loadSnapshot, saveSnapshot, deleteSnapshot };
6
+
7
+ /**
8
+ * @import { Extension } from '../utils/extensions.js';
9
+ */
10
+
11
+ /**
12
+ * Create a Nabu editor instance with automatic IndexedDB persistence.
13
+ *
14
+ * Async counterpart of `createEditor()` — same `{ preset, extensions }` signature,
15
+ * extended with a required `docId` for storage.
16
+ *
17
+ * On first call, the document is empty (only the default paragraph from ParagraphExtension).
18
+ * On subsequent calls, the document state is restored from the last saved snapshot.
19
+ *
20
+ * The instance gains three extra methods:
21
+ * - `nabu.saveNow()` — force an immediate save (returns a Promise)
22
+ * - `nabu.clearPersistence()` — delete the stored snapshot (returns a Promise)
23
+ * - `nabu.isNew` — `true` if no previous snapshot was found (fresh document)
24
+ *
25
+ * @param {object} options
26
+ * @param {string} options.docId - Unique identifier for this document in IndexedDB
27
+ * @param {Extension[]} [options.preset] - Extension preset (default: FullPreset)
28
+ * @param {Extension[]} [options.extensions] - Additional extensions to append
29
+ * @param {number} [options.debounce] - Auto-save debounce delay in ms (default: 2000)
30
+ * @returns {Promise<Nabu>}
31
+ *
32
+ * @example
33
+ * // Fresh or restored editor, full preset
34
+ * const engine = await createPersistedEditor({ docId: 'my-note' });
35
+ *
36
+ * @example
37
+ * // Document preset with custom extension
38
+ * const engine = await createPersistedEditor({
39
+ * docId: 'article-42',
40
+ * preset: DocumentPreset,
41
+ * extensions: [MyCustomExtension],
42
+ * });
43
+ */
44
+ export async function createPersistedEditor({
45
+ docId,
46
+ preset = FullPreset,
47
+ extensions = [],
48
+ debounce = 2000
49
+ } = {}) {
50
+ const snapshot = await loadSnapshot(docId);
51
+
52
+ const nabu = new Nabu({
53
+ extensions: [...preset, ...extensions],
54
+ ...(snapshot ? { snapshot } : {})
55
+ });
56
+
57
+ // Auto-save: Loro fires doc.subscribe() after every committed change
58
+ let saveTimer;
59
+ nabu.doc.subscribe(() => {
60
+ clearTimeout(saveTimer);
61
+ saveTimer = setTimeout(async () => {
62
+ const snap = nabu.doc.export({ mode: 'snapshot' });
63
+ await saveSnapshot(docId, snap);
64
+ }, debounce);
65
+ });
66
+
67
+ /** Whether this instance was created from a fresh document (no prior snapshot). */
68
+ nabu.isNew = !snapshot;
69
+
70
+ /** Force an immediate save of the current document state. @returns {Promise<void>} */
71
+ nabu.saveNow = async () => {
72
+ const snap = nabu.doc.export({ mode: 'snapshot' });
73
+ await saveSnapshot(docId, snap);
74
+ };
75
+
76
+ /** Delete the persisted snapshot for this document. @returns {Promise<void>} */
77
+ nabu.clearPersistence = () => deleteSnapshot(docId);
78
+
79
+ return nabu;
80
+ }
package/dist/presets.d.ts CHANGED
@@ -33,5 +33,6 @@ export function createDocumentEditor(options?: {
33
33
  export function createFullEditor(options?: {
34
34
  extensions?: (typeof ParagraphExtension)[];
35
35
  }): Nabu;
36
+ export { createPersistedEditor } from "./persistence/persistence.js";
36
37
  import { Nabu } from './blocks/nabu.svelte.js';
37
38
  import { ParagraphExtension } from './blocks/paragraph/index.js';
package/dist/presets.js CHANGED
@@ -2,6 +2,7 @@ import { Nabu } from './blocks/nabu.svelte.js';
2
2
  import { ParagraphExtension } from './blocks/paragraph/index.js';
3
3
  import { HeadingExtension } from './blocks/heading/index.js';
4
4
  import { ListExtension, ListItemExtension } from './blocks/list/index.js';
5
+ import { DialogueExtension } from './blocks/dialogue/index.js';
5
6
  import { RichTextExtension } from './behaviors/text/index.js';
6
7
 
7
8
  // ---------------------------------------------------------------------------
@@ -18,7 +19,7 @@ export const TextPreset = [ParagraphExtension, RichTextExtension];
18
19
  export const DocumentPreset = [ParagraphExtension, HeadingExtension, RichTextExtension];
19
20
 
20
21
  /** All built-in block types + rich text. */
21
- export const FullPreset = [ParagraphExtension, HeadingExtension, ListExtension, ListItemExtension, RichTextExtension];
22
+ export const FullPreset = [ParagraphExtension, HeadingExtension, ListExtension, ListItemExtension, DialogueExtension, RichTextExtension];
22
23
 
23
24
  // ---------------------------------------------------------------------------
24
25
  // Factory — generic createEditor
@@ -70,3 +71,9 @@ export const createDocumentEditor = (options = {}) =>
70
71
  */
71
72
  export const createFullEditor = (options = {}) =>
72
73
  createEditor({ preset: FullPreset, ...options });
74
+
75
+ // ---------------------------------------------------------------------------
76
+ // Persisted factory
77
+ // ---------------------------------------------------------------------------
78
+
79
+ export { createPersistedEditor } from './persistence/persistence.js';
@@ -21,6 +21,7 @@
21
21
  * @property {typeof Block} [block]
22
22
  * @property {Partial<ExtensionHooks>} [hooks]
23
23
  * @property {Record<string, (nabu: Nabu) => any>} [serializers]
24
+ * @property {Record<string, (nabu: Nabu, data: any, topic: string) => any>} [actions]
24
25
  */
25
26
  export class Extension {
26
27
  /** @param {string} name @param {ExtensionInit} init */
@@ -32,6 +33,8 @@ export class Extension {
32
33
  hooks: Partial<ExtensionHooks>;
33
34
  /** @type {Record<string, (nabu: Nabu) => any>} */
34
35
  serializers: Record<string, (nabu: Nabu) => any>;
36
+ /** @type {Record<string, (nabu: Nabu, data: any, topic: string) => any>} */
37
+ actions: Record<string, (nabu: Nabu, data: any, topic: string) => any>;
35
38
  }
36
39
  export function extension(name: string, init: ExtensionInit): Extension;
37
40
  export type ExtensionHooks = {
@@ -64,6 +67,7 @@ export type ExtensionInit = {
64
67
  block?: typeof Block | undefined;
65
68
  hooks?: Partial<ExtensionHooks> | undefined;
66
69
  serializers?: Record<string, (nabu: Nabu) => any> | undefined;
70
+ actions?: Record<string, (nabu: Nabu, data: any, topic: string) => any> | undefined;
67
71
  };
68
72
  import type { Block } from '../blocks';
69
73
  import type { Nabu } from '../blocks';
@@ -23,6 +23,7 @@
23
23
  * @property {typeof Block} [block]
24
24
  * @property {Partial<ExtensionHooks>} [hooks]
25
25
  * @property {Record<string, (nabu: Nabu) => any>} [serializers]
26
+ * @property {Record<string, (nabu: Nabu, data: any, topic: string) => any>} [actions]
26
27
  */
27
28
 
28
29
  export class Extension {
@@ -35,6 +36,8 @@ export class Extension {
35
36
  this.hooks = init.hooks || {};
36
37
  /** @type {Record<string, (nabu: Nabu) => any>} */
37
38
  this.serializers = init.serializers || {};
39
+ /** @type {Record<string, (nabu: Nabu, data: any, topic: string) => any>} */
40
+ this.actions = init.actions || {};
38
41
  }
39
42
  }
40
43
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aionbuilders/nabu",
3
- "version": "0.1.0-alpha.3",
3
+ "version": "0.1.0-alpha.5",
4
4
  "scripts": {
5
5
  "dev": "vite dev",
6
6
  "build": "vite build && npm run prepack",
@@ -69,6 +69,7 @@
69
69
  "collaborative"
70
70
  ],
71
71
  "dependencies": {
72
+ "@aionbuilders/pulse": "^1.0.1",
72
73
  "loro-crdt": "^1.10.6"
73
74
  }
74
75
  }