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

Limit number of characters in editor #293

Closed
kskrlin opened this issue May 6, 2019 · 7 comments
Closed

Limit number of characters in editor #293

kskrlin opened this issue May 6, 2019 · 7 comments
Labels
Type: Feature The issue or pullrequest is a new feature

Comments

@kskrlin
Copy link

kskrlin commented May 6, 2019

Is there any example/solution/feature/extension within this editor to limit number of characters?

For example, textarea has maxlength;
TinyMCE editor has examples how to make it.

@kskrlin kskrlin added the Type: Feature The issue or pullrequest is a new feature label May 6, 2019
@Alecyrus
Copy link
Sponsor Contributor

Alecyrus commented May 7, 2019

Write a plugin to count characters, see this, or just get the doc.contentSize

@Chrissi2812
Copy link
Contributor

I've written a basic demo https://codesandbox.io/s/3204l3on1q

PS. Deletion doesn't quite work at the moment (Maybe someone can fix this)

@philippkuehn
Copy link
Contributor

Thanks @Chrissi2812! Something like that is the way to go so I'll close this here.

@usb248
Copy link

usb248 commented Sep 25, 2019

@Chrissi2812 your solution is so buggy.
Any working solution ?
It's a basic functionality

@marcpaskell
Copy link

Anyone found a reliable way to do this?

@alexrififi
Copy link

An approximate solution could be like this (it remains to refine the cropping of pasted text)

new Editor({
    ...
    editorProps: {
      handleTextInput: (view) => {
        if (this.maxlength && view.state.doc.textContent.length >= this.maxlength && view.state.selection.empty) {
          return true;
        }
      },
      handlePaste: (view, event, slice) => {
        if (this.maxlength && view.state.doc.textContent.length + slice.size > this.maxlength) {
          slice.content = new DocumentFragment();
        }
      },
    },
    ...
}); 

@usb248
Copy link

usb248 commented Jan 25, 2020

When i see the code :
image
Without any documentation a little bit detailed... Good luck !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Feature The issue or pullrequest is a new feature
Projects
None yet
Development

No branches or pull requests

7 participants