Edit the document
The AI Toolkit provides primitives to edit the document in different formats.
insertText
Insert plain text into the editor.
Parameters
content
(string
): Text to insertoptions?
(InsertTextOptions
): Options for theinsertText
methodposition?
(InsertPosition
): Where to insert. One ofRange
,number
,'selection'
,'selectionStart'
,'selectionEnd'
,'document'
,'documentStart'
,'documentEnd'
. Default:'selection'
reviewOptions?
(ReviewOptions
): Control preview/review behaviormode?
('disabled' | 'review' | 'preview'
): Whether to review the changes before or after applying them.'disabled'
means no review,'review'
means review after applying,'preview'
means preview before applying. Default:'disabled'
showDiff?
(boolean
): Whether to diff the documents before and after the change to display the change as a detailed diff. Default:false
displayOptions?
(DisplayOptions<{ suggestion: Suggestion }>
): Customize how suggestions are displayedshowAsDiff?
(boolean
): Whether to show the suggestion as a diff where inserted and original content are displayed side by side. Default:true
diffPosition?
('before' | 'after'
): The position of the diff relative to the suggestion. Default:'before'
attributes?
(Record<string, any>
): Extra HTML attributes to be added to the suggestionrenderDecorations?
(RenderDecorations<{ suggestion: Suggestion }>
): A function to render the suggestion as ProseMirror decorations
Returns
void
Example
// Insert text at the current selection and show review markers
toolkit.insertText('AI content', { position: 'selection', reviewOptions: { mode: 'review' } })
insertHtml
Insert HTML into the editor.
Parameters
content
(string
): HTML to insertoptions?
(InsertHtmlOptions
): Options for theinsertHtml
methodposition?
(InsertPosition
): Where to insert. One ofRange
,number
,'selection'
,'selectionStart'
,'selectionEnd'
,'document'
,'documentStart'
,'documentEnd'
. Default:'selection'
reviewOptions?
(ReviewOptions
): Control preview/review behaviormode?
('disabled' | 'review' | 'preview'
): Whether to review the changes before or after applying them.'disabled'
means no review,'review'
means review after applying,'preview'
means preview before applying. Default:'disabled'
showDiff?
(boolean
): Whether to diff the documents before and after the change to display the change as a detailed diff. Default:false
displayOptions?
(DisplayOptions<{ suggestion: Suggestion }>
): Customize how suggestions are displayedshowAsDiff?
(boolean
): Whether to show the suggestion as a diff where inserted and original content are displayed side by side. Default:true
diffPosition?
('before' | 'after'
): The position of the diff relative to the suggestion. Default:'before'
attributes?
(Record<string, any>
): Extra HTML attributes to be added to the suggestionrenderDecorations?
(RenderDecorations<{ suggestion: Suggestion }>
): A function to render the suggestion as ProseMirror decorations
Returns
void
Example
// Insert a paragraph after the selection
toolkit.insertHtml('<p>AI paragraph</p>', { position: 'selectionEnd' })
insertJson
Insert ProseMirror JSON into the editor.
Parameters
content
(any
): ProseMirror JSON node or sliceoptions?
(InsertJsonOptions
): Options for theinsertJson
methodposition?
(InsertPosition
): Where to insert. One ofRange
,number
,'selection'
,'selectionStart'
,'selectionEnd'
,'document'
,'documentStart'
,'documentEnd'
. Default:'selection'
reviewOptions?
(ReviewOptions
): Control preview/review behaviormode?
('disabled' | 'review' | 'preview'
): Whether to review the changes before or after applying them.'disabled'
means no review,'review'
means review after applying,'preview'
means preview before applying. Default:'disabled'
showDiff?
(boolean
): Whether to diff the documents before and after the change to display the change as a detailed diff. Default:false
displayOptions?
(DisplayOptions<{ suggestion: Suggestion }>
): Customize how suggestions are displayedshowAsDiff?
(boolean
): Whether to show the suggestion as a diff where inserted and original content are displayed side by side. Default:true
diffPosition?
('before' | 'after'
): The position of the diff relative to the suggestion. Default:'before'
attributes?
(Record<string, any>
): Extra HTML attributes to be added to the suggestionrenderDecorations?
(RenderDecorations<{ suggestion: Suggestion }>
): A function to render the suggestion as ProseMirror decorations
Returns
void
Example
// Insert a paragraph node
toolkit.insertJson({ type: 'paragraph', content: [{ type: 'text', text: 'AI' }] })
applyHtmlDiff
Apply context-aware HTML diffs to a chunk or the whole document.
Parameters
diffs
(HtmlDiff[]
): Each item has:context?
(string
): Text/HTML that appears beforedelete
to anchor the changedelete
(string
): Exact HTML to removeinsert
(string
): HTML to insert
options?
(ApplyHtmlDiffOptions
): Options for theapplyHtmlDiff
methodchunkIndex?
(number
): Target chunk index. Default:0
reviewOptions?
(Omit<ReviewOptions, 'showDiff'>
): Review behavior. Note: diff previews forceshowAsDiff
in compare modemode?
('disabled' | 'review' | 'preview'
): Whether to review the changes before or after applying them.'disabled'
means no review,'review'
means review after applying,'preview'
means preview before applying. Default:'disabled'
displayOptions?
(DisplayOptions<{ suggestion: Suggestion }>
): Customize how suggestions are displayedshowAsDiff?
(boolean
): Whether to show the suggestion as a diff where inserted and original content are displayed side by side. Default:true
diffPosition?
('before' | 'after'
): The position of the diff relative to the suggestion. Default:'before'
attributes?
(Record<string, any>
): Extra HTML attributes to be added to the suggestionrenderDecorations?
(RenderDecorations<{ suggestion: Suggestion }>
): A function to render the suggestion as ProseMirror decorations
Returns (ApplyHtmlDiffResult
)
errorMessage
(string | null
):null
when all diffs applied; error message otherwise
Example
// Replace "fine" with emphasized "great" in the first chunk
toolkit.applyHtmlDiff([{ context: '<p>This is ', delete: 'fine', insert: '<em>great</em>' }])