From 0640985cb710134671d00b54cc44d011cabaf0b3 Mon Sep 17 00:00:00 2001 From: Harrison Healey Date: Tue, 7 May 2024 15:08:38 -0400 Subject: [PATCH] MM-52570 Prevent pasteHandler from having any effect when shift is held (#26953) --- webapp/channels/src/utils/paste.test.tsx | 38 ++++++++++++++++++++++-- webapp/channels/src/utils/paste.tsx | 8 +++-- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/webapp/channels/src/utils/paste.test.tsx b/webapp/channels/src/utils/paste.test.tsx index 48479cea9e..c8bc1e55c7 100644 --- a/webapp/channels/src/utils/paste.test.tsx +++ b/webapp/channels/src/utils/paste.test.tsx @@ -287,6 +287,36 @@ describe('pasteHandler', () => { }, expectedMarkdown: "```\n// a javascript codeblock example\nif (1 > 0) {\n return 'condition is true';\n}\n```", }, + { + testName: 'should paste table as plain text when shift is held', + isNonFormattedPaste: true, + clipboardData: { + items: [1], + types: ['text/plain', 'text/html'], + getData: (dataType: string) => { + if (dataType === 'text/plain') { + return 'test \ttest\ntest \ttest'; + } + return '\n\n\n\n\n
testtest
testtest
'; + }, + }, + expectedMarkdown: 'test \ttest\ntest \ttest', + }, + { + testName: 'should paste github code as plain text when shift is held', + isNonFormattedPaste: true, + clipboardData: { + items: [1], + types: ['text/plain', 'text/html'], + getData: (type: string) => { + if (type === 'text/plain') { + return '// a javascript codeblock example\nif (1 > 0) {\n return \'condition is true\';\n}'; + } + return '
// a javascript codeblock example
 if (1 > 0) {
 console.log(\'condition is true\');
 }
'; + }, + }, + expectedMarkdown: '// a javascript codeblock example\nif (1 > 0) {\n return \'condition is true\';\n}', + }, ]; for (const tc of testCases) { @@ -300,9 +330,13 @@ describe('pasteHandler', () => { clipboardData: tc.clipboardData, }; - pasteHandler(event, location, '', false, 0); + pasteHandler(event, location, '', tc.isNonFormattedPaste ?? false, 0); - expect(execCommandInsertText).toHaveBeenCalledWith(tc.expectedMarkdown); + if (tc.isNonFormattedPaste) { + expect(execCommandInsertText).not.toHaveBeenCalled(); + } else { + expect(execCommandInsertText).toHaveBeenCalledWith(tc.expectedMarkdown); + } }); } }); diff --git a/webapp/channels/src/utils/paste.tsx b/webapp/channels/src/utils/paste.tsx index 747ddc3135..0d58f1d08f 100644 --- a/webapp/channels/src/utils/paste.tsx +++ b/webapp/channels/src/utils/paste.tsx @@ -160,7 +160,7 @@ export function formatMarkdownLinkMessage({message, clipboardData, selectionStar return markdownLink; } -export function pasteHandler(event: ClipboardEvent, location: string, message: string, isNonFormattedPaste?: boolean, caretPosition?: number) { +export function pasteHandler(event: ClipboardEvent, location: string, message: string, isNonFormattedPaste: boolean, caretPosition?: number) { const {clipboardData, target} = event; const textboxId = location === Locations.RHS_COMMENT ? 'reply_textbox' : 'post_textbox'; @@ -169,11 +169,15 @@ export function pasteHandler(event: ClipboardEvent, location: string, message: s return; } + if (isNonFormattedPaste) { + return; + } + const {selectionStart, selectionEnd} = target as TextboxElement; const hasSelection = !isNil(selectionStart) && !isNil(selectionEnd) && selectionStart < selectionEnd; const hasTextUrl = isTextUrl(clipboardData); - const hasHTMLLinks = !isNonFormattedPaste && hasHtmlLink(clipboardData); + const hasHTMLLinks = hasHtmlLink(clipboardData); const htmlTable = getHtmlTable(clipboardData); const shouldApplyLinkMarkdown = hasSelection && hasTextUrl; const shouldApplyGithubCodeBlock = htmlTable && isGitHubCodeBlock(htmlTable.className);