@catchlight/editor-web 0.0.0 → 0.6.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/LICENSE-APACHE +201 -0
- package/LICENSE-MIT +21 -0
- package/README.md +26 -2
- package/THIRD-PARTY-NOTICES.md +331 -0
- package/dist/editor/CatchlightEditor.d.ts +21 -0
- package/dist/editor/CatchlightEditor.js +720 -0
- package/dist/editor/dialogs.d.ts +18 -0
- package/dist/editor/dialogs.js +71 -0
- package/dist/editor/import-panel.d.ts +6 -0
- package/dist/editor/import-panel.js +98 -0
- package/dist/editor/logo.svg +11 -0
- package/dist/editor/notification.d.ts +4 -0
- package/dist/editor/notification.js +24 -0
- package/dist/editor/stage.d.ts +13 -0
- package/dist/editor/stage.js +23 -0
- package/dist/editor/test/setup.d.ts +5 -0
- package/dist/editor/test/setup.js +5 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +10 -0
- package/dist/lib/animations-panel.d.ts +10 -0
- package/dist/lib/animations-panel.js +48 -0
- package/dist/lib/authoring.d.ts +17 -0
- package/dist/lib/authoring.js +116 -0
- package/dist/lib/binding-grid.d.ts +19 -0
- package/dist/lib/binding-grid.js +92 -0
- package/dist/lib/bindings.d.ts +21 -0
- package/dist/lib/bindings.js +100 -0
- package/dist/lib/camera.d.ts +21 -0
- package/dist/lib/camera.js +61 -0
- package/dist/lib/controls-panel.d.ts +36 -0
- package/dist/lib/controls-panel.js +201 -0
- package/dist/lib/controls.d.ts +103 -0
- package/dist/lib/controls.js +163 -0
- package/dist/lib/editing.d.ts +126 -0
- package/dist/lib/editing.js +415 -0
- package/dist/lib/editor-context.d.ts +15 -0
- package/dist/lib/editor-context.js +23 -0
- package/dist/lib/expressions-panel.d.ts +10 -0
- package/dist/lib/expressions-panel.js +120 -0
- package/dist/lib/file-open.d.ts +11 -0
- package/dist/lib/file-open.js +25 -0
- package/dist/lib/file-save.d.ts +29 -0
- package/dist/lib/file-save.js +68 -0
- package/dist/lib/gizmo.d.ts +13 -0
- package/dist/lib/gizmo.js +247 -0
- package/dist/lib/image-markup.d.ts +12 -0
- package/dist/lib/image-markup.js +153 -0
- package/dist/lib/index.d.ts +71 -0
- package/dist/lib/index.js +44 -0
- package/dist/lib/inspector.d.ts +19 -0
- package/dist/lib/inspector.js +161 -0
- package/dist/lib/latest.d.ts +3 -0
- package/dist/lib/latest.js +9 -0
- package/dist/lib/layer-import.d.ts +38 -0
- package/dist/lib/layer-import.js +187 -0
- package/dist/lib/mesh-editor.d.ts +13 -0
- package/dist/lib/mesh-editor.js +337 -0
- package/dist/lib/model-panels.d.ts +23 -0
- package/dist/lib/model-panels.js +180 -0
- package/dist/lib/node-actions.d.ts +42 -0
- package/dist/lib/node-actions.js +115 -0
- package/dist/lib/node-drag.d.ts +18 -0
- package/dist/lib/node-drag.js +65 -0
- package/dist/lib/node-patch.d.ts +8 -0
- package/dist/lib/node-patch.js +10 -0
- package/dist/lib/node-tree.d.ts +36 -0
- package/dist/lib/node-tree.js +221 -0
- package/dist/lib/official.d.ts +82 -0
- package/dist/lib/official.js +197 -0
- package/dist/lib/param-actions.d.ts +48 -0
- package/dist/lib/param-actions.js +106 -0
- package/dist/lib/param-list.d.ts +36 -0
- package/dist/lib/param-list.js +73 -0
- package/dist/lib/param-slider.d.ts +44 -0
- package/dist/lib/param-slider.js +149 -0
- package/dist/lib/pose.d.ts +13 -0
- package/dist/lib/pose.js +32 -0
- package/dist/lib/posing-panel.d.ts +40 -0
- package/dist/lib/posing-panel.js +154 -0
- package/dist/lib/presence.d.ts +27 -0
- package/dist/lib/presence.js +98 -0
- package/dist/lib/properties-panel.d.ts +13 -0
- package/dist/lib/properties-panel.js +63 -0
- package/dist/lib/recording-panel.d.ts +4 -0
- package/dist/lib/recording-panel.js +161 -0
- package/dist/lib/replica.d.ts +18 -0
- package/dist/lib/replica.js +34 -0
- package/dist/lib/review.d.ts +113 -0
- package/dist/lib/review.js +529 -0
- package/dist/lib/rigging.d.ts +10 -0
- package/dist/lib/rigging.js +116 -0
- package/dist/lib/selection.d.ts +3 -0
- package/dist/lib/selection.js +2 -0
- package/dist/lib/session-list.d.ts +16 -0
- package/dist/lib/session-list.js +7 -0
- package/dist/lib/sessions.d.ts +8 -0
- package/dist/lib/sessions.js +29 -0
- package/dist/lib/test/harness.d.ts +32 -0
- package/dist/lib/test/harness.js +139 -0
- package/dist/lib/test/setup.d.ts +4 -0
- package/dist/lib/test/setup.js +5 -0
- package/dist/lib/versions.d.ts +69 -0
- package/dist/lib/versions.js +250 -0
- package/dist/lib/viewport.d.ts +57 -0
- package/dist/lib/viewport.js +315 -0
- package/dist/lib/workspace-layout.d.ts +24 -0
- package/dist/lib/workspace-layout.js +145 -0
- package/dist/lib/workspace.d.ts +29 -0
- package/dist/lib/workspace.js +263 -0
- package/dist/theme.css +4577 -0
- package/package.json +32 -6
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
/**
|
|
3
|
+
* Reads, live inputs and edits shared by the panels over the official
|
|
4
|
+
* extensions: humanoid controls, expressions and visemes, and animations.
|
|
5
|
+
*/
|
|
6
|
+
import { ProtocolError } from "@catchlight/editor-core";
|
|
7
|
+
import { useCallback, useEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
|
|
8
|
+
import { Icon, Modal } from "./controls.js";
|
|
9
|
+
import { useRevision } from "./replica.js";
|
|
10
|
+
export const HUMANOID = "catchlight.humanoid";
|
|
11
|
+
export const EXPRESSIONS = "catchlight.expressions";
|
|
12
|
+
export const ANIMATIONS = "catchlight.animations";
|
|
13
|
+
export function useOfficialRead(session, command, result) {
|
|
14
|
+
const rev = useRevision(session);
|
|
15
|
+
const key = JSON.stringify(command);
|
|
16
|
+
return useMemo(() => {
|
|
17
|
+
let body;
|
|
18
|
+
try {
|
|
19
|
+
body = session.query(JSON.parse(key));
|
|
20
|
+
}
|
|
21
|
+
catch (cause) {
|
|
22
|
+
if (cause instanceof ProtocolError && cause.code === "extension_unread")
|
|
23
|
+
return { unread: cause.message };
|
|
24
|
+
throw cause;
|
|
25
|
+
}
|
|
26
|
+
if (body.result !== result)
|
|
27
|
+
throw new Error(`Could not read ${result}.`);
|
|
28
|
+
return { body: body };
|
|
29
|
+
}, [session, rev, key, result]);
|
|
30
|
+
}
|
|
31
|
+
export function useHumanoid(session) {
|
|
32
|
+
return useOfficialRead(session, { cmd: "humanoid_get" }, "humanoid");
|
|
33
|
+
}
|
|
34
|
+
export function useExpressions(session) {
|
|
35
|
+
return useOfficialRead(session, { cmd: "expression_list" }, "expressions");
|
|
36
|
+
}
|
|
37
|
+
export function useClips(session) {
|
|
38
|
+
return useOfficialRead(session, { cmd: "clip_list" }, "clips");
|
|
39
|
+
}
|
|
40
|
+
/** The findings about the extension under `key`, and the params they concern. */
|
|
41
|
+
export function useExtensionWarnings(session, key) {
|
|
42
|
+
const rev = useRevision(session);
|
|
43
|
+
return useMemo(() => {
|
|
44
|
+
const body = session.query({ cmd: "extension_warnings" });
|
|
45
|
+
const warnings = body.result === "extension_warnings" ? body.warnings.filter((w) => w.key === key) : [];
|
|
46
|
+
const params = new Map();
|
|
47
|
+
for (const warning of warnings) {
|
|
48
|
+
for (const subject of warning.subjects) {
|
|
49
|
+
const param = subjectParam(subject);
|
|
50
|
+
if (param !== undefined)
|
|
51
|
+
params.set(param, [...(params.get(param) ?? []), warning.message]);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return { warnings, params };
|
|
55
|
+
}, [session, rev, key]);
|
|
56
|
+
}
|
|
57
|
+
function subjectParam(subject) {
|
|
58
|
+
if ("param" in subject)
|
|
59
|
+
return subject.param;
|
|
60
|
+
if ("removed" in subject)
|
|
61
|
+
return subjectParam(subject.removed);
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
/** The messages of the warnings about any of `params`; empty when none. */
|
|
65
|
+
export function warningsOn(warned, params) {
|
|
66
|
+
const out = new Set();
|
|
67
|
+
for (const param of params)
|
|
68
|
+
for (const message of warned.get(param) ?? [])
|
|
69
|
+
out.add(message);
|
|
70
|
+
return [...out];
|
|
71
|
+
}
|
|
72
|
+
export function useLiveInputs(session) {
|
|
73
|
+
const read = useCallback(() => session.inputs(), [session]);
|
|
74
|
+
return useSyncExternalStore(session.onInvalidate, read, read);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* A playing clip moves params without invalidating, so this re-reads every
|
|
78
|
+
* animation frame while one plays and stops once none does. `select` keeps
|
|
79
|
+
* re-renders to what a caller shows: an unchanged value renders nothing.
|
|
80
|
+
*/
|
|
81
|
+
function usePlayingSelect(session, select, same) {
|
|
82
|
+
const latest = useRef({ select, same });
|
|
83
|
+
latest.current = { select, same };
|
|
84
|
+
const [value, setValue] = useState(() => select(session?.playing()));
|
|
85
|
+
useEffect(() => {
|
|
86
|
+
let frame = 0;
|
|
87
|
+
const read = () => {
|
|
88
|
+
const playing = session?.playing();
|
|
89
|
+
const next = latest.current.select(playing);
|
|
90
|
+
setValue((previous) => (latest.current.same(previous, next) ? previous : next));
|
|
91
|
+
if (playing && !frame)
|
|
92
|
+
frame = requestAnimationFrame(tick);
|
|
93
|
+
};
|
|
94
|
+
const tick = () => {
|
|
95
|
+
frame = 0;
|
|
96
|
+
read();
|
|
97
|
+
};
|
|
98
|
+
read();
|
|
99
|
+
if (!session)
|
|
100
|
+
return;
|
|
101
|
+
const off = session.onInvalidate(read);
|
|
102
|
+
return () => {
|
|
103
|
+
off();
|
|
104
|
+
if (frame)
|
|
105
|
+
cancelAnimationFrame(frame);
|
|
106
|
+
};
|
|
107
|
+
}, [session]);
|
|
108
|
+
return value;
|
|
109
|
+
}
|
|
110
|
+
/** The name of the clip that plays; `undefined` when none does. */
|
|
111
|
+
export function usePlayingClip(session) {
|
|
112
|
+
return usePlayingSelect(session, (p) => p?.name, Object.is);
|
|
113
|
+
}
|
|
114
|
+
/** Where the playing clip is, updated every frame while it plays. */
|
|
115
|
+
export function usePlayhead(session) {
|
|
116
|
+
return usePlayingSelect(session, (p) => p, samePlaying);
|
|
117
|
+
}
|
|
118
|
+
const samePlaying = (a, b) => a === b || (!!a && !!b && a.name === b.name && a.time === b.time && a.frame === b.frame);
|
|
119
|
+
/**
|
|
120
|
+
* Edits that may name a control, or key a zone or landmark, outside the
|
|
121
|
+
* humanoid standard. A refusal for
|
|
122
|
+
* that waits in `asking` until the person keeps the name (resent with
|
|
123
|
+
* `nonstandard`) or cancels; every other failure goes to `onError`.
|
|
124
|
+
*/
|
|
125
|
+
export function useOfficialCommand(session, onError) {
|
|
126
|
+
const [pending, setPending] = useState(0);
|
|
127
|
+
const [asking, setAsking] = useState();
|
|
128
|
+
const live = useRef({ session, onError });
|
|
129
|
+
live.current = { session, onError };
|
|
130
|
+
const run = useCallback(async (command, done) => {
|
|
131
|
+
setPending((n) => n + 1);
|
|
132
|
+
try {
|
|
133
|
+
const body = await live.current.session.send(command);
|
|
134
|
+
done?.();
|
|
135
|
+
return body;
|
|
136
|
+
}
|
|
137
|
+
catch (cause) {
|
|
138
|
+
if (cause instanceof ProtocolError &&
|
|
139
|
+
(cause.code === "nonstandard_control" || cause.code === "nonstandard_key") &&
|
|
140
|
+
"nonstandard" in command &&
|
|
141
|
+
!command.nonstandard) {
|
|
142
|
+
setAsking({ command, code: cause.code, message: cause.message, ...(done ? { done } : {}) });
|
|
143
|
+
}
|
|
144
|
+
else
|
|
145
|
+
live.current.onError(cause);
|
|
146
|
+
return undefined;
|
|
147
|
+
}
|
|
148
|
+
finally {
|
|
149
|
+
setPending((n) => n - 1);
|
|
150
|
+
}
|
|
151
|
+
}, []);
|
|
152
|
+
const keep = useCallback(() => {
|
|
153
|
+
if (!asking)
|
|
154
|
+
return;
|
|
155
|
+
setAsking(undefined);
|
|
156
|
+
void run({ ...asking.command, nonstandard: true }, asking.done);
|
|
157
|
+
}, [asking, run]);
|
|
158
|
+
const dismiss = useCallback(() => setAsking(undefined), []);
|
|
159
|
+
return { run, busy: pending > 0, asking, keep, dismiss };
|
|
160
|
+
}
|
|
161
|
+
export function NonstandardDialog({ asking, onKeep, onCancel, }) {
|
|
162
|
+
if (!asking)
|
|
163
|
+
return null;
|
|
164
|
+
const key = asking.code === "nonstandard_key";
|
|
165
|
+
return (_jsxs(Modal, { title: key ? "Keep a key outside the standard?" : "Keep a control outside the standard?", onClose: onCancel, children: [_jsxs("p", { "data-catchlight-hint": "", children: [capitalize(asking.message), ".", " ", key
|
|
166
|
+
? "For a key of your own, cancel and add a dot. Keep this one only if it comes from a newer standard."
|
|
167
|
+
: "Apps that speak the humanoid standard will not drive it."] }), _jsxs("div", { "data-catchlight-dialog-actions": "", children: [_jsx("button", { type: "button", onClick: onCancel, children: "Cancel" }), _jsx("button", { type: "button", "data-primary": "", onClick: onKeep, children: "Keep the name" })] })] }));
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* In the style of the Model tab's health list. Folded when there are more
|
|
171
|
+
* than two, so a model with many findings still shows its rows.
|
|
172
|
+
*/
|
|
173
|
+
export function ExtensionWarnings({ warnings }) {
|
|
174
|
+
if (!warnings.length)
|
|
175
|
+
return null;
|
|
176
|
+
return (_jsxs("details", { "data-catchlight-extension-warnings": "", open: warnings.length <= 2, children: [_jsxs("summary", { children: [_jsx(Icon, { name: "warning", width: "13", height: "13" }), plural(warnings.length, "warning"), _jsx(Icon, { name: "chevron", width: "12", height: "12", "data-catchlight-chevron": "" })] }), _jsx("div", { "data-catchlight-extension-warning-list": "", children: warnings.map((w, i) => (_jsxs("p", { "data-catchlight-warning": "", "data-code": w.code, children: [_jsx(Icon, { name: "warning" }), capitalize(w.message)] }, i))) })] }));
|
|
177
|
+
}
|
|
178
|
+
export function UnreadNotice({ what, why }) {
|
|
179
|
+
return (_jsxs("div", { "data-catchlight-extension-unread": "", role: "status", children: [_jsxs("p", { "data-catchlight-warning": "", children: [_jsx(Icon, { name: "warning" }), "This build cannot read the model\u2019s ", what, ", so they cannot be edited here. They are kept and saved as they are."] }), _jsx("p", { "data-catchlight-hint": "", children: capitalize(why) })] }));
|
|
180
|
+
}
|
|
181
|
+
export function capitalize(text) {
|
|
182
|
+
return text.charAt(0).toLocaleUpperCase() + text.slice(1);
|
|
183
|
+
}
|
|
184
|
+
export function plural(count, one, many = `${one}s`) {
|
|
185
|
+
return `${count} ${count === 1 ? one : many}`;
|
|
186
|
+
}
|
|
187
|
+
/** A question mark whose tooltip explains what sits beside it. */
|
|
188
|
+
export function HelpMark({ text }) {
|
|
189
|
+
return (_jsx("span", { "data-catchlight-help-mark": "", role: "img", tabIndex: 0, "aria-label": text, title: text, children: _jsx(Icon, { name: "help", width: "13", height: "13" }) }));
|
|
190
|
+
}
|
|
191
|
+
/** Marks a card or row that a warning concerns; the messages are its label. */
|
|
192
|
+
export function WarningMark({ warnings }) {
|
|
193
|
+
if (!warnings.length)
|
|
194
|
+
return null;
|
|
195
|
+
const label = warnings.map(capitalize).join("\n");
|
|
196
|
+
return (_jsx("span", { "data-catchlight-warning-mark": "", role: "img", "aria-label": label, title: label, children: _jsx(Icon, { name: "warning", width: "13", height: "13" }) }));
|
|
197
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Param and binding edits as commands. Posing is `session.setParam` and stays
|
|
3
|
+
* out of this hook. A binding is addressed by node, target and params, never a
|
|
4
|
+
* local handle, so it stays valid across other clients' edits.
|
|
5
|
+
*/
|
|
6
|
+
import type { BindingParams, BindingTarget, Interpolate, NodeId, ParamId, ResponseBody, ScalarTarget, Session } from "@catchlight/editor-core";
|
|
7
|
+
/** `[x, y]`; `y` is 0 for a one-param binding. */
|
|
8
|
+
export type BindingCell = [number, number];
|
|
9
|
+
export type BindingAddress = BindingParams & {
|
|
10
|
+
node: NodeId;
|
|
11
|
+
target: BindingTarget;
|
|
12
|
+
};
|
|
13
|
+
/** The range defaults to 0..1. */
|
|
14
|
+
export interface NewParam {
|
|
15
|
+
name: string;
|
|
16
|
+
min?: number;
|
|
17
|
+
max?: number;
|
|
18
|
+
default?: number;
|
|
19
|
+
}
|
|
20
|
+
/** An absent field is left alone. */
|
|
21
|
+
export interface ParamPatch {
|
|
22
|
+
name?: string;
|
|
23
|
+
min?: number;
|
|
24
|
+
max?: number;
|
|
25
|
+
default?: number;
|
|
26
|
+
}
|
|
27
|
+
export interface ParamActions {
|
|
28
|
+
add(param: NewParam): Promise<ParamId>;
|
|
29
|
+
/** Key positions are normalized, so a range change does not move them. */
|
|
30
|
+
set(param: ParamId, patch: ParamPatch): Promise<ResponseBody>;
|
|
31
|
+
remove(param: ParamId): Promise<ResponseBody>;
|
|
32
|
+
insertKey(binding: BindingAddress, axis: ParamId, value: number): Promise<ResponseBody>;
|
|
33
|
+
deleteKey(binding: BindingAddress, axis: ParamId, index: number): Promise<ResponseBody>;
|
|
34
|
+
moveKey(binding: BindingAddress, axis: ParamId, index: number, value: number, ifRev?: number): Promise<ResponseBody>;
|
|
35
|
+
/** Creates an everywhere-unset binding. */
|
|
36
|
+
addBinding(node: NodeId, target: ScalarTarget, params: BindingParams): Promise<ResponseBody>;
|
|
37
|
+
deleteBinding(node: NodeId, target: BindingTarget, params: BindingParams): Promise<ResponseBody>;
|
|
38
|
+
/** Creates the binding if this is its first key. */
|
|
39
|
+
setKey(node: NodeId, target: ScalarTarget, params: BindingParams, cell: BindingCell, value: number): Promise<ResponseBody>;
|
|
40
|
+
/** 1 for scale and opacity, 0 otherwise. */
|
|
41
|
+
resetKey(node: NodeId, target: BindingTarget, params: BindingParams, cell: BindingCell): Promise<ResponseBody>;
|
|
42
|
+
/** The model derives the cell again. */
|
|
43
|
+
unsetKey(node: NodeId, target: BindingTarget, params: BindingParams, cell: BindingCell): Promise<ResponseBody>;
|
|
44
|
+
interpolate(node: NodeId, target: BindingTarget, params: BindingParams, mode: Interpolate): Promise<ResponseBody>;
|
|
45
|
+
invert(node: NodeId, target: BindingTarget, params: BindingParams): Promise<ResponseBody>;
|
|
46
|
+
copyKey(node: NodeId, target: BindingTarget, params: BindingParams, from: BindingCell, to: BindingCell): Promise<ResponseBody>;
|
|
47
|
+
}
|
|
48
|
+
export declare function useParamActions(session: Session): ParamActions;
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Param and binding edits as commands. Posing is `session.setParam` and stays
|
|
3
|
+
* out of this hook. A binding is addressed by node, target and params, never a
|
|
4
|
+
* local handle, so it stays valid across other clients' edits.
|
|
5
|
+
*/
|
|
6
|
+
import { useMemo } from "react";
|
|
7
|
+
export function useParamActions(session) {
|
|
8
|
+
return useMemo(() => ({
|
|
9
|
+
async add(param) {
|
|
10
|
+
const body = await session.send({
|
|
11
|
+
cmd: "param_add",
|
|
12
|
+
name: param.name,
|
|
13
|
+
min: param.min ?? 0,
|
|
14
|
+
max: param.max ?? 1,
|
|
15
|
+
default: param.default ?? 0,
|
|
16
|
+
});
|
|
17
|
+
if (body.result !== "param") {
|
|
18
|
+
throw new Error(`param_add answered ${body.result}, not the param it added`);
|
|
19
|
+
}
|
|
20
|
+
return body.param;
|
|
21
|
+
},
|
|
22
|
+
set(param, patch) {
|
|
23
|
+
// Absent and null both mean "leave alone" to the editor.
|
|
24
|
+
return session.send({
|
|
25
|
+
cmd: "param_set",
|
|
26
|
+
param,
|
|
27
|
+
name: patch.name ?? null,
|
|
28
|
+
min: patch.min ?? null,
|
|
29
|
+
max: patch.max ?? null,
|
|
30
|
+
default: patch.default ?? null,
|
|
31
|
+
});
|
|
32
|
+
},
|
|
33
|
+
remove(param) {
|
|
34
|
+
return session.send({ cmd: "param_delete", param });
|
|
35
|
+
},
|
|
36
|
+
insertKey(binding, axis, value) {
|
|
37
|
+
return session.send({ cmd: "binding_key_insert", ...binding, axis, value, if_rev: session.getRevision() });
|
|
38
|
+
},
|
|
39
|
+
deleteKey(binding, axis, index) {
|
|
40
|
+
return session.send({ cmd: "binding_key_delete", ...binding, axis, index, if_rev: session.getRevision() });
|
|
41
|
+
},
|
|
42
|
+
moveKey(binding, axis, index, value, ifRev) {
|
|
43
|
+
return session.send({ cmd: "binding_key_move", ...binding, axis, index, value, if_rev: ifRev ?? session.getRevision() });
|
|
44
|
+
},
|
|
45
|
+
addBinding(node, target, params) {
|
|
46
|
+
return session.send({ cmd: "binding_add", node, target, ...params });
|
|
47
|
+
},
|
|
48
|
+
deleteBinding(node, target, params) {
|
|
49
|
+
return session.send({ cmd: "binding_delete", node, target, ...params });
|
|
50
|
+
},
|
|
51
|
+
setKey(node, target, params, cell, value) {
|
|
52
|
+
return session.send({ cmd: "edit_apply", if_rev: session.getRevision(), edits: [
|
|
53
|
+
{ op: "binding_add", node, target, ...params, key_positions: null },
|
|
54
|
+
{ op: "binding_cells_set", node, target, ...params, cells: [{ cell, value: { scalar: value } }] },
|
|
55
|
+
] });
|
|
56
|
+
},
|
|
57
|
+
resetKey(node, target, params, cell) {
|
|
58
|
+
const binding = session.bindings(node).find((b) => b.target === target && b.param === params.param && b.param_y == params.param_y);
|
|
59
|
+
if (!binding)
|
|
60
|
+
return Promise.reject(new Error("The binding no longer exists."));
|
|
61
|
+
const identity = binding.identity;
|
|
62
|
+
const value = "scalar" in identity
|
|
63
|
+
? { scalar: identity.scalar }
|
|
64
|
+
: { offsets: Array.from({ length: identity.vertex_count }, () => [...identity.offset]) };
|
|
65
|
+
return writeCells(session, { node, target, ...params }, [{ cell, value }]);
|
|
66
|
+
},
|
|
67
|
+
unsetKey(node, target, params, cell) {
|
|
68
|
+
return session.send({ cmd: "binding_cells_unset", node, target, cells: [cell], ...params, if_rev: session.getRevision() });
|
|
69
|
+
},
|
|
70
|
+
interpolate(node, target, params, mode) {
|
|
71
|
+
return session.send({ cmd: "binding_interpolation_set", node, target, mode, ...params });
|
|
72
|
+
},
|
|
73
|
+
invert(node, target, params) {
|
|
74
|
+
const address = { node, target, ...params };
|
|
75
|
+
const binding = session.bindings(node).find((b) => b.target === target && b.param === params.param && b.param_y == params.param_y);
|
|
76
|
+
if (!binding)
|
|
77
|
+
return Promise.reject(new Error("The binding no longer exists."));
|
|
78
|
+
const cells = binding.authored.flatMap((row, y) => row.flatMap((authored, x) => authored ? [[x, y]] : []));
|
|
79
|
+
if (cells.length === 0)
|
|
80
|
+
return Promise.resolve({ result: "empty" });
|
|
81
|
+
const read = readCells(session, address, cells, false);
|
|
82
|
+
return writeCells(session, address, read.map(({ cell, value }) => ({ cell, value: "scalar" in value
|
|
83
|
+
? { scalar: -value.scalar }
|
|
84
|
+
: { offsets: value.offsets.map(([x, y]) => [-x, -y]) } })));
|
|
85
|
+
},
|
|
86
|
+
copyKey(node, target, params, from, to) {
|
|
87
|
+
const address = { node, target, ...params };
|
|
88
|
+
const source = readCells(session, address, [from], true)[0];
|
|
89
|
+
if (!source)
|
|
90
|
+
return Promise.reject(new Error("The source key has no value."));
|
|
91
|
+
return writeCells(session, address, [{ cell: to, value: source.value }]);
|
|
92
|
+
},
|
|
93
|
+
}), [session]);
|
|
94
|
+
}
|
|
95
|
+
function readCells(session, binding, cells, includeDerived) {
|
|
96
|
+
const read = session.query({ cmd: "binding_cells_get", ...binding, cells, include_derived: includeDerived, if_rev: session.getRevision() });
|
|
97
|
+
if (read.result !== "binding_cells")
|
|
98
|
+
throw new Error("The binding cells could not be read.");
|
|
99
|
+
return read.cells.flatMap((cell) => {
|
|
100
|
+
const value = cell.value ?? cell.derived;
|
|
101
|
+
return value ? [{ cell: cell.cell, value }] : [];
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
function writeCells(session, binding, cells) {
|
|
105
|
+
return session.send({ cmd: "binding_cells_set", ...binding, cells, if_rev: session.getRevision() });
|
|
106
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The param list, plus the separate add form and per-param fields. Fields are
|
|
3
|
+
* uncontrolled, keyed on their source values, and commit on blur or Enter so
|
|
4
|
+
* typing "0.25" is one edit, not four.
|
|
5
|
+
*/
|
|
6
|
+
import type { ParamInfo, Session } from "@catchlight/editor-core";
|
|
7
|
+
import type { ComponentProps, ReactNode } from "react";
|
|
8
|
+
type ErrorSink = ((cause: unknown) => void) | undefined;
|
|
9
|
+
export interface ParamListRootProps extends Omit<ComponentProps<"div">, "children"> {
|
|
10
|
+
session: Session;
|
|
11
|
+
children?: (param: ParamInfo) => ReactNode;
|
|
12
|
+
}
|
|
13
|
+
export declare function ParamListRoot({ session, children, ...rest }: ParamListRootProps): import("react").JSX.Element;
|
|
14
|
+
export interface ParamAddRootProps extends Omit<ComponentProps<"div">, "children" | "onError"> {
|
|
15
|
+
session: Session;
|
|
16
|
+
onError?: ErrorSink;
|
|
17
|
+
}
|
|
18
|
+
/** The editor keys a new param at both endpoints. */
|
|
19
|
+
export declare function ParamAddRoot({ session, onError, ...rest }: ParamAddRootProps): import("react").JSX.Element;
|
|
20
|
+
export interface ParamFieldsRootProps extends Omit<ComponentProps<"div">, "children" | "onError"> {
|
|
21
|
+
session: Session;
|
|
22
|
+
param: ParamInfo;
|
|
23
|
+
onError?: ErrorSink;
|
|
24
|
+
}
|
|
25
|
+
/** Deleting a param also deletes every binding it drove. */
|
|
26
|
+
export declare function ParamFieldsRoot({ session, param, onError, ...rest }: ParamFieldsRootProps): import("react").JSX.Element;
|
|
27
|
+
export declare const ParamList: {
|
|
28
|
+
Root: typeof ParamListRoot;
|
|
29
|
+
};
|
|
30
|
+
export declare const ParamAdd: {
|
|
31
|
+
Root: typeof ParamAddRoot;
|
|
32
|
+
};
|
|
33
|
+
export declare const ParamFields: {
|
|
34
|
+
Root: typeof ParamFieldsRoot;
|
|
35
|
+
};
|
|
36
|
+
export {};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useState } from "react";
|
|
3
|
+
import { useParamActions } from "./param-actions.js";
|
|
4
|
+
import { ParamSliderRoot } from "./param-slider.js";
|
|
5
|
+
import { useParams } from "./replica.js";
|
|
6
|
+
export function ParamListRoot({ session, children, ...rest }) {
|
|
7
|
+
const params = useParams(session);
|
|
8
|
+
return (_jsx("div", { role: "list", "data-catchlight-param-list": "", ...rest, children: params.map((param) => (_jsx("div", { role: "listitem", "data-catchlight-param-item": "", "data-param": param.id, children: children ? children(param) : _jsx(ParamSliderRoot, { session: session, param: param }) }, param.id))) }));
|
|
9
|
+
}
|
|
10
|
+
/** The editor keys a new param at both endpoints. */
|
|
11
|
+
export function ParamAddRoot({ session, onError, ...rest }) {
|
|
12
|
+
const actions = useParamActions(session);
|
|
13
|
+
const [name, setName] = useState("");
|
|
14
|
+
const [min, setMin] = useState("0");
|
|
15
|
+
const [max, setMax] = useState("1");
|
|
16
|
+
const [fallback, setFallback] = useState("0");
|
|
17
|
+
const submit = () => {
|
|
18
|
+
const label = name.trim();
|
|
19
|
+
if (label === "")
|
|
20
|
+
return;
|
|
21
|
+
setName("");
|
|
22
|
+
report(onError, actions.add({
|
|
23
|
+
name: label,
|
|
24
|
+
min: number(min, 0),
|
|
25
|
+
max: number(max, 1),
|
|
26
|
+
default: number(fallback, 0),
|
|
27
|
+
}));
|
|
28
|
+
};
|
|
29
|
+
return (_jsxs("div", { "data-catchlight-param-add": "", ...rest, children: [_jsx("input", { "data-catchlight-param-add-name": "", "aria-label": "New param name", placeholder: "New param", value: name, onInput: (event) => setName(event.currentTarget.value), onChange: noop, onKeyDown: (event) => {
|
|
30
|
+
if (event.key === "Enter")
|
|
31
|
+
submit();
|
|
32
|
+
} }), _jsx("input", { type: "number", step: "any", "data-catchlight-param-add-min": "", "aria-label": "Minimum", value: min, onInput: (event) => setMin(event.currentTarget.value), onChange: noop }), _jsx("input", { type: "number", step: "any", "data-catchlight-param-add-max": "", "aria-label": "Maximum", value: max, onInput: (event) => setMax(event.currentTarget.value), onChange: noop }), _jsx("input", { type: "number", step: "any", "data-catchlight-param-add-default": "", "aria-label": "Default", value: fallback, onInput: (event) => setFallback(event.currentTarget.value), onChange: noop }), _jsx("button", { type: "button", "data-catchlight-param-add-submit": "", disabled: name.trim() === "", onClick: submit, children: "Add" })] }));
|
|
33
|
+
}
|
|
34
|
+
/** Deleting a param also deletes every binding it drove. */
|
|
35
|
+
export function ParamFieldsRoot({ session, param, onError, ...rest }) {
|
|
36
|
+
const actions = useParamActions(session);
|
|
37
|
+
const stamp = `${param.name}|${param.min}|${param.max}|${param.default}`;
|
|
38
|
+
const rename = (value) => {
|
|
39
|
+
const name = value.trim();
|
|
40
|
+
if (name === "" || name === param.name)
|
|
41
|
+
return;
|
|
42
|
+
report(onError, actions.set(param.id, { name }));
|
|
43
|
+
};
|
|
44
|
+
const setNumber = (field, value) => {
|
|
45
|
+
const next = Number(value);
|
|
46
|
+
if (!Number.isFinite(next) || next === param[field])
|
|
47
|
+
return;
|
|
48
|
+
report(onError, actions.set(param.id, { [field]: next }));
|
|
49
|
+
};
|
|
50
|
+
return (_jsxs("div", { "data-catchlight-param-fields": "", "data-param": param.id, ...rest, children: [_jsx("input", { "data-catchlight-param-rename": "", "aria-label": `${param.name} name`, defaultValue: param.name, onBlur: (event) => rename(event.currentTarget.value), onKeyDown: commitOnEnter }, `name:${stamp}`), ["min", "max", "default"].map((field) => (_jsx("input", { type: "number", step: "any", "data-catchlight-param-field": field, "aria-label": `${param.name} ${field}`, defaultValue: param[field], onBlur: (event) => setNumber(field, event.currentTarget.value), onKeyDown: commitOnEnter }, `${field}:${stamp}`))), _jsx("button", { type: "button", "data-catchlight-param-delete": "", title: `Delete ${param.name} and its ${param.bindings} binding(s)`, onClick: () => report(onError, actions.remove(param.id)), children: "\u00D7" })] }));
|
|
51
|
+
}
|
|
52
|
+
export const ParamList = { Root: ParamListRoot };
|
|
53
|
+
export const ParamAdd = { Root: ParamAddRoot };
|
|
54
|
+
export const ParamFields = { Root: ParamFieldsRoot };
|
|
55
|
+
// React requires a controlled input's `onChange`; handling the native input
|
|
56
|
+
// event once through `onInput` keeps it from firing twice.
|
|
57
|
+
function noop() { }
|
|
58
|
+
function commitOnEnter(event) {
|
|
59
|
+
if (event.key === "Enter")
|
|
60
|
+
event.currentTarget.blur();
|
|
61
|
+
}
|
|
62
|
+
function number(text, fallback) {
|
|
63
|
+
const value = Number(text);
|
|
64
|
+
return Number.isFinite(value) ? value : fallback;
|
|
65
|
+
}
|
|
66
|
+
function report(onError, work) {
|
|
67
|
+
void work.catch((cause) => {
|
|
68
|
+
if (onError)
|
|
69
|
+
onError(cause);
|
|
70
|
+
else
|
|
71
|
+
console.warn("catchlight: the param edit failed", cause);
|
|
72
|
+
});
|
|
73
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One param's range input and key-position strip. Posing is not an edit: the
|
|
3
|
+
* value comes from the replica's pose on the repaint channel, not a revision.
|
|
4
|
+
*/
|
|
5
|
+
import type { BindingInfo, NodeId, ParamInfo, Session } from "@catchlight/editor-core";
|
|
6
|
+
import type { ComponentProps } from "react";
|
|
7
|
+
type ErrorSink = ((cause: unknown) => void) | undefined;
|
|
8
|
+
export interface ParamSliderRootProps extends Omit<ComponentProps<"input">, "type" | "value" | "min" | "max" | "step"> {
|
|
9
|
+
session: Session;
|
|
10
|
+
param: ParamInfo;
|
|
11
|
+
}
|
|
12
|
+
export declare function ParamSliderRoot({ session, param, onInput, disabled, ...rest }: ParamSliderRootProps): import("react").JSX.Element;
|
|
13
|
+
export interface ParamKeysRootProps extends Omit<ComponentProps<"div">, "children" | "onError"> {
|
|
14
|
+
session: Session;
|
|
15
|
+
param: ParamInfo;
|
|
16
|
+
onError?: ErrorSink;
|
|
17
|
+
/** Grid being edited. Without one, this is a pose-only range/default strip. */
|
|
18
|
+
binding?: BindingInfo;
|
|
19
|
+
node?: NodeId;
|
|
20
|
+
}
|
|
21
|
+
/** Binding-local key positions, or range/default pose shortcuts when no
|
|
22
|
+
* binding is selected. Clicking poses; dragging a binding marker commits once.
|
|
23
|
+
* Insert/delete affect only this binding's chosen axis. At least one position
|
|
24
|
+
* remains, and endpoints can be moved or removed like other positions. */
|
|
25
|
+
export declare function ParamKeysRoot({ session, param, binding, node, onError, ...rest }: ParamKeysRootProps): import("react").JSX.Element;
|
|
26
|
+
export declare const ParamSlider: {
|
|
27
|
+
Root: typeof ParamSliderRoot;
|
|
28
|
+
};
|
|
29
|
+
export declare const ParamKeys: {
|
|
30
|
+
Root: typeof ParamKeysRoot;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* A motion response moves its output every frame without invalidating, so a
|
|
34
|
+
* param it holds is re-read each animation frame; React re-renders only when
|
|
35
|
+
* the value changed.
|
|
36
|
+
*/
|
|
37
|
+
export declare function useParamValue(session: Session, param: ParamInfo): number;
|
|
38
|
+
/** This tab's own pause; editing modes pause motion without setting it. */
|
|
39
|
+
export declare function useMotionPaused(session: Session | undefined): boolean;
|
|
40
|
+
/** Neither paused nor in an editing mode. */
|
|
41
|
+
export declare function useMotionRunning(session: Session | undefined): boolean;
|
|
42
|
+
/** Whether a motion response writes `param` now, so posing it would be overridden. */
|
|
43
|
+
export declare function useHeldByMotion(session: Session | undefined, param: ParamInfo | undefined): boolean;
|
|
44
|
+
export {};
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useCallback, useRef, useState, useSyncExternalStore } from "react";
|
|
3
|
+
import { keyIndexNear, normalizedValue, valueAtKey } from "./bindings.js";
|
|
4
|
+
import { useParamActions } from "./param-actions.js";
|
|
5
|
+
export function ParamSliderRoot({ session, param, onInput, disabled, ...rest }) {
|
|
6
|
+
const value = useParamValue(session, param);
|
|
7
|
+
const held = useHeldByMotion(session, param);
|
|
8
|
+
const handleInput = (event) => {
|
|
9
|
+
const next = Number(event.currentTarget.value);
|
|
10
|
+
if (Number.isFinite(next))
|
|
11
|
+
session.setParam(param.id, next);
|
|
12
|
+
onInput?.(event);
|
|
13
|
+
};
|
|
14
|
+
return (_jsx("input", { type: "range", "data-catchlight-param-slider": "", "data-param": param.id, "aria-label": param.name, min: param.min, max: param.max,
|
|
15
|
+
// A param is continuous. `key_positions` says where its bindings sample
|
|
16
|
+
// it, not where the slider is allowed to stop.
|
|
17
|
+
step: "any", value: value, onInput: handleInput, onChange: noop, disabled: held || disabled, "data-held": held ? "" : undefined, ...rest }));
|
|
18
|
+
}
|
|
19
|
+
/** Binding-local key positions, or range/default pose shortcuts when no
|
|
20
|
+
* binding is selected. Clicking poses; dragging a binding marker commits once.
|
|
21
|
+
* Insert/delete affect only this binding's chosen axis. At least one position
|
|
22
|
+
* remains, and endpoints can be moved or removed like other positions. */
|
|
23
|
+
export function ParamKeysRoot({ session, param, binding, node, onError, ...rest }) {
|
|
24
|
+
const actions = useParamActions(session);
|
|
25
|
+
const value = useParamValue(session, param);
|
|
26
|
+
const track = useRef(null);
|
|
27
|
+
const [drag, setDrag] = useState(undefined);
|
|
28
|
+
const suppressClick = useRef(false);
|
|
29
|
+
const positions = binding?.key_positions[binding.param === param.id ? 0 : 1]
|
|
30
|
+
?? [...new Set([0, normalizedValue(param, param.default), 1])].sort((a, b) => a - b);
|
|
31
|
+
const address = binding && node ? { node, target: binding.target, param: binding.param, param_y: binding.param_y ?? null } : undefined;
|
|
32
|
+
const last = positions.length - 1;
|
|
33
|
+
const at = normalizedValue(param, value);
|
|
34
|
+
const on = keyIndexNear(param, value, positions);
|
|
35
|
+
const editable = (index) => !!address && index >= 0 && index <= last;
|
|
36
|
+
/** On the 0..1 scale key positions use. */
|
|
37
|
+
const positionOf = (event) => {
|
|
38
|
+
const box = track.current?.getBoundingClientRect();
|
|
39
|
+
if (!box || box.width <= 0)
|
|
40
|
+
return at;
|
|
41
|
+
return Math.min(1, Math.max(0, (event.clientX - box.left) / box.width));
|
|
42
|
+
};
|
|
43
|
+
const down = (event, index) => {
|
|
44
|
+
suppressClick.current = false;
|
|
45
|
+
if (!editable(index))
|
|
46
|
+
return;
|
|
47
|
+
event.currentTarget.setPointerCapture(event.pointerId);
|
|
48
|
+
setDrag({ index, at: positions[index] ?? 0, moved: false, revision: session.getRevision() });
|
|
49
|
+
};
|
|
50
|
+
const move = (event, index) => {
|
|
51
|
+
if (!drag || drag.index !== index)
|
|
52
|
+
return;
|
|
53
|
+
const to = between(positions, index, positionOf(event));
|
|
54
|
+
const budged = Math.abs(to - (positions[index] ?? 0)) > 1e-4;
|
|
55
|
+
setDrag({ ...drag, index, at: to, moved: drag.moved || budged });
|
|
56
|
+
};
|
|
57
|
+
const up = (event, index) => {
|
|
58
|
+
const held = drag;
|
|
59
|
+
setDrag(undefined);
|
|
60
|
+
if (event.currentTarget.hasPointerCapture(event.pointerId)) {
|
|
61
|
+
event.currentTarget.releasePointerCapture(event.pointerId);
|
|
62
|
+
}
|
|
63
|
+
if (!held || held.index !== index || !held.moved) {
|
|
64
|
+
session.setParam(param.id, valueAtKey(param, index, positions));
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
suppressClick.current = true;
|
|
68
|
+
if (address)
|
|
69
|
+
report(onError, actions.moveKey(address, param.id, index, held.at, held.revision).then(() => {
|
|
70
|
+
session.setParam(param.id, param.min + held.at * (param.max - param.min));
|
|
71
|
+
}));
|
|
72
|
+
};
|
|
73
|
+
return (_jsxs("div", { "data-catchlight-param-keys": "", "data-param": param.id, ...rest, children: [_jsx("div", { ref: track, "data-catchlight-param-key-track": "", children: positions.map((position, index) => (_jsx("button", { type: "button", "data-catchlight-param-key": "", "data-index": index, "data-editable": editable(index) ? "" : undefined, "data-current": on === index ? "" : undefined, "data-dragging": drag?.index === index ? "" : undefined, style: {
|
|
74
|
+
left: `${(drag?.index === index ? drag.at : position) * 100}%`,
|
|
75
|
+
}, "aria-label": `${param.name} key ${index}`, title: `${valueAtKey(param, index, positions)}${address ? " · Drag to move this binding’s key position" : " · Click to pose"}`, onPointerDown: (event) => down(event, index), onPointerMove: (event) => move(event, index), onPointerUp: (event) => up(event, index), onPointerCancel: () => setDrag(undefined), onLostPointerCapture: () => setDrag(undefined), onClick: () => {
|
|
76
|
+
if (suppressClick.current) {
|
|
77
|
+
suppressClick.current = false;
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
session.setParam(param.id, valueAtKey(param, index, positions));
|
|
81
|
+
} }, index))) }), address && _jsxs("div", { "data-catchlight-param-key-actions": "", children: [_jsx("button", { type: "button", "data-catchlight-param-key-insert": "", "aria-label": `Add ${param.name} key position`,
|
|
82
|
+
// A binding may omit either endpoint; insert any missing position.
|
|
83
|
+
disabled: on !== undefined, title: "Add a key at the current value", onClick: () => report(onError, actions.insertKey(address, param.id, at)), children: "+" }), _jsx("button", { type: "button", "data-catchlight-param-key-delete": "", "aria-label": `Delete ${param.name} key position`, disabled: on === undefined || positions.length < 2, title: "Delete the key at the current value", onClick: () => {
|
|
84
|
+
if (on !== undefined)
|
|
85
|
+
report(onError, actions.deleteKey(address, param.id, on));
|
|
86
|
+
}, children: "\u2212" })] })] }));
|
|
87
|
+
}
|
|
88
|
+
export const ParamSlider = { Root: ParamSliderRoot };
|
|
89
|
+
export const ParamKeys = { Root: ParamKeysRoot };
|
|
90
|
+
// Clamped strictly between the neighbours, where the model requires a moved key
|
|
91
|
+
// to land, so a drag stops at a neighbour instead of failing.
|
|
92
|
+
function between(positions, index, to) {
|
|
93
|
+
const gap = 1e-4;
|
|
94
|
+
const low = index === 0 ? 0 : positions[index - 1] + gap;
|
|
95
|
+
const high = index === positions.length - 1 ? 1 : positions[index + 1] - gap;
|
|
96
|
+
if (low >= high)
|
|
97
|
+
return positions[index] ?? to;
|
|
98
|
+
return Math.min(high, Math.max(low, to));
|
|
99
|
+
}
|
|
100
|
+
function report(onError, work) {
|
|
101
|
+
void work.catch((cause) => {
|
|
102
|
+
if (onError)
|
|
103
|
+
onError(cause);
|
|
104
|
+
else
|
|
105
|
+
console.warn("catchlight: the param edit failed", cause);
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* A motion response moves its output every frame without invalidating, so a
|
|
110
|
+
* param it holds is re-read each animation frame; React re-renders only when
|
|
111
|
+
* the value changed.
|
|
112
|
+
*/
|
|
113
|
+
export function useParamValue(session, param) {
|
|
114
|
+
const held = useHeldByMotion(session, param);
|
|
115
|
+
const read = useCallback(() => session.paramValue(param.id) ?? param.default, [session, param.id, param.default]);
|
|
116
|
+
const subscribe = useCallback((changed) => {
|
|
117
|
+
const off = session.onInvalidate(changed);
|
|
118
|
+
if (!held)
|
|
119
|
+
return off;
|
|
120
|
+
let frame = requestAnimationFrame(function tick() {
|
|
121
|
+
changed();
|
|
122
|
+
frame = requestAnimationFrame(tick);
|
|
123
|
+
});
|
|
124
|
+
return () => {
|
|
125
|
+
off();
|
|
126
|
+
cancelAnimationFrame(frame);
|
|
127
|
+
};
|
|
128
|
+
}, [session, held]);
|
|
129
|
+
return useSyncExternalStore(subscribe, read, read);
|
|
130
|
+
}
|
|
131
|
+
const never = () => false;
|
|
132
|
+
const noSubscription = () => noop;
|
|
133
|
+
/** This tab's own pause; editing modes pause motion without setting it. */
|
|
134
|
+
export function useMotionPaused(session) {
|
|
135
|
+
const read = useCallback(() => session?.motionPaused() ?? false, [session]);
|
|
136
|
+
return useSyncExternalStore(session?.onInvalidate ?? noSubscription, session ? read : never);
|
|
137
|
+
}
|
|
138
|
+
/** Neither paused nor in an editing mode. */
|
|
139
|
+
export function useMotionRunning(session) {
|
|
140
|
+
const read = useCallback(() => session?.motionRunning() ?? false, [session]);
|
|
141
|
+
return useSyncExternalStore(session?.onInvalidate ?? noSubscription, session ? read : never);
|
|
142
|
+
}
|
|
143
|
+
/** Whether a motion response writes `param` now, so posing it would be overridden. */
|
|
144
|
+
export function useHeldByMotion(session, param) {
|
|
145
|
+
return useMotionRunning(session) && !!param?.driven_by;
|
|
146
|
+
}
|
|
147
|
+
// React requires a controlled input's `onChange`; handling the native input
|
|
148
|
+
// event once through `onInput` keeps a host's `onInput` from firing twice.
|
|
149
|
+
function noop() { }
|