@aionbuilders/nabu 0.1.0-alpha.4 → 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.
- package/dist/behaviors/text/rich-text.extension.js +87 -30
- package/dist/blocks/block.extension.d.ts +2 -0
- package/dist/blocks/block.extension.js +31 -0
- package/dist/blocks/heading/index.js +8 -0
- package/dist/blocks/index.d.ts +1 -0
- package/dist/blocks/index.js +1 -0
- package/dist/blocks/list/index.js +13 -1
- package/dist/blocks/list/list-item.svelte.d.ts +12 -0
- package/dist/blocks/list/list-item.svelte.js +71 -83
- package/dist/blocks/nabu.svelte.d.ts +8 -0
- package/dist/blocks/nabu.svelte.js +25 -1
- package/dist/blocks/paragraph/index.js +3 -0
- package/dist/utils/extensions.d.ts +4 -0
- package/dist/utils/extensions.js +3 -0
- package/package.json +2 -1
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
* @import { Nabu } from '../../blocks/nabu.svelte.js'
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import { tick } from 'svelte';
|
|
6
5
|
import { Extension } from '../../utils/extensions.js';
|
|
7
6
|
import { TextBehavior } from './text.behavior.svelte.js';
|
|
8
7
|
|
|
@@ -15,28 +14,11 @@ const MARK_SHORTCUTS = {
|
|
|
15
14
|
};
|
|
16
15
|
|
|
17
16
|
/**
|
|
17
|
+
* Collect all selected blocks with a TextBehavior and a non-collapsed selection.
|
|
18
18
|
* @param {Nabu} nabu
|
|
19
|
-
* @
|
|
19
|
+
* @returns {{ behavior: TextBehavior, sel: any }[]}
|
|
20
20
|
*/
|
|
21
|
-
function
|
|
22
|
-
const ctrl = event.ctrlKey || event.metaKey;
|
|
23
|
-
if (!ctrl) return;
|
|
24
|
-
|
|
25
|
-
const endBlock = nabu.selection.endBlock;
|
|
26
|
-
const startBlock = nabu.selection.startBlock;
|
|
27
|
-
const startFocusData = startBlock?.focus(null, true);
|
|
28
|
-
const endFocusData = endBlock?.focus(null, true);
|
|
29
|
-
|
|
30
|
-
let markName = null;
|
|
31
|
-
if (!event.shiftKey) {
|
|
32
|
-
markName = MARK_SHORTCUTS[event.key.toLowerCase()] ?? null;
|
|
33
|
-
} else if (event.key.toLowerCase() === 'x') {
|
|
34
|
-
markName = 'strikethrough';
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
if (!markName) return;
|
|
38
|
-
|
|
39
|
-
// Collect all targeted block/selection pairs
|
|
21
|
+
function collectMarkTargets(nabu) {
|
|
40
22
|
const targets = [];
|
|
41
23
|
for (const [, block] of nabu.blocks) {
|
|
42
24
|
if (!block.selected) continue;
|
|
@@ -46,10 +28,23 @@ function onKeyDown(nabu, event) {
|
|
|
46
28
|
if (!sel || sel.isCollapsed) continue;
|
|
47
29
|
targets.push({ behavior, sel });
|
|
48
30
|
}
|
|
31
|
+
return targets;
|
|
32
|
+
}
|
|
49
33
|
|
|
50
|
-
|
|
34
|
+
/**
|
|
35
|
+
* Toggle a mark on all selected text targets.
|
|
36
|
+
* @param {Nabu} nabu
|
|
37
|
+
* @param {string} markName
|
|
38
|
+
*/
|
|
39
|
+
function toggleMark(nabu, markName) {
|
|
40
|
+
const startBlock = nabu.selection.startBlock;
|
|
41
|
+
const endBlock = nabu.selection.endBlock;
|
|
42
|
+
const startFocusData = startBlock?.focus(null, true);
|
|
43
|
+
const endFocusData = endBlock?.focus(null, true);
|
|
44
|
+
|
|
45
|
+
const targets = collectMarkTargets(nabu);
|
|
46
|
+
if (!targets.length) return false;
|
|
51
47
|
|
|
52
|
-
// Classic editor behavior: remove if mark is fully active across all targets, apply otherwise
|
|
53
48
|
const isFullyActive = targets.every(({ behavior, sel }) => behavior.isMarkActive(markName, sel));
|
|
54
49
|
for (const { behavior, sel } of targets) {
|
|
55
50
|
if (isFullyActive) {
|
|
@@ -58,18 +53,80 @@ function onKeyDown(nabu, event) {
|
|
|
58
53
|
behavior.applyMark(markName, true, sel);
|
|
59
54
|
}
|
|
60
55
|
}
|
|
61
|
-
const applied = true;
|
|
62
56
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
57
|
+
nabu.commit();
|
|
58
|
+
if (startBlock && endBlock) {
|
|
59
|
+
nabu.focus({
|
|
60
|
+
from: { block: startBlock, offset: startFocusData?.start?.offset ?? 0 },
|
|
61
|
+
to: { block: endBlock, offset: endFocusData?.end?.offset ?? 0 }
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
68
66
|
|
|
69
|
-
|
|
67
|
+
/**
|
|
68
|
+
* Apply a mark on all selected text targets.
|
|
69
|
+
* @param {Nabu} nabu
|
|
70
|
+
* @param {string} markName
|
|
71
|
+
* @param {any} value
|
|
72
|
+
*/
|
|
73
|
+
function applyMark(nabu, markName, value = true) {
|
|
74
|
+
const targets = collectMarkTargets(nabu);
|
|
75
|
+
if (!targets.length) return;
|
|
76
|
+
for (const { behavior, sel } of targets) {
|
|
77
|
+
behavior.applyMark(markName, value, sel);
|
|
78
|
+
}
|
|
79
|
+
nabu.commit();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Remove a mark from all selected text targets.
|
|
84
|
+
* @param {Nabu} nabu
|
|
85
|
+
* @param {string} markName
|
|
86
|
+
*/
|
|
87
|
+
function removeMark(nabu, markName) {
|
|
88
|
+
const targets = collectMarkTargets(nabu);
|
|
89
|
+
if (!targets.length) return;
|
|
90
|
+
for (const { behavior, sel } of targets) {
|
|
91
|
+
behavior.removeMark(markName, sel);
|
|
70
92
|
}
|
|
93
|
+
nabu.commit();
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* @param {Nabu} nabu
|
|
98
|
+
* @param {KeyboardEvent} event
|
|
99
|
+
*/
|
|
100
|
+
function onKeyDown(nabu, event) {
|
|
101
|
+
const ctrl = event.ctrlKey || event.metaKey;
|
|
102
|
+
if (!ctrl) return;
|
|
103
|
+
|
|
104
|
+
let markName = null;
|
|
105
|
+
if (!event.shiftKey) {
|
|
106
|
+
markName = MARK_SHORTCUTS[event.key.toLowerCase()] ?? null;
|
|
107
|
+
} else if (event.key.toLowerCase() === 'x') {
|
|
108
|
+
markName = 'strikethrough';
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (!markName) return;
|
|
112
|
+
|
|
113
|
+
event.preventDefault();
|
|
114
|
+
nabu.exec('mark:toggle', { mark: markName });
|
|
115
|
+
return nabu.BREAK;
|
|
71
116
|
}
|
|
72
117
|
|
|
73
118
|
export const RichTextExtension = new Extension('rich-text', {
|
|
119
|
+
actions: {
|
|
120
|
+
// Full API
|
|
121
|
+
'mark:toggle': (nabu, data) => toggleMark(nabu, data?.mark),
|
|
122
|
+
'mark:apply': (nabu, data) => applyMark(nabu, data?.mark, data?.value),
|
|
123
|
+
'mark:remove': (nabu, data) => removeMark(nabu, data?.mark),
|
|
124
|
+
// Aliases
|
|
125
|
+
'bold': (nabu) => toggleMark(nabu, 'bold'),
|
|
126
|
+
'italic': (nabu) => toggleMark(nabu, 'italic'),
|
|
127
|
+
'underline': (nabu) => toggleMark(nabu, 'underline'),
|
|
128
|
+
'code': (nabu) => toggleMark(nabu, 'code'),
|
|
129
|
+
'strikethrough': (nabu) => toggleMark(nabu, 'strikethrough'),
|
|
130
|
+
},
|
|
74
131
|
hooks: { onKeyDown }
|
|
75
132
|
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import { Nabu } from './nabu.svelte.js'
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { tick } from 'svelte';
|
|
6
|
+
import { Extension } from '../utils/extensions.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Transform the anchor block to a new type, then restore cursor position.
|
|
10
|
+
* @param {Nabu} nabu
|
|
11
|
+
* @param {string} type
|
|
12
|
+
* @param {Object} [props={}]
|
|
13
|
+
*/
|
|
14
|
+
function transformBlock(nabu, type, props = {}) {
|
|
15
|
+
const block = nabu.selection.anchorBlock;
|
|
16
|
+
if (!block) return;
|
|
17
|
+
const blockId = block.id;
|
|
18
|
+
const offset = block.behaviors?.get('text')?.selection?.from ?? 0;
|
|
19
|
+
block.transformTo(type, props);
|
|
20
|
+
// After transformTo, updateRoots() replaces the block instance — fetch the new one.
|
|
21
|
+
tick().then(() => {
|
|
22
|
+
const newBlock = nabu.blocks.get(blockId);
|
|
23
|
+
if (newBlock) nabu.focus({ from: { block: newBlock, offset }, to: { block: newBlock, offset } });
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export const BlockExtension = new Extension('block', {
|
|
28
|
+
actions: {
|
|
29
|
+
'block:transform': (nabu, data) => transformBlock(nabu, data?.type, data?.props ?? {}),
|
|
30
|
+
}
|
|
31
|
+
});
|
|
@@ -12,6 +12,14 @@ import { onBeforeInput } from "./hooks/onBeforeInput.hook";
|
|
|
12
12
|
const HeadingExtension = extension("heading", {
|
|
13
13
|
block: Heading,
|
|
14
14
|
component: HeadingComponent,
|
|
15
|
+
actions: {
|
|
16
|
+
'heading:1': (nabu) => nabu.exec('block:transform', { type: 'heading', props: { level: 1 } }),
|
|
17
|
+
'heading:2': (nabu) => nabu.exec('block:transform', { type: 'heading', props: { level: 2 } }),
|
|
18
|
+
'heading:3': (nabu) => nabu.exec('block:transform', { type: 'heading', props: { level: 3 } }),
|
|
19
|
+
'heading:4': (nabu) => nabu.exec('block:transform', { type: 'heading', props: { level: 4 } }),
|
|
20
|
+
'heading:5': (nabu) => nabu.exec('block:transform', { type: 'heading', props: { level: 5 } }),
|
|
21
|
+
'heading:6': (nabu) => nabu.exec('block:transform', { type: 'heading', props: { level: 6 } }),
|
|
22
|
+
},
|
|
15
23
|
hooks: {
|
|
16
24
|
/** @param {Nabu} nabu @param {Heading} block @param {Event} event @param {{offset: number, delta: import('loro-crdt').Delta<string>}} data */
|
|
17
25
|
onSplit: (nabu, block, event, data) => {
|
package/dist/blocks/index.d.ts
CHANGED
package/dist/blocks/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { extension } from "../../utils/extensions";
|
|
2
2
|
import { List } from "./list.svelte.js";
|
|
3
3
|
import ListComponent from "./List.svelte";
|
|
4
|
-
import { ListItem } from "./list-item.svelte.js";
|
|
4
|
+
import { ListItem, indentListItem, unindentListItem } from "./list-item.svelte.js";
|
|
5
5
|
import ListItemComponent from "./ListItem.svelte";
|
|
6
6
|
import { ListBehavior } from "./list.behavior.svelte.js";
|
|
7
7
|
import { onBeforeInput } from "./hooks/onBeforeInput.hook.js";
|
|
@@ -38,6 +38,18 @@ const ListExtension = extension("list", {
|
|
|
38
38
|
const ListItemExtension = extension("list-item", {
|
|
39
39
|
block: ListItem,
|
|
40
40
|
component: ListItemComponent,
|
|
41
|
+
actions: {
|
|
42
|
+
'list:indent': (nabu) => {
|
|
43
|
+
const block = nabu.selection.anchorBlock;
|
|
44
|
+
if (block?.type !== 'list-item') return;
|
|
45
|
+
indentListItem(/** @type {ListItem} */ (block));
|
|
46
|
+
},
|
|
47
|
+
'list:unindent': (nabu) => {
|
|
48
|
+
const block = nabu.selection.anchorBlock;
|
|
49
|
+
if (block?.type !== 'list-item') return;
|
|
50
|
+
unindentListItem(/** @type {ListItem} */ (block));
|
|
51
|
+
},
|
|
52
|
+
}
|
|
41
53
|
});
|
|
42
54
|
|
|
43
55
|
export { ListExtension, ListItemExtension, List, ListItem, ListComponent, ListItemComponent };
|
|
@@ -4,6 +4,18 @@
|
|
|
4
4
|
/**
|
|
5
5
|
* @typedef {NabuNode<{type: "list-item", text: LoroText}>} ListItemNode
|
|
6
6
|
*/
|
|
7
|
+
/**
|
|
8
|
+
* Indent a list item (Tab behavior): move into previous sibling's sublist, hoist own children.
|
|
9
|
+
* @param {ListItem} item
|
|
10
|
+
* @returns {boolean}
|
|
11
|
+
*/
|
|
12
|
+
export function indentListItem(item: ListItem): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Unindent a list item (Shift+Tab behavior): move out of parent sublist, carry followers.
|
|
15
|
+
* @param {ListItem} item
|
|
16
|
+
* @returns {boolean}
|
|
17
|
+
*/
|
|
18
|
+
export function unindentListItem(item: ListItem): boolean;
|
|
7
19
|
export class ListItem extends MegaBlock {
|
|
8
20
|
/** @param {Nabu} nabu @param {string} type @param {Object} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
|
|
9
21
|
static create(nabu: Nabu, type: string, props?: Object, parentId?: string | null, index?: number | null): ListItem;
|
|
@@ -11,6 +11,73 @@ import { TextBehavior } from "../../behaviors/text";
|
|
|
11
11
|
* @typedef {NabuNode<{type: "list-item", text: LoroText}>} ListItemNode
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
|
+
/**
|
|
15
|
+
* Indent a list item (Tab behavior): move into previous sibling's sublist, hoist own children.
|
|
16
|
+
* @param {ListItem} item
|
|
17
|
+
* @returns {boolean}
|
|
18
|
+
*/
|
|
19
|
+
export function indentListItem(item) {
|
|
20
|
+
const currentIndex = item.node.index();
|
|
21
|
+
if (currentIndex === 0) return false;
|
|
22
|
+
|
|
23
|
+
const parentList = item.node.parent();
|
|
24
|
+
if (!parentList) return false;
|
|
25
|
+
|
|
26
|
+
const previousNode = parentList.children()?.[currentIndex - 1];
|
|
27
|
+
if (!previousNode) return false;
|
|
28
|
+
const previousItem = item.nabu.blocks.get(previousNode.id.toString());
|
|
29
|
+
if (!previousItem || previousItem.type !== 'list-item') return false;
|
|
30
|
+
|
|
31
|
+
// @ts-ignore
|
|
32
|
+
const targetSublist = previousItem.getOrCreateSublist();
|
|
33
|
+
const targetSublistId = targetSublist.node.id.toString();
|
|
34
|
+
|
|
35
|
+
item.nabu.tree.move(item.node.id.toString(), targetSublistId);
|
|
36
|
+
|
|
37
|
+
if (item.sublist) {
|
|
38
|
+
for (const childNode of item.sublist.node.children()) {
|
|
39
|
+
item.nabu.tree.move(childNode.id.toString(), targetSublistId);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
item.commit();
|
|
44
|
+
setTimeout(() => item.behavior.block.focus(), 0);
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Unindent a list item (Shift+Tab behavior): move out of parent sublist, carry followers.
|
|
50
|
+
* @param {ListItem} item
|
|
51
|
+
* @returns {boolean}
|
|
52
|
+
*/
|
|
53
|
+
export function unindentListItem(item) {
|
|
54
|
+
const parentListNode = item.node.parent();
|
|
55
|
+
if (!parentListNode) return false;
|
|
56
|
+
|
|
57
|
+
const grandParentItemNode = parentListNode.parent();
|
|
58
|
+
if (!grandParentItemNode || grandParentItemNode.data.get('type') !== 'list-item') return false;
|
|
59
|
+
|
|
60
|
+
const myIndex = item.node.index();
|
|
61
|
+
const followers = parentListNode.children().slice(myIndex + 1);
|
|
62
|
+
|
|
63
|
+
if (followers.length > 0) {
|
|
64
|
+
const mySublist = item.getOrCreateSublist();
|
|
65
|
+
for (const follower of followers) {
|
|
66
|
+
item.nabu.tree.move(follower.id.toString(), mySublist.node.id.toString());
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const targetParentListNode = grandParentItemNode.parent();
|
|
71
|
+
if (!targetParentListNode) return false;
|
|
72
|
+
|
|
73
|
+
const targetIndex = grandParentItemNode.index() + 1;
|
|
74
|
+
item.nabu.tree.move(item.node.id.toString(), targetParentListNode.id.toString(), targetIndex);
|
|
75
|
+
|
|
76
|
+
item.commit();
|
|
77
|
+
setTimeout(() => item.behavior.block.focus(), 0);
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
|
|
14
81
|
export class ListItem extends MegaBlock {
|
|
15
82
|
/** @param {Nabu} nabu @param {ListItemNode} node */
|
|
16
83
|
constructor(nabu, node) {
|
|
@@ -93,94 +160,15 @@ export class ListItem extends MegaBlock {
|
|
|
93
160
|
|
|
94
161
|
/** @param {KeyboardEvent} event */
|
|
95
162
|
keydown(event) {
|
|
96
|
-
if (event.key ===
|
|
97
|
-
event.preventDefault();
|
|
98
|
-
|
|
163
|
+
if (event.key === 'Tab') {
|
|
164
|
+
event.preventDefault();
|
|
99
165
|
if (event.shiftKey) {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
// 1. Notre parent immédiat (une List)
|
|
103
|
-
const parentListNode = this.node.parent();
|
|
104
|
-
if (!parentListNode) return true;
|
|
105
|
-
|
|
106
|
-
// 2. Le ListItem qui contient cette liste (le "grand-parent" logique)
|
|
107
|
-
const grandParentItemNode = parentListNode.parent();
|
|
108
|
-
|
|
109
|
-
// Si on n'a pas de ListItem au-dessus (on est déjà au premier niveau)
|
|
110
|
-
if (!grandParentItemNode || grandParentItemNode.data.get("type") !== "list-item") {
|
|
111
|
-
return true;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// 3. Gérer le "Carry" : emporter les frères suivants avec nous
|
|
115
|
-
const myIndex = this.node.index();
|
|
116
|
-
const siblings = parentListNode.children();
|
|
117
|
-
const followers = siblings.slice(myIndex + 1);
|
|
118
|
-
|
|
119
|
-
if (followers.length > 0) {
|
|
120
|
-
// On s'assure d'avoir notre propre sous-liste pour accueillir nos anciens frères
|
|
121
|
-
const mySublist = this.getOrCreateSublist();
|
|
122
|
-
for (const follower of followers) {
|
|
123
|
-
this.nabu.tree.move(follower.id.toString(), mySublist.node.id.toString());
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
// 4. Le parent cible est la List qui contient notre grand-parent
|
|
128
|
-
const targetParentListNode = grandParentItemNode.parent();
|
|
129
|
-
if (!targetParentListNode) return true;
|
|
130
|
-
|
|
131
|
-
// 5. On se place juste après notre ancien grand-parent
|
|
132
|
-
const targetIndex = grandParentItemNode.index() + 1;
|
|
133
|
-
|
|
134
|
-
this.nabu.tree.move(this.node.id.toString(), targetParentListNode.id.toString(), targetIndex);
|
|
135
|
-
|
|
136
|
-
this.commit();
|
|
137
|
-
|
|
138
|
-
// 6. Restauration du focus
|
|
139
|
-
setTimeout(() => this.behavior.block.focus(), 0);
|
|
166
|
+
unindentListItem(this);
|
|
140
167
|
} else {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
// 1. On trouve notre index actuel
|
|
144
|
-
const currentIndex = this.node.index();
|
|
145
|
-
if (currentIndex === 0) return true; // Impossible d'indenter le premier item
|
|
146
|
-
|
|
147
|
-
// 2. On trouve le frère précédent (qui va devenir notre parent "logique")
|
|
148
|
-
const parentList = this.node.parent();
|
|
149
|
-
if (!parentList) return true;
|
|
150
|
-
|
|
151
|
-
const previousNode = parentList.children()?.[currentIndex - 1];
|
|
152
|
-
if (!previousNode) return true;
|
|
153
|
-
const previousItem = this.nabu.blocks.get(previousNode.id.toString());
|
|
154
|
-
|
|
155
|
-
// Si le frère précédent n'est pas un ListItem
|
|
156
|
-
if (!previousItem || previousItem.type !== "list-item") return true;
|
|
157
|
-
|
|
158
|
-
// 3. On demande au frère précédent sa sous-liste (ou de la créer)
|
|
159
|
-
// @ts-ignore : On sait que c'est un ListItem
|
|
160
|
-
const targetSublist = previousItem.getOrCreateSublist();
|
|
161
|
-
const targetSublistId = targetSublist.node.id.toString();
|
|
162
|
-
|
|
163
|
-
// 4. On se déplace à la fin de cette sous-liste cible
|
|
164
|
-
this.nabu.tree.move(this.node.id.toString(), targetSublistId);
|
|
165
|
-
|
|
166
|
-
// 5. Si on avait des enfants (Hoist), on les remonte avec nous en tant que frères
|
|
167
|
-
if (this.sublist) {
|
|
168
|
-
const childrenNodes = this.sublist.node.children();
|
|
169
|
-
for (const childNode of childrenNodes) {
|
|
170
|
-
// Ils se placeront après nous dans la nouvelle liste
|
|
171
|
-
this.nabu.tree.move(childNode.id.toString(), targetSublistId);
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
this.commit();
|
|
176
|
-
|
|
177
|
-
// 6. On restaure le curseur
|
|
178
|
-
setTimeout(() => this.behavior.block.focus(), 0);
|
|
168
|
+
indentListItem(this);
|
|
179
169
|
}
|
|
180
170
|
return true;
|
|
181
171
|
}
|
|
182
|
-
|
|
183
|
-
// On délègue les flèches etc.
|
|
184
172
|
return false;
|
|
185
173
|
}
|
|
186
174
|
|
|
@@ -51,6 +51,13 @@ export class Nabu {
|
|
|
51
51
|
init(): void;
|
|
52
52
|
/** Rafraîchit les blocs racines de l'éditeur */
|
|
53
53
|
updateRoots(): void;
|
|
54
|
+
/**
|
|
55
|
+
* Dispatch an action to all registered handlers for the given topic.
|
|
56
|
+
* @param {string} topic
|
|
57
|
+
* @param {any} [data]
|
|
58
|
+
* @returns {Promise<import('@aionbuilders/pulse').PulseEvent>}
|
|
59
|
+
*/
|
|
60
|
+
exec(topic: string, data?: any): Promise<import("@aionbuilders/pulse").PulseEvent>;
|
|
54
61
|
trigger(hookName: any, ...args: any[]): void;
|
|
55
62
|
commit(): void;
|
|
56
63
|
/**
|
|
@@ -102,6 +109,7 @@ export class Nabu {
|
|
|
102
109
|
beforeinput(e: InputEvent): any;
|
|
103
110
|
/** @param {...any} args */
|
|
104
111
|
warn(...args: any[]): void;
|
|
112
|
+
#private;
|
|
105
113
|
}
|
|
106
114
|
export type NabuNode<T extends {
|
|
107
115
|
[x: string]: any;
|
|
@@ -4,6 +4,7 @@ import { Block } from './block.svelte.js';
|
|
|
4
4
|
import { NabuSelection } from './selection.svelte.js';
|
|
5
5
|
import { handleContainerBeforeInput } from './container.utils.js';
|
|
6
6
|
import { tick } from 'svelte';
|
|
7
|
+
import { Pulse } from '@aionbuilders/pulse';
|
|
7
8
|
|
|
8
9
|
|
|
9
10
|
/**
|
|
@@ -24,6 +25,9 @@ import { tick } from 'svelte';
|
|
|
24
25
|
*/
|
|
25
26
|
|
|
26
27
|
export class Nabu {
|
|
28
|
+
/** @type {Pulse} */
|
|
29
|
+
#pulse = new Pulse();
|
|
30
|
+
|
|
27
31
|
/** @param {NabuInit} init */
|
|
28
32
|
constructor(init = {}) {
|
|
29
33
|
this.doc = new LoroDoc();
|
|
@@ -87,9 +91,19 @@ export class Nabu {
|
|
|
87
91
|
}
|
|
88
92
|
}
|
|
89
93
|
|
|
94
|
+
if (ext.actions) {
|
|
95
|
+
for (const [topic, handler] of Object.entries(ext.actions)) {
|
|
96
|
+
this.#pulse.on(topic, ({ event }) => handler(this, event.data, event.topic));
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
90
100
|
}
|
|
91
101
|
}
|
|
92
|
-
|
|
102
|
+
|
|
103
|
+
// Core actions — always available
|
|
104
|
+
this.#pulse.on('undo', () => this.undo());
|
|
105
|
+
this.#pulse.on('redo', () => this.redo());
|
|
106
|
+
|
|
93
107
|
const roots = /** @type {NabuNode[]} */ (this.tree.roots());
|
|
94
108
|
if (roots?.length) {
|
|
95
109
|
for (const root of roots) {
|
|
@@ -222,6 +236,16 @@ export class Nabu {
|
|
|
222
236
|
});
|
|
223
237
|
}
|
|
224
238
|
|
|
239
|
+
/**
|
|
240
|
+
* Dispatch an action to all registered handlers for the given topic.
|
|
241
|
+
* @param {string} topic
|
|
242
|
+
* @param {any} [data]
|
|
243
|
+
* @returns {Promise<import('@aionbuilders/pulse').PulseEvent>}
|
|
244
|
+
*/
|
|
245
|
+
exec(topic, data) {
|
|
246
|
+
return this.#pulse.emit(topic, data);
|
|
247
|
+
}
|
|
248
|
+
|
|
225
249
|
trigger(hookName, ...args) {
|
|
226
250
|
const hooks = this.hooks.get(hookName) || [];
|
|
227
251
|
for (const hook of hooks) {
|
|
@@ -9,6 +9,9 @@ import ParagraphComponent from "./Paragraph.svelte";
|
|
|
9
9
|
const ParagraphExtension = extension("paragraph", {
|
|
10
10
|
block: Paragraph,
|
|
11
11
|
component: ParagraphComponent,
|
|
12
|
+
actions: {
|
|
13
|
+
'paragraph': (nabu) => nabu.exec('block:transform', { type: 'paragraph' }),
|
|
14
|
+
},
|
|
12
15
|
hooks: {
|
|
13
16
|
onInit: (nabu) => {
|
|
14
17
|
if (nabu.children.length === 0) {
|
|
@@ -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';
|
package/dist/utils/extensions.js
CHANGED
|
@@ -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
|
+
"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
|
}
|