Skip to content
This repository has been archived by the owner on Jan 20, 2022. It is now read-only.

Commit

Permalink
fix: Pasting into code formatting should not leave the code block (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
tommoor authored Sep 27, 2020
1 parent 164fddf commit cd37137
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/plugins/MarkdownPaste.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Plugin } from "prosemirror-state";
import { toggleMark } from "prosemirror-commands";
import Extension from "../lib/Extension";
import isUrl from "../lib/isUrl";
import isInCode from "../queries/isInCode";

export default class MarkdownPaste extends Extension {
get name() {
Expand Down Expand Up @@ -69,6 +70,11 @@ export default class MarkdownPaste extends Extension {

event.preventDefault();

if (isInCode(view.state)) {
view.dispatch(view.state.tr.insertText(text));
return true;
}

const paste = this.editor.parser.parse(text);
const slice = paste.slice(0);

Expand Down
12 changes: 12 additions & 0 deletions src/queries/isInCode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import isMarkActive from "./isMarkActive";

export default function isInCode(state) {
const $head = state.selection.$head;
for (let d = $head.depth; d > 0; d--) {
if ($head.node(d).type === state.schema.nodes.code_block) {
return true;
}
}

return isMarkActive(state.schema.marks.code_inline)(state);
}

0 comments on commit cd37137

Please sign in to comment.