pull/444/head
Zef Hemel 2023-07-02 12:15:37 +02:00
parent 809fc7e2e1
commit 0c006bb84c
1 changed files with 30 additions and 1 deletions

View File

@ -1,4 +1,4 @@
import { EditorView, ViewPlugin, ViewUpdate } from "../deps.ts"; import { EditorView, syntaxTree, ViewPlugin, ViewUpdate } from "../deps.ts";
import { maximumAttachmentSize } from "../../common/types.ts"; import { maximumAttachmentSize } from "../../common/types.ts";
import { Editor } from "../editor.tsx"; import { Editor } from "../editor.tsx";
@ -11,6 +11,12 @@ import {
taskListItems, taskListItems,
} from "https://cdn.skypack.dev/@joplin/turndown-plugin-gfm@1.0.45"; } from "https://cdn.skypack.dev/@joplin/turndown-plugin-gfm@1.0.45";
import { safeRun } from "../../common/util.ts"; import { safeRun } from "../../common/util.ts";
import { lezerToParseTree } from "../../common/markdown_parser/parse_tree.ts";
import {
addParentPointers,
findParentMatching,
nodeAtPos,
} from "../../plug-api/lib/tree.ts";
const turndownService = new TurndownService({ const turndownService = new TurndownService({
hr: "---", hr: "---",
codeBlockStyle: "fenced", codeBlockStyle: "fenced",
@ -106,8 +112,31 @@ export function attachmentExtension(editor: Editor) {
paste: (event: ClipboardEvent) => { paste: (event: ClipboardEvent) => {
const payload = [...event.clipboardData!.items]; const payload = [...event.clipboardData!.items];
const richText = event.clipboardData?.getData("text/html"); const richText = event.clipboardData?.getData("text/html");
// Only do rich text paste if shift is NOT down // Only do rich text paste if shift is NOT down
if (richText && !shiftDown) { if (richText && !shiftDown) {
// Are we in a fencede code block?
const editorText = editor.editorView!.state.sliceDoc();
const tree = lezerToParseTree(
editorText,
syntaxTree(editor.editorView!.state).topNode,
);
addParentPointers(tree);
const currentNode = nodeAtPos(
tree,
editor.editorView!.state.selection.main.from,
);
if (currentNode) {
const fencedParentNode = findParentMatching(
currentNode,
(t) => t.type === "FencedCode",
);
if (fencedParentNode || currentNode.type === "FencedCode") {
console.log("Inside of fenced code block, not pasting rich text");
return false;
}
}
const markdown = striptHtmlComments(turndownService.turndown(richText)) const markdown = striptHtmlComments(turndownService.turndown(richText))
.trim(); .trim();
const view = editor.editorView!; const view = editor.editorView!;