From 2c7750cac5949a935a570a9590a82187673b9a44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20H=C3=B6rnicke?= Date: Mon, 15 Nov 2021 09:59:02 +0100 Subject: [PATCH] fix: weak guard on DataTransfer to not rely on current window (#4654) --- .changeset/beige-hotels-vanish.md | 5 +++++ packages/slate-react/src/components/editable.tsx | 8 +++++--- 2 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 .changeset/beige-hotels-vanish.md diff --git a/.changeset/beige-hotels-vanish.md b/.changeset/beige-hotels-vanish.md new file mode 100644 index 000000000..9bf8ad8b7 --- /dev/null +++ b/.changeset/beige-hotels-vanish.md @@ -0,0 +1,5 @@ +--- +'slate-react': patch +--- + +weak guard on DataTransfer to not rely on current window diff --git a/packages/slate-react/src/components/editable.tsx b/packages/slate-react/src/components/editable.tsx index 1623c9b99..bbdc35a3e 100644 --- a/packages/slate-react/src/components/editable.tsx +++ b/packages/slate-react/src/components/editable.tsx @@ -429,9 +429,11 @@ export const Editable = (props: EditableProps) => { state.isComposing = false } - const window = ReactEditor.getWindow(editor) - if (data instanceof window.DataTransfer) { - ReactEditor.insertData(editor, data as DataTransfer) + // use a weak comparison instead of 'instanceof' to allow + // programmatic access of paste events coming from external windows + // like cypress where cy.window does not work realibly + if (data?.constructor.name === 'DataTransfer') { + ReactEditor.insertData(editor, data) } else if (typeof data === 'string') { // Only insertText operations use the native functionality, for now. // Potentially expand to single character deletes, as well.