@docx-editor.dev/vue 2.37.0 → 2.38.0-canary.20261113032851.sha-e2cbc55

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.
@@ -28,6 +28,88 @@ 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
+ ## Run the source again
32
+
33
+ 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.
34
+
35
+ ```vue
36
+ <script setup lang="ts">
37
+ import { useHighlights, type Editor } from '@docx-editor.dev/vue';
38
+
39
+ const props = defineProps<{ terms: readonly string[] }>();
40
+ // A plain Set is not reactive, so the source does not run again by itself.
41
+ const ignored = new Set<string>();
42
+ const marks = useHighlights('terms', (editor: Editor) =>
43
+ editor
44
+ .findMatches(props.terms, { wholeWord: true })
45
+ .flat()
46
+ .filter((match) => !ignored.has(match.text))
47
+ );
48
+ function ignore(term: string) {
49
+ ignored.add(term);
50
+ marks.value.refresh();
51
+ }
52
+ </script>
53
+
54
+ <template>
55
+ <button @click="ignore(props.terms[0] ?? '')">Ignore first term</button>
56
+ </template>
57
+ ```
58
+
59
+ `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.
60
+
61
+ ## Read highlights in the context menu
62
+
63
+ 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.
64
+
65
+ ```vue
66
+ <script setup lang="ts">
67
+ import {
68
+ DocxEditor,
69
+ useContextMenuHighlights,
70
+ useDocxEditor,
71
+ type TextMatch,
72
+ } from '@docx-editor.dev/vue';
73
+
74
+ type TermRange = TextMatch & { readonly replacement: string };
75
+
76
+ // Pass `refresh` from the value of useHighlights('terms', ...).
77
+ const props = defineProps<{ recheck: () => void }>();
78
+ const editor = useDocxEditor();
79
+ const hits = useContextMenuHighlights<TermRange>('terms');
80
+
81
+ function replace() {
82
+ const hit = hits.value[0];
83
+ if (!hit || !editor.value) return;
84
+ const result = editor.value.exec({
85
+ type: 'replaceMatch',
86
+ // Where the term is now, and the text it must still have.
87
+ match: {
88
+ blockId: hit.range.blockId,
89
+ start: hit.start,
90
+ length: hit.length,
91
+ expectedText: hit.range.text,
92
+ },
93
+ text: hit.range.replacement,
94
+ });
95
+ // The text changed after the menu opened: check the document again.
96
+ if (!result.ok && result.code === 'notFound') props.recheck();
97
+ }
98
+ </script>
99
+
100
+ <template>
101
+ <DocxEditor.ContextMenu.Item
102
+ v-if="hits[0]"
103
+ :label="hits[0].range.replacement"
104
+ @select="replace"
105
+ />
106
+ </template>
107
+ ```
108
+
109
+ 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.
110
+
111
+ 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`.
112
+
31
113
  ## Configure a highlight set
32
114
 
33
115
  | Argument or field | Description |
@@ -37,6 +119,7 @@ A function source runs again after each document change, so the marks follow the
37
119
  | `options.className` | CSS classes added to every mark of the set. |
38
120
  | `options.activeIndex` | The range to show in the active color, such as the current result of your own search list. |
39
121
  | `applied` | The number of ranges that resolve and paint. |
122
+ | `refresh()` | Runs the source again, or applies an array again. |
40
123
 
41
124
  ## Use highlights outside components
42
125
 
@@ -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, so the body must be the open story. `replaceMatch` also accepts a match in the open header, footer, or note, and refuses text-box matches. The editor needs an `author` and `reviewModule()` in its modules, or `setEditingMode('suggesting')` refuses.
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`. replaceMatch needs the match's story open.
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. To replace every match in one undo step, run `{ type: 'replaceAllMatches', query, text }` through `exec`.
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.37.0",
3
+ "version": "2.38.0-canary.20261113032851.sha-e2cbc55",
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": "~2.37.0",
31
+ "@docx-editor.dev/core": "2.38.0-canary.20261113032851.sha-e2cbc55",
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": "^2.37.0"
46
+ "@docx-editor.dev/i18n": "2.38.0-canary.20261113032851.sha-e2cbc55"
47
47
  },
48
48
  "keywords": [
49
49
  "docx",