Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the limit of words/characters #2573

Open
msamsel opened this issue Jul 1, 2019 · 6 comments
Open

Implement the limit of words/characters #2573

msamsel opened this issue Jul 1, 2019 · 6 comments
Labels
package:word-count support:2 An issue reported by a commercially licensed client. type:feature This issue reports a feature request (an idea for a new functionality or a missing option).

Comments

@msamsel
Copy link
Contributor

msamsel commented Jul 1, 2019

🆕 Feature Request

There should be an option to limit words/characters in the editor:

  • Indicate the limit by changing the style of the WordCounter container
  • display limit as: Characters: NNN/MMM

If you'd like to see this fixed sooner, add a 👍 reaction to this post.

@PawanKPhogat
Copy link

I too need to set the limit of characters in ckeditor5 to satisfy backward compatibility

@mlewand mlewand transferred this issue from ckeditor/ckeditor5-word-count Oct 9, 2019
@mlewand mlewand added this to the backlog milestone Oct 9, 2019
@mlewand mlewand added status:confirmed type:feature This issue reports a feature request (an idea for a new functionality or a missing option). package:word-count labels Oct 9, 2019
@pankaarya
Copy link

@mlewand : May i know by when ckeditor5 will support limiting number of characters? ETA?

@mlewand
Copy link
Contributor

mlewand commented Jan 8, 2020

@pankaarya no particular plans regarding this feature for now. Best what you can do is to add 👍 reaction to the first post. Other option is also trying to roll out your own plugin as it shouldn't be difficult to observe word count plugin and act accordingly upon reaching the limit.

@lslowikowska lslowikowska added the support:2 An issue reported by a commercially licensed client. label Jan 18, 2021
@yangfan0095
Copy link

yangfan0095 commented Aug 17, 2021

solved like this

let oldData = '';

onUpdate: (data) => {
if (data.characters >= 4500) {
            disableInputRef.current = true;
            setShowLimitError(true);
          } else {
            disableInputRef.current = false;
            setShowLimitError(false);
             if (window.CKeditor) {
               oldData = window.CKeditor.getData();
             }
          }
...

editor.model.document.on('change:data', () => {
          if (disableInputRef.current === true) {
             window.CKeditor.setData(oldData);
          }
        });
  
    

this way can solve , but cannot control the scroll. when call setData , the content will be reset and the scroll will be Initialized to the top.

@yangfan0095
Copy link

yangfan0095 commented Sep 15, 2021

 const getUndoElement = () => {
    if (!window.CKeditor) {
      return null;
    }
    const ckButtonLabelElements = document.getElementsByClassName('ck ck-button__label');
    if (typeof ckButtonLabelElements === 'object') {
      const keys = Object.keys(ckButtonLabelElements);
      const undoKey = keys.find((key) => ckButtonLabelElements[key].textContent === 'Undo');
      if (undoKey) {
        return ckButtonLabelElements[undoKey].parentElement;
      }
    }
    return null;
  };


  const undoEdit = _.debounce(() => {
    try {
      if (
        undoButtonElementRef.current &&
        !undoButtonElementRef.current.className.includes('ck-disabled')
      ) {
        console.log('undo input from outer');
        undoButtonElementRef.current.click();
      }
    } catch (e) {
      console.log(e);
    }
  }, 1500);


 editor.model.document.on('change:data', () => {
          // 输入长度超过限制 则编辑器禁止输入, 实现方式为手动触发undo 插件执行。
          if (disableInputRef.current === true) {
            undoEdit();
          }
        });




the other way , find the undo Element ,use the undo plugin by dom click manually , it's the best way that I tried , to limit words input from time to now

  • 1 find the undo Element
  • 2 set change:data listener
  • 3 when limit trigger, click undoElement manually

@pomek pomek removed this from the backlog milestone Feb 21, 2022
@Reinmar
Copy link
Member

Reinmar commented Jul 13, 2023

I wrote this PoC of characters limit plugin: https://github.com/Reinmar/ckeditor5-maximum-length

It works similar to how Twitter handles it.

This is it in the Strapi integration:

Characters limit by highlighting excess text

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package:word-count support:2 An issue reported by a commercially licensed client. type:feature This issue reports a feature request (an idea for a new functionality or a missing option).
Projects
None yet
Development

No branches or pull requests

8 participants