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

Switch implementation to use custom onPaste handler with custom mime type instead of handlePastedText #269

Open
thibaudcolas opened this issue Nov 19, 2020 · 0 comments
Labels
enhancement New feature or request
Milestone

Comments

@thibaudcolas
Copy link
Owner

thibaudcolas commented Nov 19, 2020

Is your proposal related to a problem?

Follow-up to #268. This package now uses the undocumented onCopy and onCut props of the Draft.js Editor, but not onPaste. The benefits of onPaste aren’t that clear-cut to me just yet. It would make it possible to use a custom MIME type rather than having to hack the transfer of the content in a data- attribute, but requires importing more Draft.js internals so we can fall back to the Draft.js default paste handling.

Describe the solution you’d like

First draft – to add to the copy/cut handler (potentially also removing all the other custom logic):

e.clipboardData.setData(
    "application/x-draftjs-conductor-fragment",
    serialisedContent,
);

Here is what the new paste handler would look like:

import editOnPaste from "draft-js/lib/editOnPaste";

export const onDraftEditorPaste = (
    editor: Editor,
    e: SyntheticClipboardEvent<>,
  ) => {
    const editorState = editor._latestEditorState;
    const draftEditorPaste = e.clipboardData.getData(
      "application/x-draftjs-conductor-fragment",
    );
    if (draftEditorPaste) {
      let rawContent;
      try {
        // If JSON parsing fails, leave paste handling to Draft.js.
        // There is no reason for this to happen, unless the clipboard was altered somehow.
        // $FlowFixMe
        rawContent = JSON.parse(draftEditorPaste);
      } catch (error) {
        return;
      }
      console.log(rawContent);
  
      const fragment = convertFromRaw(rawContent).getBlockMap();
  
      const content = Modifier.replaceWithFragment(
        editorState.getCurrentContent(),
        editorState.getSelection(),
        fragment,
      );
      e.preventDefault();
      editor.update(EditorState.push(editorState, content, "insert-fragment"));
      return;
    }
    console.log("here?");
    editOnPaste(editor, e);
  };

Describe alternatives you’ve considered

Keep on using handlePastedText.

Additional context

One of the reasons I haven’t done this yet is that it feels like a much bigger departure from the Draft.js 0.10 implementation than the copy/cut listeners are – ideally we’d only change this implementation as part of a breaking change to remove support for Draft.js 0.10, in a future major release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant