Skip to content
Source
Sign inSign up
fuko/trytanstackdb.com
Open History to see the last commit

editor-toolbar.tsx

232 lines7.5 KB

1import {
2 ChevronsRight,
3 Ellipsis,
4 Link as LinkIcon,
5 ListChecks,
6 Paperclip,
7 PenTool,
8 Search,
9 Share,
10 SquarePen,
11 Table,
12 UserRoundPlus,
13} from "lucide-react";
14import type { MouseEvent, ReactNode } from "react";
15​
16import {
17 DropdownMenu,
18 DropdownMenuContent,
19 DropdownMenuItem,
20 DropdownMenuSeparator,
21 DropdownMenuSub,
22 DropdownMenuSubContent,
23 DropdownMenuSubTrigger,
24 DropdownMenuTrigger,
25} from "@/components/ui/dropdown-menu";
26import type { Note } from "@/db/schema";
27import { useElementWidth } from "@/hooks/use-element-width";
28import { cn } from "@/lib/utils";
29​
30import { NoteActionItems } from "../note-actions";
31import { useComposeNote } from "../use-compose-note";
32​
33import { MacCircleButton, MacPill, MacPillButton } from "./controls";
34import { FORMAT_ITEMS, planToolbar, TOOLBAR_METRICS } from "./toolbar-plan";
35import type { ToolbarItemId } from "./toolbar-plan";
36​
37const ITEMS: Record<ToolbarItemId, { label: string; icon: ReactNode }> = {
38 format: {
39 label: "Format",
40 icon: <span className="text-[15px] font-medium tracking-tight">Aa</span>,
41 },
42 checklist: { label: "Checklist", icon: <ListChecks /> },
43 table: { label: "Table", icon: <Table /> },
44 attachment: { label: "Attach file", icon: <Paperclip /> },
45 markup: { label: "Markup", icon: <PenTool /> },
46 link: { label: "Add link", icon: <LinkIcon /> },
47 collaborate: { label: "Collaborate", icon: <UserRoundPlus /> },
48 share: { label: "Share", icon: <Share /> },
49 more: { label: "More", icon: <Ellipsis /> },
50};
51​
52interface EditorToolbarProps {
53 /** The open note, for the "•••" menu. */
54 note: Note | undefined;
55 /** The checklist button; `undefined` disables it. */
56 onToggleChecklist: (() => void) | undefined;
58​
59/** Buttons that act on the editor keep focus (and the caret) where it is. */
60const keepFocus = (event: MouseEvent) => event.preventDefault();
61​
62/**
63 * The editor half of the window toolbar. It measures its own width (the app
64 * pane is resizable) and collapses items into "»" when they don't fit.
65 */
66export function EditorToolbar({ note, onToggleChecklist }: EditorToolbarProps) {
67 const [ref, width] = useElementWidth<HTMLDivElement>();
68 const composeNote = useComposeNote();
69 const plan = planToolbar(width ?? Number.POSITIVE_INFINITY);
70 const shown = (id: ToolbarItemId) => plan.visible.has(id);
71 const formatItems = FORMAT_ITEMS.filter(shown);
72 const trailingPill = (["share", "more"] as const).filter(shown);
73​
74 return (
75 <div
76 ref={ref}
77 role="toolbar"
78 aria-label="Note"
79 className="grid h-[52px] w-full min-w-0 shrink-0 grid-cols-[minmax(auto,1fr)_auto_minmax(auto,1fr)] items-center"
80 style={{ paddingInline: TOOLBAR_METRICS.padding, columnGap: TOOLBAR_METRICS.gap }}
81 >
82 <div className="flex items-center">
83 <MacCircleButton
84 aria-label="New note"
85 title="New note"
86 disabled={!composeNote}
87 onClick={composeNote}
88 className="disabled:opacity-40"
89 >
90 <SquarePen />
91 </MacCircleButton>
92 </div>
93​
94 <div className="flex justify-center">
95 {formatItems.length > 0 && (
96 <MacPill>
97 {formatItems.map((id) =>
98 id === "checklist" ? (
99 <MacPillButton
100 key={id}
101 aria-label={ITEMS[id].label}
102 title={ITEMS[id].label}
103 disabled={!onToggleChecklist}
104 onMouseDown={keepFocus}
105 onClick={onToggleChecklist}
106 className="disabled:opacity-40"
107 >
108 {ITEMS[id].icon}
109 </MacPillButton>
110 ) : (
111 <MacPillButton key={id} aria-label={ITEMS[id].label} title={ITEMS[id].label}>
112 {ITEMS[id].icon}
113 </MacPillButton>
114 ),
115 )}
116 </MacPill>
117 )}
118 </div>
119​
120 <div className="flex items-center justify-end" style={{ gap: TOOLBAR_METRICS.gap }}>
121 {shown("collaborate") && (
122 <MacCircleButton aria-label={ITEMS.collaborate.label} title={ITEMS.collaborate.label}>
123 {ITEMS.collaborate.icon}
124 </MacCircleButton>
125 )}
126 {trailingPill.length > 0 && (
127 <MacPill>
128 {trailingPill.map((id) =>
129 id === "more" ? (
130 <NoteMenu key={id} note={note}>
131 <MacPillButton aria-label="Note actions" title="Note actions" disabled={!note}>
132 {ITEMS[id].icon}
133 </MacPillButton>
134 </NoteMenu>
135 ) : (
136 <MacPillButton key={id} aria-label={ITEMS[id].label} title={ITEMS[id].label}>
137 {ITEMS[id].icon}
138 </MacPillButton>
139 ),
140 )}
141 </MacPill>
142 )}
143 {plan.overflow.length > 0 && (
144 <OverflowMenu items={plan.overflow} note={note} onToggleChecklist={onToggleChecklist} />
145 )}
146 <SearchControl field={plan.searchField} />
147 </div>
148 </div>
149 );
151​
152function NoteMenu({ note, children }: { note: Note | undefined; children: ReactNode }) {
153 return (
154 <DropdownMenu>
155 <DropdownMenuTrigger asChild>{children}</DropdownMenuTrigger>
156 {note && (
157 <DropdownMenuContent align="end" className="min-w-44">
158 <NoteActionItems note={note} menu="dropdown" />
159 </DropdownMenuContent>
160 )}
161 </DropdownMenu>
162 );
164​
165function OverflowMenu({
166 items,
167 note,
168 onToggleChecklist,
169}: { items: ToolbarItemId[] } & EditorToolbarProps) {
170 const plain = items.filter((id) => id !== "more");
171 return (
172 <DropdownMenu>
173 <DropdownMenuTrigger asChild>
174 <MacCircleButton aria-label="More toolbar items" title="More toolbar items">
175 <ChevronsRight />
176 </MacCircleButton>
177 </DropdownMenuTrigger>
178 <DropdownMenuContent align="end" className="min-w-44">
179 {plain.map((id) => (
180 <DropdownMenuItem
181 key={id}
182 className="[&_svg]:text-muted-foreground"
183 disabled={id === "checklist" && !onToggleChecklist}
184 onSelect={id === "checklist" ? onToggleChecklist : undefined}
185 >
186 <span className="grid w-5 place-items-center [&>span]:text-xs">{ITEMS[id].icon}</span>
187 {ITEMS[id].label}
188 </DropdownMenuItem>
189 ))}
190 {items.includes("more") && note && (
191 <>
192 <DropdownMenuSeparator />
193 <DropdownMenuSub>
194 <DropdownMenuSubTrigger className="[&_svg]:text-muted-foreground">
195 <span className="grid w-5 place-items-center">{ITEMS.more.icon}</span>
196 Note
197 </DropdownMenuSubTrigger>
198 <DropdownMenuSubContent className="min-w-44">
199 <NoteActionItems note={note} menu="dropdown" />
200 </DropdownMenuSubContent>
201 </DropdownMenuSub>
202 </>
203 )}
204 </DropdownMenuContent>
205 </DropdownMenu>
206 );
208​
209function SearchControl({ field }: { field: boolean }) {
210 if (!field) {
211 return (
212 <MacCircleButton aria-label="Search" title="Search">
213 <Search />
214 </MacCircleButton>
215 );
216 }
217 return (
218 <label
219 className={cn(
220 "flex h-[34px] items-center gap-1.5 rounded-full border border-mac-control-border bg-mac-control px-3 text-mac-icon",
221 )}
222 style={{ width: TOOLBAR_METRICS.searchField }}
223 >
224 <Search className="size-[15px] shrink-0" />
225 <input
226 type="search"
227 placeholder="Search"
228 className="min-w-0 flex-1 bg-transparent text-[13px] text-mac-text outline-none placeholder:text-mac-text-secondary"
229 />
230 </label>
231 );