Suggest mode (track changes)
@nextgensoftwares/folio-plugin-suggest lets people suggest edits instead of making them. While an editor is suggesting, what the user types is kept as a suggested insertion (underlined) and what they delete stays on the page as a suggested deletion (struck through), each in the author's colour, with who and when. Someone who may edit accepts or rejects them, one at a time or all at once.
import { composePlugins } from '@nextgensoftwares/folio-editor';
import { suggestPlugin } from '@nextgensoftwares/folio-plugin-suggest';
const suggest = suggestPlugin({
user: { id: 'u42', name: 'Mona Adel' },
onRefused: (reason) => toast(reason), // optional: the status bar shows it too
});
const composed = composePlugins(standardSchema, [suggest /* , commentsPlugin(...) */]);
// Turn it on for an editor (FolioBookApp does it for `access: 'suggest'`).
suggest.setSuggesting(editor, true);The plugin contributes
- the
suggestInsertandsuggestDeletemarks (schema), - the transaction gate that turns edits into suggestions while an editor is suggesting,
- a Suggestions tab in the
sidebarslot: every suggestion in document order with its author, time and text (click to go to it), Accept / Reject per suggestion, Accept all / Reject all, - Accept suggestion / Reject suggestion in the context menu when the caret is in one,
- a Suggesting badge in the
statusbarslot, with the reason when an edit was refused, - commands
suggest.accept(id),suggest.reject(id),suggest.acceptAll(filter?),suggest.rejectAll(filter?).
Strings are in Folio's catalog, Arabic included.
In a book
Give the chapters the plugin (openChapter → plugins, or appOptions.plugins) and set access: 'suggest' (or chapterAccess). A suggest chapter opens like an editable one (beforeActivate, createSync), in suggest mode; switching between 'edit' and 'suggest' flips the live editor in place. With 'edit' the user sees the suggestions and accepts or rejects them; with 'comment' the chapter is read-only, so the panel lists them without buttons. 'view' readers see the marks drawn on the pages (the reader, canvas, DOM and PDF painters all draw them).
<FolioBookApp access={grant.level /* 'edit' | 'suggest' | 'comment' | 'view' */}
openChapter={async (id) => ({ doc, plugins: [suggest, comments], createSync })} … />suggestOnly(chapterId) is the same switch without access.
The marks
Both marks carry { id, author, authorName, time } (time in ms since the epoch), are inclusive: false and coexist with every other mark, so a suggestion keeps its formatting. They are ordinary document content: collab sends them as ordinary steps, undo takes them back, and servers that validate documents need them in their schema:
import { schema as suggestSchema } from '@nextgensoftwares/folio-plugin-suggest/schema';
const serverSchema = standardSchema.extend(suggestSchema);Keep typing next to your own suggestion and it grows (same id); a suggestion is listed once however many runs it spans.
The rules
| The user… | becomes |
|---|---|
| types or pastes text (one paragraph's worth) | a suggested insertion, in the formatting where it lands (pasted formatting isn't kept: formatting can't be suggested) |
| deletes text | a suggested deletion: the text stays, struck through; Backspace moves the caret before it, Delete after it |
| deletes their own pending insertion | it is removed for good |
| deletes across someone else's insertion | that text gets the user's deletion too (both marks: accepting the deletion removes it, rejecting it keeps the insertion) |
| deletes across an existing deletion | nothing changes there: it stays the other person's deletion |
| types over a selection | the selection struck, the new text after it |
Refused, with a message (status bar, onRefused, editor.lastRefused.reason), because a half-tracked version would lose edits:
- formatting (bold, styles, links, colours…): mark changes other than comment anchors (
passMarks), - structure: Enter (splitting a paragraph), joining paragraphs, deleting or pasting across paragraphs, lists, tables (rows, columns, cells), inserting or deleting blocks, inserting inline objects (images, equations, line breaks),
- changes in code: code blocks and inline code take no other marks, so a suggestion there couldn't be tracked.
Accepting an insertion removes its mark; accepting a deletion removes the text. Rejecting does the reverse. Accept/reject transactions are undoable and pass the gate, so a suggester can reject their own suggestions (the panel offers only that to them); the default canReview(editor) (who gets Accept) is an editable editor that isn't suggesting.
Undo
Undo and redo in suggest mode only take back (or redo) suggestions. Turning suggest mode on closes the undo group, and changes made before it, in edit mode, can't be undone or redone while suggesting: the attempt is refused with "Can't undo changes made before suggesting." Back in edit mode, undo works as usual.
Enforce it on the server
Suggest mode is enforced in the client: a modified client can send any step. For users with suggest access, the server must check what they push. ./core has the check: isSuggestionStep(step, doc, options) accepts only what a suggester's editor sends:
- inserted content that is text only, each run carrying exactly the pusher's
suggestInsertplus the formatting at the insertion point (what typing there continues). No other formatting, nosuggestDelete, no one else's marks; - removal of text that is all the pusher's own pending insertion;
- the pusher's
suggestDeleteadded or removed; passMarks(default['comment'], the comment anchors). If the mark type has an author attribute (author,authorId,userIdoruser), it must name the pusher, for adds and removals. Comment anchors carry only a thread id, so who made one can't be known: adding one is allowed, and removing one is refused unlessallowPassMarkRemoval: true. A suggester deleting a comment thread of their own then needs that option, or the host removes the anchor on the server.
The pusher's id is required: options.author, or userId in a push. Without it every step is refused, unless anyAuthor: true is passed to isSuggestionStep on purpose. suggestionPolicy(options?) applies the check to a whole push and refuses a push without userId. It is a PushPolicy for @nextgensoftwares/folio-sync/server, and the collab room's suggestions option takes the same policy:
import { suggestionPolicy } from '@nextgensoftwares/folio-plugin-suggest/core';
new CollabRoom(server, docId, { roleOf: (claimed, id, user) => roleFromSession(user), suggestions: suggestionPolicy() });
// or: rolePolicy('suggester', { suggestions: suggestionPolicy() })Accepting and rejecting other people's suggestions are ordinary edits, so they need edit access on the server.
DOCX
DOCX export writes the marks as Word tracked changes (w:ins, and w:del with w:delText), author and date included (exportDocx(doc, { suggestions: 'track' }), the default; 'accept' / 'reject' write the decided text). Import keeps Word's tracked changes as suggestions with trackedChanges: 'keep'; the default 'accept' applies them, since hosts without this plugin's marks in their schema can't hold them. Pass 'keep' when the plugin is loaded. Moves, formatting changes and table-row revisions import in their accepted state.