From d55826132ba3e3c7a759ab9378bea6a6bb1d9b48 Mon Sep 17 00:00:00 2001 From: Shota Fuji Date: Wed, 9 Nov 2022 09:29:52 +0000 Subject: [PATCH] fix(blocks): Do not copy snippet when preview text is selected https://github.com/storybookjs/storybook/issues/18611 This patch introduces skip conditional for the "copy code snippet by shortcut key" feature. this condition would not work when the preview is iframe (user sets `docs: { inlineStories: false }`), but that does not matter as the event handler won't run in the first place. --- code/ui/blocks/src/components/Preview.tsx | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/code/ui/blocks/src/components/Preview.tsx b/code/ui/blocks/src/components/Preview.tsx index f93ba28cdf2f..79e901eec908 100644 --- a/code/ui/blocks/src/components/Preview.tsx +++ b/code/ui/blocks/src/components/Preview.tsx @@ -223,6 +223,14 @@ export const Preview: FC = ({ }, []); const onCopyCapture = (e: ClipboardEvent) => { + // When the selection range is neither empty nor collapsed, we can assume + // user's intention is to copy the selected text, instead of the story's + // code snippet. + const selection: Selection | null = globalWindow.getSelection(); + if (selection && selection.type === 'Range') { + return; + } + e.preventDefault(); if (additionalActionItems.filter((item) => item.title === 'Copied').length === 0) { copyToClipboard(source.props.code).then(() => {