@aionbuilders/nabu 0.1.6 → 0.1.7
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 +14 -0
- package/dist/behaviors/text/text.behavior.svelte.d.ts +1 -1
- package/dist/behaviors/text/text.behavior.svelte.js +2 -2
- package/dist/blocks/nabu.svelte.d.ts +13 -0
- package/dist/blocks/nabu.svelte.js +15 -0
- package/dist/blocks/region/region.extension.d.ts +10 -0
- package/dist/blocks/region/region.extension.js +7 -0
- package/dist/format.d.ts +1 -0
- package/dist/format.js +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +3 -0
- package/dist/presets.d.ts +2 -1
- package/dist/presets.js +5 -2
- package/dist/utils/words.d.ts +6 -1
- package/dist/utils/words.js +7 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.1.7 (2026-11-17)
|
|
4
|
+
|
|
5
|
+
Your own word counter, and types for `nabu.regions`.
|
|
6
|
+
|
|
7
|
+
### Added
|
|
8
|
+
- **`countWords` option: your own word counter.** `createEditor({ countWords: (text, lang) => number })` replaces the rule used everywhere Nabu counts words (`wordCount` of a block, a list item, a toggle, a region, the preamble). It receives the text of one block and the editor's `lang`, and is given once at creation. The default does not change. Use it when your app has a different definition of a word, for example Word's, where a hyphenated compound is one word. The function in use is `nabu.countWords`; a value that is not a function is ignored with a warning.
|
|
9
|
+
- **`countWords(text, lang)` is exported** by `@aionbuilders/nabu` and by `@aionbuilders/nabu/format` (which still loads neither Loro nor Svelte): the default rule, to count words of a serialized document without the editor. The `WordCounter` type describes a counter.
|
|
10
|
+
|
|
11
|
+
### Fixed
|
|
12
|
+
- **`nabu.regions` had no type.** `RegionsExtension` installs it when the editor starts, so the `Nabu` type does not have it, and an IDE offered nothing after `engine.regions`. `createFullEditor()` now returns a `RegionsEditor`, where `regions` is always typed, and the new `RegionsEditor` type annotates an editor built with your own preset. `Regions.get(nabu)` still works everywhere.
|
|
13
|
+
|
|
14
|
+
### Tests
|
|
15
|
+
- `bun run test:consumer` also type-checks a file against the installed package, the way an IDE sees it.
|
|
16
|
+
|
|
3
17
|
## 0.1.6 (2026-11-15)
|
|
4
18
|
|
|
5
19
|
Two browser fixes: IME compositions in Firefox, and typing in long documents on WebKit. No API change.
|
|
@@ -42,7 +42,7 @@ export class TextBehavior {
|
|
|
42
42
|
/**
|
|
43
43
|
* Number of words in this text: segmented with `Intl.Segmenter` in the language of the editor (`nabu.lang`) when the platform
|
|
44
44
|
* has it, so Chinese or Japanese text counts its words rather than one per sentence; whitespace-separated otherwise. See
|
|
45
|
-
* `utils/words.js`. Reactive in the text and in `nabu.lang`.
|
|
45
|
+
* `utils/words.js`. The editor's `countWords` option replaces that rule. Reactive in the text and in `nabu.lang`.
|
|
46
46
|
* @returns {number}
|
|
47
47
|
* @public
|
|
48
48
|
*/
|
|
@@ -90,12 +90,12 @@ export class TextBehavior {
|
|
|
90
90
|
/**
|
|
91
91
|
* Number of words in this text: segmented with `Intl.Segmenter` in the language of the editor (`nabu.lang`) when the platform
|
|
92
92
|
* has it, so Chinese or Japanese text counts its words rather than one per sentence; whitespace-separated otherwise. See
|
|
93
|
-
* `utils/words.js`. Reactive in the text and in `nabu.lang`.
|
|
93
|
+
* `utils/words.js`. The editor's `countWords` option replaces that rule. Reactive in the text and in `nabu.lang`.
|
|
94
94
|
* @returns {number}
|
|
95
95
|
* @public
|
|
96
96
|
*/
|
|
97
97
|
get wordCount() {
|
|
98
|
-
return countWords(this.text, this.nabu?.lang);
|
|
98
|
+
return (this.nabu?.countWords ?? countWords)(this.text, this.nabu?.lang);
|
|
99
99
|
}
|
|
100
100
|
|
|
101
101
|
/**
|
|
@@ -145,6 +145,13 @@ export class Nabu {
|
|
|
145
145
|
* @public
|
|
146
146
|
*/
|
|
147
147
|
public get lang(): string | undefined;
|
|
148
|
+
/**
|
|
149
|
+
* The function that counts words: the `countWords` option, or the default rule (`countWords` of `utils/words.js`, also exported
|
|
150
|
+
* by the package). Fixed at creation.
|
|
151
|
+
* @type {import('../utils/words.js').WordCounter}
|
|
152
|
+
* @public
|
|
153
|
+
*/
|
|
154
|
+
public countWords: import("../utils/words.js").WordCounter;
|
|
148
155
|
public set readonly(value: boolean);
|
|
149
156
|
/**
|
|
150
157
|
* Read-only mode (see the `readonly` option of NabuInit). Reactive: assign to switch it on or off at runtime. While `true`
|
|
@@ -792,6 +799,12 @@ export type NabuInit = {
|
|
|
792
799
|
* Reactive: `nabu.lang`.
|
|
793
800
|
*/
|
|
794
801
|
lang?: string | undefined;
|
|
802
|
+
/**
|
|
803
|
+
* - Your own word counter, `(text, lang) => number`, called with the text of
|
|
804
|
+
* one block and the editor's `lang`; it replaces the default rule (Unicode word breaks) everywhere Nabu counts words (`wordCount` of
|
|
805
|
+
* a block, a region...). Given once at creation, not reactive. Not a function: ignored (warning). Read back as `nabu.countWords`.
|
|
806
|
+
*/
|
|
807
|
+
countWords?: import("../utils/words.js").WordCounter | undefined;
|
|
795
808
|
/**
|
|
796
809
|
* - What Tab / Shift+Tab do in a plain text block, where no block handles them
|
|
797
810
|
* structurally (list indent, code indentation and region titles keep their own meaning, whatever this says; P-41).
|
|
@@ -20,6 +20,7 @@ import { closestWithin } from '../utils/dom-scope.js';
|
|
|
20
20
|
import { captureSelection, explicitSelection, legacySelection, restoreSelection, keepSelection, makeEndpoint, resolveOffset, revealFocus } from './undo-selection.js';
|
|
21
21
|
import { LARGE_DOC } from './chunks.js';
|
|
22
22
|
import { revealCaret } from '../utils/reveal-caret.js';
|
|
23
|
+
import { countWords as defaultCountWords } from '../utils/words.js';
|
|
23
24
|
import { markAtCaret } from '../behaviors/text/mark-edges.js';
|
|
24
25
|
import { swapEmptyBlocks, restoreEmptyBlocks } from '../utils/composition.js';
|
|
25
26
|
import { transferKind, wantsCopy, pickDropEffect, sameTarget, resolveDropPosition, documentEnd, positionInRange, positionAfterRemoval, DRAG_MARKER, NABU_FORMAT } from '../utils/drop.js';
|
|
@@ -63,6 +64,9 @@ import { transferKind, wantsCopy, pickDropEffect, sameTarget, resolveDropPositio
|
|
|
63
64
|
* @property {string} [lang] - Language of the content, a BCP 47 tag (`'fr'`, `'en-GB'`). Set on the root as `lang`, so the browser's
|
|
64
65
|
* spellchecker, hyphenation and screen readers use it. Unset: inherits the page language. An invalid tag is ignored (warning).
|
|
65
66
|
* Reactive: `nabu.lang`.
|
|
67
|
+
* @property {import('../utils/words.js').WordCounter} [countWords] - Your own word counter, `(text, lang) => number`, called with the text of
|
|
68
|
+
* one block and the editor's `lang`; it replaces the default rule (Unicode word breaks) everywhere Nabu counts words (`wordCount` of
|
|
69
|
+
* a block, a region...). Given once at creation, not reactive. Not a function: ignored (warning). Read back as `nabu.countWords`.
|
|
66
70
|
* @property {'focus' | 'stay' | 'indent'} [tab] - What Tab / Shift+Tab do in a plain text block, where no block handles them
|
|
67
71
|
* structurally (list indent, code indentation and region titles keep their own meaning, whatever this says; P-41).
|
|
68
72
|
* `'focus'`: the browser default, Tab leaves the editor. `'stay'`: both keys are swallowed and the caret stays; Escape then Tab
|
|
@@ -287,6 +291,17 @@ export class Nabu {
|
|
|
287
291
|
this.placeholders = normalizePlaceholders(init.placeholders);
|
|
288
292
|
this.spellcheck = init.spellcheck !== false;
|
|
289
293
|
this.lang = init.lang;
|
|
294
|
+
/**
|
|
295
|
+
* The function that counts words: the `countWords` option, or the default rule (`countWords` of `utils/words.js`, also exported
|
|
296
|
+
* by the package). Fixed at creation.
|
|
297
|
+
* @type {import('../utils/words.js').WordCounter}
|
|
298
|
+
* @public
|
|
299
|
+
*/
|
|
300
|
+
this.countWords = defaultCountWords;
|
|
301
|
+
if (init.countWords !== undefined) {
|
|
302
|
+
if (typeof init.countWords === 'function') this.countWords = init.countWords;
|
|
303
|
+
else this.warn(`Ignoring invalid "countWords" (not a function): ${String(init.countWords)}`);
|
|
304
|
+
}
|
|
290
305
|
this.readonly = !!init.readonly;
|
|
291
306
|
this.#tabDefault = resolveTabMode(init.tabDefault, 'focus', (message) => this.warn(message));
|
|
292
307
|
this.tab = init.tab;
|
|
@@ -54,5 +54,15 @@ export function createRegionsExtension(options?: {
|
|
|
54
54
|
types?: Record<string, RegionTypeInit>;
|
|
55
55
|
messages?: RegionMessages;
|
|
56
56
|
}): import("../../extend.js").Extension;
|
|
57
|
+
/**
|
|
58
|
+
* An editor built with `RegionsExtension`: `nabu.regions` (the registry, same as `Regions.get(nabu)`) is installed by the
|
|
59
|
+
* extension's `onInit` hook, never by the engine, so the `Nabu` type does not have it. `createFullEditor()` returns this type;
|
|
60
|
+
* with your own preset, annotate the editor: `/** @type {RegionsEditor} *\/ (createEditor({ preset }))`, or use `Regions.get(nabu)`.
|
|
61
|
+
*/
|
|
62
|
+
export type RegionsEditor = Nabu & {
|
|
63
|
+
readonly regions: Regions;
|
|
64
|
+
};
|
|
57
65
|
import type { RegionTypeInit } from "./types.js";
|
|
58
66
|
import type { RegionMessages } from "./messages.js";
|
|
67
|
+
import type { Nabu } from "../nabu.svelte.js";
|
|
68
|
+
import { Regions } from "./regions.svelte.js";
|
|
@@ -17,6 +17,13 @@ import { closeOnEnter } from "./close.js";
|
|
|
17
17
|
* @import { RegionMessages } from "./messages.js";
|
|
18
18
|
*/
|
|
19
19
|
|
|
20
|
+
/**
|
|
21
|
+
* @typedef {Nabu & { readonly regions: Regions }} RegionsEditor
|
|
22
|
+
* An editor built with `RegionsExtension`: `nabu.regions` (the registry, same as `Regions.get(nabu)`) is installed by the
|
|
23
|
+
* extension's `onInit` hook, never by the engine, so the `Nabu` type does not have it. `createFullEditor()` returns this type;
|
|
24
|
+
* with your own preset, annotate the editor: `/** @type {RegionsEditor} *\/ (createEditor({ preset }))`, or use `Regions.get(nabu)`.
|
|
25
|
+
*/
|
|
26
|
+
|
|
20
27
|
/**
|
|
21
28
|
* @param {Nabu} nabu @param {InputEvent} event @param {import("../block.svelte.js").Block} block
|
|
22
29
|
*/
|
package/dist/format.d.ts
CHANGED
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
export function jsonToText(json: {
|
|
19
19
|
blocks?: any[];
|
|
20
20
|
} | any[]): string;
|
|
21
|
+
export { countWords } from "./utils/words.js";
|
|
21
22
|
export { deltaToHtml, deltaToMarkdown } from "./behaviors/text/delta-format.js";
|
|
22
23
|
export { sanitizeUrl, escapeHtml } from "./utils/html.js";
|
|
23
24
|
export { JSON_FORMAT_VERSION, isSupportedJsonVersion } from "./utils/extensions.js";
|
package/dist/format.js
CHANGED
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
|
|
13
13
|
export { deltaToHtml, deltaToMarkdown } from './behaviors/text/delta-format.js';
|
|
14
14
|
export { sanitizeUrl, escapeHtml } from './utils/html.js';
|
|
15
|
+
export { countWords } from './utils/words.js';
|
|
15
16
|
export { JSON_FORMAT_VERSION, isSupportedJsonVersion } from './utils/extensions.js';
|
|
16
17
|
|
|
17
18
|
/** The text of a block's `content`: a plain string (code), or the `text` of its runs. Marks are ignored. @param {unknown} content */
|
package/dist/index.d.ts
CHANGED
|
@@ -13,6 +13,11 @@ export { FileExtension } from "./blocks/file/index.js";
|
|
|
13
13
|
export { insertMediaFromFile } from "./blocks/media/index.js";
|
|
14
14
|
export { sanitizeUrl } from "./utils/html.js";
|
|
15
15
|
export { formatChord } from "./utils/keys.js";
|
|
16
|
+
export { countWords } from "./utils/words.js";
|
|
17
|
+
/**
|
|
18
|
+
* A word counter, `(text, lang?) => number`: the `countWords` option.
|
|
19
|
+
*/
|
|
20
|
+
export type WordCounter = import("./utils/words.js").WordCounter;
|
|
16
21
|
/**
|
|
17
22
|
* The options of an editor: `new Nabu(init)`, the factories and `createPersistedEditor`.
|
|
18
23
|
*/
|
|
@@ -21,6 +26,10 @@ export type NabuInit = import("./blocks/nabu.svelte.js").NabuInit;
|
|
|
21
26
|
* The options of the editor factories (`createEditor` and the preset factories): every `NabuInit` option plus the `preset` to start from.
|
|
22
27
|
*/
|
|
23
28
|
export type EditorOptions = import("./presets.js").EditorOptions;
|
|
29
|
+
/**
|
|
30
|
+
* An editor with `nabu.regions` typed (see `RegionsExtension`).
|
|
31
|
+
*/
|
|
32
|
+
export type RegionsEditor = import("./blocks/region/region.extension.js").RegionsEditor;
|
|
24
33
|
/**
|
|
25
34
|
* The `files` option: uploads, limits, embedding.
|
|
26
35
|
*/
|
package/dist/index.js
CHANGED
|
@@ -78,16 +78,19 @@ export { JSON_FORMAT_VERSION, CLIPBOARD_FORMAT_VERSION, isSupportedJsonVersion }
|
|
|
78
78
|
export { deltaToMarkdown, deltaToHtml } from './behaviors/text/index.js';
|
|
79
79
|
export { sanitizeUrl } from './utils/html.js';
|
|
80
80
|
export { formatChord } from './utils/keys.js';
|
|
81
|
+
export { countWords } from './utils/words.js';
|
|
81
82
|
|
|
82
83
|
// ---------------------------------------------------------------------------
|
|
83
84
|
// Types (JSDoc typedefs, importable as `import('@aionbuilders/nabu').NabuInit`…)
|
|
84
85
|
// ---------------------------------------------------------------------------
|
|
85
86
|
|
|
87
|
+
/** @typedef {import('./utils/words.js').WordCounter} WordCounter A word counter, `(text, lang?) => number`: the `countWords` option. */
|
|
86
88
|
/** @typedef {import('./blocks/nabu.svelte.js').NabuInit} NabuInit The options of an editor: `new Nabu(init)`, the factories and `createPersistedEditor`. */
|
|
87
89
|
/**
|
|
88
90
|
* @typedef {import('./presets.js').EditorOptions} EditorOptions
|
|
89
91
|
* The options of the editor factories (`createEditor` and the preset factories): every `NabuInit` option plus the `preset` to start from.
|
|
90
92
|
*/
|
|
93
|
+
/** @typedef {import('./blocks/region/region.extension.js').RegionsEditor} RegionsEditor An editor with `nabu.regions` typed (see `RegionsExtension`). */
|
|
91
94
|
/** @typedef {import('./files/files.svelte.js').FilesConfig} FilesConfig The `files` option: uploads, limits, embedding. */
|
|
92
95
|
/** @typedef {import('./files/files.svelte.js').EmbeddedAsset} EmbeddedAsset */
|
|
93
96
|
/** @typedef {import('./utils/placeholders.js').PlaceholdersOption} PlaceholdersOption */
|
package/dist/presets.d.ts
CHANGED
|
@@ -27,7 +27,7 @@ export function tabDefaultFor(preset: unknown): "focus" | "stay" | "indent";
|
|
|
27
27
|
export function createMinimalEditor(options?: EditorOptions): Nabu;
|
|
28
28
|
export function createTextEditor(options?: EditorOptions): Nabu;
|
|
29
29
|
export function createDocumentEditor(options?: EditorOptions): Nabu;
|
|
30
|
-
export function createFullEditor(options?: EditorOptions):
|
|
30
|
+
export function createFullEditor(options?: EditorOptions): RegionsEditor;
|
|
31
31
|
export { createPersistedEditor } from "./persistence/persistence.js";
|
|
32
32
|
/**
|
|
33
33
|
* Options of the editor factories: every `NabuInit` option (`files`, `placeholders`, `snapshot`, `keymap`, `tab`, `lang`…, forwarded to
|
|
@@ -41,3 +41,4 @@ export type EditorOptions = Omit<import("./blocks/nabu.svelte.js").NabuInit, "ex
|
|
|
41
41
|
};
|
|
42
42
|
import { Nabu } from './blocks/nabu.svelte.js';
|
|
43
43
|
import type { Extension } from './utils/extensions.js';
|
|
44
|
+
import type { RegionsEditor } from './blocks/region/region.extension.js';
|
package/dist/presets.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @import { Extension } from './utils/extensions.js';
|
|
3
|
+
* @import { RegionsEditor } from './blocks/region/region.extension.js';
|
|
3
4
|
*/
|
|
4
5
|
|
|
5
6
|
import { Nabu } from './blocks/nabu.svelte.js';
|
|
@@ -106,11 +107,13 @@ export const createDocumentEditor = (options = {}) =>
|
|
|
106
107
|
createEditor({ preset: DocumentPreset, ...options });
|
|
107
108
|
|
|
108
109
|
/**
|
|
109
|
-
* All built-in blocks + rich text. The full Nabu experience.
|
|
110
|
+
* All built-in blocks + rich text. The full Nabu experience. It includes `RegionsExtension`, so `nabu.regions` is always there
|
|
111
|
+
* (typed without `undefined`).
|
|
110
112
|
* @param {EditorOptions} [options] every `NabuInit` option (`files`, `snapshot`, `keymap`…) and extra `extensions`
|
|
113
|
+
* @returns {RegionsEditor}
|
|
111
114
|
*/
|
|
112
115
|
export const createFullEditor = (options = {}) =>
|
|
113
|
-
createEditor({ preset: FullPreset, ...options });
|
|
116
|
+
/** @type {RegionsEditor} */ (createEditor({ preset: FullPreset, ...options }));
|
|
114
117
|
|
|
115
118
|
// ---------------------------------------------------------------------------
|
|
116
119
|
// Persisted factory
|
package/dist/utils/words.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Number of words in a text.
|
|
2
|
+
* Number of words in a text (the default {@link WordCounter} of Nabu).
|
|
3
3
|
* @param {string} text
|
|
4
4
|
* @param {string} [lang] BCP 47 tag of the text (the editor's `lang`); unset: the platform default
|
|
5
5
|
* @returns {number}
|
|
@@ -35,6 +35,11 @@ export function wordDeleteRange(text: string, caret: number, direction: "backwar
|
|
|
35
35
|
from: number;
|
|
36
36
|
to: number;
|
|
37
37
|
};
|
|
38
|
+
/**
|
|
39
|
+
* A word-counting function: the text of one block and the editor's `lang`, the number of words. The `countWords` option of an
|
|
40
|
+
* editor replaces the default one with a function of this shape (see `NabuInit`).
|
|
41
|
+
*/
|
|
42
|
+
export type WordCounter = (text: string, lang?: string) => number;
|
|
38
43
|
export type WordToken = {
|
|
39
44
|
start: number;
|
|
40
45
|
end: number;
|
package/dist/utils/words.js
CHANGED
|
@@ -16,6 +16,12 @@
|
|
|
16
16
|
* one word by the same rules).
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
+
/**
|
|
20
|
+
* A word-counting function: the text of one block and the editor's `lang`, the number of words. The `countWords` option of an
|
|
21
|
+
* editor replaces the default one with a function of this shape (see `NabuInit`).
|
|
22
|
+
* @typedef {(text: string, lang?: string) => number} WordCounter
|
|
23
|
+
*/
|
|
24
|
+
|
|
19
25
|
/** @type {Map<string, Intl.Segmenter | null>} */
|
|
20
26
|
const segmenters = new Map();
|
|
21
27
|
|
|
@@ -42,7 +48,7 @@ function segmenterFor(lang) {
|
|
|
42
48
|
const LATIN_WORD = /^[0-9A-Za-zÀ-ÖØ-öø-ɏ]+$/;
|
|
43
49
|
|
|
44
50
|
/**
|
|
45
|
-
* Number of words in a text.
|
|
51
|
+
* Number of words in a text (the default {@link WordCounter} of Nabu).
|
|
46
52
|
* @param {string} text
|
|
47
53
|
* @param {string} [lang] BCP 47 tag of the text (the editor's `lang`); unset: the platform default
|
|
48
54
|
* @returns {number}
|