castle-web-cli 0.4.148 → 0.4.149

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,5 +1,18 @@
1
1
  import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
2
- import { onSaveReloadState, openFile, renameFile, takeReloadState, writeFile } from 'castle-web-sdk';
2
+ import { onSaveReloadState, takeReloadState, writeFile } from 'castle-web-sdk';
3
+ import * as castleSdk from 'castle-web-sdk';
4
+
5
+ // Same tolerance as SingleEditor's onSaveState/hasPendingSave: a deck that takes
6
+ // a kit update keeps whatever SDK it already installed, so these may be absent.
7
+ // Without openFile, view-source/edit-sprite taps do nothing; without renameFile,
8
+ // a blueprint rename throws inside its try and keeps the old paths -- degraded,
9
+ // not broken.
10
+ const openFile = castleSdk.openFile ?? (() => undefined);
11
+ const renameFile =
12
+ castleSdk.renameFile ??
13
+ (() => {
14
+ throw new Error('renameFile needs a newer castle-web-sdk');
15
+ });
3
16
  import { basename, formatJson, parseJsonFile } from '../engine/files';
4
17
  import {
5
18
  arrangeActors,
@@ -104,8 +117,7 @@ const MIN_EDIT_ZOOM = 0.4;
104
117
  const MAX_EDIT_ZOOM = 6;
105
118
  // Wheel-delta -> zoom-factor sensitivity for trackpad pinch (ctrl+wheel).
106
119
  const ZOOM_WHEEL_SPEED = 0.01;
107
- const clampZoom = (zoom) =>
108
- Math.min(MAX_EDIT_ZOOM, Math.max(MIN_EDIT_ZOOM, Number(zoom) || 1));
120
+ const clampZoom = (zoom) => Math.min(MAX_EDIT_ZOOM, Math.max(MIN_EDIT_ZOOM, Number(zoom) || 1));
109
121
  // Derive the effective edit viewport for a zoom level. Both extent and origin
110
122
  // scale by 1/zoom, which keeps the canvas draw path, `screenToCard`, and the
111
123
  // SelectionOverlay's `sx = box/card` projection mutually consistent (the overlay
@@ -146,8 +158,13 @@ function useMigrateOrphanActors({ isBlueprintFile, sceneData, files, text, onCha
146
158
  if (isBlueprintFile || !sceneData) return;
147
159
  const hasOrphans = (sceneData.actors ?? []).some((actor) => !actor.blueprint);
148
160
  if (!hasOrphans) return;
149
- const { sceneData: migrated, newBlueprintFiles } = migrateOrphanActors(files, behaviorClasses, sceneData);
150
- for (const blueprintFile of newBlueprintFiles) void writeFile(blueprintFile.path, blueprintFile.text);
161
+ const { sceneData: migrated, newBlueprintFiles } = migrateOrphanActors(
162
+ files,
163
+ behaviorClasses,
164
+ sceneData
165
+ );
166
+ for (const blueprintFile of newBlueprintFiles)
167
+ void writeFile(blueprintFile.path, blueprintFile.text);
151
168
  onChange(formatJson(migrated));
152
169
  // Re-run only when this file's own text changes (e.g. after the migration
153
170
  // write lands, or the user opens a different orphaned file) -- `files`
@@ -269,7 +286,9 @@ export function SceneEditor({
269
286
  // Sparse per-instance overrides merged with each actor's blueprint template
270
287
  // -- what hit-testing, drag-start snapshots, and the inspector should read.
271
288
  // Never the basis for a write (see engine/blueprint.js).
272
- const previewSceneData = sceneData ? makeScene(sceneData, behaviorClasses, sprites, files, path).data : null;
289
+ const previewSceneData = sceneData
290
+ ? makeScene(sceneData, behaviorClasses, sprites, files, path).data
291
+ : null;
273
292
  const serialize = useCallback(
274
293
  (next) => formatJson(isBlueprintFile ? stripTemplateId(next) : next),
275
294
  [isBlueprintFile]
@@ -376,7 +395,7 @@ export function SceneEditor({
376
395
  ? getBlueprintTemplate(files, rawSelectedActor.blueprint)
377
396
  : undefined;
378
397
  const selectedBlueprintName = rawSelectedActor?.blueprint
379
- ? selectedBlueprintTemplate?.name ?? basename(rawSelectedActor.blueprint)
398
+ ? (selectedBlueprintTemplate?.name ?? basename(rawSelectedActor.blueprint))
380
399
  : undefined;
381
400
  // Hotbar-selected blueprint, resolved fresh from live files each render. A
382
401
  // selection whose file vanished (cascade delete, external edit) degrades to
@@ -539,9 +558,21 @@ export function SceneEditor({
539
558
  const dragPlace = {
540
559
  enter: (blueprintPath, position) => {
541
560
  history.recordSnapshot();
542
- const { sceneData: next, newId } = placeBlueprintInstance(sceneData, files, blueprintPath, position, snapSettings);
561
+ const { sceneData: next, newId } = placeBlueprintInstance(
562
+ sceneData,
563
+ files,
564
+ blueprintPath,
565
+ position,
566
+ snapSettings
567
+ );
543
568
  const layout = next.actors.find((actor) => actor.id === newId)?.components?.Layout ?? {};
544
- dragPlaceRef.current = { actorId: newId, blueprintPath, sceneData: next, x: layout.x, y: layout.y };
569
+ dragPlaceRef.current = {
570
+ actorId: newId,
571
+ blueprintPath,
572
+ sceneData: next,
573
+ x: layout.x,
574
+ y: layout.y,
575
+ };
545
576
  onChange(serialize(next));
546
577
  onSelectActorIds([newId]);
547
578
  },
@@ -598,7 +629,7 @@ export function SceneEditor({
598
629
  {sceneData.name ?? basename(path)}
599
630
  </span>
600
631
  ) : (
601
- sceneData.name ?? basename(path)
632
+ (sceneData.name ?? basename(path))
602
633
  )
603
634
  }
604
635
  subtitle={
@@ -734,11 +765,7 @@ export function SceneEditor({
734
765
  label="Zoom out"
735
766
  onClick={() => panGesture.zoomAtCenter(0.8)}
736
767
  />
737
- <IconButton
738
- icon="fit"
739
- label="Reset view"
740
- onClick={() => panGesture.resetView()}
741
- />
768
+ <IconButton icon="fit" label="Reset view" onClick={() => panGesture.resetView()} />
742
769
  </div>
743
770
  )}
744
771
  </div>
@@ -906,15 +933,28 @@ function behaviorsToAdd(behaviorName, actor, seen = new Set()) {
906
933
  return out;
907
934
  }
908
935
 
909
- function makeSceneActions({ sceneData, files, serialize, commit, selectedActorIds, onSelectActorIds, isBlueprintFile, sprites, resolvedActors }) {
936
+ function makeSceneActions({
937
+ sceneData,
938
+ files,
939
+ serialize,
940
+ commit,
941
+ selectedActorIds,
942
+ onSelectActorIds,
943
+ isBlueprintFile,
944
+ sprites,
945
+ resolvedActors,
946
+ }) {
910
947
  function commitScene(next, options) {
911
948
  commit(serialize(next), options);
912
949
  }
913
950
  return {
914
951
  updateComponent: (actorId, behaviorName, nextProps) =>
915
- commitScene(setInstanceComponent(files, behaviorClasses, sceneData, actorId, behaviorName, nextProps), {
916
- coalesceKey: componentCoalesceKey(actorId, behaviorName, nextProps),
917
- }),
952
+ commitScene(
953
+ setInstanceComponent(files, behaviorClasses, sceneData, actorId, behaviorName, nextProps),
954
+ {
955
+ coalesceKey: componentCoalesceKey(actorId, behaviorName, nextProps),
956
+ }
957
+ ),
918
958
  addBehavior: (actorId, behaviorName) => {
919
959
  if (!findBehaviorClass(behaviorName)) return;
920
960
  const resolved = resolvedActors?.find((actor) => actor.id === actorId);
@@ -923,7 +963,12 @@ function makeSceneActions({ sceneData, files, serialize, commit, selectedActorId
923
963
  for (const name of behaviorsToAdd(behaviorName, resolved)) {
924
964
  const Behavior = findBehaviorClass(name);
925
965
  if (!Behavior) continue;
926
- next = setActorComponent(next, actorId, name, initialBehaviorProps(Behavior, resolved, sprites));
966
+ next = setActorComponent(
967
+ next,
968
+ actorId,
969
+ name,
970
+ initialBehaviorProps(Behavior, resolved, sprites)
971
+ );
927
972
  }
928
973
  if (next !== sceneData) commitScene(next);
929
974
  },
@@ -1015,7 +1060,13 @@ function useSelectionGesture(args) {
1015
1060
  const cam = current.editCameraRef.current;
1016
1061
  const point = { x: raw.x + cam.x, y: raw.y + cam.y };
1017
1062
  current.canvasRef.current.setPointerCapture(event.pointerId);
1018
- const scene = makeScene(current.sceneData, behaviorClasses, current.sprites, current.files, current.path);
1063
+ const scene = makeScene(
1064
+ current.sceneData,
1065
+ behaviorClasses,
1066
+ current.sprites,
1067
+ current.files,
1068
+ current.path
1069
+ );
1019
1070
  const stack = scene.actorsAt(point.x, point.y);
1020
1071
  const stackIds = stack.map((a) => a.id);
1021
1072
  const actor = stack[0] ?? null;
@@ -1119,12 +1170,7 @@ function useSelectionGesture(args) {
1119
1170
  finalizeMarquee(drag, current);
1120
1171
  } else if (drag.kind === 'idle' && !drag.movedFar && !drag.longPressFired) {
1121
1172
  handleTap(drag, current);
1122
- } else if (
1123
- drag.kind === 'move' &&
1124
- !drag.movedFar &&
1125
- !drag.longPressFired &&
1126
- drag.cycleStack
1127
- ) {
1173
+ } else if (drag.kind === 'move' && !drag.movedFar && !drag.longPressFired && drag.cycleStack) {
1128
1174
  cycleSelection(drag, current);
1129
1175
  }
1130
1176
  current.marqueeRef.current = null;
@@ -1358,18 +1404,21 @@ function usePanGesture({ canvasRef, editCameraRef, isPlaying }) {
1358
1404
  canvas.removeEventListener('gestureend', onGestureEnd, opts);
1359
1405
  };
1360
1406
  }, [canvasRef, panByScreenDelta, zoomByFactorAtPointer]);
1361
- const onPointerDown = useCallback((event) => {
1362
- if (isPlaying) return;
1363
- // Pan when Space is held (existing modifier), on a middle-button drag, or on
1364
- // a ⌘+left-drag (metaKey) — matching the art editor. preventDefault also
1365
- // suppresses the browser's middle-click autoscroll.
1366
- const middle = event.button === 1;
1367
- const cmdDrag = event.button === 0 && event.metaKey;
1368
- if (!spaceDownRef.current && !middle && !cmdDrag) return;
1369
- event.preventDefault();
1370
- event.currentTarget.setPointerCapture(event.pointerId);
1371
- dragRef.current = { id: event.pointerId, lastX: event.clientX, lastY: event.clientY };
1372
- }, [isPlaying]);
1407
+ const onPointerDown = useCallback(
1408
+ (event) => {
1409
+ if (isPlaying) return;
1410
+ // Pan when Space is held (existing modifier), on a middle-button drag, or on
1411
+ // a ⌘+left-drag (metaKey) — matching the art editor. preventDefault also
1412
+ // suppresses the browser's middle-click autoscroll.
1413
+ const middle = event.button === 1;
1414
+ const cmdDrag = event.button === 0 && event.metaKey;
1415
+ if (!spaceDownRef.current && !middle && !cmdDrag) return;
1416
+ event.preventDefault();
1417
+ event.currentTarget.setPointerCapture(event.pointerId);
1418
+ dragRef.current = { id: event.pointerId, lastX: event.clientX, lastY: event.clientY };
1419
+ },
1420
+ [isPlaying]
1421
+ );
1373
1422
  const onPointerMove = useCallback(
1374
1423
  (event) => {
1375
1424
  const drag = dragRef.current;
@@ -1495,7 +1544,13 @@ function finalizeMarquee(drag, current) {
1495
1544
  const m = current.marqueeRef.current;
1496
1545
  if (!m || !current.sceneData) return;
1497
1546
  if (m.width === 0 && m.height === 0) return;
1498
- const scene = makeScene(current.sceneData, behaviorClasses, current.sprites, current.files, current.path);
1547
+ const scene = makeScene(
1548
+ current.sceneData,
1549
+ behaviorClasses,
1550
+ current.sprites,
1551
+ current.files,
1552
+ current.path
1553
+ );
1499
1554
  const hits = scene.actorIdsInRect(m);
1500
1555
  const merged = new Set(drag.selectionBeforeMarquee);
1501
1556
  for (const id of hits) merged.add(id);
@@ -1538,7 +1593,11 @@ function useSelectionKeyboard(args) {
1538
1593
  const current = ref.current;
1539
1594
  if (current.isPlaying || isEditableTarget(event.target)) return;
1540
1595
  if (event.key === 'Escape') {
1541
- if (current.selectedActorIds.length || current.multiSelectMode || current.selectedBlueprintPath) {
1596
+ if (
1597
+ current.selectedActorIds.length ||
1598
+ current.multiSelectMode ||
1599
+ current.selectedBlueprintPath
1600
+ ) {
1542
1601
  event.preventDefault();
1543
1602
  // The wrapped setter also clears any hotbar blueprint selection.
1544
1603
  current.onSelectActorIds([]);
@@ -1553,7 +1612,8 @@ function useSelectionKeyboard(args) {
1553
1612
  return;
1554
1613
  }
1555
1614
  if ((event.metaKey || event.ctrlKey) && event.key.toLowerCase() === 'd') {
1556
- if (!current.sceneData || current.isBlueprintFile || current.selectedActorIds.length === 0) return;
1615
+ if (!current.sceneData || current.isBlueprintFile || current.selectedActorIds.length === 0)
1616
+ return;
1557
1617
  event.preventDefault();
1558
1618
  const { sceneData: next, newIds } = duplicateActors(
1559
1619
  current.sceneData,
@@ -1564,7 +1624,8 @@ function useSelectionKeyboard(args) {
1564
1624
  return;
1565
1625
  }
1566
1626
  if (event.key === 'Delete' || event.key === 'Backspace') {
1567
- if (!current.sceneData || current.isBlueprintFile || current.selectedActorIds.length === 0) return;
1627
+ if (!current.sceneData || current.isBlueprintFile || current.selectedActorIds.length === 0)
1628
+ return;
1568
1629
  event.preventDefault();
1569
1630
  current.commitScene(removeActors(current.sceneData, current.selectedActorIds));
1570
1631
  current.onSelectActorIds([]);
@@ -1612,9 +1673,7 @@ function useScenePlayLoop({
1612
1673
  const canvas = canvasRef.current;
1613
1674
  const ctx = canvas.getContext('2d');
1614
1675
  if (!ctx) return undefined;
1615
- const viewport = isPlaying
1616
- ? undefined
1617
- : editViewportForZoom(editCameraRef.current.zoom);
1676
+ const viewport = isPlaying ? undefined : editViewportForZoom(editCameraRef.current.zoom);
1618
1677
  configureSceneCanvas(canvas, ctx, viewport);
1619
1678
  let raf = 0;
1620
1679
  let last = performance.now();
@@ -1623,9 +1682,7 @@ function useScenePlayLoop({
1623
1682
  last = now;
1624
1683
  // Recompute each frame so the edit zoom (mutated on the camera ref by the
1625
1684
  // pinch gesture) takes effect without restarting the loop.
1626
- const frameViewport = isPlaying
1627
- ? undefined
1628
- : editViewportForZoom(editCameraRef.current.zoom);
1685
+ const frameViewport = isPlaying ? undefined : editViewportForZoom(editCameraRef.current.zoom);
1629
1686
  const scene = isPlaying
1630
1687
  ? runtimeRef.current
1631
1688
  : makeScene(sceneData, behaviorClasses, sprites, files, path);
@@ -1636,7 +1693,7 @@ function useScenePlayLoop({
1636
1693
  configureSceneCanvas(canvas, ctx, frameViewport);
1637
1694
  // Read selection from the ref each frame so the grid/box gating tracks
1638
1695
  // the current selection without resetting this loop.
1639
- const editSelectedActorIds = isPlaying ? [] : selectedActorIdsRef.current ?? [];
1696
+ const editSelectedActorIds = isPlaying ? [] : (selectedActorIdsRef.current ?? []);
1640
1697
  scene.draw(ctx, {
1641
1698
  selectedActorIds: [],
1642
1699
  marquee: isPlaying ? null : marqueeRef.current,
@@ -1653,7 +1710,7 @@ function useScenePlayLoop({
1653
1710
  dimBlueprintPath: isPlaying ? null : selectedBlueprintPathRef.current,
1654
1711
  // Highlight the collider shape the blueprint inspector has selected on
1655
1712
  // its lit instances.
1656
- colliderShapeSel: isPlaying ? -1 : colliderShapeSelRef?.current ?? -1,
1713
+ colliderShapeSel: isPlaying ? -1 : (colliderShapeSelRef?.current ?? -1),
1657
1714
  });
1658
1715
  }
1659
1716
  raf = requestAnimationFrame(frame);
@@ -1882,7 +1939,9 @@ function BlueprintInspector({
1882
1939
  rawActor={templateActor}
1883
1940
  files={files}
1884
1941
  sprites={sprites}
1885
- onSetComponent={(actorId, behaviorName, nextProps) => onSetComponent(behaviorName, nextProps)}
1942
+ onSetComponent={(actorId, behaviorName, nextProps) =>
1943
+ onSetComponent(behaviorName, nextProps)
1944
+ }
1886
1945
  onAddBehavior={(actorId, behaviorName) => onAddBehavior(behaviorName)}
1887
1946
  onRemoveBehavior={(actorId, behaviorName) => onRemoveBehavior(behaviorName)}
1888
1947
  onSelectShape={onSelectShape}
@@ -1962,7 +2021,11 @@ function ActorInspector({
1962
2021
  // since the store is plain localStorage and not reactive on its own.
1963
2022
  const [orderTick, setOrderTick] = useState(0);
1964
2023
  const savedOrder = useMemo(
1965
- () => applyOrder(ownerKey, visibleEntries.map(([behaviorName]) => behaviorName)),
2024
+ () =>
2025
+ applyOrder(
2026
+ ownerKey,
2027
+ visibleEntries.map(([behaviorName]) => behaviorName)
2028
+ ),
1966
2029
  // `orderTick` is the dependency that matters after a commit; the entry list
1967
2030
  // is what matters when the actor changes.
1968
2031
  // eslint-disable-next-line react-hooks/exhaustive-deps
@@ -2022,37 +2085,37 @@ function ActorInspector({
2022
2085
  />
2023
2086
  ) : null}
2024
2087
  <PanelSortContext names={orderedNames} onReorder={commitOrder}>
2025
- {orderedEntries.map(([behaviorName, component], index) => (
2026
- <BehaviorRow
2027
- key={behaviorName}
2028
- behaviorName={behaviorName}
2029
- component={component}
2030
- ownerKey={ownerKey}
2031
- selectedActor={selectedActor}
2032
- rawActor={rawActor}
2033
- blueprintTemplate={blueprintTemplate}
2034
- isInstance={isInstance}
2035
- files={files}
2036
- sprites={sprites}
2037
- onSetComponent={onSetComponent}
2038
- onRemoveBehavior={onRemoveBehavior}
2039
- onBeginPick={onBeginPick}
2040
- pickActive={pickActive}
2041
- onSelectShape={onSelectShape}
2042
- onCollapseChanged={() => setOrderTick((tick) => tick + 1)}
2043
- onViewSource={onViewSource}
2044
- isLast={index === orderedEntries.length - 1 && !showReveal}
2045
- />
2046
- ))}
2047
- {showReveal ? (
2048
- <button
2049
- type="button"
2050
- className={styles.revealOverridesButton}
2051
- onClick={() => setShowAllBehaviors(true)}>
2052
- <Icon name="chevron-down" />
2053
- <span>Override behavior properties</span>
2054
- </button>
2055
- ) : null}
2088
+ {orderedEntries.map(([behaviorName, component], index) => (
2089
+ <BehaviorRow
2090
+ key={behaviorName}
2091
+ behaviorName={behaviorName}
2092
+ component={component}
2093
+ ownerKey={ownerKey}
2094
+ selectedActor={selectedActor}
2095
+ rawActor={rawActor}
2096
+ blueprintTemplate={blueprintTemplate}
2097
+ isInstance={isInstance}
2098
+ files={files}
2099
+ sprites={sprites}
2100
+ onSetComponent={onSetComponent}
2101
+ onRemoveBehavior={onRemoveBehavior}
2102
+ onBeginPick={onBeginPick}
2103
+ pickActive={pickActive}
2104
+ onSelectShape={onSelectShape}
2105
+ onCollapseChanged={() => setOrderTick((tick) => tick + 1)}
2106
+ onViewSource={onViewSource}
2107
+ isLast={index === orderedEntries.length - 1 && !showReveal}
2108
+ />
2109
+ ))}
2110
+ {showReveal ? (
2111
+ <button
2112
+ type="button"
2113
+ className={styles.revealOverridesButton}
2114
+ onClick={() => setShowAllBehaviors(true)}>
2115
+ <Icon name="chevron-down" />
2116
+ <span>Override behavior properties</span>
2117
+ </button>
2118
+ ) : null}
2056
2119
  </PanelSortContext>
2057
2120
  </>
2058
2121
  );
@@ -2147,7 +2210,9 @@ function BehaviorRow({
2147
2210
  onCollapseChanged();
2148
2211
  }}
2149
2212
  handleProps={handleProps}
2150
- onViewSource={sourcePath ? { path: sourcePath, open: () => void onViewSource?.(sourcePath) } : null}
2213
+ onViewSource={
2214
+ sourcePath ? { path: sourcePath, open: () => void onViewSource?.(sourcePath) } : null
2215
+ }
2151
2216
  onRemove={removable ? () => onRemoveBehavior(selectedActor.id, behaviorName) : null}
2152
2217
  enabled={enabled}
2153
2218
  onToggleEnabled={
@@ -72,5 +72,5 @@
72
72
  "autoUpdateWhenImported": true,
73
73
  "deckId": "JH0SclbPVP0y",
74
74
  "cardId": "eXfAaUdbSU5A",
75
- "publishedVersion": "2026-08-27T18:04:56.551Z"
75
+ "publishedVersion": "2026-08-27T19:43:40.084Z"
76
76
  }
@@ -30,6 +30,7 @@ import {
30
30
  HotbarButton,
31
31
  StageCanvas,
32
32
  StageControls,
33
+ isViewerOnly,
33
34
  useEditorShortcuts,
34
35
  useFileSaver3D,
35
36
  } from './editorChrome';
@@ -234,9 +235,11 @@ export function PxModelEditor({ path }) {
234
235
  });
235
236
  };
236
237
 
238
+ const readOnly = isViewerOnly();
237
239
  useEditorShortcuts({
238
240
  history,
239
241
  setMode,
242
+ readOnly,
240
243
  onDuplicate: meshEdit ? undefined : duplicatePart,
241
244
  onDelete: meshEdit ? undefined : deletePart,
242
245
  onEscape: () => (meshEdit ? exitMeshEdit() : setSelectedPartId(null)),
@@ -276,6 +279,21 @@ export function PxModelEditor({ path }) {
276
279
  );
277
280
  }
278
281
 
282
+ // Hoisted so the hotbar markup reads flat (and jscpd stops seeing the two
283
+ // editors' identical scaffolding as one clone).
284
+ const addPartButtons = readOnly
285
+ ? null
286
+ : KIND_OPTIONS.map((option) => (
287
+ <HotbarButton
288
+ key={option.value}
289
+ icon={option.icon}
290
+ glyph={option.glyph}
291
+ label={option.label}
292
+ title={`Add a ${option.label.toLowerCase()} part`}
293
+ onClick={() => addPart(option.value)}
294
+ />
295
+ ));
296
+
279
297
  return (
280
298
  <div className={styles.panelBare}>
281
299
  <MainEditor>
@@ -283,21 +301,12 @@ export function PxModelEditor({ path }) {
283
301
  <div className={styles.sceneWorkspace}>
284
302
  <div className={styles.sceneTools}>
285
303
  <div className={styles.bpHotbar}>
286
- {KIND_OPTIONS.map((option) => (
287
- <HotbarButton
288
- key={option.value}
289
- icon={option.icon}
290
- glyph={option.glyph}
291
- label={option.label}
292
- title={`Add a ${option.label.toLowerCase()} part`}
293
- onClick={() => addPart(option.value)}
294
- />
295
- ))}
304
+ {addPartButtons}
296
305
  <span style={{ width: 10 }} />
297
- {selectedPart?.kind === 'box' && !meshEdit ? (
306
+ {selectedPart?.kind === 'box' && !meshEdit && !readOnly ? (
298
307
  <HotbarButton icon="object-group" label="To mesh" onClick={convertToMesh} />
299
308
  ) : null}
300
- {selectedPart?.kind === 'mesh' && !meshEdit ? (
309
+ {selectedPart?.kind === 'mesh' && !meshEdit && !readOnly ? (
301
310
  <HotbarButton icon="pencil" label="Edit mesh" onClick={enterMeshEdit} />
302
311
  ) : null}
303
312
  {meshEdit ? (
@@ -345,12 +354,14 @@ export function PxModelEditor({ path }) {
345
354
  history={history}
346
355
  mode={mode}
347
356
  setMode={setMode}
357
+ readOnly={readOnly}
348
358
  onDuplicate={duplicatePart}
349
359
  onDelete={deletePart}
350
360
  actionsDisabled={!selectedPartId}
351
361
  />
352
362
  </div>
353
363
  <ModelInspectorPane
364
+ readOnly={readOnly}
354
365
  model={model}
355
366
  selectedPartId={selectedPartId}
356
367
  onSelectPart={setSelectedPartId}
@@ -571,6 +582,7 @@ function ModelInspectorPane({
571
582
  onSelectPart,
572
583
  commitModel,
573
584
  setSelectedPartId,
585
+ readOnly = false,
574
586
  }) {
575
587
  const sheet = useMobileSheet({
576
588
  open: true,
@@ -591,7 +603,11 @@ function ModelInspectorPane({
591
603
  <div {...sheet.grabProps}>
592
604
  <SheetGrabHandle />
593
605
  </div>
594
- <div className={cx(styles.sheetBody, styles.inspectorBody)}>
606
+ <div
607
+ className={cx(styles.sheetBody, styles.inspectorBody)}
608
+ // The shared ui primitives have no disabled mode yet; the saver already
609
+ // refuses writes, this keeps the controls from looking live.
610
+ style={readOnly ? { pointerEvents: 'none', opacity: 0.75 } : undefined}>
595
611
  <Panel title="Model">
596
612
  <TextField
597
613
  label="Name"
@@ -59,11 +59,20 @@ import {
59
59
  initialBehaviorProps,
60
60
  } from './editor3dInspector';
61
61
  import { THUMB_PREFIX, thumbArtFor, withThumbRef } from './thumbnails';
62
- import { StageCanvas, StageControls, useEditorShortcuts, useFileSaver3D } from './editorChrome';
62
+ import {
63
+ StageCanvas,
64
+ StageControls,
65
+ isViewerOnly,
66
+ useEditorShortcuts,
67
+ useFileSaver3D,
68
+ } from './editorChrome';
63
69
 
64
70
  export function Scene3DEditor({ path }) {
65
71
  const stashKey = `scene3d-editor:${path}`;
66
72
  const [stash] = useState(() => takeReloadState(stashKey));
73
+ // The saver is already guarded; this flag is for the affordances, so the
74
+ // editor doesn't LOOK writable while nothing saves.
75
+ const readOnly = isViewerOnly();
67
76
  const saver = useFileSaver3D();
68
77
  const { files, setFiles, dataVersion, hydrated } = useLiveDeckFiles3D({
69
78
  shouldSkipPath: saver.hasPending,
@@ -133,7 +142,7 @@ export function Scene3DEditor({ path }) {
133
142
  onSelectActorIds([hits[next]]);
134
143
  },
135
144
  onCommitTransforms: (changes) => {
136
- if (!sceneData) return;
145
+ if (readOnly || !sceneData) return;
137
146
  const next = structuredClone(sceneData);
138
147
  for (const change of changes) {
139
148
  const actor = next.actors?.find((candidate) => candidate.id === change.id);
@@ -201,6 +210,7 @@ export function Scene3DEditor({ path }) {
201
210
  useEditorShortcuts({
202
211
  history,
203
212
  setMode,
213
+ readOnly,
204
214
  onDuplicate: actions.duplicateSelection,
205
215
  onDelete: actions.deleteSelection,
206
216
  onEscape: () => onSelectActorIds([]),
@@ -332,9 +342,9 @@ export function Scene3DEditor({ path }) {
332
342
  selectedBlueprintPath={selectedBlueprintPath}
333
343
  instanceBlueprintPath={rawSelectedActor?.blueprint ?? null}
334
344
  onSelectBlueprint={onSelectBlueprint}
335
- onAddActor={onAddActor}
336
- dragPlace={dragPlace}
337
- onDeleteBlueprint={onDeleteBlueprint}
345
+ onAddActor={readOnly ? undefined : onAddActor}
346
+ dragPlace={readOnly ? undefined : dragPlace}
347
+ onDeleteBlueprint={readOnly ? undefined : onDeleteBlueprint}
338
348
  />
339
349
  )}
340
350
  </div>
@@ -344,12 +354,14 @@ export function Scene3DEditor({ path }) {
344
354
  history={history}
345
355
  mode={mode}
346
356
  setMode={setMode}
357
+ readOnly={readOnly}
347
358
  onDuplicate={isBlueprintFile ? undefined : actions.duplicateSelection}
348
359
  onDelete={isBlueprintFile ? undefined : actions.deleteSelection}
349
360
  actionsDisabled={selectedActorIds.length === 0}
350
361
  />
351
362
  </div>
352
363
  <InspectorPane
364
+ readOnly={readOnly}
353
365
  hotbarBlueprint={
354
366
  hotbarBlueprint ? withThumbRef(hotbarBlueprint, selectedBlueprintPath) : null
355
367
  }
@@ -549,6 +561,7 @@ function InspectorPane(props) {
549
561
  storageKey: 'scene3d-inspector',
550
562
  });
551
563
  const {
564
+ readOnly,
552
565
  hotbarBlueprint,
553
566
  selectedBlueprintPath,
554
567
  blueprintActions,
@@ -596,7 +609,7 @@ function InspectorPane(props) {
596
609
  blueprintTemplate={selectedBlueprintTemplate}
597
610
  sprites={sprites}
598
611
  files={files}
599
- showAddBehavior={isBlueprintFile}
612
+ showAddBehavior={isBlueprintFile && !readOnly}
600
613
  onSetComponent={actions.updateComponent}
601
614
  onAddBehavior={actions.addBehavior}
602
615
  onRemoveBehavior={actions.removeBehavior}
@@ -623,7 +636,13 @@ function InspectorPane(props) {
623
636
  <div {...inspectorSheet.grabProps}>
624
637
  <SheetGrabHandle />
625
638
  </div>
626
- <div className={cx(styles.sheetBody, styles.inspectorBody)}>{body}</div>
639
+ <div
640
+ className={cx(styles.sheetBody, styles.inspectorBody)}
641
+ // The shared ui primitives have no disabled mode yet; the saver already
642
+ // refuses writes, this keeps the controls from looking live.
643
+ style={readOnly ? { pointerEvents: 'none', opacity: 0.75 } : undefined}>
644
+ {body}
645
+ </div>
627
646
  </aside>
628
647
  );
629
648
  }
@@ -82,6 +82,10 @@ const MODE_STYLE = {
82
82
  // imported kit's SingleEditor. This decides WHEN a change is worth writing; the
83
83
  // queue decides that it gets written at all, holding the newest text per path,
84
84
  // retrying, and surviving a reload.
85
+ // Same flag the imported kit's SingleEditor reads: the host page (view source)
86
+ // says the whole deck is read-only.
87
+ export const isViewerOnly = () => typeof window !== 'undefined' && window.__castleReadOnly === true;
88
+
85
89
  export function useFileSaver3D() {
86
90
  const pending = useRef({});
87
91
  const timers = useRef({});
@@ -98,6 +102,10 @@ export function useFileSaver3D() {
98
102
  return writeFile(path, value).catch(() => undefined);
99
103
  }
100
104
  function schedule(path, text) {
105
+ // A read-only host refuses these writes anyway; skip the request so a
106
+ // missed UI lock doesn't look like a save that failed. Guarding here is
107
+ // enough: nothing reaches the queue unscheduled.
108
+ if (isViewerOnly()) return;
101
109
  pending.current[path] = text;
102
110
  if (timers.current[path]) window.clearTimeout(timers.current[path]);
103
111
  timers.current[path] = window.setTimeout(() => void commit(path), 500);
@@ -147,6 +155,11 @@ export function useEditorShortcuts(handlers) {
147
155
  const onKeyDown = (event) => {
148
156
  if (isEditableTarget(event.target)) return;
149
157
  const h = latest.current;
158
+ if (h.readOnly) {
159
+ if (event.key === 'Escape') h.onEscape?.();
160
+ else if (event.key === '1') h.setMode('select');
161
+ return;
162
+ }
150
163
  const meta = event.metaKey || event.ctrlKey;
151
164
  if (meta && event.key.toLowerCase() === 'z') {
152
165
  event.preventDefault();
@@ -189,7 +202,15 @@ export const StageCanvas = forwardRef(function StageCanvas(props, ref) {
189
202
 
190
203
  // Undo/redo top-left; select/move/rotate (plus duplicate/delete when handlers
191
204
  // are given) down the right edge.
192
- export function StageControls({ history, mode, setMode, onDuplicate, onDelete, actionsDisabled }) {
205
+ export function StageControls({
206
+ history,
207
+ mode,
208
+ setMode,
209
+ onDuplicate,
210
+ onDelete,
211
+ actionsDisabled,
212
+ readOnly = false,
213
+ }) {
193
214
  const modeButton = (value, icon, label) => (
194
215
  <IconButton
195
216
  icon={icon}
@@ -199,6 +220,10 @@ export function StageControls({ history, mode, setMode, onDuplicate, onDelete, a
199
220
  onClick={() => setMode(value)}
200
221
  />
201
222
  );
223
+ // Selection stays (inspecting needs it); everything that mutates goes.
224
+ if (readOnly) {
225
+ return <div style={MODE_STYLE}>{modeButton('select', 'marquee', 'Select (1)')}</div>;
226
+ }
202
227
  return (
203
228
  <>
204
229
  <div style={UNDO_STYLE}>