castle-web-cli 0.4.3 → 0.4.5

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.
Files changed (97) hide show
  1. package/dist/index.js +8 -2
  2. package/dist/init.js +1 -1
  3. package/kits/basic-2d/CLAUDE.md +3 -3
  4. package/package.json +1 -1
  5. package/kits/basic-2d-frozen/.prettierrc +0 -8
  6. package/kits/basic-2d-frozen/CLAUDE.md +0 -131
  7. package/kits/basic-2d-frozen/behaviors/Camera.jsx +0 -43
  8. package/kits/basic-2d-frozen/behaviors/Collider.jsx +0 -71
  9. package/kits/basic-2d-frozen/behaviors/Drawing.jsx +0 -139
  10. package/kits/basic-2d-frozen/behaviors/Layout.jsx +0 -16
  11. package/kits/basic-2d-frozen/drawings/floor.drawing +0 -70
  12. package/kits/basic-2d-frozen/editors/App.jsx +0 -152
  13. package/kits/basic-2d-frozen/editors/CodeEditor.jsx +0 -112
  14. package/kits/basic-2d-frozen/editors/DrawingEditor.jsx +0 -222
  15. package/kits/basic-2d-frozen/editors/FileBrowser.jsx +0 -143
  16. package/kits/basic-2d-frozen/editors/PlayOnly.jsx +0 -21
  17. package/kits/basic-2d-frozen/editors/SceneEditor.jsx +0 -1012
  18. package/kits/basic-2d-frozen/editors/behaviorRegistry.js +0 -24
  19. package/kits/basic-2d-frozen/editors/editorHistory.js +0 -52
  20. package/kits/basic-2d-frozen/engine/ScenePlayer.jsx +0 -83
  21. package/kits/basic-2d-frozen/engine/SceneUI.jsx +0 -67
  22. package/kits/basic-2d-frozen/engine/TouchControls.jsx +0 -136
  23. package/kits/basic-2d-frozen/engine/autoInspector.jsx +0 -51
  24. package/kits/basic-2d-frozen/engine/files.js +0 -62
  25. package/kits/basic-2d-frozen/engine/scene.js +0 -420
  26. package/kits/basic-2d-frozen/engine/ui.jsx +0 -344
  27. package/kits/basic-2d-frozen/engine/ui.module.css +0 -928
  28. package/kits/basic-2d-frozen/eslint.config.js +0 -50
  29. package/kits/basic-2d-frozen/index.html +0 -11
  30. package/kits/basic-2d-frozen/main.jsx +0 -10
  31. package/kits/basic-2d-frozen/package-lock.json +0 -2706
  32. package/kits/basic-2d-frozen/package.json +0 -41
  33. package/kits/basic-2d-frozen/scenes/main.scene +0 -108
  34. package/kits/basic-2d-frozen/vite.config.js +0 -1
  35. package/kits/rpg-2d/.prettierrc +0 -8
  36. package/kits/rpg-2d/behaviors/Camera.tsx +0 -52
  37. package/kits/rpg-2d/behaviors/Collider.tsx +0 -98
  38. package/kits/rpg-2d/behaviors/Dialog.tsx +0 -184
  39. package/kits/rpg-2d/behaviors/Drawing.tsx +0 -161
  40. package/kits/rpg-2d/behaviors/Friend.tsx +0 -45
  41. package/kits/rpg-2d/behaviors/Layout.tsx +0 -29
  42. package/kits/rpg-2d/behaviors/PlayerController.tsx +0 -255
  43. package/kits/rpg-2d/behaviors/Portal.tsx +0 -60
  44. package/kits/rpg-2d/behaviors/QuestLog.tsx +0 -90
  45. package/kits/rpg-2d/behaviors/SaveMenu.tsx +0 -123
  46. package/kits/rpg-2d/behaviors/Tilemap.tsx +0 -90
  47. package/kits/rpg-2d/drawings/bld-home.drawing +0 -8136
  48. package/kits/rpg-2d/drawings/env-crate.drawing +0 -509
  49. package/kits/rpg-2d/drawings/env-fence.drawing +0 -536
  50. package/kits/rpg-2d/drawings/env-flower-bed.drawing +0 -607
  51. package/kits/rpg-2d/drawings/env-fountain.drawing +0 -2622
  52. package/kits/rpg-2d/drawings/env-hedge.drawing +0 -601
  53. package/kits/rpg-2d/drawings/env-house-blue.drawing +0 -1
  54. package/kits/rpg-2d/drawings/env-house-green.drawing +0 -1
  55. package/kits/rpg-2d/drawings/env-tree-oak.drawing +0 -1540
  56. package/kits/rpg-2d/drawings/env-tree-pine.drawing +0 -1315
  57. package/kits/rpg-2d/drawings/floor.drawing +0 -70
  58. package/kits/rpg-2d/drawings/fx-sparkle.drawing +0 -926
  59. package/kits/rpg-2d/drawings/npc-juno-idle-down.drawing +0 -1099
  60. package/kits/rpg-2d/drawings/npc-juno-walk-down.drawing +0 -4177
  61. package/kits/rpg-2d/drawings/npc-opal-idle-down.drawing +0 -1099
  62. package/kits/rpg-2d/drawings/npc-opal-walk-down.drawing +0 -4177
  63. package/kits/rpg-2d/drawings/player-idle-down.drawing +0 -1070
  64. package/kits/rpg-2d/drawings/player-idle-left.drawing +0 -1070
  65. package/kits/rpg-2d/drawings/player-idle-right.drawing +0 -1070
  66. package/kits/rpg-2d/drawings/player-idle-up.drawing +0 -1070
  67. package/kits/rpg-2d/drawings/player-walk-down.drawing +0 -4148
  68. package/kits/rpg-2d/drawings/player-walk-left.drawing +0 -4148
  69. package/kits/rpg-2d/drawings/player-walk-right.drawing +0 -4148
  70. package/kits/rpg-2d/drawings/player-walk-up.drawing +0 -4148
  71. package/kits/rpg-2d/editors/App.tsx +0 -163
  72. package/kits/rpg-2d/editors/CodeEditor.tsx +0 -120
  73. package/kits/rpg-2d/editors/DrawingEditor.tsx +0 -278
  74. package/kits/rpg-2d/editors/FileBrowser.tsx +0 -191
  75. package/kits/rpg-2d/editors/PlayOnly.tsx +0 -26
  76. package/kits/rpg-2d/editors/SceneEditor.tsx +0 -1093
  77. package/kits/rpg-2d/editors/behaviorRegistry.ts +0 -33
  78. package/kits/rpg-2d/editors/editorHistory.ts +0 -75
  79. package/kits/rpg-2d/editors/editorProps.ts +0 -10
  80. package/kits/rpg-2d/engine/ScenePlayer.tsx +0 -130
  81. package/kits/rpg-2d/engine/SceneUI.tsx +0 -74
  82. package/kits/rpg-2d/engine/TouchControls.tsx +0 -157
  83. package/kits/rpg-2d/engine/autoInspector.tsx +0 -111
  84. package/kits/rpg-2d/engine/drawing.ts +0 -81
  85. package/kits/rpg-2d/engine/files.ts +0 -215
  86. package/kits/rpg-2d/engine/scene.ts +0 -484
  87. package/kits/rpg-2d/engine/ui.module.css +0 -928
  88. package/kits/rpg-2d/engine/ui.tsx +0 -483
  89. package/kits/rpg-2d/eslint.config.js +0 -46
  90. package/kits/rpg-2d/index.html +0 -11
  91. package/kits/rpg-2d/main.tsx +0 -14
  92. package/kits/rpg-2d/package-lock.json +0 -3149
  93. package/kits/rpg-2d/package.json +0 -46
  94. package/kits/rpg-2d/scenes/main.scene +0 -203
  95. package/kits/rpg-2d/tsconfig.json +0 -17
  96. package/kits/rpg-2d/vite-env.d.ts +0 -7
  97. package/kits/rpg-2d/vite.config.js +0 -1
@@ -1,24 +0,0 @@
1
- // Discover every behavior class from `behaviors/*.tsx`. Vite HMR is off, so a
2
- // newly-added behavior file is picked up on the next reload/restart.
3
- const modules = import.meta.glob('../behaviors/*.jsx', { eager: true });
4
- function isBehaviorClass(value) {
5
- return typeof value === 'function' && typeof value.behaviorName === 'string';
6
- }
7
- function collectBehaviors() {
8
- const found = new Map();
9
- for (const mod of Object.values(modules)) {
10
- for (const exported of Object.values(mod)) {
11
- if (isBehaviorClass(exported)) found.set(exported.behaviorName, exported);
12
- }
13
- }
14
- // Layout first (every actor needs it), then alphabetical.
15
- return [...found.values()].sort((a, b) => {
16
- if (a.behaviorName === 'Layout') return -1;
17
- if (b.behaviorName === 'Layout') return 1;
18
- return a.behaviorName.localeCompare(b.behaviorName);
19
- });
20
- }
21
- export const behaviorClasses = collectBehaviors();
22
- export function findBehaviorClass(behaviorName) {
23
- return behaviorClasses.find((candidate) => candidate.behaviorName === behaviorName);
24
- }
@@ -1,52 +0,0 @@
1
- import { useState } from 'react';
2
- const HISTORY_LIMIT = 50;
3
- // Text-undo/redo for file-backed editors. `text` is the canonical current
4
- // value; `onChange` writes the new value back. The hook owns the undo/redo
5
- // stacks; it never mutates `text` directly.
6
- export function useEditHistory(text, onChange) {
7
- const [history, setHistory] = useState({ undo: [], redo: [] });
8
- function commit(nextText) {
9
- if (nextText === text) return;
10
- setHistory((current) => ({
11
- undo: [...current.undo, text].slice(-HISTORY_LIMIT),
12
- redo: [],
13
- }));
14
- onChange(nextText);
15
- }
16
- function recordSnapshot() {
17
- setHistory((current) => ({
18
- undo: [...current.undo, text].slice(-HISTORY_LIMIT),
19
- redo: [],
20
- }));
21
- }
22
- function undo() {
23
- setHistory((current) => {
24
- const previous = current.undo.at(-1);
25
- if (!previous) return current;
26
- onChange(previous);
27
- return {
28
- undo: current.undo.slice(0, -1),
29
- redo: [text, ...current.redo].slice(0, HISTORY_LIMIT),
30
- };
31
- });
32
- }
33
- function redo() {
34
- setHistory((current) => {
35
- const next = current.redo[0];
36
- if (!next) return current;
37
- onChange(next);
38
- return {
39
- undo: [...current.undo, text].slice(-HISTORY_LIMIT),
40
- redo: current.redo.slice(1),
41
- };
42
- });
43
- }
44
- return {
45
- commit,
46
- undo,
47
- redo,
48
- canUndo: history.undo.length > 0,
49
- canRedo: history.redo.length > 0,
50
- recordSnapshot,
51
- };
52
- }
@@ -1,83 +0,0 @@
1
- import React, { useCallback, useEffect, useRef } from 'react';
2
- import { configureSceneCanvas, makeScene } from './scene';
3
- import { SceneUI } from './SceneUI';
4
- import { TouchControls } from './TouchControls';
5
- // Engine-level scene player: mount a `SceneRuntime` against a canvas, wire
6
- // keyboard / pointer input, run the update+draw loop, and render the
7
- // behavior-driven UI overlay. No game logic lives here -- behaviors and
8
- // scenes are the place for that.
9
- export function ScenePlayer({ sceneData, drawings, behaviorClasses }) {
10
- const canvasRef = useRef(null);
11
- const runtimeRef = useRef(null);
12
- const getKeys = useCallback(() => runtimeRef.current?.keys ?? null, []);
13
- const getRuntime = useCallback(() => runtimeRef.current, []);
14
- useEffect(() => {
15
- const canvas = canvasRef.current;
16
- if (!canvas) return undefined;
17
- const ctx = canvas.getContext('2d');
18
- if (!ctx) return undefined;
19
- configureSceneCanvas(canvas, ctx);
20
- const runtime = makeScene(sceneData, behaviorClasses, drawings).clone();
21
- runtimeRef.current = runtime;
22
- const onKeyDown = (event) => {
23
- runtime.keys.add(event.key);
24
- };
25
- const onKeyUp = (event) => {
26
- runtime.keys.delete(event.key);
27
- };
28
- const onPointerDown = (event) => {
29
- runtime.setPointerFromScreen(canvas, event.clientX, event.clientY, true);
30
- canvas.setPointerCapture(event.pointerId);
31
- };
32
- const onPointerMove = (event) => {
33
- runtime.setPointerFromScreen(canvas, event.clientX, event.clientY);
34
- };
35
- const onPointerUp = (event) => {
36
- runtime.setPointerFromScreen(canvas, event.clientX, event.clientY, false);
37
- try {
38
- canvas.releasePointerCapture(event.pointerId);
39
- } catch {
40
- // Pointer capture may already be gone after cancel/blur.
41
- }
42
- };
43
- window.addEventListener('keydown', onKeyDown);
44
- window.addEventListener('keyup', onKeyUp);
45
- canvas.addEventListener('pointerdown', onPointerDown);
46
- canvas.addEventListener('pointermove', onPointerMove);
47
- canvas.addEventListener('pointerup', onPointerUp);
48
- canvas.addEventListener('pointercancel', onPointerUp);
49
- const stopLoop = startPlayerLoop(canvas, ctx, runtime);
50
- return () => {
51
- stopLoop();
52
- window.removeEventListener('keydown', onKeyDown);
53
- window.removeEventListener('keyup', onKeyUp);
54
- canvas.removeEventListener('pointerdown', onPointerDown);
55
- canvas.removeEventListener('pointermove', onPointerMove);
56
- canvas.removeEventListener('pointerup', onPointerUp);
57
- canvas.removeEventListener('pointercancel', onPointerUp);
58
- runtimeRef.current = null;
59
- };
60
- // eslint-disable-next-line react-hooks/exhaustive-deps
61
- }, []);
62
- return (
63
- <div style={{ position: 'fixed', inset: 0, background: '#000' }}>
64
- <canvas ref={canvasRef} style={{ width: '100%', height: '100%', display: 'block' }} />
65
- <SceneUI getRuntime={getRuntime} />
66
- <TouchControls getKeys={getKeys} />
67
- </div>
68
- );
69
- }
70
- function startPlayerLoop(canvas, ctx, runtime) {
71
- let raf = 0;
72
- let previousTime = performance.now();
73
- const tick = (now) => {
74
- const dt = Math.min(0.033, (now - previousTime) / 1000);
75
- previousTime = now;
76
- runtime.update(dt);
77
- configureSceneCanvas(canvas, ctx);
78
- runtime.draw(ctx, { useCamera: true });
79
- raf = requestAnimationFrame(tick);
80
- };
81
- raf = requestAnimationFrame(tick);
82
- return () => cancelAnimationFrame(raf);
83
- }
@@ -1,67 +0,0 @@
1
- import React, { useEffect, useRef, useState } from 'react';
2
- import styles from './ui.module.css';
3
- import { cardSize } from './scene';
4
- // Game-time UI overlay.
5
- //
6
- // Behaviors expose a `ui(actor, scene)` handler -- the React counterpart of
7
- // `draw`. `SceneUI` collects every behavior's UI output each frame and renders
8
- // it into a deck-sized layer that sits exactly over the canvas. The layer is
9
- // `cardSize` units wide/tall and scaled onto the canvas box, so behaviors lay
10
- // out in the same card units as `Layout`. The root clips with `overflow:
11
- // hidden`, so deck UI can never render outside the card.
12
- export function SceneUI({ getRuntime }) {
13
- const rootRef = useRef(null);
14
- const [box, setBox] = useState({ width: 0, height: 0 });
15
- const [, forceRender] = useState(0);
16
- // Track the canvas-sized overlay box so the card-unit layer scales onto it.
17
- useEffect(() => {
18
- const root = rootRef.current;
19
- if (!root) return undefined;
20
- const measure = () => setBox({ width: root.clientWidth, height: root.clientHeight });
21
- const observer = new ResizeObserver(measure);
22
- observer.observe(root);
23
- measure();
24
- return () => observer.disconnect();
25
- }, []);
26
- // Re-render every frame so behavior UI reflects live runtime state.
27
- useEffect(() => {
28
- let raf = 0;
29
- const frame = () => {
30
- forceRender((tick) => tick + 1);
31
- raf = requestAnimationFrame(frame);
32
- };
33
- raf = requestAnimationFrame(frame);
34
- return () => cancelAnimationFrame(raf);
35
- }, []);
36
- return (
37
- <div ref={rootRef} className={styles.sceneUiRoot}>
38
- <div
39
- className={styles.sceneUiLayer}
40
- style={{
41
- width: cardSize.width,
42
- height: cardSize.height,
43
- transform: `scale(${box.width / cardSize.width}, ${box.height / cardSize.height})`,
44
- }}>
45
- {collectUiNodes(getRuntime())}
46
- </div>
47
- </div>
48
- );
49
- }
50
- // Build one keyed React node per behavior that defines a `ui` handler.
51
- function collectUiNodes(runtime) {
52
- if (!runtime) return [];
53
- const nodes = [];
54
- for (const actor of runtime.getActors()) {
55
- for (const [behaviorName, props] of Object.entries(actor.components)) {
56
- if (!props) continue;
57
- const Behavior = runtime.behaviors.get(behaviorName);
58
- if (!Behavior) continue;
59
- const instance = new Behavior(props);
60
- if (!instance.ui) continue;
61
- const node = instance.ui(actor, runtime);
62
- if (node == null) continue;
63
- nodes.push(<React.Fragment key={`${actor.id}:${behaviorName}`}>{node}</React.Fragment>);
64
- }
65
- }
66
- return nodes;
67
- }
@@ -1,136 +0,0 @@
1
- import React, { useCallback, useEffect, useRef, useState } from 'react';
2
- // On-screen movement controls: a 4-way d-pad feeds arrow keys onto
3
- // `scene.keys` -- the exact same keyboard keys behaviors consume, no separate
4
- // input path.
5
- const KEYS = {
6
- up: 'ArrowUp',
7
- down: 'ArrowDown',
8
- left: 'ArrowLeft',
9
- right: 'ArrowRight',
10
- };
11
- export function TouchControls({ getKeys, visible = true }) {
12
- const [isTouch, setIsTouch] = useState(false);
13
- const heldRef = useRef(new Set());
14
- useEffect(() => {
15
- const params = new URLSearchParams(window.location.search);
16
- const override = params.get('touch');
17
- if (override === '1' || override === 'true') {
18
- setIsTouch(true);
19
- return undefined;
20
- }
21
- if (override === '0' || override === 'false') {
22
- setIsTouch(false);
23
- return undefined;
24
- }
25
- const mq = window.matchMedia('(pointer: coarse)');
26
- const update = () => {
27
- setIsTouch(mq.matches || 'ontouchstart' in window || navigator.maxTouchPoints > 0);
28
- };
29
- update();
30
- mq.addEventListener?.('change', update);
31
- return () => mq.removeEventListener?.('change', update);
32
- }, []);
33
- const release = useCallback(
34
- (id) => {
35
- if (!heldRef.current.has(id)) return;
36
- heldRef.current.delete(id);
37
- getKeys()?.delete(KEYS[id]);
38
- },
39
- [getKeys]
40
- );
41
- useEffect(() => {
42
- if (!visible || !isTouch) {
43
- // Drop any keys we were holding when controls hide.
44
- const keys = getKeys();
45
- if (keys) for (const id of heldRef.current) keys.delete(KEYS[id]);
46
- heldRef.current.clear();
47
- }
48
- }, [visible, isTouch, getKeys]);
49
- if (!visible || !isTouch) return null;
50
- const press = (id) => {
51
- heldRef.current.add(id);
52
- getKeys()?.add(KEYS[id]);
53
- };
54
- const button = (id, label, extra) => (
55
- <button
56
- key={id}
57
- type="button"
58
- tabIndex={-1}
59
- aria-label={id}
60
- onTouchStart={(event) => {
61
- event.preventDefault();
62
- press(id);
63
- }}
64
- onTouchEnd={(event) => {
65
- event.preventDefault();
66
- release(id);
67
- }}
68
- onTouchCancel={(event) => {
69
- event.preventDefault();
70
- release(id);
71
- }}
72
- onMouseDown={(event) => {
73
- event.preventDefault();
74
- press(id);
75
- }}
76
- onMouseUp={() => release(id)}
77
- onMouseLeave={() => release(id)}
78
- onContextMenu={(event) => event.preventDefault()}
79
- style={{ ...buttonStyle, ...extra }}>
80
- {label}
81
- </button>
82
- );
83
- return (
84
- <div style={containerStyle}>
85
- <div style={dpadStyle}>
86
- {button('up', '▲', dpadUpStyle)}
87
- {button('left', '◀', dpadLeftStyle)}
88
- {button('right', '▶', dpadRightStyle)}
89
- {button('down', '▼', dpadDownStyle)}
90
- </div>
91
- </div>
92
- );
93
- }
94
- const containerStyle = {
95
- position: 'fixed',
96
- left: 0,
97
- right: 0,
98
- bottom: 0,
99
- display: 'flex',
100
- justifyContent: 'flex-start',
101
- alignItems: 'flex-end',
102
- padding: '12px 16px calc(env(safe-area-inset-bottom, 0px) + 12px)',
103
- pointerEvents: 'none',
104
- zIndex: 50,
105
- };
106
- // 3x3 grid -- the d-pad arms occupy the cross cells, corners stay empty.
107
- const dpadStyle = {
108
- display: 'grid',
109
- gridTemplateColumns: 'repeat(3, 64px)',
110
- gridTemplateRows: 'repeat(3, 64px)',
111
- gap: 6,
112
- pointerEvents: 'auto',
113
- };
114
- const dpadUpStyle = { gridColumn: 2, gridRow: 1 };
115
- const dpadLeftStyle = { gridColumn: 1, gridRow: 2 };
116
- const dpadRightStyle = { gridColumn: 3, gridRow: 2 };
117
- const dpadDownStyle = { gridColumn: 2, gridRow: 3 };
118
- const buttonStyle = {
119
- width: 64,
120
- height: 64,
121
- borderRadius: 12,
122
- border: '1px solid rgba(255, 255, 255, 0.35)',
123
- background: 'rgba(0, 0, 0, 0.35)',
124
- color: 'rgba(255, 255, 255, 0.92)',
125
- fontSize: 24,
126
- fontWeight: 600,
127
- display: 'flex',
128
- alignItems: 'center',
129
- justifyContent: 'center',
130
- userSelect: 'none',
131
- WebkitUserSelect: 'none',
132
- WebkitTouchCallout: 'none',
133
- WebkitTapHighlightColor: 'transparent',
134
- touchAction: 'none',
135
- cursor: 'pointer',
136
- };
@@ -1,51 +0,0 @@
1
- import React from 'react';
2
- import { CheckboxField, ColorField, NumberField, Panel, TextField, isHexColor } from './ui';
3
- export function AutoFields({ defaultProps, component, setComponent, only, exclude }) {
4
- const keys = Object.keys(defaultProps).filter((key) => {
5
- if (only) return only.includes(key);
6
- if (exclude) return !exclude.includes(key);
7
- return true;
8
- });
9
- return (
10
- <>
11
- {keys.map((key) => {
12
- const fallback = defaultProps[key];
13
- const current = key in component ? component[key] : fallback;
14
- const set = (value) => setComponent({ [key]: value });
15
- const label = humanizeKey(key);
16
- const sample = fallback ?? current;
17
- if (typeof sample === 'number') {
18
- return <NumberField key={key} label={label} value={current} onChange={set} />;
19
- }
20
- if (typeof sample === 'boolean') {
21
- return <CheckboxField key={key} label={label} checked={current} onChange={set} />;
22
- }
23
- if (isHexColor(sample)) {
24
- return <ColorField key={key} label={label} value={current} onChange={set} />;
25
- }
26
- return (
27
- <TextField
28
- key={key}
29
- label={label}
30
- value={current == null ? '' : String(current)}
31
- onChange={set}
32
- />
33
- );
34
- })}
35
- </>
36
- );
37
- }
38
- export function AutoInspector({ behaviorName, defaultProps, component, setComponent }) {
39
- return (
40
- <Panel title={humanizeKey(behaviorName)}>
41
- <AutoFields defaultProps={defaultProps} component={component} setComponent={setComponent} />
42
- </Panel>
43
- );
44
- }
45
- // `deadZoneY` -> `Dead Zone Y`, `PlatformerCharacter` -> `Platformer Character`.
46
- export function humanizeKey(key) {
47
- return key
48
- .replace(/([a-z0-9])([A-Z])/g, '$1 $2')
49
- .replace(/^./, (c) => c.toUpperCase())
50
- .trim();
51
- }
@@ -1,62 +0,0 @@
1
- // Seed the file map by scanning the deck dir. Vite module-cache is invalidated
2
- // on restart, so a newly-created file just shows up on the next reload.
3
- const rawModules = import.meta.glob(
4
- ['../scenes/*.scene', '../drawings/*.drawing', '../behaviors/*.jsx'],
5
- { query: '?raw', import: 'default', eager: true }
6
- );
7
- export const initialFiles = Object.fromEntries(
8
- Object.entries(rawModules)
9
- .map(([globPath, text]) => [globPath.replace(/^\.\.\//, ''), text])
10
- .sort(([a], [b]) => a.localeCompare(b))
11
- );
12
- export function getFileKind(path) {
13
- if (path.endsWith('.scene')) return 'scene';
14
- if (path.endsWith('.drawing')) return 'drawing';
15
- if (path.endsWith('.js') || path.endsWith('.jsx')) return 'code';
16
- return 'text';
17
- }
18
- export function parseJsonFile(path, text) {
19
- try {
20
- return { value: JSON.parse(text), error: null };
21
- } catch (error) {
22
- const message = error instanceof Error ? error.message : String(error);
23
- return { value: null, error: `${path}: ${message}` };
24
- }
25
- }
26
- export function formatJson(value) {
27
- return `${JSON.stringify(value, null, 2)}\n`;
28
- }
29
- export function basename(path) {
30
- return path.split('/').pop() ?? path;
31
- }
32
- // Flat list of file paths in the order FileBrowser renders them: a
33
- // depth-first walk of the directory tree, mirroring buildFileTree there.
34
- export function flatFileOrder(paths) {
35
- const root = { isFile: false, path: '', children: [], childMap: new Map() };
36
- for (const path of paths) {
37
- const parts = path.split('/');
38
- let parent = root;
39
- for (let index = 0; index < parts.length; index++) {
40
- const name = parts[index];
41
- const nodePath = parts.slice(0, index + 1).join('/');
42
- const isFile = index === parts.length - 1;
43
- if (!parent.childMap.has(name)) {
44
- const node = { isFile, path: nodePath, children: [], childMap: new Map() };
45
- parent.childMap.set(name, node);
46
- parent.children.push(node);
47
- }
48
- const child = parent.childMap.get(name);
49
- if (!child || child.isFile) break;
50
- parent = child;
51
- }
52
- }
53
- const order = [];
54
- const walk = (node) => {
55
- for (const child of node.children) {
56
- if (child.isFile) order.push(child.path);
57
- else walk(child);
58
- }
59
- };
60
- walk(root);
61
- return order;
62
- }