-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat]: Tiptap table integration (#2008)
* added basic table support * fixed table position at bottom * fixed image node deletion logic's regression issue * added compatible styles * enabled slash commands * disabled slash command and bubble menu's node selector for table cells * added dropcursor support to type below the table/image * blocked image uploads for handledrop and paste actions
- Loading branch information
1 parent
320608e
commit 38b7f43
Showing
15 changed files
with
306 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { TableCell } from "@tiptap/extension-table-cell"; | ||
|
||
export const CustomTableCell = TableCell.extend({ | ||
addAttributes() { | ||
return { | ||
...this.parent?.(), | ||
isHeader: { | ||
default: false, | ||
parseHTML: (element) => { isHeader: element.tagName === "TD" }, | ||
renderHTML: (attributes) => { tag: attributes.isHeader ? "th" : "td" } | ||
}, | ||
}; | ||
}, | ||
renderHTML({ HTMLAttributes }) { | ||
if (HTMLAttributes.isHeader) { | ||
return [ | ||
"th", | ||
{ | ||
...HTMLAttributes, | ||
class: `relative ${HTMLAttributes.class}`, | ||
}, | ||
[ | ||
"span", | ||
{ class: "absolute top-0 right-0" }, | ||
], | ||
0, | ||
]; | ||
} | ||
return ["td", HTMLAttributes, 0]; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { TableHeader as BaseTableHeader } from "@tiptap/extension-table-header"; | ||
|
||
const TableHeader = BaseTableHeader.extend({ | ||
content: "paragraph" | ||
}); | ||
|
||
export { TableHeader }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Table as BaseTable } from "@tiptap/extension-table"; | ||
|
||
const Table = BaseTable.configure({ | ||
resizable: true, | ||
cellMinWidth: 100, | ||
allowTableNodeSelection: true | ||
}); | ||
|
||
export { Table }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { useState, useEffect } from "react"; | ||
import { Rows, Columns, ToggleRight } from "lucide-react"; | ||
import { cn } from "../utils"; | ||
|
||
interface TableMenuItem { | ||
name: string; | ||
command: () => void; | ||
icon: any; | ||
} | ||
|
||
export const findTableAncestor = (node: Node | null): HTMLTableElement | null => { | ||
while (node !== null && node.nodeName !== "TABLE") { | ||
node = node.parentNode; | ||
} | ||
return node as HTMLTableElement; | ||
}; | ||
|
||
export const TableMenu = ({ editor }: { editor: any }) => { | ||
const [tableLocation, setTableLocation] = useState({ bottom: 0, left: 0 }); | ||
const items: TableMenuItem[] = [ | ||
{ | ||
name: "Insert Column right", | ||
command: () => editor.chain().focus().addColumnBefore().run(), | ||
icon: Columns, | ||
}, | ||
{ | ||
name: "Insert Row below", | ||
command: () => editor.chain().focus().addRowAfter().run(), | ||
icon: Rows, | ||
}, | ||
{ | ||
name: "Delete Column", | ||
command: () => editor.chain().focus().deleteColumn().run(), | ||
icon: Columns, | ||
}, | ||
{ | ||
name: "Delete Rows", | ||
command: () => editor.chain().focus().deleteRow().run(), | ||
icon: Rows, | ||
}, | ||
{ | ||
name: "Toggle Header Row", | ||
command: () => editor.chain().focus().toggleHeaderRow().run(), | ||
icon: ToggleRight, | ||
} | ||
|
||
]; | ||
|
||
useEffect(() => { | ||
if (typeof window !== "undefined") { | ||
const handleWindowClick = () => { | ||
const selection: any = window?.getSelection(); | ||
if (selection.rangeCount !== 0) { | ||
const range = selection.getRangeAt(0); | ||
const tableNode = findTableAncestor(range.startContainer); | ||
if (tableNode) { | ||
const tableRect = tableNode.getBoundingClientRect(); | ||
const tableCenter = tableRect.left + tableRect.width / 2; | ||
const menuWidth = 45; | ||
const menuLeft = tableCenter - menuWidth / 2; | ||
const tableBottom = tableRect.bottom; | ||
setTableLocation({ bottom: tableBottom, left: menuLeft }); | ||
} | ||
} | ||
} | ||
|
||
window.addEventListener("click", handleWindowClick); | ||
|
||
return () => { | ||
window.removeEventListener("click", handleWindowClick); | ||
}; | ||
} | ||
}, [tableLocation]); | ||
|
||
return ( | ||
<section | ||
className="fixed left-1/2 transform -translate-x-1/2 overflow-hidden rounded border border-custom-border-300 bg-custom-background-100 shadow-xl" | ||
style={{ bottom: `calc(100vh - ${tableLocation.bottom + 45}px)`, left: `${tableLocation.left}px` }} | ||
> | ||
{items.map((item, index) => ( | ||
<button | ||
key={index} | ||
onClick={item.command} | ||
className="p-2 text-custom-text-200 hover:bg-text-custom-text-100 hover:bg-custom-primary-100/10 active:bg-custom-background-100" | ||
title={item.name} | ||
> | ||
<item.icon | ||
className={cn("h-5 w-5 text-lg", { | ||
"text-red-600": item.name.includes("Delete"), | ||
})} | ||
/> | ||
</button> | ||
))} | ||
</section> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
38b7f43
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
plane-dev – ./apps/app
plane-dev-git-develop-plane.vercel.app
plane-dev.vercel.app
plane-dev-plane.vercel.app