mirror of
https://github.com/docmost/docmost.git
synced 2026-05-07 06:23:06 +08:00
Merge branch 'main' into editor-972
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
// adapted from: https://github.com/aguingand/tiptap-markdown/blob/main/src/extensions/tiptap/clipboard.js - MIT
|
// adapted from: https://github.com/aguingand/tiptap-markdown/blob/main/src/extensions/tiptap/clipboard.js - MIT
|
||||||
import { Extension } from "@tiptap/core";
|
import { Extension } from "@tiptap/core";
|
||||||
import { Plugin, PluginKey } from "@tiptap/pm/state";
|
import { Plugin, PluginKey } from "@tiptap/pm/state";
|
||||||
import { DOMParser, DOMSerializer } from "@tiptap/pm/model";
|
import { DOMParser, DOMSerializer, Fragment, Slice } from "@tiptap/pm/model";
|
||||||
import { find } from "linkifyjs";
|
import { find } from "linkifyjs";
|
||||||
import { markdownToHtml, htmlToMarkdown } from "@docmost/editor-ext";
|
import { markdownToHtml, htmlToMarkdown } from "@docmost/editor-ext";
|
||||||
|
|
||||||
@@ -61,7 +61,7 @@ export const MarkdownClipboard = Extension.create({
|
|||||||
const { tr } = view.state;
|
const { tr } = view.state;
|
||||||
const { from, to } = view.state.selection;
|
const { from, to } = view.state.selection;
|
||||||
|
|
||||||
const html = markdownToHtml(text);
|
const html = markdownToHtml(text.replace(/\n+$/, ""));
|
||||||
|
|
||||||
const contentNodes = DOMParser.fromSchema(
|
const contentNodes = DOMParser.fromSchema(
|
||||||
this.editor.schema,
|
this.editor.schema,
|
||||||
@@ -74,6 +74,37 @@ export const MarkdownClipboard = Extension.create({
|
|||||||
view.dispatch(tr);
|
view.dispatch(tr);
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
|
// Strip trailing whitespace-only paragraphs from pasted content.
|
||||||
|
// Terminals (GNOME Terminal, etc.) often include trailing
|
||||||
|
// whitespace in their HTML clipboard data, which ProseMirror
|
||||||
|
// parses as an extra paragraph. Inside a list item this creates
|
||||||
|
// an orphan empty line that breaks the list structure.
|
||||||
|
transformPasted: (slice) => {
|
||||||
|
let { content, openStart, openEnd } = slice;
|
||||||
|
|
||||||
|
// Remove trailing paragraphs that contain only whitespace
|
||||||
|
while (content.childCount > 1) {
|
||||||
|
const lastChild = content.lastChild;
|
||||||
|
if (
|
||||||
|
lastChild?.type.name === "paragraph" &&
|
||||||
|
lastChild.textContent.trim() === ""
|
||||||
|
) {
|
||||||
|
const children = [];
|
||||||
|
for (let i = 0; i < content.childCount - 1; i++) {
|
||||||
|
children.push(content.child(i));
|
||||||
|
}
|
||||||
|
content = Fragment.from(children);
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (content !== slice.content) {
|
||||||
|
return new Slice(content, openStart, Math.max(openEnd, 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
return slice;
|
||||||
|
},
|
||||||
clipboardTextParser: (text, context, plainText) => {
|
clipboardTextParser: (text, context, plainText) => {
|
||||||
const link = find(text, {
|
const link = find(text, {
|
||||||
defaultProtocol: "http",
|
defaultProtocol: "http",
|
||||||
@@ -85,7 +116,7 @@ export const MarkdownClipboard = Extension.create({
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const parsed = markdownToHtml(text);
|
const parsed = markdownToHtml(text.replace(/\n+$/, ""));
|
||||||
return DOMParser.fromSchema(this.editor.schema).parseSlice(
|
return DOMParser.fromSchema(this.editor.schema).parseSlice(
|
||||||
elementFromString(parsed),
|
elementFromString(parsed),
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user