@sincpro/web-composer 0.1.0 → 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.
@@ -1,76 +1,200 @@
1
1
  import * as model from "../domain/index.js";
2
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
+ }
3
15
  function readHash(fallback) {
4
16
  if (!inBrowser() || !window.location.hash.startsWith("#/w"))
5
17
  return fallback;
6
18
  return model.fromHash(window.location.hash);
7
19
  }
8
- export function createWorkspaceStore({ initial = model.EMPTY, hash = false, onChange, } = {}) {
20
+ const onBeforeUnload = (e) => {
21
+ e.preventDefault();
22
+ e.returnValue = "";
23
+ };
24
+ export function createWorkspaceStore({ initial = model.EMPTY, hash = false, onChange, confirmDiscard, } = {}) {
9
25
  let state = hash ? readHash(initial) : initial;
10
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;
11
34
  const notify = () => {
12
35
  for (const l of listeners)
13
36
  l();
14
37
  onChange?.(state);
15
38
  };
16
- /** Compare the text before writing; otherwise writing the hash fires `hashchange` and the
17
- * loop never ends. */
18
- const writeUrl = () => {
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) {
19
73
  if (!hash || !inBrowser())
20
74
  return;
21
75
  const text = model.toHash(state);
22
- if (window.location.hash !== text)
23
- window.history.replaceState(null, "", text);
24
- };
25
- const apply = (change) => {
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) {
26
90
  const next = change(state);
27
91
  if (next === state)
28
92
  return;
29
- state = next;
30
- writeUrl();
31
- notify();
32
- };
33
- function onHashChange() {
34
- const fromUrl = readHash(state);
35
- if (model.toHash(fromUrl) === model.toHash(state))
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))
36
110
  return;
37
- state = fromUrl;
38
- notify();
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);
39
143
  }
40
144
  const subscribe = (listener) => {
41
145
  listeners.add(listener);
42
146
  if (listeners.size === 1 && hash && inBrowser())
43
- window.addEventListener("hashchange", onHashChange);
147
+ window.addEventListener("popstate", onPopState);
44
148
  return () => {
45
149
  listeners.delete(listener);
46
150
  if (listeners.size === 0 && hash && inBrowser())
47
- window.removeEventListener("hashchange", onHashChange);
151
+ window.removeEventListener("popstate", onPopState);
48
152
  };
49
153
  };
50
154
  return {
51
155
  get: () => state,
52
156
  subscribe,
53
- apply,
54
- reset: () => apply(() => initial),
55
- push: (view) => apply((w) => model.push(w, view)),
56
- goTo: (index) => apply((w) => model.goTo(w, index)),
57
- back: () => apply(model.back),
58
- openPanel: (view, options) => apply((w) => model.openPanel(w, view, options)),
59
- navigatePanel: (id, view) => apply((w) => model.navigatePanel(w, id, view)),
60
- movePanel: (id, zone) => apply((w) => model.movePanel(w, id, zone)),
61
- resizeZone: (zone, size) => apply((w) => model.resizeZone(w, zone, size)),
62
- resizePanel: (id, share) => apply((w) => model.resizePanel(w, id, share)),
63
- floatPanel: (id, box) => apply((w) => model.floatPanel(w, id, box)),
64
- raisePanel: (id) => apply((w) => model.raisePanel(w, id)),
65
- collapsePanel: (id) => apply((w) => model.collapsePanel(w, id)),
66
- closePanel: (id) => apply((w) => model.closePanel(w, id)),
67
- expandPanel: (id) => apply((w) => model.expandPanel(w, id)),
68
- maximize: (target) => apply((w) => model.maximize(w, target)),
69
- focusPanel: (target) => apply((w) => model.focusPanel(w, target)),
70
- cycleFocus: (delta) => apply((w) => model.cycleFocus(w, delta)),
71
- peek: (view) => apply((w) => model.peek(w, view)),
72
- closePeek: () => apply(model.closePeek),
73
- peekToPage: () => apply(model.peekToPage),
74
- peekToSide: (zone) => apply((w) => model.peekToSide(w, zone)),
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
+ },
75
199
  };
76
200
  }
@@ -2,6 +2,10 @@ import type { WorkspaceStore } from "./store.js";
2
2
  /**
3
3
  * Shortcuts talk to the WORKSPACE, not to the browser-focused panel: that focus lives INSIDE the
4
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.
5
9
  */
6
10
  export type UseKeyboardOptions = {
7
11
  workspace: WorkspaceStore;
@@ -1,4 +1,4 @@
1
- import { PAGE } from "../domain/index.js";
1
+ import { PAGE, PEEK } from "../domain/index.js";
2
2
  import { useEffect } from "react";
3
3
  /** Inside a field only Escape survives. Stealing a key from someone who is typing is worse than
4
4
  * having no shortcuts. */
@@ -14,6 +14,8 @@ export function useKeyboard({ workspace, enabled = true, onPalette }) {
14
14
  function onKeyDown(e) {
15
15
  const state = workspace.get();
16
16
  if (e.key === "Escape") {
17
+ if (e.defaultPrevented)
18
+ return;
17
19
  if (state.peek !== null) {
18
20
  workspace.closePeek();
19
21
  e.preventDefault();
@@ -33,7 +35,10 @@ export function useKeyboard({ workspace, enabled = true, onPalette }) {
33
35
  if (isTyping(e.target))
34
36
  return;
35
37
  if (e.altKey && !mod && e.code === "ArrowLeft") {
36
- workspace.back();
38
+ if (state.focus === PEEK && state.peekTrail.length > 0)
39
+ workspace.peekBack();
40
+ else
41
+ workspace.back();
37
42
  e.preventDefault();
38
43
  }
39
44
  else if (mod && e.altKey && (e.code === "BracketLeft" || e.code === "BracketRight")) {
@@ -45,7 +50,10 @@ export function useKeyboard({ workspace, enabled = true, onPalette }) {
45
50
  e.preventDefault();
46
51
  }
47
52
  else if (mod && e.shiftKey && e.code === "Backspace" && state.focus !== PAGE) {
48
- workspace.closePanel(state.focus);
53
+ if (state.focus === PEEK)
54
+ workspace.closePeek();
55
+ else
56
+ workspace.closePanel(state.focus);
49
57
  e.preventDefault();
50
58
  }
51
59
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sincpro/web-composer",
3
- "version": "0.1.0",
4
- "description": "The Sincpro multitasking workspace for React: tabs, panels, peek, global panels and ⌘K over references.",
3
+ "version": "1.0.0",
4
+ "description": "The Sincpro workspace for React: one page with its breadcrumb, a peek, side panels and windows, and ⌘K — all over references, never data.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",
@@ -27,16 +27,16 @@
27
27
  "printWidth": 94
28
28
  },
29
29
  "peerDependencies": {
30
- "@sincpro/web-ui": "*",
31
30
  "react": ">=19",
32
31
  "react-dom": ">=19"
33
32
  },
34
33
  "devDependencies": {
35
34
  "@eslint/js": "^10.0.1",
36
- "@sincpro/web-ui": "^0.1.0",
35
+ "@sincpro/web-ui": "^0.2.0",
37
36
  "@storybook/addon-a11y": "10.6.5",
38
37
  "@storybook/addon-docs": "10.6.5",
39
38
  "@storybook/addon-mcp": "10.6.5",
39
+ "@storybook/addon-vitest": "10.6.5",
40
40
  "@storybook/react-vite": "10.6.5",
41
41
  "@tailwindcss/vite": "4.3.3",
42
42
  "@tanstack/react-table": "^9.3.1",
@@ -45,26 +45,28 @@
45
45
  "@types/react": "^19.3.0",
46
46
  "@types/react-dom": "^19.3.0",
47
47
  "@vitejs/plugin-react": "6.1.2",
48
+ "@vitest/browser-playwright": "5.0.5",
48
49
  "eslint": "^10.14.0",
49
50
  "eslint-config-prettier": "^10.1.8",
50
51
  "eslint-plugin-react-hooks": "^7.1.1",
51
52
  "eslint-plugin-simple-import-sort": "^14.0.0",
53
+ "lucide-react": "^1.65.0",
54
+ "playwright": "1.64.1",
52
55
  "prettier": "^3.9.10",
53
- "puppeteer-core": "^25.16.0",
54
56
  "react": "19.3.1",
55
57
  "react-dom": "19.3.1",
56
58
  "storybook": "10.6.5",
57
59
  "tailwindcss": "4.3.3",
58
60
  "tsc-alias": "^1.9.9",
59
- "tsx": "^4.23.15",
60
61
  "typescript": "~6.0.3",
61
62
  "typescript-eslint": "^8.72.1",
62
- "vite": "8.3.5"
63
+ "vite": "8.3.5",
64
+ "vitest": "5.0.5"
63
65
  },
64
66
  "keywords": [
65
67
  "workspace",
66
68
  "react",
67
- "tabs",
69
+ "breadcrumb",
68
70
  "command-palette",
69
71
  "sincpro"
70
72
  ],
@@ -78,7 +80,6 @@
78
80
  "node": ">=26"
79
81
  },
80
82
  "dependencies": {
81
- "@atlaskit/pragmatic-drag-and-drop": "^4.0.0",
82
- "lucide-react": "^1.65.0"
83
+ "@atlaskit/pragmatic-drag-and-drop": "^4.0.0"
83
84
  }
84
85
  }