From 7857a68a2beebf459208fdd136f5bb32019ced9b Mon Sep 17 00:00:00 2001 From: Ryan Mitts Date: Mon, 9 Mar 2020 14:48:37 -0700 Subject: [PATCH] Attach the cloned DOM node before coputing its plaintext representation when copying. (#3543) The cloned dom node constructed when copying must be attached to the dom for getComputedStyle to work. This adds the div to the body as a hidden element and removes after computing its text representation. --- packages/slate-react/src/components/editable.tsx | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/slate-react/src/components/editable.tsx b/packages/slate-react/src/components/editable.tsx index 29877dbe3..8e1ec6b9d 100644 --- a/packages/slate-react/src/components/editable.tsx +++ b/packages/slate-react/src/components/editable.tsx @@ -1087,13 +1087,18 @@ const setFragmentData = ( // Add the content to a
so that we can get its inner HTML. const div = document.createElement('div') div.appendChild(contents) + div.setAttribute('hidden', 'true') + document.body.appendChild(div) dataTransfer.setData('text/html', div.innerHTML) dataTransfer.setData('text/plain', getPlainText(div)) + document.body.removeChild(div) } /** * Get a plaintext representation of the content of a node, accounting for block * elements which get a newline appended. + * + * The domNode must be attached to the DOM. */ const getPlainText = (domNode: DOMNode) => {