@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,36 @@
|
|
|
1
|
+
import { Destination, type Registry, type ResourceRef, Surface, type ViewSpec, type WorkspaceApi, Zone } from "../domain/index.js";
|
|
2
|
+
import type { WorkspaceStore } from "./store.js";
|
|
3
|
+
/**
|
|
4
|
+
* The API each view receives is built once per surface AND resource, and kept while the
|
|
5
|
+
* surface shows it. Recreating it on every workspace change swapped the prop on every mounted
|
|
6
|
+
* view, so moving one panel redrew the other five. Rebuilding it when the resource changes is
|
|
7
|
+
* what lets a late call from the view that WAS there — a name that arrives after navigating —
|
|
8
|
+
* be recognised and ignored.
|
|
9
|
+
*
|
|
10
|
+
* Anything that needs current state reads it with `store.get()` at call time, never captured —
|
|
11
|
+
* that is what lets the object stay stable without going stale.
|
|
12
|
+
*/
|
|
13
|
+
export type Target = {
|
|
14
|
+
surface: typeof Surface.Page;
|
|
15
|
+
} | {
|
|
16
|
+
surface: typeof Surface.Panel;
|
|
17
|
+
panelId: string;
|
|
18
|
+
} | {
|
|
19
|
+
surface: typeof Surface.Peek;
|
|
20
|
+
};
|
|
21
|
+
export type ApiFactory = {
|
|
22
|
+
for: (resource: ResourceRef, target: Target) => WorkspaceApi;
|
|
23
|
+
/** Zone to open into when nobody asks: the one the domain prefers. */
|
|
24
|
+
zoneFor: (spec: ViewSpec | null) => Zone;
|
|
25
|
+
/** The name the shell draws for `ref`: the one its view gave, else its `ViewSpec` title. */
|
|
26
|
+
titleOf: (ref: ResourceRef) => string;
|
|
27
|
+
/** Stops listening to the store. Call it when the workspace goes away. */
|
|
28
|
+
dispose: () => void;
|
|
29
|
+
};
|
|
30
|
+
export type ApiFactoryOptions = {
|
|
31
|
+
/** Gives the breadcrumb its titles and `open(ref, { to: "side" })` the zone a domain prefers. */
|
|
32
|
+
registry?: Registry;
|
|
33
|
+
/** Where `open(ref)` goes without a `to`. The page: what people expect of a click. */
|
|
34
|
+
defaultOpen?: Destination;
|
|
35
|
+
};
|
|
36
|
+
export declare function createApiFactory(store: WorkspaceStore, { registry, defaultOpen }?: ApiFactoryOptions): ApiFactory;
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import * as model from "../domain/index.js";
|
|
2
|
+
import { Destination, formatRef, PAGE, PEEK, Surface, Zone, } from "../domain/index.js";
|
|
3
|
+
/** The id the workspace knows the surface by: `PAGE`, `PEEK` or the panel id. */
|
|
4
|
+
const surfaceId = (target) => target.surface === Surface.Panel
|
|
5
|
+
? target.panelId
|
|
6
|
+
: target.surface === Surface.Peek
|
|
7
|
+
? PEEK
|
|
8
|
+
: PAGE;
|
|
9
|
+
export function createApiFactory(store, { registry, defaultOpen = Destination.Page } = {}) {
|
|
10
|
+
const cache = new Map();
|
|
11
|
+
const choosers = new Map();
|
|
12
|
+
const zoneFor = (spec) => spec?.preferredZone ?? Zone.Right;
|
|
13
|
+
const titleOf = (ref) => model.nameOf(store.get(), ref) ?? registry?.resolve(ref)?.title(ref) ?? formatRef(ref);
|
|
14
|
+
/* A chooser whose surface is gone is forgotten, and so is a cached API of a surface that no
|
|
15
|
+
longer exists — or every panel ever opened would stay in memory. */
|
|
16
|
+
const stop = store.subscribe(() => {
|
|
17
|
+
const w = store.get();
|
|
18
|
+
for (const id of choosers.keys())
|
|
19
|
+
if (model.refOn(w, id) === null)
|
|
20
|
+
choosers.delete(id);
|
|
21
|
+
for (const key of cache.keys())
|
|
22
|
+
if (model.refOn(w, key) === null)
|
|
23
|
+
cache.delete(key);
|
|
24
|
+
});
|
|
25
|
+
const context = {
|
|
26
|
+
focused: () => model.focusedRef(store.get()),
|
|
27
|
+
selection: () => model.selectionOf(store.get()),
|
|
28
|
+
subscribe: store.subscribe,
|
|
29
|
+
};
|
|
30
|
+
function build(resource, target) {
|
|
31
|
+
const me = surfaceId(target);
|
|
32
|
+
const inPeek = target.surface === Surface.Peek;
|
|
33
|
+
/** Still showing what this API was lent for. */
|
|
34
|
+
const here = () => {
|
|
35
|
+
const now = model.refOn(store.get(), me);
|
|
36
|
+
return now !== null && model.sameRef(now, resource);
|
|
37
|
+
};
|
|
38
|
+
function open(ref, to) {
|
|
39
|
+
if (to === Destination.Peek) {
|
|
40
|
+
if (inPeek)
|
|
41
|
+
store.drillPeek(ref);
|
|
42
|
+
else
|
|
43
|
+
store.peek(ref);
|
|
44
|
+
}
|
|
45
|
+
else if (to === Destination.Window)
|
|
46
|
+
store.openPanel(ref, Zone.Floating);
|
|
47
|
+
else if (to === Destination.Side)
|
|
48
|
+
store.openPanel(ref, zoneFor(registry?.resolve(ref) ?? null));
|
|
49
|
+
else
|
|
50
|
+
store.push(ref);
|
|
51
|
+
}
|
|
52
|
+
function replace(ref) {
|
|
53
|
+
if (target.surface === Surface.Panel)
|
|
54
|
+
store.navigatePanel(target.panelId, ref);
|
|
55
|
+
else if (inPeek)
|
|
56
|
+
store.replacePeek(ref);
|
|
57
|
+
else
|
|
58
|
+
store.replace(ref);
|
|
59
|
+
}
|
|
60
|
+
/** In the peek the page's crumbs leave the drawer — a crumb is the page — and its own steps
|
|
61
|
+
* stay in it. */
|
|
62
|
+
function crumbs() {
|
|
63
|
+
const w = store.get();
|
|
64
|
+
const page = w.page ?? resource;
|
|
65
|
+
const pageCrumbs = w.trail.map((past, i) => ({
|
|
66
|
+
label: titleOf(past),
|
|
67
|
+
onClick: () => store.goTo(i),
|
|
68
|
+
}));
|
|
69
|
+
if (!inPeek)
|
|
70
|
+
return [...pageCrumbs, { label: titleOf(page) }];
|
|
71
|
+
return [
|
|
72
|
+
...pageCrumbs,
|
|
73
|
+
{ label: titleOf(page), onClick: store.closePeek },
|
|
74
|
+
...w.peekTrail.map((past, i) => ({
|
|
75
|
+
label: titleOf(past),
|
|
76
|
+
onClick: () => store.peekGoTo(i),
|
|
77
|
+
})),
|
|
78
|
+
{ label: titleOf(w.peek ?? resource) },
|
|
79
|
+
];
|
|
80
|
+
}
|
|
81
|
+
return {
|
|
82
|
+
self: resource,
|
|
83
|
+
surface: target.surface,
|
|
84
|
+
context,
|
|
85
|
+
open: (ref, options) => open(ref, options?.to ??
|
|
86
|
+
(inPeek && defaultOpen === Destination.Page ? Destination.Peek : defaultOpen)),
|
|
87
|
+
navigate: (to) => {
|
|
88
|
+
if (target.surface === Surface.Panel)
|
|
89
|
+
store.navigatePanel(target.panelId, to);
|
|
90
|
+
else if (inPeek)
|
|
91
|
+
store.drillPeek(to);
|
|
92
|
+
else
|
|
93
|
+
store.push(to);
|
|
94
|
+
},
|
|
95
|
+
replace,
|
|
96
|
+
setParams: (params) => {
|
|
97
|
+
const now = model.refOn(store.get(), me);
|
|
98
|
+
if (now === null || !model.sameRef(model.rootRef(now), model.rootRef(resource)))
|
|
99
|
+
return;
|
|
100
|
+
replace(model.withParams(now, params));
|
|
101
|
+
},
|
|
102
|
+
back: () => {
|
|
103
|
+
if (target.surface === Surface.Page)
|
|
104
|
+
store.back();
|
|
105
|
+
else if (inPeek)
|
|
106
|
+
store.peekBack();
|
|
107
|
+
},
|
|
108
|
+
expand: () => {
|
|
109
|
+
if (target.surface === Surface.Panel)
|
|
110
|
+
store.expandPanel(target.panelId);
|
|
111
|
+
else if (inPeek)
|
|
112
|
+
store.peekToPage();
|
|
113
|
+
},
|
|
114
|
+
close: () => {
|
|
115
|
+
if (target.surface === Surface.Panel)
|
|
116
|
+
store.closePanel(target.panelId);
|
|
117
|
+
else if (inPeek)
|
|
118
|
+
store.closePeek();
|
|
119
|
+
else
|
|
120
|
+
store.back();
|
|
121
|
+
},
|
|
122
|
+
focus: () => store.focusPanel(me),
|
|
123
|
+
crumbs: () => (target.surface === Surface.Panel ? [] : crumbs()),
|
|
124
|
+
setTitle: (title) => {
|
|
125
|
+
if (here())
|
|
126
|
+
store.setTitle(resource, title);
|
|
127
|
+
},
|
|
128
|
+
setDirty: (dirty) => store.setDirty(me, resource, dirty),
|
|
129
|
+
select: (refs) => store.apply((w) => model.select(w, me, refs)),
|
|
130
|
+
pick: (ref, onPick, options) => {
|
|
131
|
+
const to = options?.to ?? Destination.Window;
|
|
132
|
+
if (to === Destination.Peek) {
|
|
133
|
+
store.peek(ref);
|
|
134
|
+
choosers.set(PEEK, onPick);
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
open(ref, to);
|
|
138
|
+
const id = store.get().focus;
|
|
139
|
+
if (id !== PAGE && id !== PEEK)
|
|
140
|
+
choosers.set(id, onPick);
|
|
141
|
+
},
|
|
142
|
+
answer: (value) => {
|
|
143
|
+
const onPick = choosers.get(me);
|
|
144
|
+
if (onPick === undefined)
|
|
145
|
+
return;
|
|
146
|
+
/* Forget it BEFORE closing, so closing is not mistaken for anything else. */
|
|
147
|
+
choosers.delete(me);
|
|
148
|
+
onPick(value);
|
|
149
|
+
if (inPeek)
|
|
150
|
+
store.closePeek();
|
|
151
|
+
else if (target.surface === Surface.Panel)
|
|
152
|
+
store.closePanel(target.panelId);
|
|
153
|
+
},
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
return {
|
|
157
|
+
for: (resource, target) => {
|
|
158
|
+
const key = surfaceId(target);
|
|
159
|
+
const cached = cache.get(key);
|
|
160
|
+
if (cached !== undefined && model.sameRef(cached.resource, resource))
|
|
161
|
+
return cached.api;
|
|
162
|
+
const api = build(resource, target);
|
|
163
|
+
cache.set(key, { resource, api });
|
|
164
|
+
return api;
|
|
165
|
+
},
|
|
166
|
+
zoneFor,
|
|
167
|
+
titleOf,
|
|
168
|
+
dispose: stop,
|
|
169
|
+
};
|
|
170
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { Zone } from "../domain/index.js";
|
|
2
|
+
import { type Dock, DragKind, type ResourceRef } from "../domain/index.js";
|
|
3
|
+
import { type RefObject } from "react";
|
|
4
|
+
export type DragData = {
|
|
5
|
+
kind: typeof DragKind.Panel;
|
|
6
|
+
panelId: string;
|
|
7
|
+
} | {
|
|
8
|
+
kind: typeof DragKind.Ref;
|
|
9
|
+
ref: ResourceRef;
|
|
10
|
+
};
|
|
11
|
+
export type DropData = {
|
|
12
|
+
kind: typeof DragKind.Zone;
|
|
13
|
+
zone: Zone;
|
|
14
|
+
};
|
|
15
|
+
/** Only a DOCKED panel is dragged to another edge. A window moves with the pointer, and a native
|
|
16
|
+
* drag on the same header would swallow that gesture — `enabled` keeps the two apart. */
|
|
17
|
+
export declare function useDraggablePanel(ref: RefObject<HTMLElement | null>, panelId: string, enabled?: boolean): boolean;
|
|
18
|
+
export declare function useDropZone(ref: RefObject<HTMLElement | null>, zone: Dock): boolean;
|
|
19
|
+
/** Makes an element drag a REFERENCE — a row, a chip — that a zone turns into a panel. */
|
|
20
|
+
export declare function useDraggableRef(ref: RefObject<HTMLElement | null>, resource: ResourceRef): boolean;
|
|
21
|
+
/** True while a panel or a reference is being dragged. Drop targets must only steal
|
|
22
|
+
* pointer events then — otherwise they sit on top of “beside” / “floating”
|
|
23
|
+
* and the buttons never fire. */
|
|
24
|
+
export declare function usePanelDragArmed(): boolean;
|
|
25
|
+
export type DropHandlers = {
|
|
26
|
+
onPanelToZone: (panelId: string, zone: Zone) => void;
|
|
27
|
+
onRefToZone: (ref: ResourceRef, zone: Zone) => void;
|
|
28
|
+
};
|
|
29
|
+
/** One monitor for the whole workspace: drop is ONE event with ONE result.
|
|
30
|
+
* Split across targets, an overlapping zone and tab would both apply. */
|
|
31
|
+
export declare function useDropMonitor({ onPanelToZone, onRefToZone }: DropHandlers): void;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { draggable, dropTargetForElements, monitorForElements, } from "@atlaskit/pragmatic-drag-and-drop/element/adapter";
|
|
2
|
+
import { DragKind } from "../domain/index.js";
|
|
3
|
+
import { useEffect, useState } from "react";
|
|
4
|
+
const isDrag = (v) => v["kind"] === DragKind.Panel || v["kind"] === DragKind.Ref;
|
|
5
|
+
/** Only a DOCKED panel is dragged to another edge. A window moves with the pointer, and a native
|
|
6
|
+
* drag on the same header would swallow that gesture — `enabled` keeps the two apart. */
|
|
7
|
+
export function useDraggablePanel(ref, panelId, enabled = true) {
|
|
8
|
+
const [dragging, setDragging] = useState(false);
|
|
9
|
+
useEffect(() => {
|
|
10
|
+
const el = ref.current;
|
|
11
|
+
if (el === null || !enabled)
|
|
12
|
+
return;
|
|
13
|
+
return draggable({
|
|
14
|
+
element: el,
|
|
15
|
+
getInitialData: () => ({
|
|
16
|
+
kind: DragKind.Panel,
|
|
17
|
+
panelId,
|
|
18
|
+
}),
|
|
19
|
+
onDragStart: () => setDragging(true),
|
|
20
|
+
onDrop: () => setDragging(false),
|
|
21
|
+
});
|
|
22
|
+
}, [ref, panelId, enabled]);
|
|
23
|
+
return dragging;
|
|
24
|
+
}
|
|
25
|
+
export function useDropZone(ref, zone) {
|
|
26
|
+
const [over, setOver] = useState(false);
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
const el = ref.current;
|
|
29
|
+
if (el === null)
|
|
30
|
+
return;
|
|
31
|
+
return dropTargetForElements({
|
|
32
|
+
element: el,
|
|
33
|
+
getData: () => ({ kind: DragKind.Zone, zone }),
|
|
34
|
+
canDrop: ({ source }) => isDrag(source.data) &&
|
|
35
|
+
(source.data.kind === DragKind.Panel || source.data.kind === DragKind.Ref),
|
|
36
|
+
onDragEnter: () => setOver(true),
|
|
37
|
+
onDragLeave: () => setOver(false),
|
|
38
|
+
onDrop: () => setOver(false),
|
|
39
|
+
});
|
|
40
|
+
}, [ref, zone]);
|
|
41
|
+
return over;
|
|
42
|
+
}
|
|
43
|
+
/** Makes an element drag a REFERENCE — a row, a chip — that a zone turns into a panel. */
|
|
44
|
+
export function useDraggableRef(ref, resource) {
|
|
45
|
+
const [dragging, setDragging] = useState(false);
|
|
46
|
+
useEffect(() => {
|
|
47
|
+
const el = ref.current;
|
|
48
|
+
if (el === null)
|
|
49
|
+
return;
|
|
50
|
+
return draggable({
|
|
51
|
+
element: el,
|
|
52
|
+
getInitialData: () => ({
|
|
53
|
+
kind: DragKind.Ref,
|
|
54
|
+
ref: resource,
|
|
55
|
+
}),
|
|
56
|
+
onDragStart: () => setDragging(true),
|
|
57
|
+
onDrop: () => setDragging(false),
|
|
58
|
+
});
|
|
59
|
+
}, [ref, resource]);
|
|
60
|
+
return dragging;
|
|
61
|
+
}
|
|
62
|
+
/** True while a panel or a reference is being dragged. Drop targets must only steal
|
|
63
|
+
* pointer events then — otherwise they sit on top of “beside” / “floating”
|
|
64
|
+
* and the buttons never fire. */
|
|
65
|
+
export function usePanelDragArmed() {
|
|
66
|
+
const [armed, setArmed] = useState(false);
|
|
67
|
+
useEffect(() => monitorForElements({
|
|
68
|
+
canMonitor: ({ source }) => isDrag(source.data) &&
|
|
69
|
+
(source.data.kind === DragKind.Panel || source.data.kind === DragKind.Ref),
|
|
70
|
+
onDragStart: () => setArmed(true),
|
|
71
|
+
onDrop: () => setArmed(false),
|
|
72
|
+
}), []);
|
|
73
|
+
return armed;
|
|
74
|
+
}
|
|
75
|
+
/** One monitor for the whole workspace: drop is ONE event with ONE result.
|
|
76
|
+
* Split across targets, an overlapping zone and tab would both apply. */
|
|
77
|
+
export function useDropMonitor({ onPanelToZone, onRefToZone }) {
|
|
78
|
+
useEffect(() => monitorForElements({
|
|
79
|
+
canMonitor: ({ source }) => isDrag(source.data),
|
|
80
|
+
onDrop: ({ source, location }) => {
|
|
81
|
+
const target = location.current.dropTargets[0];
|
|
82
|
+
if (target === undefined || !isDrag(source.data))
|
|
83
|
+
return;
|
|
84
|
+
const data = target.data;
|
|
85
|
+
if (source.data.kind === DragKind.Panel && data.kind === DragKind.Zone) {
|
|
86
|
+
onPanelToZone(source.data.panelId, data.zone);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if (source.data.kind === DragKind.Ref && data.kind === DragKind.Zone) {
|
|
90
|
+
onRefToZone(source.data.ref, data.zone);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
}), [onPanelToZone, onRefToZone]);
|
|
95
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { Dock, OpenPanelOptions, ResourceRef, WindowBox, Workspace, Zone } from "../domain/index.js";
|
|
2
|
+
/**
|
|
3
|
+
* The pure reducers already are the state handling; this only binds them to the browser. An
|
|
4
|
+
* external store, not `useState`: with state in the component every action recreated the API
|
|
5
|
+
* views receive, and every panel redrew on any change.
|
|
6
|
+
*
|
|
7
|
+
* Two things live here and not in the model, because they are about the browser and not about
|
|
8
|
+
* the workspace: the history entries, and what a view has typed and not saved.
|
|
9
|
+
*/
|
|
10
|
+
export type Unsubscribe = () => void;
|
|
11
|
+
/** Asked before throwing away views with unsaved changes; `lost` is what they show. */
|
|
12
|
+
export type ConfirmDiscard = (lost: readonly ResourceRef[]) => boolean | Promise<boolean>;
|
|
13
|
+
export type WorkspaceStore = {
|
|
14
|
+
/** Current state. Read at call time, never captured. */
|
|
15
|
+
get: () => Workspace;
|
|
16
|
+
subscribe: (listener: () => void) => Unsubscribe;
|
|
17
|
+
/** Escape hatch: any reducer, including one of your own. */
|
|
18
|
+
apply: (change: (state: Workspace) => Workspace) => void;
|
|
19
|
+
/** Back to the starting workspace — the `initial` it was created with. */
|
|
20
|
+
reset: () => void;
|
|
21
|
+
/** Start over from `view`, the way a menu does: no breadcrumb behind it, no peek. The panels
|
|
22
|
+
* stay. */
|
|
23
|
+
start: (view: ResourceRef) => void;
|
|
24
|
+
push: (view: ResourceRef) => void;
|
|
25
|
+
/** The page shows `view` with no crumb and no browser history entry. */
|
|
26
|
+
replace: (view: ResourceRef) => void;
|
|
27
|
+
goTo: (index: number) => void;
|
|
28
|
+
back: () => void;
|
|
29
|
+
openPanel: (view: ResourceRef, options?: Zone | OpenPanelOptions) => void;
|
|
30
|
+
navigatePanel: (id: string, view: ResourceRef) => void;
|
|
31
|
+
movePanel: (id: string, zone: Zone) => void;
|
|
32
|
+
resizeZone: (zone: Dock, size: number) => void;
|
|
33
|
+
resizePanel: (id: string, share: number) => void;
|
|
34
|
+
floatPanel: (id: string, box: WindowBox) => void;
|
|
35
|
+
raisePanel: (id: string) => void;
|
|
36
|
+
collapsePanel: (id: string) => void;
|
|
37
|
+
closePanel: (id: string) => void;
|
|
38
|
+
expandPanel: (id: string) => void;
|
|
39
|
+
maximize: (target: string | null) => void;
|
|
40
|
+
focusPanel: (target: string) => void;
|
|
41
|
+
cycleFocus: (delta: number) => void;
|
|
42
|
+
peek: (view: ResourceRef) => void;
|
|
43
|
+
drillPeek: (view: ResourceRef) => void;
|
|
44
|
+
replacePeek: (view: ResourceRef) => void;
|
|
45
|
+
peekGoTo: (index: number) => void;
|
|
46
|
+
peekBack: () => void;
|
|
47
|
+
closePeek: () => void;
|
|
48
|
+
peekToPage: () => void;
|
|
49
|
+
peekToSide: (zone?: Zone) => void;
|
|
50
|
+
/** The name a view gives `ref` (see `api.setTitle`). */
|
|
51
|
+
setTitle: (ref: ResourceRef, title: string | null) => void;
|
|
52
|
+
/** `surface` (`PAGE`, `PEEK` or a panel id) has unsaved changes for `ref`. Ignored if it no
|
|
53
|
+
* longer shows it. */
|
|
54
|
+
setDirty: (surface: string, ref: ResourceRef, dirty: boolean) => void;
|
|
55
|
+
/** Where `<Composer>` installs its own dialog. A `confirmDiscard` given to the store wins. */
|
|
56
|
+
setConfirmDiscard: (confirm: ConfirmDiscard | null) => void;
|
|
57
|
+
};
|
|
58
|
+
export type StoreOptions = {
|
|
59
|
+
initial?: Workspace;
|
|
60
|
+
/** Persist the workspace in the URL hash. The browser's Back and «share this link» come
|
|
61
|
+
* from here. */
|
|
62
|
+
hash?: boolean;
|
|
63
|
+
/** Called after every change. Hook for telemetry or saving the user's workspace. */
|
|
64
|
+
onChange?: (state: Workspace) => void;
|
|
65
|
+
/** Asked before a change throws away a view with unsaved changes. Without it, `<Composer>`
|
|
66
|
+
* asks with its own dialog; with neither, nothing is asked. */
|
|
67
|
+
confirmDiscard?: ConfirmDiscard;
|
|
68
|
+
};
|
|
69
|
+
export declare function createWorkspaceStore({ initial, hash, onChange, confirmDiscard, }?: StoreOptions): WorkspaceStore;
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import * as model from "../domain/index.js";
|
|
2
|
+
const inBrowser = () => typeof window !== "undefined";
|
|
3
|
+
/** A history entry records a new PLACE — another page or another peek. Sizes, focus and
|
|
4
|
+
* selection are corrections of the same place: they replace the entry. */
|
|
5
|
+
const History = { Auto: "auto", Replace: "replace" };
|
|
6
|
+
const placeOf = (w) => `${w.page === null ? "" : model.formatRef(w.page)} ${w.peek === null ? "" : model.formatRef(w.peek)}`;
|
|
7
|
+
const memoryOf = (w) => ({
|
|
8
|
+
trail: w.trail,
|
|
9
|
+
peekTrail: w.peekTrail,
|
|
10
|
+
});
|
|
11
|
+
function isMemory(value) {
|
|
12
|
+
const m = value;
|
|
13
|
+
return typeof m === "object" && m !== null && Array.isArray(m.trail);
|
|
14
|
+
}
|
|
15
|
+
function readHash(fallback) {
|
|
16
|
+
if (!inBrowser() || !window.location.hash.startsWith("#/w"))
|
|
17
|
+
return fallback;
|
|
18
|
+
return model.fromHash(window.location.hash);
|
|
19
|
+
}
|
|
20
|
+
const onBeforeUnload = (e) => {
|
|
21
|
+
e.preventDefault();
|
|
22
|
+
e.returnValue = "";
|
|
23
|
+
};
|
|
24
|
+
export function createWorkspaceStore({ initial = model.EMPTY, hash = false, onChange, confirmDiscard, } = {}) {
|
|
25
|
+
let state = hash ? readHash(initial) : initial;
|
|
26
|
+
const listeners = new Set();
|
|
27
|
+
/** Surface → the resource it has unsaved changes for. */
|
|
28
|
+
const dirty = new Map();
|
|
29
|
+
let installed = null;
|
|
30
|
+
let guarding = false;
|
|
31
|
+
/** A discard being asked asynchronously: every change after it waits in line, so
|
|
32
|
+
* `back(); open(ref)` keeps its order. */
|
|
33
|
+
let line = null;
|
|
34
|
+
const notify = () => {
|
|
35
|
+
for (const l of listeners)
|
|
36
|
+
l();
|
|
37
|
+
onChange?.(state);
|
|
38
|
+
};
|
|
39
|
+
/** The browser asks before closing the tab only while something is unsaved. */
|
|
40
|
+
function guardUnload() {
|
|
41
|
+
const want = dirty.size > 0;
|
|
42
|
+
if (want === guarding || !inBrowser())
|
|
43
|
+
return;
|
|
44
|
+
guarding = want;
|
|
45
|
+
if (want)
|
|
46
|
+
window.addEventListener("beforeunload", onBeforeUnload);
|
|
47
|
+
else
|
|
48
|
+
window.removeEventListener("beforeunload", onBeforeUnload);
|
|
49
|
+
}
|
|
50
|
+
/** What `next` would throw away: a dirty surface gone, or showing another resource. Another
|
|
51
|
+
* face of the same one (only `params`) keeps the view mounted, and with it what was typed. */
|
|
52
|
+
function lostIn(next) {
|
|
53
|
+
return [...dirty].filter(([surface, ref]) => {
|
|
54
|
+
const after = model.refOn(next, surface);
|
|
55
|
+
return after === null || !model.sameRef(model.rootRef(after), model.rootRef(ref));
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
/** `true`, `false`, or a promise of either; a confirm that throws is a «no». */
|
|
59
|
+
function ask(lost) {
|
|
60
|
+
const confirm = confirmDiscard ?? installed;
|
|
61
|
+
if (confirm === null)
|
|
62
|
+
return true;
|
|
63
|
+
try {
|
|
64
|
+
const answer = confirm(lost.map(([, ref]) => ref));
|
|
65
|
+
return answer instanceof Promise ? answer.catch(() => false) : answer;
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/** Only a new place is a new entry; the memory of the trails rides along in each one. */
|
|
72
|
+
function writeUrl(before, mode) {
|
|
73
|
+
if (!hash || !inBrowser())
|
|
74
|
+
return;
|
|
75
|
+
const text = model.toHash(state);
|
|
76
|
+
if (mode === History.Auto && placeOf(before) !== placeOf(state))
|
|
77
|
+
window.history.pushState(memoryOf(state), "", text);
|
|
78
|
+
else
|
|
79
|
+
window.history.replaceState(memoryOf(state), "", text);
|
|
80
|
+
}
|
|
81
|
+
function commit(next, mode) {
|
|
82
|
+
const before = state;
|
|
83
|
+
state = next;
|
|
84
|
+
writeUrl(before, mode);
|
|
85
|
+
notify();
|
|
86
|
+
}
|
|
87
|
+
/** Runs `change`, asking first if it would throw away something unsaved. After a «yes» it
|
|
88
|
+
* runs again on the state of THEN — the answer may have taken a while. */
|
|
89
|
+
function run(change, mode) {
|
|
90
|
+
const next = change(state);
|
|
91
|
+
if (next === state)
|
|
92
|
+
return;
|
|
93
|
+
const lost = lostIn(next);
|
|
94
|
+
if (lost.length === 0)
|
|
95
|
+
return commit(next, mode);
|
|
96
|
+
const proceed = (ok) => {
|
|
97
|
+
if (!ok)
|
|
98
|
+
return;
|
|
99
|
+
for (const [surface] of lost)
|
|
100
|
+
dirty.delete(surface);
|
|
101
|
+
guardUnload();
|
|
102
|
+
return run(change, mode);
|
|
103
|
+
};
|
|
104
|
+
const answer = ask(lost);
|
|
105
|
+
return answer instanceof Promise ? answer.then(proceed) : proceed(answer);
|
|
106
|
+
}
|
|
107
|
+
function go(change, mode = History.Auto) {
|
|
108
|
+
const result = line === null ? run(change, mode) : line.then(() => run(change, mode));
|
|
109
|
+
if (!(result instanceof Promise))
|
|
110
|
+
return;
|
|
111
|
+
line = result;
|
|
112
|
+
void result.finally(() => {
|
|
113
|
+
if (line === result)
|
|
114
|
+
line = null;
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Back or forward: the browser already moved, so this cannot be cancelled — only undone. If
|
|
119
|
+
* the person keeps the unsaved changes, the place they had is pushed again (which costs the
|
|
120
|
+
* forward entries; there is no other way to stay).
|
|
121
|
+
*/
|
|
122
|
+
function onPopState(e) {
|
|
123
|
+
const target = model.restore(state, readHash(initial), isMemory(e.state) ? e.state : null);
|
|
124
|
+
if (model.toHash(target) === model.toHash(state))
|
|
125
|
+
return;
|
|
126
|
+
const settle = (ok) => {
|
|
127
|
+
if (!ok) {
|
|
128
|
+
window.history.pushState(memoryOf(state), "", model.toHash(state));
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
for (const [surface] of lostIn(target))
|
|
132
|
+
dirty.delete(surface);
|
|
133
|
+
guardUnload();
|
|
134
|
+
state = target;
|
|
135
|
+
notify();
|
|
136
|
+
};
|
|
137
|
+
const lost = lostIn(target);
|
|
138
|
+
const answer = lost.length === 0 ? true : ask(lost);
|
|
139
|
+
if (answer instanceof Promise)
|
|
140
|
+
void answer.then(settle);
|
|
141
|
+
else
|
|
142
|
+
settle(answer);
|
|
143
|
+
}
|
|
144
|
+
const subscribe = (listener) => {
|
|
145
|
+
listeners.add(listener);
|
|
146
|
+
if (listeners.size === 1 && hash && inBrowser())
|
|
147
|
+
window.addEventListener("popstate", onPopState);
|
|
148
|
+
return () => {
|
|
149
|
+
listeners.delete(listener);
|
|
150
|
+
if (listeners.size === 0 && hash && inBrowser())
|
|
151
|
+
window.removeEventListener("popstate", onPopState);
|
|
152
|
+
};
|
|
153
|
+
};
|
|
154
|
+
return {
|
|
155
|
+
get: () => state,
|
|
156
|
+
subscribe,
|
|
157
|
+
apply: (change) => go(change),
|
|
158
|
+
reset: () => go(() => initial),
|
|
159
|
+
start: (view) => go((w) => model.start(w, view)),
|
|
160
|
+
push: (view) => go((w) => model.push(w, view)),
|
|
161
|
+
replace: (view) => go((w) => model.replace(w, view), History.Replace),
|
|
162
|
+
goTo: (index) => go((w) => model.goTo(w, index)),
|
|
163
|
+
back: () => go(model.back),
|
|
164
|
+
openPanel: (view, options) => go((w) => model.openPanel(w, view, options)),
|
|
165
|
+
navigatePanel: (id, view) => go((w) => model.navigatePanel(w, id, view)),
|
|
166
|
+
movePanel: (id, zone) => go((w) => model.movePanel(w, id, zone)),
|
|
167
|
+
resizeZone: (zone, size) => go((w) => model.resizeZone(w, zone, size)),
|
|
168
|
+
resizePanel: (id, share) => go((w) => model.resizePanel(w, id, share)),
|
|
169
|
+
floatPanel: (id, box) => go((w) => model.floatPanel(w, id, box)),
|
|
170
|
+
raisePanel: (id) => go((w) => model.raisePanel(w, id)),
|
|
171
|
+
collapsePanel: (id) => go((w) => model.collapsePanel(w, id)),
|
|
172
|
+
closePanel: (id) => go((w) => model.closePanel(w, id)),
|
|
173
|
+
expandPanel: (id) => go((w) => model.expandPanel(w, id)),
|
|
174
|
+
maximize: (target) => go((w) => model.maximize(w, target)),
|
|
175
|
+
focusPanel: (target) => go((w) => model.focusPanel(w, target)),
|
|
176
|
+
cycleFocus: (delta) => go((w) => model.cycleFocus(w, delta)),
|
|
177
|
+
peek: (view) => go((w) => model.peek(w, view)),
|
|
178
|
+
drillPeek: (view) => go((w) => model.drillPeek(w, view)),
|
|
179
|
+
replacePeek: (view) => go((w) => model.replacePeek(w, view), History.Replace),
|
|
180
|
+
peekGoTo: (index) => go((w) => model.peekGoTo(w, index)),
|
|
181
|
+
peekBack: () => go(model.peekBack),
|
|
182
|
+
closePeek: () => go(model.closePeek),
|
|
183
|
+
peekToPage: () => go(model.peekToPage),
|
|
184
|
+
peekToSide: (zone) => go((w) => model.peekToSide(w, zone)),
|
|
185
|
+
setTitle: (ref, title) => go((w) => model.nameRef(w, ref, title), History.Replace),
|
|
186
|
+
setDirty: (surface, ref, on) => {
|
|
187
|
+
const shown = model.refOn(state, surface);
|
|
188
|
+
if (shown === null || !model.sameRef(model.rootRef(shown), model.rootRef(ref)))
|
|
189
|
+
return;
|
|
190
|
+
if (on)
|
|
191
|
+
dirty.set(surface, ref);
|
|
192
|
+
else
|
|
193
|
+
dirty.delete(surface);
|
|
194
|
+
guardUnload();
|
|
195
|
+
},
|
|
196
|
+
setConfirmDiscard: (confirm) => {
|
|
197
|
+
installed = confirm;
|
|
198
|
+
},
|
|
199
|
+
};
|
|
200
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { WorkspaceStore } from "./store.js";
|
|
2
|
+
/**
|
|
3
|
+
* Shortcuts talk to the WORKSPACE, not to the browser-focused panel: that focus lives INSIDE the
|
|
4
|
+
* view — almost always in a text field — and the keys belong to it there.
|
|
5
|
+
*
|
|
6
|
+
* Escape is shared by everything on the page: a menu, a search field, a cell being edited. The
|
|
7
|
+
* contract is that whoever consumes it CLAIMS it (`preventDefault`), and the workspace only
|
|
8
|
+
* takes an Escape nobody claimed — otherwise closing a menu inside the peek closed the peek too.
|
|
9
|
+
*/
|
|
10
|
+
export type UseKeyboardOptions = {
|
|
11
|
+
workspace: WorkspaceStore;
|
|
12
|
+
enabled?: boolean;
|
|
13
|
+
/** ⌘K. Works while typing too: the palette is where you go FROM a field. */
|
|
14
|
+
onPalette?: () => void;
|
|
15
|
+
};
|
|
16
|
+
export declare function useKeyboard({ workspace, enabled, onPalette }: UseKeyboardOptions): void;
|