diff --git a/packages/core/src/api/clipboard/fromClipboard/pasteExtension.ts b/packages/core/src/api/clipboard/fromClipboard/pasteExtension.ts index ecc17120e..c10a57c62 100644 --- a/packages/core/src/api/clipboard/fromClipboard/pasteExtension.ts +++ b/packages/core/src/api/clipboard/fromClipboard/pasteExtension.ts @@ -5,6 +5,7 @@ import type { BlockNoteEditor, BlockNoteEditorOptions, } from "../../../editor/BlockNoteEditor"; +import { isMarkdown } from "../../parsers/markdown/detectMarkdown.js"; import { BlockSchema, InlineContentSchema, @@ -13,7 +14,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 +26,24 @@ 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 isInCodeBlock = editor.transact( + (tr) => + tr.selection.$from.parent.type.spec.code && + tr.selection.$to.parent.type.spec.code + ); + + if (isInCodeBlock) { + 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)) {