@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.
- package/LICENSE +21 -0
- package/README.md +269 -2
- package/dist/domain/enums.d.ts +55 -0
- package/dist/domain/enums.js +48 -0
- package/dist/domain/index.d.ts +9 -0
- package/dist/domain/index.js +8 -0
- package/dist/domain/layout.d.ts +92 -0
- package/dist/domain/layout.js +216 -0
- package/dist/domain/palette.d.ts +42 -0
- package/dist/domain/palette.js +89 -0
- package/dist/domain/registry.d.ts +135 -0
- package/dist/domain/registry.js +37 -0
- package/dist/domain/resource.d.ts +21 -0
- package/dist/domain/resource.js +62 -0
- package/dist/domain/url.d.ts +16 -0
- package/dist/domain/url.js +145 -0
- package/dist/domain/workspace.d.ts +174 -0
- package/dist/domain/workspace.js +393 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +23 -0
- package/dist/shell/Composer.d.ts +23 -0
- package/dist/shell/Composer.js +88 -0
- package/dist/shell/Palette.d.ts +9 -0
- package/dist/shell/Palette.js +124 -0
- package/dist/shell/PanelFrame.d.ts +38 -0
- package/dist/shell/PanelFrame.js +70 -0
- package/dist/shell/PanelHost.d.ts +23 -0
- package/dist/shell/PanelHost.js +201 -0
- package/dist/shell/Peek.d.ts +26 -0
- package/dist/shell/Peek.js +64 -0
- package/dist/shell/RecordLink.d.ts +12 -0
- package/dist/shell/RecordLink.js +55 -0
- package/dist/shell/ViewHost.d.ts +11 -0
- package/dist/shell/ViewHost.js +53 -0
- package/dist/shell/copy.d.ts +73 -0
- package/dist/shell/copy.js +130 -0
- package/dist/shell/icons.d.ts +22 -0
- package/dist/shell/icons.js +19 -0
- package/dist/shell/skin.d.ts +72 -0
- package/dist/shell/skin.js +35 -0
- package/dist/shell/styles.d.ts +19 -0
- package/dist/shell/styles.js +212 -0
- package/dist/state/api.d.ts +36 -0
- package/dist/state/api.js +170 -0
- package/dist/state/drag.d.ts +31 -0
- package/dist/state/drag.js +95 -0
- package/dist/state/store.d.ts +69 -0
- package/dist/state/store.js +200 -0
- package/dist/state/useKeyboard.d.ts +16 -0
- package/dist/state/useKeyboard.js +63 -0
- package/dist/state/useWorkspace.d.ts +23 -0
- package/dist/state/useWorkspace.js +34 -0
- package/package.json +83 -4
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import { Axis, HandleKind, Zone } from "./enums.js";
|
|
2
|
+
import { MAX_ZONE, MIN_ZONE, PAGE, panelsIn, } from "./workspace.js";
|
|
3
|
+
const len = (fr, px = 0) => ({ fr, px });
|
|
4
|
+
const plus = (a, b) => len(a.fr + b.fr, a.px + b.px);
|
|
5
|
+
const times = (a, k) => len(a.fr * k, a.px * k);
|
|
6
|
+
/** A length measured on a container `size` pixels long. */
|
|
7
|
+
export const resolve = (l, size) => l.fr * size + l.px;
|
|
8
|
+
const ZERO = len(0);
|
|
9
|
+
const FULL = { left: ZERO, top: ZERO, width: len(1), height: len(1) };
|
|
10
|
+
/** What a minimized panel keeps: its title bar. It matches the frame's header. */
|
|
11
|
+
export const STRIP = 36;
|
|
12
|
+
const isCollapsed = (p) => p.placement.collapsed === true;
|
|
13
|
+
const shareOf = (p) => (p.placement.zone === Zone.Floating ? 0 : p.placement.share);
|
|
14
|
+
const allCollapsed = (panels) => panels.length > 0 && panels.every(isCollapsed);
|
|
15
|
+
/** A zone with every panel minimized shrinks to one strip: the page takes the room back. */
|
|
16
|
+
function extent(w, zone) {
|
|
17
|
+
const panels = panelsIn(w, zone);
|
|
18
|
+
if (panels.length === 0)
|
|
19
|
+
return ZERO;
|
|
20
|
+
return allCollapsed(panels) ? len(0, STRIP) : len(w.zones[zone]);
|
|
21
|
+
}
|
|
22
|
+
/** The lengths the page and each zone take of the container. */
|
|
23
|
+
function frame(w) {
|
|
24
|
+
const left = extent(w, Zone.Left);
|
|
25
|
+
const right = extent(w, Zone.Right);
|
|
26
|
+
const bottom = extent(w, Zone.Bottom);
|
|
27
|
+
const middle = len(Math.max(0, 1 - left.fr - right.fr), -(left.px + right.px));
|
|
28
|
+
return { left, right, bottom, middle, top: len(1 - bottom.fr, -bottom.px) };
|
|
29
|
+
}
|
|
30
|
+
/** A zone stacks top to bottom; the bottom one lays its panels side by side. */
|
|
31
|
+
const stacks = (zone) => zone !== Zone.Bottom;
|
|
32
|
+
/**
|
|
33
|
+
* Along the zone, a minimized panel is a fixed strip and the open ones share the rest, each in
|
|
34
|
+
* proportion to its share among the OPEN ones — so restoring one gives back the size it had.
|
|
35
|
+
* `unit` is the length a share of 1 takes, what a handle between two of them measures against.
|
|
36
|
+
*/
|
|
37
|
+
function pieces(panels, along) {
|
|
38
|
+
const strips = panels.filter(isCollapsed).length;
|
|
39
|
+
const all = strips === panels.length;
|
|
40
|
+
const open = panels.reduce((sum, p) => (isCollapsed(p) ? sum : sum + shareOf(p)), 0);
|
|
41
|
+
const unit = open <= 0 ? ZERO : times(plus(along, len(0, -strips * STRIP)), 1 / open);
|
|
42
|
+
let cursor = ZERO;
|
|
43
|
+
const list = panels.map((panel) => {
|
|
44
|
+
const size = all
|
|
45
|
+
? times(along, 1 / panels.length)
|
|
46
|
+
: isCollapsed(panel)
|
|
47
|
+
? len(0, STRIP)
|
|
48
|
+
: times(unit, shareOf(panel));
|
|
49
|
+
const piece = { panel, start: cursor, size };
|
|
50
|
+
cursor = plus(cursor, size);
|
|
51
|
+
return piece;
|
|
52
|
+
});
|
|
53
|
+
return { list, unit };
|
|
54
|
+
}
|
|
55
|
+
function zoneSpace(f, zone) {
|
|
56
|
+
if (zone === Zone.Left)
|
|
57
|
+
return { left: ZERO, top: ZERO, width: f.left, height: len(1) };
|
|
58
|
+
if (zone === Zone.Right)
|
|
59
|
+
return { left: plus(f.left, f.middle), top: ZERO, width: f.right, height: len(1) };
|
|
60
|
+
return { left: f.left, top: f.top, width: f.middle, height: f.bottom };
|
|
61
|
+
}
|
|
62
|
+
function docked(w, f, zone) {
|
|
63
|
+
const panels = panelsIn(w, zone);
|
|
64
|
+
const space = zoneSpace(f, zone);
|
|
65
|
+
const vertical = stacks(zone);
|
|
66
|
+
const all = allCollapsed(panels);
|
|
67
|
+
return pieces(panels, vertical ? space.height : space.width).list.map(({ panel, start, size }) => ({
|
|
68
|
+
id: panel.id,
|
|
69
|
+
view: panel.view,
|
|
70
|
+
panel,
|
|
71
|
+
box: vertical
|
|
72
|
+
? { left: space.left, top: plus(space.top, start), width: space.width, height: size }
|
|
73
|
+
: {
|
|
74
|
+
left: plus(space.left, start),
|
|
75
|
+
top: space.top,
|
|
76
|
+
width: size,
|
|
77
|
+
height: space.height,
|
|
78
|
+
},
|
|
79
|
+
floating: false,
|
|
80
|
+
z: 0,
|
|
81
|
+
visible: true,
|
|
82
|
+
collapsed: isCollapsed(panel),
|
|
83
|
+
rail: isCollapsed(panel) && (vertical ? all : !all),
|
|
84
|
+
}));
|
|
85
|
+
}
|
|
86
|
+
function windows(panels) {
|
|
87
|
+
return panels.flatMap((panel) => panel.placement.zone !== Zone.Floating
|
|
88
|
+
? []
|
|
89
|
+
: [
|
|
90
|
+
{
|
|
91
|
+
id: panel.id,
|
|
92
|
+
view: panel.view,
|
|
93
|
+
panel,
|
|
94
|
+
box: {
|
|
95
|
+
left: len(0, panel.placement.x),
|
|
96
|
+
top: len(0, panel.placement.y),
|
|
97
|
+
width: len(0, panel.placement.w),
|
|
98
|
+
height: len(0, isCollapsed(panel) ? STRIP : panel.placement.h),
|
|
99
|
+
},
|
|
100
|
+
floating: true,
|
|
101
|
+
z: panel.placement.z,
|
|
102
|
+
visible: true,
|
|
103
|
+
collapsed: isCollapsed(panel),
|
|
104
|
+
rail: false,
|
|
105
|
+
},
|
|
106
|
+
]);
|
|
107
|
+
}
|
|
108
|
+
export function layout(w) {
|
|
109
|
+
const f = frame(w);
|
|
110
|
+
const slots = [
|
|
111
|
+
...(w.page === null
|
|
112
|
+
? []
|
|
113
|
+
: [
|
|
114
|
+
{
|
|
115
|
+
id: PAGE,
|
|
116
|
+
view: w.page,
|
|
117
|
+
panel: null,
|
|
118
|
+
box: { left: f.left, top: ZERO, width: f.middle, height: f.top },
|
|
119
|
+
floating: false,
|
|
120
|
+
z: 0,
|
|
121
|
+
visible: true,
|
|
122
|
+
collapsed: false,
|
|
123
|
+
rail: false,
|
|
124
|
+
},
|
|
125
|
+
]),
|
|
126
|
+
...docked(w, f, Zone.Left),
|
|
127
|
+
...docked(w, f, Zone.Right),
|
|
128
|
+
...docked(w, f, Zone.Bottom),
|
|
129
|
+
...windows(w.panels),
|
|
130
|
+
];
|
|
131
|
+
if (w.maximized === null || !slots.some((s) => s.id === w.maximized))
|
|
132
|
+
return slots;
|
|
133
|
+
return slots.map((slot) => slot.id === w.maximized
|
|
134
|
+
? { ...slot, box: FULL, floating: false, visible: true, collapsed: false, rail: false }
|
|
135
|
+
: { ...slot, visible: false });
|
|
136
|
+
}
|
|
137
|
+
/** Sorted by id so sibling order does not change when a panel changes zone — reordering
|
|
138
|
+
* siblings is what unmounts. */
|
|
139
|
+
export function stableOrder(slots) {
|
|
140
|
+
return [...slots].sort((a, b) => a.id.localeCompare(b.id));
|
|
141
|
+
}
|
|
142
|
+
/** The stored size a pointer `p` pixels into a container `size` pixels long asks for. */
|
|
143
|
+
export function sizeAt(handle, p, size) {
|
|
144
|
+
const origin = resolve(handle.origin, size);
|
|
145
|
+
const span = resolve(handle.span, size);
|
|
146
|
+
if (span <= 0)
|
|
147
|
+
return 0;
|
|
148
|
+
return (handle.invert ? origin - p : p - origin) / span;
|
|
149
|
+
}
|
|
150
|
+
export function handles(w) {
|
|
151
|
+
if (w.maximized !== null)
|
|
152
|
+
return [];
|
|
153
|
+
const f = frame(w);
|
|
154
|
+
const result = [];
|
|
155
|
+
const resizable = (zone) => {
|
|
156
|
+
const panels = panelsIn(w, zone);
|
|
157
|
+
return panels.length > 0 && !allCollapsed(panels);
|
|
158
|
+
};
|
|
159
|
+
if (resizable(Zone.Left))
|
|
160
|
+
result.push({
|
|
161
|
+
kind: HandleKind.Zone,
|
|
162
|
+
zone: Zone.Left,
|
|
163
|
+
axis: Axis.X,
|
|
164
|
+
at: f.left,
|
|
165
|
+
origin: ZERO,
|
|
166
|
+
span: len(1),
|
|
167
|
+
invert: false,
|
|
168
|
+
});
|
|
169
|
+
if (resizable(Zone.Right))
|
|
170
|
+
result.push({
|
|
171
|
+
kind: HandleKind.Zone,
|
|
172
|
+
zone: Zone.Right,
|
|
173
|
+
axis: Axis.X,
|
|
174
|
+
at: plus(f.left, f.middle),
|
|
175
|
+
origin: len(1),
|
|
176
|
+
span: len(1),
|
|
177
|
+
invert: true,
|
|
178
|
+
});
|
|
179
|
+
if (resizable(Zone.Bottom))
|
|
180
|
+
result.push({
|
|
181
|
+
kind: HandleKind.Zone,
|
|
182
|
+
zone: Zone.Bottom,
|
|
183
|
+
axis: Axis.Y,
|
|
184
|
+
at: f.top,
|
|
185
|
+
origin: len(1),
|
|
186
|
+
span: len(1),
|
|
187
|
+
invert: true,
|
|
188
|
+
});
|
|
189
|
+
for (const zone of [Zone.Left, Zone.Right, Zone.Bottom]) {
|
|
190
|
+
if (!resizable(zone))
|
|
191
|
+
continue;
|
|
192
|
+
const space = zoneSpace(f, zone);
|
|
193
|
+
const vertical = stacks(zone);
|
|
194
|
+
const base = vertical ? space.top : space.left;
|
|
195
|
+
const { list, unit } = pieces(panelsIn(w, zone), vertical ? space.height : space.width);
|
|
196
|
+
list.forEach((piece, i) => {
|
|
197
|
+
const next = list[i + 1];
|
|
198
|
+
if (next === undefined || isCollapsed(piece.panel) || isCollapsed(next.panel))
|
|
199
|
+
return;
|
|
200
|
+
result.push({
|
|
201
|
+
kind: HandleKind.Panel,
|
|
202
|
+
panelId: piece.panel.id,
|
|
203
|
+
axis: vertical ? Axis.Y : Axis.X,
|
|
204
|
+
at: plus(base, next.start),
|
|
205
|
+
origin: plus(base, piece.start),
|
|
206
|
+
span: unit,
|
|
207
|
+
invert: false,
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
return result;
|
|
212
|
+
}
|
|
213
|
+
/** How far an arrow key moves a handle. A mouse-only handle leaves the workspace
|
|
214
|
+
* half-assembled for anyone who does not use a pointer. */
|
|
215
|
+
export const KEYBOARD_STEP = 0.02;
|
|
216
|
+
export const ZONE_LIMITS = { min: MIN_ZONE, max: MAX_ZONE };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { Action, Registry, ViewSpec } from "./registry.js";
|
|
2
|
+
import { type ResourceRef } from "./resource.js";
|
|
3
|
+
/**
|
|
4
|
+
* ⌘K in three layers: GO TO a screen, DO something with what I am on, and
|
|
5
|
+
* what each domain FOUND for the query. Pure on purpose — what the palette
|
|
6
|
+
* offers and how a query matches are tested without drawing it.
|
|
7
|
+
*/
|
|
8
|
+
export declare const PaletteSection: {
|
|
9
|
+
readonly Goto: "goto";
|
|
10
|
+
readonly Actions: "actions";
|
|
11
|
+
readonly Found: "found";
|
|
12
|
+
};
|
|
13
|
+
export type PaletteSection = (typeof PaletteSection)[keyof typeof PaletteSection];
|
|
14
|
+
type Icon = ViewSpec["icon"];
|
|
15
|
+
export type PaletteEntry = {
|
|
16
|
+
section: typeof PaletteSection.Goto | typeof PaletteSection.Found;
|
|
17
|
+
id: string;
|
|
18
|
+
label: string;
|
|
19
|
+
detail?: string;
|
|
20
|
+
icon?: Icon;
|
|
21
|
+
ref: ResourceRef;
|
|
22
|
+
} | {
|
|
23
|
+
section: typeof PaletteSection.Actions;
|
|
24
|
+
id: string;
|
|
25
|
+
label: string;
|
|
26
|
+
icon?: Icon;
|
|
27
|
+
shortcut?: string;
|
|
28
|
+
keywords?: readonly string[];
|
|
29
|
+
action: Action;
|
|
30
|
+
};
|
|
31
|
+
/** Shorter than this, every domain answers with half its table. */
|
|
32
|
+
export declare const MIN_QUERY = 2;
|
|
33
|
+
/** Every word of the query, in any order, ignoring accents and case:
|
|
34
|
+
* «facturacion» finds «Facturación», «nueva fac» finds «Nueva factura». */
|
|
35
|
+
export declare function matches(entry: PaletteEntry, query: string): boolean;
|
|
36
|
+
/** What is known without asking anyone: the screens, and the actions of the
|
|
37
|
+
* resource the user is on. */
|
|
38
|
+
export declare function localEntries(registry: Registry, focused: ResourceRef | null): readonly PaletteEntry[];
|
|
39
|
+
/** Asks every domain that searches. A domain that fails is logged and left
|
|
40
|
+
* out; the others still answer. */
|
|
41
|
+
export declare function searchEntries(registry: Registry, query: string): Promise<readonly PaletteEntry[]>;
|
|
42
|
+
export {};
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { formatRef } from "./resource.js";
|
|
2
|
+
/**
|
|
3
|
+
* ⌘K in three layers: GO TO a screen, DO something with what I am on, and
|
|
4
|
+
* what each domain FOUND for the query. Pure on purpose — what the palette
|
|
5
|
+
* offers and how a query matches are tested without drawing it.
|
|
6
|
+
*/
|
|
7
|
+
export const PaletteSection = {
|
|
8
|
+
Goto: "goto",
|
|
9
|
+
Actions: "actions",
|
|
10
|
+
Found: "found",
|
|
11
|
+
};
|
|
12
|
+
/** Shorter than this, every domain answers with half its table. */
|
|
13
|
+
export const MIN_QUERY = 2;
|
|
14
|
+
const fold = (text) => text
|
|
15
|
+
.normalize("NFD")
|
|
16
|
+
.replace(/\p{Diacritic}/gu, "")
|
|
17
|
+
.toLowerCase();
|
|
18
|
+
/** Every word of the query, in any order, ignoring accents and case:
|
|
19
|
+
* «facturacion» finds «Facturación», «nueva fac» finds «Nueva factura». */
|
|
20
|
+
export function matches(entry, query) {
|
|
21
|
+
const words = fold(query).split(/\s+/).filter(Boolean);
|
|
22
|
+
if (words.length === 0)
|
|
23
|
+
return true;
|
|
24
|
+
const keywords = entry.section === PaletteSection.Actions ? (entry.keywords ?? []) : [];
|
|
25
|
+
const detail = entry.section === PaletteSection.Actions ? "" : (entry.detail ?? "");
|
|
26
|
+
const haystack = fold([entry.label, detail, ...keywords].join(" "));
|
|
27
|
+
return words.every((word) => haystack.includes(word));
|
|
28
|
+
}
|
|
29
|
+
/** What is known without asking anyone: the screens, and the actions of the
|
|
30
|
+
* resource the user is on. */
|
|
31
|
+
export function localEntries(registry, focused) {
|
|
32
|
+
const goto = registry
|
|
33
|
+
.all()
|
|
34
|
+
.filter((spec) => spec.goto === true)
|
|
35
|
+
.map((spec) => {
|
|
36
|
+
const ref = { domain: spec.domain, kind: spec.kind };
|
|
37
|
+
return {
|
|
38
|
+
section: PaletteSection.Goto,
|
|
39
|
+
id: `goto:${formatRef(ref)}`,
|
|
40
|
+
label: spec.title(ref),
|
|
41
|
+
...(spec.icon === undefined ? {} : { icon: spec.icon }),
|
|
42
|
+
ref,
|
|
43
|
+
};
|
|
44
|
+
});
|
|
45
|
+
const spec = focused === null ? null : registry.resolve(focused);
|
|
46
|
+
const actions = (focused === null ? [] : (spec?.actions?.(focused) ?? [])).map((action) => ({
|
|
47
|
+
section: PaletteSection.Actions,
|
|
48
|
+
id: `action:${action.id}`,
|
|
49
|
+
label: action.label,
|
|
50
|
+
...(action.icon === undefined ? {} : { icon: action.icon }),
|
|
51
|
+
...(action.shortcut === undefined ? {} : { shortcut: action.shortcut }),
|
|
52
|
+
...(action.keywords === undefined ? {} : { keywords: action.keywords }),
|
|
53
|
+
action,
|
|
54
|
+
}));
|
|
55
|
+
return [...actions, ...goto];
|
|
56
|
+
}
|
|
57
|
+
/** Asks every domain that searches. A domain that fails is logged and left
|
|
58
|
+
* out; the others still answer. */
|
|
59
|
+
export async function searchEntries(registry, query) {
|
|
60
|
+
const text = query.trim();
|
|
61
|
+
if (text.length < MIN_QUERY)
|
|
62
|
+
return [];
|
|
63
|
+
const specs = registry.all().filter((spec) => spec.search !== undefined);
|
|
64
|
+
const answers = await Promise.allSettled(specs.map((spec) => spec.search(text)));
|
|
65
|
+
const seen = new Set();
|
|
66
|
+
const found = [];
|
|
67
|
+
answers.forEach((answer, i) => {
|
|
68
|
+
if (answer.status === "rejected") {
|
|
69
|
+
console.warn(`[composer] search failed in «${specs[i]?.domain}»`, answer.reason);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
for (const hit of answer.value) {
|
|
73
|
+
const id = `found:${formatRef(hit.ref)}`;
|
|
74
|
+
if (seen.has(id))
|
|
75
|
+
continue;
|
|
76
|
+
seen.add(id);
|
|
77
|
+
const icon = registry.resolve(hit.ref)?.icon;
|
|
78
|
+
found.push({
|
|
79
|
+
section: PaletteSection.Found,
|
|
80
|
+
id,
|
|
81
|
+
label: hit.label,
|
|
82
|
+
...(hit.detail === undefined ? {} : { detail: hit.detail }),
|
|
83
|
+
...(icon === undefined ? {} : { icon }),
|
|
84
|
+
ref: hit.ref,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
return found;
|
|
89
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import type { Destination, Surface, Zone } from "./enums.js";
|
|
2
|
+
import type { ResourceRef } from "./resource.js";
|
|
3
|
+
import type { ComponentType } from "react";
|
|
4
|
+
export { Surface } from "./enums.js";
|
|
5
|
+
/**
|
|
6
|
+
* Each domain declares itself. A `switch (ref.domain)` would force the host to import every
|
|
7
|
+
* domain, and one bundle would drag the others in.
|
|
8
|
+
*/
|
|
9
|
+
export type OpenOptions = {
|
|
10
|
+
/** Where it opens. Without it, the workspace's default: the page. */
|
|
11
|
+
to?: Destination;
|
|
12
|
+
};
|
|
13
|
+
export type PickOptions = {
|
|
14
|
+
/** Where the chooser opens. A window unless said otherwise. */
|
|
15
|
+
to?: typeof Destination.Peek | typeof Destination.Window | typeof Destination.Side;
|
|
16
|
+
};
|
|
17
|
+
/** One step of a breadcrumb, ready for `List.Crumbs` or `Record.Crumbs`. */
|
|
18
|
+
export type Crumb = {
|
|
19
|
+
label: string;
|
|
20
|
+
onClick?: () => void;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* What the workspace lends a view. It can open things and navigate; it cannot read the
|
|
24
|
+
* workspace — if it could, two domains would couple through it instead of through a reference.
|
|
25
|
+
*/
|
|
26
|
+
export type WorkspaceApi = {
|
|
27
|
+
self: ResourceRef;
|
|
28
|
+
surface: Surface;
|
|
29
|
+
/**
|
|
30
|
+
* The one way to open a resource: `open(ref)` goes where the workspace opens by default (the
|
|
31
|
+
* page), `open(ref, { to })` where the caller says. Opening the page from a window leaves the
|
|
32
|
+
* window where it is; from the peek it closes the drawer — it sat over a page that is no longer
|
|
33
|
+
* there. Inside the peek a plain `open(ref)` drills IN the drawer, and ← comes back: reading
|
|
34
|
+
* does not throw the reader out.
|
|
35
|
+
*/
|
|
36
|
+
open: (ref: ResourceRef, options?: OpenOptions) => void;
|
|
37
|
+
/** Show `ref` HERE, remembering where it was: a crumb on the page, a step back in the peek. A
|
|
38
|
+
* panel has no way back: it just changes. */
|
|
39
|
+
navigate: (ref: ResourceRef) => void;
|
|
40
|
+
/** Show `ref` HERE without a step to come back to: the record a form just created, a filter. */
|
|
41
|
+
replace: (ref: ResourceRef) => void;
|
|
42
|
+
/** This resource with `params` merged (`""` removes one), in place: filters live in the
|
|
43
|
+
* reference — they survive a reload, a link and a move to another surface. */
|
|
44
|
+
setParams: (params: Record<string, string>) => void;
|
|
45
|
+
/** One step back: the page's breadcrumb or the peek's own trail. Nothing in a panel. */
|
|
46
|
+
back: () => void;
|
|
47
|
+
/** A window, a side panel or the peek grows into the page. Nothing on the page itself. */
|
|
48
|
+
expand: () => void;
|
|
49
|
+
/** Leave what this view is in: its panel or the peek closes; the page goes one step back.
|
|
50
|
+
* «Cancel» on a form is this one call wherever the form was opened. */
|
|
51
|
+
close: () => void;
|
|
52
|
+
/** Request workspace focus so shortcuts talk to this view. */
|
|
53
|
+
focus: () => void;
|
|
54
|
+
/**
|
|
55
|
+
* The way here, ready for `List.Crumbs` / `Record.Crumbs`: the pages before, each a link, and
|
|
56
|
+
* this one. In the peek the page's crumbs leave the drawer and its own steps stay in it. Empty
|
|
57
|
+
* in a panel: its frame already says what it shows.
|
|
58
|
+
*/
|
|
59
|
+
crumbs: () => readonly Crumb[];
|
|
60
|
+
/** The name this resource goes by once loaded — the crumbs, the frame and the peek show it
|
|
61
|
+
* instead of the `ViewSpec` title. `null` gives the title back. Ignored once this surface
|
|
62
|
+
* shows something else, so a late answer cannot rename the wrong record. */
|
|
63
|
+
setTitle: (title: string | null) => void;
|
|
64
|
+
/** Something typed here would be lost. While it is, anything that would throw this view away
|
|
65
|
+
* — a crumb, a close, a reset, the browser's back or a reload — asks first. Set it back to
|
|
66
|
+
* `false` after saving, BEFORE leaving. */
|
|
67
|
+
setDirty: (dirty: boolean) => void;
|
|
68
|
+
/** Publish what THIS surface has selected, so a detail panel or an assistant can follow it.
|
|
69
|
+
* An empty list clears it. */
|
|
70
|
+
select: (refs: readonly ResourceRef[]) => void;
|
|
71
|
+
/** What the user is working on — references only. Read-only on purpose. */
|
|
72
|
+
context: {
|
|
73
|
+
focused: () => ResourceRef | null;
|
|
74
|
+
selection: () => readonly ResourceRef[];
|
|
75
|
+
subscribe: (listener: () => void) => () => void;
|
|
76
|
+
};
|
|
77
|
+
/** Open `ref` as a chooser: when its view calls `answer`, `onPick` receives the value and
|
|
78
|
+
* the chooser closes. */
|
|
79
|
+
pick: (ref: ResourceRef, onPick: (value: ResourceRef) => void, options?: PickOptions) => void;
|
|
80
|
+
/** From a chooser: deliver the value to whoever asked, and close this surface. */
|
|
81
|
+
answer: (value: ResourceRef) => void;
|
|
82
|
+
};
|
|
83
|
+
/** Named `resource` rather than `ref` because `ref` is a reserved React prop. */
|
|
84
|
+
export type ViewProps = {
|
|
85
|
+
resource: ResourceRef;
|
|
86
|
+
api: WorkspaceApi;
|
|
87
|
+
};
|
|
88
|
+
type Icon = ComponentType<{
|
|
89
|
+
className?: string;
|
|
90
|
+
strokeWidth?: number;
|
|
91
|
+
}>;
|
|
92
|
+
/** Something a domain lets the user do with one of its resources: «Audit», «Reconcile».
|
|
93
|
+
* ⌘K offers the actions of the resource the user is on. */
|
|
94
|
+
export type Action = {
|
|
95
|
+
id: string;
|
|
96
|
+
label: string;
|
|
97
|
+
icon?: Icon;
|
|
98
|
+
/** Drawn, not bound: the palette is where the shortcut gets learned. */
|
|
99
|
+
shortcut?: string;
|
|
100
|
+
keywords?: readonly string[];
|
|
101
|
+
/** Receives the API of the surface the resource is on. */
|
|
102
|
+
run: (api: WorkspaceApi) => void;
|
|
103
|
+
};
|
|
104
|
+
/** A record a domain found for a ⌘K query. */
|
|
105
|
+
export type SearchHit = {
|
|
106
|
+
ref: ResourceRef;
|
|
107
|
+
label: string;
|
|
108
|
+
/** What tells two hits with the same label apart: the customer, the date. */
|
|
109
|
+
detail?: string;
|
|
110
|
+
};
|
|
111
|
+
export type ViewSpec = {
|
|
112
|
+
domain: string;
|
|
113
|
+
kind: string;
|
|
114
|
+
title: (ref: ResourceRef) => string;
|
|
115
|
+
icon?: Icon;
|
|
116
|
+
/** Where to open when asked beside the page with no zone. */
|
|
117
|
+
preferredZone?: Zone;
|
|
118
|
+
/** Listed under «Go to» in ⌘K, opened as `domain:kind` with no id. */
|
|
119
|
+
goto?: boolean;
|
|
120
|
+
actions?: (ref: ResourceRef) => readonly Action[];
|
|
121
|
+
/** ⌘K asks every domain that declares it; one that fails does not take the others'
|
|
122
|
+
* results with it. */
|
|
123
|
+
search?: (query: string) => Promise<readonly SearchHit[]>;
|
|
124
|
+
/** Lazy: the point where a remote module enters without changing anything else. */
|
|
125
|
+
load: () => Promise<ComponentType<ViewProps>>;
|
|
126
|
+
};
|
|
127
|
+
export type Registry = {
|
|
128
|
+
resolve: (ref: ResourceRef) => ViewSpec | null;
|
|
129
|
+
domains: () => readonly string[];
|
|
130
|
+
all: () => readonly ViewSpec[];
|
|
131
|
+
};
|
|
132
|
+
/** A function, not a singleton, so two workspaces with different domains can
|
|
133
|
+
* mount on the same page. */
|
|
134
|
+
export declare function createRegistry(specs: readonly ViewSpec[]): Registry;
|
|
135
|
+
export declare function loadView(spec: ViewSpec): Promise<ComponentType<ViewProps>>;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export { Surface } from "./enums.js";
|
|
2
|
+
const specKey = (spec) => `${spec.domain}:${spec.kind}`;
|
|
3
|
+
/** A function, not a singleton, so two workspaces with different domains can
|
|
4
|
+
* mount on the same page. */
|
|
5
|
+
export function createRegistry(specs) {
|
|
6
|
+
const byKey = new Map();
|
|
7
|
+
for (const spec of specs) {
|
|
8
|
+
const key = specKey(spec);
|
|
9
|
+
if (byKey.has(key)) {
|
|
10
|
+
console.warn(`[composer] ${key} is already registered; ignoring the duplicate`);
|
|
11
|
+
continue;
|
|
12
|
+
}
|
|
13
|
+
byKey.set(key, spec);
|
|
14
|
+
}
|
|
15
|
+
return {
|
|
16
|
+
resolve: (ref) => byKey.get(specKey(ref)) ?? null,
|
|
17
|
+
domains: () => [...new Set(specs.map((s) => s.domain))],
|
|
18
|
+
all: () => [...byKey.values()],
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Cache against the `ViewSpec` object, not `domain:kind`. With a string key,
|
|
23
|
+
* two registries declaring the same pair — which `createRegistry` promises to
|
|
24
|
+
* allow — shared an entry and the second received the first's component.
|
|
25
|
+
*
|
|
26
|
+
* A failing module is not cached, so retry can work.
|
|
27
|
+
*/
|
|
28
|
+
const modules = new WeakMap();
|
|
29
|
+
export function loadView(spec) {
|
|
30
|
+
const cached = modules.get(spec);
|
|
31
|
+
if (cached !== undefined)
|
|
32
|
+
return cached;
|
|
33
|
+
const loading = spec.load();
|
|
34
|
+
modules.set(spec, loading);
|
|
35
|
+
loading.catch(() => modules.delete(spec));
|
|
36
|
+
return loading;
|
|
37
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The workspace stores references, not data — that way one domain can carry a resource
|
|
3
|
+
* belonging to another without knowing its shape.
|
|
4
|
+
*
|
|
5
|
+
* Format: `domain:kind/id?param=value`
|
|
6
|
+
*/
|
|
7
|
+
export type ResourceRef = {
|
|
8
|
+
domain: string;
|
|
9
|
+
kind: string;
|
|
10
|
+
id?: string;
|
|
11
|
+
params?: Readonly<Record<string, string>>;
|
|
12
|
+
};
|
|
13
|
+
export declare function isRef(value: unknown): value is ResourceRef;
|
|
14
|
+
/** Params are sorted so that two equivalent refs produce the same text and `sameRef` works. */
|
|
15
|
+
export declare function formatRef(ref: ResourceRef): string;
|
|
16
|
+
export declare function parseRef(text: string): ResourceRef | null;
|
|
17
|
+
export declare function sameRef(a: ResourceRef, b: ResourceRef): boolean;
|
|
18
|
+
/** Merges instead of replacing; almost always one param changes and the rest are kept. */
|
|
19
|
+
export declare function withParams(ref: ResourceRef, params: Record<string, string>): ResourceRef;
|
|
20
|
+
/** The resource with no params: tells whether two panels look at the same thing. */
|
|
21
|
+
export declare function rootRef(ref: ResourceRef): ResourceRef;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
const SEGMENT = /^[A-Za-z0-9_.~-]+$/;
|
|
2
|
+
const REF = /^([^:]+):([^/?]+)(?:\/([^?]*))?(?:\?(.*))?$/;
|
|
3
|
+
function parseParams(query) {
|
|
4
|
+
const params = {};
|
|
5
|
+
for (const pair of query.split("&")) {
|
|
6
|
+
const eq = pair.indexOf("=");
|
|
7
|
+
if (eq > 0)
|
|
8
|
+
params[decodeURIComponent(pair.slice(0, eq))] = decodeURIComponent(pair.slice(eq + 1));
|
|
9
|
+
}
|
|
10
|
+
return params;
|
|
11
|
+
}
|
|
12
|
+
export function isRef(value) {
|
|
13
|
+
if (typeof value !== "object" || value === null)
|
|
14
|
+
return false;
|
|
15
|
+
const ref = value;
|
|
16
|
+
return (typeof ref.domain === "string" &&
|
|
17
|
+
SEGMENT.test(ref.domain) &&
|
|
18
|
+
typeof ref.kind === "string" &&
|
|
19
|
+
SEGMENT.test(ref.kind));
|
|
20
|
+
}
|
|
21
|
+
/** Params are sorted so that two equivalent refs produce the same text and `sameRef` works. */
|
|
22
|
+
export function formatRef(ref) {
|
|
23
|
+
const id = ref.id ? `/${encodeURIComponent(ref.id)}` : "";
|
|
24
|
+
const entries = Object.entries(ref.params ?? {})
|
|
25
|
+
.filter(([, v]) => v !== "")
|
|
26
|
+
.sort(([a], [b]) => a.localeCompare(b));
|
|
27
|
+
const query = entries.length
|
|
28
|
+
? `?${entries.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`).join("&")}`
|
|
29
|
+
: "";
|
|
30
|
+
return `${ref.domain}:${ref.kind}${id}${query}`;
|
|
31
|
+
}
|
|
32
|
+
export function parseRef(text) {
|
|
33
|
+
const match = REF.exec(text);
|
|
34
|
+
if (!match)
|
|
35
|
+
return null;
|
|
36
|
+
const [, domain, kind, id, query] = match;
|
|
37
|
+
if (!domain || !kind || !SEGMENT.test(domain) || !SEGMENT.test(kind))
|
|
38
|
+
return null;
|
|
39
|
+
const params = query ? parseParams(query) : {};
|
|
40
|
+
return {
|
|
41
|
+
domain,
|
|
42
|
+
kind,
|
|
43
|
+
...(id ? { id: decodeURIComponent(id) } : {}),
|
|
44
|
+
...(Object.keys(params).length ? { params } : {}),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
export function sameRef(a, b) {
|
|
48
|
+
return formatRef(a) === formatRef(b);
|
|
49
|
+
}
|
|
50
|
+
/** Merges instead of replacing; almost always one param changes and the rest are kept. */
|
|
51
|
+
export function withParams(ref, params) {
|
|
52
|
+
const merged = { ...(ref.params ?? {}), ...params };
|
|
53
|
+
for (const [key, value] of Object.entries(merged))
|
|
54
|
+
if (value === "")
|
|
55
|
+
delete merged[key];
|
|
56
|
+
const { params: _, ...rest } = ref;
|
|
57
|
+
return Object.keys(merged).length ? { ...rest, params: merged } : rest;
|
|
58
|
+
}
|
|
59
|
+
/** The resource with no params: tells whether two panels look at the same thing. */
|
|
60
|
+
export function rootRef(ref) {
|
|
61
|
+
return { domain: ref.domain, kind: ref.kind, ...(ref.id ? { id: ref.id } : {}) };
|
|
62
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type ResourceRef } from "./resource.js";
|
|
2
|
+
import { type Workspace } from "./workspace.js";
|
|
3
|
+
export declare function toHash(w: Workspace): string;
|
|
4
|
+
export declare function fromHash(hash: string): Workspace;
|
|
5
|
+
/** What the URL does not carry, written into each browser history entry. */
|
|
6
|
+
export type SessionMemory = {
|
|
7
|
+
trail: readonly ResourceRef[];
|
|
8
|
+
peekTrail: readonly ResourceRef[];
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* The browser moved through its history: the URL says WHERE, the session says how I got there.
|
|
12
|
+
* With the entry's own memory the trails are exact; without it they are reconciled. The
|
|
13
|
+
* selection survives only while its surface still shows the same resource, and the names views
|
|
14
|
+
* gave stay.
|
|
15
|
+
*/
|
|
16
|
+
export declare function restore(now: Workspace, url: Workspace, memory: SessionMemory | null): Workspace;
|