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

Override Draft.js copy-paste to preserve full editor content #2

Merged
merged 21 commits into from
Jun 3, 2018

Conversation

thibaudcolas
Copy link
Owner

Initially based on springload/draftail#148. This addresses springload/draftail#147, facebookarchive/draft-js#787.

Originally, the code from that first PR only made it work for editors within the same page. I think I found a way to make it work for all editors within the same site (origin), and potentially for all editors everywhere always.

  • The initial approach (4366190) from WIP: On paste, read content from internal clipboard via paste registry springload/draftail#148 relies on an in-memory registry of all editors, so they can access each other's internal clipboard on paste.
  • The "within the same site" approach hasn't been implemented. The idea would be to put the contents of the editor's internal clipboard in localStorage on copy, then on paste other editors can check what is stored there based on the data-editor attr they receive in HTML. This would work between pages and between page reloads of a given site, in a given browser.
  • The most promising approach (WIP, 1058d5a) works somewhat similarly, except the copied content is serialised into the editor's clipboardData. The catch is that the content isn't stored with its own mime type – it's injected in the HTML content instead, so other tools where the paste could go can still process HTML as usual. For Draft.js editors, they check the HTML for the serialised content and then use this. This means it will work across sites, browsers, in all circumstances.

All of those approaches rely on the internals of Draft.js to some extent, and generally can be considered hacks. Lucky for us, in all those scenarios there would still be the normal "process paste from HTML" behavior as a fallback, so the risk for breakage is minimal.

@thibaudcolas thibaudcolas added the enhancement New feature or request label May 20, 2018

// Handle the paste if it comes from Draft.js, but not the current editor, and serialised content is present.
if (sourceEditorKey && sourceEditorKey !== targetEditorKey && raw) {
const rawContent = JSON.parse(raw.getAttribute(FRAGMENT_ATTR) || "");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small note: JSON.parse can throw so even if it's not expected to, a try/catch with an error reporter is a good idea.

@thibaudcolas
Copy link
Owner Author

After some early testing I can confirm this does fix those three issues.

Something else came up while testing: if the selection contains only one line of text, with the current implementation, the serialised content won't be put in the clipboard by the browser and make its way to the other editors. I'll have to check if there is a better way.

@thibaudcolas thibaudcolas force-pushed the feature/copy-paste-idempotence branch from acdac6f to dbc5a3b Compare June 3, 2018 22:43
@thibaudcolas thibaudcolas changed the title WIP: Override Draft.js copy-paste to preserve full editor content Override Draft.js copy-paste to preserve full editor content Jun 3, 2018
Repository owner deleted a comment from thi-bot Jun 3, 2018
@thibaudcolas
Copy link
Owner Author

I've fixed the "doesn't work for one-liners" problem, this is now good to go.

Tested in:

  • Windows 10, IE11 (no support, default "plain text" behavior)
  • Windows 10, Edge 17
  • Windows 10, Firefox latest
  • macOS 10.13, Firefox latest
  • macOS 10.13, Chrome latest
  • macOS 10.13, Safari latest
  • iOS 11.3 Safari

It could technically work in IE11 (and we could get IE11 to have rich text copy-paste between editors) by combining the current approach with that of springload/draftail#148. It's not something I'll do myself though.

@thibaudcolas thibaudcolas merged commit aead7ef into master Jun 3, 2018
@thibaudcolas thibaudcolas deleted the feature/copy-paste-idempotence branch June 3, 2018 23:01
thibaudcolas added a commit to springload/draftail that referenced this pull request Jun 3, 2018
…#147

This makes Draftail always preserve the full content as-is when copy-pasting between editors. See also thibaudcolas/draftjs-conductor#2.
thibaudcolas added a commit to springload/draftail that referenced this pull request Jun 4, 2018
…#147

This makes Draftail always preserve the full content as-is when copy-pasting between editors. See also thibaudcolas/draftjs-conductor#2.
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

Successfully merging this pull request may close these issues.

2 participants