Handling pasted text with handlePastedText does not work!
See original GitHub issueI just want to trim the text when pasting, to avoid undesired whitespace. The DraftJs docs says that I should use handlePastedText
, that’s what I did with the following code :
handlePastedText(text, html) {
console.log("handlePastedText executed");
const {editorState} = this.state;
const newState = Modifier.replaceText(editorState.getCurrentContent(), editorState.getSelection(), text.trim());
this.onChange(EditorState.push(editorState, newState, 'insert-fragment'));
return true;
}
but that didn’t work, until I changed import { Editor } from 'draft-js-plugins-editor';
to import { Editor } from 'draft-js';
Here are some gifs :
(With the normal Editor)
(With the Plugins Editor)
Issue Analytics
- State:
- Created 6 years ago
- Reactions:6
- Comments:12 (2 by maintainers)
Top Results From Across the Web
Handling Pasted Content in Draft.js - Binary Nirvana
Not Ideal. As I looked for a way to get better control over what the pasted values would look like, I ran across...
Read more >draft.js: how to preserve paragraph breaks when pasting ...
You can handle this using a prop for Editor. handlePastedText = (text: string, html?: string, editorState: EditorState) => { const selection = editorState....
Read more >Editor Component - Draft.js
Handle text and html(for rich text) that has been pasted directly into the editor. Returning true will prevent the default paste behavior.
Read more >handlepastedtext - npm Package Health Analysis - Snyk
Function to handle pasted text in draft-js. This seems to be one of the key features of draftail. I didn't want the other...
Read more >handlepastedtext - npm
Function to handle pasted text in draft-js. This seems to be one of the key features of draftail. I didn't want the other...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@ramrami and @juliankrispel we figured this one out. The draft-js docs are confusing for handlePastedtext which state:
This should actually read:
So, for my example above, switching to this works:
The reason why returning
true
was not working in the plugins editor is because of the createHandleHooks which expects ‘handled’ or ‘not-handled’.So IMO the main problem here is the draft-js docs for
handlePastedText
to reflect this which is outlined in Cancelable HandlersEDIT: submitted documentation PR to draft-js here: https://github.com/facebook/draft-js/pull/1318
Thanks @icd2k3, this is interesting. However, if you look at the
draft-js
source code here, the function is checking forvalue === true
.So both values should work (maybe the docs should be updated with both values). But IMO it’s still a problem with
draft-js-plugins
that should check for both values, or useisEventHandled
if it’s public.