Comment on found text
Find text in the open document and comment on it with useReview, or with Range.insertComment in a headless pipeline.
Find text with editor.findMatches, select a match with editor.selectMatch, and comment with useReview().comment. On a server, search with body.search and call Range.insertComment.
Comment on a search result
The following button comments on the first whole-word match of a term. The editor needs an author and reviewModule() in its modules.
import { useState } from 'react';
import { useDocxEditor } from '@docx-editor.dev/react';
import { useReview } from '@docx-editor.dev/pro/react';
export function CommentOnTerm({ term }: { term: string }) {
const editor = useDocxEditor();
const { comment, lastRefusal } = useReview();
const [status, setStatus] = useState('');
const run = () => {
const [match] = editor?.findMatches(term, { wholeWord: true }) ?? [];
if (!match || !editor?.selectMatch(match).ok) return setStatus('No match');
setStatus(comment(`Define "${term}".`) ? '' : 'Refused');
};
return (
<>
<button onClick={run}>Comment on {term}</button>
{/* lastRefusal updates on the next render, so read it here. */}
<span role="status">{status === 'Refused' ? lastRefusal?.code : status}</span>
</>
);
}<script setup lang="ts">
import { ref } from 'vue';
import { useDocxEditor } from '@docx-editor.dev/vue';
import { useReview } from '@docx-editor.dev/pro/vue';
const props = defineProps<{ term: string }>();
const editor = useDocxEditor();
const { comment, lastRefusal } = useReview();
const status = ref('');
function run() {
const [match] = editor.value?.findMatches(props.term, { wholeWord: true }) ?? [];
if (!match || !editor.value?.selectMatch(match).ok) {
status.value = 'No match';
return;
}
if (!comment(`Define "${props.term}".`)) status.value = lastRefusal.value?.code ?? 'Refused';
}
</script>
<template>
<button @click="run">Comment on {{ term }}</button>
<span role="status">{{ status }}</span>
</template>Comment only after selectMatch(match).ok is true. selectMatch opens the header, footer, or note that holds the match. comment returns false when the editor refuses the write, and lastRefusal holds the diagnostic code.
Comment on a server
The following example searches the body, not every story, and saves the result:
import { DocxEditor } from '@docx-editor.dev/editor-api';
const runtime = await DocxEditor.createServer(bytes, { author: 'Review bot' });
try {
await runtime.run(async (context) => {
const found = context.document.body.search(term, { matchWholeWord: true });
found.load('items');
await context.sync();
if (found.items.length === 0) return;
found.items[0]!.insertComment(`Define "${term}".`);
await context.sync();
});
const saved = await runtime.save();
} finally {
runtime.dispose();
}Load the search results before you read them, and end with await context.sync() to send the queued comment. The runtime writes the comment as its author option.
See also
Task recipes
Choose between the React and Vue review hooks and the Office.js-shaped editing API, then find the exact symbol for each common task.
Replace as a tracked change
Record a tracked replacement with findMatches and replaceMatch in the editor, or with insertText and TrackMineOnly in the editing API.