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

toolbar-plan.ts

104 lines2.9 KB

1/**
2 * Decides which editor toolbar items fit, the way the macOS Notes toolbar
3 * collapses: first the search field shrinks to an icon, then items move into
4 * a "»" overflow menu, least important first.
5 */
6​
7export type ToolbarItemId =
8 | "format"
9 | "checklist"
10 | "table"
11 | "attachment"
12 | "markup"
13 | "link"
14 | "collaborate"
15 | "share"
16 | "more";
17​
18/** Items in the centred formatting capsule, in display order. */
19export const FORMAT_ITEMS = [
20 "format",
21 "checklist",
22 "table",
23 "attachment",
24 "markup",
25 "link",
26] as const satisfies ToolbarItemId[];
27​
28/** Items on the right of the toolbar, in display order. */
29export const TRAILING_ITEMS = ["collaborate", "share", "more"] as const satisfies ToolbarItemId[];
30​
31/** Hidden first → hidden last. */
32const COLLAPSE_ORDER: ToolbarItemId[] = [
33 "more",
34 "share",
35 "collaborate",
36 "markup",
37 "attachment",
38 "link",
39 "table",
40 "checklist",
41 "format",
42];
43​
44export const TOOLBAR_METRICS = {
45 padding: 12,
46 gap: 8,
47 circle: 34,
48 pillItem: 32,
49 pillPadding: 10,
50 searchField: 200,
51} as const;
52​
53export interface ToolbarPlan {
54 visible: ReadonlySet<ToolbarItemId>;
55 /** Hidden items, in toolbar order, shown in the "»" menu. */
56 overflow: ToolbarItemId[];
57 searchField: boolean;
59​
60function pillWidth(items: number): number {
61 return items === 0 ? 0 : items * TOOLBAR_METRICS.pillItem + TOOLBAR_METRICS.pillPadding;
63​
64/** Total width the toolbar needs for a given set of visible items. */
65export function toolbarWidth(
66 visible: ReadonlySet<ToolbarItemId>,
67 { searchField, overflowButton }: { searchField: boolean; overflowButton: boolean },
68): number {
69 const { padding, gap, circle, searchField: fieldWidth } = TOOLBAR_METRICS;
70 const widths = [
71 circle, // compose
72 pillWidth(FORMAT_ITEMS.filter((id) => visible.has(id)).length),
73 visible.has("collaborate") ? circle : 0,
74 pillWidth(["share", "more"].filter((id) => visible.has(id as ToolbarItemId)).length),
75 overflowButton ? circle : 0,
76 searchField ? fieldWidth : circle,
77 ].filter((width) => width > 0);
78 return padding * 2 + widths.reduce((sum, width) => sum + width, 0) + gap * (widths.length - 1);
80​
81const ALL_ITEMS: ToolbarItemId[] = [...FORMAT_ITEMS, ...TRAILING_ITEMS];
82​
83export function planToolbar(availableWidth: number): ToolbarPlan {
84 const visible = new Set(ALL_ITEMS);
85​
86 if (toolbarWidth(visible, { searchField: true, overflowButton: false }) <= availableWidth) {
87 return { visible, overflow: [], searchField: true };
88 }
89 if (toolbarWidth(visible, { searchField: false, overflowButton: false }) <= availableWidth) {
90 return { visible, overflow: [], searchField: false };
91 }
92​
93 for (const id of COLLAPSE_ORDER) {
94 visible.delete(id);
95 if (toolbarWidth(visible, { searchField: false, overflowButton: true }) <= availableWidth)
96 break;
97 }
98​
99 return {
100 visible,
101 overflow: ALL_ITEMS.filter((id) => !visible.has(id)),
102 searchField: false,
103 };