fix: Improve paste handler parsing for more cases, specifically Google Docs (#3322)

This commit is contained in:
Tom Moor
2022-04-01 15:13:44 -07:00
committed by GitHub
parent d119ed8963
commit f48c86c56d
4 changed files with 30 additions and 3 deletions

View File

@@ -4,6 +4,9 @@ import { MarkSpec, MarkType } from "prosemirror-model";
import markInputRule from "../lib/markInputRule"; import markInputRule from "../lib/markInputRule";
import Mark from "./Mark"; import Mark from "./Mark";
const heavyWeightRegex = /^(bold(er)?|[5-9]\d{2,})$/;
const normalWeightRegex = /^(normal|[1-4]\d{2,})$/;
export default class Bold extends Mark { export default class Bold extends Mark {
get name() { get name() {
return "strong"; return "strong";
@@ -11,7 +14,20 @@ export default class Bold extends Mark {
get schema(): MarkSpec { get schema(): MarkSpec {
return { return {
parseDOM: [{ tag: "b" }, { tag: "strong" }], parseDOM: [
{
tag: "b",
// Google Docs includes <b> tags with font-weight: normal so we need
// to account for this case specifically as not becoming bold when pasted.
getAttrs: (dom: HTMLElement) =>
normalWeightRegex.test(dom.style.fontWeight) ? false : null,
},
{ tag: "strong" },
{
style: "font-weight",
getAttrs: (style: string) => heavyWeightRegex.test(style) && null,
},
],
toDOM: () => ["strong"], toDOM: () => ["strong"],
}; };
} }

View File

@@ -12,7 +12,14 @@ export default class Italic extends Mark {
get schema(): MarkSpec { get schema(): MarkSpec {
return { return {
parseDOM: [{ tag: "i" }, { tag: "em" }], parseDOM: [
{ tag: "i" },
{ tag: "em" },
{
style: "font-style",
getAttrs: (value) => (value === "italic" ? null : false),
},
],
toDOM: () => ["em"], toDOM: () => ["em"],
}; };
} }

View File

@@ -20,6 +20,10 @@ export default class Strikethrough extends Mark {
{ {
tag: "strike", tag: "strike",
}, },
{
style: "text-decoration",
getAttrs: (value) => (value === "line-through" ? null : false),
},
], ],
toDOM: () => ["del", 0], toDOM: () => ["del", 0],
}; };

View File

@@ -15,7 +15,7 @@ export default class Underline extends Mark {
{ tag: "u" }, { tag: "u" },
{ {
style: "text-decoration", style: "text-decoration",
getAttrs: (value) => (value === "underline" ? {} : undefined), getAttrs: (value) => (value === "underline" ? null : false),
}, },
], ],
toDOM: () => ["u", 0], toDOM: () => ["u", 0],