From 9cfe24f994802e4e481101b3f97a3c54dc040255 Mon Sep 17 00:00:00 2001 From: Alex Vladut Date: Fri, 17 Mar 2023 13:39:58 +0200 Subject: [PATCH] fix: allow copying from editable void input --- .changeset/funny-students-sparkle.md | 5 +++++ .../slate-react/src/components/editable.tsx | 21 +++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 .changeset/funny-students-sparkle.md diff --git a/.changeset/funny-students-sparkle.md b/.changeset/funny-students-sparkle.md new file mode 100644 index 0000000000..d5783f1435 --- /dev/null +++ b/.changeset/funny-students-sparkle.md @@ -0,0 +1,5 @@ +--- +'slate-react': patch +--- + +Allow copying from editable void input diff --git a/packages/slate-react/src/components/editable.tsx b/packages/slate-react/src/components/editable.tsx index 90eb4efc20..7e288bb8e9 100644 --- a/packages/slate-react/src/components/editable.tsx +++ b/packages/slate-react/src/components/editable.tsx @@ -1157,7 +1157,8 @@ export const Editable = (props: EditableProps) => { (event: React.ClipboardEvent) => { if ( ReactEditor.hasSelectableTarget(editor, event.target) && - !isEventHandled(event, attributes.onCopy) + !isEventHandled(event, attributes.onCopy) && + !isDOMEventTargetInput(event) ) { event.preventDefault() ReactEditor.setFragmentData( @@ -1174,7 +1175,8 @@ export const Editable = (props: EditableProps) => { if ( !readOnly && ReactEditor.hasSelectableTarget(editor, event.target) && - !isEventHandled(event, attributes.onCut) + !isEventHandled(event, attributes.onCut) && + !isDOMEventTargetInput(event) ) { event.preventDefault() ReactEditor.setFragmentData( @@ -1736,6 +1738,21 @@ export const isEventHandled = < return event.isDefaultPrevented() || event.isPropagationStopped() } +/** + * Check if the event's target is an input element + */ +export const isDOMEventTargetInput = < + EventType extends React.SyntheticEvent +>( + event: EventType +) => { + return ( + isDOMNode(event.target) && + (event.target instanceof HTMLInputElement || + event.target instanceof HTMLTextAreaElement) + ) +} + /** * Check if a DOM event is overrided by a handler. */