Contribute to this guide

guideRead-only support

CKEditor 5 offers an out of the box read-only mode. 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 restriction, 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.

# Demo

Use the demo below to toggle between editing modes and test the feature. Some features, like exports or search, are still functional even in the read-only mode. While the search is available, the replace function, however, is disabled, as changing the content is blocked.

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%

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.

# 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 the isReadOnly 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.

# Hiding toolbar in 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' ), {
        // ...
    } )
    .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 read-only mode together with the editor’s toolbar with the dedicated button.

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%

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.