From a12a9199245cdd585fb748560442ccdc5bf40579 Mon Sep 17 00:00:00 2001 From: Matthew Lipski Date: Tue, 6 May 2025 19:16:43 +0200 Subject: [PATCH 1/4] Forced pasting plain text in code block --- .../clipboard/fromClipboard/pasteExtension.ts | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/packages/core/src/api/clipboard/fromClipboard/pasteExtension.ts b/packages/core/src/api/clipboard/fromClipboard/pasteExtension.ts index ecc17120e..7260d75c7 100644 --- a/packages/core/src/api/clipboard/fromClipboard/pasteExtension.ts +++ b/packages/core/src/api/clipboard/fromClipboard/pasteExtension.ts @@ -5,6 +5,11 @@ import type { BlockNoteEditor, BlockNoteEditorOptions, } from "../../../editor/BlockNoteEditor"; +import { + getBlockInfoFromResolvedPos, + getBlockInfoFromSelection, +} from "../../getBlockInfoFromPos.js"; +import { isMarkdown } from "../../parsers/markdown/detectMarkdown.js"; import { BlockSchema, InlineContentSchema, @@ -13,7 +18,6 @@ import { import { acceptedMIMETypes } from "./acceptedMIMETypes.js"; import { handleFileInsertion } from "./handleFileInsertion.js"; import { handleVSCodePaste } from "./handleVSCodePaste.js"; -import { isMarkdown } from "../../parsers/markdown/detectMarkdown.js"; function defaultPasteHandler({ event, @@ -26,6 +30,27 @@ function defaultPasteHandler({ prioritizeMarkdownOverHTML: boolean; plainTextAsMarkdown: boolean; }) { + // Special case for code blocks, as they do not support any rich text + // formatting, so we force pasting plain text. + const selection = editor.prosemirrorView?.state.selection; + if (selection) { + const { isBlockContainer, blockNoteType } = getBlockInfoFromSelection( + editor.prosemirrorView.state + ); + + const selectionInCodeBlock = + isBlockContainer && blockNoteType === "codeBlock"; + + if (selectionInCodeBlock) { + const data = event.clipboardData?.getData("text/plain"); + if (data) { + editor.pasteText(data); + } + + return true; + } + } + let format: (typeof acceptedMIMETypes)[number] | undefined; for (const mimeType of acceptedMIMETypes) { if (event.clipboardData!.types.includes(mimeType)) { From f69a98f865785e083f3d5da4e9c8fd155a8f3c92 Mon Sep 17 00:00:00 2001 From: Matthew Lipski Date: Tue, 6 May 2025 19:21:28 +0200 Subject: [PATCH 2/4] Updated check --- .../core/src/api/clipboard/fromClipboard/pasteExtension.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/core/src/api/clipboard/fromClipboard/pasteExtension.ts b/packages/core/src/api/clipboard/fromClipboard/pasteExtension.ts index 7260d75c7..04642674f 100644 --- a/packages/core/src/api/clipboard/fromClipboard/pasteExtension.ts +++ b/packages/core/src/api/clipboard/fromClipboard/pasteExtension.ts @@ -34,12 +34,10 @@ function defaultPasteHandler({ // formatting, so we force pasting plain text. const selection = editor.prosemirrorView?.state.selection; if (selection) { - const { isBlockContainer, blockNoteType } = getBlockInfoFromSelection( - editor.prosemirrorView.state - ); + const blockInfo = getBlockInfoFromSelection(editor.prosemirrorView.state); const selectionInCodeBlock = - isBlockContainer && blockNoteType === "codeBlock"; + blockInfo.isBlockContainer && blockInfo.blockContent.node.type.spec.code; if (selectionInCodeBlock) { const data = event.clipboardData?.getData("text/plain"); From 8e288ec44b6669a7968b81c1fd55c3dd42c24b2a Mon Sep 17 00:00:00 2001 From: Matthew Lipski Date: Wed, 7 May 2025 16:47:08 +0200 Subject: [PATCH 3/4] Implemented PR feedback --- .../clipboard/fromClipboard/pasteExtension.ts | 27 ++++++++----------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/packages/core/src/api/clipboard/fromClipboard/pasteExtension.ts b/packages/core/src/api/clipboard/fromClipboard/pasteExtension.ts index 04642674f..7b8ff5635 100644 --- a/packages/core/src/api/clipboard/fromClipboard/pasteExtension.ts +++ b/packages/core/src/api/clipboard/fromClipboard/pasteExtension.ts @@ -5,10 +5,6 @@ import type { BlockNoteEditor, BlockNoteEditorOptions, } from "../../../editor/BlockNoteEditor"; -import { - getBlockInfoFromResolvedPos, - getBlockInfoFromSelection, -} from "../../getBlockInfoFromPos.js"; import { isMarkdown } from "../../parsers/markdown/detectMarkdown.js"; import { BlockSchema, @@ -32,21 +28,20 @@ function defaultPasteHandler({ }) { // Special case for code blocks, as they do not support any rich text // formatting, so we force pasting plain text. - const selection = editor.prosemirrorView?.state.selection; - if (selection) { - const blockInfo = getBlockInfoFromSelection(editor.prosemirrorView.state); - - const selectionInCodeBlock = - blockInfo.isBlockContainer && blockInfo.blockContent.node.type.spec.code; + const isInCodeBlock = editor.transact( + (tr) => + tr.selection.$from.parent.type.spec.code && + tr.selection.$to.parent.type.spec.code + ); - if (selectionInCodeBlock) { - const data = event.clipboardData?.getData("text/plain"); - if (data) { - editor.pasteText(data); - } + if (isInCodeBlock) { + const data = event.clipboardData?.getData("text/plain"); - return true; + if (data) { + editor.pasteText(data); } + + return true; } let format: (typeof acceptedMIMETypes)[number] | undefined; From daa6d48ab463763fc40c943277888200c2ac25e7 Mon Sep 17 00:00:00 2001 From: Matthew Lipski Date: Thu, 8 May 2025 11:47:58 +0200 Subject: [PATCH 4/4] Small fix --- .../core/src/api/clipboard/fromClipboard/pasteExtension.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/core/src/api/clipboard/fromClipboard/pasteExtension.ts b/packages/core/src/api/clipboard/fromClipboard/pasteExtension.ts index 7b8ff5635..c10a57c62 100644 --- a/packages/core/src/api/clipboard/fromClipboard/pasteExtension.ts +++ b/packages/core/src/api/clipboard/fromClipboard/pasteExtension.ts @@ -39,9 +39,9 @@ function defaultPasteHandler({ if (data) { editor.pasteText(data); - } - return true; + return true; + } } let format: (typeof acceptedMIMETypes)[number] | undefined;