@sincpro/web-composer 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +198 -2
  3. package/dist/domain/enums.d.ts +65 -0
  4. package/dist/domain/enums.js +56 -0
  5. package/dist/domain/index.d.ts +9 -0
  6. package/dist/domain/index.js +8 -0
  7. package/dist/domain/layout.d.ts +75 -0
  8. package/dist/domain/layout.js +180 -0
  9. package/dist/domain/palette.d.ts +42 -0
  10. package/dist/domain/palette.js +89 -0
  11. package/dist/domain/registry.d.ts +112 -0
  12. package/dist/domain/registry.js +37 -0
  13. package/dist/domain/resource.d.ts +21 -0
  14. package/dist/domain/resource.js +62 -0
  15. package/dist/domain/url.d.ts +3 -0
  16. package/dist/domain/url.js +107 -0
  17. package/dist/domain/workspace.d.ts +138 -0
  18. package/dist/domain/workspace.js +272 -0
  19. package/dist/index.d.ts +20 -0
  20. package/dist/index.js +19 -0
  21. package/dist/shell/Composer.d.ts +24 -0
  22. package/dist/shell/Composer.js +59 -0
  23. package/dist/shell/Palette.d.ts +9 -0
  24. package/dist/shell/Palette.js +123 -0
  25. package/dist/shell/PanelFrame.d.ts +31 -0
  26. package/dist/shell/PanelFrame.js +44 -0
  27. package/dist/shell/PanelHost.d.ts +23 -0
  28. package/dist/shell/PanelHost.js +188 -0
  29. package/dist/shell/RecordLink.d.ts +12 -0
  30. package/dist/shell/RecordLink.js +51 -0
  31. package/dist/shell/ViewHost.d.ts +11 -0
  32. package/dist/shell/ViewHost.js +42 -0
  33. package/dist/shell/copy.d.ts +64 -0
  34. package/dist/shell/copy.js +108 -0
  35. package/dist/state/api.d.ts +32 -0
  36. package/dist/state/api.js +140 -0
  37. package/dist/state/drag.d.ts +31 -0
  38. package/dist/state/drag.js +95 -0
  39. package/dist/state/store.d.ts +45 -0
  40. package/dist/state/store.js +76 -0
  41. package/dist/state/useKeyboard.d.ts +12 -0
  42. package/dist/state/useKeyboard.js +55 -0
  43. package/dist/state/useWorkspace.d.ts +23 -0
  44. package/dist/state/useWorkspace.js +34 -0
  45. package/package.json +82 -4
@@ -0,0 +1,272 @@
1
+ import { DOCKS, Zone } from "./enums.js";
2
+ import { sameRef } from "./resource.js";
3
+ export { DOCKS, Zone };
4
+ /** The page is not a panel and has no id; it needs one so it can be focused and maximized. */
5
+ export const PAGE = "page";
6
+ export const DEFAULT_ZONES = {
7
+ [Zone.Left]: 0.28,
8
+ [Zone.Right]: 0.36,
9
+ [Zone.Bottom]: 0.34,
10
+ };
11
+ /** Outside these limits one of the two sides stops being usable. */
12
+ export const MIN_ZONE = 0.12;
13
+ export const MAX_ZONE = 0.72;
14
+ export const MIN_SHARE = 0.12;
15
+ const WINDOW_BOX = { x: 72, y: 56, w: 480, h: 360 };
16
+ const WINDOW_OFFSET = 28;
17
+ export const EMPTY = {
18
+ page: null,
19
+ trail: [],
20
+ panels: [],
21
+ zones: DEFAULT_ZONES,
22
+ peek: null,
23
+ focus: PAGE,
24
+ maximized: null,
25
+ selected: null,
26
+ };
27
+ const clampZone = (n) => Math.max(MIN_ZONE, Math.min(MAX_ZONE, n));
28
+ /** The id comes from the content and not from a global counter, so two identical workspaces
29
+ * serialize identically. */
30
+ function nextId(used) {
31
+ let n = 1;
32
+ while (used.includes(`p${n}`))
33
+ n += 1;
34
+ return `p${n}`;
35
+ }
36
+ export const findPanel = (w, id) => w.panels.find((p) => p.id === id) ?? null;
37
+ export const panelsIn = (w, zone) => w.panels.filter((p) => p.placement.zone === zone);
38
+ function withPanel(w, id, change) {
39
+ if (!w.panels.some((p) => p.id === id))
40
+ return w;
41
+ return { ...w, panels: normalizeAll(w.panels.map((p) => (p.id === id ? change(p) : p))) };
42
+ }
43
+ /** The shares of a zone are normalized to 1 on every change. Without that, opening a panel
44
+ * leaves the set summing 1.4 and the resize handle measures against a number that is no
45
+ * longer the one stored. */
46
+ function normalize(panels, zone) {
47
+ const inZone = panels.filter((p) => p.placement.zone === zone);
48
+ if (inZone.length === 0)
49
+ return panels;
50
+ const share = (p) => (p.placement.zone === Zone.Floating ? 0 : p.placement.share);
51
+ const total = inZone.reduce((sum, p) => sum + share(p), 0);
52
+ return panels.map((p) => {
53
+ if (p.placement.zone !== zone)
54
+ return p;
55
+ const next = total <= 0 ? 1 / inZone.length : p.placement.share / total;
56
+ return { ...p, placement: { ...p.placement, share: next } };
57
+ });
58
+ }
59
+ const normalizeAll = (panels) => DOCKS.reduce((acc, zone) => normalize(acc, zone), panels);
60
+ function topZ(panels) {
61
+ return panels.reduce((max, p) => (p.placement.zone === Zone.Floating ? Math.max(max, p.placement.z) : max), 0);
62
+ }
63
+ function placementFor(zone, panels) {
64
+ if (zone !== Zone.Floating)
65
+ return { zone, share: 1 };
66
+ const floating = panels.filter((p) => p.placement.zone === Zone.Floating).length;
67
+ return {
68
+ zone,
69
+ x: WINDOW_BOX.x + floating * WINDOW_OFFSET,
70
+ y: WINDOW_BOX.y + floating * WINDOW_OFFSET,
71
+ w: WINDOW_BOX.w,
72
+ h: WINDOW_BOX.h,
73
+ z: topZ(panels) + 1,
74
+ };
75
+ }
76
+ /* ── The page ───────────────────────────────────────────────────────── */
77
+ /**
78
+ * Opens `view` as the page and keeps where I was, so the breadcrumb can take me back. The first
79
+ * page has nothing behind it. When there is nothing to change it returns THE SAME object —
80
+ * every reducer here does, and that is what makes it possible to subscribe to a part of the
81
+ * workspace: if every call returned a new one, any action would redraw everything.
82
+ */
83
+ export function push(w, view) {
84
+ if (w.page === null)
85
+ return { ...w, page: view };
86
+ if (sameRef(w.page, view))
87
+ return w;
88
+ return { ...w, page: view, trail: [...w.trail, w.page], selected: dropSelection(w, PAGE) };
89
+ }
90
+ /** Back to the page at `index` of the trail; the pages after it are dropped. A crumb is this. */
91
+ export function goTo(w, index) {
92
+ const target = w.trail[index];
93
+ if (target === undefined)
94
+ return w;
95
+ return {
96
+ ...w,
97
+ page: target,
98
+ trail: w.trail.slice(0, index),
99
+ selected: dropSelection(w, PAGE),
100
+ };
101
+ }
102
+ /** One page back. Nothing to go back to is not an error: it changes nothing. */
103
+ export const back = (w) => w.trail.length === 0 ? w : goTo(w, w.trail.length - 1);
104
+ /** Opens a resource as a panel. If it is already open it moves it to the requested zone and
105
+ * focuses it, unless a duplicate is explicitly asked for. */
106
+ export function openPanel(w, view, options = {}) {
107
+ const opts = typeof options === "string" ? { zone: options } : options;
108
+ const zone = opts.zone ?? Zone.Right;
109
+ if (opts.allowDuplicate !== true) {
110
+ const existing = w.panels.find((p) => sameRef(p.view, view));
111
+ if (existing)
112
+ return focusPanel(movePanel(w, existing.id, zone), existing.id);
113
+ }
114
+ const id = nextId(w.panels.map((p) => p.id));
115
+ const panel = { id, view, placement: placementFor(zone, w.panels) };
116
+ return focusPanel({ ...w, panels: normalizeAll([...w.panels, panel]) }, id);
117
+ }
118
+ export function movePanel(w, id, zone) {
119
+ const panel = findPanel(w, id);
120
+ if (panel === null || panel.placement.zone === zone)
121
+ return w;
122
+ return withPanel(w, id, (p) => ({ ...p, placement: placementFor(zone, w.panels) }));
123
+ }
124
+ /** Closing the maximized or the focused one has to give the rest back, or the workspace ends
125
+ * up empty with panels inside it. */
126
+ export function closePanel(w, id) {
127
+ if (findPanel(w, id) === null)
128
+ return w;
129
+ return {
130
+ ...w,
131
+ panels: normalizeAll(w.panels.filter((p) => p.id !== id)),
132
+ maximized: w.maximized === id ? null : w.maximized,
133
+ focus: w.focus === id ? PAGE : w.focus,
134
+ selected: dropSelection(w, id),
135
+ };
136
+ }
137
+ /** Changes what a panel looks at without moving or closing it. */
138
+ export function navigatePanel(w, id, view) {
139
+ const panel = findPanel(w, id);
140
+ if (panel === null || sameRef(panel.view, view))
141
+ return w;
142
+ return { ...withPanel(w, id, (p) => ({ ...p, view })), selected: dropSelection(w, id) };
143
+ }
144
+ /** How much of the container a docked zone takes. */
145
+ export function resizeZone(w, zone, size) {
146
+ const clamped = clampZone(size);
147
+ if (w.zones[zone] === clamped)
148
+ return w;
149
+ return { ...w, zones: { ...w.zones, [zone]: clamped } };
150
+ }
151
+ /**
152
+ * How much of ITS zone a panel takes. What it gains it takes from the neighbour below, like
153
+ * any divider between two panels: moving a handle has to affect both sides and nobody else.
154
+ */
155
+ export function resizePanel(w, id, share) {
156
+ const panel = findPanel(w, id);
157
+ if (panel === null || panel.placement.zone === Zone.Floating)
158
+ return w;
159
+ const zone = panel.placement.zone;
160
+ const neighbors = panelsIn(w, zone);
161
+ const next = neighbors[neighbors.findIndex((p) => p.id === id) + 1];
162
+ if (next === undefined || next.placement.zone === Zone.Floating)
163
+ return w;
164
+ const together = panel.placement.share + next.placement.share;
165
+ const mine = Math.max(MIN_SHARE, Math.min(together - MIN_SHARE, share));
166
+ return {
167
+ ...w,
168
+ panels: w.panels.map((p) => p.id === id
169
+ ? { ...p, placement: { ...panel.placement, share: mine } }
170
+ : p.id === next.id
171
+ ? { ...p, placement: { ...next.placement, share: together - mine } }
172
+ : p),
173
+ };
174
+ }
175
+ /** Moves or resizes a window. */
176
+ export function floatPanel(w, id, box) {
177
+ return withPanel(w, id, (p) => p.placement.zone === Zone.Floating ? { ...p, placement: { ...p.placement, ...box } } : p);
178
+ }
179
+ /** Bring to front. Without this, two overlapping windows cannot be pulled apart. */
180
+ export function raisePanel(w, id) {
181
+ const panel = findPanel(w, id);
182
+ if (panel === null || panel.placement.zone !== Zone.Floating)
183
+ return w;
184
+ const mine = panel.placement.z;
185
+ const above = w.panels.some((p) => p.id !== id && p.placement.zone === Zone.Floating && p.placement.z >= mine);
186
+ if (!above)
187
+ return w;
188
+ return withPanel(w, id, (p) => p.placement.zone === Zone.Floating
189
+ ? { ...p, placement: { ...p.placement, z: topZ(w.panels) + 1 } }
190
+ : p);
191
+ }
192
+ /** Collapse to its header without closing it: still there, still mounted, taking no room. */
193
+ export function collapsePanel(w, id) {
194
+ return withPanel(w, id, (p) => ({
195
+ ...p,
196
+ placement: { ...p.placement, collapsed: p.placement.collapsed !== true },
197
+ }));
198
+ }
199
+ /** Take the whole workspace. Calling it on what is already maximized gives it back. */
200
+ export function maximize(w, target) {
201
+ if (target !== null && target !== PAGE && findPanel(w, target) === null)
202
+ return w;
203
+ return { ...w, maximized: w.maximized === target ? null : target };
204
+ }
205
+ /** The WORKSPACE focus, which is who the shortcuts talk to. Opening a panel focuses it. */
206
+ export function focusPanel(w, target) {
207
+ if (w.focus === target)
208
+ return w;
209
+ if (target !== PAGE && findPanel(w, target) === null)
210
+ return w;
211
+ return { ...w, focus: target };
212
+ }
213
+ /** The next one in reading order: the page, then each panel. */
214
+ export function cycleFocus(w, delta) {
215
+ const order = [PAGE, ...w.panels.map((p) => p.id)];
216
+ const current = Math.max(0, order.indexOf(w.focus));
217
+ const next = order[(current + delta + order.length) % order.length];
218
+ return next === undefined ? w : focusPanel(w, next);
219
+ }
220
+ /**
221
+ * A panel grows into the page: its resource becomes the page, with the previous page in the
222
+ * breadcrumb, and the panel is gone. «Enlarge» that keeps where you came from.
223
+ */
224
+ export function expandPanel(w, id) {
225
+ const panel = findPanel(w, id);
226
+ if (panel === null)
227
+ return w;
228
+ return push(closePanel(w, id), panel.view);
229
+ }
230
+ /* ── Peek ───────────────────────────────────────────────────────────── */
231
+ export function peek(w, view) {
232
+ if (w.peek !== null && sameRef(w.peek, view))
233
+ return w;
234
+ return { ...w, peek: view };
235
+ }
236
+ export const closePeek = (w) => w.peek === null ? w : { ...w, peek: null };
237
+ /** The peek becomes the page. */
238
+ export function peekToPage(w) {
239
+ return w.peek === null ? w : closePeek(push(w, w.peek));
240
+ }
241
+ /** It docks to the right because the drawer was already there and the piece does not move. */
242
+ export function peekToSide(w, zone = Zone.Right) {
243
+ return w.peek === null ? w : closePeek(openPanel(w, w.peek, { zone }));
244
+ }
245
+ /* ── Context ────────────────────────────────────────────────────────── */
246
+ const NO_REFS = [];
247
+ function dropSelection(w, by) {
248
+ return w.selected?.by === by ? null : w.selected;
249
+ }
250
+ /**
251
+ * What a surface has selected — the rows of a list. The workspace keeps only references, so a
252
+ * chat in another domain can read them without knowing what they are. Selecting nothing clears
253
+ * it only if that surface owned it.
254
+ */
255
+ export function select(w, by, refs) {
256
+ if (refs.length === 0) {
257
+ return w.selected?.by === by ? { ...w, selected: null } : w;
258
+ }
259
+ const current = w.selected;
260
+ if (current?.by === by &&
261
+ current.refs.length === refs.length &&
262
+ current.refs.every((r, i) => sameRef(r, refs[i])))
263
+ return w;
264
+ return { ...w, selected: { by, refs } };
265
+ }
266
+ /** What the user is working on: the page, or the panel that has the focus. */
267
+ export function focusedRef(w) {
268
+ if (w.focus === PAGE)
269
+ return w.page;
270
+ return findPanel(w, w.focus)?.view ?? w.page;
271
+ }
272
+ export const selectionOf = (w) => w.selected?.refs ?? NO_REFS;
@@ -0,0 +1,20 @@
1
+ /**
2
+ * The shell — React and the design system. The model comes in through the other door,
3
+ * `@sincpro/web-composer/domain`, which depends on nothing.
4
+ *
5
+ * Only the model TYPES that appear in these signatures are re-exported, so declaring a domain
6
+ * does not force a two-place import.
7
+ */
8
+ export type { Action, Crumb, OpenOptions, PaletteEntry, PickOptions, Registry, ResourceRef, SearchHit, ViewProps, ViewSpec, Workspace, WorkspaceApi, } from "./domain/index.js";
9
+ export { Destination, focusedRef, PAGE, PaletteSection, selectionOf, Surface, Zone, } from "./domain/index.js";
10
+ export { Composer, type ComposerProps } from "./shell/Composer.js";
11
+ export { type ComposerCopy, composerCopy, ENGLISH_COMPOSER_COPY, installComposerCopy, SPANISH_COMPOSER_COPY, } from "./shell/copy.js";
12
+ export { WorkspacePalette, type WorkspacePaletteProps, } from "./shell/Palette.js";
13
+ export { type PanelActions, PanelFrame, type PanelFrameProps, } from "./shell/PanelFrame.js";
14
+ export { PanelHost, type PanelHostProps } from "./shell/PanelHost.js";
15
+ export { RecordLink, type RecordLinkProps } from "./shell/RecordLink.js";
16
+ export { ViewHost, type ViewHostProps } from "./shell/ViewHost.js";
17
+ export { type ApiFactory, createApiFactory, type Target, } from "./state/api.js";
18
+ export { createWorkspaceStore, type StoreOptions, type WorkspaceStore, } from "./state/store.js";
19
+ export { useKeyboard, type UseKeyboardOptions, } from "./state/useKeyboard.js";
20
+ export { useWorkspace, useWorkspaceContext, useWorkspaceSlice, useWorkspaceState, } from "./state/useWorkspace.js";
package/dist/index.js ADDED
@@ -0,0 +1,19 @@
1
+ /**
2
+ * The shell — React and the design system. The model comes in through the other door,
3
+ * `@sincpro/web-composer/domain`, which depends on nothing.
4
+ *
5
+ * Only the model TYPES that appear in these signatures are re-exported, so declaring a domain
6
+ * does not force a two-place import.
7
+ */
8
+ export { Destination, focusedRef, PAGE, PaletteSection, selectionOf, Surface, Zone, } from "./domain/index.js";
9
+ export { Composer } from "./shell/Composer.js";
10
+ export { composerCopy, ENGLISH_COMPOSER_COPY, installComposerCopy, SPANISH_COMPOSER_COPY, } from "./shell/copy.js";
11
+ export { WorkspacePalette, } from "./shell/Palette.js";
12
+ export { PanelFrame, } from "./shell/PanelFrame.js";
13
+ export { PanelHost } from "./shell/PanelHost.js";
14
+ export { RecordLink } from "./shell/RecordLink.js";
15
+ export { ViewHost } from "./shell/ViewHost.js";
16
+ export { createApiFactory, } from "./state/api.js";
17
+ export { createWorkspaceStore, } from "./state/store.js";
18
+ export { useKeyboard, } from "./state/useKeyboard.js";
19
+ export { useWorkspace, useWorkspaceContext, useWorkspaceSlice, useWorkspaceState, } from "./state/useWorkspace.js";
@@ -0,0 +1,24 @@
1
+ import { Destination, type Registry } from "../domain/index.js";
2
+ import type { WorkspaceStore } from "../state/store.js";
3
+ import { type ReactNode } from "react";
4
+ /**
5
+ * The workspace — the page, the panels around it, the peek and ⌘K — wired against the
6
+ * registry. Adding a domain does not touch any file in this package: register its `ViewSpec`
7
+ * and it appears.
8
+ */
9
+ export type ComposerProps = {
10
+ workspace: WorkspaceStore;
11
+ registry: Registry;
12
+ /** Drawn when there is no page yet. */
13
+ empty?: ReactNode;
14
+ /** Keyboard shortcuts, ⌘K included. Turn off if the app already defines its own. */
15
+ keyboard?: boolean;
16
+ /** Where `api.open(ref)` goes when the caller does not say. The page — what people expect of
17
+ * a click on a record. */
18
+ defaultOpen?: Destination;
19
+ /** ⌘K, controlled — for a search button in the app's own bar. Without it the composer keeps
20
+ * the state and only the shortcut opens it. */
21
+ palette?: boolean;
22
+ onPaletteChange?: (open: boolean) => void;
23
+ };
24
+ export declare function Composer({ workspace, registry, empty, keyboard, defaultOpen, palette: paletteProp, onPaletteChange, }: ComposerProps): import("react").JSX.Element;
@@ -0,0 +1,59 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { Destination, focusedRef, formatRef, Surface, Zone, } from "../domain/index.js";
3
+ import { composerCopy } from "./copy.js";
4
+ import { WorkspacePalette } from "./Palette.js";
5
+ import { PanelHost } from "./PanelHost.js";
6
+ import { ViewHost } from "./ViewHost.js";
7
+ import { createApiFactory } from "../state/api.js";
8
+ import { useDropMonitor } from "../state/drag.js";
9
+ import { useKeyboard } from "../state/useKeyboard.js";
10
+ import { useWorkspaceState } from "../state/useWorkspace.js";
11
+ import { Button, Peek } from "@sincpro/web-ui";
12
+ import { useCallback, useEffect, useMemo, useState } from "react";
13
+ export function Composer({ workspace, registry, empty, keyboard = true, defaultOpen = Destination.Page, palette: paletteProp, onPaletteChange, }) {
14
+ const state = useWorkspaceState(workspace);
15
+ const copy = composerCopy();
16
+ const [paletteState, setPaletteState] = useState(false);
17
+ const palette = paletteProp ?? paletteState;
18
+ const setPalette = useCallback((open) => {
19
+ setPaletteState(open);
20
+ onPaletteChange?.(open);
21
+ }, [onPaletteChange]);
22
+ const api = useMemo(() => createApiFactory(workspace, { registry, defaultOpen }), [workspace, registry, defaultOpen]);
23
+ useEffect(() => api.dispose, [api]);
24
+ useKeyboard({
25
+ workspace,
26
+ enabled: keyboard,
27
+ onPalette: useCallback(() => setPalette(true), [setPalette]),
28
+ });
29
+ useDropMonitor(useMemo(() => ({
30
+ onPanelToZone: workspace.movePanel,
31
+ onRefToZone: (ref, zone) => workspace.openPanel(ref, zone),
32
+ }), [workspace]));
33
+ /** Where a chosen search result opens. Without a page yet, the first one becomes it. */
34
+ function open(ref, destination) {
35
+ const here = workspace.get();
36
+ if (destination === Destination.Peek)
37
+ workspace.peek(ref);
38
+ else if (destination === Destination.Window)
39
+ workspace.openPanel(ref, Zone.Floating);
40
+ else if (destination === Destination.Side && here.page !== null)
41
+ workspace.openPanel(ref, api.zoneFor(registry.resolve(ref)));
42
+ else
43
+ workspace.push(ref);
44
+ }
45
+ /** An action runs with the API of the surface its resource is on, so «audit» can open beside
46
+ * it or navigate it. */
47
+ function run(action) {
48
+ const w = workspace.get();
49
+ const ref = focusedRef(w);
50
+ if (ref === null)
51
+ return;
52
+ action.run(api.for(ref, w.focus === "page"
53
+ ? { surface: Surface.Page }
54
+ : { surface: Surface.Panel, panelId: w.focus }));
55
+ }
56
+ const peeked = state.peek;
57
+ const peekSpec = peeked === null ? null : registry.resolve(peeked);
58
+ return (_jsxs("div", { className: "relative flex min-h-0 flex-1 flex-col bg-ground", children: [state.page === null ? (_jsx("div", { className: "flex min-h-0 flex-1 items-center justify-center p-8", children: empty })) : (_jsx(PanelHost, { workspace: state, registry: registry, store: workspace, api: api, overlay: peeked === null ? null : (_jsx(Peek, { open: true, title: peekSpec?.title(peeked) ?? formatRef(peeked), onClose: workspace.closePeek, onPromote: () => workspace.peekToSide(peekSpec?.preferredZone ?? Zone.Right), actions: _jsx(Button, { size: "sm", role: "ghost", onClick: workspace.peekToPage, children: copy.peek.openAsPage }), children: _jsx(ViewHost, { resource: peeked, registry: registry, api: api.for(peeked, { surface: Surface.Peek }) }) })) })), palette ? (_jsx(WorkspacePalette, { registry: registry, focused: focusedRef(state), onOpen: open, onAction: run, onClose: () => setPalette(false) })) : null] }));
59
+ }
@@ -0,0 +1,9 @@
1
+ import { type Action, Destination, type Registry, type ResourceRef } from "../domain/index.js";
2
+ export type WorkspacePaletteProps = {
3
+ registry: Registry;
4
+ focused: ResourceRef | null;
5
+ onOpen: (ref: ResourceRef, destination: Destination) => void;
6
+ onAction: (action: Action) => void;
7
+ onClose: () => void;
8
+ };
9
+ export declare function WorkspacePalette({ registry, focused, onOpen, onAction, onClose, }: WorkspacePaletteProps): import("react").JSX.Element;
@@ -0,0 +1,123 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { Destination, localEntries, matches, MIN_QUERY, PaletteSection, searchEntries, } from "../domain/index.js";
3
+ import { composerCopy } from "./copy.js";
4
+ import { Search } from "lucide-react";
5
+ import { useEffect, useId, useMemo, useRef, useState, } from "react";
6
+ /**
7
+ * ⌘K over the workspace. The list is the model's (`localEntries`,
8
+ * `searchEntries`); this only draws it and decides WHERE a resource opens,
9
+ * which is the one thing a generic palette cannot know:
10
+ *
11
+ * Enter the page — what a click on a record does
12
+ * ⌥Enter a peek — look without losing the place
13
+ * ⇧Enter a panel beside what I am on
14
+ * ⌘Enter a window
15
+ *
16
+ * Built by hand because synthesis-ui has no palette. When this moves onto
17
+ * `@sincpro/web-ui`, the drawing is its `CommandPalette` and only the
18
+ * destinations stay here.
19
+ */
20
+ const SECTION_KEY = {
21
+ [PaletteSection.Actions]: "actions",
22
+ [PaletteSection.Goto]: "goto",
23
+ [PaletteSection.Found]: "found",
24
+ };
25
+ /** Long enough to not ask every domain per keystroke, short enough to feel live. */
26
+ const SEARCH_DELAY_MS = 180;
27
+ function destinationOf(e) {
28
+ if (e.metaKey || e.ctrlKey)
29
+ return Destination.Window;
30
+ if (e.shiftKey)
31
+ return Destination.Side;
32
+ if (e.altKey)
33
+ return Destination.Peek;
34
+ return Destination.Page;
35
+ }
36
+ /** The remote search, debounced, with the stale answers dropped: typing «fac»
37
+ * then «fact» must not let the slower «fac» land last. */
38
+ function useFound(registry, query) {
39
+ const [found, setFound] = useState([]);
40
+ const [loading, setLoading] = useState(false);
41
+ useEffect(() => {
42
+ if (query.trim().length < MIN_QUERY) {
43
+ setFound([]);
44
+ setLoading(false);
45
+ return;
46
+ }
47
+ let current = true;
48
+ setLoading(true);
49
+ const timer = setTimeout(() => {
50
+ void searchEntries(registry, query).then((entries) => {
51
+ if (!current)
52
+ return;
53
+ setFound(entries);
54
+ setLoading(false);
55
+ });
56
+ }, SEARCH_DELAY_MS);
57
+ return () => {
58
+ current = false;
59
+ clearTimeout(timer);
60
+ };
61
+ }, [registry, query]);
62
+ return { found, loading };
63
+ }
64
+ export function WorkspacePalette({ registry, focused, onOpen, onAction, onClose, }) {
65
+ const copy = composerCopy().palette;
66
+ const listId = useId();
67
+ const [query, setQuery] = useState("");
68
+ const [cursor, setCursor] = useState(0);
69
+ const { found, loading } = useFound(registry, query);
70
+ const local = useMemo(() => localEntries(registry, focused), [registry, focused]);
71
+ const entries = useMemo(() => [...local.filter((e) => matches(e, query)), ...found], [local, found, query]);
72
+ const active = entries[Math.min(cursor, entries.length - 1)];
73
+ /* The focus goes back where it was: the palette is a path, not a place. */
74
+ const returnTo = useRef(null);
75
+ useEffect(() => {
76
+ returnTo.current = document.activeElement;
77
+ return () => {
78
+ if (returnTo.current instanceof HTMLElement)
79
+ returnTo.current.focus();
80
+ };
81
+ }, []);
82
+ function run(entry, destination) {
83
+ onClose();
84
+ if (entry.section === PaletteSection.Actions)
85
+ onAction(entry.action);
86
+ else
87
+ onOpen(entry.ref, destination);
88
+ }
89
+ function onKeyDown(e) {
90
+ const step = e.key === "ArrowDown" ? 1 : e.key === "ArrowUp" ? -1 : 0;
91
+ if (step !== 0 && entries.length > 0) {
92
+ setCursor((c) => (Math.min(c, entries.length - 1) + step + entries.length) % entries.length);
93
+ e.preventDefault();
94
+ }
95
+ else if (e.key === "Enter" && active !== undefined) {
96
+ run(active, destinationOf(e));
97
+ e.preventDefault();
98
+ }
99
+ else if (e.key === "Escape") {
100
+ onClose();
101
+ e.preventDefault();
102
+ }
103
+ else if (e.key === "Tab") {
104
+ /* The search box is the palette's only stop: Tab would walk out of the
105
+ dialog into the page behind it. */
106
+ e.preventDefault();
107
+ }
108
+ }
109
+ return (_jsx("div", { className: "fixed inset-0 z-50 flex items-start justify-center bg-ink/30 px-4 pt-24", onPointerDown: (e) => {
110
+ if (e.target === e.currentTarget)
111
+ onClose();
112
+ }, children: _jsxs("div", { role: "dialog", "aria-label": copy.label, className: "w-full max-w-xl overflow-hidden rounded-box border border-line-strong bg-surface shadow-window", children: [_jsxs("div", { className: "flex items-center gap-2 border-b border-line px-3", children: [_jsx(Search, { className: "size-(--size-icon-md) shrink-0 text-muted", strokeWidth: 2, "aria-hidden": true }), _jsx("input", { autoFocus: true, role: "combobox", "aria-expanded": true, "aria-controls": listId, "aria-autocomplete": "list", ...(active === undefined
113
+ ? {}
114
+ : { "aria-activedescendant": `${listId}-${active.id}` }), value: query, placeholder: copy.placeholder, onChange: (e) => {
115
+ setQuery(e.target.value);
116
+ setCursor(0);
117
+ }, onKeyDown: onKeyDown, className: "h-(--size-control-lg) min-w-0 flex-1 bg-transparent text-body text-ink outline-0 placeholder:text-muted" })] }), _jsxs("ul", { id: listId, role: "listbox", className: "max-h-96 overflow-y-auto p-1", children: [entries.map((entry, i) => {
118
+ const Icon = entry.icon;
119
+ const heading = entries[i - 1]?.section !== entry.section;
120
+ return (_jsxs("li", { role: "presentation", children: [heading ? (_jsx("p", { className: "px-2 pt-2 pb-1 text-micro font-medium text-muted", children: copy.sections[SECTION_KEY[entry.section]] })) : null, _jsxs("div", { id: `${listId}-${entry.id}`, role: "option", "aria-selected": entry === active, onPointerMove: () => setCursor(i), onClick: (e) => run(entry, destinationOf(e)), className: `flex cursor-default items-center gap-2 rounded-control px-2 py-1.5 text-meta text-ink ${entry === active ? "bg-primary/8" : ""}`, children: [_jsx("span", { className: "flex size-(--size-icon-md) shrink-0 items-center justify-center text-muted", children: Icon === undefined ? null : (_jsx(Icon, { className: "size-(--size-icon-md)", strokeWidth: 2 })) }), _jsx("span", { className: "min-w-0 flex-1 truncate", children: entry.label }), entry.section !== PaletteSection.Actions && entry.detail !== undefined ? (_jsx("span", { className: "shrink-0 truncate text-micro text-muted", children: entry.detail })) : null, entry.section === PaletteSection.Actions &&
121
+ entry.shortcut !== undefined ? (_jsx("kbd", { className: "shrink-0 rounded-control border border-line px-1.5 text-micro text-muted", children: entry.shortcut })) : null] })] }, entry.id));
122
+ }), entries.length === 0 && !loading ? (_jsx("li", { className: "px-3 py-6 text-center text-meta text-muted italic", children: copy.empty })) : null, loading ? (_jsx("li", { className: "px-3 py-2 text-micro text-muted", children: copy.searching })) : null] }), _jsx("p", { className: "border-t border-line px-3 py-1.5 text-micro text-muted", children: copy.hint })] }) }));
123
+ }
@@ -0,0 +1,31 @@
1
+ import { Zone } from "../domain/index.js";
2
+ import { type PointerEvent as ReactPointerEvent, type ReactNode } from "react";
3
+ /**
4
+ * The frame of a side panel or a window. Four icons, always the same, in OS order: more,
5
+ * minimize, enlarge, close. The page has no frame: a list or a record draws its own header.
6
+ */
7
+ export type PanelActions = {
8
+ onMove: (zone: Zone) => void;
9
+ onCollapse: () => void;
10
+ /** Grow into the page, keeping where it came from. */
11
+ onExpand: () => void;
12
+ onClose: () => void;
13
+ };
14
+ export type PanelFrameProps = {
15
+ title: string;
16
+ actions: PanelActions;
17
+ zone: Zone;
18
+ maximized: boolean;
19
+ collapsed: boolean;
20
+ focused: boolean;
21
+ /** The id a docked panel is dragged by. A window has none: it moves with the pointer. */
22
+ dragId?: string;
23
+ onMaximize: () => void;
24
+ onFocus: () => void;
25
+ /** Pointer down on the title of a window: the host moves it. */
26
+ onMoveStart?: (e: ReactPointerEvent) => void;
27
+ children: ReactNode;
28
+ };
29
+ declare function Frame({ title, actions, zone, maximized, collapsed, focused, dragId, onMaximize, onFocus, onMoveStart, children, }: PanelFrameProps): import("react").JSX.Element;
30
+ export declare const PanelFrame: import("react").MemoExoticComponent<typeof Frame>;
31
+ export {};
@@ -0,0 +1,44 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { Zone } from "../domain/index.js";
3
+ import { composerCopy } from "./copy.js";
4
+ import { useDraggablePanel } from "../state/drag.js";
5
+ import { Menu } from "@sincpro/web-ui";
6
+ import { ArrowUpRight, Copy, Ellipsis, Minus, PanelBottom, PanelLeft, PanelRight, PictureInPicture2, Square, X, } from "lucide-react";
7
+ import { memo, useRef } from "react";
8
+ const DESTINATIONS = (c) => [
9
+ { zone: Zone.Left, icon: PanelLeft, label: c.left, hint: c.leftHint },
10
+ { zone: Zone.Right, icon: PanelRight, label: c.right, hint: c.rightHint },
11
+ { zone: Zone.Bottom, icon: PanelBottom, label: c.bottom, hint: c.bottomHint },
12
+ { zone: Zone.Floating, icon: PictureInPicture2, label: c.window, hint: c.windowHint },
13
+ ];
14
+ function Control({ icon: Icon, label, active = false, danger = false, onClick, }) {
15
+ const paint = active
16
+ ? "bg-primary/12 text-primary"
17
+ : danger
18
+ ? "text-muted hover:bg-bad/10 hover:text-bad"
19
+ : "text-muted hover:bg-ink/6 hover:text-ink";
20
+ return (_jsx("button", { type: "button", title: label, "aria-label": label, "aria-pressed": active, onClick: onClick, onPointerDown: (e) => e.stopPropagation(), className: `inline-flex size-(--size-control-sm) shrink-0 items-center justify-center rounded-control transition-colors duration-(--duration-instant) focus-visible:outline-1 focus-visible:outline-offset-1 focus-visible:outline-primary ${paint}`, children: _jsx(Icon, { className: "size-(--size-icon-sm)", strokeWidth: 2 }) }));
21
+ }
22
+ const MenuAction = { Expand: "expand" };
23
+ function Frame({ title, actions, zone, maximized, collapsed, focused, dragId, onMaximize, onFocus, onMoveStart, children, }) {
24
+ const copy = composerCopy().panel;
25
+ const header = useRef(null);
26
+ const floating = zone === Zone.Floating;
27
+ const dragging = useDraggablePanel(header, dragId ?? "", dragId !== undefined && !floating);
28
+ return (_jsxs("section", { role: "region", "aria-label": title, onPointerDownCapture: onFocus, className: `flex h-full min-h-0 min-w-0 flex-col overflow-hidden rounded-box border bg-surface transition-[border-color,box-shadow,opacity] duration-(--duration-instant) ${focused ? "border-primary/55 ring-1 ring-primary/25" : "border-line-strong"} ${floating ? "shadow-window" : ""} ${dragging ? "opacity-40" : ""}`, children: [_jsxs("header", { ref: header, onPointerDown: floating ? onMoveStart : undefined, style: floating ? { touchAction: "none" } : undefined, className: `flex shrink-0 items-center gap-0.5 border-b border-line bg-ground py-1 pr-1 pl-2.5 select-none ${floating ? "cursor-move" : "cursor-grab active:cursor-grabbing"}`, children: [_jsx("span", { className: "min-w-0 flex-1 truncate pr-1 text-micro font-medium text-ink", children: title }), _jsx("div", { onPointerDown: (e) => e.stopPropagation(), children: _jsx(Menu, { label: copy.more, icon: Ellipsis, iconOnly: true, chevron: false, align: "end", variant: "text", groups: [
29
+ {
30
+ label: copy.moveTo,
31
+ options: DESTINATIONS(copy).map((d) => ({
32
+ id: d.zone,
33
+ label: d.label,
34
+ hint: d.hint,
35
+ icon: d.icon,
36
+ checked: zone === d.zone,
37
+ })),
38
+ },
39
+ {
40
+ options: [{ id: MenuAction.Expand, label: copy.expand, icon: ArrowUpRight }],
41
+ },
42
+ ], onSelect: (id) => id === MenuAction.Expand ? actions.onExpand() : actions.onMove(id) }) }), _jsx(Control, { icon: Minus, label: collapsed ? copy.restore : copy.minimize, active: collapsed, onClick: actions.onCollapse }), _jsx(Control, { icon: maximized ? Copy : Square, label: maximized ? copy.restore : copy.enlarge, active: maximized, onClick: onMaximize }), _jsx(Control, { icon: X, label: copy.close, danger: true, onClick: actions.onClose })] }), _jsx("div", { hidden: collapsed, className: "@container min-h-0 flex-1 overflow-auto", children: children })] }));
43
+ }
44
+ export const PanelFrame = memo(Frame);
@@ -0,0 +1,23 @@
1
+ import { type Registry, type Workspace } from "../domain/index.js";
2
+ import type { ApiFactory } from "../state/api.js";
3
+ import type { WorkspaceStore } from "../state/store.js";
4
+ import { type ReactNode } from "react";
5
+ /**
6
+ * Every surface is a sibling on one layer, ordered by id, positioned with CSS. Changing zone
7
+ * only changes its box: React keeps the node, and with it its scroll, filter, and typed input.
8
+ * Nesting containers per zone would unmount it on every move.
9
+ *
10
+ * Each surface is its own stacking context (`isolate`): a list's sticky bar carries a z-index,
11
+ * and without the boundary it painted OVER the windows — covering their title, so they could
12
+ * not be grabbed.
13
+ */
14
+ export type PanelHostProps = {
15
+ workspace: Workspace;
16
+ /** Drawn over the PAGE only — the peek. Anchored to the page's box, it reads beside the list
17
+ * it came from and never covers the panels docked around it. */
18
+ overlay?: ReactNode;
19
+ registry: Registry;
20
+ store: WorkspaceStore;
21
+ api: ApiFactory;
22
+ };
23
+ export declare function PanelHost({ workspace, overlay, registry, store, api }: PanelHostProps): import("react").JSX.Element;