Manage comments and review edits from code
Document Authoring includes an in-editor comment UI, and it also enables you to manage comments from code. Inside a transaction, you can query, create, and resolve comment threads. You can also author edits as tracked revisions.
Before you start, make sure the Document Authoring library is installed and running in your app. If you haven’t set it up yet, refer to the getting started guide.
The examples omit error handling. Add it before you use this code in production.
Query comment threads
Use CommentThreadCollection to read comment threads from the draft document:
const currentDoc = editor.currentDocument();
await currentDoc.transaction(async ({ draft }) => { const openThreads = draft.commentThreads().all({ status: 'open' });
for (const thread of openThreads) { console.log(thread.comment.text); }
return false;});Create a comment thread
Create a text-anchored comment thread by passing a comment body and one or more text anchor ranges:
const currentDoc = editor.currentDocument();
await currentDoc.transaction(async ({ draft }) => { const paragraph = draft.body().content().addParagraph(); const textView = paragraph.asTextView();
textView.setText('The supplier must provide reviewed text before signing.');
const match = textView.searchText('reviewed text');
if (!match) { return false; }
draft.commentThreads().add({ body: 'Can Legal confirm this wording?', anchor: { type: 'text', ranges: [{ textView, range: match.range }], }, });
return true;});Manage a thread
After you query or create a thread, use CommentThread methods to manage it:
reply(body)edit(body)resolve()unresolve()remove()
await currentDoc.transaction(async ({ draft }) => { const [thread] = draft.commentThreads().all({ status: 'open' });
if (thread) { thread.reply('Checked by automated review.'); thread.resolve(); }
return true;});Author edits as tracked changes
Use TransactionOptions review to run supported mutations in review mode. Document Authoring records those mutations as tracked revisions for the supplied author:
const currentDoc = editor.currentDocument();
await currentDoc.transaction( async ({ draft }) => { const paragraph = draft.body().content().addParagraph(); paragraph.asTextView().setText('This change is authored in review mode.');
return true; }, { review: { author: 'Nutrient Docs', }, },);Supported mutations create revisions for the supplied author. Mutations without tracked-change support still apply directly. Comment-thread changes also work inside a review transaction, but they don’t create tracked-change revisions.
Learn more
Tracked changes and editor modes
Use review mode and tracked changes in the editor.
Comments and review workflows
Use the in-editor comment experience.
Programmatic editing
Learn how transactions work and how to use the document model.