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