-
Notifications
You must be signed in to change notification settings - Fork 6
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
Conversation
1058d5a
to
1869b10
Compare
src/lib/api/copypaste.js
Outdated
|
||
// 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) || ""); |
There was a problem hiding this comment.
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.
Here are the Draft.js issues this should fix (to be confirmed after testing):
And non-Draft.js:
|
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. |
acdac6f
to
dbc5a3b
Compare
I've fixed the "doesn't work for one-liners" problem, this is now good to go. Tested in:
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. |
…#147 This makes Draftail always preserve the full content as-is when copy-pasting between editors. See also thibaudcolas/draftjs-conductor#2.
…#147 This makes Draftail always preserve the full content as-is when copy-pasting between editors. See also thibaudcolas/draftjs-conductor#2.
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.
localStorage
on copy, then on paste other editors can check what is stored there based on thedata-editor
attr they receive in HTML. This would work between pages and between page reloads of a given site, in a given browser.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.