mirror of
https://github.com/docmost/docmost.git
synced 2026-05-15 21:24:09 +08:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 62f0a2278d | |||
| a689cca7a0 |
@@ -81,6 +81,7 @@ export const MarkdownClipboard = Extension.create({
|
|||||||
|
|
||||||
const parsed = markdownToHtml(text.replace(/\n+$/, ""));
|
const parsed = markdownToHtml(text.replace(/\n+$/, ""));
|
||||||
const body = elementFromString(parsed);
|
const body = elementFromString(parsed);
|
||||||
|
stripBlockLevelWhitespaceNodes(body);
|
||||||
normalizeTableColumnWidths(body);
|
normalizeTableColumnWidths(body);
|
||||||
|
|
||||||
const contentNodes = DOMParser.fromSchema(
|
const contentNodes = DOMParser.fromSchema(
|
||||||
@@ -91,7 +92,7 @@ export const MarkdownClipboard = Extension.create({
|
|||||||
|
|
||||||
tr.replaceRange(from, to, contentNodes);
|
tr.replaceRange(from, to, contentNodes);
|
||||||
const insertEnd = tr.mapping.map(from, 1);
|
const insertEnd = tr.mapping.map(from, 1);
|
||||||
tr.setSelection(TextSelection.near(tr.doc.resolve(Math.max(from, insertEnd - 2)), -1));
|
tr.setSelection(TextSelection.near(tr.doc.resolve(insertEnd), -1));
|
||||||
tr.setMeta('paste', true)
|
tr.setMeta('paste', true)
|
||||||
view.dispatch(tr);
|
view.dispatch(tr);
|
||||||
return true;
|
return true;
|
||||||
@@ -104,21 +105,28 @@ export const MarkdownClipboard = Extension.create({
|
|||||||
transformPasted: (slice) => {
|
transformPasted: (slice) => {
|
||||||
let { content, openStart, openEnd } = slice;
|
let { content, openStart, openEnd } = slice;
|
||||||
|
|
||||||
// Remove trailing paragraphs that contain only whitespace
|
const isTrailingNoise = (node: any) => {
|
||||||
while (content.childCount > 1) {
|
if (!node) return false;
|
||||||
const lastChild = content.lastChild;
|
if (node.type.name === "hardBreak") return true;
|
||||||
if (
|
if (node.isText && (node.text ?? "").trim() === "") return true;
|
||||||
lastChild?.type.name === "paragraph" &&
|
if (node.type.name === "paragraph") {
|
||||||
lastChild.textContent.trim() === ""
|
let onlyNoise = true;
|
||||||
) {
|
node.content.forEach((c: any) => {
|
||||||
const children = [];
|
if (c.type.name === "hardBreak") return;
|
||||||
for (let i = 0; i < content.childCount - 1; i++) {
|
if (c.isText && (c.text ?? "").trim() === "") return;
|
||||||
children.push(content.child(i));
|
onlyNoise = false;
|
||||||
}
|
});
|
||||||
content = Fragment.from(children);
|
return onlyNoise;
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
while (content.childCount > 1 && isTrailingNoise(content.lastChild)) {
|
||||||
|
const children = [];
|
||||||
|
for (let i = 0; i < content.childCount - 1; i++) {
|
||||||
|
children.push(content.child(i));
|
||||||
|
}
|
||||||
|
content = Fragment.from(children);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (content !== slice.content) {
|
if (content !== slice.content) {
|
||||||
@@ -140,6 +148,21 @@ function elementFromString(value) {
|
|||||||
return new window.DOMParser().parseFromString(wrappedValue, "text/html").body;
|
return new window.DOMParser().parseFromString(wrappedValue, "text/html").body;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// marked.parse() emits "<p>...</p>\n<p>...</p>\n" — those literal newlines
|
||||||
|
// become whitespace text nodes that parseSlice (preserveWhitespace: true)
|
||||||
|
// converts into spurious empty paragraphs at the insertion site. Inside a
|
||||||
|
// list item the trailing one prevents Enter from exiting the list.
|
||||||
|
function stripBlockLevelWhitespaceNodes(body: HTMLElement): void {
|
||||||
|
Array.from(body.childNodes).forEach((node) => {
|
||||||
|
if (
|
||||||
|
node.nodeType === 3 /* TEXT_NODE */ &&
|
||||||
|
(node.textContent ?? "").trim() === ""
|
||||||
|
) {
|
||||||
|
body.removeChild(node);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const DEFAULT_PASTE_COL_WIDTH_PX = 150;
|
const DEFAULT_PASTE_COL_WIDTH_PX = 150;
|
||||||
|
|
||||||
function parsePixelWidth(el: Element): number | null {
|
function parsePixelWidth(el: Element): number | null {
|
||||||
|
|||||||
Reference in New Issue
Block a user