Read-only support
CKEditor 5 offers an out-of-the-box read-only mode. You can use it to prevent users under certain circumstances from editing your content.
# Demo
Use the button below to toggle the read-only mode. Some features, like exports or search, are still functional in the read-only mode. Others, like the replace function, are disabled.
The sales data for Q3 for the Click’n’go product line
Income | Revenue | |
---|---|---|
Walker 3 | $104,000 | 15% |
Stroller | $12,000 | 10% |
Runner | $97,500 | 15% |
This demo presents a limited set of features. Visit the feature-rich editor example to see more in action.
You can see that after switching to read-only mode, some of the toolbar items are still active and functional. It happens thanks to the affectsData
property. For most of the plugins, it is set to true
by default, which makes them inactive when entering read-only mode. However, for those plugins that do not make any changes in the model – do not affect the content – it is set to false
, thus allowing to still make use of them in modes with restricted user write permissions.
# Additional feature information
The feature does not require any additional plugin and the editor can be set into a read-only mode using the editor’s Editor#enableReadOnlyMode()
method.
The read-only mode may have several applications. It may be used to impose user-based access restrictions, where a selected user or a group of users is only allowed to access the content for evaluation purposes but not change it.
The feature may also be used to view content that should not be edited, like financial reports, software logs, or reprinted stories. While not editable, this content will still be accessible for copying or for screen readers.
The editor can be switched to or out of the read-only mode by many features, under various circumstances. It supports a dedicated locking mechanism for the read-only mode. This solution enables easy control over the read-only mode even when many features try to turn it on or off at the same time, without conflicting with each other. It guarantees that the user will not make the editor content editable by accident (which could lead to errors).
See also the restricted editing feature that lets you define which parts of a document can be editable for a group of users with limited editing rights, leaving the rest of the content non-editable to them. You can also read the dedicated blog post about write-restricted editor modes.
# Hiding the toolbar in the read-only mode
Some use cases might require hiding the editor toolbar when entering the read-only mode. This can be achieved easily with the following code:
ClassicEditor
.create( document.querySelector( '#editor' ), {
// The editor's configuration.
// ...
} )
.then( editor => {
const toolbarElement = editor.ui.view.toolbar.element;
editor.on( 'change:isReadOnly', ( evt, propertyName, isReadOnly ) => {
if ( isReadOnly ) {
toolbarElement.style.display = 'none';
} else {
toolbarElement.style.display = 'flex';
}
} );
} )
.catch( error => {
console.log( error );
} );
When the button is clicked, the editor.enableReadOnlyMode()
creates a lock that sets the read-only mode on the editor. This triggers the code showed above, which in turn hides the toolbar using CSS styles. After clicking the button once more, the editor.disableReadOnlyMode()
is called, which removes the read-only lock and the editor’s and the toolbar is visible again. This approach will work both for classic and decoupled editors.
Use the demo below to see this code in action. Toggle the read-only mode with the button. You will see that the toolbar disappears in the read-only mode.
The sales data for Q4 for the Click’n’go product line
Income | Income rise | |
---|---|---|
Walker 3 | $121,000 | 15% |
Stroller | $14,400 | 20% |
Runner | $100,000 | 0.5% |
# Common API
The editor provides the following API to manage the read-only mode:
- The
isReadOnly
property is a read-only, observable property that allows you to check theisReadOnly
value and react to its changes, - The
Editor#enableReadOnlyMode( featureId )
method turns on the read-only mode for the editor by creating a lock with given unique id. - The
Editor#disableReadOnlyMode( featureId )
method removes the read-only lock from the editor. The editor becomes editable when no lock is present on the editor anymore.
# Related features
There are more features that help control user permissions in the WYSIWYG editor:
- Restricted editing – Define editable areas of the document for users with restricted editing rights.
- Comments-only mode – Users can add comments to any part of the content instead of editing it directly.
# Common API
The editor provides the following API to manage the read-only mode:
- The
isReadOnly
property is a read-only, observable property that allows you to check theisReadOnly
value and react to its changes, - The
Editor#enableReadOnlyMode( featureId )
method turns on the read-only mode for the editor by creating a lock with given unique id. - The
Editor#disableReadOnlyMode( featureId )
method removes the read-only lock from the editor. The editor becomes editable when no lock is present on the editor anymore.
# Contribute
The source code of the feature is available on GitHub at https://github.com/ckeditor/ckeditor5/tree/master/packages/ckeditor5-core.
Every day, we work hard to keep our documentation complete. Have you spotted outdated information? Is something missing? Please report it via our issue tracker.
With the release of version 42.0.0, we have rewritten much of our documentation to reflect the new import paths and features. We appreciate your feedback to help us ensure its accuracy and completeness.