Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

editor: paste single line of code as inline code

Abdullah Atta0cfa7ce77432
diff --git a/packages/editor/src/extensions/code-block/code-block.ts b/packages/editor/src/extensions/code-block/code-block.ts
index 52ae76146..9b999138c 100644
--- a/packages/editor/src/extensions/code-block/code-block.ts
+++ b/packages/editor/src/extensions/code-block/code-block.ts
@@ -26,7 +26,7 @@ import {
Transaction,
Selection
} from "prosemirror-state";
-import { ResolvedPos, Node as ProsemirrorNode } from "prosemirror-model";
+import { ResolvedPos, Node as ProsemirrorNode, Slice } from "prosemirror-model";
import { CodeblockComponent } from "./component";
import { HighlighterPlugin } from "./highlighter";
import { createNodeView } from "../react";
@@ -508,7 +508,6 @@ export const CodeBlock = Node.create<CodeBlockOptions>({
if (!event.clipboardData) {
return false;
}
- const text = event.clipboardData.getData("text/plain");
const { isCode, language } = detectCodeBlock(event.clipboardData);
const isInsideCodeBlock = this.editor.isActive(this.type.name);
@@ -516,6 +515,12 @@ export const CodeBlock = Node.create<CodeBlockOptions>({
return false;
}
+ const text = event.clipboardData
+ .getData("text/plain")
+ // strip carriage return chars from text pasted as code
+ // see: https://github.com/ProseMirror/prosemirror-view/commit/a50a6bcceb4ce52ac8fcc6162488d8875613aacd
+ .replace(/\r\n?/g, "\n");
+
const indent = fixIndentation(
text,
parseIndentation(view.state.selection.$from.parent)
@@ -523,22 +528,36 @@ export const CodeBlock = Node.create<CodeBlockOptions>({
const { tr } = view.state;
- // create an empty code block if not already within one
- if (isCode && !isInsideCodeBlock) {
- tr.replaceSelectionWith(
- this.type.create({
- id: createCodeblockId(),
- language,
- indentType: indent.type,
- indentLength: indent.amount
+ const isInlineCode =
+ indent.code.length < 80 &&
+ indent.code.split(/[\r\n]/).length === 1;
+ if (isInlineCode && !isInsideCodeBlock) {
+ tr.replaceSelection(
+ Slice.fromJSON(this.editor.view.state.schema, {
+ content: [
+ {
+ type: "text",
+ text: indent.code,
+ marks: [{ type: "code" }]
+ }
+ ]
})
);
- }
+ } else {
+ // create an empty code block if not already within one
+ if (!isInsideCodeBlock)
+ tr.replaceSelectionWith(
+ this.type.create({
+ id: createCodeblockId(),
+ language,
+ indentType: indent.type,
+ indentLength: indent.amount
+ })
+ );
- // add text to code block
- // strip carriage return chars from text pasted as code
- // see: https://github.com/ProseMirror/prosemirror-view/commit/a50a6bcceb4ce52ac8fcc6162488d8875613aacd
- tr.insertText(indent.code.replace(/\r\n?/g, "\n"));
+ // add text to code block
+ tr.insertText(indent.code);
+ }
// store meta information
// this is useful for other plugins that depends on the paste event