1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-21 14:41:23 +02:00

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.
This commit is contained in:
Ryan Mitts
2020-03-09 14:48:37 -07:00
committed by GitHub
parent f45058ec31
commit 7857a68a2b

View File

@@ -1087,13 +1087,18 @@ const setFragmentData = (
// Add the content to a <div> so that we can get its inner HTML. // Add the content to a <div> so that we can get its inner HTML.
const div = document.createElement('div') const div = document.createElement('div')
div.appendChild(contents) div.appendChild(contents)
div.setAttribute('hidden', 'true')
document.body.appendChild(div)
dataTransfer.setData('text/html', div.innerHTML) dataTransfer.setData('text/html', div.innerHTML)
dataTransfer.setData('text/plain', getPlainText(div)) dataTransfer.setData('text/plain', getPlainText(div))
document.body.removeChild(div)
} }
/** /**
* Get a plaintext representation of the content of a node, accounting for block * Get a plaintext representation of the content of a node, accounting for block
* elements which get a newline appended. * elements which get a newline appended.
*
* The domNode must be attached to the DOM.
*/ */
const getPlainText = (domNode: DOMNode) => { const getPlainText = (domNode: DOMNode) => {