返回 presentation-ai
align-toolbar-button.tsx
根目录 / src / components / plate / ui / align-toolbar-button.tsx
1 "use client";
2
3 import { type Alignment } from "@platejs/basic-styles";
4 import { TextAlignPlugin } from "@platejs/basic-styles/react";
5 import { type DropdownMenuProps } from "@radix-ui/react-dropdown-menu";
6 import {
7 AlignCenterIcon,
8 AlignJustifyIcon,
9 AlignLeftIcon,
10 AlignRightIcon,
11 } from "lucide-react";
12 import { useEditorPlugin, useSelectionFragmentProp } from "platejs/react";
13 import * as React from "react";
14
15 import {
16 DropdownMenu,
17 DropdownMenuContent,
18 DropdownMenuRadioGroup,
19 DropdownMenuRadioItem,
20 DropdownMenuTrigger,
21 } from "@/components/plate/ui/dropdown-menu";
22 import { ToolbarButton } from "./toolbar";
23
24 const items = [
25 {
26 icon: AlignLeftIcon,
27 value: "left",
28 },
29 {
30 icon: AlignCenterIcon,
31 value: "center",
32 },
33 {
34 icon: AlignRightIcon,
35 value: "right",
36 },
37 {
38 icon: AlignJustifyIcon,
39 value: "justify",
40 },
41 ];
42
43 export function AlignToolbarButton(props: DropdownMenuProps) {
44 const { editor, tf } = useEditorPlugin(TextAlignPlugin);
45 const value =
46 useSelectionFragmentProp({
47 defaultValue: "start",
48 getProp: (node) => node.align,
49 }) ?? "left";
50
51 const [open, setOpen] = React.useState(false);
52 const IconValue =
53 items.find((item) => item.value === value)?.icon ?? AlignLeftIcon;
54
55 return (
56 <DropdownMenu open={open} onOpenChange={setOpen} modal={false} {...props}>
57 <DropdownMenuTrigger asChild>
58 <ToolbarButton pressed={open} tooltip="Align" isDropdown>
59 <IconValue />
60 </ToolbarButton>
61 </DropdownMenuTrigger>
62
63 <DropdownMenuContent
64 className="ignore-click-outside/toolbar min-w-0"
65 align="start"
66 >
67 <DropdownMenuRadioGroup
68 value={value}
69 onValueChange={(value) => {
70 tf.textAlign.setNodes(value as Alignment);
71 editor.tf.focus();
72 }}
73 >
74 {items.map(({ icon: Icon, value: itemValue }) => (
75 <DropdownMenuRadioItem
76 key={itemValue}
77 className="pl-2 data-[state=checked]:bg-accent [span]:first:*:hidden"
78 value={itemValue}
79 >
80 <Icon />
81 </DropdownMenuRadioItem>
82 ))}
83 </DropdownMenuRadioGroup>
84 </DropdownMenuContent>
85 </DropdownMenu>
86 );
87 }
88
88 lines Plain Text