Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ const NumberedListItemBlockContent = createStronglyTypedTiptapNode({
const startIndex =
parseInt(parent.getAttribute("start") || "1") || 1;

if (element.previousSibling || startIndex === 1) {
if (element.previousElementSibling || startIndex === 1) {
return {};
}

Expand Down
53 changes: 53 additions & 0 deletions packages/core/src/blocks/defaultProps.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Attribute } from "@tiptap/core";

import type { Props, PropSchema } from "../schema/index.js";

// TODO: this system should probably be moved / refactored.
Expand All @@ -22,3 +24,54 @@ export type DefaultProps = Props<typeof defaultProps>;
// `blockContent` nodes. Ensures that they are not redundantly added to
// a custom block's TipTap node attributes.
export const inheritedProps = ["backgroundColor", "textColor"];

export const getBackgroundColorAttribute = (
attributeName = "backgroundColor"
): Attribute => ({
default: defaultProps.backgroundColor.default,
parseHTML: (element) => {
if (element.hasAttribute("data-background-color")) {
return element.getAttribute("data-background-color");
}

if (element.style.backgroundColor) {
return element.style.backgroundColor;
}

return defaultProps.backgroundColor.default;
},
renderHTML: (attributes) => {
if (attributes[attributeName] === defaultProps.backgroundColor.default) {
return {};
}

return {
"data-background-color": attributes[attributeName],
};
},
});

export const getTextColorAttribute = (
attributeName = "textColor"
): Attribute => ({
default: defaultProps.textColor.default,
parseHTML: (element) => {
if (element.hasAttribute("data-text-color")) {
return element.getAttribute("data-text-color");
}

if (element.style.color) {
return element.style.color;
}

return defaultProps.textColor.default;
},
renderHTML: (attributes) => {
if (attributes[attributeName] === defaultProps.textColor.default) {
return {};
}
return {
"data-text-color": attributes[attributeName],
};
},
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Extension } from "@tiptap/core";
import { defaultProps } from "../../blocks/defaultProps.js";
import { getBackgroundColorAttribute } from "../../blocks/defaultProps.js";

export const BackgroundColorExtension = Extension.create({
name: "blockBackgroundColor",
Expand All @@ -9,24 +9,7 @@ export const BackgroundColorExtension = Extension.create({
{
types: ["blockContainer", "tableCell", "tableHeader"],
attributes: {
backgroundColor: {
default: defaultProps.backgroundColor.default,
parseHTML: (element) =>
element.hasAttribute("data-background-color")
? element.getAttribute("data-background-color")
: defaultProps.backgroundColor.default,
renderHTML: (attributes) => {
if (
attributes.backgroundColor ===
defaultProps.backgroundColor.default
) {
return {};
}
return {
"data-background-color": attributes.backgroundColor,
};
},
},
backgroundColor: getBackgroundColorAttribute(),
},
},
];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import { Mark } from "@tiptap/core";
import { getBackgroundColorAttribute } from "../../blocks/defaultProps.js";
import { createStyleSpecFromTipTapMark } from "../../schema/index.js";

const BackgroundColorMark = Mark.create({
name: "backgroundColor",

addAttributes() {
return {
stringValue: {
default: undefined,
parseHTML: (element) => element.getAttribute("data-background-color"),
renderHTML: (attributes) => ({
"data-background-color": attributes.stringValue,
}),
},
stringValue: getBackgroundColorAttribute("stringValue"),
};
},

Expand All @@ -25,10 +20,11 @@ const BackgroundColorMark = Mark.create({
return false;
}

if (element.hasAttribute("data-background-color")) {
return {
stringValue: element.getAttribute("data-background-color"),
};
if (
element.hasAttribute("data-background-color") ||
element.style.backgroundColor
) {
return {};
}

return false;
Expand Down
18 changes: 2 additions & 16 deletions packages/core/src/extensions/TextColor/TextColorExtension.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Extension } from "@tiptap/core";
import { defaultProps } from "../../blocks/defaultProps.js";
import { getTextColorAttribute } from "../../blocks/defaultProps.js";

export const TextColorExtension = Extension.create({
name: "blockTextColor",
Expand All @@ -9,21 +9,7 @@ export const TextColorExtension = Extension.create({
{
types: ["blockContainer", "tableCell", "tableHeader"],
attributes: {
textColor: {
default: defaultProps.textColor.default,
parseHTML: (element) =>
element.hasAttribute("data-text-color")
? element.getAttribute("data-text-color")
: defaultProps.textColor.default,
renderHTML: (attributes) => {
if (attributes.textColor === defaultProps.textColor.default) {
return {};
}
return {
"data-text-color": attributes.textColor,
};
},
},
textColor: getTextColorAttribute(),
},
},
];
Expand Down
14 changes: 5 additions & 9 deletions packages/core/src/extensions/TextColor/TextColorMark.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import { Mark } from "@tiptap/core";
import { getTextColorAttribute } from "../../blocks/defaultProps.js";
import { createStyleSpecFromTipTapMark } from "../../schema/index.js";

const TextColorMark = Mark.create({
name: "textColor",
priority: 1000,

addAttributes() {
return {
stringValue: {
default: undefined,
parseHTML: (element) => element.getAttribute("data-text-color"),
renderHTML: (attributes) => ({
"data-text-color": attributes.stringValue,
}),
},
stringValue: getTextColorAttribute("stringValue"),
};
},

Expand All @@ -25,8 +21,8 @@ const TextColorMark = Mark.create({
return false;
}

if (element.hasAttribute("data-text-color")) {
return { stringValue: element.getAttribute("data-text-color") };
if (element.hasAttribute("data-text-color") || element.style.color) {
return {};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return {}?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we don't need to parse the color because it's already handled in the parseHTML function of the stringValue attribute.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't we omit line 25 / 26 then entirely? it's still a bit confusing right? (maybe tiptap issue)

Copy link
Collaborator Author

@matthewlipski matthewlipski Apr 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because return {} tells TipTap that the element should get parsed as a textColor mark, and to just use the default attribute values for that mark (here it is equivalent to return { textColor: defaultProps.textColor.default }). The parseHTML function of the stringValue attribute then does its thing, parsing the actual value of stringValue and replacing the default one. Returning false instead means the mark never gets parsed at all.

This is a kind of weird TipTap thing especially with return {}, but it does mostly make sense.

We could choose to parse the actual value of stringValue within the parseHTML function of TextColorMark, but this would result in duplicated code between TextColorMark and TextColorExtension.

}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports[`Test ServerBlockNoteEditor > converts to HTML (blocksToFullHTML) 1`] = `"<div class="bn-block-group" data-node-type="blockGroup"><div class="bn-block-outer" data-node-type="blockOuter" data-id="1" data-text-color="yellow" data-background-color="blue"><div class="bn-block" data-node-type="blockContainer" data-id="1" data-text-color="yellow" data-background-color="blue"><div class="bn-block-content" data-content-type="heading" data-text-alignment="right" data-level="2"><h2 class="bn-inline-content"><strong><u>Heading </u></strong><em><s>2</s></em></h2></div><div class="bn-block-group" data-node-type="blockGroup"><div class="bn-block-outer" data-node-type="blockOuter" data-id="2" data-background-color="red"><div class="bn-block" data-node-type="blockContainer" data-id="2" data-background-color="red"><div class="bn-block-content" data-content-type="paragraph"><p class="bn-inline-content">Paragraph</p></div></div></div><div class="bn-block-outer" data-node-type="blockOuter" data-id="3"><div class="bn-block" data-node-type="blockContainer" data-id="3"><div class="bn-block-content" data-content-type="bulletListItem"><p class="bn-inline-content">list item</p></div></div></div></div></div></div><div class="bn-block-outer" data-node-type="blockOuter" data-id="4"><div class="bn-block" data-node-type="blockContainer" data-id="4"><div class="bn-block-content" data-content-type="image" data-name="Example" data-url="exampleURL" data-caption="Caption" data-preview-width="256" data-file-block=""><div class="bn-file-block-content-wrapper" style="width: 256px;"><div class="bn-visual-media-wrapper"><img class="bn-visual-media" src="exampleURL" alt="Example" draggable="false"></div><p class="bn-file-caption">Caption</p></div></div></div></div><div class="bn-block-outer" data-node-type="blockOuter" data-id="5"><div class="bn-block" data-node-type="blockContainer" data-id="5"><div class="bn-block-content" data-content-type="image" data-name="Example" data-url="exampleURL" data-caption="Caption" data-show-preview="false" data-preview-width="256" data-file-block=""><div class="bn-file-block-content-wrapper"><div class="bn-file-name-with-icon"><div class="bn-file-icon"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><path d="M3 8L9.00319 2H19.9978C20.5513 2 21 2.45531 21 2.9918V21.0082C21 21.556 20.5551 22 20.0066 22H3.9934C3.44476 22 3 21.5501 3 20.9932V8ZM10 4V9H5V20H19V4H10Z"></path></svg></div><p class="bn-file-name">Example</p></div><p class="bn-file-caption">Caption</p></div></div></div></div></div>"`;

exports[`Test ServerBlockNoteEditor > converts to and from HTML (blocksToHTMLLossy) 1`] = `"<h2 data-text-color="yellow" data-background-color="blue" data-text-alignment="right" data-level="2"><strong><u>Heading </u></strong><em><s>2</s></em></h2><p data-background-color="red">Paragraph</p><ul><li><p>list item</p></li></ul><figure data-name="Example" data-url="exampleURL" data-caption="Caption" data-preview-width="256"><img src="exampleURL" alt="Example" width="256"><figcaption>Caption</figcaption></figure><div data-name="Example" data-url="exampleURL" data-caption="Caption" data-show-preview="false" data-preview-width="256"><a href="exampleURL">Example</a><p>Caption</p></div>"`;
exports[`Test ServerBlockNoteEditor > converts to and from HTML (blocksToHTMLLossy) 1`] = `"<h2 class="bn-block-content" data-node-type="blockOuter" data-id="1" data-text-color="yellow" data-background-color="blue" data-content-type="heading" data-text-alignment="right" data-level="2"><strong><u>Heading </u></strong><em><s>2</s></em></h2><p class="bn-block-content" data-node-type="blockOuter" data-id="2" data-background-color="red" data-content-type="paragraph">Paragraph</p><ul><li class="bn-block-content" data-node-type="blockOuter" data-id="3" data-content-type="bulletListItem"><p class="bn-inline-content">list item</p></li></ul><figure class="bn-block-content" data-node-type="blockOuter" data-id="4" data-content-type="image" data-name="Example" data-url="exampleURL" data-caption="Caption" data-preview-width="256" data-file-block=""><img src="exampleURL" alt="Example" width="256"><figcaption>Caption</figcaption></figure><div class="bn-block-content" data-node-type="blockOuter" data-id="5" data-content-type="image" data-name="Example" data-url="exampleURL" data-caption="Caption" data-show-preview="false" data-preview-width="256" data-file-block=""><a href="exampleURL">Example</a><p>Caption</p></div>"`;

exports[`Test ServerBlockNoteEditor > converts to and from HTML (blocksToHTMLLossy) 2`] = `
[
Expand All @@ -26,12 +26,12 @@ exports[`Test ServerBlockNoteEditor > converts to and from HTML (blocksToHTMLLos
"type": "text",
},
],
"id": "0",
"id": "1",
"props": {
"backgroundColor": "default",
"backgroundColor": "blue",
"level": 2,
"textAlignment": "right",
"textColor": "default",
"textColor": "yellow",
},
"type": "heading",
},
Expand All @@ -44,9 +44,9 @@ exports[`Test ServerBlockNoteEditor > converts to and from HTML (blocksToHTMLLos
"type": "text",
},
],
"id": "1",
"id": "2",
"props": {
"backgroundColor": "default",
"backgroundColor": "red",
"textAlignment": "left",
"textColor": "default",
},
Expand All @@ -61,7 +61,7 @@ exports[`Test ServerBlockNoteEditor > converts to and from HTML (blocksToHTMLLos
"type": "text",
},
],
"id": "2",
"id": "3",
"props": {
"backgroundColor": "default",
"textAlignment": "left",
Expand All @@ -72,7 +72,7 @@ exports[`Test ServerBlockNoteEditor > converts to and from HTML (blocksToHTMLLos
{
"children": [],
"content": undefined,
"id": "3",
"id": "4",
"props": {
"backgroundColor": "default",
"caption": "Caption",
Expand All @@ -86,43 +86,18 @@ exports[`Test ServerBlockNoteEditor > converts to and from HTML (blocksToHTMLLos
},
{
"children": [],
"content": [
{
"content": [
{
"styles": {},
"text": "Example",
"type": "text",
},
],
"href": "exampleURL",
"type": "link",
},
],
"id": "4",
"props": {
"backgroundColor": "default",
"textAlignment": "left",
"textColor": "default",
},
"type": "paragraph",
},
{
"children": [],
"content": [
{
"styles": {},
"text": "Caption",
"type": "text",
},
],
"content": undefined,
"id": "5",
"props": {
"backgroundColor": "default",
"caption": "Caption",
"name": "Example",
"previewWidth": 256,
"showPreview": false,
"textAlignment": "left",
"textColor": "default",
"url": "exampleURL",
},
"type": "paragraph",
"type": "image",
},
]
`;
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<div class="bn-block-column-list" data-node-type="columnList" data-id="1" style="display: flex;"><div class="bn-block-column" data-node-type="column" data-id="2" data-width="1" style="flex-grow: 1;"><p>Column Paragraph 0</p><p>Column Paragraph 1</p></div><div class="bn-block-column" data-node-type="column" data-id="5" data-width="1" style="flex-grow: 1;"><p>Column Paragraph 2</p><p>Column Paragraph 3</p></div></div>
<div class="bn-block-column-list" data-node-type="columnList" data-id="1" style="display: flex;"><div class="bn-block-column" data-node-type="column" data-id="2" data-width="1" style="flex-grow: 1;"><p class="bn-block-content" data-node-type="blockOuter" data-id="3" data-content-type="paragraph">Column Paragraph 0</p><p class="bn-block-content" data-node-type="blockOuter" data-id="4" data-content-type="paragraph">Column Paragraph 1</p></div><div class="bn-block-column" data-node-type="column" data-id="5" data-width="1" style="flex-grow: 1;"><p class="bn-block-content" data-node-type="blockOuter" data-id="6" data-content-type="paragraph">Column Paragraph 2</p><p class="bn-block-content" data-node-type="blockOuter" data-id="7" data-content-type="paragraph">Column Paragraph 3</p></div></div>
Loading
Loading