@sincpro/web-composer 0.0.0-stage → 1.0.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 (53) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +269 -2
  3. package/dist/domain/enums.d.ts +55 -0
  4. package/dist/domain/enums.js +48 -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 +92 -0
  8. package/dist/domain/layout.js +216 -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 +135 -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 +16 -0
  16. package/dist/domain/url.js +145 -0
  17. package/dist/domain/workspace.d.ts +174 -0
  18. package/dist/domain/workspace.js +393 -0
  19. package/dist/index.d.ts +24 -0
  20. package/dist/index.js +23 -0
  21. package/dist/shell/Composer.d.ts +23 -0
  22. package/dist/shell/Composer.js +88 -0
  23. package/dist/shell/Palette.d.ts +9 -0
  24. package/dist/shell/Palette.js +124 -0
  25. package/dist/shell/PanelFrame.d.ts +38 -0
  26. package/dist/shell/PanelFrame.js +70 -0
  27. package/dist/shell/PanelHost.d.ts +23 -0
  28. package/dist/shell/PanelHost.js +201 -0
  29. package/dist/shell/Peek.d.ts +26 -0
  30. package/dist/shell/Peek.js +64 -0
  31. package/dist/shell/RecordLink.d.ts +12 -0
  32. package/dist/shell/RecordLink.js +55 -0
  33. package/dist/shell/ViewHost.d.ts +11 -0
  34. package/dist/shell/ViewHost.js +53 -0
  35. package/dist/shell/copy.d.ts +73 -0
  36. package/dist/shell/copy.js +130 -0
  37. package/dist/shell/icons.d.ts +22 -0
  38. package/dist/shell/icons.js +19 -0
  39. package/dist/shell/skin.d.ts +72 -0
  40. package/dist/shell/skin.js +35 -0
  41. package/dist/shell/styles.d.ts +19 -0
  42. package/dist/shell/styles.js +212 -0
  43. package/dist/state/api.d.ts +36 -0
  44. package/dist/state/api.js +170 -0
  45. package/dist/state/drag.d.ts +31 -0
  46. package/dist/state/drag.js +95 -0
  47. package/dist/state/store.d.ts +69 -0
  48. package/dist/state/store.js +200 -0
  49. package/dist/state/useKeyboard.d.ts +16 -0
  50. package/dist/state/useKeyboard.js +63 -0
  51. package/dist/state/useWorkspace.d.ts +23 -0
  52. package/dist/state/useWorkspace.js +34 -0
  53. package/package.json +83 -4
@@ -0,0 +1,393 @@
1
+ import { DOCKS, Zone } from "./enums.js";
2
+ import { formatRef, rootRef, 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
+ /** Nor is the peek; it needs one to take the focus and publish a selection. */
7
+ export const PEEK = "peek";
8
+ export const DEFAULT_ZONES = {
9
+ [Zone.Left]: 0.28,
10
+ [Zone.Right]: 0.36,
11
+ [Zone.Bottom]: 0.34,
12
+ };
13
+ /** Outside these limits one of the two sides stops being usable. */
14
+ export const MIN_ZONE = 0.12;
15
+ export const MAX_ZONE = 0.72;
16
+ export const MIN_SHARE = 0.12;
17
+ const WINDOW_BOX = { x: 72, y: 56, w: 480, h: 360 };
18
+ const WINDOW_OFFSET = 28;
19
+ export const EMPTY = {
20
+ page: null,
21
+ trail: [],
22
+ panels: [],
23
+ zones: DEFAULT_ZONES,
24
+ peek: null,
25
+ peekTrail: [],
26
+ focus: PAGE,
27
+ maximized: null,
28
+ selected: null,
29
+ titles: {},
30
+ };
31
+ const clampZone = (n) => Math.max(MIN_ZONE, Math.min(MAX_ZONE, n));
32
+ /** The id comes from the content and not from a global counter, so two identical workspaces
33
+ * serialize identically. */
34
+ function nextId(used) {
35
+ let n = 1;
36
+ while (used.includes(`p${n}`))
37
+ n += 1;
38
+ return `p${n}`;
39
+ }
40
+ export const findPanel = (w, id) => w.panels.find((p) => p.id === id) ?? null;
41
+ /** What a surface shows now — the page, the peek or a panel — or `null` if it is not there. */
42
+ export function refOn(w, surface) {
43
+ if (surface === PAGE)
44
+ return w.page;
45
+ if (surface === PEEK)
46
+ return w.peek;
47
+ return findPanel(w, surface)?.view ?? null;
48
+ }
49
+ export const panelsIn = (w, zone) => w.panels.filter((p) => p.placement.zone === zone);
50
+ function withPanel(w, id, change) {
51
+ if (!w.panels.some((p) => p.id === id))
52
+ return w;
53
+ return { ...w, panels: normalizeAll(w.panels.map((p) => (p.id === id ? change(p) : p))) };
54
+ }
55
+ /** The shares of a zone are normalized to 1 on every change. Without that, opening a panel
56
+ * leaves the set summing 1.4 and the resize handle measures against a number that is no
57
+ * longer the one stored. */
58
+ function normalize(panels, zone) {
59
+ const inZone = panels.filter((p) => p.placement.zone === zone);
60
+ if (inZone.length === 0)
61
+ return panels;
62
+ const share = (p) => (p.placement.zone === Zone.Floating ? 0 : p.placement.share);
63
+ const total = inZone.reduce((sum, p) => sum + share(p), 0);
64
+ return panels.map((p) => {
65
+ if (p.placement.zone !== zone)
66
+ return p;
67
+ const next = total <= 0 ? 1 / inZone.length : p.placement.share / total;
68
+ return { ...p, placement: { ...p.placement, share: next } };
69
+ });
70
+ }
71
+ const normalizeAll = (panels) => DOCKS.reduce((acc, zone) => normalize(acc, zone), panels);
72
+ function topZ(panels) {
73
+ return panels.reduce((max, p) => (p.placement.zone === Zone.Floating ? Math.max(max, p.placement.z) : max), 0);
74
+ }
75
+ function placementFor(zone, panels) {
76
+ if (zone !== Zone.Floating)
77
+ return { zone, share: 1 };
78
+ const floating = panels.filter((p) => p.placement.zone === Zone.Floating).length;
79
+ return {
80
+ zone,
81
+ x: WINDOW_BOX.x + floating * WINDOW_OFFSET,
82
+ y: WINDOW_BOX.y + floating * WINDOW_OFFSET,
83
+ w: WINDOW_BOX.w,
84
+ h: WINDOW_BOX.h,
85
+ z: topZ(panels) + 1,
86
+ };
87
+ }
88
+ /* ── The page ───────────────────────────────────────────────────────── */
89
+ /**
90
+ * The peek is a drawer over THIS page. When the page becomes another resource the drawer would
91
+ * float over something that is no longer there, so it closes; another face of the same one —
92
+ * only `params` change: a filter, a tab — keeps it.
93
+ */
94
+ function leavePage(w, next) {
95
+ const stays = w.page !== null && next.page !== null && sameRef(rootRef(w.page), rootRef(next.page));
96
+ return stays ? next : closePeek(next);
97
+ }
98
+ /**
99
+ * Opens `view` as the page and keeps where I was, so the breadcrumb can take me back. The first
100
+ * page has nothing behind it. When there is nothing to change it returns THE SAME object —
101
+ * every reducer here does, and that is what makes it possible to subscribe to a part of the
102
+ * workspace: if every call returned a new one, any action would redraw everything.
103
+ */
104
+ export function push(w, view) {
105
+ if (w.page === null)
106
+ return { ...w, page: view };
107
+ if (sameRef(w.page, view))
108
+ return w;
109
+ return leavePage(w, {
110
+ ...w,
111
+ page: view,
112
+ trail: [...w.trail, w.page],
113
+ selected: dropSelection(w, PAGE),
114
+ });
115
+ }
116
+ /** Puts `view` where the page is WITHOUT a crumb: a filter that changed, or the record a form
117
+ * just created — the form should not stay in the way back. */
118
+ export function replace(w, view) {
119
+ if (w.page === null)
120
+ return { ...w, page: view };
121
+ if (sameRef(w.page, view))
122
+ return w;
123
+ return leavePage(w, { ...w, page: view, selected: dropSelection(w, PAGE) });
124
+ }
125
+ /** Starting over, from a menu: `view` is the page and the way back starts with it. The panels
126
+ * stay — they are how the person arranged the desk, not where they were. */
127
+ export function start(w, view) {
128
+ if (w.page !== null && sameRef(w.page, view) && w.trail.length === 0 && w.peek === null)
129
+ return w;
130
+ return closePeek({ ...w, page: view, trail: [], selected: dropSelection(w, PAGE) });
131
+ }
132
+ /** Back to the page at `index` of the trail; the pages after it are dropped. A crumb is this. */
133
+ export function goTo(w, index) {
134
+ const target = w.trail[index];
135
+ if (target === undefined)
136
+ return w;
137
+ return leavePage(w, {
138
+ ...w,
139
+ page: target,
140
+ trail: w.trail.slice(0, index),
141
+ selected: dropSelection(w, PAGE),
142
+ });
143
+ }
144
+ /** One page back. Nothing to go back to is not an error: it changes nothing. */
145
+ export const back = (w) => w.trail.length === 0 ? w : goTo(w, w.trail.length - 1);
146
+ /** Opens a resource as a panel. If it is already open it moves it to the requested zone and
147
+ * focuses it, unless a duplicate is explicitly asked for. */
148
+ export function openPanel(w, view, options = {}) {
149
+ const opts = typeof options === "string" ? { zone: options } : options;
150
+ const zone = opts.zone ?? Zone.Right;
151
+ if (opts.allowDuplicate !== true) {
152
+ const existing = w.panels.find((p) => sameRef(p.view, view));
153
+ if (existing)
154
+ return focusPanel(movePanel(w, existing.id, zone), existing.id);
155
+ }
156
+ const id = nextId(w.panels.map((p) => p.id));
157
+ const panel = { id, view, placement: placementFor(zone, w.panels) };
158
+ return focusPanel({ ...w, panels: normalizeAll([...w.panels, panel]) }, id);
159
+ }
160
+ export function movePanel(w, id, zone) {
161
+ const panel = findPanel(w, id);
162
+ if (panel === null || panel.placement.zone === zone)
163
+ return w;
164
+ return withPanel(w, id, (p) => ({ ...p, placement: placementFor(zone, w.panels) }));
165
+ }
166
+ /** Closing the maximized or the focused one has to give the rest back, or the workspace ends
167
+ * up empty with panels inside it. */
168
+ export function closePanel(w, id) {
169
+ if (findPanel(w, id) === null)
170
+ return w;
171
+ return {
172
+ ...w,
173
+ panels: normalizeAll(w.panels.filter((p) => p.id !== id)),
174
+ maximized: w.maximized === id ? null : w.maximized,
175
+ focus: w.focus === id ? PAGE : w.focus,
176
+ selected: dropSelection(w, id),
177
+ };
178
+ }
179
+ /** Changes what a panel looks at without moving or closing it. */
180
+ export function navigatePanel(w, id, view) {
181
+ const panel = findPanel(w, id);
182
+ if (panel === null || sameRef(panel.view, view))
183
+ return w;
184
+ return { ...withPanel(w, id, (p) => ({ ...p, view })), selected: dropSelection(w, id) };
185
+ }
186
+ /** How much of the container a docked zone takes. */
187
+ export function resizeZone(w, zone, size) {
188
+ const clamped = clampZone(size);
189
+ if (w.zones[zone] === clamped)
190
+ return w;
191
+ return { ...w, zones: { ...w.zones, [zone]: clamped } };
192
+ }
193
+ /**
194
+ * How much of ITS zone a panel takes. What it gains it takes from the neighbour below, like
195
+ * any divider between two panels: moving a handle has to affect both sides and nobody else.
196
+ */
197
+ export function resizePanel(w, id, share) {
198
+ const panel = findPanel(w, id);
199
+ if (panel === null || panel.placement.zone === Zone.Floating)
200
+ return w;
201
+ const zone = panel.placement.zone;
202
+ const neighbors = panelsIn(w, zone);
203
+ const next = neighbors[neighbors.findIndex((p) => p.id === id) + 1];
204
+ if (next === undefined || next.placement.zone === Zone.Floating)
205
+ return w;
206
+ const together = panel.placement.share + next.placement.share;
207
+ const mine = Math.max(MIN_SHARE, Math.min(together - MIN_SHARE, share));
208
+ return {
209
+ ...w,
210
+ panels: w.panels.map((p) => p.id === id
211
+ ? { ...p, placement: { ...panel.placement, share: mine } }
212
+ : p.id === next.id
213
+ ? { ...p, placement: { ...next.placement, share: together - mine } }
214
+ : p),
215
+ };
216
+ }
217
+ /** Moves or resizes a window. */
218
+ export function floatPanel(w, id, box) {
219
+ return withPanel(w, id, (p) => p.placement.zone === Zone.Floating ? { ...p, placement: { ...p.placement, ...box } } : p);
220
+ }
221
+ /** Bring to front. Without this, two overlapping windows cannot be pulled apart. */
222
+ export function raisePanel(w, id) {
223
+ const panel = findPanel(w, id);
224
+ if (panel === null || panel.placement.zone !== Zone.Floating)
225
+ return w;
226
+ const mine = panel.placement.z;
227
+ const above = w.panels.some((p) => p.id !== id && p.placement.zone === Zone.Floating && p.placement.z >= mine);
228
+ if (!above)
229
+ return w;
230
+ return withPanel(w, id, (p) => p.placement.zone === Zone.Floating
231
+ ? { ...p, placement: { ...p.placement, z: topZ(w.panels) + 1 } }
232
+ : p);
233
+ }
234
+ /** Collapse to its header without closing it: still there, still mounted, taking no room. */
235
+ export function collapsePanel(w, id) {
236
+ return withPanel(w, id, (p) => ({
237
+ ...p,
238
+ placement: { ...p.placement, collapsed: p.placement.collapsed !== true },
239
+ }));
240
+ }
241
+ /** Take the whole workspace. Calling it on what is already maximized gives it back. The peek is
242
+ * a drawer: it grows into the page instead (`peekToPage`). */
243
+ export function maximize(w, target) {
244
+ if (target === PEEK)
245
+ return w;
246
+ if (target !== null && target !== PAGE && findPanel(w, target) === null)
247
+ return w;
248
+ return { ...w, maximized: w.maximized === target ? null : target };
249
+ }
250
+ /** The WORKSPACE focus, which is who the shortcuts talk to. Opening a panel focuses it. */
251
+ export function focusPanel(w, target) {
252
+ if (w.focus === target)
253
+ return w;
254
+ if (refOn(w, target) === null)
255
+ return w;
256
+ return { ...w, focus: target };
257
+ }
258
+ /** The next one in reading order: the page, the peek over it, then each panel. */
259
+ export function cycleFocus(w, delta) {
260
+ const order = [PAGE, ...(w.peek === null ? [] : [PEEK]), ...w.panels.map((p) => p.id)];
261
+ const current = Math.max(0, order.indexOf(w.focus));
262
+ const next = order[(current + delta + order.length) % order.length];
263
+ return next === undefined ? w : focusPanel(w, next);
264
+ }
265
+ /**
266
+ * A panel grows into the page: its resource becomes the page, with the previous page in the
267
+ * breadcrumb, and the panel is gone. «Enlarge» that keeps where you came from.
268
+ */
269
+ export function expandPanel(w, id) {
270
+ const panel = findPanel(w, id);
271
+ if (panel === null)
272
+ return w;
273
+ return push(closePanel(w, id), panel.view);
274
+ }
275
+ /* ── Peek ───────────────────────────────────────────────────────────── */
276
+ /**
277
+ * Opened from outside — a row, ⌘K — the drawer starts fresh: its trail is only of what was
278
+ * drilled INSIDE it. It takes the focus, so ⌘K acts on what I am reading. Without a page there
279
+ * is nothing to read beside, so it becomes the page.
280
+ */
281
+ export function peek(w, view) {
282
+ if (w.page === null)
283
+ return push(w, view);
284
+ if (w.peek !== null && sameRef(w.peek, view) && w.peekTrail.length === 0)
285
+ return w;
286
+ return { ...w, peek: view, peekTrail: [], focus: PEEK, selected: dropSelection(w, PEEK) };
287
+ }
288
+ /** From inside the drawer: what it shows now goes to its trail, so ← comes back to it. */
289
+ export function drillPeek(w, view) {
290
+ if (w.peek === null)
291
+ return peek(w, view);
292
+ if (sameRef(w.peek, view))
293
+ return w;
294
+ return {
295
+ ...w,
296
+ peek: view,
297
+ peekTrail: [...w.peekTrail, w.peek],
298
+ selected: dropSelection(w, PEEK),
299
+ };
300
+ }
301
+ /** Another face, or the record a form in the drawer just created: no step in its trail. */
302
+ export function replacePeek(w, view) {
303
+ if (w.peek === null)
304
+ return peek(w, view);
305
+ if (sameRef(w.peek, view))
306
+ return w;
307
+ return { ...w, peek: view, selected: dropSelection(w, PEEK) };
308
+ }
309
+ /** Back to the step at `index` of the drawer's trail. */
310
+ export function peekGoTo(w, index) {
311
+ const target = w.peekTrail[index];
312
+ if (target === undefined)
313
+ return w;
314
+ return {
315
+ ...w,
316
+ peek: target,
317
+ peekTrail: w.peekTrail.slice(0, index),
318
+ selected: dropSelection(w, PEEK),
319
+ };
320
+ }
321
+ export const peekBack = (w) => w.peekTrail.length === 0 ? w : peekGoTo(w, w.peekTrail.length - 1);
322
+ export function closePeek(w) {
323
+ if (w.peek === null)
324
+ return w;
325
+ return {
326
+ ...w,
327
+ peek: null,
328
+ peekTrail: [],
329
+ focus: w.focus === PEEK ? PAGE : w.focus,
330
+ selected: dropSelection(w, PEEK),
331
+ };
332
+ }
333
+ /** The peek becomes the page. */
334
+ export function peekToPage(w) {
335
+ return w.peek === null ? w : closePeek(push(w, w.peek));
336
+ }
337
+ /** It docks to the right because the drawer was already there and the piece does not move. */
338
+ export function peekToSide(w, zone = Zone.Right) {
339
+ return w.peek === null ? w : closePeek(openPanel(w, w.peek, { zone }));
340
+ }
341
+ /* ── Context ────────────────────────────────────────────────────────── */
342
+ const NO_REFS = [];
343
+ function dropSelection(w, by) {
344
+ return w.selected?.by === by ? null : w.selected;
345
+ }
346
+ /**
347
+ * What a surface has selected — the rows of a list. The workspace keeps only references, so a
348
+ * chat in another domain can read them without knowing what they are. Selecting nothing clears
349
+ * it only if that surface owned it.
350
+ */
351
+ export function select(w, by, refs) {
352
+ if (refs.length === 0) {
353
+ return w.selected?.by === by ? { ...w, selected: null } : w;
354
+ }
355
+ const current = w.selected;
356
+ if (current?.by === by &&
357
+ current.refs.length === refs.length &&
358
+ current.refs.every((r, i) => sameRef(r, refs[i])))
359
+ return w;
360
+ return { ...w, selected: { by, refs } };
361
+ }
362
+ /** What the user is working on: the page, the peek or the panel that has the focus. */
363
+ export function focusedRef(w) {
364
+ return refOn(w, w.focus) ?? w.page;
365
+ }
366
+ /** Every resource the workspace shows or can go back to. */
367
+ const reachable = (w) => [
368
+ ...w.trail,
369
+ ...(w.page === null ? [] : [w.page]),
370
+ ...w.peekTrail,
371
+ ...(w.peek === null ? [] : [w.peek]),
372
+ ...w.panels.map((p) => p.view),
373
+ ];
374
+ /**
375
+ * The name a view gives its resource once it has loaded it — «Acme S.R.L.» instead of
376
+ * `acme`. `null` gives the `ViewSpec` title back. Names of what is no longer reachable are
377
+ * forgotten on the way, so the map does not grow with the session.
378
+ */
379
+ export function nameRef(w, ref, title) {
380
+ const key = formatRef(ref);
381
+ if ((w.titles[key] ?? null) === title)
382
+ return w;
383
+ const alive = new Set(reachable(w).map(formatRef));
384
+ const titles = {};
385
+ for (const [k, v] of Object.entries(w.titles))
386
+ if (k !== key && alive.has(k))
387
+ titles[k] = v;
388
+ if (title !== null)
389
+ titles[key] = title;
390
+ return { ...w, titles };
391
+ }
392
+ export const nameOf = (w, ref) => w.titles[formatRef(ref)] ?? null;
393
+ export const selectionOf = (w) => w.selected?.refs ?? NO_REFS;
@@ -0,0 +1,24 @@
1
+ /**
2
+ * The shell — React and nothing else: the composer draws its windows with its own skin
3
+ * (`sc-*` classes, `--sc-*` variables) and depends on no design system. The model comes in
4
+ * through the other door, `@sincpro/web-composer/domain`, which depends on nothing.
5
+ *
6
+ * Only the model TYPES that appear in these signatures are re-exported, so declaring a domain
7
+ * does not force a two-place import.
8
+ */
9
+ export type { Action, Crumb, OpenOptions, PaletteEntry, PickOptions, Registry, ResourceRef, SearchHit, ViewProps, ViewSpec, Workspace, WorkspaceApi, } from "./domain/index.js";
10
+ export { Destination, focusedRef, PAGE, PaletteSection, PEEK, selectionOf, Surface, Zone, } from "./domain/index.js";
11
+ export { Composer, type ComposerProps } from "./shell/Composer.js";
12
+ export { type ComposerCopy, composerCopy, ENGLISH_COMPOSER_COPY, installComposerCopy, SPANISH_COMPOSER_COPY, } from "./shell/copy.js";
13
+ export { type IconProps } from "./shell/icons.js";
14
+ export { WorkspacePalette, type WorkspacePaletteProps, } from "./shell/Palette.js";
15
+ export { type PanelActions, PanelFrame, type PanelFrameProps, } from "./shell/PanelFrame.js";
16
+ export { PanelHost, type PanelHostProps } from "./shell/PanelHost.js";
17
+ export { RecordLink, type RecordLinkProps } from "./shell/RecordLink.js";
18
+ export { type ComposerSkin, type ConfirmProps, DEFAULT_ICONS, type NoticeProps, type PeekProps, type SkinIcons, } from "./shell/skin.js";
19
+ export { ComposerStyles } from "./shell/styles.js";
20
+ export { ViewHost, type ViewHostProps } from "./shell/ViewHost.js";
21
+ export { type ApiFactory, createApiFactory, type Target, } from "./state/api.js";
22
+ export { type ConfirmDiscard, createWorkspaceStore, type StoreOptions, type WorkspaceStore, } from "./state/store.js";
23
+ export { useKeyboard, type UseKeyboardOptions, } from "./state/useKeyboard.js";
24
+ export { useWorkspace, useWorkspaceContext, useWorkspaceSlice, useWorkspaceState, } from "./state/useWorkspace.js";
package/dist/index.js ADDED
@@ -0,0 +1,23 @@
1
+ /**
2
+ * The shell — React and nothing else: the composer draws its windows with its own skin
3
+ * (`sc-*` classes, `--sc-*` variables) and depends on no design system. The model comes in
4
+ * through the other door, `@sincpro/web-composer/domain`, which depends on nothing.
5
+ *
6
+ * Only the model TYPES that appear in these signatures are re-exported, so declaring a domain
7
+ * does not force a two-place import.
8
+ */
9
+ export { Destination, focusedRef, PAGE, PaletteSection, PEEK, selectionOf, Surface, Zone, } from "./domain/index.js";
10
+ export { Composer } from "./shell/Composer.js";
11
+ export { composerCopy, ENGLISH_COMPOSER_COPY, installComposerCopy, SPANISH_COMPOSER_COPY, } from "./shell/copy.js";
12
+ export {} from "./shell/icons.js";
13
+ export { WorkspacePalette, } from "./shell/Palette.js";
14
+ export { PanelFrame, } from "./shell/PanelFrame.js";
15
+ export { PanelHost } from "./shell/PanelHost.js";
16
+ export { RecordLink } from "./shell/RecordLink.js";
17
+ export { DEFAULT_ICONS, } from "./shell/skin.js";
18
+ export { ComposerStyles } from "./shell/styles.js";
19
+ export { ViewHost } from "./shell/ViewHost.js";
20
+ export { createApiFactory, } from "./state/api.js";
21
+ export { createWorkspaceStore, } from "./state/store.js";
22
+ export { useKeyboard, } from "./state/useKeyboard.js";
23
+ export { useWorkspace, useWorkspaceContext, useWorkspaceSlice, useWorkspaceState, } from "./state/useWorkspace.js";
@@ -0,0 +1,23 @@
1
+ import { Destination, type Registry } from "../domain/index.js";
2
+ import { type ComposerSkin } from "./skin.js";
3
+ import type { WorkspaceStore } from "../state/store.js";
4
+ import { type ReactNode } from "react";
5
+ export type ComposerProps = {
6
+ workspace: WorkspaceStore;
7
+ registry: Registry;
8
+ /** Drawn when there is no page yet. */
9
+ empty?: ReactNode;
10
+ /** Keyboard shortcuts, ⌘K included. Turn off if the app already defines its own. */
11
+ keyboard?: boolean;
12
+ /** Where `api.open(ref)` goes when the caller does not say. The page — what people expect of
13
+ * a click on a record. */
14
+ defaultOpen?: Destination;
15
+ /** ⌘K, controlled — for a search button in the app's own bar. Without it the composer keeps
16
+ * the state and only the shortcut opens it. */
17
+ palette?: boolean;
18
+ onPaletteChange?: (open: boolean) => void;
19
+ /** Your design over the composer: icons, the peek drawer, the confirm dialog, the notice, or
20
+ * `styles: false` to drop the default sheet. What is not passed keeps the default. */
21
+ skin?: ComposerSkin;
22
+ };
23
+ export declare function Composer({ workspace, registry, empty, keyboard, defaultOpen, palette: paletteProp, onPaletteChange, skin: skinProp, }: ComposerProps): import("react").JSX.Element;
@@ -0,0 +1,88 @@
1
+ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { Destination, focusedRef, PAGE, PEEK, 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 { ChromeButton, ConfirmDialog, PeekDrawer } from "./Peek.js";
7
+ import { resolveSkin, SkinProvider, } from "./skin.js";
8
+ import { ComposerStyles } from "./styles.js";
9
+ import { ViewHost } from "./ViewHost.js";
10
+ import { createApiFactory } from "../state/api.js";
11
+ import { useDropMonitor } from "../state/drag.js";
12
+ import { useKeyboard } from "../state/useKeyboard.js";
13
+ import { useWorkspaceState } from "../state/useWorkspace.js";
14
+ import { useCallback, useEffect, useMemo, useState } from "react";
15
+ /**
16
+ * The question before throwing away what someone typed, drawn with the design system. The store
17
+ * calls it; a `confirmDiscard` given to the store replaces it.
18
+ */
19
+ function useDiscardDialog(workspace, titleOf) {
20
+ const [asking, setAsking] = useState(null);
21
+ useEffect(() => {
22
+ workspace.setConfirmDiscard((lost) => new Promise((resolve) => setAsking({
23
+ names: lost.map(titleOf),
24
+ answer: (ok) => {
25
+ setAsking(null);
26
+ resolve(ok);
27
+ },
28
+ })));
29
+ return () => workspace.setConfirmDiscard(null);
30
+ }, [workspace, titleOf]);
31
+ return asking;
32
+ }
33
+ export function Composer({ workspace, registry, empty, keyboard = true, defaultOpen = Destination.Page, palette: paletteProp, onPaletteChange, skin: skinProp, }) {
34
+ const skin = useMemo(() => resolveSkin(skinProp), [skinProp]);
35
+ const Drawer = skin.Peek ?? PeekDrawer;
36
+ const Confirm = skin.Confirm ?? ConfirmDialog;
37
+ const state = useWorkspaceState(workspace);
38
+ const copy = composerCopy();
39
+ const [paletteState, setPaletteState] = useState(false);
40
+ const palette = paletteProp ?? paletteState;
41
+ const setPalette = useCallback((open) => {
42
+ setPaletteState(open);
43
+ onPaletteChange?.(open);
44
+ }, [onPaletteChange]);
45
+ const api = useMemo(() => createApiFactory(workspace, { registry, defaultOpen }), [workspace, registry, defaultOpen]);
46
+ useEffect(() => api.dispose, [api]);
47
+ useKeyboard({
48
+ workspace,
49
+ enabled: keyboard,
50
+ onPalette: useCallback(() => setPalette(true), [setPalette]),
51
+ });
52
+ useDropMonitor(useMemo(() => ({
53
+ onPanelToZone: workspace.movePanel,
54
+ onRefToZone: (ref, zone) => workspace.openPanel(ref, zone),
55
+ }), [workspace]));
56
+ const asking = useDiscardDialog(workspace, api.titleOf);
57
+ /** Where a chosen search result opens. Without a page yet, the first one becomes it —
58
+ * whatever was asked: a peek or a window over nothing would be invisible. */
59
+ function open(ref, destination) {
60
+ if (workspace.get().page === null)
61
+ workspace.push(ref);
62
+ else if (destination === Destination.Peek)
63
+ workspace.peek(ref);
64
+ else if (destination === Destination.Window)
65
+ workspace.openPanel(ref, Zone.Floating);
66
+ else if (destination === Destination.Side)
67
+ workspace.openPanel(ref, api.zoneFor(registry.resolve(ref)));
68
+ else
69
+ workspace.push(ref);
70
+ }
71
+ /** An action runs with the API of the surface its resource is on — the page, the peek I am
72
+ * reading or a panel — so «audit» can open beside it or navigate it. */
73
+ function run(action) {
74
+ const w = workspace.get();
75
+ const ref = focusedRef(w);
76
+ if (ref === null)
77
+ return;
78
+ action.run(api.for(ref, w.focus === PAGE
79
+ ? { surface: Surface.Page }
80
+ : w.focus === PEEK
81
+ ? { surface: Surface.Peek }
82
+ : { surface: Surface.Panel, panelId: w.focus }));
83
+ }
84
+ const peeked = state.peek;
85
+ const peekSpec = peeked === null ? null : registry.resolve(peeked);
86
+ const cameFrom = state.peekTrail.at(-1);
87
+ return (_jsx(SkinProvider, { value: skin, children: _jsxs("div", { className: "sc-root", children: [skin.styles ? _jsx(ComposerStyles, {}) : null, state.page === null ? (_jsx("div", { className: "sc-empty", children: empty })) : (_jsx(PanelHost, { workspace: state, registry: registry, store: workspace, api: api, overlay: peeked === null ? null : (_jsx("div", { style: { display: "contents" }, onPointerDownCapture: () => workspace.focusPanel(PEEK), children: _jsx(Drawer, { title: api.titleOf(peeked), onClose: workspace.closePeek, actions: _jsxs(_Fragment, { children: [_jsx(ChromeButton, { onClick: () => workspace.peekToSide(peekSpec?.preferredZone ?? Zone.Right), children: copy.peek.openAsPanel }), cameFrom === undefined ? null : (_jsx(ChromeButton, { role: "ghost", icon: skin.icons.back, onClick: workspace.peekBack, children: copy.peek.back(api.titleOf(cameFrom)) })), _jsx(ChromeButton, { role: "ghost", onClick: workspace.peekToPage, children: copy.peek.openAsPage })] }), children: _jsx(ViewHost, { resource: peeked, registry: registry, api: api.for(peeked, { surface: Surface.Peek }) }) }) })) })), _jsx(Confirm, { open: asking !== null, title: copy.discard.title, detail: asking === null ? "" : copy.discard.detail(asking.names), keep: copy.discard.keep, discard: copy.discard.discard, onAnswer: (ok) => asking?.answer(ok) }), palette ? (_jsx(WorkspacePalette, { registry: registry, focused: focusedRef(state), onOpen: open, onAction: run, onClose: () => setPalette(false) })) : null] }) }));
88
+ }
@@ -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;