@aionbuilders/nabu 0.1.0-alpha.8 → 0.1.0-alpha.9
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/blocks/Block.svelte +3 -3
- package/dist/blocks/BlockGap.svelte +55 -0
- package/dist/blocks/BlockGap.svelte.d.ts +11 -0
- package/dist/blocks/index.d.ts +1 -0
- package/dist/blocks/index.js +1 -0
- package/dist/blocks/paragraph/index.js +8 -0
- package/dist/blocks/selection.svelte.d.ts +1 -0
- package/dist/blocks/selection.svelte.js +202 -16
- package/dist/blocks/zone/Zone.svelte +258 -0
- package/dist/blocks/zone/Zone.svelte.d.ts +11 -0
- package/dist/blocks/zone/index.d.ts +2 -0
- package/dist/blocks/zone/index.js +2 -0
- package/dist/blocks/zone/zone.extension.d.ts +2 -0
- package/dist/blocks/zone/zone.extension.js +221 -0
- package/dist/blocks/zone/zone.svelte.d.ts +112 -0
- package/dist/blocks/zone/zone.svelte.js +321 -0
- package/dist/presets.js +2 -1
- package/package.json +1 -1
package/dist/blocks/Block.svelte
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
<script>
|
|
2
|
+
import BlockGap from './BlockGap.svelte';
|
|
3
|
+
|
|
2
4
|
/** @type {{ block: import('./block.svelte.js').Block }} */
|
|
3
5
|
let { block } = $props();
|
|
4
6
|
|
|
@@ -8,11 +10,9 @@
|
|
|
8
10
|
</script>
|
|
9
11
|
|
|
10
12
|
{#if Component}
|
|
11
|
-
<!-- On passe l'instance du bloc au composant spécifique -->
|
|
12
13
|
<Component {block} />
|
|
13
14
|
{:else}
|
|
14
|
-
<!-- Fallback pour les types inconnus (utile en dev) -->
|
|
15
15
|
<div style="color: red; border: 1px dashed red; padding: 0.5rem; margin: 0.5rem 0;" contenteditable="false">
|
|
16
16
|
⚠️ Unknown block type: <strong>{block.type}</strong>
|
|
17
17
|
</div>
|
|
18
|
-
{/if}
|
|
18
|
+
{/if}<BlockGap {block} />
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
<script>
|
|
2
|
+
/** @type {{ block: import('./block.svelte.js').Block }} */
|
|
3
|
+
let { block } = $props();
|
|
4
|
+
|
|
5
|
+
function onclick() {
|
|
6
|
+
const parentId = block.parent?.node.id.toString() ?? null;
|
|
7
|
+
const insertIndex = block.index + 1;
|
|
8
|
+
block.nabu.exec('gap:click', { parentId, insertIndex });
|
|
9
|
+
}
|
|
10
|
+
</script>
|
|
11
|
+
|
|
12
|
+
<!-- svelte-ignore a11y_click_events_have_key_events a11y_no_static_element_interactions -->
|
|
13
|
+
<div
|
|
14
|
+
class="nabu-block-gap"
|
|
15
|
+
contenteditable="false"
|
|
16
|
+
data-nabu-inert
|
|
17
|
+
aria-hidden="true"
|
|
18
|
+
{onclick}
|
|
19
|
+
>
|
|
20
|
+
<div class="nabu-block-gap-line"></div>
|
|
21
|
+
</div>
|
|
22
|
+
|
|
23
|
+
<style>
|
|
24
|
+
.nabu-block-gap {
|
|
25
|
+
position: relative;
|
|
26
|
+
height: 0.75rem;
|
|
27
|
+
cursor: pointer;
|
|
28
|
+
user-select: none;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
.nabu-block-gap-line {
|
|
32
|
+
position: absolute;
|
|
33
|
+
inset: 50% 0 auto 0;
|
|
34
|
+
transform: translateY(-50%);
|
|
35
|
+
height: 1px;
|
|
36
|
+
background: color-mix(in srgb, currentColor 20%, transparent);
|
|
37
|
+
display: flex;
|
|
38
|
+
align-items: center;
|
|
39
|
+
justify-content: center;
|
|
40
|
+
opacity: 0;
|
|
41
|
+
transition: opacity 0.15s;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
.nabu-block-gap:hover .nabu-block-gap-line {
|
|
45
|
+
opacity: 1;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
.nabu-block-gap-plus {
|
|
49
|
+
font-size: 0.6rem;
|
|
50
|
+
line-height: 1;
|
|
51
|
+
background: white;
|
|
52
|
+
padding: 0 0.25rem;
|
|
53
|
+
color: #aaa;
|
|
54
|
+
}
|
|
55
|
+
</style>
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export default BlockGap;
|
|
2
|
+
type BlockGap = {
|
|
3
|
+
$on?(type: string, callback: (e: any) => void): () => void;
|
|
4
|
+
$set?(props: Partial<$$ComponentProps>): void;
|
|
5
|
+
};
|
|
6
|
+
declare const BlockGap: import("svelte").Component<{
|
|
7
|
+
block: import("./block.svelte.js").Block;
|
|
8
|
+
}, {}, "">;
|
|
9
|
+
type $$ComponentProps = {
|
|
10
|
+
block: import("./block.svelte.js").Block;
|
|
11
|
+
};
|
package/dist/blocks/index.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ export * from "./paragraph";
|
|
|
7
7
|
export * from "./heading";
|
|
8
8
|
export * from "./list";
|
|
9
9
|
export * from "./dialogue";
|
|
10
|
+
export * from "./zone";
|
|
10
11
|
export * from "./selection.svelte.js";
|
|
11
12
|
export { default as NabuEditor } from "./Nabu.svelte";
|
|
12
13
|
export { default as BlockComponent } from "./Block.svelte";
|
package/dist/blocks/index.js
CHANGED
|
@@ -11,6 +11,14 @@ const ParagraphExtension = extension("paragraph", {
|
|
|
11
11
|
component: ParagraphComponent,
|
|
12
12
|
actions: {
|
|
13
13
|
'paragraph': (nabu) => nabu.exec('block:transform', { type: 'paragraph' }),
|
|
14
|
+
'gap:click': (nabu, data) => {
|
|
15
|
+
const { parentId = null, insertIndex = undefined } = data ?? {};
|
|
16
|
+
const newBlock = nabu.insert('paragraph', {}, parentId, insertIndex);
|
|
17
|
+
nabu.commit().then(() => {
|
|
18
|
+
const fresh = nabu.blocks.get(newBlock.id);
|
|
19
|
+
if (fresh) nabu.selection.setCursor(fresh, 0);
|
|
20
|
+
});
|
|
21
|
+
},
|
|
14
22
|
},
|
|
15
23
|
hooks: {
|
|
16
24
|
onInit: (nabu) => {
|
|
@@ -32,6 +32,7 @@ export class NabuSelection extends SvelteSelection {
|
|
|
32
32
|
*/
|
|
33
33
|
setCursor(block: Block, offset: number): void;
|
|
34
34
|
blocks: Set<Block>;
|
|
35
|
+
#private;
|
|
35
36
|
}
|
|
36
37
|
import { SvelteSelection } from "../utils/selection.svelte.js";
|
|
37
38
|
import type { Nabu } from "./nabu.svelte.js";
|
|
@@ -15,25 +15,101 @@ export class NabuSelection extends SvelteSelection {
|
|
|
15
15
|
|
|
16
16
|
$effect.root(() => {
|
|
17
17
|
$effect(() => {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
18
|
+
if (this.blocks) {
|
|
19
|
+
this.previous.forEach(block => block.clearSelection());
|
|
20
|
+
let i = 0;
|
|
21
|
+
const lastIndex = this.blocks.size - 1;
|
|
22
|
+
this.blocks.forEach((block) => {
|
|
23
|
+
block.selected = true;
|
|
24
|
+
block.isSelectionStart = (i === 0);
|
|
25
|
+
block.isSelectionEnd = i === lastIndex;
|
|
26
|
+
i++;
|
|
27
|
+
});
|
|
28
|
+
this.previous = new Set(this.blocks);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
31
|
|
|
32
|
-
|
|
32
|
+
// Track the intended navigation direction from arrow key presses.
|
|
33
|
+
// This is the only reliable source of direction — DOM position comparisons
|
|
34
|
+
// break when anchorNode is an ancestor of the previous position.
|
|
35
|
+
$effect(() => {
|
|
36
|
+
/** @param {KeyboardEvent} e */
|
|
37
|
+
const onKeydown = (e) => {
|
|
38
|
+
if (e.key === 'ArrowRight' || e.key === 'ArrowDown') {
|
|
39
|
+
this.#pendingDirection = 'forward';
|
|
40
|
+
} else if (e.key === 'ArrowLeft' || e.key === 'ArrowUp') {
|
|
41
|
+
this.#pendingDirection = 'backward';
|
|
42
|
+
} else {
|
|
43
|
+
this.#pendingDirection = null;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
document.addEventListener('keydown', onKeydown, true);
|
|
47
|
+
return () => document.removeEventListener('keydown', onKeydown, true);
|
|
48
|
+
});
|
|
33
49
|
|
|
34
|
-
|
|
50
|
+
// Normalize invalid caret/focus positions: inert elements and element-node
|
|
51
|
+
// cursors (which happen in container padding / between block children).
|
|
52
|
+
// Works for both collapsed cursors and non-collapsed selections (shift+arrow):
|
|
53
|
+
// - anchor invalid → collapse to corrected anchor
|
|
54
|
+
// - only focus invalid → keep anchor, correct only focus (preserves the selection)
|
|
55
|
+
$effect(() => {
|
|
56
|
+
const activeEl = document.activeElement;
|
|
57
|
+
if (activeEl && activeEl.closest?.('[data-nabu-ui], input, textarea, select, button')) {
|
|
58
|
+
this.#pendingDirection = null;
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const anchor = this.anchorNode;
|
|
63
|
+
const anchorOffset = this.anchorOffset;
|
|
64
|
+
const focus = this.focusNode;
|
|
65
|
+
const focusOffset = this.focusOffset;
|
|
66
|
+
if (!anchor) return;
|
|
67
|
+
|
|
68
|
+
const anchorEl = /** @type {Element | null} */ (anchor.nodeType === Node.ELEMENT_NODE ? anchor : anchor.parentElement);
|
|
69
|
+
const focusEl = /** @type {Element | null} */ (focus?.nodeType === Node.ELEMENT_NODE ? focus : focus?.parentElement);
|
|
70
|
+
|
|
71
|
+
// Skip normalization when the selection touches a UI element (e.g. zone title input).
|
|
72
|
+
if (anchorEl?.closest('[data-nabu-ui]') || focusEl?.closest('[data-nabu-ui]')) {
|
|
73
|
+
this.#pendingDirection = null;
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const anchorInvalid = isInInert(anchor) || (
|
|
78
|
+
anchor.nodeType !== Node.TEXT_NODE &&
|
|
79
|
+
!!/** @type {Element} */ (anchor).closest?.('[data-block-id]')
|
|
80
|
+
);
|
|
81
|
+
const focusInvalid = !!focus && (isInInert(focus) || (
|
|
82
|
+
focus.nodeType !== Node.TEXT_NODE &&
|
|
83
|
+
!!/** @type {Element} */ (focus).closest?.('[data-block-id]')
|
|
84
|
+
));
|
|
85
|
+
|
|
86
|
+
if (!anchorInvalid && !focusInvalid) {
|
|
87
|
+
this.#pendingDirection = null;
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (anchorInvalid) {
|
|
92
|
+
const dir = this.#pendingDirection ?? offsetHeuristic(anchor, anchorOffset);
|
|
93
|
+
this.#pendingDirection = null;
|
|
94
|
+
const target = findNearestTextNode(anchor, anchorOffset, dir);
|
|
95
|
+
if (!target) return;
|
|
96
|
+
const offset = dir === 'forward' ? 0 : (target.textContent?.length ?? 0);
|
|
97
|
+
this.setBaseAndExtent(target, offset, target, offset);
|
|
98
|
+
} else if (focusInvalid && focus) {
|
|
99
|
+
const dir = this.#pendingDirection ?? offsetHeuristic(focus, focusOffset);
|
|
100
|
+
this.#pendingDirection = null;
|
|
101
|
+
const target = findNearestTextNode(focus, focusOffset, dir);
|
|
102
|
+
if (!target) return;
|
|
103
|
+
const offset = dir === 'forward' ? 0 : (target.textContent?.length ?? 0);
|
|
104
|
+
this.setBaseAndExtent(anchor, anchorOffset, target, offset);
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
});
|
|
35
108
|
}
|
|
36
|
-
|
|
109
|
+
|
|
110
|
+
/** @type {'forward' | 'backward' | null} */
|
|
111
|
+
#pendingDirection = null;
|
|
112
|
+
|
|
37
113
|
anchorBlock = $derived.by(() => {
|
|
38
114
|
this.anchorNode;
|
|
39
115
|
const id = this.anchorNode?.parentElement?.closest("[data-block-id]")?.getAttribute("data-block-id");
|
|
@@ -95,6 +171,116 @@ export class NabuSelection extends SvelteSelection {
|
|
|
95
171
|
|
|
96
172
|
|
|
97
173
|
|
|
174
|
+
/** @param {Node | null} node */
|
|
175
|
+
function isInInert(node) {
|
|
176
|
+
if (!node) return false;
|
|
177
|
+
const el = node.nodeType === Node.ELEMENT_NODE
|
|
178
|
+
? /** @type {Element} */ (node)
|
|
179
|
+
: node.parentElement;
|
|
180
|
+
return !!el?.closest('[data-nabu-inert]');
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Heuristic for mouse clicks: infer direction from where in the element the
|
|
185
|
+
* cursor landed. At/past the last child → user likely moving forward; otherwise
|
|
186
|
+
* backward.
|
|
187
|
+
* @param {Node} anchor
|
|
188
|
+
* @param {number} anchorOffset
|
|
189
|
+
* @returns {'forward' | 'backward'}
|
|
190
|
+
*/
|
|
191
|
+
function offsetHeuristic(anchor, anchorOffset) {
|
|
192
|
+
if (anchor.nodeType !== Node.TEXT_NODE) {
|
|
193
|
+
return anchorOffset >= anchor.childNodes.length ? 'forward' : 'backward';
|
|
194
|
+
}
|
|
195
|
+
return 'backward';
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** @type {NodeFilter} */
|
|
199
|
+
const TEXT_FILTER = {
|
|
200
|
+
acceptNode(/** @type {Node} */ node) {
|
|
201
|
+
if (isInInert(node)) return NodeFilter.FILTER_REJECT;
|
|
202
|
+
if (!/** @type {Text} */ (node).parentElement?.closest('[data-block-id]')) return NodeFilter.FILTER_REJECT;
|
|
203
|
+
return NodeFilter.FILTER_ACCEPT;
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Returns the deepest last descendant of a node (for positioning a TreeWalker
|
|
209
|
+
* so that nextNode() starts at the node immediately after the subtree).
|
|
210
|
+
* @param {Node} node
|
|
211
|
+
*/
|
|
212
|
+
function getLastDescendant(node) {
|
|
213
|
+
let n = node;
|
|
214
|
+
while (n.lastChild) n = n.lastChild;
|
|
215
|
+
return n;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Find the nearest valid (non-inert, inside a block) text node from a given
|
|
220
|
+
* DOM position, respecting the intended navigation direction.
|
|
221
|
+
*
|
|
222
|
+
* For element-node cursors, (fromNode, fromOffset) means the caret sits between
|
|
223
|
+
* child[fromOffset-1] and child[fromOffset]. We use fromOffset to start the
|
|
224
|
+
* search at the right position — NOT from the beginning/end of the element —
|
|
225
|
+
* so we never jump back to a position the user is trying to leave.
|
|
226
|
+
*
|
|
227
|
+
* @param {Node} fromNode
|
|
228
|
+
* @param {number} fromOffset
|
|
229
|
+
* @param {'forward' | 'backward'} direction
|
|
230
|
+
* @returns {Text | null}
|
|
231
|
+
*/
|
|
232
|
+
function findNearestTextNode(fromNode, fromOffset, direction) {
|
|
233
|
+
// @ts-ignore — TreeWalker filter shape is correct, TS typedef is overly strict
|
|
234
|
+
const bodyWalker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT, TEXT_FILTER);
|
|
235
|
+
|
|
236
|
+
if (fromNode.nodeType === Node.TEXT_NODE) {
|
|
237
|
+
bodyWalker.currentNode = fromNode;
|
|
238
|
+
return /** @type {Text | null} */ (
|
|
239
|
+
direction === 'backward' ? bodyWalker.previousNode() : bodyWalker.nextNode()
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// Element-node cursor: caret is between child[fromOffset-1] and child[fromOffset].
|
|
244
|
+
const childCount = fromNode.childNodes.length;
|
|
245
|
+
|
|
246
|
+
if (direction === 'backward') {
|
|
247
|
+
if (fromOffset === 0) {
|
|
248
|
+
// Before all children → escape before element
|
|
249
|
+
bodyWalker.currentNode = fromNode;
|
|
250
|
+
return /** @type {Text | null} */ (bodyWalker.previousNode());
|
|
251
|
+
}
|
|
252
|
+
if (fromOffset < childCount) {
|
|
253
|
+
// Between child[fromOffset-1] and child[fromOffset]:
|
|
254
|
+
// last valid text before child[fromOffset] (may be inside or before element)
|
|
255
|
+
bodyWalker.currentNode = fromNode.childNodes[fromOffset];
|
|
256
|
+
return /** @type {Text | null} */ (bodyWalker.previousNode());
|
|
257
|
+
}
|
|
258
|
+
// Past last child → last valid text inside the element (sub-walker), or before it
|
|
259
|
+
// @ts-ignore
|
|
260
|
+
const sub = document.createTreeWalker(fromNode, NodeFilter.SHOW_TEXT, TEXT_FILTER);
|
|
261
|
+
let last = null, n;
|
|
262
|
+
while ((n = sub.nextNode())) last = n;
|
|
263
|
+
if (last) return /** @type {Text} */ (last);
|
|
264
|
+
bodyWalker.currentNode = fromNode;
|
|
265
|
+
return /** @type {Text | null} */ (bodyWalker.previousNode());
|
|
266
|
+
} else {
|
|
267
|
+
if (fromOffset >= childCount) {
|
|
268
|
+
// Past last child → escape after element
|
|
269
|
+
bodyWalker.currentNode = getLastDescendant(fromNode);
|
|
270
|
+
return /** @type {Text | null} */ (bodyWalker.nextNode());
|
|
271
|
+
}
|
|
272
|
+
// Between child[fromOffset-1] and child[fromOffset]:
|
|
273
|
+
// first valid text at or after child[fromOffset] (may be inside or after element).
|
|
274
|
+
// Pivot to the last descendant of child[fromOffset-1] so nextNode() starts
|
|
275
|
+
// from child[fromOffset] onward — never re-entering already-visited content.
|
|
276
|
+
const pivot = fromOffset > 0
|
|
277
|
+
? getLastDescendant(fromNode.childNodes[fromOffset - 1])
|
|
278
|
+
: fromNode;
|
|
279
|
+
bodyWalker.currentNode = pivot;
|
|
280
|
+
return /** @type {Text | null} */ (bodyWalker.nextNode());
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
98
284
|
/**
|
|
99
285
|
* Récupère tous les nœuds situés entre deux nœuds dans un arbre Loro (version aplatie).
|
|
100
286
|
* L'ordre dans lequel on passe A et B n'a pas d'importance.
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
<script>
|
|
2
|
+
import Block from '../Block.svelte';
|
|
3
|
+
/** @type {{ block: import('./zone.svelte.js').ZoneBlock }} */
|
|
4
|
+
let { block } = $props();
|
|
5
|
+
|
|
6
|
+
let editing = $state(false);
|
|
7
|
+
let titleDraft = $state('');
|
|
8
|
+
/** @type {HTMLInputElement | null} */
|
|
9
|
+
let inputEl = $state(null);
|
|
10
|
+
|
|
11
|
+
// Ancestor zones, ordered from outermost to nearest parent
|
|
12
|
+
const ancestors = $derived.by(() => {
|
|
13
|
+
/** @type {import('./zone.svelte.js').ZoneBlock[]} */
|
|
14
|
+
const crumbs = [];
|
|
15
|
+
let p = block.parent;
|
|
16
|
+
while (p) {
|
|
17
|
+
if (p.type === 'zone') crumbs.unshift(/** @type {any} */ (p));
|
|
18
|
+
p = p.parent;
|
|
19
|
+
}
|
|
20
|
+
return crumbs;
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
function startEdit() {
|
|
24
|
+
titleDraft = block.title;
|
|
25
|
+
editing = true;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function commitTitle() {
|
|
29
|
+
block.title = titleDraft.trim();
|
|
30
|
+
editing = false;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** @param {KeyboardEvent} e */
|
|
34
|
+
function onKeydown(e) {
|
|
35
|
+
e.stopPropagation();
|
|
36
|
+
if (e.key === 'Enter' || e.key === 'Escape') {
|
|
37
|
+
e.preventDefault();
|
|
38
|
+
commitTitle();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** @param {InputEvent} e */
|
|
43
|
+
function onBeforeInput(e) {
|
|
44
|
+
e.stopPropagation();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
$effect(() => {
|
|
48
|
+
if (editing && inputEl) {
|
|
49
|
+
inputEl.focus();
|
|
50
|
+
inputEl.select();
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
</script>
|
|
54
|
+
|
|
55
|
+
<div
|
|
56
|
+
class="nabu-zone"
|
|
57
|
+
data-block-id={block.id}
|
|
58
|
+
data-block-type="zone"
|
|
59
|
+
data-zone-id={block.id}
|
|
60
|
+
data-zone-status={block.status}
|
|
61
|
+
>
|
|
62
|
+
{#each block.children as child (child.id)}
|
|
63
|
+
<Block block={child} />
|
|
64
|
+
{/each}<!--
|
|
65
|
+
Zone UI overlay: boundaries + title (contenteditable=false = hors-flux éditeur)
|
|
66
|
+
--><div class="nabu-zone-ui" contenteditable="false" aria-hidden="true" style="user-select: none;">
|
|
67
|
+
|
|
68
|
+
<!-- <div class="nabu-zone-boundary nabu-zone-boundary--top" data-nabu-inert></div> -->
|
|
69
|
+
<div class="nabu-zone-boundary nabu-zone-boundary--bottom" data-nabu-inert style="user-select: none;">
|
|
70
|
+
<div class="nabu-zone-close">
|
|
71
|
+
{#each ancestors as anc}
|
|
72
|
+
<span class="nabu-zone-close-sep">‹</span>
|
|
73
|
+
<span class="nabu-zone-close-crumb">{anc.title || '…'}</span>
|
|
74
|
+
{/each}
|
|
75
|
+
<span class="nabu-zone-close-sep">‹</span>
|
|
76
|
+
<span class="nabu-zone-close-current">{block.title || '…'}</span>
|
|
77
|
+
</div>
|
|
78
|
+
</div>
|
|
79
|
+
|
|
80
|
+
<div
|
|
81
|
+
class="nabu-zone-title"
|
|
82
|
+
data-nabu-ui
|
|
83
|
+
|
|
84
|
+
>
|
|
85
|
+
<span class="nabu-zone-crumb-sep">›</span>
|
|
86
|
+
{#if ancestors.length > 0}
|
|
87
|
+
<!-- Breadcrumb for nested zones -->
|
|
88
|
+
{#each ancestors as anc}
|
|
89
|
+
<span class="nabu-zone-crumb">{anc.title || '…'}</span>
|
|
90
|
+
<span class="nabu-zone-crumb-sep">›</span>
|
|
91
|
+
{/each}
|
|
92
|
+
{/if}
|
|
93
|
+
|
|
94
|
+
{#if editing}
|
|
95
|
+
<input
|
|
96
|
+
bind:this={inputEl}
|
|
97
|
+
bind:value={titleDraft}
|
|
98
|
+
class="nabu-zone-title-input"
|
|
99
|
+
placeholder="Titre de la zone…"
|
|
100
|
+
onblur={commitTitle}
|
|
101
|
+
onkeydown={onKeydown}
|
|
102
|
+
onbeforeinput={onBeforeInput}
|
|
103
|
+
/>
|
|
104
|
+
{:else}
|
|
105
|
+
<span class="nabu-zone-title-label" onclick={startEdit}>
|
|
106
|
+
{block.title || '+ Titre'}
|
|
107
|
+
</span>
|
|
108
|
+
{/if}
|
|
109
|
+
|
|
110
|
+
<span class="nabu-zone-meta">
|
|
111
|
+
<!-- <span class="nabu-zone-sep">·</span>
|
|
112
|
+
<span class="nabu-zone-status">{block.status}</span> -->
|
|
113
|
+
{#if block.wordCount > 0}
|
|
114
|
+
<span class="nabu-zone-sep">·</span>
|
|
115
|
+
<span class="nabu-zone-wc">{block.wordCount} mots</span>
|
|
116
|
+
{/if}
|
|
117
|
+
</span>
|
|
118
|
+
</div>
|
|
119
|
+
|
|
120
|
+
</div>
|
|
121
|
+
</div>
|
|
122
|
+
|
|
123
|
+
<style>
|
|
124
|
+
.nabu-zone {
|
|
125
|
+
margin: 1rem 0;
|
|
126
|
+
padding: 0.75rem 0;
|
|
127
|
+
position: relative;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
.nabu-zone-ui {
|
|
131
|
+
position: absolute;
|
|
132
|
+
inset: 0;
|
|
133
|
+
pointer-events: none;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
.nabu-zone-boundary {
|
|
137
|
+
position: absolute;
|
|
138
|
+
left: 0;
|
|
139
|
+
right: 0;
|
|
140
|
+
opacity: 0.6;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
.nabu-zone-boundary--bottom {
|
|
145
|
+
bottom: 0; display: flex; justify-content: flex-start; align-items: center;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
.nabu-zone-close {
|
|
149
|
+
transform: translateY(50%);
|
|
150
|
+
display: flex;
|
|
151
|
+
align-items: center;
|
|
152
|
+
gap: 0;
|
|
153
|
+
line-height: 1;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
.nabu-zone-close-sep {
|
|
157
|
+
font-size: 0.6rem;
|
|
158
|
+
color: #ccc;
|
|
159
|
+
margin: 0 0.8em;
|
|
160
|
+
&:first-child {
|
|
161
|
+
margin-left: 0;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
.nabu-zone-close-crumb {
|
|
166
|
+
font-size: 0.6rem;
|
|
167
|
+
color: #bbb;
|
|
168
|
+
letter-spacing: 0.04em;
|
|
169
|
+
&:first-child {
|
|
170
|
+
margin-left: 0;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
.nabu-zone-close-current {
|
|
175
|
+
font-size: 0.6rem;
|
|
176
|
+
color: #aaa;
|
|
177
|
+
letter-spacing: 0.04em;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
[data-zone-status="revised"] .nabu-zone-boundary { background: #f59e0b; opacity: 0.35; }
|
|
181
|
+
[data-zone-status="done"] .nabu-zone-boundary { background: #10b981; opacity: 0.35; }
|
|
182
|
+
|
|
183
|
+
/* Title — flotte sur la boundary top */
|
|
184
|
+
.nabu-zone-title {
|
|
185
|
+
position: absolute;
|
|
186
|
+
top: 0;
|
|
187
|
+
transform: translateY(-50%);
|
|
188
|
+
pointer-events: all;
|
|
189
|
+
line-height: 1;
|
|
190
|
+
display: flex;
|
|
191
|
+
align-items: center;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
.nabu-zone-title-label {
|
|
195
|
+
font-size: 0.6rem;
|
|
196
|
+
font-weight: 400;
|
|
197
|
+
letter-spacing: 0.04em;
|
|
198
|
+
color: #aaa;
|
|
199
|
+
cursor: pointer;
|
|
200
|
+
user-select: none;
|
|
201
|
+
transition: color 0.15s;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
.nabu-zone-title-label:hover {
|
|
205
|
+
color: #666;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
.nabu-zone-title-input {
|
|
209
|
+
font-size: 0.7rem;
|
|
210
|
+
font-weight: 400;
|
|
211
|
+
letter-spacing: 0.04em;
|
|
212
|
+
color: #666;
|
|
213
|
+
border: none;
|
|
214
|
+
outline: none;
|
|
215
|
+
background: transparent;
|
|
216
|
+
padding: 0;
|
|
217
|
+
width: 14rem;
|
|
218
|
+
max-width: 40vw;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
.nabu-zone-crumb {
|
|
222
|
+
font-size: 0.6rem;
|
|
223
|
+
font-weight: 400;
|
|
224
|
+
letter-spacing: 0.04em;
|
|
225
|
+
color: #bbb;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
.nabu-zone-crumb-sep {
|
|
229
|
+
font-size: 0.6rem;
|
|
230
|
+
color: #ccc;
|
|
231
|
+
margin: 0 0.8em;
|
|
232
|
+
&:first-child {
|
|
233
|
+
margin-left: 0;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
.nabu-zone-meta {
|
|
238
|
+
display: contents;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
.nabu-zone-sep {
|
|
242
|
+
font-size: 0.6rem;
|
|
243
|
+
color: #ccc;
|
|
244
|
+
margin: 0 0.8em;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
.nabu-zone-status {
|
|
248
|
+
font-size: 0.6rem;
|
|
249
|
+
color: #bbb;
|
|
250
|
+
letter-spacing: 0.04em;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
.nabu-zone-wc {
|
|
254
|
+
font-size: 0.6rem;
|
|
255
|
+
color: #bbb;
|
|
256
|
+
letter-spacing: 0.04em;
|
|
257
|
+
}
|
|
258
|
+
</style>
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export default Zone;
|
|
2
|
+
type Zone = {
|
|
3
|
+
$on?(type: string, callback: (e: any) => void): () => void;
|
|
4
|
+
$set?(props: Partial<$$ComponentProps>): void;
|
|
5
|
+
};
|
|
6
|
+
declare const Zone: import("svelte").Component<{
|
|
7
|
+
block: import("./zone.svelte.js").ZoneBlock;
|
|
8
|
+
}, {}, "">;
|
|
9
|
+
type $$ComponentProps = {
|
|
10
|
+
block: import("./zone.svelte.js").ZoneBlock;
|
|
11
|
+
};
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { Extension } from '../../utils/extensions.js';
|
|
2
|
+
import { ZoneBlock } from './zone.svelte.js';
|
|
3
|
+
import ZoneComponent from './Zone.svelte';
|
|
4
|
+
import { findLCA, findDirectChildOf } from '../container.utils.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @import { Nabu } from '../nabu.svelte.js';
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* onBeforeInput hook — intercepts deleteContentBackward at the start of the
|
|
12
|
+
* first content block inside a Zone, before TextBehavior's findBackward() can
|
|
13
|
+
* traverse up and out of the Zone boundary.
|
|
14
|
+
*
|
|
15
|
+
* Fires BEFORE block routing in Nabu.dispatchEventToSelection.
|
|
16
|
+
*
|
|
17
|
+
* @param {Nabu} nabu
|
|
18
|
+
* @param {InputEvent} event
|
|
19
|
+
* @param {import('../block.svelte.js').Block} block
|
|
20
|
+
*/
|
|
21
|
+
function zoneBackspaceGuard(nabu, event, block) {
|
|
22
|
+
if (event.inputType !== 'deleteContentBackward') return;
|
|
23
|
+
|
|
24
|
+
// Use the block's model selection (TextBehavior.selection) rather than the raw
|
|
25
|
+
// DOM startOffset, which varies by text node structure and is unreliable here.
|
|
26
|
+
const blockSel = block.selection;
|
|
27
|
+
if (!blockSel || !blockSel.isCollapsed || blockSel.from !== 0) return;
|
|
28
|
+
|
|
29
|
+
// Walk up from block. If every ancestor up to a Zone is the first child (index 0),
|
|
30
|
+
// the cursor is at the very start of the Zone — block the event.
|
|
31
|
+
// Stop early if a non-first-child is found (there's something to merge with inside).
|
|
32
|
+
let current = block;
|
|
33
|
+
while (current && current.parent) {
|
|
34
|
+
if (current.parent.type === 'zone') {
|
|
35
|
+
if (current.index === 0) return nabu.BREAK;
|
|
36
|
+
break; // Has a previous sibling inside the zone — allow normal merge
|
|
37
|
+
}
|
|
38
|
+
if (current.index !== 0) break; // Has a previous sibling in a sub-container
|
|
39
|
+
current = current.parent;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export const ZoneExtension = new Extension('zone', {
|
|
44
|
+
block: ZoneBlock,
|
|
45
|
+
component: ZoneComponent,
|
|
46
|
+
|
|
47
|
+
hooks: {
|
|
48
|
+
onBeforeInput: zoneBackspaceGuard,
|
|
49
|
+
onZoneFocus: null,
|
|
50
|
+
onZoneBlur: null,
|
|
51
|
+
},
|
|
52
|
+
|
|
53
|
+
actions: {
|
|
54
|
+
/**
|
|
55
|
+
* Move all children of the Zone to its parent position, then delete the Zone.
|
|
56
|
+
* @param {Nabu} nabu
|
|
57
|
+
* @param {{ zoneId: string }} data
|
|
58
|
+
*/
|
|
59
|
+
'zone:unwrap': (nabu, { zoneId }) => {
|
|
60
|
+
const zone = nabu.blocks.get(zoneId);
|
|
61
|
+
if (!zone || zone.type !== 'zone') return;
|
|
62
|
+
|
|
63
|
+
const parentNode = zone.node.parent();
|
|
64
|
+
const parentId = parentNode ? parentNode.id : undefined;
|
|
65
|
+
const zoneIndex = zone.node.index();
|
|
66
|
+
|
|
67
|
+
// Reverse so that when each child is inserted at zoneIndex, the original
|
|
68
|
+
// order is preserved (each insert pushes the previous one down).
|
|
69
|
+
// @ts-ignore — zone.children comes from MegaBlock
|
|
70
|
+
const children = /** @type {import('../block.svelte.js').Block[]} */ ([...zone.children]);
|
|
71
|
+
children.reverse().forEach(child => {
|
|
72
|
+
nabu.tree.move(child.node.id, parentId, zoneIndex);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
nabu.tree.delete(zone.node.id);
|
|
76
|
+
nabu.commit();
|
|
77
|
+
},
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* @param {Nabu} nabu
|
|
81
|
+
* @param {{ zoneId: string, status: string }} data
|
|
82
|
+
*/
|
|
83
|
+
'zone:setStatus': (nabu, { zoneId, status }) => {
|
|
84
|
+
const zone = /** @type {import('./zone.svelte.js').ZoneBlock | undefined} */ (nabu.blocks.get(zoneId));
|
|
85
|
+
if (!zone || zone.type !== 'zone') return;
|
|
86
|
+
zone.status = status;
|
|
87
|
+
},
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* @param {Nabu} nabu
|
|
91
|
+
* @param {{ zoneId: string, text: string }} data
|
|
92
|
+
*/
|
|
93
|
+
'zone:setSynopsis': (nabu, { zoneId, text }) => {
|
|
94
|
+
const zone = /** @type {import('./zone.svelte.js').ZoneBlock | undefined} */ (nabu.blocks.get(zoneId));
|
|
95
|
+
if (!zone || zone.type !== 'zone') return;
|
|
96
|
+
zone.setSynopsis(text);
|
|
97
|
+
},
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* @param {Nabu} nabu
|
|
101
|
+
* @param {{ zoneId: string, target: number | null }} data
|
|
102
|
+
*/
|
|
103
|
+
'zone:setWordCountTarget': (nabu, { zoneId, target }) => {
|
|
104
|
+
const zone = /** @type {import('./zone.svelte.js').ZoneBlock | undefined} */ (nabu.blocks.get(zoneId));
|
|
105
|
+
if (!zone || zone.type !== 'zone') return;
|
|
106
|
+
zone.wordCountTarget = target;
|
|
107
|
+
},
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Insert a new empty zone (with one paragraph) after the anchor block's
|
|
111
|
+
* root-level ancestor. Moves the cursor into the new paragraph.
|
|
112
|
+
* @param {Nabu} nabu
|
|
113
|
+
* @param {{ props?: Object }} [data]
|
|
114
|
+
*/
|
|
115
|
+
'zone:insert': (nabu, data) => {
|
|
116
|
+
const anchor = nabu.selection.anchorBlock;
|
|
117
|
+
if (!anchor) return;
|
|
118
|
+
|
|
119
|
+
// If the cursor is inside a zone, insert there; otherwise insert at root level.
|
|
120
|
+
let nearestZone = anchor.parent;
|
|
121
|
+
while (nearestZone && nearestZone.type !== 'zone') nearestZone = nearestZone.parent;
|
|
122
|
+
|
|
123
|
+
let insertIndex, parentId;
|
|
124
|
+
if (nearestZone) {
|
|
125
|
+
const directChild = findDirectChildOf(anchor, nearestZone);
|
|
126
|
+
insertIndex = (directChild?.index ?? 0) + 1;
|
|
127
|
+
parentId = nearestZone.node.id.toString();
|
|
128
|
+
} else {
|
|
129
|
+
let rootBlock = anchor;
|
|
130
|
+
while (rootBlock.parent) rootBlock = rootBlock.parent;
|
|
131
|
+
insertIndex = (nabu.children.indexOf(rootBlock)) + 1;
|
|
132
|
+
parentId = null;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Create zone + paragraph in one transaction
|
|
136
|
+
const zone = ZoneBlock.create(nabu, 'zone', data?.props ?? {}, parentId, insertIndex);
|
|
137
|
+
|
|
138
|
+
const ParagraphClass = nabu.registry.get('paragraph');
|
|
139
|
+
let newParagraph = null;
|
|
140
|
+
if (ParagraphClass) {
|
|
141
|
+
newParagraph = ParagraphClass.create(nabu, 'paragraph', {}, zone.node.id.toString(), 0);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
nabu.commit().then(() => {
|
|
145
|
+
const target = newParagraph ? nabu.blocks.get(newParagraph.id) ?? newParagraph : null;
|
|
146
|
+
if (target) nabu.selection.setCursor(target, 0);
|
|
147
|
+
});
|
|
148
|
+
},
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Wrap the selected blocks in a new zone.
|
|
152
|
+
*
|
|
153
|
+
* - Collapsed / single block: wraps the block's direct child of its
|
|
154
|
+
* nearest non-zone ancestor (or the root-level block).
|
|
155
|
+
* - Multi-block selection: finds the LCA of startBlock/endBlock, resolves
|
|
156
|
+
* their direct children inside that container, and wraps the whole range.
|
|
157
|
+
* - No-op if every candidate is already a zone.
|
|
158
|
+
*
|
|
159
|
+
* @param {Nabu} nabu
|
|
160
|
+
* @param {{ props?: Object }} [data]
|
|
161
|
+
*/
|
|
162
|
+
'zone:wrap': (nabu, data) => {
|
|
163
|
+
const sel = nabu.selection;
|
|
164
|
+
const startBlock = sel.startBlock ?? sel.anchorBlock;
|
|
165
|
+
const endBlock = sel.endBlock ?? sel.focusBlock;
|
|
166
|
+
if (!startBlock) return;
|
|
167
|
+
|
|
168
|
+
const savedOffset = startBlock.behaviors?.get('text')?.selection?.from ?? 0;
|
|
169
|
+
|
|
170
|
+
/** @type {import('../block.svelte.js').Block[]} */
|
|
171
|
+
let toWrap;
|
|
172
|
+
/** @type {string | null} */
|
|
173
|
+
let parentId;
|
|
174
|
+
/** @type {number} */
|
|
175
|
+
let insertIndex;
|
|
176
|
+
|
|
177
|
+
if (!endBlock || startBlock === endBlock || sel.isCollapsed) {
|
|
178
|
+
// ── Single-block path ──────────────────────────────────────────
|
|
179
|
+
// Find direct child of the nearest container (zone or root level).
|
|
180
|
+
// The while loop stops at zone boundaries, so wrapping inside a zone
|
|
181
|
+
// creates a nested zone — which is now intentionally supported.
|
|
182
|
+
let target = startBlock;
|
|
183
|
+
while (target.parent && target.parent.type !== 'zone') {
|
|
184
|
+
target = target.parent;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
toWrap = [target];
|
|
188
|
+
parentId = target.parent ? target.parent.node.id.toString() : null;
|
|
189
|
+
insertIndex = target.index ?? 0;
|
|
190
|
+
} else {
|
|
191
|
+
// ── Multi-block path ───────────────────────────────────────────
|
|
192
|
+
const lca = findLCA(startBlock, endBlock);
|
|
193
|
+
// Container is the LCA block, or nabu itself if blocks are root-level
|
|
194
|
+
const container = lca ?? nabu;
|
|
195
|
+
|
|
196
|
+
const sc = findDirectChildOf(startBlock, container);
|
|
197
|
+
const ec = findDirectChildOf(endBlock, container);
|
|
198
|
+
if (!sc || !ec) return;
|
|
199
|
+
|
|
200
|
+
const containerChildren = /** @type {import('../block.svelte.js').Block[]} */ (
|
|
201
|
+
lca ? /** @type {any} */ (lca).children : nabu.children
|
|
202
|
+
);
|
|
203
|
+
const si = containerChildren.indexOf(sc);
|
|
204
|
+
const ei = containerChildren.indexOf(ec);
|
|
205
|
+
if (si === -1 || ei === -1 || si > ei) return;
|
|
206
|
+
|
|
207
|
+
toWrap = containerChildren.slice(si, ei + 1);
|
|
208
|
+
parentId = lca ? lca.node.id.toString() : null;
|
|
209
|
+
insertIndex = si;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const zone = ZoneBlock.create(nabu, 'zone', data?.props ?? {}, parentId, insertIndex);
|
|
213
|
+
toWrap.forEach((block, i) => nabu.tree.move(block.node.id, zone.node.id, i));
|
|
214
|
+
|
|
215
|
+
nabu.commit().then(() => {
|
|
216
|
+
const fresh = nabu.blocks.get(startBlock.id);
|
|
217
|
+
if (fresh) nabu.selection.setCursor(fresh, savedOffset);
|
|
218
|
+
});
|
|
219
|
+
},
|
|
220
|
+
},
|
|
221
|
+
});
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import { Nabu, NabuNode } from '../nabu.svelte.js';
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* @typedef {NabuNode<{type: "zone", synopsis: LoroText, status: string, wordCountTarget: number | null, title: string}>} ZoneNode
|
|
6
|
+
*/
|
|
7
|
+
export class ZoneBlock extends MegaBlock {
|
|
8
|
+
/**
|
|
9
|
+
* @param {Nabu} nabu
|
|
10
|
+
* @param {string} type
|
|
11
|
+
* @param {Object} [props]
|
|
12
|
+
* @param {string | null} [parentId]
|
|
13
|
+
* @param {number | null} [index]
|
|
14
|
+
*/
|
|
15
|
+
static create(nabu: Nabu, type: string, props?: Object, parentId?: string | null, index?: number | null): ZoneBlock;
|
|
16
|
+
/** @param {Nabu} nabu @param {ZoneNode} node */
|
|
17
|
+
constructor(nabu: Nabu, node: ZoneNode);
|
|
18
|
+
synopsisContainer: LoroText;
|
|
19
|
+
/** @type {string} */
|
|
20
|
+
_status: string;
|
|
21
|
+
/** @type {number | null} */
|
|
22
|
+
_wordCountTarget: number | null;
|
|
23
|
+
/** @type {string} */
|
|
24
|
+
_title: string;
|
|
25
|
+
/** @type {string} */
|
|
26
|
+
synopsisText: string;
|
|
27
|
+
zones: Zones;
|
|
28
|
+
wordCount: number;
|
|
29
|
+
set status(value: string);
|
|
30
|
+
get status(): string;
|
|
31
|
+
set wordCountTarget(value: number | null);
|
|
32
|
+
get wordCountTarget(): number | null;
|
|
33
|
+
set title(value: string);
|
|
34
|
+
get title(): string;
|
|
35
|
+
/** Returns the LoroText container for direct CRDT mutation. */
|
|
36
|
+
get synopsis(): LoroText;
|
|
37
|
+
/**
|
|
38
|
+
* Replace the synopsis text entirely.
|
|
39
|
+
* @param {string} text
|
|
40
|
+
*/
|
|
41
|
+
setSynopsis(text: string): void;
|
|
42
|
+
get clipboardProps(): {
|
|
43
|
+
status: string;
|
|
44
|
+
wordCountTarget: number | null;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Called via ascend("onSplit") when a child block's Enter key fires.
|
|
48
|
+
* Creates a new paragraph inside this Zone, never outside it.
|
|
49
|
+
*
|
|
50
|
+
* @param {Event | null} event
|
|
51
|
+
* @param {{ from: import('../block.svelte.js').Block, offset: number, delta: import('loro-crdt').Delta<string>[] }} data
|
|
52
|
+
*/
|
|
53
|
+
onSplit(event: Event | null, data: {
|
|
54
|
+
from: import("../block.svelte.js").Block;
|
|
55
|
+
offset: number;
|
|
56
|
+
delta: import("loro-crdt").Delta<string>[];
|
|
57
|
+
}): {
|
|
58
|
+
block: import("../block.svelte.js").Block;
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
export class Zones {
|
|
62
|
+
/** @param {Nabu} nabu */
|
|
63
|
+
static get(nabu: Nabu): Zones;
|
|
64
|
+
/** @param {Nabu} nabu */
|
|
65
|
+
constructor(nabu: Nabu);
|
|
66
|
+
/**
|
|
67
|
+
* Flat list of all ZoneBlocks in document order (DFS traversal).
|
|
68
|
+
* @type {ZoneBlock[]}
|
|
69
|
+
*/
|
|
70
|
+
all: ZoneBlock[];
|
|
71
|
+
/**
|
|
72
|
+
* Total number of zones.
|
|
73
|
+
*/
|
|
74
|
+
count: number;
|
|
75
|
+
/**
|
|
76
|
+
* Hierarchical zone tree — zones nested inside other zones.
|
|
77
|
+
* @type {Array<{zone: ZoneBlock, children: any[]}>}
|
|
78
|
+
*/
|
|
79
|
+
tree: Array<{
|
|
80
|
+
zone: ZoneBlock;
|
|
81
|
+
children: any[];
|
|
82
|
+
}>;
|
|
83
|
+
/**
|
|
84
|
+
* The innermost zone containing the current cursor, or null if outside any zone.
|
|
85
|
+
* @type {ZoneBlock | null}
|
|
86
|
+
*/
|
|
87
|
+
current: ZoneBlock | null;
|
|
88
|
+
/**
|
|
89
|
+
* Index of the current zone in `all` (-1 if cursor is outside any zone).
|
|
90
|
+
*/
|
|
91
|
+
currentIndex: number;
|
|
92
|
+
/**
|
|
93
|
+
* Navigate to the next zone (moves cursor to its first text position).
|
|
94
|
+
*/
|
|
95
|
+
goToNext(): void;
|
|
96
|
+
/**
|
|
97
|
+
* Navigate to the previous zone.
|
|
98
|
+
*/
|
|
99
|
+
goToPrevious(): void;
|
|
100
|
+
#private;
|
|
101
|
+
}
|
|
102
|
+
export type ZoneNode = NabuNode<{
|
|
103
|
+
type: "zone";
|
|
104
|
+
synopsis: LoroText;
|
|
105
|
+
status: string;
|
|
106
|
+
wordCountTarget: number | null;
|
|
107
|
+
title: string;
|
|
108
|
+
}>;
|
|
109
|
+
import { MegaBlock } from '../megablock.svelte.js';
|
|
110
|
+
import { LoroText } from 'loro-crdt';
|
|
111
|
+
import type { Nabu } from '../nabu.svelte.js';
|
|
112
|
+
import type { NabuNode } from '../nabu.svelte.js';
|
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
import { untrack } from 'svelte';
|
|
2
|
+
import { MegaBlock } from '../megablock.svelte.js';
|
|
3
|
+
import { LoroText } from 'loro-crdt';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @import { Nabu, NabuNode } from '../nabu.svelte.js';
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @typedef {NabuNode<{type: "zone", synopsis: LoroText, status: string, wordCountTarget: number | null, title: string}>} ZoneNode
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export class ZoneBlock extends MegaBlock {
|
|
14
|
+
/** @param {Nabu} nabu @param {ZoneNode} node */
|
|
15
|
+
constructor(nabu, node) {
|
|
16
|
+
super(nabu, node);
|
|
17
|
+
|
|
18
|
+
const data = node.data;
|
|
19
|
+
|
|
20
|
+
// Synopsis LoroText — get existing or create on first construction
|
|
21
|
+
this.synopsisContainer = /** @type {LoroText} */ (
|
|
22
|
+
data.get('synopsis') ?? data.setContainer('synopsis', new LoroText())
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
// Initialize $state backing fields from Loro
|
|
26
|
+
this._status = data.get('status') ?? 'draft';
|
|
27
|
+
this._wordCountTarget = data.get('wordCountTarget') ?? null;
|
|
28
|
+
this._title = data.get('title') ?? '';
|
|
29
|
+
this.synopsisText = this.synopsisContainer.toString();
|
|
30
|
+
|
|
31
|
+
// Subscribe to map changes for reactive scalar fields
|
|
32
|
+
data.subscribe(() => {
|
|
33
|
+
this._status = data.get('status') ?? 'draft';
|
|
34
|
+
this._wordCountTarget = data.get('wordCountTarget') ?? null;
|
|
35
|
+
this._title = data.get('title') ?? '';
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// Subscribe to synopsis LoroText changes
|
|
39
|
+
this.synopsisContainer.subscribe(() => {
|
|
40
|
+
this.synopsisText = this.synopsisContainer.toString();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
this.serializers.set('markdown', () => {
|
|
44
|
+
const header = `<!-- zone: ${this._status} -->`;
|
|
45
|
+
const body = this.children
|
|
46
|
+
.map(child => child.serialize('markdown'))
|
|
47
|
+
.filter(Boolean)
|
|
48
|
+
.join('\n\n');
|
|
49
|
+
return [header, body].filter(Boolean).join('\n');
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
this.serializers.set('json', () => ({
|
|
53
|
+
id: this.id,
|
|
54
|
+
type: 'zone',
|
|
55
|
+
props: { status: this._status, wordCountTarget: this._wordCountTarget },
|
|
56
|
+
children: this.children.map(child => child.serialize('json')).filter(Boolean),
|
|
57
|
+
}));
|
|
58
|
+
|
|
59
|
+
this.serializers.set('application/x-nabu+json', () => ({
|
|
60
|
+
type: 'zone',
|
|
61
|
+
props: this.clipboardProps,
|
|
62
|
+
children: this.children
|
|
63
|
+
.map(child => child.serialize('application/x-nabu+json'))
|
|
64
|
+
.filter(Boolean),
|
|
65
|
+
}));
|
|
66
|
+
|
|
67
|
+
this.zones = Zones.get(nabu);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
// zones = $derived.by(() => {
|
|
74
|
+
// this.nabu.systems.has('zones');
|
|
75
|
+
// return untrack(() => Zones.get(this.nabu));
|
|
76
|
+
// });
|
|
77
|
+
|
|
78
|
+
// ── Reactive backing fields ─────────────────────────────────────────────
|
|
79
|
+
|
|
80
|
+
/** @type {string} */
|
|
81
|
+
_status = $state('draft');
|
|
82
|
+
|
|
83
|
+
/** @type {number | null} */
|
|
84
|
+
_wordCountTarget = $state(null);
|
|
85
|
+
|
|
86
|
+
/** @type {string} */
|
|
87
|
+
_title = $state('');
|
|
88
|
+
|
|
89
|
+
/** @type {string} */
|
|
90
|
+
synopsisText = $state('');
|
|
91
|
+
|
|
92
|
+
// ── Derived ─────────────────────────────────────────────────────────────
|
|
93
|
+
|
|
94
|
+
wordCount = $derived.by(() => {
|
|
95
|
+
let count = 0;
|
|
96
|
+
/** @param {import('../block.svelte.js').Block[]} blocks */
|
|
97
|
+
const traverse = (blocks) => {
|
|
98
|
+
for (const block of blocks) {
|
|
99
|
+
const tb = block.behaviors?.get('text');
|
|
100
|
+
if (tb && typeof tb.text === 'string') {
|
|
101
|
+
count += tb.text.trim().split(/\s+/).filter(Boolean).length;
|
|
102
|
+
}
|
|
103
|
+
// @ts-ignore — children exists on MegaBlock
|
|
104
|
+
if (block.children?.length) traverse(block.children);
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
traverse(this.children);
|
|
108
|
+
return count;
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
// ── Getters / setters ───────────────────────────────────────────────────
|
|
112
|
+
|
|
113
|
+
get status() { return this._status; }
|
|
114
|
+
set status(value) {
|
|
115
|
+
this._status = value;
|
|
116
|
+
this.node.data.set('status', value);
|
|
117
|
+
this.commit();
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
get wordCountTarget() { return this._wordCountTarget; }
|
|
121
|
+
set wordCountTarget(value) {
|
|
122
|
+
this._wordCountTarget = value ?? null;
|
|
123
|
+
this.node.data.set('wordCountTarget', value ?? null);
|
|
124
|
+
this.commit();
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
get title() { return this._title; }
|
|
128
|
+
set title(value) {
|
|
129
|
+
this._title = value ?? '';
|
|
130
|
+
/** @type {any} */ (this.node.data).set('title', value ?? '');
|
|
131
|
+
this.commit();
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Returns the LoroText container for direct CRDT mutation. */
|
|
135
|
+
get synopsis() { return this.synopsisContainer; }
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Replace the synopsis text entirely.
|
|
139
|
+
* @param {string} text
|
|
140
|
+
*/
|
|
141
|
+
setSynopsis(text) {
|
|
142
|
+
const len = this.synopsisContainer.length;
|
|
143
|
+
if (len > 0) this.synopsisContainer.delete(0, len);
|
|
144
|
+
if (text) this.synopsisContainer.insert(0, text);
|
|
145
|
+
this.commit();
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
get clipboardProps() {
|
|
149
|
+
return { status: this._status, wordCountTarget: this._wordCountTarget };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// ── Container split ─────────────────────────────────────────────────────
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Called via ascend("onSplit") when a child block's Enter key fires.
|
|
156
|
+
* Creates a new paragraph inside this Zone, never outside it.
|
|
157
|
+
*
|
|
158
|
+
* @param {Event | null} event
|
|
159
|
+
* @param {{ from: import('../block.svelte.js').Block, offset: number, delta: import('loro-crdt').Delta<string>[] }} data
|
|
160
|
+
*/
|
|
161
|
+
onSplit(event, data) {
|
|
162
|
+
const { from: sourceBlock, offset, delta } = data;
|
|
163
|
+
|
|
164
|
+
// Delete content from split point to end of source block
|
|
165
|
+
sourceBlock.delete({ from: offset, to: -1 });
|
|
166
|
+
|
|
167
|
+
const currentIndex = sourceBlock.node.index();
|
|
168
|
+
|
|
169
|
+
// Insert new paragraph inside THIS zone (not the zone's parent)
|
|
170
|
+
const newParagraph = this.nabu.insert(
|
|
171
|
+
'paragraph',
|
|
172
|
+
{ delta },
|
|
173
|
+
this.node.id.toString(),
|
|
174
|
+
currentIndex + 1
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
this.nabu.commit().then(() => this.nabu.selection.setCursor(newParagraph, 0));
|
|
178
|
+
return { block: newParagraph };
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// ── Static factory ──────────────────────────────────────────────────────
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* @param {Nabu} nabu
|
|
185
|
+
* @param {string} type
|
|
186
|
+
* @param {Object} [props]
|
|
187
|
+
* @param {string | null} [parentId]
|
|
188
|
+
* @param {number | null} [index]
|
|
189
|
+
*/
|
|
190
|
+
static create(nabu, type, props = {}, parentId = null, index = null) {
|
|
191
|
+
const node = nabu.tree.createNode(
|
|
192
|
+
parentId || undefined,
|
|
193
|
+
index != null ? index : undefined
|
|
194
|
+
);
|
|
195
|
+
|
|
196
|
+
node.data.set('type', 'zone');
|
|
197
|
+
node.data.set('status', props.status ?? 'draft');
|
|
198
|
+
node.data.set('wordCountTarget', props.wordCountTarget ?? null);
|
|
199
|
+
|
|
200
|
+
const synopsisContainer = node.data.setContainer('synopsis', new LoroText());
|
|
201
|
+
if (props.synopsis) synopsisContainer.insert(0, props.synopsis);
|
|
202
|
+
|
|
203
|
+
return new ZoneBlock(nabu, node);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export class Zones {
|
|
208
|
+
/** @type {Nabu} */
|
|
209
|
+
#nabu;
|
|
210
|
+
|
|
211
|
+
/** @param {Nabu} nabu */
|
|
212
|
+
constructor(nabu) {
|
|
213
|
+
this.#nabu = nabu;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** @param {Nabu} nabu */
|
|
217
|
+
static get(nabu) {
|
|
218
|
+
if (!nabu.systems.has('zones')) {
|
|
219
|
+
const zones = new Zones(nabu);
|
|
220
|
+
nabu.systems.set('zones', zones);
|
|
221
|
+
return zones;
|
|
222
|
+
}
|
|
223
|
+
return /** @type {Zones} */ (nabu.systems.get('zones'));
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Flat list of all ZoneBlocks in document order (DFS traversal).
|
|
228
|
+
* @type {ZoneBlock[]}
|
|
229
|
+
*/
|
|
230
|
+
all = $derived.by(() => {
|
|
231
|
+
/** @type {ZoneBlock[]} */
|
|
232
|
+
const result = [];
|
|
233
|
+
/** @param {import('../block.svelte.js').Block[]} blocks */
|
|
234
|
+
const traverse = (blocks) => {
|
|
235
|
+
for (const block of blocks) {
|
|
236
|
+
if (block.type === 'zone') result.push(/** @type {ZoneBlock} */ (block));
|
|
237
|
+
const children = /** @type {any} */ (block).children;
|
|
238
|
+
if (children?.length) traverse(children);
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
traverse(this.#nabu.children);
|
|
242
|
+
return result;
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Total number of zones.
|
|
247
|
+
*/
|
|
248
|
+
count = $derived(this.all.length);
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Hierarchical zone tree — zones nested inside other zones.
|
|
252
|
+
* @type {Array<{zone: ZoneBlock, children: any[]}>}
|
|
253
|
+
*/
|
|
254
|
+
tree = $derived.by(() => {
|
|
255
|
+
/** @param {import('../block.svelte.js').Block[]} blocks */
|
|
256
|
+
const buildTree = (blocks) => {
|
|
257
|
+
/** @type {Array<{zone: ZoneBlock, children: any[]}>} */
|
|
258
|
+
const result = [];
|
|
259
|
+
for (const block of blocks) {
|
|
260
|
+
const children = /** @type {any} */ (block).children ?? [];
|
|
261
|
+
if (block.type === 'zone') {
|
|
262
|
+
result.push({ zone: /** @type {ZoneBlock} */ (block), children: buildTree(children) });
|
|
263
|
+
} else if (children.length) {
|
|
264
|
+
result.push(...buildTree(children));
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
return result;
|
|
268
|
+
};
|
|
269
|
+
return buildTree(this.#nabu.children);
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* The innermost zone containing the current cursor, or null if outside any zone.
|
|
274
|
+
* @type {ZoneBlock | null}
|
|
275
|
+
*/
|
|
276
|
+
current = $derived.by(() => {
|
|
277
|
+
const anchor = this.#nabu.selection.anchorBlock;
|
|
278
|
+
if (!anchor) return null;
|
|
279
|
+
let node = anchor.parent;
|
|
280
|
+
while (node) {
|
|
281
|
+
if (node.type === 'zone') return /** @type {ZoneBlock} */ (node);
|
|
282
|
+
node = node.parent;
|
|
283
|
+
}
|
|
284
|
+
return null;
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Index of the current zone in `all` (-1 if cursor is outside any zone).
|
|
289
|
+
*/
|
|
290
|
+
currentIndex = $derived.by(() => {
|
|
291
|
+
const zone = this.current;
|
|
292
|
+
if (!zone) return -1;
|
|
293
|
+
return this.all.indexOf(zone);
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Navigate to the next zone (moves cursor to its first text position).
|
|
298
|
+
*/
|
|
299
|
+
goToNext() {
|
|
300
|
+
const idx = this.currentIndex;
|
|
301
|
+
const next = idx === -1
|
|
302
|
+
? this.all[0]
|
|
303
|
+
: this.all[idx + 1];
|
|
304
|
+
if (next) this.#focusZone(next);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Navigate to the previous zone.
|
|
309
|
+
*/
|
|
310
|
+
goToPrevious() {
|
|
311
|
+
const idx = this.currentIndex;
|
|
312
|
+
if (idx <= 0) return;
|
|
313
|
+
this.#focusZone(this.all[idx - 1]);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/** @param {ZoneBlock} zone */
|
|
317
|
+
#focusZone(zone) {
|
|
318
|
+
const firstChild = /** @type {any} */ (zone).children?.[0];
|
|
319
|
+
if (firstChild) this.#nabu.selection.setCursor(firstChild, 0);
|
|
320
|
+
}
|
|
321
|
+
}
|
package/dist/presets.js
CHANGED
|
@@ -6,6 +6,7 @@ import { DialogueExtension } from './blocks/dialogue/index.js';
|
|
|
6
6
|
import { RichTextExtension } from './behaviors/text/index.js';
|
|
7
7
|
import { PlainTextPasteExtension, NabuPasteExtension, HtmlPasteExtension, MarkdownPasteExtension } from './paste/index.js';
|
|
8
8
|
import { BlockExtension } from './blocks/block.extension.js';
|
|
9
|
+
import { ZoneExtension } from './blocks/zone/zone.extension.js';
|
|
9
10
|
|
|
10
11
|
// ---------------------------------------------------------------------------
|
|
11
12
|
// Presets — composable extension arrays
|
|
@@ -21,7 +22,7 @@ export const TextPreset = [ParagraphExtension, RichTextExtension, PlainTextPaste
|
|
|
21
22
|
export const DocumentPreset = [ParagraphExtension, HeadingExtension, RichTextExtension, PlainTextPasteExtension, HtmlPasteExtension, MarkdownPasteExtension, NabuPasteExtension, BlockExtension];
|
|
22
23
|
|
|
23
24
|
/** All built-in block types + rich text. */
|
|
24
|
-
export const FullPreset =
|
|
25
|
+
export const FullPreset = [ParagraphExtension, HeadingExtension, ListExtension, ListItemExtension, DialogueExtension, ZoneExtension, RichTextExtension, PlainTextPasteExtension, HtmlPasteExtension, MarkdownPasteExtension, NabuPasteExtension, BlockExtension];
|
|
25
26
|
|
|
26
27
|
// ---------------------------------------------------------------------------
|
|
27
28
|
// Factory — generic createEditor
|