by underwater · 10y ago · 0 comments
Draft.JS is a controlled input. The view is completely derived from the model. This eliminates whole classes of browser and editor bugs.
It's also makes the editor really easy to extend. You can add new object types and complex styling without having to worry about all the nuances of text editing.
For example you could highlight hashtags by giving the editor a regex to match and then tell it which styles your hashtag should be decorated with. Draft.js handles creating and removing tags as you type, drag and drop, copy, paste, etc.
Undo and other text editing facilities aren't completely nonfunctional, they just become broken and unpredictable. For example, these steps:
1. Load http://facebook.github.io/draft-js/ 2. Click in the text box 3. Type 'hello' on the keyboard 4. Using the mouse, choose Edit -> Undo
The text box will show "h" instead of being empty. Do you see this too?
Note that cmd-Z won't reproduce this since the page tries to handle that itself to work around these bugs.
Anyway this approach is the only good one if you don't want to produce crappy HTML that will end up on your site. TinyMCE and the rest are obsolete, and editors like DraftJS or mobiledoc-kit are the only viable solutions until browsers offer a new, not broken alternative to content-editable.
I was involved in a project that used contentEditable DOM as the source of truth before I had a chance to work with Draft, and I believe that Draft's model is the best compromise.
Draft does a lot to handle the edge cases where it's possible. IME and spellcheck are two of the big ones.
(disclaimer: I've been out of Facebook for a while so they may have introduced that feature already)