castle-web-cli 0.4.142 → 0.4.145
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/agent-prompts.d.ts +1 -0
- package/dist/agent-prompts.js +7 -2
- package/dist/agent.js +11 -9
- package/dist/imports.js +44 -4
- package/dist/init.d.ts +4 -0
- package/dist/init.js +63 -26
- package/dist/install.js +4 -0
- package/dist/platformDoc.d.ts +12 -0
- package/dist/platformDoc.js +236 -0
- package/dist/serve.d.ts +3 -0
- package/dist/serve.js +157 -13
- package/dist/serveSecurity.d.ts +5 -1
- package/dist/serveSecurity.js +27 -8
- package/dist/shell/assets/index-BaiRLZ8p.css +1 -0
- package/dist/shell/assets/{index-qbygvQIx.js → index-CbRAepsq.js} +68 -68
- package/dist/shell/index.html +2 -2
- package/kits/blank/CLAUDE.md +198 -0
- package/kits/blank/castle.json +8 -0
- package/kits/blank/index.html +42 -0
- package/kits/blank/package-lock.json +10 -0
- package/kits/blank/package.json +9 -0
- package/kits/physics-2d/CLAUDE.md +44 -4
- package/kits/physics-2d/behaviors/Collider.jsx +21 -0
- package/kits/physics-2d/behaviors/Draggable.jsx +9 -0
- package/kits/physics-2d/behaviors/Layout.jsx +7 -0
- package/kits/physics-2d/behaviors/RigidBody.jsx +6 -0
- package/kits/physics-2d/behaviors/Sprite.jsx +9 -4
- package/kits/physics-2d/editors/BlueprintLibrary.jsx +64 -32
- package/kits/physics-2d/editors/NewBlueprintModal.jsx +69 -0
- package/kits/physics-2d/editors/PlayOnly.jsx +1 -1
- package/kits/physics-2d/editors/SceneEditor.jsx +333 -108
- package/kits/physics-2d/editors/SelectionOverlay.jsx +6 -2
- package/kits/physics-2d/editors/behaviorRegistry.js +78 -5
- package/kits/physics-2d/engine/SceneUI.jsx +2 -2
- package/kits/physics-2d/engine/blueprint.js +212 -15
- package/kits/physics-2d/engine/editorFlags.js +50 -0
- package/kits/physics-2d/engine/files.js +9 -0
- package/kits/physics-2d/engine/liveReload.js +16 -4
- package/kits/physics-2d/engine/panelPrefs.js +78 -0
- package/kits/physics-2d/engine/panelSorting.jsx +84 -0
- package/kits/physics-2d/engine/physics/PhysicsSystem.js +5 -2
- package/kits/physics-2d/engine/physics/matterBridge.js +12 -2
- package/kits/physics-2d/engine/scene.js +94 -3
- package/kits/physics-2d/engine/ui.jsx +157 -4
- package/kits/physics-2d/engine/ui.module.css +615 -1
- package/kits/physics-2d/package-lock.json +78 -1
- package/kits/physics-2d/package.json +4 -0
- package/kits/physics-3d/CLAUDE.md +2 -3
- package/package.json +1 -1
- package/dist/commonInstructions.d.ts +0 -1
- package/dist/commonInstructions.js +0 -26
- package/dist/shell/assets/index-CcVJjnL3.css +0 -1
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// Drag-to-reorder for the inspector's behavior panels.
|
|
2
|
+
|
|
3
|
+
import React from 'react';
|
|
4
|
+
import {
|
|
5
|
+
DndContext,
|
|
6
|
+
KeyboardSensor,
|
|
7
|
+
PointerSensor,
|
|
8
|
+
TouchSensor,
|
|
9
|
+
closestCenter,
|
|
10
|
+
useSensor,
|
|
11
|
+
useSensors,
|
|
12
|
+
} from '@dnd-kit/core';
|
|
13
|
+
import { restrictToParentElement, restrictToVerticalAxis } from '@dnd-kit/modifiers';
|
|
14
|
+
import {
|
|
15
|
+
SortableContext,
|
|
16
|
+
arrayMove,
|
|
17
|
+
sortableKeyboardCoordinates,
|
|
18
|
+
useSortable,
|
|
19
|
+
verticalListSortingStrategy,
|
|
20
|
+
} from '@dnd-kit/sortable';
|
|
21
|
+
import { CSS } from '@dnd-kit/utilities';
|
|
22
|
+
|
|
23
|
+
// Hold, then drag -- on both input kinds, because the drag target is the whole
|
|
24
|
+
// title row rather than a handle. Arming on movement alone would turn any click
|
|
25
|
+
// that drifts a pixel into a reorder, and the row carries a disclosure toggle
|
|
26
|
+
// and an enable switch that must stay clickable.
|
|
27
|
+
//
|
|
28
|
+
// `tolerance` is the drift allowed during the hold: past it dnd-kit abandons the
|
|
29
|
+
// gesture, so a press that was really the start of a scroll stays a scroll.
|
|
30
|
+
// A finger waits longer than a mouse -- a finger resting on a surface is
|
|
31
|
+
// ordinary, a held mouse button is deliberate.
|
|
32
|
+
const MOUSE_ACTIVATION = { delay: 200, tolerance: 6 };
|
|
33
|
+
const TOUCH_ACTIVATION = { delay: 380, tolerance: 8 };
|
|
34
|
+
|
|
35
|
+
export function PanelSortContext({ names, onReorder, children }) {
|
|
36
|
+
const sensors = useSensors(
|
|
37
|
+
useSensor(PointerSensor, { activationConstraint: MOUSE_ACTIVATION }),
|
|
38
|
+
useSensor(TouchSensor, { activationConstraint: TOUCH_ACTIVATION }),
|
|
39
|
+
useSensor(KeyboardSensor, { coordinateGetter: sortableKeyboardCoordinates })
|
|
40
|
+
);
|
|
41
|
+
const onDragEnd = (event) => {
|
|
42
|
+
const { active, over } = event;
|
|
43
|
+
if (!over || active.id === over.id) return;
|
|
44
|
+
const from = names.indexOf(active.id);
|
|
45
|
+
const to = names.indexOf(over.id);
|
|
46
|
+
if (from < 0 || to < 0) return;
|
|
47
|
+
onReorder(arrayMove(names, from, to));
|
|
48
|
+
};
|
|
49
|
+
return (
|
|
50
|
+
<DndContext
|
|
51
|
+
sensors={sensors}
|
|
52
|
+
collisionDetection={closestCenter}
|
|
53
|
+
// The panels are a vertical column inside a scrolling pane: a row that
|
|
54
|
+
// could drift sideways or out of the pane would just be a way to lose it.
|
|
55
|
+
modifiers={[restrictToVerticalAxis, restrictToParentElement]}
|
|
56
|
+
onDragEnd={onDragEnd}>
|
|
57
|
+
<SortableContext items={names} strategy={verticalListSortingStrategy}>
|
|
58
|
+
{children}
|
|
59
|
+
</SortableContext>
|
|
60
|
+
</DndContext>
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// One sortable row. Returns the props for the row element and the handle, so
|
|
65
|
+
// the caller decides what the handle looks like -- here it is the grip in the
|
|
66
|
+
// panel header, which is several components away.
|
|
67
|
+
export function usePanelSortable(id) {
|
|
68
|
+
const { attributes, listeners, setNodeRef, transform, transition, isDragging } = useSortable({
|
|
69
|
+
id,
|
|
70
|
+
});
|
|
71
|
+
return {
|
|
72
|
+
isDragging,
|
|
73
|
+
rowProps: {
|
|
74
|
+
ref: setNodeRef,
|
|
75
|
+
style: {
|
|
76
|
+
transform: CSS.Translate.toString(transform),
|
|
77
|
+
// No transition on the row being dragged: it is pinned to the pointer,
|
|
78
|
+
// and easing there shows up as lag.
|
|
79
|
+
transition: isDragging ? undefined : transition,
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
handleProps: { ...attributes, ...listeners },
|
|
83
|
+
};
|
|
84
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isBehaviorEnabled } from '../scene';
|
|
1
2
|
// The physics system: one matter-js engine per scene runtime. Registered as a
|
|
2
3
|
// runtime system (see engine/scene.js `registerSystem`), it runs once per frame
|
|
3
4
|
// in `afterBehaviors` -- after behaviors have expressed intent (velocities,
|
|
@@ -136,7 +137,8 @@ export class PhysicsSystem {
|
|
|
136
137
|
reconcileJoints(scene) {
|
|
137
138
|
const seen = new Set();
|
|
138
139
|
for (const actor of scene.getActors()) {
|
|
139
|
-
const
|
|
140
|
+
const joints = actor.components.Joints;
|
|
141
|
+
const list = isBehaviorEnabled(joints) ? joints?.list : null;
|
|
140
142
|
if (!Array.isArray(list) || list.length === 0) continue;
|
|
141
143
|
const bodyA = this.bodies.get(actor.id)?.body;
|
|
142
144
|
if (!bodyA) continue;
|
|
@@ -481,7 +483,8 @@ export class PhysicsSystem {
|
|
|
481
483
|
if (!scene) return [];
|
|
482
484
|
const out = [];
|
|
483
485
|
for (const actor of scene.getActors()) {
|
|
484
|
-
const
|
|
486
|
+
const joints = actor.components.Joints;
|
|
487
|
+
const list = isBehaviorEnabled(joints) ? joints?.list : null;
|
|
485
488
|
if (!Array.isArray(list)) continue;
|
|
486
489
|
const a = centerOfLayout(actor.components.Layout);
|
|
487
490
|
for (const joint of list) {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isBehaviorEnabled } from '../scene';
|
|
1
2
|
// Pure helpers bridging Cauldron actors (Layout + Collider + RigidBody
|
|
2
3
|
// components) to matter-js bodies. No engine state lives here -- the
|
|
3
4
|
// PhysicsSystem owns the world and calls these, feeding in the collider rect it
|
|
@@ -40,13 +41,22 @@ export const DEFAULT_GRAVITY = { x: 0, y: 1, scale: 0.001 };
|
|
|
40
41
|
|
|
41
42
|
// An actor participates in physics if it has a Collider (+ Layout). A RigidBody
|
|
42
43
|
// makes it move (dynamic/kinematic); a Collider alone is a static obstacle.
|
|
44
|
+
// Physics reads components directly rather than through `forEachBehavior`, so
|
|
45
|
+
// the enabled gate has to be repeated at each entry point. A disabled Collider
|
|
46
|
+
// means no body at all: the actor stops colliding, and its RigidBody, Joints and
|
|
47
|
+
// controls go inert with it -- which is the honest consequence of "a body only
|
|
48
|
+
// exists for an actor with a Collider", not a special case.
|
|
43
49
|
export function isPhysicsActor(actor) {
|
|
44
|
-
return
|
|
50
|
+
return (
|
|
51
|
+
isBehaviorEnabled(actor?.components?.Collider) && Boolean(actor?.components?.Layout)
|
|
52
|
+
);
|
|
45
53
|
}
|
|
46
54
|
|
|
55
|
+
// A disabled RigidBody leaves the Collider behind as a static obstacle, which is
|
|
56
|
+
// what a lone Collider already means -- so this needs no teardown of its own.
|
|
47
57
|
export function rigidBodyType(actor) {
|
|
48
58
|
const rb = actor?.components?.RigidBody;
|
|
49
|
-
if (!rb) return 'static';
|
|
59
|
+
if (!isBehaviorEnabled(rb)) return 'static';
|
|
50
60
|
return rb.bodyType ?? 'dynamic';
|
|
51
61
|
}
|
|
52
62
|
|
|
@@ -30,6 +30,13 @@ function resolveSceneFileKey(name) {
|
|
|
30
30
|
return candidates.find((candidate) => initialFiles[candidate] !== undefined) ?? key;
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
+
// `$enabled: false` turns a behavior off while keeping its props. Absent means
|
|
34
|
+
// on, so it is only ever stored when false, and every existing scene reads the
|
|
35
|
+
// same. `$` marks a prop the ENGINE owns -- behaviors must not define one.
|
|
36
|
+
export function isBehaviorEnabled(props) {
|
|
37
|
+
return Boolean(props) && props.$enabled !== false;
|
|
38
|
+
}
|
|
39
|
+
|
|
33
40
|
export class SceneRuntime {
|
|
34
41
|
// `files` is the live deck files map (path -> text), used to resolve every
|
|
35
42
|
// actor's `blueprint` ref against the CURRENT blueprint file content -- not
|
|
@@ -161,9 +168,11 @@ export class SceneRuntime {
|
|
|
161
168
|
return actor?.components?.[behaviorName] ?? null;
|
|
162
169
|
}
|
|
163
170
|
|
|
171
|
+
// "Actors where this behavior is WORKING", so a switched-off one must not
|
|
172
|
+
// answer. `getComponent` stays ungated -- it is a raw prop accessor.
|
|
164
173
|
actorWith(behaviorName) {
|
|
165
174
|
for (const actor of this.actors.values()) {
|
|
166
|
-
if (actor.components?.[behaviorName]) return actor;
|
|
175
|
+
if (isBehaviorEnabled(actor.components?.[behaviorName])) return actor;
|
|
167
176
|
}
|
|
168
177
|
return null;
|
|
169
178
|
}
|
|
@@ -171,7 +180,7 @@ export class SceneRuntime {
|
|
|
171
180
|
actorsWith(behaviorName) {
|
|
172
181
|
const out = [];
|
|
173
182
|
for (const actor of this.getActors()) {
|
|
174
|
-
if (actor.components?.[behaviorName]) out.push(actor);
|
|
183
|
+
if (isBehaviorEnabled(actor.components?.[behaviorName])) out.push(actor);
|
|
175
184
|
}
|
|
176
185
|
return out;
|
|
177
186
|
}
|
|
@@ -309,9 +318,48 @@ export class SceneRuntime {
|
|
|
309
318
|
this.pointer.down = false;
|
|
310
319
|
}
|
|
311
320
|
|
|
321
|
+
// A behavior that stops running cannot clean up after itself, and some hold
|
|
322
|
+
// state outside their own props -- `Draggable` is the only caller of
|
|
323
|
+
// `physics.release`, so switching it off mid-drag would weld the actor to the
|
|
324
|
+
// pointer for the session.
|
|
325
|
+
//
|
|
326
|
+
// Observed per frame rather than hooked at the mutation site, so it fires
|
|
327
|
+
// however the flag changed.
|
|
328
|
+
// Sprite specifically, not "any disabled behavior": a disabled Collider must
|
|
329
|
+
// NOT keep drawing. Sprite is the one whose absence loses the actor.
|
|
330
|
+
#drawDisabledSprite(ctx, actor, options) {
|
|
331
|
+
const props = actor.components?.Sprite;
|
|
332
|
+
if (!props || isBehaviorEnabled(props)) return;
|
|
333
|
+
const Behavior = this.behaviors.get('Sprite');
|
|
334
|
+
if (!Behavior) return;
|
|
335
|
+
ctx.save();
|
|
336
|
+
ctx.globalAlpha *= 0.5;
|
|
337
|
+
new Behavior(props).draw?.(actor, this, ctx, options);
|
|
338
|
+
ctx.restore();
|
|
339
|
+
drawDisabledSpriteLabel(ctx, actor);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
#syncEnabled(actor) {
|
|
343
|
+
const was = (actor.runtime._enabled ??= new Map());
|
|
344
|
+
for (const [behaviorName, props] of Object.entries(actor.components)) {
|
|
345
|
+
const on = isBehaviorEnabled(props);
|
|
346
|
+
if (was.get(behaviorName) === on) continue;
|
|
347
|
+
const first = !was.has(behaviorName);
|
|
348
|
+
was.set(behaviorName, on);
|
|
349
|
+
if (first) continue; // first sight is not a transition
|
|
350
|
+
const Behavior = this.behaviors.get(behaviorName);
|
|
351
|
+
if (!Behavior) continue;
|
|
352
|
+
const hook = on ? 'onEnable' : 'onDisable';
|
|
353
|
+
// Constructed from the props it still has -- the whole point of
|
|
354
|
+
// disabling is that they survive.
|
|
355
|
+
new Behavior(props ?? {})[hook]?.(actor, this);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
312
359
|
update(dt) {
|
|
313
360
|
this.time += dt;
|
|
314
361
|
for (const actor of this.getActors()) {
|
|
362
|
+
this.#syncEnabled(actor);
|
|
315
363
|
this.forEachBehavior(actor, (instance) => instance.update?.(actor, this, dt));
|
|
316
364
|
}
|
|
317
365
|
// Behaviors have expressed their intent (velocities, forces) for this frame;
|
|
@@ -326,7 +374,7 @@ export class SceneRuntime {
|
|
|
326
374
|
|
|
327
375
|
forEachBehavior(actor, callback) {
|
|
328
376
|
for (const [behaviorName, props] of Object.entries(actor.components)) {
|
|
329
|
-
if (!props) continue;
|
|
377
|
+
if (!isBehaviorEnabled(props)) continue;
|
|
330
378
|
const Behavior = this.behaviors.get(behaviorName);
|
|
331
379
|
if (!Behavior) continue;
|
|
332
380
|
callback(new Behavior(props));
|
|
@@ -377,6 +425,10 @@ export class SceneRuntime {
|
|
|
377
425
|
drawEditPlaceholder(ctx, actor);
|
|
378
426
|
}
|
|
379
427
|
this.forEachBehavior(actor, (instance) => instance.draw?.(actor, this, ctx, options));
|
|
428
|
+
// A disabled Sprite draws nothing at runtime -- `forEachBehavior` above
|
|
429
|
+
// skipped it. In the EDITOR it still draws, ghosted and labelled, so the
|
|
430
|
+
// actor stays findable, selectable and positionable while switched off.
|
|
431
|
+
if (options.editPlaceholders) this.#drawDisabledSprite(ctx, actor, options);
|
|
380
432
|
ctx.restore();
|
|
381
433
|
}
|
|
382
434
|
|
|
@@ -727,6 +779,45 @@ function shouldDrawEditPlaceholder(actor, sprites) {
|
|
|
727
779
|
return spriteIsEmpty(resolved);
|
|
728
780
|
}
|
|
729
781
|
|
|
782
|
+
// Wraps to two lines when the actor is too narrow for one -- a label that always
|
|
783
|
+
// overflows its actor stops reading as belonging to it.
|
|
784
|
+
function drawDisabledSpriteLabel(ctx, actor) {
|
|
785
|
+
const layout = getLayout(actor);
|
|
786
|
+
if (!layout) return;
|
|
787
|
+
const fontSize = Math.max(8, Math.min(12, layout.width / 9));
|
|
788
|
+
ctx.save();
|
|
789
|
+
ctx.font = `600 ${fontSize}px -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif`;
|
|
790
|
+
ctx.textAlign = 'center';
|
|
791
|
+
ctx.textBaseline = 'middle';
|
|
792
|
+
const padX = fontSize * 0.6;
|
|
793
|
+
const padY = fontSize * 0.4;
|
|
794
|
+
|
|
795
|
+
const single = 'Sprite disabled';
|
|
796
|
+
const lines =
|
|
797
|
+
ctx.measureText(single).width + padX * 2 <= layout.width ? [single] : ['Sprite', 'disabled'];
|
|
798
|
+
const lineHeight = fontSize * 1.15;
|
|
799
|
+
const textWidth = Math.max(...lines.map((line) => ctx.measureText(line).width));
|
|
800
|
+
const width = textWidth + padX * 2;
|
|
801
|
+
const height = lineHeight * lines.length + padY * 2;
|
|
802
|
+
const cx = layout.x + layout.width / 2;
|
|
803
|
+
const cy = layout.y + layout.height / 2;
|
|
804
|
+
const x = cx - width / 2;
|
|
805
|
+
const y = cy - height / 2;
|
|
806
|
+
const r = Math.min(4, height / 2);
|
|
807
|
+
ctx.beginPath();
|
|
808
|
+
if (ctx.roundRect) ctx.roundRect(x, y, width, height, r);
|
|
809
|
+
else ctx.rect(x, y, width, height);
|
|
810
|
+
ctx.fillStyle = 'rgba(10, 12, 18, 0.82)';
|
|
811
|
+
ctx.fill();
|
|
812
|
+
ctx.strokeStyle = 'rgba(255, 255, 255, 0.22)';
|
|
813
|
+
ctx.lineWidth = 1;
|
|
814
|
+
ctx.stroke();
|
|
815
|
+
ctx.fillStyle = 'rgba(255, 255, 255, 0.85)';
|
|
816
|
+
const firstBaseline = cy - ((lines.length - 1) * lineHeight) / 2;
|
|
817
|
+
lines.forEach((line, i) => ctx.fillText(line, cx, firstBaseline + i * lineHeight));
|
|
818
|
+
ctx.restore();
|
|
819
|
+
}
|
|
820
|
+
|
|
730
821
|
function drawEditPlaceholder(ctx, actor) {
|
|
731
822
|
const layout = getLayout(actor);
|
|
732
823
|
if (!layout) return;
|
|
@@ -24,6 +24,9 @@ import {
|
|
|
24
24
|
faFilm,
|
|
25
25
|
faFillDrip,
|
|
26
26
|
faGlobe,
|
|
27
|
+
faGripVertical,
|
|
28
|
+
faToggleOff,
|
|
29
|
+
faToggleOn,
|
|
27
30
|
faImage,
|
|
28
31
|
faLayerGroup,
|
|
29
32
|
faLocationArrow,
|
|
@@ -59,6 +62,26 @@ export const theme = {
|
|
|
59
62
|
export function cx(...parts) {
|
|
60
63
|
return parts.filter(Boolean).join(' ');
|
|
61
64
|
}
|
|
65
|
+
// Vertical wheel scrolls a horizontal strip, so reaching the far end needs no
|
|
66
|
+
// horizontal axis. Subtracting deltaY, not adding: under macOS natural
|
|
67
|
+
// scrolling a downward gesture reports a NEGATIVE deltaY, and the strip should
|
|
68
|
+
// follow the gesture's physical direction.
|
|
69
|
+
//
|
|
70
|
+
// Bound natively so the listener can be non-passive and preventDefault.
|
|
71
|
+
export function useWheelToHorizontal(ref) {
|
|
72
|
+
useEffect(() => {
|
|
73
|
+
const strip = ref.current;
|
|
74
|
+
if (!strip) return undefined;
|
|
75
|
+
const onWheel = (event) => {
|
|
76
|
+
if (event.deltaY !== 0 && Math.abs(event.deltaY) > Math.abs(event.deltaX)) {
|
|
77
|
+
strip.scrollLeft -= event.deltaY;
|
|
78
|
+
event.preventDefault();
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
strip.addEventListener('wheel', onWheel, { passive: false });
|
|
82
|
+
return () => strip.removeEventListener('wheel', onWheel);
|
|
83
|
+
}, [ref]);
|
|
84
|
+
}
|
|
62
85
|
// Track an element's content box via ResizeObserver. Shared by the canvas
|
|
63
86
|
// overlays (SceneUI, SelectionOverlay) that scale a card-unit layer onto the
|
|
64
87
|
// live canvas size.
|
|
@@ -221,6 +244,9 @@ const icons = {
|
|
|
221
244
|
file: faFile,
|
|
222
245
|
film: faFilm,
|
|
223
246
|
globe: faGlobe,
|
|
247
|
+
grip: faGripVertical,
|
|
248
|
+
'toggle-off': faToggleOff,
|
|
249
|
+
'toggle-on': faToggleOn,
|
|
224
250
|
grid: faTh,
|
|
225
251
|
image: faImage,
|
|
226
252
|
'layer-group': faLayerGroup,
|
|
@@ -272,16 +298,143 @@ export function Icon({ name }) {
|
|
|
272
298
|
</svg>
|
|
273
299
|
);
|
|
274
300
|
}
|
|
301
|
+
// Panels inside a `<BehaviorPanel>` gain the collapse toggle, summary, drag and
|
|
302
|
+
// enable/remove/view-source controls; panels outside one are plain.
|
|
303
|
+
//
|
|
304
|
+
// A context rather than props, because nine behaviors render their own
|
|
305
|
+
// `<Panel>` and a tenth added tomorrow should get all of it for free. One
|
|
306
|
+
// wrapper per BEHAVIOR, so what is inside it is unambiguously that one's.
|
|
307
|
+
const BehaviorPanelContext = React.createContext(null);
|
|
308
|
+
|
|
309
|
+
export function BehaviorPanel({
|
|
310
|
+
summary = null,
|
|
311
|
+
collapsed = false,
|
|
312
|
+
onToggle,
|
|
313
|
+
handleProps = null,
|
|
314
|
+
onViewSource = null,
|
|
315
|
+
onRemove = null,
|
|
316
|
+
enabled = true,
|
|
317
|
+
onToggleEnabled = null,
|
|
318
|
+
dragging = false,
|
|
319
|
+
children,
|
|
320
|
+
}) {
|
|
321
|
+
const value = React.useMemo(
|
|
322
|
+
() => ({
|
|
323
|
+
summary,
|
|
324
|
+
collapsed,
|
|
325
|
+
onToggle,
|
|
326
|
+
handleProps,
|
|
327
|
+
onViewSource,
|
|
328
|
+
onRemove,
|
|
329
|
+
enabled,
|
|
330
|
+
onToggleEnabled,
|
|
331
|
+
dragging,
|
|
332
|
+
}),
|
|
333
|
+
[summary, collapsed, onToggle, handleProps, onViewSource, onRemove, enabled, onToggleEnabled, dragging]
|
|
334
|
+
);
|
|
335
|
+
return <BehaviorPanelContext.Provider value={value}>{children}</BehaviorPanelContext.Provider>;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// `static summary(props)` on the behavior class. Never allowed to break the
|
|
339
|
+
// inspector: a throwing or over-long summary is dropped rather than rendered.
|
|
340
|
+
const SUMMARY_MAX = 40;
|
|
341
|
+
|
|
342
|
+
export function behaviorSummary(Behavior, props) {
|
|
343
|
+
let text;
|
|
344
|
+
try {
|
|
345
|
+
text = Behavior?.summary?.(props ?? {});
|
|
346
|
+
} catch {
|
|
347
|
+
return null;
|
|
348
|
+
}
|
|
349
|
+
if (typeof text !== 'string') return null;
|
|
350
|
+
const trimmed = text.trim();
|
|
351
|
+
if (!trimmed) return null;
|
|
352
|
+
return trimmed.length > SUMMARY_MAX ? `${trimmed.slice(0, SUMMARY_MAX - 1)}…` : trimmed;
|
|
353
|
+
}
|
|
354
|
+
|
|
275
355
|
export function Panel({ title, action, children, overridden = false }) {
|
|
356
|
+
const behavior = React.useContext(BehaviorPanelContext);
|
|
357
|
+
const collapsible = Boolean(behavior) && Boolean(title);
|
|
358
|
+
const collapsed = collapsible ? behavior.collapsed : false;
|
|
276
359
|
return (
|
|
277
|
-
<section className={styles.panel}>
|
|
360
|
+
<section className={cx(styles.panel, behavior?.dragging && styles.panelDragging)}>
|
|
278
361
|
{title ? (
|
|
279
|
-
<div
|
|
280
|
-
|
|
362
|
+
<div
|
|
363
|
+
className={cx(styles.panelHeader, collapsed && styles.panelHeaderCollapsed)}
|
|
364
|
+
{...(behavior?.handleProps ?? {})}>
|
|
365
|
+
{collapsible && behavior.handleProps ? (
|
|
366
|
+
// Affordance only -- the whole header row carries the drag
|
|
367
|
+
// listeners, so there is nothing to hit precisely.
|
|
368
|
+
<span className={styles.panelGrip} aria-hidden="true">
|
|
369
|
+
<Icon name="grip" />
|
|
370
|
+
</span>
|
|
371
|
+
) : null}
|
|
372
|
+
{collapsible ? (
|
|
373
|
+
// The whole title row is the hit target, not just the chevron --
|
|
374
|
+
// it is a 200px-wide row and the chevron is 10px.
|
|
375
|
+
<button
|
|
376
|
+
type="button"
|
|
377
|
+
className={styles.panelDisclosure}
|
|
378
|
+
aria-expanded={!collapsed}
|
|
379
|
+
title={collapsed ? `Expand ${title}` : `Collapse ${title}`}
|
|
380
|
+
onClick={behavior.onToggle}>
|
|
381
|
+
<Icon name={collapsed ? 'chevron-right' : 'chevron-down'} />
|
|
382
|
+
<span className={cx(styles.panelTitleText, overridden && styles.panelTitleOverridden)}>
|
|
383
|
+
{title}
|
|
384
|
+
</span>
|
|
385
|
+
{/* Only while collapsed: expanded, the fields ARE the summary,
|
|
386
|
+
and repeating them in the header is noise. */}
|
|
387
|
+
{/* Off REPLACES the summary rather than decorating it: "spring ·
|
|
388
|
+
target: anchor" on a joint that is not running is a lie. */}
|
|
389
|
+
{collapsed && (behavior.enabled === false || behavior.summary) ? (
|
|
390
|
+
<span className={styles.panelSummary}>
|
|
391
|
+
{behavior.enabled === false ? 'Off' : behavior.summary}
|
|
392
|
+
</span>
|
|
393
|
+
) : null}
|
|
394
|
+
</button>
|
|
395
|
+
) : (
|
|
396
|
+
<span className={cx(overridden && styles.panelTitleOverridden)}>{title}</span>
|
|
397
|
+
)}
|
|
398
|
+
{behavior?.onToggleEnabled ? (
|
|
399
|
+
<button
|
|
400
|
+
type="button"
|
|
401
|
+
className={cx(styles.panelPower, !behavior.enabled && styles.panelPowerOff)}
|
|
402
|
+
aria-label={behavior.enabled ? `Disable ${title}` : `Enable ${title}`}
|
|
403
|
+
aria-pressed={behavior.enabled}
|
|
404
|
+
title={behavior.enabled ? `Disable ${title}` : `Enable ${title}`}
|
|
405
|
+
onClick={behavior.onToggleEnabled}>
|
|
406
|
+
<Icon name={behavior.enabled ? 'toggle-on' : 'toggle-off'} />
|
|
407
|
+
</button>
|
|
408
|
+
) : null}
|
|
281
409
|
{action}
|
|
282
410
|
</div>
|
|
283
411
|
) : null}
|
|
284
|
-
|
|
412
|
+
{collapsed || !behavior ? null : (
|
|
413
|
+
// Occasional actions, so they live in the OPEN panel rather than the
|
|
414
|
+
// collapsed row -- a shut panel is for reading state, and every icon
|
|
415
|
+
// competing there costs more than these two are worth. As text, because
|
|
416
|
+
// they read as what they do instead of needing a tooltip to say.
|
|
417
|
+
<div className={styles.panelLinks}>
|
|
418
|
+
{behavior.onViewSource ? (
|
|
419
|
+
<button type="button" className={styles.panelLink} onClick={() => behavior.onViewSource.open()}>
|
|
420
|
+
View source
|
|
421
|
+
</button>
|
|
422
|
+
) : null}
|
|
423
|
+
{behavior.onRemove ? (
|
|
424
|
+
<button
|
|
425
|
+
type="button"
|
|
426
|
+
className={cx(styles.panelLink, styles.panelLinkDanger)}
|
|
427
|
+
onClick={behavior.onRemove}>
|
|
428
|
+
Remove
|
|
429
|
+
</button>
|
|
430
|
+
) : null}
|
|
431
|
+
</div>
|
|
432
|
+
)}
|
|
433
|
+
{collapsed ? null : (
|
|
434
|
+
<div className={cx(styles.panelBody, behavior?.enabled === false && styles.panelBodyOff)}>
|
|
435
|
+
{children}
|
|
436
|
+
</div>
|
|
437
|
+
)}
|
|
285
438
|
</section>
|
|
286
439
|
);
|
|
287
440
|
}
|