@ai-matrx/canvas 0.2.2 → 0.4.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/dist/react.cjs CHANGED
@@ -57,6 +57,7 @@ __export(react_exports, {
57
57
  useCanvasHostPorts: () => useCanvasHostPorts,
58
58
  useCanvasIsPresented: () => useCanvasIsPresented,
59
59
  useCanvasKind: () => useCanvasKind,
60
+ useCanvasKindToggle: () => useCanvasKindToggle,
60
61
  useCanvasKinds: () => useCanvasKinds,
61
62
  useCanvasNativeBodies: () => useCanvasNativeBodies,
62
63
  useCanvasState: () => useCanvasState,
@@ -1278,6 +1279,44 @@ function selectCanvasWindowItemCount(s, windowId) {
1278
1279
  }
1279
1280
  var NO_WINDOWS = Object.freeze({});
1280
1281
 
1282
+ // src/core/toggleKind.ts
1283
+ var CANVAS_DEFAULT_KEY = "default";
1284
+ function selectCanvasKindVisibility(state, kind, key = CANVAS_DEFAULT_KEY) {
1285
+ const itemId = canvasItemId(kind, key);
1286
+ if (!state.items[itemId]) return "absent";
1287
+ const windowId = canvasWindowOfItem(state, itemId);
1288
+ if (!windowId) return "background";
1289
+ const isOpen = canvasWindowFrame(state, windowId)?.isOpen ?? false;
1290
+ const pane = Object.values(state.panes).find((p) => p.itemIds.includes(itemId));
1291
+ return isOpen && pane?.activeItemId === itemId ? "visible" : "background";
1292
+ }
1293
+ function toggleKind(controller, input) {
1294
+ const key = input.key ?? CANVAS_DEFAULT_KEY;
1295
+ const itemId = canvasItemId(input.kind, key);
1296
+ const before = selectCanvasKindVisibility(controller.getState(), input.kind, key);
1297
+ if (before === "visible") {
1298
+ if ((input.whenVisible ?? "close") === "close") {
1299
+ controller.close(itemId);
1300
+ } else {
1301
+ controller.hide(canvasWindowOfItem(controller.getState(), itemId) ?? void 0);
1302
+ }
1303
+ return selectCanvasKindVisibility(controller.getState(), input.kind, key);
1304
+ }
1305
+ if (before === "background") {
1306
+ controller.open({ kind: input.kind, key });
1307
+ const windowId = canvasWindowOfItem(controller.getState(), itemId);
1308
+ if (windowId) controller.show(windowId);
1309
+ return selectCanvasKindVisibility(controller.getState(), input.kind, key);
1310
+ }
1311
+ const opened = controller.open({
1312
+ kind: input.kind,
1313
+ key,
1314
+ ...input.data !== void 0 ? { data: input.data } : {},
1315
+ ...input.title !== void 0 ? { title: input.title } : {}
1316
+ });
1317
+ return opened ? selectCanvasKindVisibility(controller.getState(), input.kind, key) : "absent";
1318
+ }
1319
+
1281
1320
  // src/react/layer.tsx
1282
1321
  var import_react = require("react");
1283
1322
  var import_react_dom = require("react-dom");
@@ -1577,6 +1616,17 @@ function useOptionalCanvasState(selector, fallback) {
1577
1616
  const read = () => store ? selector(store.getState()) : fallback;
1578
1617
  return (0, import_react2.useSyncExternalStore)(store ? store.subscribe : NO_SUBSCRIPTION, read, read);
1579
1618
  }
1619
+ function useCanvasKindToggle(input) {
1620
+ const controller = useCanvasContext().controller;
1621
+ const key = input.key ?? CANVAS_DEFAULT_KEY;
1622
+ const visibility = useCanvasState((s) => selectCanvasKindVisibility(s, input.kind, key));
1623
+ return {
1624
+ visibility,
1625
+ isOpen: visibility !== "absent",
1626
+ isVisible: visibility === "visible",
1627
+ toggle: () => toggleKind(controller, input)
1628
+ };
1629
+ }
1580
1630
  function useCanvasKinds() {
1581
1631
  (0, import_react2.useSyncExternalStore)(subscribeCanvasKinds, getCanvasKindsVersion, getCanvasKindsVersion);
1582
1632
  return listCanvasKinds();
@@ -1999,6 +2049,7 @@ function CanvasPaneView({ paneId }) {
1999
2049
  const kindProps = active && kind ? { item: active, data: active.data, paneId, isFocused: focused, canvas, windowId } : null;
2000
2050
  const Leading = kind?.HeaderLeading;
2001
2051
  const Action = kind?.HeaderAction;
2052
+ const Toolbar = kind?.Toolbar;
2002
2053
  const single = tabStrip === "auto" && pane.itemIds.length === 1 && active;
2003
2054
  const hideDrop = () => {
2004
2055
  drop.current?.();
@@ -2025,7 +2076,7 @@ function CanvasPaneView({ paneId }) {
2025
2076
  canvas.moveItem(itemId, paneId);
2026
2077
  };
2027
2078
  const closeLabel = role === "pop-out" && paneCount === 1 ? "Close window" : paneCount > 1 ? "Close pane" : "Close canvas";
2028
- return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
2079
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(
2029
2080
  CanvasPanel,
2030
2081
  {
2031
2082
  ref: sectionRef,
@@ -2050,15 +2101,30 @@ function CanvasPaneView({ paneId }) {
2050
2101
  onDragOver,
2051
2102
  onDragLeave,
2052
2103
  onDrop,
2053
- children: pane.itemIds.length === 0 ? /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(Launcher, { paneId }) : pane.itemIds.map((itemId) => {
2054
- const item = items[itemId];
2055
- if (!item) return null;
2056
- const isActive = itemId === pane.activeItemId;
2057
- return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(ItemBody, { item, paneId, windowId, isActive, isFocused: focused && isActive, hasNativePort: !!ports.onNativeBodyFrame }, itemId);
2058
- })
2104
+ children: [
2105
+ Toolbar && kindProps ? /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { className: "mxc-item-toolbar", "data-kind": active?.kind, children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(ToolbarBoundary, { kindId: kind?.id ?? "", children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(Toolbar, { ...kindProps }) }) }) : null,
2106
+ pane.itemIds.length === 0 ? /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(Launcher, { paneId }) : pane.itemIds.map((itemId) => {
2107
+ const item = items[itemId];
2108
+ if (!item) return null;
2109
+ const isActive = itemId === pane.activeItemId;
2110
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(ItemBody, { item, paneId, windowId, isActive, isFocused: focused && isActive, hasNativePort: !!ports.onNativeBodyFrame }, itemId);
2111
+ })
2112
+ ]
2059
2113
  }
2060
2114
  );
2061
2115
  }
2116
+ var ToolbarBoundary = class extends import_react4.Component {
2117
+ state = { failed: false };
2118
+ static getDerivedStateFromError() {
2119
+ return { failed: true };
2120
+ }
2121
+ componentDidCatch(error) {
2122
+ console.error(`[@ai-matrx/canvas] "${this.props.kindId}" Toolbar threw`, error);
2123
+ }
2124
+ render() {
2125
+ return this.state.failed ? null : this.props.children;
2126
+ }
2127
+ };
2062
2128
  function SingleTitle({ item, kind }) {
2063
2129
  const title = itemTitle(item, kind);
2064
2130
  const drag = useTabDrag(item.id, title);