返回 presentation-ai
table-node-static.tsx
根目录 / src / components / plate / ui / table-node-static.tsx
1 import { BaseTablePlugin } from "@platejs/table";
2 import { type TTableCellElement, type TTableElement } from "platejs";
3 import { SlateElement, type SlateElementProps } from "platejs/static";
4 import type * as React from "react";
5
6 import { cn } from "@/lib/utils";
7
8 export function TableElementStatic({
9 children,
10 ...props
11 }: SlateElementProps<TTableElement>) {
12 const { disableMarginLeft } = props.editor.getOptions(BaseTablePlugin);
13 const marginLeft = disableMarginLeft ? 0 : props.element.marginLeft;
14
15 return (
16 <SlateElement
17 {...props}
18 className="overflow-x-auto py-5 transition-all duration-300"
19 style={{ paddingLeft: marginLeft }}
20 >
21 <div className="group/table relative w-full bg-transparent">
22 <table className="mr-0 ml-px table h-px w-full max-w-[calc(100%-2rem)] table-fixed border-collapse bg-transparent text-(--presentation-text)">
23 <tbody className="w-full">{children}</tbody>
24 </table>
25 </div>
26 </SlateElement>
27 );
28 }
29
30 export function TableRowElementStatic(props: SlateElementProps) {
31 return (
32 <SlateElement {...props} as="tr" className="h-full">
33 {props.children}
34 </SlateElement>
35 );
36 }
37
38 export function TableCellElementStatic({
39 isHeader,
40 ...props
41 }: SlateElementProps<TTableCellElement> & {
42 isHeader?: boolean;
43 }) {
44 const { editor, element } = props;
45 const { api } = editor.getPlugin(BaseTablePlugin);
46
47 const { minHeight } = api.table.getCellSize({ element });
48 const borders = api.table.getCellBorders({ element });
49
50 return (
51 <SlateElement
52 {...props}
53 as={isHeader ? "th" : "td"}
54 className={cn(
55 "h-full overflow-visible border-none bg-transparent p-0",
56 element.background ? "bg-(--cellBackground)" : "bg-transparent",
57 isHeader && "text-left *:m-0",
58 "before:size-full",
59 "before:absolute before:box-border before:content-[''] before:select-none",
60 borders &&
61 cn(
62 borders.bottom?.size && `before:border-b before:border-b-border`,
63 borders.right?.size && `before:border-r before:border-r-border`,
64 borders.left?.size && `before:border-l before:border-l-border`,
65 borders.top?.size && `before:border-t before:border-t-border`,
66 ),
67 )}
68 style={
69 {
70 "--cellBackground": element.background,
71 ...(isHeader && {
72 backgroundColor:
73 element.color || "var(--presentation-card-background)",
74 }),
75 } as React.CSSProperties
76 }
77 attributes={{
78 ...props.attributes,
79 colSpan: api.table.getColSpan(element),
80 rowSpan: api.table.getRowSpan(element),
81 }}
82 >
83 <div
84 className={cn(
85 "relative z-20 box-border h-full rounded-md px-3 py-2",
86 isHeader ? "text-lg font-bold text-primary" : "presentation-text",
87 )}
88 style={{ minHeight }}
89 >
90 {props.children}
91 </div>
92 </SlateElement>
93 );
94 }
95
96 export function TableCellHeaderElementStatic(
97 props: SlateElementProps<TTableCellElement>,
98 ) {
99 return <TableCellElementStatic {...props} isHeader />;
100 }
101
101 lines Plain Text