2023-08-31 08:11:41 +00:00
|
|
|
import { useState, useEffect } from "react";
|
|
|
|
import { Rows, Columns, ToggleRight } from "lucide-react";
|
|
|
|
import { cn } from "../utils";
|
2023-08-31 11:00:28 +00:00
|
|
|
import { Tooltip } from "components/ui";
|
2023-09-01 19:15:34 +00:00
|
|
|
import InsertLeftTableIcon from "./InsertLeftTableIcon";
|
|
|
|
import InsertRightTableIcon from "./InsertRightTableIcon";
|
|
|
|
import InsertTopTableIcon from "./InsertTopTableIcon";
|
|
|
|
import InsertBottomTableIcon from "./InsertBottomTableIcon";
|
2023-08-31 08:11:41 +00:00
|
|
|
|
|
|
|
interface TableMenuItem {
|
|
|
|
command: () => void;
|
|
|
|
icon: any;
|
2023-08-31 11:00:28 +00:00
|
|
|
key: string;
|
|
|
|
name: string;
|
2023-08-31 08:11:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 });
|
2023-08-31 11:00:28 +00:00
|
|
|
const isOpen = editor?.isActive("table");
|
|
|
|
|
2023-08-31 08:11:41 +00:00
|
|
|
const items: TableMenuItem[] = [
|
|
|
|
{
|
|
|
|
command: () => editor.chain().focus().addColumnBefore().run(),
|
2023-09-01 19:15:34 +00:00
|
|
|
icon: InsertLeftTableIcon,
|
|
|
|
key: "insert-column-left",
|
|
|
|
name: "Insert 1 column left",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
command: () => editor.chain().focus().addColumnAfter().run(),
|
|
|
|
icon: InsertRightTableIcon,
|
2023-08-31 11:00:28 +00:00
|
|
|
key: "insert-column-right",
|
|
|
|
name: "Insert 1 column right",
|
2023-08-31 08:11:41 +00:00
|
|
|
},
|
2023-09-01 19:15:34 +00:00
|
|
|
{
|
|
|
|
command: () => editor.chain().focus().addRowBefore().run(),
|
|
|
|
icon: InsertTopTableIcon,
|
|
|
|
key: "insert-row-above",
|
|
|
|
name: "Insert 1 row above",
|
|
|
|
},
|
2023-08-31 08:11:41 +00:00
|
|
|
{
|
|
|
|
command: () => editor.chain().focus().addRowAfter().run(),
|
2023-09-01 19:15:34 +00:00
|
|
|
icon: InsertBottomTableIcon,
|
2023-08-31 11:00:28 +00:00
|
|
|
key: "insert-row-below",
|
|
|
|
name: "Insert 1 row below",
|
2023-08-31 08:11:41 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
command: () => editor.chain().focus().deleteColumn().run(),
|
|
|
|
icon: Columns,
|
2023-08-31 11:00:28 +00:00
|
|
|
key: "delete-column",
|
|
|
|
name: "Delete column",
|
2023-08-31 08:11:41 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
command: () => editor.chain().focus().deleteRow().run(),
|
|
|
|
icon: Rows,
|
2023-08-31 11:00:28 +00:00
|
|
|
key: "delete-row",
|
|
|
|
name: "Delete row",
|
2023-08-31 08:11:41 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
command: () => editor.chain().focus().toggleHeaderRow().run(),
|
|
|
|
icon: ToggleRight,
|
2023-08-31 11:00:28 +00:00
|
|
|
key: "toggle-header-row",
|
|
|
|
name: "Toggle header row",
|
|
|
|
},
|
2023-08-31 08:11:41 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-08-31 11:00:28 +00:00
|
|
|
if (!window) return;
|
|
|
|
|
|
|
|
const handleWindowClick = () => {
|
|
|
|
const selection: any = window?.getSelection();
|
|
|
|
|
|
|
|
if (selection.rangeCount !== 0) {
|
|
|
|
const range = selection.getRangeAt(0);
|
|
|
|
const tableNode = findTableAncestor(range.startContainer);
|
|
|
|
|
|
|
|
let parent = tableNode?.parentElement;
|
|
|
|
|
|
|
|
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 });
|
|
|
|
|
|
|
|
while (parent) {
|
|
|
|
if (!parent.classList.contains("disable-scroll"))
|
|
|
|
parent.classList.add("disable-scroll");
|
|
|
|
parent = parent.parentElement;
|
2023-08-31 08:11:41 +00:00
|
|
|
}
|
2023-08-31 11:00:28 +00:00
|
|
|
} else {
|
|
|
|
const scrollDisabledContainers = document.querySelectorAll(".disable-scroll");
|
|
|
|
|
|
|
|
scrollDisabledContainers.forEach((container) => {
|
|
|
|
container.classList.remove("disable-scroll");
|
|
|
|
});
|
2023-08-31 08:11:41 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-31 11:00:28 +00:00
|
|
|
};
|
2023-08-31 08:11:41 +00:00
|
|
|
|
2023-08-31 11:00:28 +00:00
|
|
|
window.addEventListener("click", handleWindowClick);
|
2023-08-31 08:11:41 +00:00
|
|
|
|
2023-08-31 11:00:28 +00:00
|
|
|
return () => {
|
|
|
|
window.removeEventListener("click", handleWindowClick);
|
|
|
|
};
|
|
|
|
}, [tableLocation, editor]);
|
2023-08-31 08:11:41 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<section
|
2023-09-03 13:20:30 +00:00
|
|
|
className={`fixed left-1/2 transform -translate-x-1/2 overflow-hidden rounded border border-custom-border-300 bg-custom-background-100 shadow-custom-shadow-sm p-1 ${
|
|
|
|
isOpen ? "block" : "hidden"
|
|
|
|
}`}
|
2023-08-31 11:00:28 +00:00
|
|
|
style={{
|
|
|
|
bottom: `calc(100vh - ${tableLocation.bottom + 45}px)`,
|
|
|
|
left: `${tableLocation.left}px`,
|
|
|
|
}}
|
2023-08-31 08:11:41 +00:00
|
|
|
>
|
|
|
|
{items.map((item, index) => (
|
2023-08-31 11:00:28 +00:00
|
|
|
<Tooltip key={index} tooltipContent={item.name}>
|
|
|
|
<button
|
|
|
|
onClick={item.command}
|
|
|
|
className="p-1.5 text-custom-text-200 hover:bg-text-custom-text-100 hover:bg-custom-background-80 active:bg-custom-background-80 rounded"
|
|
|
|
title={item.name}
|
|
|
|
>
|
|
|
|
<item.icon
|
|
|
|
className={cn("h-4 w-4 text-lg", {
|
|
|
|
"text-red-600": item.key.includes("delete"),
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
</button>
|
|
|
|
</Tooltip>
|
2023-08-31 08:11:41 +00:00
|
|
|
))}
|
|
|
|
</section>
|
|
|
|
);
|
|
|
|
};
|