Skip to content

Commit 6209aee

Browse files
authored
fix: color extension not working on issue description and published page (#5852)
* fix: color extension not working * chore: update types
1 parent 1099c59 commit 6209aee

File tree

9 files changed

+144
-174
lines changed

9 files changed

+144
-174
lines changed

packages/editor/src/core/components/menus/bubble-menu/color-selector.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ type Props = {
1616
export const BubbleMenuColorSelector: FC<Props> = (props) => {
1717
const { editor, isOpen, setIsOpen } = props;
1818

19-
const activeTextColor = COLORS_LIST.find((c) => editor.getAttributes("textStyle").color === c.key);
20-
const activeBackgroundColor = COLORS_LIST.find((c) => editor.getAttributes("textStyle").backgroundColor === c.key);
19+
const activeTextColor = COLORS_LIST.find((c) => TextColorItem(editor).isActive(c.key));
20+
const activeBackgroundColor = COLORS_LIST.find((c) => BackgroundColorItem(editor).isActive(c.key));
2121

2222
return (
2323
<div className="relative h-full">

packages/editor/src/core/components/menus/menu-items.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,15 +211,15 @@ export const ImageItem = (editor: Editor) =>
211211
export const TextColorItem = (editor: Editor): EditorMenuItem => ({
212212
key: "text-color",
213213
name: "Color",
214-
isActive: (color) => editor.getAttributes("textStyle").color === color,
214+
isActive: (color) => editor.isActive("customColor", { color }),
215215
command: (color: string) => toggleTextColor(color, editor),
216216
icon: Palette,
217217
});
218218

219219
export const BackgroundColorItem = (editor: Editor): EditorMenuItem => ({
220220
key: "background-color",
221221
name: "Background color",
222-
isActive: (color) => editor.getAttributes("textStyle").backgroundColor === color,
222+
isActive: (color) => editor.isActive("customColor", { backgroundColor: color }),
223223
command: (color: string) => toggleBackgroundColor(color, editor),
224224
icon: Palette,
225225
});

packages/editor/src/core/extensions/core-without-props.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ import { IssueWidgetWithoutProps } from "./issue-embed/issue-embed-without-props
1616
import { CustomMentionWithoutProps } from "./mentions/mentions-without-props";
1717
import { CustomQuoteExtension } from "./quote";
1818
import { TableHeader, TableCell, TableRow, Table } from "./table";
19-
import { CustomTextColorExtension } from "./custom-text-color";
20-
import { CustomBackgroundColorExtension } from "./custom-background-color";
19+
import { CustomColorExtension } from "./custom-color";
2120

2221
export const CoreEditorExtensionsWithoutProps = [
2322
StarterKit.configure({
@@ -85,8 +84,7 @@ export const CoreEditorExtensionsWithoutProps = [
8584
TableCell,
8685
TableRow,
8786
CustomMentionWithoutProps(),
88-
CustomTextColorExtension,
89-
CustomBackgroundColorExtension,
87+
CustomColorExtension,
9088
];
9189

9290
export const DocumentEditorExtensionsWithoutProps = [IssueWidgetWithoutProps()];

packages/editor/src/core/extensions/custom-background-color.ts

Lines changed: 0 additions & 78 deletions
This file was deleted.
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import { Mark, mergeAttributes } from "@tiptap/core";
2+
// constants
3+
import { COLORS_LIST } from "@/constants/common";
4+
5+
declare module "@tiptap/core" {
6+
interface Commands<ReturnType> {
7+
color: {
8+
/**
9+
* Set the text color
10+
* @param {string} color The color to set
11+
* @example editor.commands.setTextColor('red')
12+
*/
13+
setTextColor: (color: string) => ReturnType;
14+
15+
/**
16+
* Unset the text color
17+
* @example editor.commands.unsetTextColor()
18+
*/
19+
unsetTextColor: () => ReturnType;
20+
/**
21+
* Set the background color
22+
* @param {string} backgroundColor The color to set
23+
* @example editor.commands.setBackgroundColor('red')
24+
*/
25+
setBackgroundColor: (backgroundColor: string) => ReturnType;
26+
27+
/**
28+
* Unset the background color
29+
* @example editor.commands.unsetBackgroundColorColor()
30+
*/
31+
unsetBackgroundColor: () => ReturnType;
32+
};
33+
}
34+
}
35+
36+
export const CustomColorExtension = Mark.create({
37+
name: "customColor",
38+
39+
addOptions() {
40+
return {
41+
HTMLAttributes: {},
42+
};
43+
},
44+
45+
addAttributes() {
46+
return {
47+
color: {
48+
default: null,
49+
parseHTML: (element: HTMLElement) => element.getAttribute("data-text-color"),
50+
renderHTML: (attributes: { color: string }) => {
51+
const { color } = attributes;
52+
if (!color) {
53+
return {};
54+
}
55+
56+
let elementAttributes: Record<string, string> = {
57+
"data-text-color": color,
58+
};
59+
60+
if (!COLORS_LIST.find((c) => c.key === color)) {
61+
elementAttributes = {
62+
...elementAttributes,
63+
style: `color: ${color}`,
64+
};
65+
}
66+
67+
return elementAttributes;
68+
},
69+
},
70+
backgroundColor: {
71+
default: null,
72+
parseHTML: (element: HTMLElement) => element.getAttribute("data-background-color"),
73+
renderHTML: (attributes: { backgroundColor: string }) => {
74+
const { backgroundColor } = attributes;
75+
if (!backgroundColor) {
76+
return {};
77+
}
78+
79+
let elementAttributes: Record<string, string> = {
80+
"data-background-color": backgroundColor,
81+
};
82+
83+
if (!COLORS_LIST.find((c) => c.key === backgroundColor)) {
84+
elementAttributes = {
85+
...elementAttributes,
86+
style: `background-color: ${backgroundColor}`,
87+
};
88+
}
89+
90+
return elementAttributes;
91+
},
92+
},
93+
};
94+
},
95+
96+
parseHTML() {
97+
return [
98+
{
99+
tag: "span",
100+
getAttrs: (node) => node.getAttribute("data-text-color") && null,
101+
},
102+
{
103+
tag: "span",
104+
getAttrs: (node) => node.getAttribute("data-background-color") && null,
105+
},
106+
];
107+
},
108+
109+
renderHTML({ HTMLAttributes }) {
110+
return ["span", mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0];
111+
},
112+
113+
addCommands() {
114+
return {
115+
setTextColor:
116+
(color: string) =>
117+
({ chain }) =>
118+
chain().setMark(this.name, { color }).run(),
119+
unsetTextColor:
120+
() =>
121+
({ chain }) =>
122+
chain().setMark(this.name, { color: null }).run(),
123+
setBackgroundColor:
124+
(backgroundColor: string) =>
125+
({ chain }) =>
126+
chain().setMark(this.name, { backgroundColor }).run(),
127+
unsetBackgroundColor:
128+
() =>
129+
({ chain }) =>
130+
chain().setMark(this.name, { backgroundColor: null }).run(),
131+
};
132+
},
133+
});

packages/editor/src/core/extensions/custom-text-color.ts

Lines changed: 0 additions & 78 deletions
This file was deleted.

packages/editor/src/core/extensions/extensions.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,16 @@ import StarterKit from "@tiptap/starter-kit";
88
import { Markdown } from "tiptap-markdown";
99
// extensions
1010
import {
11-
CustomBackgroundColorExtension,
1211
CustomCodeBlockExtension,
1312
CustomCodeInlineExtension,
1413
CustomCodeMarkPlugin,
14+
CustomColorExtension,
1515
CustomHorizontalRule,
1616
CustomImageExtension,
1717
CustomKeymap,
1818
CustomLinkExtension,
1919
CustomMention,
2020
CustomQuoteExtension,
21-
CustomTextColorExtension,
2221
CustomTypographyExtension,
2322
DropHandlerExtension,
2423
ImageExtension,
@@ -155,7 +154,6 @@ export const CoreEditorExtensions = (args: TArguments) => {
155154
includeChildren: true,
156155
}),
157156
CharacterCount,
158-
CustomTextColorExtension,
159-
CustomBackgroundColorExtension,
157+
CustomColorExtension,
160158
];
161159
};

packages/editor/src/core/extensions/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ export * from "./slash-commands";
1010
export * from "./table";
1111
export * from "./typography";
1212
export * from "./core-without-props";
13-
export * from "./custom-background-color";
1413
export * from "./custom-code-inline";
15-
export * from "./custom-text-color";
14+
export * from "./custom-color";
1615
export * from "./drop";
1716
export * from "./enter-key-extension";
1817
export * from "./extensions";

packages/editor/src/core/extensions/read-only-extensions.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ import {
2121
CustomMention,
2222
HeadingListExtension,
2323
CustomReadOnlyImageExtension,
24-
CustomTextColorExtension,
25-
CustomBackgroundColorExtension,
24+
CustomColorExtension,
2625
} from "@/extensions";
2726
// helpers
2827
import { isValidHttpUrl } from "@/helpers/common";
@@ -123,8 +122,7 @@ export const CoreReadOnlyEditorExtensions = (props: Props) => {
123122
readonly: true,
124123
}),
125124
CharacterCount,
126-
CustomTextColorExtension,
127-
CustomBackgroundColorExtension,
125+
CustomColorExtension,
128126
HeadingListExtension,
129127
];
130128
};

0 commit comments

Comments
 (0)