From dc95ad66a522e9d5c1626e65844f160834224696 Mon Sep 17 00:00:00 2001 From: Jinxuan Zhu Date: Fri, 10 Aug 2018 13:40:14 -0400 Subject: [PATCH] Do not use requestAnimation if DataTransfer is available (#2052) * not to requestAnimationFrame in modern browser * Fix bugs * Fix bugs * Fix bugs * Use callback --- packages/slate-react/src/plugins/after.js | 5 +---- packages/slate-react/src/utils/clone-fragment.js | 10 ++++++++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/slate-react/src/plugins/after.js b/packages/slate-react/src/plugins/after.js index 55a26d880..002aa1557 100644 --- a/packages/slate-react/src/plugins/after.js +++ b/packages/slate-react/src/plugins/after.js @@ -204,12 +204,9 @@ function AfterPlugin() { function onCut(event, change, editor) { debug('onCut', { event }) - cloneFragment(event, change.value) - const window = getWindow(event.target) - // Once the fake cut content has successfully been added to the clipboard, // delete the content in the current selection. - window.requestAnimationFrame(() => { + cloneFragment(event, change.value, change.value.fragment, () => { // If user cuts a void block node or a void inline node, // manually removes it since selection is collapsed in this case. const { value } = change diff --git a/packages/slate-react/src/utils/clone-fragment.js b/packages/slate-react/src/utils/clone-fragment.js index 878c6eb62..657554a3f 100644 --- a/packages/slate-react/src/utils/clone-fragment.js +++ b/packages/slate-react/src/utils/clone-fragment.js @@ -18,7 +18,12 @@ const { FRAGMENT, HTML, TEXT } = TRANSFER_TYPES * @param {Document} [fragment] */ -function cloneFragment(event, value, fragment = value.fragment) { +function cloneFragment( + event, + value, + fragment = value.fragment, + callback = () => undefined +) { const window = getWindow(event.target) const native = window.getSelection() const { start, end } = value.selection @@ -106,7 +111,7 @@ function cloneFragment(event, value, fragment = value.fragment) { event.clipboardData.setData(TEXT, plainText) event.clipboardData.setData(FRAGMENT, encoded) event.clipboardData.setData(HTML, div.innerHTML) - return + callback() } // COMPAT: For browser that don't support the Clipboard API's setData method, @@ -124,6 +129,7 @@ function cloneFragment(event, value, fragment = value.fragment) { editor.removeChild(div) removeAllRanges(native) native.addRange(range) + callback() }) }