@aionbuilders/nabu 0.1.0-alpha.9 → 0.1.0
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 +523 -0
- package/LICENSE +21 -0
- package/README.md +82 -144
- package/dist/behaviors/text/RichText.svelte +25 -2
- package/dist/behaviors/text/index.d.ts +2 -0
- package/dist/behaviors/text/index.js +2 -0
- package/dist/behaviors/text/markdown-shortcuts.extension.d.ts +5 -0
- package/dist/behaviors/text/markdown-shortcuts.extension.js +79 -0
- package/dist/behaviors/text/markdown-syntax.d.ts +42 -0
- package/dist/behaviors/text/markdown-syntax.js +81 -0
- package/dist/behaviors/text/rich-text.extension.d.ts +3 -0
- package/dist/behaviors/text/rich-text.extension.js +135 -53
- package/dist/behaviors/text/text.behavior.svelte.d.ts +217 -42
- package/dist/behaviors/text/text.behavior.svelte.js +657 -55
- package/dist/behaviors/text/typography.extension.d.ts +7 -0
- package/dist/behaviors/text/typography.extension.js +25 -0
- package/dist/blocks/Block.svelte +5 -2
- package/dist/blocks/BlockList.svelte +48 -0
- package/dist/blocks/BlockList.svelte.d.ts +17 -0
- package/dist/blocks/Nabu.svelte +180 -14
- package/dist/blocks/Nabu.svelte.d.ts +13 -0
- package/dist/blocks/block.extension.d.ts +3 -0
- package/dist/blocks/block.extension.js +8 -9
- package/dist/blocks/block.svelte.d.ts +356 -68
- package/dist/blocks/block.svelte.js +388 -42
- package/dist/blocks/blockquote/Blockquote.svelte +39 -0
- package/dist/blocks/blockquote/Blockquote.svelte.d.ts +11 -0
- package/dist/blocks/blockquote/blockquote.extension.d.ts +4 -0
- package/dist/blocks/blockquote/blockquote.extension.js +80 -0
- package/dist/blocks/blockquote/blockquote.svelte.d.ts +63 -0
- package/dist/blocks/blockquote/blockquote.svelte.js +136 -0
- package/dist/blocks/blockquote/index.d.ts +3 -0
- package/dist/blocks/blockquote/index.js +3 -0
- package/dist/blocks/callout/Callout.svelte +67 -0
- package/dist/blocks/callout/Callout.svelte.d.ts +11 -0
- package/dist/blocks/callout/callout.extension.d.ts +4 -0
- package/dist/blocks/callout/callout.extension.js +84 -0
- package/dist/blocks/callout/callout.svelte.d.ts +80 -0
- package/dist/blocks/callout/callout.svelte.js +189 -0
- package/dist/blocks/callout/index.d.ts +3 -0
- package/dist/blocks/callout/index.js +3 -0
- package/dist/blocks/chunks.d.ts +49 -0
- package/dist/blocks/chunks.js +90 -0
- package/dist/blocks/code/Code.svelte +84 -0
- package/dist/blocks/code/Code.svelte.d.ts +11 -0
- package/dist/blocks/code/code.extension.d.ts +4 -0
- package/dist/blocks/code/code.extension.js +88 -0
- package/dist/blocks/code/code.svelte.d.ts +68 -0
- package/dist/blocks/code/code.svelte.js +245 -0
- package/dist/blocks/code/index.d.ts +3 -0
- package/dist/blocks/code/index.js +3 -0
- package/dist/blocks/container.utils.d.ts +85 -13
- package/dist/blocks/container.utils.js +338 -178
- package/dist/blocks/dialogue/Dialogue.svelte +8 -2
- package/dist/blocks/dialogue/dialogue.svelte.d.ts +36 -23
- package/dist/blocks/dialogue/dialogue.svelte.js +51 -45
- package/dist/blocks/dialogue/hooks/onBeforeInput.hook.d.ts +2 -2
- package/dist/blocks/dialogue/hooks/onBeforeInput.hook.js +6 -4
- package/dist/blocks/dialogue/index.d.ts +12 -1
- package/dist/blocks/dialogue/index.js +51 -35
- package/dist/blocks/divider/Divider.svelte +28 -0
- package/dist/blocks/divider/Divider.svelte.d.ts +11 -0
- package/dist/blocks/divider/divider.extension.d.ts +23 -0
- package/dist/blocks/divider/divider.extension.js +153 -0
- package/dist/blocks/divider/divider.svelte.d.ts +54 -0
- package/dist/blocks/divider/divider.svelte.js +89 -0
- package/dist/blocks/divider/index.d.ts +3 -0
- package/dist/blocks/divider/index.js +3 -0
- package/dist/blocks/file/File.svelte +56 -0
- package/dist/blocks/file/File.svelte.d.ts +11 -0
- package/dist/blocks/file/file.extension.d.ts +4 -0
- package/dist/blocks/file/file.extension.js +27 -0
- package/dist/blocks/file/file.svelte.d.ts +29 -0
- package/dist/blocks/file/file.svelte.js +77 -0
- package/dist/blocks/file/index.d.ts +3 -0
- package/dist/blocks/file/index.js +3 -0
- package/dist/blocks/heading/Heading.svelte +2 -1
- package/dist/blocks/heading/heading.svelte.d.ts +27 -34
- package/dist/blocks/heading/heading.svelte.js +72 -60
- package/dist/blocks/heading/hooks/onBeforeInput.hook.js +7 -5
- package/dist/blocks/heading/index.d.ts +2 -2
- package/dist/blocks/heading/index.js +33 -8
- package/dist/blocks/image/Image.svelte +41 -0
- package/dist/blocks/image/Image.svelte.d.ts +11 -0
- package/dist/blocks/image/image.extension.d.ts +5 -0
- package/dist/blocks/image/image.extension.js +34 -0
- package/dist/blocks/image/image.svelte.d.ts +61 -0
- package/dist/blocks/image/image.svelte.js +114 -0
- package/dist/blocks/image/index.d.ts +3 -0
- package/dist/blocks/image/index.js +3 -0
- package/dist/blocks/index.d.ts +11 -1
- package/dist/blocks/index.js +11 -1
- package/dist/blocks/list/List.svelte +9 -2
- package/dist/blocks/list/ListItem.svelte +44 -7
- package/dist/blocks/list/convert.d.ts +20 -0
- package/dist/blocks/list/convert.js +92 -0
- package/dist/blocks/list/hooks/onBeforeInput.hook.js +60 -26
- package/dist/blocks/list/html-flat.d.ts +78 -0
- package/dist/blocks/list/html-flat.js +244 -0
- package/dist/blocks/list/indent.d.ts +86 -0
- package/dist/blocks/list/indent.js +205 -0
- package/dist/blocks/list/index.d.ts +7 -3
- package/dist/blocks/list/index.js +43 -31
- package/dist/blocks/list/list-item.svelte.d.ts +43 -17
- package/dist/blocks/list/list-item.svelte.js +143 -93
- package/dist/blocks/list/list.behavior.svelte.d.ts +7 -4
- package/dist/blocks/list/list.behavior.svelte.js +10 -5
- package/dist/blocks/list/list.svelte.d.ts +101 -23
- package/dist/blocks/list/list.svelte.js +136 -114
- package/dist/blocks/list/markdown.d.ts +149 -0
- package/dist/blocks/list/markdown.js +442 -0
- package/dist/blocks/list/normalize.d.ts +11 -0
- package/dist/blocks/list/normalize.js +396 -0
- package/dist/blocks/list/paste.d.ts +45 -0
- package/dist/blocks/list/paste.js +242 -0
- package/dist/blocks/list/step-back.d.ts +24 -0
- package/dist/blocks/list/step-back.js +87 -0
- package/dist/blocks/list/to-list.d.ts +7 -0
- package/dist/blocks/list/to-list.js +44 -0
- package/dist/blocks/list/todos-toggle.d.ts +20 -0
- package/dist/blocks/list/todos-toggle.js +48 -0
- package/dist/blocks/list/todos.svelte.d.ts +35 -0
- package/dist/blocks/list/todos.svelte.js +76 -0
- package/dist/blocks/media/index.d.ts +2 -0
- package/dist/blocks/media/index.js +2 -0
- package/dist/blocks/media/markdown.d.ts +12 -0
- package/dist/blocks/media/markdown.js +26 -0
- package/dist/blocks/media/media-block.svelte.d.ts +107 -0
- package/dist/blocks/media/media-block.svelte.js +181 -0
- package/dist/blocks/media/media.helpers.d.ts +24 -0
- package/dist/blocks/media/media.helpers.js +88 -0
- package/dist/blocks/megablock.svelte.d.ts +50 -19
- package/dist/blocks/megablock.svelte.js +115 -74
- package/dist/blocks/move.d.ts +23 -0
- package/dist/blocks/move.js +92 -0
- package/dist/blocks/nabu.svelte.d.ts +846 -75
- package/dist/blocks/nabu.svelte.js +1990 -182
- package/dist/blocks/paragraph/Paragraph.svelte +1 -0
- package/dist/blocks/paragraph/index.d.ts +2 -2
- package/dist/blocks/paragraph/index.js +35 -16
- package/dist/blocks/paragraph/paragraph.svelte.d.ts +49 -35
- package/dist/blocks/paragraph/paragraph.svelte.js +81 -69
- package/dist/blocks/paragraph/table.html.d.ts +9 -0
- package/dist/blocks/paragraph/table.html.js +111 -0
- package/dist/blocks/paragraph/table.markdown.d.ts +32 -0
- package/dist/blocks/paragraph/table.markdown.js +90 -0
- package/dist/blocks/region/Region.svelte +264 -0
- package/dist/blocks/region/Region.svelte.d.ts +11 -0
- package/dist/blocks/region/actions.d.ts +135 -0
- package/dist/blocks/region/actions.js +402 -0
- package/dist/blocks/region/close.d.ts +35 -0
- package/dist/blocks/region/close.js +130 -0
- package/dist/blocks/region/commands.d.ts +6 -0
- package/dist/blocks/region/commands.js +277 -0
- package/dist/blocks/region/create.d.ts +72 -0
- package/dist/blocks/region/create.js +208 -0
- package/dist/blocks/region/end-marker.d.ts +35 -0
- package/dist/blocks/region/end-marker.js +51 -0
- package/dist/blocks/region/facts.d.ts +69 -0
- package/dist/blocks/region/facts.js +152 -0
- package/dist/blocks/region/format.d.ts +91 -0
- package/dist/blocks/region/format.js +260 -0
- package/dist/blocks/region/index.d.ts +14 -0
- package/dist/blocks/region/index.js +14 -0
- package/dist/blocks/region/insert.d.ts +59 -0
- package/dist/blocks/region/insert.js +102 -0
- package/dist/blocks/region/messages.d.ts +254 -0
- package/dist/blocks/region/messages.js +131 -0
- package/dist/blocks/region/normalize.d.ts +7 -0
- package/dist/blocks/region/normalize.js +170 -0
- package/dist/blocks/region/region.extension.d.ts +58 -0
- package/dist/blocks/region/region.extension.js +119 -0
- package/dist/blocks/region/region.svelte.d.ts +221 -0
- package/dist/blocks/region/region.svelte.js +582 -0
- package/dist/blocks/region/regions.svelte.d.ts +141 -0
- package/dist/blocks/region/regions.svelte.js +276 -0
- package/dist/blocks/region/serialize.d.ts +57 -0
- package/dist/blocks/region/serialize.js +112 -0
- package/dist/blocks/region/types.d.ts +255 -0
- package/dist/blocks/region/types.js +204 -0
- package/dist/blocks/selection.svelte.d.ts +106 -8
- package/dist/blocks/selection.svelte.js +632 -74
- package/dist/blocks/subthread.d.ts +80 -0
- package/dist/blocks/subthread.js +167 -0
- package/dist/blocks/text-block.svelte.d.ts +70 -0
- package/dist/blocks/text-block.svelte.js +108 -0
- package/dist/blocks/toggle/Toggle.svelte +73 -0
- package/dist/blocks/toggle/Toggle.svelte.d.ts +11 -0
- package/dist/blocks/toggle/index.d.ts +3 -0
- package/dist/blocks/toggle/index.js +3 -0
- package/dist/blocks/toggle/markdown.d.ts +20 -0
- package/dist/blocks/toggle/markdown.js +112 -0
- package/dist/blocks/toggle/toggle.extension.d.ts +4 -0
- package/dist/blocks/toggle/toggle.extension.js +18 -0
- package/dist/blocks/toggle/toggle.svelte.d.ts +135 -0
- package/dist/blocks/toggle/toggle.svelte.js +352 -0
- package/dist/blocks/undo-selection.d.ts +175 -0
- package/dist/blocks/undo-selection.js +360 -0
- package/dist/blocks/video/Video.svelte +42 -0
- package/dist/blocks/video/Video.svelte.d.ts +11 -0
- package/dist/blocks/video/index.d.ts +3 -0
- package/dist/blocks/video/index.js +3 -0
- package/dist/blocks/video/video.extension.d.ts +4 -0
- package/dist/blocks/video/video.extension.js +27 -0
- package/dist/blocks/video/video.svelte.d.ts +29 -0
- package/dist/blocks/video/video.svelte.js +80 -0
- package/dist/extend.d.ts +55 -0
- package/dist/extend.js +88 -0
- package/dist/files/assets.clipboard.d.ts +77 -0
- package/dist/files/assets.clipboard.js +152 -0
- package/dist/files/files.svelte.d.ts +363 -0
- package/dist/files/files.svelte.js +897 -0
- package/dist/files/index.d.ts +3 -0
- package/dist/files/index.js +3 -0
- package/dist/files/outbox.d.ts +69 -0
- package/dist/files/outbox.js +48 -0
- package/dist/files/status.d.ts +65 -0
- package/dist/files/status.js +87 -0
- package/dist/files/store.d.ts +97 -0
- package/dist/files/store.js +65 -0
- package/dist/files/uploads.svelte.d.ts +174 -0
- package/dist/files/uploads.svelte.js +504 -0
- package/dist/index.d.ts +68 -6
- package/dist/index.js +117 -7
- package/dist/nabu-theme.css +112 -0
- package/dist/paste/html.paste.d.ts +74 -3
- package/dist/paste/html.paste.js +555 -83
- package/dist/paste/markdown-inline.d.ts +86 -0
- package/dist/paste/markdown-inline.js +528 -0
- package/dist/paste/markdown.paste.d.ts +2 -8
- package/dist/paste/markdown.paste.js +78 -94
- package/dist/paste/nabu.paste.d.ts +3 -0
- package/dist/paste/nabu.paste.js +29 -3
- package/dist/paste/plain-text.paste.d.ts +2 -2
- package/dist/paste/plain-text.paste.js +3 -3
- package/dist/persistence/asset-store.d.ts +51 -0
- package/dist/persistence/asset-store.js +103 -0
- package/dist/persistence/autosave.d.ts +53 -0
- package/dist/persistence/autosave.js +62 -0
- package/dist/persistence/compact.d.ts +162 -0
- package/dist/persistence/compact.js +141 -0
- package/dist/persistence/history.d.ts +77 -0
- package/dist/persistence/history.js +176 -0
- package/dist/persistence/indexeddb.d.ts +190 -4
- package/dist/persistence/indexeddb.js +333 -29
- package/dist/persistence/merge.d.ts +38 -0
- package/dist/persistence/merge.js +36 -0
- package/dist/persistence/outbox.d.ts +25 -0
- package/dist/persistence/outbox.js +40 -0
- package/dist/persistence/persistence.d.ts +70 -9
- package/dist/persistence/persistence.js +507 -35
- package/dist/persistence/recovery.d.ts +79 -0
- package/dist/persistence/recovery.js +79 -0
- package/dist/persistence/release.d.ts +34 -0
- package/dist/persistence/release.js +64 -0
- package/dist/persistence/state.svelte.d.ts +53 -0
- package/dist/persistence/state.svelte.js +48 -0
- package/dist/persistence/sync.d.ts +39 -0
- package/dist/persistence/sync.js +138 -0
- package/dist/persistence/tail.d.ts +42 -0
- package/dist/persistence/tail.js +103 -0
- package/dist/presets.d.ts +29 -24
- package/dist/presets.js +51 -14
- package/dist/themes/atelier.css +318 -0
- package/dist/ui/CommandChooser.svelte +153 -0
- package/dist/ui/CommandChooser.svelte.d.ts +13 -0
- package/dist/ui/CommandInvite.svelte +99 -0
- package/dist/ui/CommandInvite.svelte.d.ts +12 -0
- package/dist/ui/DragHandle.svelte +217 -0
- package/dist/ui/DragHandle.svelte.d.ts +11 -0
- package/dist/ui/FloatingToolbar.svelte +165 -0
- package/dist/ui/FloatingToolbar.svelte.d.ts +27 -0
- package/dist/ui/SlashMenu.svelte +215 -0
- package/dist/ui/SlashMenu.svelte.d.ts +33 -0
- package/dist/ui/anchor.d.ts +112 -0
- package/dist/ui/anchor.js +136 -0
- package/dist/ui/index.d.ts +10 -0
- package/dist/ui/index.js +15 -0
- package/dist/ui/slash-menu.svelte.d.ts +112 -0
- package/dist/ui/slash-menu.svelte.js +150 -0
- package/dist/utils/command-confirm.svelte.d.ts +349 -0
- package/dist/utils/command-confirm.svelte.js +673 -0
- package/dist/utils/commands.d.ts +328 -0
- package/dist/utils/commands.js +416 -0
- package/dist/utils/dom-scope.d.ts +14 -0
- package/dist/utils/dom-scope.js +20 -0
- package/dist/utils/drop.d.ts +160 -0
- package/dist/utils/drop.js +305 -0
- package/dist/utils/extensions.d.ts +488 -40
- package/dist/utils/extensions.js +300 -19
- package/dist/utils/html.d.ts +44 -0
- package/dist/utils/html.js +183 -0
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.js +4 -0
- package/dist/utils/keys.d.ts +120 -0
- package/dist/utils/keys.js +248 -0
- package/dist/utils/links.d.ts +22 -0
- package/dist/utils/links.js +34 -0
- package/dist/utils/placeholders.d.ts +88 -0
- package/dist/utils/placeholders.js +91 -0
- package/dist/utils/selection.svelte.d.ts +47 -19
- package/dist/utils/selection.svelte.js +60 -10
- package/dist/utils/spellcheck.d.ts +9 -0
- package/dist/utils/spellcheck.js +17 -0
- package/dist/utils/tab.d.ts +31 -0
- package/dist/utils/tab.js +117 -0
- package/dist/utils/words.d.ts +7 -0
- package/dist/utils/words.js +62 -0
- package/dist/vite.d.ts +22 -0
- package/dist/vite.js +34 -0
- package/package.json +61 -22
- package/dist/blocks/zone/Zone.svelte +0 -258
- package/dist/blocks/zone/Zone.svelte.d.ts +0 -11
- package/dist/blocks/zone/index.d.ts +0 -2
- package/dist/blocks/zone/index.js +0 -2
- package/dist/blocks/zone/zone.extension.d.ts +0 -2
- package/dist/blocks/zone/zone.extension.js +0 -221
- package/dist/blocks/zone/zone.svelte.d.ts +0 -112
- package/dist/blocks/zone/zone.svelte.js +0 -321
|
@@ -25,24 +25,34 @@
|
|
|
25
25
|
export class SvelteSelection {
|
|
26
26
|
/** @type {Selection?} */
|
|
27
27
|
raw: Selection | null;
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
28
|
+
/** @public */
|
|
29
|
+
public is: boolean;
|
|
30
|
+
/** @public */
|
|
31
|
+
public anchorNode: Node | null;
|
|
32
|
+
/** @public */
|
|
33
|
+
public anchorOffset: number;
|
|
34
|
+
/** @public */
|
|
35
|
+
public focusNode: Node | null;
|
|
36
|
+
/** @public */
|
|
37
|
+
public focusOffset: number;
|
|
38
|
+
/** @public */
|
|
39
|
+
public isCollapsed: boolean;
|
|
34
40
|
rangeCount: number;
|
|
35
41
|
type: string;
|
|
36
42
|
direction: "forward" | "backward" | "none";
|
|
37
43
|
startNode: Node | null;
|
|
38
44
|
endNode: Node | null;
|
|
39
|
-
|
|
40
|
-
|
|
45
|
+
/** @public */
|
|
46
|
+
public startOffset: number;
|
|
47
|
+
/** @public */
|
|
48
|
+
public endOffset: number;
|
|
41
49
|
ranges: Range[];
|
|
42
50
|
firstRange: Range;
|
|
43
|
-
|
|
51
|
+
/** @public */
|
|
52
|
+
public get range(): Range;
|
|
44
53
|
lastRange: Range;
|
|
45
|
-
|
|
54
|
+
/** @public */
|
|
55
|
+
public text: string;
|
|
46
56
|
html: string;
|
|
47
57
|
hasSelection: boolean;
|
|
48
58
|
isEmpty: boolean;
|
|
@@ -51,8 +61,26 @@ export class SvelteSelection {
|
|
|
51
61
|
allText: string;
|
|
52
62
|
allHtml: string;
|
|
53
63
|
boundingRects: DOMRect[];
|
|
64
|
+
/**
|
|
65
|
+
* Whether the live DOM selection is one this wrapper reflects. A subclass bound to a part of the page (an editor's own
|
|
66
|
+
* root) overrides it: a selection that is none of its business is NOT mirrored, the wrapper reads as "no selection".
|
|
67
|
+
* @param {Selection} raw
|
|
68
|
+
* @returns {boolean}
|
|
69
|
+
*/
|
|
70
|
+
reflects(raw: Selection): boolean;
|
|
54
71
|
refresh: () => void;
|
|
55
72
|
/**
|
|
73
|
+
* Synchronise (de façon synchrone et idempotente) l'état avec la sélection DOM courante.
|
|
74
|
+
*
|
|
75
|
+
* `selectionchange` est asynchrone (tâche mise en file) : un événement d'entrée
|
|
76
|
+
* (keydown, beforeinput…) qui suit de très près un déplacement du caret verrait
|
|
77
|
+
* un état périmé. À appeler au tout début de ces handlers. Ne touche à l'état
|
|
78
|
+
* que si les points de la sélection ont réellement changé (pas de churn réactif).
|
|
79
|
+
* @returns {boolean} True si l'état a été mis à jour
|
|
80
|
+
* @public
|
|
81
|
+
*/
|
|
82
|
+
public sync: () => boolean;
|
|
83
|
+
/**
|
|
56
84
|
* Ajoute une range à la sélection.
|
|
57
85
|
* @param {Range} range - La range à ajouter.
|
|
58
86
|
* @returns {boolean} True si l'opération a réussi
|
|
@@ -112,9 +140,9 @@ export class SvelteSelection {
|
|
|
112
140
|
* @param {Node} focusNode - Le nœud de focus.
|
|
113
141
|
* @param {number} focusOffset - L'offset de focus.
|
|
114
142
|
* @returns {boolean} True si l'opération a réussi
|
|
115
|
-
|
|
143
|
+
* @public
|
|
116
144
|
*/
|
|
117
|
-
setBaseAndExtent: (anchorNode: Node, anchorOffset: number, focusNode: Node, focusOffset: number) => boolean;
|
|
145
|
+
public setBaseAndExtent: (anchorNode: Node, anchorOffset: number, focusNode: Node, focusOffset: number) => boolean;
|
|
118
146
|
/**
|
|
119
147
|
* Vérifie si la sélection contient un nœud donné.
|
|
120
148
|
* @param {Node} node - Le nœud à vérifier.
|
|
@@ -163,15 +191,15 @@ export class SvelteSelection {
|
|
|
163
191
|
* Observe un élément contenteditable pour les mutations DOM qui peuvent
|
|
164
192
|
* affecter la position du caret sans déclencher selectionchange
|
|
165
193
|
*
|
|
166
|
-
* @param {
|
|
167
|
-
* @param {Object} options - Options d'observation
|
|
168
|
-
* @param {number} options.debounceMs - Délai de debounce en ms (défaut: 16ms = ~1 frame)
|
|
169
|
-
* @param {boolean} options.observeAttributes - Observer les changements d'attributs (défaut: false)
|
|
194
|
+
* @param {HTMLElement} element - L'élément contenteditable à observer
|
|
195
|
+
* @param {Object} [options] - Options d'observation
|
|
196
|
+
* @param {number} [options.debounceMs] - Délai de debounce en ms (défaut: 16ms = ~1 frame)
|
|
197
|
+
* @param {boolean} [options.observeAttributes] - Observer les changements d'attributs (défaut: false)
|
|
170
198
|
* @returns {boolean} True si l'observation a été configurée avec succès
|
|
171
199
|
*/
|
|
172
|
-
observe: (element:
|
|
173
|
-
debounceMs
|
|
174
|
-
observeAttributes
|
|
200
|
+
observe: (element: HTMLElement, options?: {
|
|
201
|
+
debounceMs?: number | undefined;
|
|
202
|
+
observeAttributes?: boolean | undefined;
|
|
175
203
|
}) => boolean;
|
|
176
204
|
/**
|
|
177
205
|
* Arrête l'observation d'un élément spécifique
|
|
@@ -56,16 +56,22 @@ export class SvelteSelection {
|
|
|
56
56
|
* @type {SelectionObject?}
|
|
57
57
|
*/
|
|
58
58
|
#sel = $state(null);
|
|
59
|
+
/** @public */
|
|
59
60
|
is = $derived(!!this.#sel);
|
|
60
61
|
|
|
61
62
|
/** @type {Range[]} */
|
|
62
63
|
#ranges = $state([]);
|
|
63
64
|
|
|
64
65
|
// Propriétés réactives de base
|
|
66
|
+
/** @public */
|
|
65
67
|
anchorNode = $derived(this.#sel?.anchorNode ?? null);
|
|
68
|
+
/** @public */
|
|
66
69
|
anchorOffset = $derived(this.#sel?.anchorOffset ?? 0);
|
|
70
|
+
/** @public */
|
|
67
71
|
focusNode = $derived(this.#sel?.focusNode ?? null);
|
|
72
|
+
/** @public */
|
|
68
73
|
focusOffset = $derived(this.#sel?.focusOffset ?? 0);
|
|
74
|
+
/** @public */
|
|
69
75
|
isCollapsed = $derived(this.#sel?.isCollapsed ?? true);
|
|
70
76
|
rangeCount = $derived(this.#sel?.rangeCount ?? 0);
|
|
71
77
|
type = $derived(this.#sel?.type ?? '');
|
|
@@ -73,12 +79,15 @@ export class SvelteSelection {
|
|
|
73
79
|
|
|
74
80
|
startNode = $derived(this.#sel?.startNode ?? null);
|
|
75
81
|
endNode = $derived(this.#sel?.endNode ?? null);
|
|
82
|
+
/** @public */
|
|
76
83
|
startOffset = $derived(this.#sel?.startOffset ?? 0);
|
|
84
|
+
/** @public */
|
|
77
85
|
endOffset = $derived(this.#sel?.endOffset ?? 0);
|
|
78
86
|
|
|
79
87
|
// Propriétés des ranges
|
|
80
88
|
ranges = $derived(this.#ranges);
|
|
81
89
|
firstRange = $derived(this.#ranges[0] ?? null);
|
|
90
|
+
/** @public */
|
|
82
91
|
get range() {
|
|
83
92
|
// Retourne la première range si elle existe, sinon null
|
|
84
93
|
return this.firstRange;
|
|
@@ -86,6 +95,7 @@ export class SvelteSelection {
|
|
|
86
95
|
lastRange = $derived(this.#ranges[this.#ranges.length - 1] ?? null);
|
|
87
96
|
|
|
88
97
|
// Propriétés dérivées utiles
|
|
98
|
+
/** @public */
|
|
89
99
|
text = $derived(this.#sel?.text ?? '');
|
|
90
100
|
html = $derived(this.#sel?.html ?? '');
|
|
91
101
|
hasSelection = $derived(!this.isCollapsed && this.rangeCount > 0);
|
|
@@ -132,12 +142,20 @@ export class SvelteSelection {
|
|
|
132
142
|
}).filter(rect => rect !== null);
|
|
133
143
|
});
|
|
134
144
|
|
|
145
|
+
/**
|
|
146
|
+
* Whether the live DOM selection is one this wrapper reflects. A subclass bound to a part of the page (an editor's own
|
|
147
|
+
* root) overrides it: a selection that is none of its business is NOT mirrored, the wrapper reads as "no selection".
|
|
148
|
+
* @param {Selection} raw
|
|
149
|
+
* @returns {boolean}
|
|
150
|
+
*/
|
|
151
|
+
reflects(raw) { return true; }
|
|
152
|
+
|
|
135
153
|
// Méthode privée pour mettre à jour la sélection
|
|
136
154
|
#updateSelection = () => {
|
|
137
155
|
try {
|
|
138
156
|
this.raw = window.getSelection();
|
|
139
157
|
|
|
140
|
-
if (!this.raw) {
|
|
158
|
+
if (!this.raw || !this.reflects(this.raw)) {
|
|
141
159
|
this.#sel = null;
|
|
142
160
|
this.#ranges = [];
|
|
143
161
|
return;
|
|
@@ -190,7 +208,7 @@ export class SvelteSelection {
|
|
|
190
208
|
isCollapsed: this.raw.isCollapsed,
|
|
191
209
|
rangeCount: this.raw.rangeCount,
|
|
192
210
|
type: this.raw.type,
|
|
193
|
-
direction: this.raw.direction || 'none',
|
|
211
|
+
direction: /** @type {"forward" | "backward" | "none"} */ (this.raw.direction || 'none'),
|
|
194
212
|
text,
|
|
195
213
|
html
|
|
196
214
|
};
|
|
@@ -203,6 +221,37 @@ export class SvelteSelection {
|
|
|
203
221
|
|
|
204
222
|
// Méthode pour rafraîchir la sélection manuellement
|
|
205
223
|
refresh = () => this.#updateSelection();
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Synchronise (de façon synchrone et idempotente) l'état avec la sélection DOM courante.
|
|
227
|
+
*
|
|
228
|
+
* `selectionchange` est asynchrone (tâche mise en file) : un événement d'entrée
|
|
229
|
+
* (keydown, beforeinput…) qui suit de très près un déplacement du caret verrait
|
|
230
|
+
* un état périmé. À appeler au tout début de ces handlers. Ne touche à l'état
|
|
231
|
+
* que si les points de la sélection ont réellement changé (pas de churn réactif).
|
|
232
|
+
* @returns {boolean} True si l'état a été mis à jour
|
|
233
|
+
* @public
|
|
234
|
+
*/
|
|
235
|
+
sync = () => {
|
|
236
|
+
if (typeof window === 'undefined' || !window.getSelection) return false;
|
|
237
|
+
const raw = window.getSelection();
|
|
238
|
+
const cur = this.#sel;
|
|
239
|
+
if (raw && !this.reflects(raw)) {
|
|
240
|
+
if (!cur) return false; // already "no selection"
|
|
241
|
+
} else if (!raw || raw.rangeCount === 0) {
|
|
242
|
+
if (!cur || cur.rangeCount === 0) return false;
|
|
243
|
+
} else if (cur && cur.rangeCount === raw.rangeCount && cur.type === raw.type) {
|
|
244
|
+
const range = raw.getRangeAt(0);
|
|
245
|
+
if (
|
|
246
|
+
cur.anchorNode === raw.anchorNode && cur.anchorOffset === raw.anchorOffset &&
|
|
247
|
+
cur.focusNode === raw.focusNode && cur.focusOffset === raw.focusOffset &&
|
|
248
|
+
cur.startNode === range.startContainer && cur.startOffset === range.startOffset &&
|
|
249
|
+
cur.endNode === range.endContainer && cur.endOffset === range.endOffset
|
|
250
|
+
) return false;
|
|
251
|
+
}
|
|
252
|
+
this.#updateSelection();
|
|
253
|
+
return true;
|
|
254
|
+
};
|
|
206
255
|
|
|
207
256
|
// Méthodes de manipulation de la sélection avec gestion d'erreurs
|
|
208
257
|
|
|
@@ -354,7 +403,7 @@ export class SvelteSelection {
|
|
|
354
403
|
* @param {Node} focusNode - Le nœud de focus.
|
|
355
404
|
* @param {number} focusOffset - L'offset de focus.
|
|
356
405
|
* @returns {boolean} True si l'opération a réussi
|
|
357
|
-
|
|
406
|
+
* @public
|
|
358
407
|
*/
|
|
359
408
|
setBaseAndExtent = (anchorNode, anchorOffset, focusNode, focusOffset) => {
|
|
360
409
|
try {
|
|
@@ -477,16 +526,17 @@ export class SvelteSelection {
|
|
|
477
526
|
#observedElements = new Set();
|
|
478
527
|
|
|
479
528
|
/** @type {number?} */
|
|
529
|
+
/** @type {ReturnType<typeof setTimeout> | null} */
|
|
480
530
|
#debounceTimer = null;
|
|
481
531
|
|
|
482
532
|
/**
|
|
483
533
|
* Observe un élément contenteditable pour les mutations DOM qui peuvent
|
|
484
534
|
* affecter la position du caret sans déclencher selectionchange
|
|
485
535
|
*
|
|
486
|
-
* @param {
|
|
487
|
-
* @param {Object} options - Options d'observation
|
|
488
|
-
* @param {number} options.debounceMs - Délai de debounce en ms (défaut: 16ms = ~1 frame)
|
|
489
|
-
* @param {boolean} options.observeAttributes - Observer les changements d'attributs (défaut: false)
|
|
536
|
+
* @param {HTMLElement} element - L'élément contenteditable à observer
|
|
537
|
+
* @param {Object} [options] - Options d'observation
|
|
538
|
+
* @param {number} [options.debounceMs] - Délai de debounce en ms (défaut: 16ms = ~1 frame)
|
|
539
|
+
* @param {boolean} [options.observeAttributes] - Observer les changements d'attributs (défaut: false)
|
|
490
540
|
* @returns {boolean} True si l'observation a été configurée avec succès
|
|
491
541
|
*/
|
|
492
542
|
observe = (element, options = {}) => {
|
|
@@ -510,7 +560,7 @@ export class SvelteSelection {
|
|
|
510
560
|
if (mutation.type === 'attributes') {
|
|
511
561
|
const attr = mutation.attributeName;
|
|
512
562
|
// Ne considérer que les attributs qui peuvent affecter le layout/contenu
|
|
513
|
-
return ['class', 'style', 'contenteditable'].includes(attr);
|
|
563
|
+
return ['class', 'style', 'contenteditable'].includes(/** @type {string} */ (attr));
|
|
514
564
|
}
|
|
515
565
|
|
|
516
566
|
// Toujours considérer les changements de contenu
|
|
@@ -554,7 +604,7 @@ export class SvelteSelection {
|
|
|
554
604
|
if (this.#observedElements.size === 0 && this.#mutationObserver) {
|
|
555
605
|
this.#mutationObserver.disconnect();
|
|
556
606
|
this.#mutationObserver = null;
|
|
557
|
-
clearTimeout(this.#debounceTimer);
|
|
607
|
+
clearTimeout(this.#debounceTimer ?? undefined);
|
|
558
608
|
this.#debounceTimer = null;
|
|
559
609
|
}
|
|
560
610
|
|
|
@@ -571,7 +621,7 @@ export class SvelteSelection {
|
|
|
571
621
|
}
|
|
572
622
|
|
|
573
623
|
this.#observedElements.clear();
|
|
574
|
-
clearTimeout(this.#debounceTimer);
|
|
624
|
+
clearTimeout(this.#debounceTimer ?? undefined);
|
|
575
625
|
this.#debounceTimer = null;
|
|
576
626
|
};
|
|
577
627
|
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Normalizes the `lang` option. `undefined` / `null` / `''` mean "unset" (valid, no attribute).
|
|
3
|
+
* @param {unknown} value
|
|
4
|
+
* @returns {{ lang: string | undefined, valid: boolean }}
|
|
5
|
+
*/
|
|
6
|
+
export function normalizeLang(value: unknown): {
|
|
7
|
+
lang: string | undefined;
|
|
8
|
+
valid: boolean;
|
|
9
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Editor options `spellcheck` / `lang` (see NabuInit).
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/** Loose BCP 47 shape: primary language (2-3 letters) then subtags (e.g. `fr`, `en-GB`, `zh-Hant-TW`, `es-419`). */
|
|
6
|
+
const LANG_PATTERN = /^[a-zA-Z]{2,3}(-[a-zA-Z0-9]{2,8})*$/;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Normalizes the `lang` option. `undefined` / `null` / `''` mean "unset" (valid, no attribute).
|
|
10
|
+
* @param {unknown} value
|
|
11
|
+
* @returns {{ lang: string | undefined, valid: boolean }}
|
|
12
|
+
*/
|
|
13
|
+
export function normalizeLang(value) {
|
|
14
|
+
if (value === undefined || value === null || value === '') return { lang: undefined, valid: true };
|
|
15
|
+
if (typeof value === 'string' && LANG_PATTERN.test(value)) return { lang: value, valid: true };
|
|
16
|
+
return { lang: undefined, valid: false };
|
|
17
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether `value` is an implemented mode.
|
|
3
|
+
* @param {unknown} value
|
|
4
|
+
* @returns {value is TabMode}
|
|
5
|
+
*/
|
|
6
|
+
export function isTabMode(value: unknown): value is TabMode;
|
|
7
|
+
/**
|
|
8
|
+
* Resolve the `tab` option: a valid mode wins; `undefined` gives the default silently; anything else (unknown mode,
|
|
9
|
+
* `'nest'`, a wrong type) warns and gives the default.
|
|
10
|
+
* @param {unknown} value
|
|
11
|
+
* @param {TabMode} fallback
|
|
12
|
+
* @param {(message: string) => void} [warn]
|
|
13
|
+
* @returns {TabMode}
|
|
14
|
+
*/
|
|
15
|
+
export function resolveTabMode(value: unknown, fallback: TabMode, warn?: (message: string) => void): TabMode;
|
|
16
|
+
/**
|
|
17
|
+
* Apply the `tab` option to a Tab / Shift+Tab that nothing structural handled. Call it last in the keydown chain.
|
|
18
|
+
* @param {Nabu} nabu
|
|
19
|
+
* @param {KeyboardEvent} e
|
|
20
|
+
* @param {boolean} escaped an Escape that nothing consumed was the previous key: the way out is open for this key
|
|
21
|
+
* @returns {boolean} whether the key was swallowed
|
|
22
|
+
*/
|
|
23
|
+
export function handleTabKey(nabu: Nabu, e: KeyboardEvent, escaped: boolean): boolean;
|
|
24
|
+
/** @typedef {'focus' | 'stay' | 'indent'} TabMode */
|
|
25
|
+
/** The implemented modes. */
|
|
26
|
+
export const TAB_MODES: readonly ["focus", "stay", "indent"];
|
|
27
|
+
/** Default announcement of the Escape-then-Tab way out (option `tabHint`). */
|
|
28
|
+
export const DEFAULT_TAB_HINT: "Press Escape then Tab to leave the editor";
|
|
29
|
+
export function isModifierKey(e: KeyboardEvent): boolean;
|
|
30
|
+
export type TabMode = "focus" | "stay" | "indent";
|
|
31
|
+
import type { Nabu } from '../blocks/nabu.svelte.js';
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `tab` option (P-41): what Tab / Shift+Tab do in a plain text block, i.e. when NOTHING structural took the key.
|
|
3
|
+
*
|
|
4
|
+
* Structural meanings always come first and are never affected by this option: list indent / outdent, code indentation, region
|
|
5
|
+
* title promote / demote (a command, ADR 017) and anything an extension handles in its own `onKeyDown` / `keydown`. The engine
|
|
6
|
+
* calls {@link handleTabKey} AFTER all of them, only for a key that is still not `defaultPrevented`. Agnostic: it names no block
|
|
7
|
+
* type, it only asks a block for its `text` behavior.
|
|
8
|
+
*
|
|
9
|
+
* Modes:
|
|
10
|
+
* - `'focus'`: the browser default, Tab leaves the editor (like a `<textarea>`). Nothing is prevented.
|
|
11
|
+
* - `'stay'`: Tab and Shift+Tab are swallowed, the caret stays. Escape then Tab leaves: after an Escape that nothing consumed
|
|
12
|
+
* (no slash menu, chooser or pending command), the NEXT Tab / Shift+Tab is left to the browser; any other key or a click
|
|
13
|
+
* arms the trap again. Announced to screen readers through `aria-description` on the editor root (option `tabHint`).
|
|
14
|
+
* - `'indent'`: Tab inserts a `\t` at the caret (it replaces a selection inside ONE block); Shift+Tab removes one `\t` right
|
|
15
|
+
* before the caret, if there is one, else nothing. With a selection spanning several blocks, or in a block without a text
|
|
16
|
+
* behavior or whose text is isolated (a region title), it behaves like `'stay'`. Same Escape-then-Tab way out as `'stay'`
|
|
17
|
+
* (otherwise the editor would be a keyboard trap) and the same announcement.
|
|
18
|
+
* - `'nest'` (Notion-style nesting) is planned, NOT implemented: asking for it warns and falls back to the default.
|
|
19
|
+
*
|
|
20
|
+
* @import { Nabu } from '../blocks/nabu.svelte.js'
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { tick } from 'svelte';
|
|
24
|
+
|
|
25
|
+
/** @typedef {'focus' | 'stay' | 'indent'} TabMode */
|
|
26
|
+
|
|
27
|
+
/** The implemented modes. */
|
|
28
|
+
export const TAB_MODES = /** @type {const} */ (['focus', 'stay', 'indent']);
|
|
29
|
+
|
|
30
|
+
/** Default announcement of the Escape-then-Tab way out (option `tabHint`). */
|
|
31
|
+
export const DEFAULT_TAB_HINT = 'Press Escape then Tab to leave the editor';
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Whether `value` is an implemented mode.
|
|
35
|
+
* @param {unknown} value
|
|
36
|
+
* @returns {value is TabMode}
|
|
37
|
+
*/
|
|
38
|
+
export function isTabMode(value) {
|
|
39
|
+
return typeof value === 'string' && /** @type {readonly string[]} */ (TAB_MODES).includes(value);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Resolve the `tab` option: a valid mode wins; `undefined` gives the default silently; anything else (unknown mode,
|
|
44
|
+
* `'nest'`, a wrong type) warns and gives the default.
|
|
45
|
+
* @param {unknown} value
|
|
46
|
+
* @param {TabMode} fallback
|
|
47
|
+
* @param {(message: string) => void} [warn]
|
|
48
|
+
* @returns {TabMode}
|
|
49
|
+
*/
|
|
50
|
+
export function resolveTabMode(value, fallback, warn) {
|
|
51
|
+
if (value === undefined || value === null) return fallback;
|
|
52
|
+
if (isTabMode(value)) return value;
|
|
53
|
+
warn?.(value === 'nest'
|
|
54
|
+
? `Option "tab": 'nest' is not implemented yet, using '${fallback}'`
|
|
55
|
+
: `Ignoring invalid option "tab" (${JSON.stringify(value)}; expected 'focus', 'stay' or 'indent'), using '${fallback}'`);
|
|
56
|
+
return fallback;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Keys that are only modifiers: pressing one (e.g. the Shift of Shift+Tab) is not "another key". @type {Set<string>} */
|
|
60
|
+
const MODIFIER_KEYS = new Set(['Shift', 'Control', 'Alt', 'AltGraph', 'Meta', 'OS', 'CapsLock', 'NumLock', 'ScrollLock', 'Fn', 'FnLock', 'Hyper', 'Super', 'Symbol', 'SymbolLock']);
|
|
61
|
+
|
|
62
|
+
/** @param {KeyboardEvent} e */
|
|
63
|
+
export const isModifierKey = (e) => MODIFIER_KEYS.has(e.key);
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Apply the `tab` option to a Tab / Shift+Tab that nothing structural handled. Call it last in the keydown chain.
|
|
67
|
+
* @param {Nabu} nabu
|
|
68
|
+
* @param {KeyboardEvent} e
|
|
69
|
+
* @param {boolean} escaped an Escape that nothing consumed was the previous key: the way out is open for this key
|
|
70
|
+
* @returns {boolean} whether the key was swallowed
|
|
71
|
+
*/
|
|
72
|
+
export function handleTabKey(nabu, e, escaped) {
|
|
73
|
+
if (e.key !== 'Tab' || e.ctrlKey || e.altKey || e.metaKey || e.defaultPrevented) return false;
|
|
74
|
+
if (e.isComposing || e.keyCode === 229 || nabu.composing) return false;
|
|
75
|
+
const mode = nabu.tab;
|
|
76
|
+
if (mode === 'focus' || escaped) return false; // the browser moves the focus
|
|
77
|
+
e.preventDefault();
|
|
78
|
+
if (mode === 'indent') indentWithTab(nabu, e.shiftKey);
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* @param {Nabu} nabu
|
|
84
|
+
* @param {boolean} outdent Shift+Tab
|
|
85
|
+
*/
|
|
86
|
+
function indentWithTab(nabu, outdent) {
|
|
87
|
+
const sel = nabu.selection;
|
|
88
|
+
const block = sel.anchorBlock;
|
|
89
|
+
if (!block || block !== sel.focusBlock || block.isolatesSelection) return; // several blocks, or no editable text: like 'stay'
|
|
90
|
+
const behavior = block.behaviors?.get('text');
|
|
91
|
+
const range = behavior?.selection;
|
|
92
|
+
if (!behavior || !range) return;
|
|
93
|
+
|
|
94
|
+
if (outdent) {
|
|
95
|
+
if (!range.isCollapsed || range.from <= 0 || behavior.text[range.from - 1] !== '\t') return;
|
|
96
|
+
behavior.delete({ index: range.from - 1, length: 1 });
|
|
97
|
+
nabu.storedMarks = null;
|
|
98
|
+
block.commit();
|
|
99
|
+
tick().then(() => nabu.selection.setCursor(block, range.from - 1));
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const from = range.from;
|
|
104
|
+
if (!range.isCollapsed) behavior.delete({ index: from, length: range.to - from }, range);
|
|
105
|
+
behavior.insert(from, '\t');
|
|
106
|
+
// The cursor's pending marks apply to what is typed, like any inserted character (see TextBehavior#handleInsertText).
|
|
107
|
+
const stored = nabu.storedMarks;
|
|
108
|
+
if (stored) {
|
|
109
|
+
const span = { from, to: from + 1, isCollapsed: false };
|
|
110
|
+
for (const [name, on] of Object.entries(stored)) {
|
|
111
|
+
if (on) behavior.applyMark(name, true, span);
|
|
112
|
+
else behavior.removeMark(name, span);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
block.commit();
|
|
116
|
+
tick().then(() => nabu.selection.setCursor(block, from + 1));
|
|
117
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Word counting for the text of a block (`wordCount`).
|
|
3
|
+
*
|
|
4
|
+
* Splitting on whitespace is wrong for the languages that write words without spaces (Chinese, Japanese, Thai, Khmer...): a whole
|
|
5
|
+
* sentence would count as one word. When the platform has `Intl.Segmenter` the text is segmented with `granularity: 'word'` and
|
|
6
|
+
* only the segments that are `isWordLike` (letters and digits, not spaces or punctuation) are counted, with the document
|
|
7
|
+
* language as locale (`nabu.lang`, a BCP 47 tag; the Chinese / Japanese dictionaries need it to cut the same way as the
|
|
8
|
+
* reader would). Without `Intl.Segmenter` it falls back to the whitespace split.
|
|
9
|
+
*
|
|
10
|
+
* Differences from a whitespace count, by design (they follow the Unicode word-break rules, UAX #29): punctuation and quotation
|
|
11
|
+
* marks standing alone (`«`, `»`, `-`, an em dash) are not words, an apostrophe inside a word keeps it whole (`l'homme`,
|
|
12
|
+
* `don't`), and a hyphenated compound is several words (`well-known` is two).
|
|
13
|
+
*
|
|
14
|
+
* Cost: typing re-counts only the block being edited (the count is a `$derived` of its text). To keep the first count of a big
|
|
15
|
+
* document cheap, a plain Latin word is counted without the segmenter (whitespace tokens that are letters and digits only are
|
|
16
|
+
* one word by the same rules).
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/** @type {Map<string, Intl.Segmenter | null>} */
|
|
20
|
+
const segmenters = new Map();
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The word segmenter for a locale, `null` when the platform has none. An unusable locale falls back to the default one.
|
|
24
|
+
* @param {string | undefined} lang
|
|
25
|
+
* @returns {Intl.Segmenter | null}
|
|
26
|
+
*/
|
|
27
|
+
function segmenterFor(lang) {
|
|
28
|
+
const key = lang ?? '';
|
|
29
|
+
const known = segmenters.get(key);
|
|
30
|
+
if (known !== undefined) return known;
|
|
31
|
+
/** @type {Intl.Segmenter | null} */
|
|
32
|
+
let segmenter = null;
|
|
33
|
+
if (typeof Intl !== 'undefined' && typeof Intl.Segmenter === 'function') {
|
|
34
|
+
try { segmenter = new Intl.Segmenter(lang || undefined, { granularity: 'word' }); }
|
|
35
|
+
catch { segmenter = segmenterFor(undefined); } // a locale the platform rejects
|
|
36
|
+
}
|
|
37
|
+
segmenters.set(key, segmenter);
|
|
38
|
+
return segmenter;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** One word with certainty: Latin letters (with accents) and digits only. */
|
|
42
|
+
const LATIN_WORD = /^[0-9A-Za-zÀ-ÖØ-öø-ɏ]+$/;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Number of words in a text.
|
|
46
|
+
* @param {string} text
|
|
47
|
+
* @param {string} [lang] BCP 47 tag of the text (the editor's `lang`); unset: the platform default
|
|
48
|
+
* @returns {number}
|
|
49
|
+
*/
|
|
50
|
+
export function countWords(text, lang) {
|
|
51
|
+
if (!text) return 0;
|
|
52
|
+
const tokens = text.split(/\s+/);
|
|
53
|
+
const segmenter = segmenterFor(lang);
|
|
54
|
+
if (!segmenter) return tokens.reduce((n, token) => n + (token ? 1 : 0), 0);
|
|
55
|
+
let count = 0;
|
|
56
|
+
for (const token of tokens) {
|
|
57
|
+
if (!token) continue;
|
|
58
|
+
if (LATIN_WORD.test(token)) { count++; continue; }
|
|
59
|
+
for (const part of segmenter.segment(token)) if (part.isWordLike) count++;
|
|
60
|
+
}
|
|
61
|
+
return count;
|
|
62
|
+
}
|
package/dist/vite.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Vite plugin preset for Nabu.
|
|
3
|
+
*
|
|
4
|
+
* Loro-CRDT ships a WebAssembly module loaded with top-level `await`. This
|
|
5
|
+
* helper bundles what Vite needs for it, so consumers only add one entry to
|
|
6
|
+
* their Vite config:
|
|
7
|
+
*
|
|
8
|
+
* ```js
|
|
9
|
+
* // vite.config.js
|
|
10
|
+
* import { nabu } from '@aionbuilders/nabu/vite';
|
|
11
|
+
* export default defineConfig({ plugins: [sveltekit(), nabu()] });
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* It registers `vite-plugin-wasm` and sets `build.target` to `'esnext'`
|
|
15
|
+
* (unless you already set it): modern browsers support top-level await
|
|
16
|
+
* natively, so no `vite-plugin-top-level-await` transform is needed (that
|
|
17
|
+
* plugin is incompatible with Vite 8 / Rolldown).
|
|
18
|
+
*
|
|
19
|
+
* @returns {Promise<import('vite').Plugin[]>}
|
|
20
|
+
*/
|
|
21
|
+
export function nabu(): Promise<import("vite").Plugin[]>;
|
|
22
|
+
export default nabu;
|
package/dist/vite.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Vite plugin preset for Nabu.
|
|
3
|
+
*
|
|
4
|
+
* Loro-CRDT ships a WebAssembly module loaded with top-level `await`. This
|
|
5
|
+
* helper bundles what Vite needs for it, so consumers only add one entry to
|
|
6
|
+
* their Vite config:
|
|
7
|
+
*
|
|
8
|
+
* ```js
|
|
9
|
+
* // vite.config.js
|
|
10
|
+
* import { nabu } from '@aionbuilders/nabu/vite';
|
|
11
|
+
* export default defineConfig({ plugins: [sveltekit(), nabu()] });
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* It registers `vite-plugin-wasm` and sets `build.target` to `'esnext'`
|
|
15
|
+
* (unless you already set it): modern browsers support top-level await
|
|
16
|
+
* natively, so no `vite-plugin-top-level-await` transform is needed (that
|
|
17
|
+
* plugin is incompatible with Vite 8 / Rolldown).
|
|
18
|
+
*
|
|
19
|
+
* @returns {Promise<import('vite').Plugin[]>}
|
|
20
|
+
*/
|
|
21
|
+
export async function nabu() {
|
|
22
|
+
const { default: wasm } = await import('vite-plugin-wasm');
|
|
23
|
+
return [
|
|
24
|
+
wasm(),
|
|
25
|
+
{
|
|
26
|
+
name: 'nabu:esnext-target',
|
|
27
|
+
config(config) {
|
|
28
|
+
if (config.build?.target === undefined) return { build: { target: 'esnext' } };
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export default nabu;
|