Skip to content

Commit fe06cce

Browse files
fix: force pasting plain text into code block (#1663)
1 parent 7de3a4b commit fe06cce

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

packages/core/src/api/clipboard/fromClipboard/pasteExtension.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type {
55
BlockNoteEditor,
66
BlockNoteEditorOptions,
77
} from "../../../editor/BlockNoteEditor";
8+
import { isMarkdown } from "../../parsers/markdown/detectMarkdown.js";
89
import {
910
BlockSchema,
1011
InlineContentSchema,
@@ -13,7 +14,6 @@ import {
1314
import { acceptedMIMETypes } from "./acceptedMIMETypes.js";
1415
import { handleFileInsertion } from "./handleFileInsertion.js";
1516
import { handleVSCodePaste } from "./handleVSCodePaste.js";
16-
import { isMarkdown } from "../../parsers/markdown/detectMarkdown.js";
1717

1818
function defaultPasteHandler({
1919
event,
@@ -26,6 +26,24 @@ function defaultPasteHandler({
2626
prioritizeMarkdownOverHTML: boolean;
2727
plainTextAsMarkdown: boolean;
2828
}) {
29+
// Special case for code blocks, as they do not support any rich text
30+
// formatting, so we force pasting plain text.
31+
const isInCodeBlock = editor.transact(
32+
(tr) =>
33+
tr.selection.$from.parent.type.spec.code &&
34+
tr.selection.$to.parent.type.spec.code
35+
);
36+
37+
if (isInCodeBlock) {
38+
const data = event.clipboardData?.getData("text/plain");
39+
40+
if (data) {
41+
editor.pasteText(data);
42+
43+
return true;
44+
}
45+
}
46+
2947
let format: (typeof acceptedMIMETypes)[number] | undefined;
3048
for (const mimeType of acceptedMIMETypes) {
3149
if (event.clipboardData!.types.includes(mimeType)) {

0 commit comments

Comments
 (0)