From b5359a305f9807681cb5f70da5a32013cda6e337 Mon Sep 17 00:00:00 2001 From: Hans Pagel Date: Fri, 2 Oct 2020 21:39:16 +0200 Subject: [PATCH] add color support --- docs/src/demos/Extensions/Highlight/index.vue | 1 + packages/extension-highlight/index.ts | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/docs/src/demos/Extensions/Highlight/index.vue b/docs/src/demos/Extensions/Highlight/index.vue index d7b3ea50a..bf881cd4d 100644 --- a/docs/src/demos/Extensions/Highlight/index.vue +++ b/docs/src/demos/Extensions/Highlight/index.vue @@ -38,6 +38,7 @@ export default { content: `

This isn’t highlighted.

But this one is.

+

And this is highlighted too, but in a different color.

`, }) }, diff --git a/packages/extension-highlight/index.ts b/packages/extension-highlight/index.ts index 30c71eb66..fcf8aa162 100644 --- a/packages/extension-highlight/index.ts +++ b/packages/extension-highlight/index.ts @@ -13,12 +13,25 @@ declare module '@tiptap/core/src/Editor' { export default new Mark() .name('highlight') .schema(() => ({ + attrs: { + color: { + default: null, + }, + }, parseDOM: [ { tag: 'mark', + getAttrs: node => { + return { + color: (node as HTMLElement).style.backgroundColor, + } + }, }, ], - toDOM: () => ['mark', 0], + toDOM: node => ['mark', { + ...node.attrs, + style: node.attrs.color && `background-color: ${node.attrs.color};`, + }, 0], })) .commands(({ name }) => ({ highlight: () => ({ commands }) => {