From 2d277754ebd347fd7c98a55d41f6c4de5e9920cc Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Mon, 18 Dec 2023 21:12:08 +0000 Subject: Add latest changes from gitlab-org/gitlab@master --- .../content_editor/extensions/copy_paste.js | 34 ++++++++++++---------- 1 file changed, 19 insertions(+), 15 deletions(-) (limited to 'app/assets') diff --git a/app/assets/javascripts/content_editor/extensions/copy_paste.js b/app/assets/javascripts/content_editor/extensions/copy_paste.js index d29a407c5ca..23f2da7bc28 100644 --- a/app/assets/javascripts/content_editor/extensions/copy_paste.js +++ b/app/assets/javascripts/content_editor/extensions/copy_paste.js @@ -149,21 +149,26 @@ export default Extension.create({ const { clipboardData } = event; const gfmContent = clipboardData.getData(GFM_FORMAT); - - if (gfmContent) { - return this.editor.commands.pasteContent(gfmContent, true); - } - const textContent = clipboardData.getData(TEXT_FORMAT); const htmlContent = clipboardData.getData(HTML_FORMAT); const { from, to } = view.state.selection; + const isCodeBlockActive = CODE_BLOCK_NODE_TYPES.some((type) => + this.editor.isActive(type), + ); - if (pasteRaw) { - this.editor.commands.insertContentAt( - { from, to }, - textContent.replace(/^\s+|\s+$/gm, ''), - ); + if (pasteRaw || isCodeBlockActive) { + const isMarkdownCodeBlockActive = this.editor.isActive(CodeBlockHighlight.name, { + language: 'markdown', + }); + + const contentToInsert = isMarkdownCodeBlockActive + ? gfmContent || textContent + : textContent.replace(/^\s+|\s+$/gm, ''); + + if (!contentToInsert) return false; + + this.editor.commands.insertContentAt({ from, to }, contentToInsert); return true; } @@ -172,11 +177,6 @@ export default Extension.create({ const vsCodeMeta = hasVsCode ? JSON.parse(clipboardData.getData(VS_CODE_FORMAT)) : {}; const language = vsCodeMeta.mode; - // if a code block is active, paste as plain text - if (!textContent || CODE_BLOCK_NODE_TYPES.some((type) => this.editor.isActive(type))) { - return false; - } - if (hasVsCode) { return this.editor.commands.pasteContent( language === 'markdown' ? textContent : `\`\`\`${language}\n${textContent}\n\`\`\``, @@ -184,6 +184,10 @@ export default Extension.create({ ); } + if (gfmContent) { + return this.editor.commands.pasteContent(gfmContent, true); + } + const preStartRegex = /^]*lang="markdown"[^>]*>/; const preEndRegex = /<\/pre>$/; const htmlContentWithoutMeta = htmlContent?.replace(/^]*>/, ''); -- cgit v1.2.3