@aionbuilders/nabu 0.1.1 → 0.1.3
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 +27 -0
- package/README.md +7 -1
- package/dist/behaviors/text/mark-edges.d.ts +39 -0
- package/dist/behaviors/text/mark-edges.js +98 -0
- package/dist/behaviors/text/text.behavior.svelte.d.ts +9 -1
- package/dist/behaviors/text/text.behavior.svelte.js +30 -7
- package/dist/blocks/callout/callout.svelte.js +2 -2
- package/dist/blocks/code/Code.svelte +29 -1
- package/dist/blocks/code/code.extension.js +20 -7
- package/dist/blocks/code/code.svelte.d.ts +4 -1
- package/dist/blocks/code/code.svelte.js +121 -24
- package/dist/blocks/code/indent.d.ts +61 -0
- package/dist/blocks/code/indent.js +91 -0
- package/dist/blocks/heading/heading.svelte.js +2 -2
- package/dist/blocks/list/hooks/onBeforeInput.hook.js +2 -1
- package/dist/blocks/list/list-item.svelte.d.ts +1 -1
- package/dist/blocks/list/list-item.svelte.js +2 -2
- package/dist/blocks/list/list.behavior.svelte.js +2 -2
- package/dist/blocks/list/to-list.js +2 -1
- package/dist/blocks/media/media-block.svelte.js +2 -2
- package/dist/blocks/nabu.svelte.d.ts +53 -0
- package/dist/blocks/nabu.svelte.js +101 -33
- package/dist/blocks/region/actions.js +2 -1
- package/dist/blocks/region/region.svelte.d.ts +1 -1
- package/dist/blocks/region/region.svelte.js +4 -3
- package/dist/blocks/selection.svelte.d.ts +2 -1
- package/dist/blocks/selection.svelte.js +29 -5
- package/dist/blocks/text-block.svelte.d.ts +1 -1
- package/dist/blocks/toggle/toggle.svelte.d.ts +1 -1
- package/dist/blocks/toggle/toggle.svelte.js +2 -2
- package/dist/files/files.svelte.js +1 -1
- package/dist/ui/slash-menu.svelte.js +2 -1
- package/dist/utils/delta.d.ts +27 -0
- package/dist/utils/delta.js +121 -0
- package/dist/utils/selection.svelte.d.ts +5 -0
- package/dist/utils/selection.svelte.js +15 -2
- package/dist/utils/tab.js +1 -0
- package/dist/vite.d.ts +8 -0
- package/dist/vite.js +15 -2
- package/package.json +1 -1
|
@@ -3,6 +3,7 @@ import { LoroText } from "loro-crdt";
|
|
|
3
3
|
import CodeComponent from "./Code.svelte";
|
|
4
4
|
import { tick } from "svelte";
|
|
5
5
|
import { escapeHtml } from "../../utils/html.js";
|
|
6
|
+
import { INDENT, leadingIndent, indentLines, outdentLines, lineStart } from "./indent.js";
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* @import { Nabu, NabuNode } from "../nabu.svelte.js";
|
|
@@ -21,6 +22,31 @@ function pbText(pb) {
|
|
|
21
22
|
return (pb.delta || []).map(op => typeof op.insert === 'string' ? op.insert : '').join('');
|
|
22
23
|
}
|
|
23
24
|
|
|
25
|
+
/** What a language name may hold: a word, plus what real ones use (`c++`, `c#`, `objective-c`, `vue.js`). */
|
|
26
|
+
export const LANGUAGE_CHARS = '[\\w#+.-]';
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The language a `<pre>` (or its `<code>`, or the wrapper of a highlighter) declares: `language-js` / `lang-js` classes (Markdown
|
|
30
|
+
* renderers, Prism, highlight.js, Shiki), GitHub's `highlight-source-js` wrapper, a `data-language` / `data-lang` attribute.
|
|
31
|
+
* @param {Element} pre
|
|
32
|
+
* @returns {string | null}
|
|
33
|
+
*/
|
|
34
|
+
function languageOf(pre) {
|
|
35
|
+
const code = pre.querySelector('code');
|
|
36
|
+
const own = new RegExp('(?:^|\\s)(?:language|lang)-(' + LANGUAGE_CHARS + '+)');
|
|
37
|
+
for (const el of [code, pre]) {
|
|
38
|
+
const m = el && own.exec(el.getAttribute('class') || '');
|
|
39
|
+
if (m) return m[1];
|
|
40
|
+
}
|
|
41
|
+
for (const el of [code, pre]) {
|
|
42
|
+
const attr = el?.getAttribute('data-language') || el?.getAttribute('data-lang');
|
|
43
|
+
if (attr && new RegExp('^' + LANGUAGE_CHARS + '+$').test(attr)) return attr;
|
|
44
|
+
}
|
|
45
|
+
// The wrapper of a highlighter: only its explicit names (a `lang-selector` class there would not be a language).
|
|
46
|
+
const wrapper = new RegExp('(?:^|\\s)(?:language-(' + LANGUAGE_CHARS + '+)|highlight-source-([\\w-]+))').exec(pre.parentElement?.getAttribute('class') || '');
|
|
47
|
+
return wrapper ? (wrapper[1] || wrapper[2]) : null;
|
|
48
|
+
}
|
|
49
|
+
|
|
24
50
|
/** The opening fence of a code block: indent, fence characters, info string. */
|
|
25
51
|
const FENCE_OPEN = /^( {0,3})(`{3,}|~{3,})(.*)$/;
|
|
26
52
|
|
|
@@ -67,9 +93,9 @@ export class CodeBlock extends TextBlock {
|
|
|
67
93
|
);
|
|
68
94
|
|
|
69
95
|
// Sync language from Loro data changes (mirrors Heading's level sync)
|
|
70
|
-
this.node.data.subscribe(() => {
|
|
96
|
+
this.nabu.track(this.node.data.subscribe(() => {
|
|
71
97
|
this.language = this.node.data.get('language') || null;
|
|
72
|
-
});
|
|
98
|
+
}));
|
|
73
99
|
}
|
|
74
100
|
|
|
75
101
|
// ── Capability flags ─────────────────────────────────────────────────────
|
|
@@ -100,7 +126,53 @@ export class CodeBlock extends TextBlock {
|
|
|
100
126
|
}
|
|
101
127
|
|
|
102
128
|
/**
|
|
103
|
-
*
|
|
129
|
+
* Put the selection back on model offsets once the edit is rendered (`backward`: the focus is the start).
|
|
130
|
+
* @param {number} from @param {number} to @param {boolean} [backward]
|
|
131
|
+
*/
|
|
132
|
+
#select(from, to, backward = false) {
|
|
133
|
+
tick().then(() => {
|
|
134
|
+
if (from === to) return this.nabu.selection.setCursor(this, from);
|
|
135
|
+
const a = this.getDOMPoint(backward ? to : from);
|
|
136
|
+
const f = this.getDOMPoint(backward ? from : to);
|
|
137
|
+
if (a && f) this.nabu.selection.setBaseAndExtent(a.node, a.offset, f.node, f.offset);
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Enter keeps the indentation of the line (auto-indent): the new line starts with the same spaces / tabs, up to the caret.
|
|
143
|
+
* Every other input is the text behavior's (Enter without indentation inserts a plain "\n").
|
|
144
|
+
* @param {InputEvent} event
|
|
145
|
+
*/
|
|
146
|
+
beforeinput(event) {
|
|
147
|
+
if (event.inputType === 'insertParagraph' || event.inputType === 'insertLineBreak') {
|
|
148
|
+
const sel = this.behavior.selection;
|
|
149
|
+
const indent = sel ? leadingIndent(this.text, sel.from) : '';
|
|
150
|
+
if (sel && indent) {
|
|
151
|
+
if (!sel.isCollapsed) this.behavior.delete({ index: sel.from, length: sel.to - sel.from });
|
|
152
|
+
this.behavior.insert(sel.from, '\n' + indent);
|
|
153
|
+
this.commit();
|
|
154
|
+
this.#select(sel.from + 1 + indent.length, sel.from + 1 + indent.length);
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return super.beforeinput(event);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Apply an indent plan (see indent.js) as one commit and keep the selection on the same characters.
|
|
163
|
+
* @param {import('./indent.js').IndentPlan} plan @param {boolean} backward
|
|
164
|
+
*/
|
|
165
|
+
#apply(plan, backward) {
|
|
166
|
+
for (const { index, remove, insert } of plan.edits) {
|
|
167
|
+
if (remove) this.behavior.delete({ index, length: remove });
|
|
168
|
+
if (insert) this.behavior.insert(index, insert);
|
|
169
|
+
}
|
|
170
|
+
this.commit();
|
|
171
|
+
this.#select(plan.from, plan.to, backward);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Handle special keydown events: Tab, Shift+Tab (except the one that follows an Escape), Ctrl+A, ArrowUp / ArrowDown at the edges of the block.
|
|
104
176
|
* Returns true if handled (engine should preventDefault).
|
|
105
177
|
* @param {KeyboardEvent} event
|
|
106
178
|
* @returns {boolean}
|
|
@@ -108,35 +180,55 @@ export class CodeBlock extends TextBlock {
|
|
|
108
180
|
keydown(event) {
|
|
109
181
|
const sel = this.behavior.selection;
|
|
110
182
|
if (!sel) return false;
|
|
183
|
+
const plain = !event.ctrlKey && !event.metaKey && !event.altKey && !event.shiftKey;
|
|
184
|
+
const backward = sel.direction === 'backward';
|
|
111
185
|
|
|
112
|
-
// Tab
|
|
186
|
+
// The way out (WCAG 2.1.2): Tab / Shift+Tab right after an Escape nobody consumed is not the code's. Forward, the browser moves the
|
|
187
|
+
// focus out of the editor. Backward, it would go before the editor and never reach the language select, so the focus goes there.
|
|
188
|
+
if (event.key === 'Tab' && this.nabu.tabWayOut) {
|
|
189
|
+
if (!event.shiftKey) return false;
|
|
190
|
+
const select = /** @type {HTMLSelectElement | null | undefined} */ (this.element?.parentElement?.querySelector('select.nabu-code-lang'));
|
|
191
|
+
if (!select || select.disabled) return false;
|
|
192
|
+
select.focus();
|
|
193
|
+
return true;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Tab without shift: over several lines, indent every line; else insert 2 spaces (replacing a selection within one line)
|
|
113
197
|
if (event.key === 'Tab' && !event.shiftKey) {
|
|
198
|
+
const plan = indentLines(this.text, sel.from, sel.to);
|
|
199
|
+
if (plan) { this.#apply(plan, backward); return true; }
|
|
114
200
|
if (!sel.isCollapsed) this.behavior.delete({ index: sel.from, length: sel.to - sel.from });
|
|
115
|
-
this.behavior.insert(sel.from,
|
|
201
|
+
this.behavior.insert(sel.from, INDENT);
|
|
116
202
|
this.commit();
|
|
117
|
-
|
|
203
|
+
this.#select(sel.from + INDENT.length, sel.from + INDENT.length);
|
|
118
204
|
return true;
|
|
119
205
|
}
|
|
120
206
|
|
|
121
|
-
// Shift+Tab
|
|
207
|
+
// Shift+Tab: outdent every line the selection touches (the caret's own line when collapsed)
|
|
122
208
|
if (event.key === 'Tab' && event.shiftKey) {
|
|
123
|
-
const
|
|
124
|
-
|
|
125
|
-
while (n < 2 && this.text[ls + n] === ' ') n++;
|
|
126
|
-
if (n > 0) {
|
|
127
|
-
this.behavior.delete({ index: ls, length: n });
|
|
128
|
-
this.commit();
|
|
129
|
-
tick().then(() => this.nabu.selection.setCursor(this, Math.max(ls, sel.from - n)));
|
|
130
|
-
}
|
|
209
|
+
const plan = outdentLines(this.text, sel.from, sel.to);
|
|
210
|
+
if (plan) this.#apply(plan, backward);
|
|
131
211
|
return true;
|
|
132
212
|
}
|
|
133
213
|
|
|
134
|
-
//
|
|
135
|
-
if (event.
|
|
136
|
-
const
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
214
|
+
// Ctrl/Cmd+A: the code first, the whole document with the next press (the browser's own Select All)
|
|
215
|
+
if ((event.ctrlKey || event.metaKey) && !event.shiftKey && !event.altKey && typeof event.key === 'string' && event.key.toLowerCase() === 'a') {
|
|
216
|
+
const length = this.text.length;
|
|
217
|
+
if (length && !(sel.from === 0 && sel.to === length)) {
|
|
218
|
+
this.nabu.selection.selectBlockText(this);
|
|
219
|
+
return true;
|
|
220
|
+
}
|
|
221
|
+
return false;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// ArrowDown from the last line / ArrowUp from the first one, with no text block beyond: a paragraph is created there, so
|
|
225
|
+
// that a code block at the end (start) of the document, or before a divider, can be left with the keyboard.
|
|
226
|
+
if ((event.key === 'ArrowDown' || event.key === 'ArrowUp') && plain && sel.isCollapsed) {
|
|
227
|
+
const down = event.key === 'ArrowDown';
|
|
228
|
+
const onEdge = down ? this.text.indexOf('\n', sel.from) === -1 : lineStart(this.text, sel.from) === 0;
|
|
229
|
+
const hasNeighbour = !!(down ? this.findForward(b => b.behaviors?.has('text')) : this.findBackward(b => b.behaviors?.has('text')));
|
|
230
|
+
if (onEdge && !hasNeighbour) {
|
|
231
|
+
const p = this.nabu.insert('paragraph', {}, this.node.parent()?.id.toString() || null, (this.node.index() ?? 0) + (down ? 1 : 0));
|
|
140
232
|
this.nabu.commit().then(() => {
|
|
141
233
|
const fresh = this.nabu.blocks.get(p.id);
|
|
142
234
|
if (fresh) this.nabu.selection.setCursor(fresh, 0);
|
|
@@ -158,7 +250,9 @@ export class CodeBlock extends TextBlock {
|
|
|
158
250
|
|
|
159
251
|
/** @param {import('../../utils/extensions.js').PasteBlock} pb */
|
|
160
252
|
static toHtml(pb) {
|
|
161
|
-
|
|
253
|
+
const language = String(pb.props?.language || '');
|
|
254
|
+
const cls = new RegExp('^' + LANGUAGE_CHARS + '+$').test(language) ? ` class="language-${language}"` : '';
|
|
255
|
+
return `<pre><code${cls}>${escapeHtml(pbText(pb))}</code></pre>`;
|
|
162
256
|
}
|
|
163
257
|
|
|
164
258
|
/**
|
|
@@ -222,8 +316,11 @@ export class CodeBlock extends TextBlock {
|
|
|
222
316
|
* @returns {import('../../utils/extensions.js').PasteBlock | null}
|
|
223
317
|
*/
|
|
224
318
|
static fromHTML(el) {
|
|
225
|
-
|
|
226
|
-
const
|
|
319
|
+
// A <br> in a <pre> is a line break (old sites, some editors); textContent alone would glue the lines.
|
|
320
|
+
const copy = /** @type {Element} */ (el.cloneNode(true));
|
|
321
|
+
for (const br of copy.querySelectorAll('br')) br.replaceWith('\n');
|
|
322
|
+
const text = copy.textContent || '';
|
|
323
|
+
const language = languageOf(el);
|
|
227
324
|
return {
|
|
228
325
|
type: 'code',
|
|
229
326
|
props: { language },
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The offset where the line holding `pos` starts.
|
|
3
|
+
* @param {string} text @param {number} pos
|
|
4
|
+
*/
|
|
5
|
+
export function lineStart(text: string, pos: number): number;
|
|
6
|
+
/**
|
|
7
|
+
* The whitespace (spaces and tabs) at the start of the line, up to the caret: what a new line typed with Enter starts with.
|
|
8
|
+
* @param {string} text @param {number} pos
|
|
9
|
+
*/
|
|
10
|
+
export function leadingIndent(text: string, pos: number): string;
|
|
11
|
+
/**
|
|
12
|
+
* The start offsets of the lines a selection touches. A selection that ends at the very start of a line does not touch that
|
|
13
|
+
* line (as in every code editor); a collapsed caret touches its own line.
|
|
14
|
+
* @param {string} text @param {number} from @param {number} to
|
|
15
|
+
* @returns {number[]}
|
|
16
|
+
*/
|
|
17
|
+
export function touchedLines(text: string, from: number, to: number): number[];
|
|
18
|
+
/**
|
|
19
|
+
* @typedef {Object} IndentPlan
|
|
20
|
+
* @property {{ index: number, remove: number, insert: string }[]} edits from the END of the text to its start (apply in order)
|
|
21
|
+
* @property {number} from the selection start after the edits
|
|
22
|
+
* @property {number} to the selection end after the edits
|
|
23
|
+
*/
|
|
24
|
+
/**
|
|
25
|
+
* Tab over a selection that spans several lines: one indentation level is inserted at the start of every non-empty line.
|
|
26
|
+
* `null` when the selection is within one line (Tab then replaces it by an indentation, like VS Code).
|
|
27
|
+
* @param {string} text @param {number} from @param {number} to
|
|
28
|
+
* @returns {IndentPlan | null}
|
|
29
|
+
*/
|
|
30
|
+
export function indentLines(text: string, from: number, to: number): IndentPlan | null;
|
|
31
|
+
/**
|
|
32
|
+
* Shift+Tab: one indentation level (a tab character, or up to two spaces) is removed from the start of every line the
|
|
33
|
+
* selection touches (the caret's own line when it is collapsed). `null` when no line has anything to remove.
|
|
34
|
+
* @param {string} text @param {number} from @param {number} to
|
|
35
|
+
* @returns {IndentPlan | null}
|
|
36
|
+
*/
|
|
37
|
+
export function outdentLines(text: string, from: number, to: number): IndentPlan | null;
|
|
38
|
+
/**
|
|
39
|
+
* Pure text helpers of the code block (no DOM, no Loro): where a line starts, what indentation it has, which lines a Tab /
|
|
40
|
+
* Shift+Tab touches and the edits that indent / outdent them. `CodeBlock` applies the edits to its Loro text.
|
|
41
|
+
*/
|
|
42
|
+
/** One level of indentation: two spaces (never a tab character: the code is not re-indented by the host's tab width). */
|
|
43
|
+
export const INDENT: " ";
|
|
44
|
+
export type IndentPlan = {
|
|
45
|
+
/**
|
|
46
|
+
* from the END of the text to its start (apply in order)
|
|
47
|
+
*/
|
|
48
|
+
edits: {
|
|
49
|
+
index: number;
|
|
50
|
+
remove: number;
|
|
51
|
+
insert: string;
|
|
52
|
+
}[];
|
|
53
|
+
/**
|
|
54
|
+
* the selection start after the edits
|
|
55
|
+
*/
|
|
56
|
+
from: number;
|
|
57
|
+
/**
|
|
58
|
+
* the selection end after the edits
|
|
59
|
+
*/
|
|
60
|
+
to: number;
|
|
61
|
+
};
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure text helpers of the code block (no DOM, no Loro): where a line starts, what indentation it has, which lines a Tab /
|
|
3
|
+
* Shift+Tab touches and the edits that indent / outdent them. `CodeBlock` applies the edits to its Loro text.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/** One level of indentation: two spaces (never a tab character: the code is not re-indented by the host's tab width). */
|
|
7
|
+
export const INDENT = ' ';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The offset where the line holding `pos` starts.
|
|
11
|
+
* @param {string} text @param {number} pos
|
|
12
|
+
*/
|
|
13
|
+
export function lineStart(text, pos) {
|
|
14
|
+
return pos <= 0 ? 0 : text.lastIndexOf('\n', pos - 1) + 1;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The whitespace (spaces and tabs) at the start of the line, up to the caret: what a new line typed with Enter starts with.
|
|
19
|
+
* @param {string} text @param {number} pos
|
|
20
|
+
*/
|
|
21
|
+
export function leadingIndent(text, pos) {
|
|
22
|
+
const start = lineStart(text, pos);
|
|
23
|
+
return /^[ \t]*/.exec(text.slice(start, pos))?.[0] ?? '';
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The start offsets of the lines a selection touches. A selection that ends at the very start of a line does not touch that
|
|
28
|
+
* line (as in every code editor); a collapsed caret touches its own line.
|
|
29
|
+
* @param {string} text @param {number} from @param {number} to
|
|
30
|
+
* @returns {number[]}
|
|
31
|
+
*/
|
|
32
|
+
export function touchedLines(text, from, to) {
|
|
33
|
+
const end = to > from && text[to - 1] === '\n' ? to - 1 : to;
|
|
34
|
+
/** @type {number[]} */
|
|
35
|
+
const starts = [];
|
|
36
|
+
let s = lineStart(text, from);
|
|
37
|
+
for (;;) {
|
|
38
|
+
starts.push(s);
|
|
39
|
+
const next = text.indexOf('\n', s);
|
|
40
|
+
if (next === -1 || next >= end) break;
|
|
41
|
+
s = next + 1;
|
|
42
|
+
}
|
|
43
|
+
return starts;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @typedef {Object} IndentPlan
|
|
48
|
+
* @property {{ index: number, remove: number, insert: string }[]} edits from the END of the text to its start (apply in order)
|
|
49
|
+
* @property {number} from the selection start after the edits
|
|
50
|
+
* @property {number} to the selection end after the edits
|
|
51
|
+
*/
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Tab over a selection that spans several lines: one indentation level is inserted at the start of every non-empty line.
|
|
55
|
+
* `null` when the selection is within one line (Tab then replaces it by an indentation, like VS Code).
|
|
56
|
+
* @param {string} text @param {number} from @param {number} to
|
|
57
|
+
* @returns {IndentPlan | null}
|
|
58
|
+
*/
|
|
59
|
+
export function indentLines(text, from, to) {
|
|
60
|
+
const lines = touchedLines(text, from, to);
|
|
61
|
+
if (lines.length < 2) return null;
|
|
62
|
+
const targets = lines.filter((s) => s < text.length && text[s] !== '\n');
|
|
63
|
+
let nextFrom = from;
|
|
64
|
+
let nextTo = to;
|
|
65
|
+
for (const s of targets) {
|
|
66
|
+
if (from > s) nextFrom += INDENT.length;
|
|
67
|
+
if (to > s) nextTo += INDENT.length;
|
|
68
|
+
}
|
|
69
|
+
return { edits: targets.reverse().map((index) => ({ index, remove: 0, insert: INDENT })), from: nextFrom, to: nextTo };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Shift+Tab: one indentation level (a tab character, or up to two spaces) is removed from the start of every line the
|
|
74
|
+
* selection touches (the caret's own line when it is collapsed). `null` when no line has anything to remove.
|
|
75
|
+
* @param {string} text @param {number} from @param {number} to
|
|
76
|
+
* @returns {IndentPlan | null}
|
|
77
|
+
*/
|
|
78
|
+
export function outdentLines(text, from, to) {
|
|
79
|
+
/** @type {{ index: number, remove: number }[]} */
|
|
80
|
+
const cuts = [];
|
|
81
|
+
for (const s of touchedLines(text, from, to)) {
|
|
82
|
+
let n = 0;
|
|
83
|
+
if (text[s] === '\t') n = 1;
|
|
84
|
+
else while (n < INDENT.length && text[s + n] === ' ') n++;
|
|
85
|
+
if (n) cuts.push({ index: s, remove: n });
|
|
86
|
+
}
|
|
87
|
+
if (!cuts.length) return null;
|
|
88
|
+
/** @param {number} p */
|
|
89
|
+
const map = (p) => cuts.reduce((q, c) => q - Math.min(Math.max(p - c.index, 0), c.remove), p);
|
|
90
|
+
return { edits: [...cuts].reverse().map((c) => ({ ...c, insert: '' })), from: map(from), to: map(to) };
|
|
91
|
+
}
|
|
@@ -49,9 +49,9 @@ export class Heading extends TextBlock {
|
|
|
49
49
|
this.serializers.set('application/x-nabu+json', (ctx) => this.behavior.toClipboardBlock({ ...ctx, props: { level: this.level } }));
|
|
50
50
|
|
|
51
51
|
// Synchronisation du niveau
|
|
52
|
-
this.node.data.subscribe(() => {
|
|
52
|
+
this.nabu.track(this.node.data.subscribe(() => {
|
|
53
53
|
this.level = this.node.data.get("level") || 1;
|
|
54
|
-
});
|
|
54
|
+
}));
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
/** @type {number} */
|
|
@@ -3,6 +3,7 @@ import { TextBehavior } from "../../../behaviors/text";
|
|
|
3
3
|
import { List } from "../list.svelte.js";
|
|
4
4
|
import { ListItem } from "../list-item.svelte.js";
|
|
5
5
|
import { stepBack } from "../step-back.js";
|
|
6
|
+
import { readDelta } from "../../../utils/delta.js";
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* @import { Nabu, Block } from "../..";
|
|
@@ -77,7 +78,7 @@ export const onBeforeInput = (nabu, event, block) => {
|
|
|
77
78
|
b.delete({ index: 0, length: prefixLength + 1 });
|
|
78
79
|
|
|
79
80
|
// Capture remaining text delta (everything after the prefix)
|
|
80
|
-
const delta = b.container
|
|
81
|
+
const delta = readDelta(b.container);
|
|
81
82
|
|
|
82
83
|
// Get current tree position from live Loro state (pre-commit)
|
|
83
84
|
const parentNode = block.node.parent();
|
|
@@ -60,9 +60,9 @@ export class ListItem extends MegaBlock {
|
|
|
60
60
|
// Initialize checked from Loro metadata
|
|
61
61
|
this.checked = !!node.data.get('checked');
|
|
62
62
|
// Subscribe to data changes to keep checked in sync
|
|
63
|
-
this.node.data.subscribe(() => {
|
|
63
|
+
this.nabu.track(this.node.data.subscribe(() => {
|
|
64
64
|
this.checked = !!this.node.data.get('checked');
|
|
65
|
-
});
|
|
65
|
+
}));
|
|
66
66
|
|
|
67
67
|
// One item (and its sublists), indented under the items that hold it. A whole list is rendered by the List.
|
|
68
68
|
this.serializers.set('markdown', () => {
|
|
@@ -17,10 +17,10 @@ export class ListBehavior {
|
|
|
17
17
|
this.node = /** @type {ListNode} */ (block.node);
|
|
18
18
|
this.listType = /** @type {"bullet" | "ordered" | "todo"} */ (this.node.data.get("listType") || "bullet");
|
|
19
19
|
this.start = /** @type {any} */ (this.node.data.get("start")) ?? null;
|
|
20
|
-
this.node.data.subscribe(() => {
|
|
20
|
+
this.nabu.track(this.node.data.subscribe(() => {
|
|
21
21
|
this.listType = /** @type {"bullet" | "ordered" | "todo"} */ (this.node.data.get("listType") || "bullet");
|
|
22
22
|
this.start = /** @type {any} */ (this.node.data.get("start")) ?? null;
|
|
23
|
-
});
|
|
23
|
+
}));
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
/** Raw `start` number from the node (null = unset); List.start normalizes it. @type {number | null} */
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
import { List } from "./list.svelte.js";
|
|
11
11
|
import { ListItem } from "./list-item.svelte.js";
|
|
12
12
|
import { convertItemToListType } from "./convert.js";
|
|
13
|
+
import { readDelta } from "../../utils/delta.js";
|
|
13
14
|
|
|
14
15
|
/**
|
|
15
16
|
* @param {Nabu} nabu
|
|
@@ -30,7 +31,7 @@ export async function convertToList(nabu, listType) {
|
|
|
30
31
|
}
|
|
31
32
|
|
|
32
33
|
const beh = block.behaviors?.get("text");
|
|
33
|
-
const delta = beh ? beh.container
|
|
34
|
+
const delta = beh ? readDelta(beh.container) : [];
|
|
34
35
|
const parentNode = block.node.parent();
|
|
35
36
|
const parentId = parentNode?.id.toString() ?? null;
|
|
36
37
|
const index = block.node.index();
|
|
@@ -29,7 +29,7 @@ export class MediaBlock extends Block {
|
|
|
29
29
|
this.status = data.get('status') || 'ready';
|
|
30
30
|
this.#reason = normalizeReason(data.get('reason'));
|
|
31
31
|
this.name = data.get('name') || '';
|
|
32
|
-
data.subscribe(() => {
|
|
32
|
+
this.nabu.track(data.subscribe(() => {
|
|
33
33
|
this.ref = data.get('ref') || '';
|
|
34
34
|
this.alt = data.get('alt') || '';
|
|
35
35
|
this.status = data.get('status') || 'ready';
|
|
@@ -41,7 +41,7 @@ export class MediaBlock extends Block {
|
|
|
41
41
|
if (this.previewUrl.startsWith('blob:')) URL.revokeObjectURL(this.previewUrl);
|
|
42
42
|
this.previewUrl = null;
|
|
43
43
|
}
|
|
44
|
-
});
|
|
44
|
+
}));
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
/** @type {string} */ ref = $state('');
|
|
@@ -66,6 +66,26 @@ export class Nabu {
|
|
|
66
66
|
* @public
|
|
67
67
|
*/
|
|
68
68
|
public static CONTINUE: symbol;
|
|
69
|
+
/**
|
|
70
|
+
* The `onPush` / `onPop` of the undo manager. Built HERE, out of the constructor, so that they close over `holder` and nothing
|
|
71
|
+
* else: a closure made in the constructor would keep the constructor's scope, and `this` with it, alive.
|
|
72
|
+
* @param {{ owner: Nabu | null }} holder
|
|
73
|
+
*/
|
|
74
|
+
static #undoCallbacks(holder: {
|
|
75
|
+
owner: Nabu | null;
|
|
76
|
+
}): {
|
|
77
|
+
/** @param {boolean} isUndo @param {{ end?: any } | undefined} range */
|
|
78
|
+
onPush: (isUndo: boolean, range: {
|
|
79
|
+
end?: any;
|
|
80
|
+
} | undefined) => {
|
|
81
|
+
value: any;
|
|
82
|
+
cursors: never[];
|
|
83
|
+
};
|
|
84
|
+
/** @param {boolean} isUndo @param {any} storedValue @param {{ end?: any } | undefined} range */
|
|
85
|
+
onPop: (isUndo: boolean, storedValue: any, range: {
|
|
86
|
+
end?: any;
|
|
87
|
+
} | undefined) => void;
|
|
88
|
+
};
|
|
69
89
|
/** @param {NabuInit} init */
|
|
70
90
|
constructor(init?: NabuInit);
|
|
71
91
|
/**
|
|
@@ -244,6 +264,13 @@ export class Nabu {
|
|
|
244
264
|
public slashCommands: import("../ui/slash-menu.svelte.js").SlashCommand[];
|
|
245
265
|
/** What the editor root exposes as `aria-description`: the hint, only while the mode keeps the focus in. */
|
|
246
266
|
get tabDescription(): string | undefined;
|
|
267
|
+
/**
|
|
268
|
+
* True while a keydown is being handled and it is the Tab / Shift+Tab that follows an Escape nobody consumed: the way out is open
|
|
269
|
+
* for it. A block that gives Tab a meaning of its own (the code block's indentation) lets this one go to the browser (WCAG 2.1.2).
|
|
270
|
+
* Plain property, not state: only meaningful during `handleKeydown`.
|
|
271
|
+
* @type {boolean}
|
|
272
|
+
*/
|
|
273
|
+
tabWayOut: boolean;
|
|
247
274
|
/** Close the Escape-then-Tab way out (any other key, a click, the focus leaving). */
|
|
248
275
|
rearmTab(): void;
|
|
249
276
|
/**
|
|
@@ -684,6 +711,32 @@ export class Nabu {
|
|
|
684
711
|
* @unstable
|
|
685
712
|
*/
|
|
686
713
|
public get internals(): NabuInternals;
|
|
714
|
+
/**
|
|
715
|
+
* Hand over a Loro subscription (or any cleanup) that lives as long as the engine: `destroy()` calls it, once. A block or an
|
|
716
|
+
* extension passes its own, `nabu.track(node.data.subscribe(...))`: Loro keeps the callback (and what it closes over, the engine
|
|
717
|
+
* with it) until it is unsubscribed, so one that is not tracked keeps the engine in memory after `destroy()`.
|
|
718
|
+
* @template {() => void} T
|
|
719
|
+
* @param {T} unsubscribe
|
|
720
|
+
* @returns {T} what it was given
|
|
721
|
+
* @public
|
|
722
|
+
*/
|
|
723
|
+
public track<T extends () => void>(unsubscribe: T): T;
|
|
724
|
+
/**
|
|
725
|
+
* Release the engine for good, for a host that is done with it: the memory, not only the page. Unmounting `<NabuEditor>`
|
|
726
|
+
* releases the page listeners but not the memory (the engine can be mounted again); `destroy()` releases the memory: it
|
|
727
|
+
* unsubscribes the engine and its blocks from the document (see {@link Nabu#track}), stops the subsystems, closes `persistence`
|
|
728
|
+
* if the engine is persisted, releases the files (upload retries) and stops the selection tracking, so that the engine can be
|
|
729
|
+
* garbage collected. Idempotent. Call it after `unmount()` or instead of it (e.g. `onDestroy(() => engine.destroy())`).
|
|
730
|
+
*
|
|
731
|
+
* Changes that are not saved yet are DROPPED, not written: `persistence.dispose()` cancels the pending autosave and removes the
|
|
732
|
+
* `pagehide` flush. Await `saveNow()` first when they matter.
|
|
733
|
+
*
|
|
734
|
+
* The document stays in memory and readable as it was (`serialize()`, `doc`), but the engine no longer follows changes: no more
|
|
735
|
+
* edits. A destroyed engine must not be mounted again.
|
|
736
|
+
* @returns {void}
|
|
737
|
+
* @public
|
|
738
|
+
*/
|
|
739
|
+
public destroy(): void;
|
|
687
740
|
/**
|
|
688
741
|
* Log a diagnostic to the console when `debugging` is on. Silent otherwise.
|
|
689
742
|
* @param {...any} args
|