Custom view for annotations
Providing a custom view is the most powerful way to customize annotations in CKEditor 5 collaboration features.
In this case, you will need to provide the whole view: the template, UI elements and any necessary behavior logic. This gives you more control than just template modifications, as you can provide any custom view you need. However, you can still use some of the default building blocks to speed up your development.
It is highly recommended to get familiar with the Custom template for annotations guide before continuing.
# Using base views
Providing a custom view is based on the same solution as providing a custom template. You will need to create your own class for the view. In this case, you will be interested in extending base view classes:
BaseCommentThreadView
– Base view for comment thread views.BaseCommentView
– Base view for comment views.BaseSuggestionThreadView
– Base view for suggestion thread views.
Base view classes provide some core functionality that is necessary for the view to operate, no matter what the view looks like or what its template is.
Note that the default view classes also extend the base view classes.
# Default view template
The default template used to create comment thread views is shown here: CommentThreadView#getTemplate()
.
The default template used to create suggestion thread views is shown here: SuggestionThreadView#getTemplate()
.
# Creating and enabling a custom view
As a reminder, the code snippet below shows how to enable a custom view for CKEditor 5 collaboration features:
The only obligatory action that needs to be done in your custom view constructor is setting up a template:
It is your responsibility to construct the template and all the UI elements that are needed for the view.
# Reading data and binding with template
Your view is passed a model object that is available under the _model
property. It should be used to set (or bind to) the initial data of your view’s properties.
Some of your view’s properties may be used to control the view template. For example, you can bind a view property so that when it is set to true
, the template main element will receive an additional CSS class. To bind view properties with a template, the properties need to be observable. Of course, you can bind already existing observable properties with your template.
You can also bind two observable properties in a way that the value of one property will depend on the other. You can also directly listen to the changes of an observable property.
# Performing actions
The view needs to communicate with other parts of the system. When a user performs an action, something needs to be executed, for example: a comment should be removed. This communication is achieved by firing events (with appropriate data). See the example below:
The list of all events that a given view class can fire is available in the API documentation of the BaseCommentThreadView
class.
# Example: Comment thread actions dropdown
In this example you will create a custom comment thread view with action buttons (edit, remove) moved to a dropdown UI element. The dropdown will be added inside a new element, placed above the thread UI.
# Creating a custom thread view with a new template
First, create a foundation for your custom solution:
Then, you need to create a dropdown UI element and fill it with items:
Note that the dropdown should not be visible if the current local user is not the author of the thread.
Since the first comment in the comment thread represents the whole thread, you can base on the properties of the first comment.
If there are no comments in the thread, it means that this is a new thread so the local user is the author.
As far as disabling the UI is concerned, the actions in the dropdown should be disabled if the comment thread is in the read-only mode. Also, the edit button should be hidden if there are no comments in the thread.
Finally, some styling will be required for the new UI elements:
Add it in customcommentthreadview.js
:
# Linking buttons with actions
The edit button should turn the first comment into edit mode:
The delete button should remove the comment thread.
As described earlier, your view should fire events to communicate with other parts of the system:
# Altering the first comment view
Your new custom comment thread view is ready.
For comment views, you will use the default comment views. However, there is one thing you need to take care of. Since you moved comment thread controls to a separate dropdown, you should hide these buttons from the first comment view.
This modification will be added in a custom comment thread view. It should not be done in a custom comment view because that would have an impact on comments in suggestion threads.
The first comment view can be obtained from the commentsListView
property. If there are no comments yet, you can listen to the property and apply the custom behavior when the first comment view is added.
# Final solution
Below you can find the final code for the created components, together with the complete HTML code and an example of appData
.
# Live demo
Every day, we work hard to keep our documentation complete. Have you spotted an outdated information? Is something missing? Please report it via our issue tracker.
Bilingual Personality Disorder
This may be the first time you hear about this made-up disorder but it actually isn’t so far from the truth. As recent studies show, the language you speak has more effects on you than you realize. According to the studies, the language a person speaks affects their cognition, behavior, emotions and hence their personality.
This shouldn’t come as a surprise since we already know that different regions of the brain become more active depending on the activity. The structure, information and especially the culture of languages varies substantially and the language a person speaks is an essential element of daily life.
Are we sure we want to use a made-up disorder name?
This comment should have normal action buttons.
A comment by another user.