Trix: Counting characters

Created on 21 Nov 2018  Â·  3Comments  Â·  Source: basecamp/trix

I'm trying to implement a remaining character counter on a trix editor. I'm using javascript to catch the keyup event and compare the editor's length property with it's maxlength. All works fine until I start adding some formatting, at which point the character count includes those used by the html tags that are added. For example, if I press return, the
tags that are added reduce the character count. That's very confusing for the user because what they see doesn't match the remaining character count. Strangely, adding formatting like Bold doesn't affect the character count until the next time I press return.

Offhand, I can't think of a way to get round this except perhaps to get the trix editor contents in my javascript, strip out any html tags and use the result as the basis for the character count calculation which feels like a pretty major task.

Any ideas?

Details
  • Trix version: 0.12.0
  • Browser name and version: Chrome 70.0.3538.102
  • Operating system: OSX 10.13.6

Most helpful comment

I ended up not using the html maxlength attribute and writing my own logic to mimic it. I added a data-maxlength attribute to the editor and used it in the character count calculation. If the number of chars in the editor is greater than maxlength, I used setSelectedRang to select the complete editor contents and insertString to replace it with substring 0 to maxlength of the editor.

All 3 comments

All works fine until I start adding some formatting, at which point the character count includes those used by the html tags that are added.

You can convert the document to a string and get its length instead of the html value.

I'm using javascript to catch the keyup event… Strangely, adding formatting like Bold doesn't affect the character count until the next time I press return.

You'll want to handle the trix-change event instead of keyup.

Offhand, I can't think of a way to get round this

addEventListener("trix-change", event => {
  const { editor } = event.target
  const string = editor.getDocument().toString()
  const characterCount = string.length - 1
  console.log(`The document has ${characterCount} characters`)
})

Thanks, that works as far as the character count is concerned. However, the html maxlength attribute of the trix editor is ignored, allowing the user to continue typing into the editor after the maxlength has been reached and showing a negative number in the character count.

In an effort to get round that, I used the tStiring() function in my original key up handler. The character count came out correctly but still a problem with the maxlength. For example, if the maxlength is 20, when I type the 21st character, the contents of the editor are truncated to the first 15 characters. I think this is because of the initial

which is 5 characters long, accounting for the truncation from 20 to 15 characters (there is no other formatting in the editor).

It feels like I will have to implement my own maxlength checker.

I ended up not using the html maxlength attribute and writing my own logic to mimic it. I added a data-maxlength attribute to the editor and used it in the character count calculation. If the number of chars in the editor is greater than maxlength, I used setSelectedRang to select the complete editor contents and insertString to replace it with substring 0 to maxlength of the editor.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

adamdebono picture adamdebono  Â·  3Comments

andreimoment picture andreimoment  Â·  3Comments

emilbruckner picture emilbruckner  Â·  5Comments

marpstar picture marpstar  Â·  5Comments

atuttle picture atuttle  Â·  4Comments