CommentsRepository (comments/comments)
@ckeditor/ckeditor5-comments/src/comments/commentsrepository
Stores the list of CommentThread
and provides event-driven API for managing them. It is also responsible for using the comments adapter
to communicate with the data source.
CommentsRepository
is a context plugin.
It can be added to a context or to an editor. Add it to the context configuration if you use
Context
in your integration.
The event-driven API makes it possible to attach a listener to each action that changes comment data.
Using different event priorities allows to attach an action before the main action ('low' priority)
or after the main action ('high' priority). It works very similar to
decorate
.
Sample usage:
// Get the comments repository:
const commentsRepository = editor.plugins.get( 'CommentsRepository' );
// Create a new, empty comment thread on a DOM form field element:
commentsRepository.openNewCommentThread( { channelId, target: formFieldElement } );
// Get all comment threads:
commentsRepository.getCommentThreads();
// Set the adapter:
commentsRepository.adapter = {
// ...
};
For more information about the comments adapter see CommentsAdapter
.
Filtering
Properties
-
activeCommentThread : CommentThread | null
readonly observable
The currently active comment thread. An annotation with this thread will be marked as active.
-
adapter : CommentsAdapter
An adapter object that should communicate with the data source to fetch or save the comments data.
-
_adapter : Object
protected
Instance of the adapter.
-
_threadToController : Map.<CommentThread, CommentThreadController>
protected
Each thread attached as an annotation to the target element has a
CommentThreadController
that createsCommentThreadView
and binds it with aCommentThread
.This map keeps connection between both of them.
-
_viewToController : Map.<CommentThreadView, CommentThreadController>
protected
Annotations
plugin uses only aCommentThreadView
and knows nothing aboutCommentThreadController
.This map helps to get a controller when only the view is known in some part of code.
Methods
-
-
addCommentThread( commentThreadData, [ data.channelId ], data.threadId, [ data.comments ], [ data.target ], [ data.isFromAdapter ] ) → CommentThread
Adds a new comment thread.
When a target is provided, the comment annotation will be attached to this target.
Use this method to load the comments data during the editor initialization if you do not use the adapter integration.
Note: This method fires the
event-addCommentThread
event and the default behavior is added as a'normal'
priority listener. It makes it possible to cancel the method or call some custom code before or after the default behavior is executed.Note: The comments adapter will send the data only if
commentThreadData.comments
is not empty andcommentThreadData.isFromAdapter
is set tofalse
.See also
CommentsRepository#openNewCommentThread()
.An example of loading a comment thread on editor initialization:
commentsRepository.addCommentThread( { threadId: 'thread-id', channelId: 'channel-id', comments: [ { commentId: 'comment-1', // String authorId: 'author-id', // String content: 'First comment', // String createdAt: new Date( ... ) // Date instance }, // ... ], target: () => ..., // Added during initialization, so do not call the adapter: isFromAdapter: true } );
Parameters
commentThreadData : Object
The data of the comment thread to add.
[ data.channelId ] : String
The ID of a document or context to which the comment thread is added.
data.threadId : String
The ID of the added comment thread.
[ data.comments ] : Array.<Object>
Comments in the comment thread. See the example above.
Defaults to
[]
[ data.target ] : HTMLElement | Rect | function
The target that the comment balloon should be attached to. If a function is passed, it should return a DOM element or
Rect
.[ data.isFromAdapter ] : Boolean
A flag describing whether the added data comes from an adapter (
true
) or is a new data (false
). If set totrue
, the comment data will be added only in the editor and will not be sent to the adapter.Defaults to
false
Returns
CommentThread
The added comment thread.
Fires
-
fetchCommentThread( data = { data.channelId, data.threadId } ) → Promise.<CommentThread>
Gets the comment thread data using the adapter and adds the thread to the editor.
When the comment thread is already present in the repository, then the adapter will not be used but the result will be asynchronous as well.
Parameters
data : Object
-
Properties
data.channelId : String
data.threadId : String
Returns
Promise.<CommentThread>
-
getCommentThread( threadId ) → CommentThread
Returns comment thread of given id.
-
getCommentThreads( [ options ] = { [options.channelId], [options.skipNotAttached], [options.skipEmpty], [options.toJSON] } ) → Array.<(CommentThread | Object)>
Returns a list of comment threads added to the repository.
You can provide a set of filtering options to narrow down the results set.
Parameters
[ options ] : Object
-
Properties
[ options.channelId ] : String
Return only comment threads added to a given channel.
[ options.skipNotAttached ] : Boolean
Skip removed comment threads.
Defaults to
false
[ options.skipEmpty ] : Boolean
Skip empty comment threads.
Defaults to
false
[ options.toJSON ] : Boolean
Return the data in JSON format.
Defaults to
false
Returns
Array.<(CommentThread | Object)>
-
hasCommentThread( threadId ) → Boolean
Checks if a comment thread with a given ID is added to the repository.
Parameters
threadId : String
Returns
Boolean
-
openNewCommentThread( commentThreadData = { [commentThreadData.channelId], [commentThreadData.threadId], commentThreadData.target } ) → CommentThread | null
Creates a new, empty comment thread.
Displays a new comment annotation attached to the target and focuses the comment editor. When the comment data is submitted, the comment thread is added to the editor and sent to the adapter.
Use this method to start a new comment thread after a user performed an action (clicked a button, etc.).
Parameters
commentThreadData : Object
-
Properties
[ commentThreadData.channelId ] : String
The ID of a document or context to which the comment is added.
[ commentThreadData.threadId ] : String
The ID of the comment thread. Random id will be generated if it is not set. All thread IDs should be unique.
commentThreadData.target : HTMLElement | Rect | function
The target that the comment balloon should be attached to. If a function is passed, it should return a DOM element or
Rect
.
Returns
CommentThread | null
The created comment thread or
null
if there was a problem creating the thread (for example, if the comments repository was in read-only mode).
Fires
-
setActiveCommentThread( threadId )
Marks a comment thread with the given ID as active. When
threadId
isnull
, the currently active comment thread will be deactivated.Parameters
threadId : String | null
-
switchReadOnly( value, [ channelId ] )
Changes the read-only state for comment threads.
When the value is
true
then all comment threads will be switched to read-only, when the value isfalse
then all comment threads will be switched to editing mode.Optionally new state can be applied to a comment threads limited to a given channel.
Parameters
value : Boolean
[ channelId ] : String
-
_addComment( data = { data.threadId, data.commentId, data.content, data.authorId, [data.createdAt], [data.attributes], [data.isFromAdapter] } )
protected
Parameters
data : Object
Data object.
Propertiesdata.threadId : String
Id of thread to which the comment will be added.
data.commentId : String
Comment id.
data.content : String
Comment content.
data.authorId : String
Author id.
[ data.createdAt ] : Date
Creation date. If not set, current date (
new Date()
) will be used.[ data.attributes ] : Object
Custom comment attributes. See also
setAttribute
andremoveAttribute
.[ data.isFromAdapter ] : Boolean
-
Defaults to
false
Related:
-
_attachCommentThread( data = { data.threadId, data.target } )
protected
Creates a
CommentThreadController
to create and bindCommentThreadView
withCommentThread
.Created view is added to
Annotations
plugin and is will be displayed by one of the Annotations UI.Parameters
data : Object
-
Properties
data.threadId : String
data.target : Object
Related:
-
_detachCommentThread( threadId )
protected
Hides comment thread instead of permanently remove.
Annotation with comment thread is removed, but
CommentThread
stays in the repository.Parameters
threadId : String
-
_removeComment( data = { data.threadId, data.commentId, [data.isFromAdapter] } )
protected
Parameters
data : Object
Data object.
Propertiesdata.threadId : String
CommentThread id.
data.commentId : String
Comment id.
[ data.isFromAdapter ] : Boolean
-
Defaults to
false
Related:
-
_removeCommentThread( threadId, isFromAdapter )
protected
-
_updateComment( data = { data.threadId, data.commentId, [data.content], [data.createdAt], [data.attributes], [data.isFromAdapter] } )
protected
Parameters
data : Object
Data object.
Propertiesdata.threadId : String
CommentThread id.
data.commentId : String
Comment id.
[ data.content ] : String
Comment content.
[ data.createdAt ] : Date
Creation date.
[ data.attributes ] : Object
Custom comment attributes. See also
setAttribute
andremoveAttribute
.[ data.isFromAdapter ] : Boolean
-
Defaults to
false
Related:
Events
-
addComment( eventInfo, data = { data.channelId, data.threadId, data.commentId, data.authorId, data.content, data.createdAt, data.isFromAdapter } )
Fired whenever a comment is added.
The event name includes
channelId
so it is possible to listen only on changes happening in the specified channel.It is also possible to listen to events from the given thread ID by appending
:[threadId]
part to the event nameconst channelId = 'foo'; const threadId = '1234'; commentsRepository.on( `addComment:${ channelId }:${ threadId }`, ( evt, data ) => { console.log( evt, data ); } );
Parameters
eventInfo : EventInfo
An object containing information about the fired event.
data : Object
-
Properties
data.channelId : String
The ID of a document or context that the comment thread is added to.
data.threadId : String
The ID of the comment thread that the comment is added to.
data.commentId : String
The comment ID.
data.authorId : String
The comment author ID.
data.content : String
The comment content.
data.createdAt : Date
The comment creation date.
data.isFromAdapter : Boolean
A flag describing whether the comment was added on a remote client (
true
) or a local one (false
).
-
addCommentThread( eventInfo, data = { data.channelId, data.threadId, data.comments, [data.target], data.isFromAdapter } )
Fired whenever a comment thread is added to the comments repository.
The event name includes
channelId
so it is possible to listen only on changes happening in the specified channel.const channelId = 'foo'; commentsRepository.on( `addCommentThread:${ channelId }`, ( evt, data ) => { console.log( evt, data ); } );
Parameters
eventInfo : EventInfo
An object containing information about the fired event.
data : Object
-
Properties
data.channelId : String
The ID of a document or context that the comment thread is added to.
data.threadId : String
The ID of the added comment thread.
data.comments : Array.<Object>
Comments in the comment thread.
[ data.target ] : HTMLElement | Rect | function
The target that the comment balloon should be attached to.
data.isFromAdapter : Boolean
A flag describing whether the comment thread was added on a remote client (
true
) or a local one (false
).
-
change:activeCommentThread( eventInfo, name, value, oldValue )
Fired when the
activeCommentThread
property changed value.Parameters
eventInfo : EventInfo
An object containing information about the fired event.
name : String
Name of the changed property (
activeCommentThread
).value : CommentThread | null
New value of the
activeCommentThread
property with given key ornull
, if operation should remove property.oldValue : CommentThread | null
Old value of the
activeCommentThread
property with given key ornull
, if property was not set before.
-
removeComment( eventInfo, data = { data.channelId, data.threadId, data.commentId, data.isFromAdapter } )
Fired whenever a comment is removed.
The event name includes
channelId
so it is possible to listen only to changes happening in the specified channel.It is also possible to listen to events from the given thread ID by appending
:[threadId]
part to the event nameconst channelId = 'foo'; const threadId = '1234'; commentsRepository.on( `removeComment:${ channelId }:${ threadId }`, ( evt, data ) => { console.log( evt, data ); } );
Parameters
eventInfo : EventInfo
An object containing information about the fired event.
data : Object
-
Properties
data.channelId : String
The ID of a document or context that the comment is removed from.
data.threadId : String
The ID of the comment thread that the comment is removed from.
data.commentId : String
The comment ID.
data.isFromAdapter : Boolean
A flag describing whether the comment was removed on a remote client (
true
) or a local one (false
).
-
removeCommentThread( eventInfo, data = { data.channelId, data.threadId, data.isFromAdapter } )
Fired whenever a comment thread is removed from the comments repository.
The event name includes
channelId
so it is possible to listen only on changes happening in the specified channel.const channelId = 'foo'; commentsRepository.on( `removeCommentThread:${ channelId }`, ( evt, data ) => { console.log( evt, data ); } );
Parameters
eventInfo : EventInfo
An object containing information about the fired event.
data : Object
-
Properties
data.channelId : String
The ID of a document or context that the comment thread was removed from.
data.threadId : String
The ID of the comment thread to remove.
data.isFromAdapter : Boolean
A flag describing whether the comment thread was removed on a remote client (
true
) or a local one (false
).
-
updateComment( eventInfo, data = { data.channelId, data.threadId, data.commentId, data.content, [data.createdAt], data.isFromAdapter } )
Fired whenever a comment is updated.
The event name includes
channelId
so it is possible to listen only to changes happening in the specified channel.It is also possible to listen to events from the given thread ID by appending
:[threadId]
part to the event nameconst channelId = 'foo'; const threadId = '1234'; commentsRepository.on( `updateComment:${ channelId }:${ threadId }`, ( evt, data ) => { console.log( evt, data ); } );
Parameters
eventInfo : EventInfo
An object containing information about the fired event.
data : Object
-
Properties
data.channelId : String
The ID of a document or context where the comment was updated.
data.threadId : String
The ID of the comment thread where the comment was updated.
data.commentId : String
The comment ID.
data.content : String
The comment content.
[ data.createdAt ] : Date
The comment creation date.
data.isFromAdapter : Boolean
A flag describing whether the comment was updated on a remote client (
true
) or a local one (false
).