@docx-editor.dev/pro 2.38.0-canary.20261115032819.sha-05856f6 → 2.38.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.
@@ -69,9 +69,11 @@ In `acceptedText`, a tab reads as `\t` and a line break as `\n`. An object, such
69
69
 
70
70
  A tracked change can make a word misspelled, for example a deletion of one letter in the middle of the word. If `acceptedTextRanges()` returns more than one range for a word, a tracked change or hidden text splits it. Tell the reader that the word reads this way after the changes are accepted.
71
71
 
72
+ Loading or destroying the editor in a listener cancels remaining paragraph notifications for the previous document.
73
+
72
74
  ### Mark misspelled words
73
75
 
74
- The following module, `spelling.ts`, checks every paragraph when a document opens. After that, it checks only the paragraphs that `paragraphsChange` lists. It finds words with `Intl.Segmenter` in the language of each `proofing` stretch, so `don’t` is one word. It normalizes each word to NFC before the lookup, and keeps the offsets of the original text. Each word gets one mark from the start of its first part to the end of its last part, so a word that a deletion splits shows one mark.
76
+ The following module, `spelling.ts`, checks every paragraph when a document opens. After that, it checks only paragraphs whose text object changed. It uses `Intl.Segmenter` with each proofing language to find words, including words with apostrophes. An invalid language tag uses the runtime's default language. The checker receives NFC-normalized words, but marks keep the original offsets. Each mark covers every visible part of its word.
75
77
 
76
78
  The source skips the word at the caret only while the reader types in that paragraph. It counts an edit as typing when the caret paragraph changed since the last run. When a document opens, the caret is at the start of the first paragraph, and the first word still gets its mark.
77
79
 
@@ -106,7 +108,12 @@ function wordSegmenter(lang: string | null): Intl.Segmenter {
106
108
  const key = lang ?? '';
107
109
  let segmenter = segmenters.get(key);
108
110
  if (!segmenter) {
109
- segmenter = new Intl.Segmenter(lang ?? undefined, { granularity: 'word' });
111
+ try {
112
+ segmenter = new Intl.Segmenter(lang ?? undefined, { granularity: 'word' });
113
+ } catch {
114
+ // A document can contain an invalid language tag.
115
+ segmenter = new Intl.Segmenter(undefined, { granularity: 'word' });
116
+ }
110
117
  segmenters.set(key, segmenter);
111
118
  }
112
119
  return segmenter;
@@ -152,44 +159,38 @@ function check(paragraph: ParagraphText, checker: SpellChecker): SpellingRange[]
152
159
 
153
160
  /**
154
161
  * A highlight source that marks each misspelled word. It checks every paragraph when a
155
- * document opens, and then only the paragraphs that an edit added or changed. While the
162
+ * document opens, and then only paragraphs whose text object changed. While the
156
163
  * reader types in a paragraph, it skips the word at the caret.
157
164
  */
158
165
  export function spellingSource(checker: SpellChecker) {
159
166
  const marks = new Map<string, SpellingRange[]>();
160
- // Paragraphs to check on the next run. Null means every paragraph.
161
- let pending: Set<string> | null = null;
162
- let watched: Editor | null = null;
167
+ const checked = new Map<string, ParagraphText>();
163
168
  let loadId: string | null = null;
164
169
  let seen: ParagraphText | null = null;
165
170
  let typing = false;
166
- return (editor: Editor): SpellingRange[] => {
167
- if (watched !== editor) {
168
- watched = editor;
169
- editor.on('paragraphsChange', ({ added, changed, deleted }) => {
170
- for (const id of deleted) marks.delete(id);
171
- for (const id of [...added, ...changed]) pending?.add(id);
172
- });
173
- }
171
+ const source = (editor: Editor): SpellingRange[] => {
174
172
  // A new document can reuse block IDs, so check all of it.
175
173
  const current = editor.getDocumentHandle().loadId;
176
174
  if (current !== loadId) {
177
175
  loadId = current;
178
- pending = null;
179
- }
180
- if (pending === null) {
176
+ checked.clear();
181
177
  marks.clear();
182
- for (const paragraph of editor.getParagraphs()) {
183
- marks.set(paragraph.blockId, check(paragraph, checker));
184
- }
185
- } else {
186
- for (const id of pending) {
187
- const paragraph = editor.getParagraphText(id);
188
- if (paragraph) marks.set(id, check(paragraph, checker));
189
- else marks.delete(id);
178
+ seen = null;
179
+ typing = false;
180
+ }
181
+ const paragraphs = editor.getParagraphs();
182
+ const present = new Set(paragraphs.map(({ blockId }) => blockId));
183
+ for (const id of checked.keys()) {
184
+ if (!present.has(id)) {
185
+ checked.delete(id);
186
+ marks.delete(id);
190
187
  }
191
188
  }
192
- pending = new Set();
189
+ for (const paragraph of paragraphs) {
190
+ if (checked.get(paragraph.blockId) === paragraph) continue;
191
+ checked.set(paragraph.blockId, paragraph);
192
+ marks.set(paragraph.blockId, check(paragraph, checker));
193
+ }
193
194
  const caret = editor.getSelectionRange()?.head;
194
195
  const here = caret ? editor.getParagraphText(caret.blockId) : null;
195
196
  // Typing starts with an edit in the caret paragraph and ends when the caret leaves it.
@@ -204,6 +205,12 @@ export function spellingSource(checker: SpellChecker) {
204
205
  );
205
206
  });
206
207
  };
208
+ return Object.assign(source, {
209
+ invalidate() {
210
+ checked.clear();
211
+ marks.clear();
212
+ },
213
+ });
207
214
  }
208
215
 
209
216
  /**
@@ -304,25 +311,51 @@ export function SpellingMarks({ checker }: { checker: SpellChecker }) {
304
311
 
305
312
  **Vue**
306
313
 
314
+ Save the suggestion rows in `SpellingRows.vue`. This child reads the context supplied by `ContextMenu.Start`.
315
+
307
316
  ```vue
317
+ <!-- SpellingRows.vue -->
308
318
  <script setup lang="ts">
309
- import {
310
- DocxEditor,
311
- useContextMenuHighlights,
312
- useDocxEditor,
313
- useEditorEvent,
314
- useHighlights,
315
- } from '@docx-editor.dev/vue';
316
- import { fixWord, spellingSource, type SpellChecker, type SpellingRange } from './spelling';
319
+ import { DocxEditor, useContextMenuHighlights, useDocxEditor } from '@docx-editor.dev/vue';
320
+ import { fixWord, type SpellingRange } from './spelling';
317
321
 
318
- // Render inside DocxEditor.Viewport. Pass a stable `checker`.
319
- const props = defineProps<{ checker: SpellChecker }>();
322
+ const props = defineProps<{ recheck: () => void }>();
320
323
  const editor = useDocxEditor();
321
- const marks = useHighlights('spelling', spellingSource(props.checker), {
324
+ const hits = useContextMenuHighlights<SpellingRange>('spelling');
325
+
326
+ function fix(suggestion: string) {
327
+ const hit = hits.value[0];
328
+ if (!hit || !editor.value) return;
329
+ if (!fixWord(editor.value, hit.range, suggestion, hit)) props.recheck();
330
+ }
331
+ </script>
332
+
333
+ <template>
334
+ <template v-if="hits[0]">
335
+ <DocxEditor.ContextMenu.Item
336
+ v-for="suggestion in hits[0].range.suggestions"
337
+ :key="suggestion"
338
+ :label="suggestion"
339
+ @select="fix(suggestion)"
340
+ />
341
+ </template>
342
+ </template>
343
+ ```
344
+
345
+ Render this parent inside `DocxEditor.Viewport`. Pass a stable `checker`.
346
+
347
+ ```vue
348
+ <script setup lang="ts">
349
+ import { DocxEditor, useEditorEvent, useHighlights } from '@docx-editor.dev/vue';
350
+ import { spellingSource, type SpellChecker } from './spelling';
351
+ import SpellingRows from './SpellingRows.vue';
352
+
353
+ const props = defineProps<{ checker: SpellChecker }>();
354
+ const source = spellingSource(props.checker);
355
+ const marks = useHighlights('spelling', source, {
322
356
  kind: 'spelling',
323
357
  className: 'spelling-error',
324
358
  });
325
- const hits = useContextMenuHighlights<SpellingRange>('spelling');
326
359
 
327
360
  let caretParagraph: string | undefined;
328
361
  useEditorEvent('selectionChange', (snapshot) => {
@@ -331,26 +364,12 @@ useEditorEvent('selectionChange', (snapshot) => {
331
364
  caretParagraph = blockId;
332
365
  marks.value.refresh();
333
366
  });
334
-
335
- function fix(suggestion: string) {
336
- const hit = hits.value[0];
337
- if (!hit || !editor.value) return;
338
- // The hit says where the mark is now, after edits before it.
339
- if (!fixWord(editor.value, hit.range, suggestion, hit)) marks.value.refresh();
340
- }
341
367
  </script>
342
368
 
343
369
  <template>
344
370
  <DocxEditor.ContextMenu>
345
371
  <DocxEditor.ContextMenu.Start>
346
- <template v-if="hits[0]">
347
- <DocxEditor.ContextMenu.Item
348
- v-for="suggestion in hits[0].range.suggestions"
349
- :key="suggestion"
350
- :label="suggestion"
351
- @select="fix(suggestion)"
352
- />
353
- </template>
372
+ <SpellingRows :recheck="marks.refresh" />
354
373
  </DocxEditor.ContextMenu.Start>
355
374
  </DocxEditor.ContextMenu>
356
375
  </template>
@@ -358,6 +377,8 @@ function fix(suggestion: string) {
358
377
 
359
378
  To fix words from a side list, keep the marks that the source returned, and call `fixWord(editor, mark, suggestion)` from the list. Build the list again after each `change` event. If `fixWord` returns false, the word changed, so check the document again.
360
379
 
380
+ If your dictionary changes, call `source.invalidate()`, then `refresh()` in React or `marks.value.refresh()` in Vue. This clears cached checks before the highlight source runs again.
381
+
361
382
  ### Make marks accessible
362
383
 
363
384
  Marks are painted over the page, so assistive technology does not see them by default. Pass `kind: 'spelling'` or `kind: 'grammar'` in the options of a proofing set:
@@ -410,8 +431,10 @@ import { checkRemotely } from './check-remotely';
410
431
  export function watchGrammar(
411
432
  editor: Editor,
412
433
  request: (text: string) => Promise<readonly { start: number; length: number }[]>,
413
- report: (ranges: readonly HighlightRange[]) => void
434
+ report: (ranges: readonly HighlightRange[]) => void,
435
+ onError: (error: unknown) => void
414
436
  ): () => void {
437
+ let stopped = false;
415
438
  const results = new Map<string, readonly HighlightRange[]>();
416
439
  const publish = () => report([...results.values()].flat());
417
440
  const check = async (blockId: string) => {
@@ -419,7 +442,7 @@ export function watchGrammar(
419
442
  if (!paragraph) return;
420
443
  const issues = await checkRemotely(editor, paragraph, request);
421
444
  // Null: the paragraph changed, and a newer check of it is on its way.
422
- if (!issues) return;
445
+ if (stopped || !issues) return;
423
446
  results.set(
424
447
  blockId,
425
448
  issues.flatMap(({ start, length }) =>
@@ -431,16 +454,27 @@ export function watchGrammar(
431
454
  );
432
455
  publish();
433
456
  };
434
- for (const paragraph of editor.getParagraphs()) void check(paragraph.blockId);
435
- return editor.on('paragraphsChange', ({ added, changed, deleted }) => {
436
- for (const id of deleted) results.delete(id);
437
- if (deleted.length > 0) publish();
438
- for (const id of [...added, ...changed]) void check(id);
457
+ const schedule = (id: string) => {
458
+ void check(id).catch((error: unknown) => {
459
+ if (!stopped) onError(error);
460
+ });
461
+ };
462
+ for (const paragraph of editor.getParagraphs()) schedule(paragraph.blockId);
463
+ const off = editor.on('paragraphsChange', ({ added, changed, deleted }) => {
464
+ for (const id of [...changed, ...deleted]) results.delete(id);
465
+ publish();
466
+ for (const id of [...added, ...changed]) schedule(id);
439
467
  });
468
+ return () => {
469
+ stopped = true;
470
+ off();
471
+ results.clear();
472
+ report([]);
473
+ };
440
474
  }
441
475
  ```
442
476
 
443
- Pass a `report` function that calls `editor.setHighlights('grammar', ranges, { kind: 'grammar' })`. After a new `load()`, stop the watch and start a new one.
477
+ Pass a `report` function that calls `editor.setHighlights('grammar', ranges, { kind: 'grammar' })`. Pass `onError` to report request failures in your application. Call the returned function on unmount to remove marks and ignore pending answers. After a new `load()`, stop the watch and start a new one.
444
478
 
445
479
  A result can still go stale between the check and a fix. Pass the checked text as `expectedText` to `replaceAcceptedText`, so a stale fix returns `notFound` and changes nothing.
446
480
 
@@ -453,6 +487,7 @@ import { acceptedTextRanges, type Editor, type HighlightRange } from '@docx-edit
453
487
 
454
488
  export function termMarks(editor: Editor, term: string): HighlightRange[] {
455
489
  const marks: HighlightRange[] = [];
490
+ if (term.length === 0) return marks;
456
491
  for (const paragraph of editor.getParagraphs()) {
457
492
  for (let at = paragraph.acceptedText.indexOf(term); at >= 0;) {
458
493
  const parts = acceptedTextRanges(paragraph, at, at + term.length);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@docx-editor.dev/pro",
3
- "version": "2.38.0-canary.20261115032819.sha-05856f6",
3
+ "version": "2.38.0",
4
4
  "description": "Commercial capabilities for @docx-editor.dev: the review module (comments, tracked changes, markup rendering), custom inline nodes, realtime collaboration, and advanced drawing rendering.",
5
5
  "sideEffects": false,
6
6
  "type": "module",
@@ -80,9 +80,9 @@
80
80
  "license:add": "license-check-and-add add -f license-check.json"
81
81
  },
82
82
  "peerDependencies": {
83
- "@docx-editor.dev/core": "2.38.0-canary.20261115032819.sha-05856f6",
84
- "@docx-editor.dev/react": "2.38.0-canary.20261115032819.sha-05856f6",
85
- "@docx-editor.dev/vue": "2.38.0-canary.20261115032819.sha-05856f6",
83
+ "@docx-editor.dev/core": "~2.38.0",
84
+ "@docx-editor.dev/react": "~2.38.0",
85
+ "@docx-editor.dev/vue": "~2.38.0",
86
86
  "react": "^18.0.0 || ^19.0.0",
87
87
  "react-dom": "^18.0.0 || ^19.0.0",
88
88
  "vue": "^3.3.0",
@@ -1,6 +0,0 @@
1
- /*!
2
- Copyright (c) 2026 Eigenpal, Inc. All rights reserved.
3
- Licensed under the EigenPal Pro Evaluation License 1.0. See LICENSE.md in this package.
4
- Production use requires a commercial agreement: licensing@eigenpal.com
5
- */
6
- 'use strict';var store=require('@docx-editor.dev/core/store');async function d(a,g,i){let{convertMetafileToDataUrl:l,getMetafileInfo:s}=await import('emf-converter'),r=await s(a.slice()),n=await l(a.slice(),{dpiScale:store.metafileRasterScale(Math.max(r?.width??0,r?.height??0)),maxWidth:i.maxEdge,maxHeight:i.maxEdge}),m="data:image/png;base64,";if(!n?.startsWith(m))return null;let t=atob(n.slice(m.length)),o=new Uint8Array(t.length);for(let e=0;e<t.length;e+=1)o[e]=t.charCodeAt(e);return Object.freeze({bytes:o,mime:"image/png"})}exports.rasterizeMetafile=d;
@@ -1,6 +0,0 @@
1
- /*!
2
- Copyright (c) 2026 Eigenpal, Inc. All rights reserved.
3
- Licensed under the EigenPal Pro Evaluation License 1.0. See LICENSE.md in this package.
4
- Production use requires a commercial agreement: licensing@eigenpal.com
5
- */
6
- import {metafileRasterScale}from'@docx-editor.dev/core/store';async function d(a,g,i){let{convertMetafileToDataUrl:l,getMetafileInfo:s}=await import('emf-converter'),r=await s(a.slice()),n=await l(a.slice(),{dpiScale:metafileRasterScale(Math.max(r?.width??0,r?.height??0)),maxWidth:i.maxEdge,maxHeight:i.maxEdge}),m="data:image/png;base64,";if(!n?.startsWith(m))return null;let t=atob(n.slice(m.length)),o=new Uint8Array(t.length);for(let e=0;e<t.length;e+=1)o[e]=t.charCodeAt(e);return Object.freeze({bytes:o,mime:"image/png"})}export{d as rasterizeMetafile};