question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Handling pasted text with handlePastedText does not work!

See original GitHub issue

I 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) ezgif com-video-to-gif 3

(With the Plugins Editor) ezgif com-video-to-gif 4

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

11reactions
icd2k3commented, Aug 1, 2017

@ramrami and @juliankrispel we figured this one out. The draft-js docs are confusing for handlePastedtext which state:

Handle text and html(for rich text) that has been pasted directly into the editor. Returning true will prevent the default paste behavior.

This should actually read:

Handle text and html(for rich text) that has been pasted directly into the editor. Returning ‘handled’ will prevent the default paste behavior.

So, for my example above, switching to this works:

import Editor from 'draft-js-plugins-editor';
<Editor
  handlePastedText={() => 'handled'}  // blocks text from being pasted into editor
  //...
/>

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 Handlers

EDIT: submitted documentation PR to draft-js here: https://github.com/facebook/draft-js/pull/1318

3reactions
ramramicommented, Aug 1, 2017

Thanks @icd2k3, this is interesting. However, if you look at the draft-js source code here, the function is checking for value === true.

/**
 * Utility method for determining whether or not the value returned
 * from a handler indicates that it was handled.
 */
function isEventHandled(value: DraftHandleValue): boolean {
  return value === 'handled' || value === 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 use isEventHandled if it’s public.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found