From f48c86c56dea33efad6895ebeba922c17fe26ee0 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Fri, 1 Apr 2022 15:13:44 -0700 Subject: [PATCH] fix: Improve paste handler parsing for more cases, specifically Google Docs (#3322) --- shared/editor/marks/Bold.ts | 18 +++++++++++++++++- shared/editor/marks/Italic.ts | 9 ++++++++- shared/editor/marks/Strikethrough.ts | 4 ++++ shared/editor/marks/Underline.ts | 2 +- 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/shared/editor/marks/Bold.ts b/shared/editor/marks/Bold.ts index f4c94e756..fce24c32e 100644 --- a/shared/editor/marks/Bold.ts +++ b/shared/editor/marks/Bold.ts @@ -4,6 +4,9 @@ import { MarkSpec, MarkType } from "prosemirror-model"; import markInputRule from "../lib/markInputRule"; 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 { get name() { return "strong"; @@ -11,7 +14,20 @@ export default class Bold extends Mark { get schema(): MarkSpec { return { - parseDOM: [{ tag: "b" }, { tag: "strong" }], + parseDOM: [ + { + tag: "b", + // Google Docs includes 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"], }; } diff --git a/shared/editor/marks/Italic.ts b/shared/editor/marks/Italic.ts index b2191dc11..51ee3d1a7 100644 --- a/shared/editor/marks/Italic.ts +++ b/shared/editor/marks/Italic.ts @@ -12,7 +12,14 @@ export default class Italic extends Mark { get schema(): MarkSpec { return { - parseDOM: [{ tag: "i" }, { tag: "em" }], + parseDOM: [ + { tag: "i" }, + { tag: "em" }, + { + style: "font-style", + getAttrs: (value) => (value === "italic" ? null : false), + }, + ], toDOM: () => ["em"], }; } diff --git a/shared/editor/marks/Strikethrough.ts b/shared/editor/marks/Strikethrough.ts index a76746ea0..cf55a5868 100644 --- a/shared/editor/marks/Strikethrough.ts +++ b/shared/editor/marks/Strikethrough.ts @@ -20,6 +20,10 @@ export default class Strikethrough extends Mark { { tag: "strike", }, + { + style: "text-decoration", + getAttrs: (value) => (value === "line-through" ? null : false), + }, ], toDOM: () => ["del", 0], }; diff --git a/shared/editor/marks/Underline.ts b/shared/editor/marks/Underline.ts index 876bdaed5..e606d5407 100644 --- a/shared/editor/marks/Underline.ts +++ b/shared/editor/marks/Underline.ts @@ -15,7 +15,7 @@ export default class Underline extends Mark { { tag: "u" }, { style: "text-decoration", - getAttrs: (value) => (value === "underline" ? {} : undefined), + getAttrs: (value) => (value === "underline" ? null : false), }, ], toDOM: () => ["u", 0],