@docx-editor.dev/vue 2.37.0 → 2.38.0-canary.20261114032659.sha-cdc64af
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/dist/index.cjs +3 -3
- package/dist/index.d.cts +1600 -201
- package/dist/index.d.ts +1600 -201
- package/dist/index.js +3 -3
- package/docs/highlight-ranges.md +361 -0
- package/docs/tracked-replacement.md +35 -4
- package/package.json +3 -3
package/docs/highlight-ranges.md
CHANGED
|
@@ -28,6 +28,365 @@ const result = useHighlights(
|
|
|
28
28
|
|
|
29
29
|
A function source runs again after each document change, so the marks follow the text. The set is removed when the component unmounts.
|
|
30
30
|
|
|
31
|
+
## Check spelling and grammar
|
|
32
|
+
|
|
33
|
+
Call `editor.getParagraphs()` to read every paragraph, including paragraphs on pages that have not been painted. The list covers body text, tables, content controls, headers, footers, notes, and supported text boxes. Repeated headers and footers appear once. To read one paragraph, call `editor.getParagraphText(blockId)`.
|
|
34
|
+
|
|
35
|
+
The list is empty until a document opens. A large document opens in steps, so read the list again after each `change` event. A function source of `useHighlights` does this for you.
|
|
36
|
+
|
|
37
|
+
### Choose the text to check
|
|
38
|
+
|
|
39
|
+
Each paragraph has three parts:
|
|
40
|
+
|
|
41
|
+
- `acceptedText` is the paragraph text with every tracked change accepted. Insertions are in, and tracked deletions and move sources are out. Hidden text and optional hyphens are left out too. Check spelling and grammar in this text.
|
|
42
|
+
- `text` holds the offsets that `setHighlights()` takes and that the caret reports. It includes deleted and hidden text.
|
|
43
|
+
- `proofing` lists stretches of `acceptedText` with their language, such as `en-GB`, and a `noProof` flag. Skip text that has `noProof` set, and check other text in its own language. East Asian characters and complex-script text have their own language tags.
|
|
44
|
+
|
|
45
|
+
In `acceptedText`, a tab reads as `\t` and a line break as `\n`. An object, such as a picture, a note reference, a symbol, or a field, reads as U+FFFC. Treat all three as word boundaries. To check the saved text of fields, read `editor.getParagraphs({ fields: 'result' })`. That read does not change how the editor addresses fields.
|
|
46
|
+
|
|
47
|
+
`acceptedText` does not depend on the review display mode. In the `original` mode, the page shows the text with every change rejected, so a mark on inserted text does not show.
|
|
48
|
+
|
|
49
|
+
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.
|
|
50
|
+
|
|
51
|
+
### Mark misspelled words
|
|
52
|
+
|
|
53
|
+
The following module, `spelling.ts`, checks each paragraph once. An unchanged paragraph returns the same object, so the cache skips it after the first check. 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.
|
|
54
|
+
|
|
55
|
+
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.
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
// spelling.ts
|
|
59
|
+
import {
|
|
60
|
+
acceptedOffsetFromText,
|
|
61
|
+
acceptedTextRanges,
|
|
62
|
+
type Editor,
|
|
63
|
+
type HighlightRange,
|
|
64
|
+
type ParagraphText,
|
|
65
|
+
} from '@docx-editor.dev/core';
|
|
66
|
+
|
|
67
|
+
/** Your checker. `lang` is the language tag of the word, or null. */
|
|
68
|
+
export interface SpellChecker {
|
|
69
|
+
isMisspelled(word: string, lang: string | null): boolean;
|
|
70
|
+
suggest(word: string, lang: string | null): readonly string[];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** One mark for one misspelled word, with what a fix needs. */
|
|
74
|
+
export type SpellingRange = HighlightRange & {
|
|
75
|
+
/** The word as `acceptedText` reads it. */
|
|
76
|
+
readonly word: string;
|
|
77
|
+
/** Where the word starts in `acceptedText`. */
|
|
78
|
+
readonly acceptedStart: number;
|
|
79
|
+
readonly suggestions: readonly string[];
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const segmenters = new Map<string, Intl.Segmenter>();
|
|
83
|
+
|
|
84
|
+
function wordSegmenter(lang: string | null): Intl.Segmenter {
|
|
85
|
+
const key = lang ?? '';
|
|
86
|
+
let segmenter = segmenters.get(key);
|
|
87
|
+
if (!segmenter) {
|
|
88
|
+
segmenter = new Intl.Segmenter(lang ?? undefined, { granularity: 'word' });
|
|
89
|
+
segmenters.set(key, segmenter);
|
|
90
|
+
}
|
|
91
|
+
return segmenter;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function check(paragraph: ParagraphText, checker: SpellChecker): SpellingRange[] {
|
|
95
|
+
const marks: SpellingRange[] = [];
|
|
96
|
+
for (const stretch of paragraph.proofing) {
|
|
97
|
+
if (stretch.noProof) continue;
|
|
98
|
+
const text = paragraph.acceptedText.slice(
|
|
99
|
+
stretch.acceptedStart,
|
|
100
|
+
stretch.acceptedStart + stretch.length
|
|
101
|
+
);
|
|
102
|
+
for (const segment of wordSegmenter(stretch.lang).segment(text)) {
|
|
103
|
+
if (!segment.isWordLike) continue;
|
|
104
|
+
const word = segment.segment;
|
|
105
|
+
const from = stretch.acceptedStart + segment.index;
|
|
106
|
+
// Look up the normalized word. The offsets stay on the original text.
|
|
107
|
+
const lookup = word.normalize('NFC');
|
|
108
|
+
if (!checker.isMisspelled(lookup, stretch.lang)) continue;
|
|
109
|
+
// One range per visible part of the word. The mark covers all of them.
|
|
110
|
+
const parts = acceptedTextRanges(paragraph, from, from + word.length);
|
|
111
|
+
const first = parts[0];
|
|
112
|
+
const last = parts.at(-1);
|
|
113
|
+
if (!first || !last) continue;
|
|
114
|
+
const end = last.start + last.length;
|
|
115
|
+
marks.push({
|
|
116
|
+
blockId: paragraph.blockId,
|
|
117
|
+
start: first.start,
|
|
118
|
+
length: end - first.start,
|
|
119
|
+
expectedText: paragraph.text.slice(first.start, end),
|
|
120
|
+
word,
|
|
121
|
+
acceptedStart: from,
|
|
122
|
+
suggestions: checker.suggest(lookup, stretch.lang),
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return marks;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* A highlight source that marks each misspelled word. While the reader types in a
|
|
131
|
+
* paragraph, it skips the word at the caret.
|
|
132
|
+
*/
|
|
133
|
+
export function spellingSource(checker: SpellChecker) {
|
|
134
|
+
const checked = new WeakMap<ParagraphText, SpellingRange[]>();
|
|
135
|
+
let seen: ParagraphText | null = null;
|
|
136
|
+
let typing = false;
|
|
137
|
+
return (editor: Editor): SpellingRange[] => {
|
|
138
|
+
const caret = editor.getSelectionRange()?.head;
|
|
139
|
+
const here = caret ? editor.getParagraphText(caret.blockId) : null;
|
|
140
|
+
// Typing starts with an edit in the caret paragraph and ends when the caret leaves it.
|
|
141
|
+
typing = here !== null && here.blockId === seen?.blockId && (typing || here !== seen);
|
|
142
|
+
seen = here;
|
|
143
|
+
return editor.getParagraphs().flatMap((paragraph) => {
|
|
144
|
+
let marks = checked.get(paragraph);
|
|
145
|
+
if (!marks) {
|
|
146
|
+
marks = check(paragraph, checker);
|
|
147
|
+
checked.set(paragraph, marks);
|
|
148
|
+
}
|
|
149
|
+
if (!typing || !caret || caret.blockId !== paragraph.blockId) return marks;
|
|
150
|
+
// The caret counts in `text`; the marks count in `acceptedText`.
|
|
151
|
+
const at = acceptedOffsetFromText(paragraph, caret.offset);
|
|
152
|
+
return marks.filter(
|
|
153
|
+
(mark) => at < mark.acceptedStart || at > mark.acceptedStart + mark.word.length
|
|
154
|
+
);
|
|
155
|
+
});
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Replace a marked word. `at` is where the mark is now, such as a context menu hit.
|
|
161
|
+
* Returns false when the command refuses the fix, for example because the text changed.
|
|
162
|
+
*/
|
|
163
|
+
export function fixWord(
|
|
164
|
+
editor: Editor,
|
|
165
|
+
mark: SpellingRange,
|
|
166
|
+
replacement: string,
|
|
167
|
+
at: { readonly start: number } = mark
|
|
168
|
+
): boolean {
|
|
169
|
+
const paragraph = editor.getParagraphText(mark.blockId);
|
|
170
|
+
if (!paragraph) return false;
|
|
171
|
+
const result = editor.exec({
|
|
172
|
+
type: 'replaceAcceptedText',
|
|
173
|
+
match: {
|
|
174
|
+
blockId: mark.blockId,
|
|
175
|
+
start: acceptedOffsetFromText(paragraph, at.start),
|
|
176
|
+
length: mark.word.length,
|
|
177
|
+
expectedText: mark.word,
|
|
178
|
+
},
|
|
179
|
+
text: replacement,
|
|
180
|
+
});
|
|
181
|
+
return result.ok;
|
|
182
|
+
}
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
`fixWord` runs the `replaceAcceptedText` command. It compares the word with the replacement and edits only the letters that differ, so the tracked changes of other authors stay where they are. The new letters go into the container of the letters they replace, such as a hyperlink or a content control. A fix of a word that a deletion splits edits each part and is one undo step. In suggesting mode, the fix is a tracked change under the editor's author.
|
|
186
|
+
|
|
187
|
+
The command refuses some fixes and changes nothing:
|
|
188
|
+
|
|
189
|
+
- If the word changed since it was checked, `expectedText` makes the command return `notFound`.
|
|
190
|
+
- If the replacement adds U+FFFC, the command returns `invalidArgs`. An object cannot be written as text.
|
|
191
|
+
- If the differing letters include an object, such as a field or a picture, the command returns `unsupported`. An object stays in place when the replacement keeps its U+FFFC at the same position.
|
|
192
|
+
- If the letters are in a locked or data-bound content control, the command returns `locked` or `bound`.
|
|
193
|
+
|
|
194
|
+
If two people fix the same word at the same time, both fixes can insert letters, so the checker marks the merged word again.
|
|
195
|
+
|
|
196
|
+
### Fix words from the context menu
|
|
197
|
+
|
|
198
|
+
The following component marks misspelled words and offers the suggestions for the word under a right-click. It runs the source again when the caret moves to another paragraph, so the word that the reader typed gets its mark.
|
|
199
|
+
|
|
200
|
+
```vue
|
|
201
|
+
<script setup lang="ts">
|
|
202
|
+
import {
|
|
203
|
+
DocxEditor,
|
|
204
|
+
useContextMenuHighlights,
|
|
205
|
+
useDocxEditor,
|
|
206
|
+
useEditorEvent,
|
|
207
|
+
useHighlights,
|
|
208
|
+
} from '@docx-editor.dev/vue';
|
|
209
|
+
import { fixWord, spellingSource, type SpellChecker, type SpellingRange } from './spelling';
|
|
210
|
+
|
|
211
|
+
// Render inside DocxEditor.Viewport. Pass a stable `checker`.
|
|
212
|
+
const props = defineProps<{ checker: SpellChecker }>();
|
|
213
|
+
const editor = useDocxEditor();
|
|
214
|
+
const marks = useHighlights('spelling', spellingSource(props.checker), {
|
|
215
|
+
className: 'spelling-error',
|
|
216
|
+
});
|
|
217
|
+
const hits = useContextMenuHighlights<SpellingRange>('spelling');
|
|
218
|
+
|
|
219
|
+
let caretParagraph: string | undefined;
|
|
220
|
+
useEditorEvent('selectionChange', (snapshot) => {
|
|
221
|
+
const blockId = snapshot.selectionRange?.head.blockId;
|
|
222
|
+
if (blockId === caretParagraph) return;
|
|
223
|
+
caretParagraph = blockId;
|
|
224
|
+
marks.value.refresh();
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
function fix(suggestion: string) {
|
|
228
|
+
const hit = hits.value[0];
|
|
229
|
+
if (!hit || !editor.value) return;
|
|
230
|
+
// The hit says where the mark is now, after edits before it.
|
|
231
|
+
if (!fixWord(editor.value, hit.range, suggestion, hit)) marks.value.refresh();
|
|
232
|
+
}
|
|
233
|
+
</script>
|
|
234
|
+
|
|
235
|
+
<template>
|
|
236
|
+
<DocxEditor.ContextMenu>
|
|
237
|
+
<DocxEditor.ContextMenu.Start>
|
|
238
|
+
<template v-if="hits[0]">
|
|
239
|
+
<DocxEditor.ContextMenu.Item
|
|
240
|
+
v-for="suggestion in hits[0].range.suggestions"
|
|
241
|
+
:key="suggestion"
|
|
242
|
+
:label="suggestion"
|
|
243
|
+
@select="fix(suggestion)"
|
|
244
|
+
/>
|
|
245
|
+
</template>
|
|
246
|
+
</DocxEditor.ContextMenu.Start>
|
|
247
|
+
</DocxEditor.ContextMenu>
|
|
248
|
+
</template>
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
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.
|
|
252
|
+
|
|
253
|
+
### Check text asynchronously
|
|
254
|
+
|
|
255
|
+
If your checker runs on a server, keep the paragraph object and the document identity with each request. The document identity is `editor.getDocumentHandle().loadId`. It is new on every `load()`, which can reuse block IDs. When the result arrives, discard it if the `loadId` changed, or if `editor.getParagraphText(paragraph.blockId)` returns another object. Undo can return an earlier object, and its earlier result is still correct.
|
|
256
|
+
|
|
257
|
+
```ts
|
|
258
|
+
import type { Editor, ParagraphText } from '@docx-editor.dev/core';
|
|
259
|
+
|
|
260
|
+
/** Check one paragraph on a server. Resolves to null when the result is stale. */
|
|
261
|
+
export async function checkRemotely(
|
|
262
|
+
editor: Editor,
|
|
263
|
+
paragraph: ParagraphText,
|
|
264
|
+
request: (text: string) => Promise<readonly { start: number; length: number }[]>
|
|
265
|
+
) {
|
|
266
|
+
const loadId = editor.getDocumentHandle().loadId;
|
|
267
|
+
const issues = await request(paragraph.acceptedText);
|
|
268
|
+
const current = editor.getParagraphText(paragraph.blockId);
|
|
269
|
+
if (editor.getDocumentHandle().loadId !== loadId || current !== paragraph) return null;
|
|
270
|
+
return issues;
|
|
271
|
+
}
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
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.
|
|
275
|
+
|
|
276
|
+
### Check terminology
|
|
277
|
+
|
|
278
|
+
To find a term, search `acceptedText`, not `editor.findMatches()`. Search finds the text that a tracked deletion removes, and it misses a term that a deletion splits. The following function marks each occurrence of a term with one mark.
|
|
279
|
+
|
|
280
|
+
```ts
|
|
281
|
+
import { acceptedTextRanges, type Editor, type HighlightRange } from '@docx-editor.dev/core';
|
|
282
|
+
|
|
283
|
+
export function termMarks(editor: Editor, term: string): HighlightRange[] {
|
|
284
|
+
const marks: HighlightRange[] = [];
|
|
285
|
+
for (const paragraph of editor.getParagraphs()) {
|
|
286
|
+
for (let at = paragraph.acceptedText.indexOf(term); at >= 0;) {
|
|
287
|
+
const parts = acceptedTextRanges(paragraph, at, at + term.length);
|
|
288
|
+
const first = parts[0];
|
|
289
|
+
const last = parts.at(-1);
|
|
290
|
+
if (first && last) {
|
|
291
|
+
const end = last.start + last.length;
|
|
292
|
+
marks.push({
|
|
293
|
+
blockId: paragraph.blockId,
|
|
294
|
+
start: first.start,
|
|
295
|
+
length: end - first.start,
|
|
296
|
+
expectedText: paragraph.text.slice(first.start, end),
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
at = paragraph.acceptedText.indexOf(term, at + term.length);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
return marks;
|
|
303
|
+
}
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
To replace a term, run `replaceAcceptedText` with the term's offset in `acceptedText` and the term as `expectedText`.
|
|
307
|
+
|
|
308
|
+
## Run the source again
|
|
309
|
+
|
|
310
|
+
A function source runs again after document changes. If it also reads your own state, call `refresh()` after that state changes. Examples are a word that you add to a dictionary, or a result that arrives from a server. The following component stops marking a term after you ignore it.
|
|
311
|
+
|
|
312
|
+
```vue
|
|
313
|
+
<script setup lang="ts">
|
|
314
|
+
import { useHighlights, type Editor } from '@docx-editor.dev/vue';
|
|
315
|
+
|
|
316
|
+
const props = defineProps<{ terms: readonly string[] }>();
|
|
317
|
+
// A plain Set is not reactive, so the source does not run again by itself.
|
|
318
|
+
const ignored = new Set<string>();
|
|
319
|
+
const marks = useHighlights('terms', (editor: Editor) =>
|
|
320
|
+
editor
|
|
321
|
+
.findMatches(props.terms, { wholeWord: true })
|
|
322
|
+
.flat()
|
|
323
|
+
.filter((match) => !ignored.has(match.text))
|
|
324
|
+
);
|
|
325
|
+
function ignore(term: string) {
|
|
326
|
+
ignored.add(term);
|
|
327
|
+
marks.value.refresh();
|
|
328
|
+
}
|
|
329
|
+
</script>
|
|
330
|
+
|
|
331
|
+
<template>
|
|
332
|
+
<button @click="ignore(props.terms[0] ?? '')">Ignore first term</button>
|
|
333
|
+
</template>
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
`refresh()` replaces a run that waits for typing to pause, so the source runs once. In React, the set updates at once, and `refresh` keeps its identity across renders. Call it from an effect or an event handler, not during render. In Vue, the set updates in the next flush, before the next paint. After the component unmounts, `refresh()` does nothing.
|
|
337
|
+
|
|
338
|
+
## Read highlights in the context menu
|
|
339
|
+
|
|
340
|
+
Call `useContextMenuHighlights(name)` in a row inside `ContextMenu.Start` to read the highlights that the menu is about. For a right-click, these are the highlights under the pointer. If the menu opens from the keyboard, with Shift+F10 or the Menu key, these are the highlights that contain the caret. Add your own fields to each range, and read them back from `range`. The following rows offer a replacement for a marked term, from a right-click or from the keyboard.
|
|
341
|
+
|
|
342
|
+
```vue
|
|
343
|
+
<script setup lang="ts">
|
|
344
|
+
import {
|
|
345
|
+
DocxEditor,
|
|
346
|
+
useContextMenuHighlights,
|
|
347
|
+
useDocxEditor,
|
|
348
|
+
type TextMatch,
|
|
349
|
+
} from '@docx-editor.dev/vue';
|
|
350
|
+
|
|
351
|
+
type TermRange = TextMatch & { readonly replacement: string };
|
|
352
|
+
|
|
353
|
+
// Pass `refresh` from the value of useHighlights('terms', ...).
|
|
354
|
+
const props = defineProps<{ recheck: () => void }>();
|
|
355
|
+
const editor = useDocxEditor();
|
|
356
|
+
const hits = useContextMenuHighlights<TermRange>('terms');
|
|
357
|
+
|
|
358
|
+
function replace() {
|
|
359
|
+
const hit = hits.value[0];
|
|
360
|
+
if (!hit || !editor.value) return;
|
|
361
|
+
const result = editor.value.exec({
|
|
362
|
+
type: 'replaceMatch',
|
|
363
|
+
// Where the term is now, and the text it must still have.
|
|
364
|
+
match: {
|
|
365
|
+
blockId: hit.range.blockId,
|
|
366
|
+
start: hit.start,
|
|
367
|
+
length: hit.length,
|
|
368
|
+
expectedText: hit.range.text,
|
|
369
|
+
},
|
|
370
|
+
text: hit.range.replacement,
|
|
371
|
+
});
|
|
372
|
+
// The text changed after the menu opened: check the document again.
|
|
373
|
+
if (!result.ok && result.code === 'notFound') props.recheck();
|
|
374
|
+
}
|
|
375
|
+
</script>
|
|
376
|
+
|
|
377
|
+
<template>
|
|
378
|
+
<DocxEditor.ContextMenu.Item
|
|
379
|
+
v-if="hits[0]"
|
|
380
|
+
:label="hits[0].range.replacement"
|
|
381
|
+
@select="replace"
|
|
382
|
+
/>
|
|
383
|
+
</template>
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
A highlight moves with its text, so use the hit's `start` and `length`, not the offsets of `range`. The hook reads the highlights again when the document changes while the menu is open. A term that moves away from the pointer or the caret is no longer reported. `expectedText` makes the replacement refuse with `notFound` if the text changed anyway.
|
|
387
|
+
|
|
388
|
+
For a keyboard open, a highlight counts when the caret is inside it or at either end, in the same paragraph. For a range selection, the caret is the end where the selection stops. The hit's `rect` is the highlight's rectangle on the caret's line. The hook returns an empty array while the menu is closed. To tell a right-click from a keyboard open, call `useContextMenuAnchor()` and check `trigger`.
|
|
389
|
+
|
|
31
390
|
## Configure a highlight set
|
|
32
391
|
|
|
33
392
|
| Argument or field | Description |
|
|
@@ -37,6 +396,8 @@ A function source runs again after each document change, so the marks follow the
|
|
|
37
396
|
| `options.className` | CSS classes added to every mark of the set. |
|
|
38
397
|
| `options.activeIndex` | The range to show in the active color, such as the current result of your own search list. |
|
|
39
398
|
| `applied` | The number of ranges that resolve and paint. |
|
|
399
|
+
| `unavailable` | The number of ranges that do not resolve or paint. While a large document lays out in the background, it includes ranges on pages that are not laid out yet, and it updates as they are. |
|
|
400
|
+
| `refresh()` | Runs the source again, or applies an array again. |
|
|
40
401
|
|
|
41
402
|
## Use highlights outside components
|
|
42
403
|
|
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
# Replace as a tracked change
|
|
4
4
|
|
|
5
|
-
Find text with `editor.findMatches` and replace a match with the `replaceMatch` command in suggesting mode. On a server, set `document.changeTrackingMode` and call `insertText(text, 'Replace')`.
|
|
5
|
+
Find text with `editor.findMatches` and replace a match with the `replaceMatch` command in suggesting mode. To replace every match, use the `replaceAllMatches` command. On a server, set `document.changeTrackingMode` and call `insertText(text, 'Replace')`.
|
|
6
6
|
|
|
7
7
|
## Replace text in the editor
|
|
8
8
|
|
|
9
|
-
The following button proposes a replacement for the first body match
|
|
9
|
+
The following button proposes a replacement for the first body match. `replaceMatch` also replaces a match in a header, footer, or note without opening that story. It refuses text-box matches, and it returns `notFound` when the matched text changed after the search. The editor needs an `author` and `reviewModule()` in its modules, or `setEditingMode('suggesting')` refuses.
|
|
10
10
|
|
|
11
11
|
```vue
|
|
12
12
|
<script setup lang="ts">
|
|
@@ -17,7 +17,7 @@ const editor = useDocxEditor();
|
|
|
17
17
|
function run() {
|
|
18
18
|
const current = editor.value;
|
|
19
19
|
if (!current || !current.setEditingMode('suggesting').ok) return;
|
|
20
|
-
// Body matches have no `scope`.
|
|
20
|
+
// Body matches have no `scope`.
|
|
21
21
|
const match = current.findMatches(props.from, { matchCase: true }).find((found) => !found.scope);
|
|
22
22
|
if (!match) return;
|
|
23
23
|
const result = current.exec({ type: 'replaceMatch', match, text: props.to });
|
|
@@ -30,7 +30,38 @@ function run() {
|
|
|
30
30
|
</template>
|
|
31
31
|
```
|
|
32
32
|
|
|
33
|
-
The replacement is a tracked change by the editor's `author`. The button prevents its `mousedown` default, so the selection and scroll position do not move.
|
|
33
|
+
The replacement is a tracked change by the editor's `author`. The button prevents its `mousedown` default, so the selection and scroll position do not move.
|
|
34
|
+
|
|
35
|
+
## Replace every match in the editor
|
|
36
|
+
|
|
37
|
+
The following button proposes a replacement for every match in the body, tables, headers, footers, notes, and text boxes. One undo step reverts all of them.
|
|
38
|
+
|
|
39
|
+
```vue
|
|
40
|
+
<script setup lang="ts">
|
|
41
|
+
import { useDocxEditor } from '@docx-editor.dev/vue';
|
|
42
|
+
|
|
43
|
+
const props = defineProps<{ from: string; to: string }>();
|
|
44
|
+
const editor = useDocxEditor();
|
|
45
|
+
function run() {
|
|
46
|
+
const current = editor.value;
|
|
47
|
+
if (!current || !current.setEditingMode('suggesting').ok) return;
|
|
48
|
+
const result = current.exec({
|
|
49
|
+
type: 'replaceAllMatches',
|
|
50
|
+
query: props.from,
|
|
51
|
+
text: props.to,
|
|
52
|
+
matchCase: true,
|
|
53
|
+
});
|
|
54
|
+
if (result.ok) console.log(`${result.replacements?.replaced ?? 0} proposed`);
|
|
55
|
+
else console.warn(result.code, result.reason);
|
|
56
|
+
}
|
|
57
|
+
</script>
|
|
58
|
+
|
|
59
|
+
<template>
|
|
60
|
+
<button @mousedown.prevent @click="run">Propose {{ to }} everywhere</button>
|
|
61
|
+
</template>
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
No story needs to be open. The command skips struck text and matches that cannot take plain text, such as field results and locked content controls, and counts them in `result.replacements.skipped`.
|
|
34
65
|
|
|
35
66
|
## Replace text on a server
|
|
36
67
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@docx-editor.dev/vue",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.38.0-canary.20261114032659.sha-cdc64af",
|
|
4
4
|
"description": "Vue 3 DOCX editor adapter for the @docx-editor.dev editor contract.",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"api:check": "node ../../scripts/api-extractor.mjs --package @docx-editor.dev/vue"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
|
-
"@docx-editor.dev/core": "
|
|
31
|
+
"@docx-editor.dev/core": "2.38.0-canary.20261114032659.sha-cdc64af",
|
|
32
32
|
"vue": "^3.3.0"
|
|
33
33
|
},
|
|
34
34
|
"peerDependenciesMeta": {
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"typescript7": "npm:typescript@^7.0.2"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@docx-editor.dev/i18n": "
|
|
46
|
+
"@docx-editor.dev/i18n": "2.38.0-canary.20261114032659.sha-cdc64af"
|
|
47
47
|
},
|
|
48
48
|
"keywords": [
|
|
49
49
|
"docx",
|