Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

editor: make cell properties work with header cells (#2506)

Muhammad Ali874c89298615
diff --git a/packages/editor/src/extensions.ts b/packages/editor/src/extensions.ts
index b3aeb9b40..a8f1e2363 100644
--- a/packages/editor/src/extensions.ts
+++ b/packages/editor/src/extensions.ts
@@ -33,7 +33,7 @@ import "./extensions/ordered-list";
import "@tiptap/extension-color";
import "@tiptap/extension-table-row";
import "./extensions/table-cell";
-import "@tiptap/extension-table-header";
+import "./extensions/table-header";
import "./extensions/image";
import "./extensions/attachment";
import "./extensions/task-list";
diff --git a/packages/editor/src/extensions/table-cell/table-cell.ts b/packages/editor/src/extensions/table-cell/table-cell.ts
index c5b7f9dcf..a504b5464 100644
--- a/packages/editor/src/extensions/table-cell/table-cell.ts
+++ b/packages/editor/src/extensions/table-cell/table-cell.ts
@@ -18,7 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import TiptapTableCell from "@tiptap/extension-table-cell";
-import { Attribute } from "@tiptap/core";
+import { addStyleAttribute } from "./utils";
export const TableCell = TiptapTableCell.extend({
addAttributes() {
@@ -32,26 +32,3 @@ export const TableCell = TiptapTableCell.extend({
};
}
});
-
-function addStyleAttribute(
- name: keyof CSSStyleDeclaration,
- cssName: string,
- unit?: string
-): Partial<Attribute> {
- return {
- default: null,
- parseHTML: (element) =>
- unit
- ? element.style[name]?.toString().replace(unit, "")
- : element.style[name],
- renderHTML: (attributes) => {
- if (!attributes[name as string]) {
- return {};
- }
-
- return {
- style: `${cssName}: ${attributes[name as string]}${unit || ""}`
- };
- }
- };
-}
diff --git a/packages/editor/src/extensions/table-cell/utils.ts b/packages/editor/src/extensions/table-cell/utils.ts
new file mode 100644
index 000000000..5d3dd05c1
--- /dev/null
+++ b/packages/editor/src/extensions/table-cell/utils.ts
@@ -0,0 +1,43 @@
+/*
+This file is part of the Notesnook project (https://notesnook.com/)
+
+Copyright (C) 2023 Streetwriters (Private) Limited
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+import { Attribute } from "@tiptap/core";
+
+export function addStyleAttribute(
+ name: keyof CSSStyleDeclaration,
+ cssName: string,
+ unit?: string
+): Partial<Attribute> {
+ return {
+ default: null,
+ parseHTML: (element) =>
+ unit
+ ? element.style[name]?.toString().replace(unit, "")
+ : element.style[name],
+ renderHTML: (attributes) => {
+ if (!attributes[name as string]) {
+ return {};
+ }
+
+ return {
+ style: `${cssName}: ${attributes[name as string]}${unit || ""}`
+ };
+ }
+ };
+}
diff --git a/packages/editor/src/extensions/table-header/index.ts b/packages/editor/src/extensions/table-header/index.ts
new file mode 100644
index 000000000..7cd9ffe61
--- /dev/null
+++ b/packages/editor/src/extensions/table-header/index.ts
@@ -0,0 +1,23 @@
+/*
+This file is part of the Notesnook project (https://notesnook.com/)
+
+Copyright (C) 2023 Streetwriters (Private) Limited
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+import { TableHeader } from "./table-header";
+
+export * from "./table-header";
+
+export default TableHeader;
diff --git a/packages/editor/src/extensions/table-header/table-header.ts b/packages/editor/src/extensions/table-header/table-header.ts
new file mode 100644
index 000000000..786817f55
--- /dev/null
+++ b/packages/editor/src/extensions/table-header/table-header.ts
@@ -0,0 +1,34 @@
+/*
+This file is part of the Notesnook project (https://notesnook.com/)
+
+Copyright (C) 2023 Streetwriters (Private) Limited
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+import TipTapTableHeader from "@tiptap/extension-table-header";
+import { addStyleAttribute } from "../table-cell/utils";
+
+export const TableHeader = TipTapTableHeader.extend({
+ addAttributes() {
+ return {
+ ...this.parent?.(),
+ backgroundColor: addStyleAttribute("backgroundColor", "background-color"),
+ color: addStyleAttribute("color", "color"),
+ borderWidth: addStyleAttribute("borderWidth", "border-width", "px"),
+ borderStyle: addStyleAttribute("borderStyle", "border-style"),
+ borderColor: addStyleAttribute("borderColor", "border-color")
+ };
+ }
+});
diff --git a/packages/editor/src/index.ts b/packages/editor/src/index.ts
index bf7283d17..d0fdbdee9 100644
--- a/packages/editor/src/index.ts
+++ b/packages/editor/src/index.ts
@@ -31,7 +31,7 @@ import { Link } from "./extensions/link";
import Placeholder from "@tiptap/extension-placeholder";
import Subscript from "@tiptap/extension-subscript";
import Superscript from "@tiptap/extension-superscript";
-import TableHeader from "@tiptap/extension-table-header";
+import TableHeader from "./extensions/table-header";
import TableRow from "@tiptap/extension-table-row";
import TextAlign from "@tiptap/extension-text-align";
import TextStyle from "@tiptap/extension-text-style";