Does not seem to work with CKEditor5

Created on 4 June 2023, over 1 year ago
Updated 10 September 2024, 3 months ago

Problem/Motivation

This modules does not seem to work with CKEditor 5 in Drupal 10.

Steps to reproduce

Install Drupal 10
Install Node Edit Protection Module
Create and save an article
Edit the article and make changes in the WYSIWYG form
Click away from the page, used the "View" system tab link
No JS message will appear

Proposed resolution

Modify the check so it checks the new CKEditor change or dirty status

๐Ÿ› Bug report
Status

Needs work

Version

1.0

Component

Code

Created by

๐Ÿ‡บ๐Ÿ‡ธUnited States duckydan

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @duckydan
  • Hi,
    The code below worked for me with CKEditor 5 in Drupal 9.5 when conducting a feasibility study.

    Proposed resolution
    Modify /node_edit_protection/node-edit-protection.js.
    ------
    L.62
    // Add CKEditor support.
    if (typeof (CKEDITOR) != 'undefined' && typeof (CKEDITOR.instances) != 'undefined') {
    for (var i in CKEDITOR.instances) {
    if (CKEDITOR.instances[i].checkDirty()) {
    edit = true;
    break;
    }
    }
    }

    // Add these code for CKEditor 5 support.
    var elements = document.getElementsByClassName('js-text-full text-full resize-vertical');
    if (elements) {
    for (var i in elements) {
    if (elements[i].hasAttribute) {
    var editor = Drupal.CKEditor5Instances.get(elements[i].getAttribute('data-ckeditor5-id'));
    if (editor) {
    if (elements[i].getAttribute('data-editor-value-is-changed')) {
    edit = true;
    break;
    }
    }
    }
    }
    }

  • Status changed to Needs review over 1 year ago
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia shailja179 India

    This patch will fix the issue with Drupal 10. Try this.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States duckydan

    @shailja179 That patch worked perfectly!

    Works exactly as expected now.

    Thank you so much.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia shailja179 India

    @duckydan,
    Please change the status for issue then.Its still in needs review.

  • Status changed to RTBC over 1 year ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States duckydan
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany vincent.hoehn Dresden, Germany

    vincent.hoehn โ†’ made their first commit to this issueโ€™s fork.

  • @vincenthoehn opened merge request.
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany vincent.hoehn Dresden, Germany

    Thank you for this patch. With the CKEditor5 it works well. I hope this gets merged quickly.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia anup.singh

    Patch #3 fails if you have a plain text area (In my case Revision log message)

    So updated patch to work with that.

  • Patch #10 fails in for Drupal 10.2.2 CKEditor in case you input unstyled text. By changing the query selector method from getElementsByClassName('js-text-full text-full resize-vertical'), that looks for 'js-text-full text-full resize-vertical', to querySelectorAll('.js-text-full, .text-full, .resize-vertical') which returns elements with any of these classes, works fine.

  • Patch #10 fails in for Drupal 10.2.2 CKEditor in case you input unstyled text. By changing the query selector method from getElementsByClassName('js-text-full text-full resize-vertical'), that looks for 'js-text-full text-full resize-vertical', to querySelectorAll('.js-text-full, .text-full, .resize-vertical') which returns elements with any of these classes, works fine.

  • Status changed to Needs work 10 months ago
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany donquixote

    The proposed code looks more complicated than it needs to be.
    The following check should be sufficient:

        if (document.querySelector('[data-ckeditor5-id][data-editor-value-is-changed=true]')) {
          [...] // Changes in ckeditor exist.
        }
    

    Overall the code can be cleaned up like this:

    
      const unsavedChangesExist = () => {
        if (click) {
          return false;
        }
        if (edit) {
          return true;
        }
        // Check modifications in CKEditor 4 instances.
        if (typeof CKEDITOR !== 'undefined' && CKEDITOR.instances) {
          for (const instance of CKEDITOR.instances) {
            if (instance.checkDirty()) {
              return true;
            }
          }
        }
        // Check modifications in CKEditor 5 instances.
        if (document.querySelector('[data-ckeditor5-id][data-editor-value-is-changed=true]')) {
          return true;
        }
        return false;
      };
    
      // Handle backbutton, exit etc.
      window.onbeforeunload = function() {
        if (unsavedChangesExist()) {
          // Force the warning dialog.
          return true;
        }
        // Do not show the warning dialog.
        return null;
      }
    
  • ๐Ÿ‡จ๐Ÿ‡ฆCanada metasim

    Adding support for Gin Admin Theme,

    The unsaved changes dialog shows up when the Save button is clicked.
    This only happens if the Admin Theme is set to Gin, because the Save button exists in the header region and not inside .node-form

    Updating the submit button event handling to also check for Save button under gin theme container, if that exists.

  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia acbramley
Production build 0.71.5 2024