@aionbuilders/nabu 0.1.0 → 0.1.2
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 +44 -23
- package/README.md +6 -0
- 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 +23 -26
- package/dist/behaviors/text/text.behavior.svelte.js +46 -61
- 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 +119 -22
- package/dist/blocks/code/indent.d.ts +61 -0
- package/dist/blocks/code/indent.js +91 -0
- package/dist/blocks/list/hooks/onBeforeInput.hook.js +2 -1
- package/dist/blocks/list/list-item.svelte.d.ts +2 -2
- package/dist/blocks/list/to-list.js +2 -1
- package/dist/blocks/nabu.svelte.d.ts +7 -0
- package/dist/blocks/nabu.svelte.js +26 -12
- package/dist/blocks/region/actions.js +2 -1
- package/dist/blocks/region/region.svelte.d.ts +2 -2
- package/dist/blocks/region/region.svelte.js +3 -2
- package/dist/blocks/text-block.svelte.d.ts +2 -2
- package/dist/blocks/toggle/toggle.svelte.d.ts +2 -2
- package/dist/ui/slash-menu.svelte.js +2 -1
- package/dist/utils/composition.d.ts +28 -0
- package/dist/utils/composition.js +51 -0
- package/dist/utils/delta.d.ts +27 -0
- package/dist/utils/delta.js +121 -0
- package/dist/utils/selection.svelte.d.ts +1 -1
- package/dist/utils/tab.js +1 -0
- package/dist/utils/words.d.ts +35 -0
- package/dist/utils/words.js +85 -0
- package/package.json +3 -1
|
@@ -34,13 +34,13 @@ export class TextBlock extends Block {
|
|
|
34
34
|
/** The plain text of the block (reactive). @public */
|
|
35
35
|
public get text(): string;
|
|
36
36
|
/** The text with its marks, as a Loro delta (reactive). @public */
|
|
37
|
-
public get delta(): import("
|
|
37
|
+
public get delta(): import("../utils/delta.js").DeltaOp[];
|
|
38
38
|
/** The selection inside this block, `{ from, to, direction }` in text offsets, or `null` (reactive). @public */
|
|
39
39
|
public selection: {
|
|
40
40
|
from: number;
|
|
41
41
|
to: number;
|
|
42
42
|
isCollapsed: boolean;
|
|
43
|
-
direction: "
|
|
43
|
+
direction: "backward" | "forward" | "none";
|
|
44
44
|
} | null;
|
|
45
45
|
/**
|
|
46
46
|
* @param {Block} block
|
|
@@ -84,12 +84,12 @@ export class ToggleBlock extends MegaBlock {
|
|
|
84
84
|
block: import("./toggle.svelte.js").ToggleBlock;
|
|
85
85
|
}, {}, "">;
|
|
86
86
|
get text(): string;
|
|
87
|
-
get delta(): import("
|
|
87
|
+
get delta(): import("../../utils/delta.js").DeltaOp[];
|
|
88
88
|
selection: {
|
|
89
89
|
from: number;
|
|
90
90
|
to: number;
|
|
91
91
|
isCollapsed: boolean;
|
|
92
|
-
direction: "
|
|
92
|
+
direction: "backward" | "forward" | "none";
|
|
93
93
|
} | null;
|
|
94
94
|
/**
|
|
95
95
|
* Set the collapsed state and persist to Loro (in a read-only editor: only what the reader sees, see {@link ToggleBlock#collapsed}).
|
|
@@ -80,7 +80,8 @@ export class SlashMenuController {
|
|
|
80
80
|
match = $derived.by(() => {
|
|
81
81
|
const sel = this.#engine.selection;
|
|
82
82
|
const block = sel?.anchorBlock;
|
|
83
|
-
|
|
83
|
+
// A verbatim buffer (the code block) has no commands: `/` is text there, and Enter must stay a newline.
|
|
84
|
+
if (!block || block.pastesAsPlainText) return null;
|
|
84
85
|
const beh = block.behaviors?.get('text');
|
|
85
86
|
const bsel = beh?.selection;
|
|
86
87
|
if (!beh || !bsel || !bsel.isCollapsed) return null;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Chromium workaround for IME compositions that start after an EMPTY block (issue #6).
|
|
3
|
+
*
|
|
4
|
+
* An empty block renders a lone "\n" text node (one line, see RichText.svelte). Chromium locates the running composition by
|
|
5
|
+
* PLAIN-TEXT offsets from the editable root, and the two directions of that mapping disagree on such a node: every lone "\n"
|
|
6
|
+
* block BEFORE the composition drifts its start back by one, so the composed text lands in the wrong (previous) block and the
|
|
7
|
+
* next `compositionupdate` deletes the blocks in between — the commit never lands. A `<br>` in the same place maps cleanly.
|
|
8
|
+
*
|
|
9
|
+
* So for the duration of a composition the lone "\n" nodes before the composing block are swapped for a `<br>` (same single
|
|
10
|
+
* line), and put back at its end, before the model catches up. Nothing in the model or the Svelte-owned nodes changes.
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Swap, in the document before `element`, every empty block's lone "\n" text node for a `<br>`.
|
|
14
|
+
* @param {Element | null | undefined} element the block holding the composition
|
|
15
|
+
* @returns {Array<{ br: Element, text: Text }>} what {@link restoreEmptyBlocks} puts back
|
|
16
|
+
*/
|
|
17
|
+
export function swapEmptyBlocks(element: Element | null | undefined): Array<{
|
|
18
|
+
br: Element;
|
|
19
|
+
text: Text;
|
|
20
|
+
}>;
|
|
21
|
+
/**
|
|
22
|
+
* Put the "\n" text nodes back (Svelte keeps references to them). A `<br>` the browser or Svelte already removed is skipped.
|
|
23
|
+
* @param {Array<{ br: Element, text: Text }>} swapped
|
|
24
|
+
*/
|
|
25
|
+
export function restoreEmptyBlocks(swapped: Array<{
|
|
26
|
+
br: Element;
|
|
27
|
+
text: Text;
|
|
28
|
+
}>): void;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Chromium workaround for IME compositions that start after an EMPTY block (issue #6).
|
|
3
|
+
*
|
|
4
|
+
* An empty block renders a lone "\n" text node (one line, see RichText.svelte). Chromium locates the running composition by
|
|
5
|
+
* PLAIN-TEXT offsets from the editable root, and the two directions of that mapping disagree on such a node: every lone "\n"
|
|
6
|
+
* block BEFORE the composition drifts its start back by one, so the composed text lands in the wrong (previous) block and the
|
|
7
|
+
* next `compositionupdate` deletes the blocks in between — the commit never lands. A `<br>` in the same place maps cleanly.
|
|
8
|
+
*
|
|
9
|
+
* So for the duration of a composition the lone "\n" nodes before the composing block are swapped for a `<br>` (same single
|
|
10
|
+
* line), and put back at its end, before the model catches up. Nothing in the model or the Svelte-owned nodes changes.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Swap, in the document before `element`, every empty block's lone "\n" text node for a `<br>`.
|
|
15
|
+
* @param {Element | null | undefined} element the block holding the composition
|
|
16
|
+
* @returns {Array<{ br: Element, text: Text }>} what {@link restoreEmptyBlocks} puts back
|
|
17
|
+
*/
|
|
18
|
+
export function swapEmptyBlocks(element) {
|
|
19
|
+
/** @type {Array<{ br: Element, text: Text }>} */
|
|
20
|
+
const swapped = [];
|
|
21
|
+
const root = element?.closest('[data-nabu-root]');
|
|
22
|
+
if (!element || !root) return swapped;
|
|
23
|
+
const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
|
|
24
|
+
/** @type {Text[]} */
|
|
25
|
+
const lone = [];
|
|
26
|
+
for (let n = /** @type {Text | null} */ (walker.nextNode()); n; n = /** @type {Text | null} */ (walker.nextNode())) {
|
|
27
|
+
if (!(element.compareDocumentPosition(n) & Node.DOCUMENT_POSITION_PRECEDING)) break; // reached the composing block
|
|
28
|
+
if (n.data !== '\n' || !n.parentNode) continue;
|
|
29
|
+
// Only an empty block's placeholder: nothing but Svelte's anchors (comments, empty text nodes) beside it (no filler <br>, no other text).
|
|
30
|
+
let alone = true;
|
|
31
|
+
for (const s of n.parentNode.childNodes) {
|
|
32
|
+
if (s === n || s.nodeType === Node.COMMENT_NODE || (s.nodeType === Node.TEXT_NODE && !/** @type {Text} */ (s).data)) continue;
|
|
33
|
+
alone = false; break;
|
|
34
|
+
}
|
|
35
|
+
if (alone) lone.push(n);
|
|
36
|
+
}
|
|
37
|
+
for (const text of lone) {
|
|
38
|
+
const br = document.createElement('br');
|
|
39
|
+
text.replaceWith(br);
|
|
40
|
+
swapped.push({ br, text });
|
|
41
|
+
}
|
|
42
|
+
return swapped;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Put the "\n" text nodes back (Svelte keeps references to them). A `<br>` the browser or Svelte already removed is skipped.
|
|
47
|
+
* @param {Array<{ br: Element, text: Text }>} swapped
|
|
48
|
+
*/
|
|
49
|
+
export function restoreEmptyBlocks(swapped) {
|
|
50
|
+
for (const { br, text } of swapped) if (br.parentNode) br.replaceWith(text);
|
|
51
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The attributes of an op without the ones that are off, `undefined` when none is left.
|
|
3
|
+
* @param {Record<string, any> | undefined} attrs
|
|
4
|
+
* @returns {Record<string, any> | undefined}
|
|
5
|
+
*/
|
|
6
|
+
export function effective(attrs: Record<string, any> | undefined): Record<string, any> | undefined;
|
|
7
|
+
/**
|
|
8
|
+
* Whether two mark values are the same (links compare by href; an object value by its content).
|
|
9
|
+
* @param {any} a @param {any} b
|
|
10
|
+
*/
|
|
11
|
+
export function sameValue(a: any, b: any): boolean;
|
|
12
|
+
/**
|
|
13
|
+
* The canonical form of a delta (see the top of this file). One linear pass; when the delta already is canonical (the usual
|
|
14
|
+
* case, this runs on every keystroke) the SAME array is returned, nothing is allocated.
|
|
15
|
+
* @param {DeltaOp[]} delta
|
|
16
|
+
* @returns {DeltaOp[]}
|
|
17
|
+
*/
|
|
18
|
+
export function normalizeDelta(delta: DeltaOp[]): DeltaOp[];
|
|
19
|
+
/**
|
|
20
|
+
* The canonical delta of a Loro text: the only way the engine reads `toDelta()`.
|
|
21
|
+
* @param {{ toDelta: () => DeltaOp[] }} container
|
|
22
|
+
* @returns {DeltaOp[]}
|
|
23
|
+
*/
|
|
24
|
+
export function readDelta(container: {
|
|
25
|
+
toDelta: () => DeltaOp[];
|
|
26
|
+
}): DeltaOp[];
|
|
27
|
+
export type DeltaOp = import("loro-crdt").Delta<string>;
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
// Workaround for a Loro quirk (the raw delta keeps a split after an exact unmark). https://github.com/loro-dev/loro/issues/1241
|
|
2
|
+
// Canary: tests/unit/loro-quirks.test.js.
|
|
3
|
+
/**
|
|
4
|
+
* The canonical form of a rich-text delta.
|
|
5
|
+
*
|
|
6
|
+
* `LoroText.toDelta()` keeps a split where a mark was removed over exactly the range it was applied to: bolding `[18, 21)` then
|
|
7
|
+
* un-bolding `[18, 21)` leaves `[{ insert: 'The ford is upstre' }, { insert: 'am.' }]` instead of one run (an unmark over a wider
|
|
8
|
+
* range merges, which is why it looked random). The split lives in the document, snapshots included, and is not repaired there:
|
|
9
|
+
* every read of a delta goes through {@link readDelta}, so that the engine only ever sees runs that differ.
|
|
10
|
+
*
|
|
11
|
+
* Canonical means: no empty insert, no attribute whose value is `null`, `undefined` or `false` (a mark that is off is an absent
|
|
12
|
+
* mark, the way the renderers and `isMarkActive` read it), no `attributes` object when none is left, and no two adjacent text
|
|
13
|
+
* runs with the same attributes. An embed (a non-string insert) is kept untouched and never merged.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/** @typedef {import('loro-crdt').Delta<string>} DeltaOp */
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Whether a mark value counts as "off".
|
|
20
|
+
* @param {unknown} value
|
|
21
|
+
*/
|
|
22
|
+
const isOff = (value) => value === null || value === undefined || value === false;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Whether an op carries an attribute that is off, or an empty attribute object (it must be rewritten).
|
|
26
|
+
* @param {Record<string, any> | undefined} attrs
|
|
27
|
+
*/
|
|
28
|
+
function isDirty(attrs) {
|
|
29
|
+
if (!attrs) return false;
|
|
30
|
+
let any = false;
|
|
31
|
+
for (const key in attrs) {
|
|
32
|
+
if (isOff(attrs[key])) return true;
|
|
33
|
+
any = true;
|
|
34
|
+
}
|
|
35
|
+
return !any;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The attributes of an op without the ones that are off, `undefined` when none is left.
|
|
40
|
+
* @param {Record<string, any> | undefined} attrs
|
|
41
|
+
* @returns {Record<string, any> | undefined}
|
|
42
|
+
*/
|
|
43
|
+
export function effective(attrs) {
|
|
44
|
+
if (!attrs) return undefined;
|
|
45
|
+
/** @type {Record<string, any> | undefined} */
|
|
46
|
+
let out;
|
|
47
|
+
for (const key in attrs) if (!isOff(attrs[key])) (out ??= {})[key] = attrs[key];
|
|
48
|
+
return out;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Whether two mark values are the same (links compare by href; an object value by its content).
|
|
53
|
+
* @param {any} a @param {any} b
|
|
54
|
+
*/
|
|
55
|
+
export function sameValue(a, b) {
|
|
56
|
+
if (a === b) return true;
|
|
57
|
+
if (a && b && typeof a === 'object' && typeof b === 'object') return JSON.stringify(a) === JSON.stringify(b);
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Whether two EFFECTIVE attribute sets (no off value) are equal.
|
|
63
|
+
* @param {Record<string, any> | undefined} a @param {Record<string, any> | undefined} b
|
|
64
|
+
*/
|
|
65
|
+
function sameAttributes(a, b) {
|
|
66
|
+
if (a === b) return true;
|
|
67
|
+
if (!a || !b) return false;
|
|
68
|
+
let count = 0;
|
|
69
|
+
for (const key in a) {
|
|
70
|
+
if (!(key in b) || !sameValue(a[key], b[key])) return false;
|
|
71
|
+
count++;
|
|
72
|
+
}
|
|
73
|
+
for (const _ in b) count--;
|
|
74
|
+
return count === 0;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The canonical form of a delta (see the top of this file). One linear pass; when the delta already is canonical (the usual
|
|
79
|
+
* case, this runs on every keystroke) the SAME array is returned, nothing is allocated.
|
|
80
|
+
* @param {DeltaOp[]} delta
|
|
81
|
+
* @returns {DeltaOp[]}
|
|
82
|
+
*/
|
|
83
|
+
export function normalizeDelta(delta) {
|
|
84
|
+
const n = delta.length;
|
|
85
|
+
// First pass: is there anything to do?
|
|
86
|
+
let canonical = true;
|
|
87
|
+
for (let i = 0; i < n && canonical; i++) {
|
|
88
|
+
const op = delta[i];
|
|
89
|
+
if (typeof op.insert !== 'string') continue;
|
|
90
|
+
if (op.insert === '' || isDirty(op.attributes)) { canonical = false; break; }
|
|
91
|
+
const next = delta[i + 1];
|
|
92
|
+
if (next && typeof next.insert === 'string' && sameAttributes(op.attributes, next.attributes)) canonical = false;
|
|
93
|
+
}
|
|
94
|
+
if (canonical) return delta;
|
|
95
|
+
|
|
96
|
+
/** @type {DeltaOp[]} */
|
|
97
|
+
const out = [];
|
|
98
|
+
/** @type {DeltaOp | null} */
|
|
99
|
+
let last = null; // the run being built (a copy, never an op of the input)
|
|
100
|
+
for (const op of delta) {
|
|
101
|
+
if (typeof op.insert !== 'string') { out.push(op); last = null; continue; }
|
|
102
|
+
if (op.insert === '') continue;
|
|
103
|
+
const attrs = effective(op.attributes);
|
|
104
|
+
if (last && sameAttributes(/** @type {any} */ (last.attributes), attrs)) {
|
|
105
|
+
last.insert = /** @type {string} */ (last.insert) + op.insert;
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
last = attrs ? { insert: op.insert, attributes: attrs } : { insert: op.insert };
|
|
109
|
+
out.push(last);
|
|
110
|
+
}
|
|
111
|
+
return out;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* The canonical delta of a Loro text: the only way the engine reads `toDelta()`.
|
|
116
|
+
* @param {{ toDelta: () => DeltaOp[] }} container
|
|
117
|
+
* @returns {DeltaOp[]}
|
|
118
|
+
*/
|
|
119
|
+
export function readDelta(container) {
|
|
120
|
+
return normalizeDelta(container.toDelta());
|
|
121
|
+
}
|
|
@@ -39,7 +39,7 @@ export class SvelteSelection {
|
|
|
39
39
|
public isCollapsed: boolean;
|
|
40
40
|
rangeCount: number;
|
|
41
41
|
type: string;
|
|
42
|
-
direction: "
|
|
42
|
+
direction: "backward" | "forward" | "none";
|
|
43
43
|
startNode: Node | null;
|
|
44
44
|
endNode: Node | null;
|
|
45
45
|
/** @public */
|
package/dist/utils/tab.js
CHANGED
package/dist/utils/words.d.ts
CHANGED
|
@@ -5,3 +5,38 @@
|
|
|
5
5
|
* @returns {number}
|
|
6
6
|
*/
|
|
7
7
|
export function countWords(text: string, lang?: string): number;
|
|
8
|
+
/**
|
|
9
|
+
* Word deletion (`deleteWordBackward` / `deleteWordForward`: Ctrl+Backspace / Ctrl+Delete, Option+Backspace / Option+Delete).
|
|
10
|
+
*
|
|
11
|
+
* The text is cut into tokens: a word (`Intl.Segmenter` word granularity, so CJK and accented words are cut as the reader would),
|
|
12
|
+
* a run of punctuation (`...`, `?!`), a run of horizontal whitespace, a line break (`\n`), or any other single
|
|
13
|
+
* segment (an emoji, a symbol). The rules follow what Chromium does in a native `contenteditable` / `<textarea>`:
|
|
14
|
+
*
|
|
15
|
+
* - Backward (same on macOS and Windows): the whitespace before the caret goes with the token before it, and the caret lands at
|
|
16
|
+
* the start of that token: `"this is a sentence |"` -> `"this is a |"`, `"hello world|"` -> `"hello |"`, `"word, |"` -> `"word|"`,
|
|
17
|
+
* `"end.|"` -> `"end|"`. Only whitespace before the caret: all of it, and the token before it.
|
|
18
|
+
* - Forward on macOS (and Linux): up to the END of the next token, whitespace in between included: `"hello| world"` -> `"hello|"`,
|
|
19
|
+
* `"one |two three"` -> `"one | three"`.
|
|
20
|
+
* - Forward on Windows: the next token and the whitespace after it, up to the START of the following one (Chromium's
|
|
21
|
+
* `ShouldSkipSpaceWhenMovingRight`): `"one |two three"` -> `"one |three"`. At whitespace, that whitespace only.
|
|
22
|
+
* - A caret inside a token deletes to the token's edge (`"th|ree"` backward -> `"|ree"`). A line break is deleted by itself, and
|
|
23
|
+
* whitespace never reaches across one. Nothing at the start (backward) or the end (forward) of the text.
|
|
24
|
+
*
|
|
25
|
+
* @param {string} text
|
|
26
|
+
* @param {number} caret offset in `text`
|
|
27
|
+
* @param {'backward' | 'forward'} direction
|
|
28
|
+
* @param {{ platform?: 'mac' | 'other', lang?: string }} [options] `platform`: `'mac'` for the macOS forward rule, anything else is Windows / Linux-as-Windows
|
|
29
|
+
* @returns {{ from: number, to: number }} the span to delete (empty when there is nothing to delete)
|
|
30
|
+
*/
|
|
31
|
+
export function wordDeleteRange(text: string, caret: number, direction: "backward" | "forward", { platform, lang }?: {
|
|
32
|
+
platform?: "mac" | "other";
|
|
33
|
+
lang?: string;
|
|
34
|
+
}): {
|
|
35
|
+
from: number;
|
|
36
|
+
to: number;
|
|
37
|
+
};
|
|
38
|
+
export type WordToken = {
|
|
39
|
+
start: number;
|
|
40
|
+
end: number;
|
|
41
|
+
kind: "word" | "punct" | "space" | "break" | "other";
|
|
42
|
+
};
|
package/dist/utils/words.js
CHANGED
|
@@ -60,3 +60,88 @@ export function countWords(text, lang) {
|
|
|
60
60
|
}
|
|
61
61
|
return count;
|
|
62
62
|
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Word deletion (`deleteWordBackward` / `deleteWordForward`: Ctrl+Backspace / Ctrl+Delete, Option+Backspace / Option+Delete).
|
|
66
|
+
*
|
|
67
|
+
* The text is cut into tokens: a word (`Intl.Segmenter` word granularity, so CJK and accented words are cut as the reader would),
|
|
68
|
+
* a run of punctuation (`...`, `?!`), a run of horizontal whitespace, a line break (`\n`), or any other single
|
|
69
|
+
* segment (an emoji, a symbol). The rules follow what Chromium does in a native `contenteditable` / `<textarea>`:
|
|
70
|
+
*
|
|
71
|
+
* - Backward (same on macOS and Windows): the whitespace before the caret goes with the token before it, and the caret lands at
|
|
72
|
+
* the start of that token: `"this is a sentence |"` -> `"this is a |"`, `"hello world|"` -> `"hello |"`, `"word, |"` -> `"word|"`,
|
|
73
|
+
* `"end.|"` -> `"end|"`. Only whitespace before the caret: all of it, and the token before it.
|
|
74
|
+
* - Forward on macOS (and Linux): up to the END of the next token, whitespace in between included: `"hello| world"` -> `"hello|"`,
|
|
75
|
+
* `"one |two three"` -> `"one | three"`.
|
|
76
|
+
* - Forward on Windows: the next token and the whitespace after it, up to the START of the following one (Chromium's
|
|
77
|
+
* `ShouldSkipSpaceWhenMovingRight`): `"one |two three"` -> `"one |three"`. At whitespace, that whitespace only.
|
|
78
|
+
* - A caret inside a token deletes to the token's edge (`"th|ree"` backward -> `"|ree"`). A line break is deleted by itself, and
|
|
79
|
+
* whitespace never reaches across one. Nothing at the start (backward) or the end (forward) of the text.
|
|
80
|
+
*
|
|
81
|
+
* @param {string} text
|
|
82
|
+
* @param {number} caret offset in `text`
|
|
83
|
+
* @param {'backward' | 'forward'} direction
|
|
84
|
+
* @param {{ platform?: 'mac' | 'other', lang?: string }} [options] `platform`: `'mac'` for the macOS forward rule, anything else is Windows / Linux-as-Windows
|
|
85
|
+
* @returns {{ from: number, to: number }} the span to delete (empty when there is nothing to delete)
|
|
86
|
+
*/
|
|
87
|
+
export function wordDeleteRange(text, caret, direction, { platform = 'other', lang } = {}) {
|
|
88
|
+
const empty = { from: caret, to: caret };
|
|
89
|
+
if (direction === 'backward' ? caret <= 0 : caret >= text.length) return empty;
|
|
90
|
+
const tokens = wordTokens(text, lang);
|
|
91
|
+
const i = tokens.findIndex((t) => direction === 'backward' ? t.start < caret && caret <= t.end : t.start <= caret && caret < t.end);
|
|
92
|
+
if (i === -1) return empty;
|
|
93
|
+
const token = tokens[i];
|
|
94
|
+
if (direction === 'backward') {
|
|
95
|
+
let from = token.start;
|
|
96
|
+
if (token.kind === 'space' && i > 0 && tokens[i - 1].kind !== 'break') from = tokens[i - 1].start;
|
|
97
|
+
return { from, to: caret };
|
|
98
|
+
}
|
|
99
|
+
let to = token.end;
|
|
100
|
+
const next = tokens[i + 1];
|
|
101
|
+
if (next && next.kind !== 'break') {
|
|
102
|
+
// macOS: whitespace then the word after it; Windows: the word then the whitespace after it.
|
|
103
|
+
if (platform === 'mac' ? token.kind === 'space' : token.kind !== 'space' && token.kind !== 'break' && next.kind === 'space') to = next.end;
|
|
104
|
+
}
|
|
105
|
+
return { from: caret, to };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** @typedef {{ start: number, end: number, kind: 'word' | 'punct' | 'space' | 'break' | 'other' }} WordToken */
|
|
109
|
+
|
|
110
|
+
const PUNCT = /^\p{P}+$/u;
|
|
111
|
+
const WORDISH = /^[\p{L}\p{N}\p{M}_]+$/u;
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* @param {string} text @param {string | undefined} lang
|
|
115
|
+
* @returns {WordToken[]}
|
|
116
|
+
*/
|
|
117
|
+
function wordTokens(text, lang) {
|
|
118
|
+
/** @type {WordToken[]} */
|
|
119
|
+
const tokens = [];
|
|
120
|
+
/** @param {number} start @param {string} piece @param {boolean | undefined} wordLike */
|
|
121
|
+
const push = (start, piece, wordLike) => {
|
|
122
|
+
/** @type {WordToken['kind']} */
|
|
123
|
+
let kind;
|
|
124
|
+
if (piece === '\n') kind = 'break';
|
|
125
|
+
else if (/^[^\S\n]+$/.test(piece)) kind = 'space';
|
|
126
|
+
else if (wordLike ?? WORDISH.test(piece)) kind = 'word';
|
|
127
|
+
else if (PUNCT.test(piece)) kind = 'punct';
|
|
128
|
+
else kind = 'other';
|
|
129
|
+
const last = tokens[tokens.length - 1];
|
|
130
|
+
// Runs of punctuation (`...`, `?!`) and of whitespace are one token; words are not merged (the segmenter decided).
|
|
131
|
+
if (last && last.end === start && last.kind === kind && (kind === 'punct' || kind === 'space')) last.end = start + piece.length;
|
|
132
|
+
else tokens.push({ start, end: start + piece.length, kind });
|
|
133
|
+
};
|
|
134
|
+
const segmenter = segmenterFor(lang);
|
|
135
|
+
if (segmenter) {
|
|
136
|
+
for (const part of segmenter.segment(text)) {
|
|
137
|
+
// UAX #29 keeps `foo.bar` whole (a dot between letters); an editor stops at the dot, as Chromium does.
|
|
138
|
+
if (part.isWordLike && part.segment.includes('.')) {
|
|
139
|
+
let at = part.index;
|
|
140
|
+
for (const piece of part.segment.split(/(?<=\p{L})(\.)(?=\p{L})/u)) { push(at, piece, piece !== '.'); at += piece.length; }
|
|
141
|
+
} else push(part.index, part.segment, part.isWordLike);
|
|
142
|
+
}
|
|
143
|
+
} else {
|
|
144
|
+
for (const m of text.matchAll(/[\p{L}\p{N}\p{M}_]+|\p{P}+|[^\S\n]+|\n|[\s\S]/gu)) push(m.index, m[0], undefined);
|
|
145
|
+
}
|
|
146
|
+
return tokens;
|
|
147
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aionbuilders/nabu",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"dev": "vite dev",
|
|
6
6
|
"predev": "npm run api:model:ensure",
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
"prepack": "bunx svelte-kit sync && bunx svelte-package && bunx publint",
|
|
13
13
|
"check": "npm run api:model:ensure && svelte-kit sync && svelte-check --tsconfig ./jsconfig.json",
|
|
14
14
|
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./jsconfig.json --watch",
|
|
15
|
+
"history": "node scripts/history.js",
|
|
15
16
|
"test:unit": "vitest run",
|
|
16
17
|
"test:unit:watch": "vitest",
|
|
17
18
|
"build:e2e": "npm run api:model && VITE_NABU_E2E=1 vite build",
|
|
@@ -61,6 +62,7 @@
|
|
|
61
62
|
"devDependencies": {
|
|
62
63
|
"@fontsource/inter": "^5.3.0",
|
|
63
64
|
"@fontsource/jetbrains-mono": "^5.3.0",
|
|
65
|
+
"@fontsource/newsreader": "^5.3.0",
|
|
64
66
|
"@fontsource/syne": "^5.3.0",
|
|
65
67
|
"@playwright/test": "^1.58.2",
|
|
66
68
|
"@sveltejs/adapter-node": "^6.0.0",
|