| 1 | "use client"; |
| 2 | |
| 3 | import { setColumns } from "@platejs/layout"; |
| 4 | import { |
| 5 | Columns2, |
| 6 | Columns3, |
| 7 | PanelLeft, |
| 8 | PanelRight, |
| 9 | SquareSplitVertical, |
| 10 | } from "lucide-react"; |
| 11 | import { type TElement } from "platejs"; |
| 12 | |
| 13 | import { COLUMN_GROUP } from "@/components/notebook/presentation/editor/lib"; |
| 14 | import { ToolbarButton, ToolbarGroup } from "@/components/plate/ui/toolbar"; |
| 15 | import { useToolbarContext } from "./ToolbarContext"; |
| 16 | |
| 17 | const COLUMN_LAYOUT_PRESETS = [ |
| 18 | { |
| 19 | icon: Columns2, |
| 20 | tooltip: "Equal columns", |
| 21 | widths: ["50%", "50%"], |
| 22 | }, |
| 23 | { |
| 24 | icon: Columns3, |
| 25 | tooltip: "Three equal columns", |
| 26 | widths: ["33.33%", "33.33%", "33.34%"], |
| 27 | }, |
| 28 | { |
| 29 | icon: PanelRight, |
| 30 | tooltip: "Wide left column", |
| 31 | widths: ["70%", "30%"], |
| 32 | }, |
| 33 | { |
| 34 | icon: PanelLeft, |
| 35 | tooltip: "Wide right column", |
| 36 | widths: ["30%", "70%"], |
| 37 | }, |
| 38 | { |
| 39 | icon: SquareSplitVertical, |
| 40 | tooltip: "Wide center column", |
| 41 | widths: ["25%", "50%", "25%"], |
| 42 | }, |
| 43 | ] as const; |
| 44 | |
| 45 | export function ColumnLayoutControls() { |
| 46 | const { editor, element, elementType } = useToolbarContext(); |
| 47 | |
| 48 | if (elementType !== COLUMN_GROUP || !element) { |
| 49 | return null; |
| 50 | } |
| 51 | |
| 52 | return ( |
| 53 | <ToolbarGroup> |
| 54 | {COLUMN_LAYOUT_PRESETS.map(({ icon: Icon, tooltip, widths }) => ( |
| 55 | <ToolbarButton |
| 56 | key={tooltip} |
| 57 | onClick={() => { |
| 58 | const elementPath = editor.api.findPath(element as TElement); |
| 59 | if (!elementPath) return; |
| 60 | |
| 61 | setColumns(editor, { |
| 62 | at: elementPath, |
| 63 | widths: [...widths], |
| 64 | }); |
| 65 | }} |
| 66 | tooltip={tooltip} |
| 67 | > |
| 68 | <Icon className="size-4" /> |
| 69 | </ToolbarButton> |
| 70 | ))} |
| 71 | </ToolbarGroup> |
| 72 | ); |
| 73 | } |
| 74 |