Skip to content

Commit 05846bc

Browse files
Rearrange the annotations snippets.
1 parent bfc648b commit 05846bc

File tree

1 file changed

+49
-74
lines changed

1 file changed

+49
-74
lines changed

docs/features/server-side-editor-api.md

Lines changed: 49 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,55 @@ for ( const item of items ) {
283283
}
284284
```
285285

286+
#### Working with annotations
287+
288+
Each suggestion in the editor is always connected with an annotation. Sometimes, you may want to gather additional data for suggestions based on their annotations, such as the suggestion label or the content in the document on which the suggestion is made, which are not available in the database.
289+
290+
The following example demonstrates retrieving suggestion data:
291+
292+
```js
293+
const results = [];
294+
const trackChangesUI = editor.plugins.get( 'TrackChangesUI' );
295+
const annotations = editor.plugins.get( 'Annotations' ).collection;
296+
297+
for ( const annotation of annotations ) {
298+
// Check if this is a suggestion annotation.
299+
if ( annotation.type.startsWith( 'suggestion' ) ) {
300+
const suggestion = trackChangesUI.getSuggestionForAnnotation( annotation );
301+
const ranges = [];
302+
303+
// Gather all ranges for this suggestion (including adjacent ones).
304+
for ( const adjacentSuggestion of suggestion.getAllAdjacentSuggestions() ) {
305+
ranges.push( ...adjacentSuggestion.getRanges() );
306+
}
307+
308+
let contextHtml = '';
309+
310+
if ( ranges.length ) {
311+
const firstRange = ranges[ 0 ];
312+
const lastRange = ranges[ ranges.length - 1 ];
313+
314+
// Find the common ancestor for the whole suggestion context.
315+
const commonAncestor = firstRange.start.getCommonAncestor( lastRange.end );
316+
317+
if ( commonAncestor ) {
318+
// Stringify the entire common ancestor element as HTML, highlighting suggestions.
319+
contextHtml = editor.data.stringify( commonAncestor, { showSuggestionHighlights: true } );
320+
}
321+
}
322+
323+
results.push( {
324+
type: 'suggestion',
325+
id: suggestion.id,
326+
label: annotation.innerView.description,
327+
context: contextHtml
328+
} );
329+
}
330+
}
331+
332+
return JSON.stringify( results );
333+
```
334+
286335
### Working with comments
287336

288337
The {@link features/comments comments} feature allows your users to have discussions attached to certain parts of your documents. You can use the comments feature API to implement interactions with comments with no need to open the editor itself.
@@ -338,80 +387,6 @@ for ( const thread of threads ) {
338387

339388
This code is particularly useful when you need to clean up a document. You might use it to automatically resolve old discussions, prepare documents for publication, or maintain a clean comment history in your content management system.
340389

341-
### Working with collaboration UI elements
342-
343-
Every comment thread and suggestion is connected with an annotation that is used to display it in the editor. You can get the corresponding UI annotation for a collaboration element, or get the collaboration element for a given annotation using dedicated methods.
344-
345-
#### Comment annotations
346-
347-
The `CommentsRepository` plugin provides methods to get the corresponding annotation for a comment thread or find a comment thread for a given annotation:
348-
349-
```js
350-
const commentsRepository = editor.plugins.get( 'CommentsRepository' );
351-
352-
// Get an annotation for a comment thread.
353-
const annotation = commentsRepository.getAnnotationForCommentThread( commentThread );
354-
355-
// Get a comment thread for an annotation.
356-
const commentThread = commentsRepository.getCommentThreadForAnnotation( annotation );
357-
```
358-
359-
#### Suggestion annotations
360-
361-
The `TrackChangesUI` plugin provides methods to work with suggestion annotations in a similar manner:
362-
363-
```js
364-
const trackChangesUI = editor.plugins.get( 'TrackChangesUI' );
365-
366-
// Get an annotation for a suggestion.
367-
const annotation = trackChangesUI.getAnnotationForSuggestion( suggestion );
368-
369-
// Get a suggestion for an annotation.
370-
const suggestion = trackChangesUI.getSuggestionForAnnotation( annotation );
371-
```
372-
373-
#### Getting collaboration elements from annotations
374-
375-
Sometimes you might only have access to a collection of annotations without their corresponding elements. This is how you read the data in such case:
376-
377-
```js
378-
const results = [];
379-
const trackChangesUI = editor.plugins.get( 'TrackChangesUI' );
380-
const commentsRepository = editor.plugins.get( 'CommentsRepository' );
381-
382-
for ( const annotation of editor.plugins.get( 'Annotations' ).collection ) {
383-
if ( annotation.type == 'comment' ) {
384-
const comment = commentsRepository.getCommentThreadForAnnotation( annotation );
385-
const ranges = Array.from( editor.model.markers.getMarkersGroup( 'comment:' + comment.id ) )
386-
.map( marker => marker.getRange() );
387-
388-
results.push( {
389-
type: 'comment',
390-
id: comment.id,
391-
context: ranges,
392-
isResolved: comment.isResolved,
393-
isUnlinked: !!comment.unlinkedAt
394-
} );
395-
} else if ( annotation.type.startsWith( 'suggestion' ) ) {
396-
const suggestion = trackChangesUI.getSuggestionForAnnotation( annotation );
397-
const ranges = [];
398-
399-
for ( const adjacentSuggestion of suggestion.getAllAdjacentSuggestions() ) {
400-
ranges.push( ...adjacentSuggestion.getRanges() );
401-
}
402-
403-
results.push( {
404-
type: 'suggestion',
405-
id: suggestion.id,
406-
label: annotation.innerView.description,
407-
context: ranges
408-
} );
409-
}
410-
}
411-
412-
return JSON.stringify( results );
413-
```
414-
415390
### Working with revision history
416391

417392
Use the {@link features/revision-history revision history} feature API to build more functional integration between your application and the document revisions data.

0 commit comments

Comments
 (0)