Support CKEditor 5

Created on 2 February 2022, almost 3 years ago
Updated 24 October 2023, about 1 year ago

Problem/Motivation

CKEditor5 is now standard in Drupal 10. Let's get this module compatible with it.

Proposed resolution

I had started some work at https://github.com/LukeLeber/ckeditor5-drupal-token that the project maintainers are welcome to take over / copy if they find it useful :).

I did some work on the Drupal integration side as well, but ran into some non-trivial conflicts between the token module and how the dialogs within ckeditor5 work. I don't have any of the Drupal side published, because I had to hack up the token module a good bit to make things work.

Remaining tasks

TBD

User interface changes

TBD

API changes

TBD

Data model changes

TBD

Feature request
Status

Fixed

Version

2.0

Component

Code

Created by

🇺🇸United States luke.leber Pennsylvania

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • Status changed to Needs review almost 2 years ago
  • 🇬🇧United Kingdom oliverpolden

    I've been trying this in combination with the patch for #3334784.
    The button works to show the token browser but when I click a token to insert it I get a JS popup:
    First click a text field to insert your tokens into.
    I'm definitely putting the cursor in the text area first. I do notice that the cursor seems to disappear immediately upon clicking the button but in CKEditor 4 the cursor stays flashing until the token browser appears or at least until a "Loading" popup appears at the top of the text area.

    I also get an error in the browser console:

    Uncaught TypeError: can't access property "contains", event.target.classList is undefined
        _allowInteraction ckeditor5.dialog.fix.js:10

    I don't know if this is particular to my install. Does anyone else get this issue?

  • 🇬🇧United Kingdom oliverpolden

    With further testing I've found the following:

    1. Position cursor, click button to insert token: "First click a text field to insert your tokens into."
    2. Close token browser.
    3. Position cursor, click button to insert token: Success.

    This also works if you click the token browser button, close the token browser, position the cursor again and then click the button.

    It seems initially drupalSettings.tokenFocusedCkeditor5 is undefined in token.js:Drupal.behaviors.tokenInsert, so it drops out into the last else {}:

                alert(Drupal.t('First click a text field to insert your tokens into.'));
    

    And with some debugging, I can see that the drupalSettings.tokenFocusedCkeditor5 = editor; isn't initially called:

          if (Drupal.CKEditor5Instances) {
            Drupal.CKEditor5Instances.forEach(function (editor) {
              editor.editing.view.document.on('change:isFocused', (event, data, isFocused) => {
                if (isFocused) {
                  drupalSettings.tokenFocusedCkeditor5 = editor;
                }
              });
            })
          }
    
  • Status changed to Needs work over 1 year ago
  • 🇦🇺Australia darvanen Sydney, Australia

    I take back #9, I was wrong.

    Marking NW for #10/#11.

  • 🇮🇳India bala_28 Chennai

    On page load, the token.js loads first and then comes the ckeditor5.js.
    Before the ckeditor is applied, we are trying to iterate the Drupal.CKEditor5Instances which is null by default.

    Hope the patch #8 works after changing the js order.

  • 🇦🇺Australia darvanen Sydney, Australia

    Yeah I noticed that, in the patch I'm working on (slowly) I'm looking for a way to trigger the behaviour every time a CKEditor instance is started because even loading them in that order won't fix what happens when the text_format is changed.

  • 🇮🇳India bala_28 Chennai

    This patch works for me. But I have not verified on text_format change.

  • Status changed to Needs review over 1 year ago
  • 🇦🇺Australia darvanen Sydney, Australia

    Thanks @bala_28... that looks like a patch for the token module?

    I've decided to simply set

    drupalSettings.tokenFocusedCkeditor5

    whenever the token_filter plugin button is clicked.

    If/when this passes community testing I'll release a 2.1 version for CKEditor5 compatibility and keep 2.0 until the end of the year for CKEditor4 compatibility because that's EOL for the ckeditor contrib replacement.

    I haven't tried the upgrade path on this, I'd be very grateful if someone could do that.

  • 🇮🇳India bala_28 Chennai

    Yes @darvanen. It's for token module.

    And here is the updated patch, where we included ckeditor5 under dependencies.
    This will make the ckeditor5 lib to load before the token.js and prevents the "'First click a text field to insert your tokens into.'" alert display and inserts the token into editor.

  • 🇦🇺Australia darvanen Sydney, Australia

    @bala_28 ah, I see it's almost exactly the patch from Implement Token-Click-Insert for CKEditor 5 Fixed which is called out as necessary in comment #8 here. I have been using that patch to test the work on this issue with.

  • Status changed to RTBC over 1 year ago
  • 🇳🇱Netherlands bskibinski Breda, The Netherlands

    I've tested the patches, and it seems to work well here.
    No more warning that there is no upgrade path for token_filter, and in my testing, all tokens we used were preserved when testing this in our product on multiple sites.

    I can't review the code itself, but functionally it seems to work well!

    For future testers, it was a bit confusing at first what patch applied where, if you want to test this patch you have to:
    Apply patch #17 to the drupal/token module.

    And the patch for drupal/token_filter i've attached to this comment: it's the same patch as the "MR5!" patch, but can be applied without worrying that the merge request will change in the future and perhaps breaks your site.

    Thanks for all the work everyone!

  • Status changed to Postponed over 1 year ago
  • 🇦🇺Australia darvanen Sydney, Australia

    Ok cool, I'm not going to commit this until the token module commits Implement Token-Click-Insert for CKEditor 5 Fixed so this is postponed until that happens. I'm watching that issue.

    The reason behind this is that the maintainers of token *could* decide to go a completely different direction.

    If you need this functionality, for now, use the patches.

  • 🇺🇸United States techlead

    Hi @darvanen; looks like the fix you were waiting for has been committed within the Token module.

  • Status changed to Fixed about 1 year ago
  • 🇦🇺Australia darvanen Sydney, Australia

    @techlead so it does, thank you, I just haven't had time to address this until now.

    Thanks for your work on this everyone!

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024