Comments outside the editor
The comments feature API, together with Context
, allows for creating deeper integrations with your application. One such integration is enabling comments on non-editor form fields.
In this guide, you will learn how to add this functionality to your application. Additionally, all users connected to the form will be visible in the presence list.
We highly recommend reading the Context and collaboration features guide before continuing.
For the purposes of this guide, the CKEditor Cloud Services comments adapter and the real-time collaborative comments will be used. However, the comments feature API can also be used in a similar way together with standalone comments.
# Setting up the context
Complementary to this guide, we provide a ready-to-use sample and an example of Angular integration.
You may use them as an example or as a starting point for your own integration.
The goal is to enable comments on non-editor form fields, so we will need to use the context to initialize the comments feature without using the editor.
First, make sure that your build includes Context
. You can build it together with the editor, as explained in the Context and collaboration features guide:
Even though the editor is available in the build above, to keep it simple, you will only use the context in this guide.
# Preparing the HTML structure
When your build is ready, it is time to prepare an HTML structure with an example form, a presence list, and a sidebar.
The form contains several fields as shown above. Each field has a button that allows for creating a comment attached to that field. Each field is assigned a unique ID. Also, the tabindex="-1"
attribute was added to make it possible to focus the DOM elements (and add them to the focus trackers).
# Implementing comments on form fields
The integration will meet the following requirements:
- It will be possible to add a comment thread to any form field.
- The comments should be sent, received and handled in real time.
- There can be just one comment thread on a non-editor form field.
- A button click creates a comment thread or activates an existing thread.
- There should be a visible indication that there is a comment thread on a given field.
# Adding a comment thread
To create a new comment thread attached to a form field, use CommentsRepository#openNewCommentThread()
.
# Handling new comment threads
Define a callback that will handle comment threads added to the comments repository — both created by the local user and incoming from remote users. For that, use the CommentsRepository#addCommentThread
event.
Note that the event name includes the context channel ID. Only comments “added to the context” will be handled.
When the context is initialized, there could already be some comment threads created by remote users and loaded by the CKEditor Cloud Services comments adapter. These comments need to be handled as well.
# Handling removed comment threads
You should also handle removing comment threads. To provide that, use the CommentsRepository#removeCommentThread
event. Again, note the event name.
# Highlighting an active form field
To make the UI more responsive, it is a good idea to highlight the form field corresponding to the active comment. To add this improvement, add a listener to the CommentsRepository#activeCommentThread
observable property.
# Full implementation
Below you can find the final solution.
Build source file:
The HTML structure and the integration code:
# Demo
Share the complete URL of this page with your colleagues to collaborate in real time!
Click the “plus” button to add a comment.
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.