@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,415 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
/** Only a mesh draft and an armed recording destination live in React state.
|
|
3
|
+
* Every route out of a dirty draft goes through the same finish dialog. */
|
|
4
|
+
import { MeshDraft, RecordingGesture } from "@catchlight/editor-core";
|
|
5
|
+
import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState, useSyncExternalStore, } from "react";
|
|
6
|
+
import { useSelection } from "./selection.js";
|
|
7
|
+
import { bindingCellAt, keyIndexNear, normalizedValue, valueAtKey } from "./bindings.js";
|
|
8
|
+
import { Modal } from "./controls.js";
|
|
9
|
+
const noop = () => () => { };
|
|
10
|
+
const zero = () => 0;
|
|
11
|
+
function useEditingController(session, onError, onNotice) {
|
|
12
|
+
const { node } = useSelection();
|
|
13
|
+
const revision = useSyncExternalStore(session?.subscribe ?? noop, session?.getRevision ?? zero, zero);
|
|
14
|
+
const params = useMemo(() => session?.params() ?? [], [session, revision]);
|
|
15
|
+
const info = useMemo(() => (node ? session?.nodeInfo(node) : undefined), [session, node, revision]);
|
|
16
|
+
const bindings = useMemo(() => (node ? (session?.bindings(node) ?? []) : []), [session, node, revision]);
|
|
17
|
+
const [mode, setMode] = useState("arrange");
|
|
18
|
+
const [param, setParam] = useState();
|
|
19
|
+
const [pair, setPair] = useState();
|
|
20
|
+
const [recordTool, setRecordTool] = useState("shape");
|
|
21
|
+
const [armed, setArmed] = useState();
|
|
22
|
+
const [busy, setBusy] = useState(false);
|
|
23
|
+
const [mesh, setMesh] = useState();
|
|
24
|
+
const draft = useRef(undefined);
|
|
25
|
+
const [meshError, setMeshError] = useState();
|
|
26
|
+
const [meshSelection, setMeshSelection] = useState([]);
|
|
27
|
+
const [meshTool, setMeshTool] = useState("move");
|
|
28
|
+
const [pending, setPending] = useState();
|
|
29
|
+
const [emptiedSlots, setEmptiedSlots] = useState([]);
|
|
30
|
+
const [poseVersion, bumpPose] = useState(0);
|
|
31
|
+
const readPose = useCallback(() => params.map((p) => session?.paramValue(p.id) ?? p.default).join(";"), [session, params]);
|
|
32
|
+
const subscribePose = useCallback((fn) => session?.onInvalidate(fn) ?? (() => { }), [session]);
|
|
33
|
+
const pose = useSyncExternalStore(subscribePose, readPose, () => "");
|
|
34
|
+
const primary = params.find((p) => p.id === (pair?.[0] ?? param));
|
|
35
|
+
const secondary = params.find((p) => p.id === pair?.[1]);
|
|
36
|
+
const xValue = primary
|
|
37
|
+
? (session?.paramValue(primary.id) ?? primary.default)
|
|
38
|
+
: 0;
|
|
39
|
+
const yValue = secondary
|
|
40
|
+
? (session?.paramValue(secondary.id) ?? secondary.default)
|
|
41
|
+
: 0;
|
|
42
|
+
const relevant = bindings.filter((b) => b.param === primary?.id && b.param_y == secondary?.id &&
|
|
43
|
+
(recordTool === "shape" ? b.target === "deform" : b.target !== "deform"));
|
|
44
|
+
// A shared shelf is only a navigation aid. Each property's authored cells
|
|
45
|
+
// are looked up through its own axes when recording or editing a key.
|
|
46
|
+
const axisPositions = (axis, parameter) => [...new Set(relevant.flatMap((b) => b.key_positions[axis] ?? [])
|
|
47
|
+
.concat(parameter ? [0, normalizedValue(parameter, parameter.default), 1] : [0]))]
|
|
48
|
+
.sort((a, b) => a - b);
|
|
49
|
+
const xPositions = axisPositions(0, primary);
|
|
50
|
+
const yPositions = axisPositions(1, secondary);
|
|
51
|
+
const position = [
|
|
52
|
+
primary ? normalizedValue(primary, xValue) : 0,
|
|
53
|
+
secondary ? normalizedValue(secondary, yValue) : 0,
|
|
54
|
+
];
|
|
55
|
+
const x = primary ? keyIndexNear(primary, xValue, xPositions, 0.00001) : undefined;
|
|
56
|
+
const y = secondary ? keyIndexNear(secondary, yValue, yPositions, 0.00001) : 0;
|
|
57
|
+
const target = node && primary
|
|
58
|
+
? { node, param: primary.id, ...(secondary ? { param_y: secondary.id } : {}), position }
|
|
59
|
+
: undefined;
|
|
60
|
+
const address = JSON.stringify([session?.id, node, primary?.id, secondary?.id, position, recordTool]);
|
|
61
|
+
const recording = mode === "record" && !!target && armed?.address === address && armed.pose === pose;
|
|
62
|
+
const keyAuthored = (at) => relevant.some((binding) => {
|
|
63
|
+
const cell = bindingCellAt(binding, at);
|
|
64
|
+
return cell && binding.authored[cell[1]]?.[cell[0]];
|
|
65
|
+
});
|
|
66
|
+
const authored = !!target && keyAuthored(position);
|
|
67
|
+
const stale = !!draft.current?.stale;
|
|
68
|
+
const pairChoices = useMemo(() => (session && node && param ? session.recordingPairs(node, param) : []), [session, node, param, revision]);
|
|
69
|
+
useEffect(() => {
|
|
70
|
+
setMode("arrange");
|
|
71
|
+
setArmed(undefined);
|
|
72
|
+
setPair(undefined);
|
|
73
|
+
setParam(session?.params()[0]?.id);
|
|
74
|
+
setMesh(undefined);
|
|
75
|
+
setMeshSelection([]);
|
|
76
|
+
setEmptiedSlots([]);
|
|
77
|
+
return () => {
|
|
78
|
+
draft.current?.dispose();
|
|
79
|
+
draft.current = undefined;
|
|
80
|
+
session?.setEditing(false);
|
|
81
|
+
};
|
|
82
|
+
}, [session]);
|
|
83
|
+
useEffect(() => {
|
|
84
|
+
if (param && !params.some((p) => p.id === param)) {
|
|
85
|
+
setParam(params[0]?.id);
|
|
86
|
+
setPair(undefined);
|
|
87
|
+
setArmed(undefined);
|
|
88
|
+
}
|
|
89
|
+
}, [params, param]);
|
|
90
|
+
useEffect(() => {
|
|
91
|
+
const next = pairChoices.find((p) => p[0] === pair?.[0] && p[1] === pair?.[1]) ??
|
|
92
|
+
pairChoices[0];
|
|
93
|
+
if (next?.join("\0") !== pair?.join("\0")) {
|
|
94
|
+
setPair(next);
|
|
95
|
+
setArmed(undefined);
|
|
96
|
+
}
|
|
97
|
+
}, [pairChoices]);
|
|
98
|
+
useEffect(() => {
|
|
99
|
+
setArmed(undefined);
|
|
100
|
+
if (info && info.kind !== "part" && info.kind !== "mesh_group")
|
|
101
|
+
setRecordTool("transform");
|
|
102
|
+
}, [node]);
|
|
103
|
+
useEffect(() => {
|
|
104
|
+
if (armed && (armed.address !== address || armed.pose !== pose)) {
|
|
105
|
+
setArmed(undefined);
|
|
106
|
+
onNotice("Recording stopped when the pose changed.");
|
|
107
|
+
}
|
|
108
|
+
}, [armed, address, pose, onNotice]);
|
|
109
|
+
useEffect(() => {
|
|
110
|
+
session?.setEditing(mode !== "arrange");
|
|
111
|
+
}, [session, mode]);
|
|
112
|
+
useEffect(() => {
|
|
113
|
+
if (!mesh?.dirty)
|
|
114
|
+
return;
|
|
115
|
+
const before = (e) => {
|
|
116
|
+
e.preventDefault();
|
|
117
|
+
e.returnValue = "";
|
|
118
|
+
};
|
|
119
|
+
window.addEventListener("beforeunload", before);
|
|
120
|
+
return () => window.removeEventListener("beforeunload", before);
|
|
121
|
+
}, [mesh?.dirty]);
|
|
122
|
+
const discardMesh = () => {
|
|
123
|
+
draft.current?.dispose();
|
|
124
|
+
draft.current = undefined;
|
|
125
|
+
setMesh(undefined);
|
|
126
|
+
setMeshSelection([]);
|
|
127
|
+
setMeshError(undefined);
|
|
128
|
+
setMode("arrange");
|
|
129
|
+
setPending(undefined);
|
|
130
|
+
};
|
|
131
|
+
const guard = (action) => {
|
|
132
|
+
if (busy)
|
|
133
|
+
return;
|
|
134
|
+
if (draft.current && mesh?.dirty) {
|
|
135
|
+
setPending(() => action);
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
if (draft.current)
|
|
139
|
+
discardMesh();
|
|
140
|
+
action();
|
|
141
|
+
};
|
|
142
|
+
const openMesh = () => {
|
|
143
|
+
if (!session || !node || draft.current)
|
|
144
|
+
return;
|
|
145
|
+
try {
|
|
146
|
+
const next = new MeshDraft(session, node);
|
|
147
|
+
draft.current = next;
|
|
148
|
+
setMesh(next.view());
|
|
149
|
+
setMeshSelection([]);
|
|
150
|
+
setMeshTool("move");
|
|
151
|
+
setMeshError(undefined);
|
|
152
|
+
setMode("mesh");
|
|
153
|
+
setArmed(undefined);
|
|
154
|
+
setEmptiedSlots([]);
|
|
155
|
+
session.setEditing(true);
|
|
156
|
+
}
|
|
157
|
+
catch (e) {
|
|
158
|
+
onError(e);
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
const requestMode = (next) => {
|
|
162
|
+
if (next === mode)
|
|
163
|
+
return;
|
|
164
|
+
guard(() => {
|
|
165
|
+
setArmed(undefined);
|
|
166
|
+
if (next === "mesh")
|
|
167
|
+
openMesh();
|
|
168
|
+
else
|
|
169
|
+
setMode(next);
|
|
170
|
+
});
|
|
171
|
+
};
|
|
172
|
+
const applyMesh = async (then) => {
|
|
173
|
+
const current = draft.current;
|
|
174
|
+
if (!current || busy)
|
|
175
|
+
return;
|
|
176
|
+
if (!current.dirty) {
|
|
177
|
+
discardMesh();
|
|
178
|
+
then?.();
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
setBusy(true);
|
|
182
|
+
try {
|
|
183
|
+
const result = await current.apply();
|
|
184
|
+
const emptied = result?.result === "emptied" ? result.slots : [];
|
|
185
|
+
discardMesh();
|
|
186
|
+
setEmptiedSlots(emptied);
|
|
187
|
+
onNotice(emptied.length
|
|
188
|
+
? `Mesh applied. ${emptied.length} vertex slot${emptied.length === 1 ? " needs" : "s need"} reassignment.`
|
|
189
|
+
: "Mesh applied. Existing shape keys were refitted.");
|
|
190
|
+
then?.();
|
|
191
|
+
}
|
|
192
|
+
catch (e) {
|
|
193
|
+
setMeshError(e instanceof Error ? e.message : String(e));
|
|
194
|
+
}
|
|
195
|
+
finally {
|
|
196
|
+
setBusy(false);
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
const reloadMesh = () => {
|
|
200
|
+
discardMesh();
|
|
201
|
+
openMesh();
|
|
202
|
+
};
|
|
203
|
+
const refreshMesh = () => {
|
|
204
|
+
if (draft.current)
|
|
205
|
+
setMesh(draft.current.view());
|
|
206
|
+
};
|
|
207
|
+
const meshAction = (action) => {
|
|
208
|
+
if (!draft.current || busy || stale)
|
|
209
|
+
return;
|
|
210
|
+
try {
|
|
211
|
+
action(draft.current);
|
|
212
|
+
refreshMesh();
|
|
213
|
+
setMeshError(undefined);
|
|
214
|
+
}
|
|
215
|
+
catch (e) {
|
|
216
|
+
setMeshError(String(e instanceof Error ? e.message : e));
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
const moveMesh = (selectedIndices, delta) => {
|
|
220
|
+
const current = draft.current;
|
|
221
|
+
if (!current || stale || busy)
|
|
222
|
+
return;
|
|
223
|
+
try {
|
|
224
|
+
current.handle.translateVertices(new Uint32Array(selectedIndices), ...delta);
|
|
225
|
+
const vertices = current.handle.vertices(), triangles = current.handle.triangles();
|
|
226
|
+
const verts = [], indices = [];
|
|
227
|
+
for (let i = 0; i < vertices.length; i += 2)
|
|
228
|
+
verts.push([vertices[i], vertices[i + 1]]);
|
|
229
|
+
for (let i = 0; i < triangles.length; i += 3)
|
|
230
|
+
indices.push([triangles[i], triangles[i + 1], triangles[i + 2]]);
|
|
231
|
+
setMesh((old) => old
|
|
232
|
+
? { ...old, dirty: true, mesh: { ...old.mesh, verts, indices } }
|
|
233
|
+
: old);
|
|
234
|
+
setMeshError(undefined);
|
|
235
|
+
}
|
|
236
|
+
catch (e) {
|
|
237
|
+
setMeshError(String(e instanceof Error ? e.message : e));
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
const selectParam = (id) => {
|
|
241
|
+
setArmed(undefined);
|
|
242
|
+
setParam(id);
|
|
243
|
+
setPair(undefined);
|
|
244
|
+
};
|
|
245
|
+
const selectCell = (cx, cy = y ?? 0) => {
|
|
246
|
+
if (!session || !primary || busy)
|
|
247
|
+
return;
|
|
248
|
+
setArmed(undefined);
|
|
249
|
+
session.setParam(primary.id, valueAtKey(primary, cx, xPositions));
|
|
250
|
+
if (secondary)
|
|
251
|
+
session.setParam(secondary.id, valueAtKey(secondary, cy, yPositions));
|
|
252
|
+
};
|
|
253
|
+
const arm = () => {
|
|
254
|
+
if (busy || !session || !info || !primary)
|
|
255
|
+
return;
|
|
256
|
+
if (!target)
|
|
257
|
+
return;
|
|
258
|
+
if (recordTool === "shape" && !info.vertex_count)
|
|
259
|
+
return;
|
|
260
|
+
try {
|
|
261
|
+
// Validated in Rust before showing the armed state; authors nothing.
|
|
262
|
+
const check = new RecordingGesture(session, target);
|
|
263
|
+
check.dispose();
|
|
264
|
+
setArmed({ address, pose: readPose() });
|
|
265
|
+
}
|
|
266
|
+
catch (e) {
|
|
267
|
+
onError(e);
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
const beginRecording = () => {
|
|
271
|
+
if (!recording || !session || !target || busy)
|
|
272
|
+
return;
|
|
273
|
+
try {
|
|
274
|
+
return new RecordingGesture(session, target);
|
|
275
|
+
}
|
|
276
|
+
catch (e) {
|
|
277
|
+
setArmed(undefined);
|
|
278
|
+
onError(e);
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
const runGesture = async (gesture, commit) => {
|
|
282
|
+
setBusy(true);
|
|
283
|
+
try {
|
|
284
|
+
await commit();
|
|
285
|
+
bumpPose((v) => v + 1);
|
|
286
|
+
}
|
|
287
|
+
catch (e) {
|
|
288
|
+
setArmed(undefined);
|
|
289
|
+
onError(e);
|
|
290
|
+
}
|
|
291
|
+
finally {
|
|
292
|
+
gesture.dispose();
|
|
293
|
+
setBusy(false);
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
const patch = async (fields, authoredBasis = false) => {
|
|
297
|
+
const gesture = beginRecording();
|
|
298
|
+
if (gesture)
|
|
299
|
+
await runGesture(gesture, () => gesture.patch(fields, authoredBasis));
|
|
300
|
+
};
|
|
301
|
+
const keyAction = async (action) => {
|
|
302
|
+
if (!session || !target || busy)
|
|
303
|
+
return;
|
|
304
|
+
setArmed(undefined);
|
|
305
|
+
setBusy(true);
|
|
306
|
+
try {
|
|
307
|
+
const edits = relevant.flatMap((binding) => {
|
|
308
|
+
const cell = bindingCellAt(binding, target.position);
|
|
309
|
+
if (!cell)
|
|
310
|
+
return [];
|
|
311
|
+
const address = { node: target.node, param: binding.param, param_y: binding.param_y ?? null, target: binding.target };
|
|
312
|
+
if (action === "clear")
|
|
313
|
+
return [{ op: "binding_cells_unset", ...address, cells: [cell] }];
|
|
314
|
+
const identity = binding.identity;
|
|
315
|
+
const value = "scalar" in identity ? { scalar: identity.scalar }
|
|
316
|
+
: { offsets: Array.from({ length: identity.vertex_count }, () => [...identity.offset]) };
|
|
317
|
+
return [{ op: "binding_cells_set", ...address, cells: [{ cell, value }] }];
|
|
318
|
+
});
|
|
319
|
+
if (edits.length)
|
|
320
|
+
await session.send({ cmd: "edit_apply", if_rev: revision, edits });
|
|
321
|
+
}
|
|
322
|
+
catch (e) {
|
|
323
|
+
onError(e);
|
|
324
|
+
}
|
|
325
|
+
finally {
|
|
326
|
+
setBusy(false);
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
const posed = useMemo(() => {
|
|
330
|
+
if (!session || !target)
|
|
331
|
+
return undefined;
|
|
332
|
+
try {
|
|
333
|
+
const g = new RecordingGesture(session, target);
|
|
334
|
+
const properties = g.posed;
|
|
335
|
+
g.dispose();
|
|
336
|
+
return properties;
|
|
337
|
+
}
|
|
338
|
+
catch {
|
|
339
|
+
return undefined;
|
|
340
|
+
}
|
|
341
|
+
}, [session, address, pose, revision, poseVersion]);
|
|
342
|
+
return {
|
|
343
|
+
session,
|
|
344
|
+
revision,
|
|
345
|
+
mode,
|
|
346
|
+
requestMode,
|
|
347
|
+
guard,
|
|
348
|
+
node,
|
|
349
|
+
info,
|
|
350
|
+
params,
|
|
351
|
+
bindings,
|
|
352
|
+
param,
|
|
353
|
+
selectParam,
|
|
354
|
+
primary,
|
|
355
|
+
secondary,
|
|
356
|
+
pair,
|
|
357
|
+
pairChoices,
|
|
358
|
+
setPair: (next) => {
|
|
359
|
+
setArmed(undefined);
|
|
360
|
+
setPair(next);
|
|
361
|
+
},
|
|
362
|
+
x,
|
|
363
|
+
y,
|
|
364
|
+
xValue,
|
|
365
|
+
yValue,
|
|
366
|
+
target,
|
|
367
|
+
relevant,
|
|
368
|
+
authored,
|
|
369
|
+
recording,
|
|
370
|
+
arm,
|
|
371
|
+
stop: () => setArmed(undefined),
|
|
372
|
+
recordTool,
|
|
373
|
+
setRecordTool: (next) => {
|
|
374
|
+
setArmed(undefined);
|
|
375
|
+
setRecordTool(next);
|
|
376
|
+
},
|
|
377
|
+
xPositions,
|
|
378
|
+
yPositions,
|
|
379
|
+
keyAuthored,
|
|
380
|
+
selectCell,
|
|
381
|
+
beginRecording,
|
|
382
|
+
runGesture,
|
|
383
|
+
patch,
|
|
384
|
+
keyAction,
|
|
385
|
+
posed,
|
|
386
|
+
busy,
|
|
387
|
+
mesh,
|
|
388
|
+
meshSelection,
|
|
389
|
+
setMeshSelection,
|
|
390
|
+
meshTool,
|
|
391
|
+
setMeshTool,
|
|
392
|
+
meshError,
|
|
393
|
+
stale,
|
|
394
|
+
meshAction,
|
|
395
|
+
moveMesh,
|
|
396
|
+
applyMesh,
|
|
397
|
+
discardMesh,
|
|
398
|
+
reloadMesh,
|
|
399
|
+
emptiedSlots,
|
|
400
|
+
pending,
|
|
401
|
+
setPending,
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
const Context = createContext(undefined);
|
|
405
|
+
export function useEditing() {
|
|
406
|
+
return useContext(Context);
|
|
407
|
+
}
|
|
408
|
+
export function EditingProvider({ session, onError, onNotice, children, }) {
|
|
409
|
+
const value = useEditingController(session, onError, onNotice);
|
|
410
|
+
const continuation = value.pending;
|
|
411
|
+
return (_jsxs(Context.Provider, { value: value, children: [children, continuation && (_jsxs(Modal, { title: "Finish your mesh edit", onClose: () => value.setPending(undefined), children: [_jsx("p", { "data-catchlight-dialog-description": "", children: "Apply your mesh changes before continuing, or discard this draft. Your artwork and existing poses are preserved when you apply." }), value.stale && (_jsx("p", { role: "alert", "data-catchlight-warning": "", children: "The model changed while this draft was open. Reload it before applying." })), _jsxs("div", { "data-catchlight-dialog-actions": "", children: [_jsx("button", { type: "button", disabled: value.busy, onClick: () => value.setPending(undefined), children: "Keep editing" }), _jsx("button", { type: "button", disabled: value.busy, onClick: () => {
|
|
412
|
+
value.discardMesh();
|
|
413
|
+
continuation();
|
|
414
|
+
}, children: "Discard draft" }), _jsx("button", { type: "button", "data-primary": "", disabled: value.busy || value.stale, onClick: () => void value.applyMesh(continuation), children: value.busy ? "Applying…" : "Apply & continue" })] })] }))] }));
|
|
415
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Editor } from "@catchlight/editor-core";
|
|
2
|
+
import type { ReactNode } from "react";
|
|
3
|
+
export interface EditorProviderProps {
|
|
4
|
+
editor: Editor;
|
|
5
|
+
children?: ReactNode;
|
|
6
|
+
}
|
|
7
|
+
export declare function EditorProvider({ editor, children }: EditorProviderProps): ReactNode;
|
|
8
|
+
/** Throws without an `EditorProvider`. */
|
|
9
|
+
export declare function useEditor(): Editor;
|
|
10
|
+
/**
|
|
11
|
+
* `"webgpu"` or `"webgl2"` once the first viewport has a device, for display.
|
|
12
|
+
* The device can arrive before this subscribes: WebGL2 resolves within a
|
|
13
|
+
* microtask of the first attach.
|
|
14
|
+
*/
|
|
15
|
+
export declare function useGpuTier(): string | undefined;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { createContext, useCallback, useContext, useSyncExternalStore } from "react";
|
|
3
|
+
const EditorContext = createContext(undefined);
|
|
4
|
+
export function EditorProvider({ editor, children }) {
|
|
5
|
+
return _jsx(EditorContext.Provider, { value: editor, children: children });
|
|
6
|
+
}
|
|
7
|
+
/** Throws without an `EditorProvider`. */
|
|
8
|
+
export function useEditor() {
|
|
9
|
+
const editor = useContext(EditorContext);
|
|
10
|
+
if (!editor)
|
|
11
|
+
throw new Error("no <EditorProvider>: every catchlight hook needs an Editor");
|
|
12
|
+
return editor;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* `"webgpu"` or `"webgl2"` once the first viewport has a device, for display.
|
|
16
|
+
* The device can arrive before this subscribes: WebGL2 resolves within a
|
|
17
|
+
* microtask of the first attach.
|
|
18
|
+
*/
|
|
19
|
+
export function useGpuTier() {
|
|
20
|
+
const editor = useEditor();
|
|
21
|
+
const subscribe = useCallback((changed) => editor.onGpuChanged(changed), [editor]);
|
|
22
|
+
return useSyncExternalStore(subscribe, () => editor.gpuTier());
|
|
23
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Session } from "@catchlight/editor-core";
|
|
2
|
+
import type { ErrorHandler } from "./authoring.js";
|
|
3
|
+
/**
|
|
4
|
+
* The model's expressions and visemes: try each at a live weight, and author
|
|
5
|
+
* them as lists of settings. A live weight is per tab and authors nothing.
|
|
6
|
+
*/
|
|
7
|
+
export declare function ExpressionsPanel({ session, onError }: {
|
|
8
|
+
session: Session;
|
|
9
|
+
onError: ErrorHandler;
|
|
10
|
+
}): import("react").JSX.Element;
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Fragment, useRef, useState } from "react";
|
|
3
|
+
import { Icon, IconButton, Modal, NumberField, round } from "./controls.js";
|
|
4
|
+
import { EXPRESSIONS, ExtensionWarnings, HelpMark, NonstandardDialog, UnreadNotice, WarningMark, plural, useExpressions, useExtensionWarnings, useHumanoid, useOfficialCommand, useLiveInputs, warningsOn, } from "./official.js";
|
|
5
|
+
import { useParams } from "./replica.js";
|
|
6
|
+
const NOUN = { expression: "expression", viseme: "viseme" };
|
|
7
|
+
const LIP_SYNC_HELP = "Lip sync drives only these four. From the voice it hears how open the mouth is and whether " +
|
|
8
|
+
"the sound is spread, rounded or a hiss, then blends ae (open), iy (wide), uw (round) and " +
|
|
9
|
+
"ss (hiss) by that. Bind all four for lip sync to work; it leaves every other viseme at zero, " +
|
|
10
|
+
"so those show only when an animation or your code sets them.";
|
|
11
|
+
/**
|
|
12
|
+
* The model's expressions and visemes: try each at a live weight, and author
|
|
13
|
+
* them as lists of settings. A live weight is per tab and authors nothing.
|
|
14
|
+
*/
|
|
15
|
+
export function ExpressionsPanel({ session, onError }) {
|
|
16
|
+
const read = useExpressions(session);
|
|
17
|
+
const humanoid = useHumanoid(session);
|
|
18
|
+
const { warnings, params: warned } = useExtensionWarnings(session, EXPRESSIONS);
|
|
19
|
+
const inputs = useLiveInputs(session);
|
|
20
|
+
const command = useOfficialCommand(session, onError);
|
|
21
|
+
const [naming, setNaming] = useState();
|
|
22
|
+
const [draft, setDraft] = useState();
|
|
23
|
+
const [askStandard, setAskStandard] = useState(false);
|
|
24
|
+
const [report, setReport] = useState();
|
|
25
|
+
if (read.unread !== undefined)
|
|
26
|
+
return (_jsx("div", { "data-catchlight-expressions-panel": "", children: _jsx(UnreadNotice, { what: "expressions and visemes", why: read.unread }) }));
|
|
27
|
+
const { expressions, visemes, emotion_names, viseme_names } = read.body;
|
|
28
|
+
const lipSync = session.lipSyncVisemes();
|
|
29
|
+
const unvoiced = visemes.filter((v) => !lipSync.includes(v.name));
|
|
30
|
+
const ranges = new Map((humanoid.body?.controls ?? []).map((c) => [c.name, [c.one_sided ? 0 : -1, 1]]));
|
|
31
|
+
const addStandard = (replace) => {
|
|
32
|
+
setAskStandard(false);
|
|
33
|
+
void command.run({ cmd: "viseme_add_standard", replace }).then((body) => {
|
|
34
|
+
if (body?.result !== "visemes_added")
|
|
35
|
+
return;
|
|
36
|
+
setReport(`Standard visemes: ${body.added.length} added, ${body.replaced.length} replaced, ${body.skipped.length} skipped.`);
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
const list = (group, members, weights) => (_jsx(ExpressionList, { group: group, members: members, weights: weights, warned: warned, onWeight: (name, weight) => group === "viseme" ? session.setVisemeWeight(name, weight) : session.setExpressionWeight(name, weight), onEdit: (member) => setDraft({ group, name: member.name, isNew: false, settings: member.settings }), onDelete: (member) => void command.run({ cmd: "expression_delete", group, name: member.name }, () => group === "viseme"
|
|
40
|
+
? session.setVisemeWeight(member.name, undefined)
|
|
41
|
+
: session.setExpressionWeight(member.name, undefined)) }));
|
|
42
|
+
return (_jsxs("div", { "data-catchlight-expressions-panel": "", children: [_jsx(ExtensionWarnings, { warnings: warnings }), _jsxs("div", { "data-catchlight-expression-groups": "", children: [_jsxs("section", { "data-catchlight-expression-group": "expression", "aria-label": "Expressions", children: [_jsxs("header", { children: [_jsx("h3", { children: "Expressions" }), _jsxs("button", { type: "button", onClick: () => setNaming("expression"), children: [_jsx(Icon, { name: "plus", width: "13", height: "13" }), "Add expression"] })] }), list("expression", expressions, inputs.expressions), !expressions.length && (_jsx("p", { "data-catchlight-hint": "", children: "No expressions yet. An emotion such as happy means the same on every model." }))] }), _jsxs("section", { "data-catchlight-expression-group": "viseme", "aria-label": "Visemes", children: [_jsxs("header", { children: [_jsx("h3", { children: "Visemes" }), _jsx("button", { type: "button", "data-catchlight-viseme-standard": "", disabled: command.busy, onClick: () => (visemes.length ? setAskStandard(true) : addStandard(false)), children: "Add standard visemes" }), _jsxs("button", { type: "button", onClick: () => setNaming("viseme"), children: [_jsx(Icon, { name: "plus", width: "13", height: "13" }), "Add viseme"] })] }), report && (_jsx("p", { "data-catchlight-hint": "", role: "status", "data-catchlight-viseme-report": "", children: report })), _jsxs("div", { "data-catchlight-lip-sync-visemes": "", role: "group", "aria-label": "Lip sync visemes", children: [_jsxs("header", { children: [_jsx("h4", { children: "Lip sync" }), _jsx(HelpMark, { text: LIP_SYNC_HELP })] }), lipSync.map((name) => {
|
|
43
|
+
const member = visemes.find((v) => v.name === name);
|
|
44
|
+
return member ? (_jsx(Fragment, { children: list("viseme", [member], inputs.visemes) }, name)) : (_jsxs("div", { "data-catchlight-viseme-missing": "", "data-name": name, children: [_jsx("span", { "data-catchlight-expression-name": "", children: name }), _jsx("small", { children: "Missing: lip sync cannot show this shape" }), _jsx("button", { type: "button", onClick: () => setDraft({ group: "viseme", name, isNew: true, settings: [] }), children: "Add" })] }, name));
|
|
45
|
+
})] }), list("viseme", unvoiced, inputs.visemes), !visemes.length && (_jsx("p", { "data-catchlight-hint": "", children: "No visemes yet. Lip sync fades between them, over the expressions." }))] })] }), naming && (_jsx(NameDialog, { group: naming, standard: (naming === "viseme" ? viseme_names : emotion_names).filter((n) => !(naming === "viseme" ? visemes : expressions).some((m) => m.name === n)), taken: (naming === "viseme" ? visemes : expressions).map((m) => m.name), onCancel: () => setNaming(undefined), onName: (name) => {
|
|
46
|
+
setNaming(undefined);
|
|
47
|
+
setDraft({ group: naming, name, isNew: true, settings: [] });
|
|
48
|
+
} })), draft && (_jsx(ExpressionEditor, { session: session, draft: draft, ranges: ranges, busy: command.busy, onCancel: () => setDraft(undefined), onSave: (settings) => void command.run({ cmd: "expression_set", group: draft.group, name: draft.name, settings, nonstandard: false }, () => setDraft(undefined)) }, `${draft.group}:${draft.name}`)), askStandard && (_jsxs(Modal, { title: "Add the standard visemes", onClose: () => setAskStandard(false), children: [_jsxs("p", { "data-catchlight-hint": "", children: ["This model already has ", plural(visemes.length, "viseme"), ". Keep the ones that share a standard name, or replace them with the standard settings? Every missing standard viseme is added either way."] }), _jsxs("div", { "data-catchlight-dialog-actions": "", children: [_jsx("button", { type: "button", onClick: () => setAskStandard(false), children: "Cancel" }), _jsx("button", { type: "button", "data-catchlight-danger": "", onClick: () => addStandard(true), children: "Replace them" }), _jsx("button", { type: "button", "data-primary": "", onClick: () => addStandard(false), children: "Keep mine" })] })] })), _jsx(NonstandardDialog, { asking: command.asking, onCancel: command.dismiss, onKeep: command.keep })] }));
|
|
49
|
+
}
|
|
50
|
+
function ExpressionList({ group, members, weights, warned, onWeight, onEdit, onDelete, }) {
|
|
51
|
+
return (_jsx(_Fragment, { children: members.map((member) => {
|
|
52
|
+
const weight = weights[member.name];
|
|
53
|
+
const warnings = warningsOn(warned, member.settings.flatMap((s) => (s.param != null ? [s.param] : [])));
|
|
54
|
+
return (_jsxs("div", { "data-catchlight-expression-row": "", "data-group": group, "data-name": member.name, "data-live": weight !== undefined ? "" : undefined, "data-warned": warnings.length ? "" : undefined, children: [_jsx("span", { "data-catchlight-expression-name": "", title: member.name, children: member.name }), _jsx(WarningMark, { warnings: warnings }), _jsx("input", { type: "range", "data-catchlight-expression-slider": "", "aria-label": `${member.name} weight`, min: 0, max: 1, step: 0.01, value: weight ?? 0, onInput: (e) => {
|
|
55
|
+
const value = Number(e.currentTarget.value);
|
|
56
|
+
onWeight(member.name, value === 0 ? undefined : value);
|
|
57
|
+
}, onChange: noop }), _jsx("output", { "data-catchlight-expression-weight": "", children: round(weight ?? 0).toFixed(2) }), _jsx("small", { "data-catchlight-expression-count": "", children: plural(member.settings.length, "setting") }), _jsx(IconButton, { icon: "settings", label: `Edit ${member.name}`, onClick: () => onEdit(member) }), _jsx(IconButton, { icon: "trash", label: `Delete ${member.name}`, onClick: () => onDelete(member) })] }, member.name));
|
|
58
|
+
}) }));
|
|
59
|
+
}
|
|
60
|
+
function NameDialog({ group, standard, taken, onCancel, onName, }) {
|
|
61
|
+
const [name, setName] = useState("");
|
|
62
|
+
const trimmed = name.trim();
|
|
63
|
+
const exists = taken.includes(trimmed);
|
|
64
|
+
return (_jsx(Modal, { title: `New ${NOUN[group]}`, onClose: onCancel, children: _jsxs("form", { onSubmit: (e) => {
|
|
65
|
+
e.preventDefault();
|
|
66
|
+
if (trimmed && !exists)
|
|
67
|
+
onName(trimmed);
|
|
68
|
+
}, children: [standard.length > 0 && (_jsxs("div", { "data-catchlight-name-choices": "", role: "group", "aria-label": "Standard names", children: [_jsx("span", { "data-catchlight-field-label": "", children: "Standard names" }), _jsx("div", { children: standard.map((n) => (_jsx("button", { type: "button", "aria-pressed": trimmed === n, "data-active": trimmed === n ? "" : undefined, onClick: () => setName(n), children: n }, n))) })] })), _jsxs("label", { "data-catchlight-form-label": "", children: ["Name", _jsx("input", { autoFocus: true, required: true, placeholder: group === "viseme" ? "e.g. aa" : "e.g. happy", value: name, onChange: (e) => setName(e.currentTarget.value) })] }), _jsx("p", { "data-catchlight-hint": "", children: group === "viseme"
|
|
69
|
+
? "A standard name lip sync knows, or one of your own."
|
|
70
|
+
: "A standard emotion means the same on every model; any other name is your own." }), exists && (_jsxs("p", { role: "alert", "data-catchlight-warning": "", children: ["There is already ", group === "viseme" ? "a viseme" : "an expression", " named \u201C", trimmed, "\u201D. Edit it from its row."] })), _jsxs("div", { "data-catchlight-dialog-actions": "", children: [_jsx("button", { type: "button", onClick: onCancel, children: "Cancel" }), _jsx("button", { type: "submit", "data-primary": "", disabled: !trimmed || exists, children: "Continue" })] })] }) }));
|
|
71
|
+
}
|
|
72
|
+
function ExpressionEditor({ session, draft, ranges, busy, onCancel, onSave, }) {
|
|
73
|
+
const params = useParams(session);
|
|
74
|
+
const next = useRef(0);
|
|
75
|
+
const [rows, setRows] = useState(() => draft.settings.map((setting) => ({ key: next.current++, setting })));
|
|
76
|
+
// Names outside the standard that the settings already use stay choosable.
|
|
77
|
+
const controls = [
|
|
78
|
+
...ranges.keys(),
|
|
79
|
+
...rows.flatMap((r) => (r.setting.control != null && !ranges.has(r.setting.control) ? [r.setting.control] : [])),
|
|
80
|
+
];
|
|
81
|
+
const range = (control) => ranges.get(control) ?? [-1, 1];
|
|
82
|
+
const update = (key, setting) => setRows((all) => all.map((r) => (r.key === key ? { key, setting } : r)));
|
|
83
|
+
const fresh = (kind) => {
|
|
84
|
+
if (kind === "param") {
|
|
85
|
+
const param = params[0];
|
|
86
|
+
return param && { param: param.id, value: param.max };
|
|
87
|
+
}
|
|
88
|
+
const used = new Set(rows.map((r) => r.setting.control));
|
|
89
|
+
const control = controls.find((c) => !used.has(c)) ?? controls[0];
|
|
90
|
+
return control === undefined ? undefined : { control, value: 1 };
|
|
91
|
+
};
|
|
92
|
+
const noun = NOUN[draft.group];
|
|
93
|
+
return (_jsx(Modal, { title: `${draft.isNew ? "New" : "Edit"} ${noun} “${draft.name}”`, onClose: onCancel, wide: true, children: _jsxs("form", { "data-catchlight-expression-editor": "", onSubmit: (e) => {
|
|
94
|
+
e.preventDefault();
|
|
95
|
+
if (rows.length)
|
|
96
|
+
onSave(rows.map((r) => r.setting));
|
|
97
|
+
}, children: [_jsx("p", { "data-catchlight-hint": "", children: "Each setting gives a control or one of the model\u2019s params a value at full weight." }), _jsx("div", { "data-catchlight-expression-settings": "", role: "list", "aria-label": "Settings", children: rows.map(({ key, setting }, i) => (_jsx(SettingRow, { index: i, setting: setting, controls: controls, params: params, range: range, onKind: (kind) => {
|
|
98
|
+
const replacement = fresh(kind);
|
|
99
|
+
if (replacement)
|
|
100
|
+
update(key, replacement);
|
|
101
|
+
}, onChange: (s) => update(key, s), onRemove: () => setRows((all) => all.filter((r) => r.key !== key)) }, key))) }), _jsxs("button", { type: "button", "data-catchlight-action": "", disabled: !controls.length && !params.length, onClick: () => {
|
|
102
|
+
const setting = fresh(controls.length ? "control" : "param");
|
|
103
|
+
if (setting)
|
|
104
|
+
setRows((all) => [...all, { key: next.current++, setting }]);
|
|
105
|
+
}, children: [_jsx(Icon, { name: "plus" }), "Add setting"] }), !rows.length && (_jsxs("p", { "data-catchlight-hint": "", role: "status", children: ["Add at least one setting to save this ", noun, "."] })), _jsxs("div", { "data-catchlight-dialog-actions": "", children: [_jsx("button", { type: "button", onClick: onCancel, children: "Cancel" }), _jsx("button", { type: "submit", "data-primary": "", disabled: !rows.length || busy, children: busy ? "Saving…" : `Save ${noun}` })] })] }) }));
|
|
106
|
+
}
|
|
107
|
+
function SettingRow({ index, setting, controls, params, range, onKind, onChange, onRemove, }) {
|
|
108
|
+
const n = index + 1;
|
|
109
|
+
const control = setting.control ?? undefined;
|
|
110
|
+
const [min, max] = control !== undefined ? range(control) : [undefined, undefined];
|
|
111
|
+
const missing = setting.param != null && !params.some((p) => p.id === setting.param);
|
|
112
|
+
return (_jsxs("div", { "data-catchlight-expression-setting": "", role: "listitem", children: [_jsxs("select", { "aria-label": `Setting ${n} target`, value: control !== undefined ? "control" : "param", onChange: (e) => onKind(e.currentTarget.value), children: [_jsx("option", { value: "control", disabled: !controls.length, children: "Control" }), _jsx("option", { value: "param", disabled: !params.length, children: "Param" })] }), control !== undefined ? (_jsx("select", { "aria-label": `Setting ${n} control`, value: control, onChange: (e) => {
|
|
113
|
+
const to = e.currentTarget.value;
|
|
114
|
+
const [low, high] = range(to);
|
|
115
|
+
onChange({ control: to, value: Math.min(high, Math.max(low, setting.value)) });
|
|
116
|
+
}, children: controls.map((c) => (_jsx("option", { value: c, children: c }, c))) })) : (_jsxs("select", { "aria-label": `Setting ${n} param`, value: setting.param ?? "", onChange: (e) => onChange({ param: e.currentTarget.value, value: setting.value }), children: [missing && _jsxs("option", { value: setting.param, children: [setting.param, " (removed)"] }), params.map((p) => (_jsx("option", { value: p.id, children: p.name }, p.id)))] })), _jsx(NumberField, { label: `Setting ${n} value`, value: setting.value, ...(min !== undefined ? { min, max } : {}), onCommit: (value) => onChange(control !== undefined ? { control, value } : { param: setting.param, value }) }), _jsx(IconButton, { icon: "close", label: `Remove setting ${n}`, onClick: onRemove })] }));
|
|
117
|
+
}
|
|
118
|
+
// React requires a controlled input's `onChange`; the native input event is
|
|
119
|
+
// handled once, through `onInput`.
|
|
120
|
+
function noop() { }
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** Opens a picked file as a model. The input is reset so picking the same file again fires again. */
|
|
2
|
+
import type { Session } from "@catchlight/editor-core";
|
|
3
|
+
import type { ComponentProps } from "react";
|
|
4
|
+
export interface FileOpenRootProps extends Omit<ComponentProps<"input">, "type" | "onChange" | "onError"> {
|
|
5
|
+
onOpened?: (session: Session) => void;
|
|
6
|
+
onError?: (cause: unknown) => void;
|
|
7
|
+
}
|
|
8
|
+
export declare function FileOpenRoot({ onOpened, onError, ...rest }: FileOpenRootProps): import("react").JSX.Element;
|
|
9
|
+
export declare const FileOpen: {
|
|
10
|
+
Root: typeof FileOpenRoot;
|
|
11
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useEditor } from "./editor-context.js";
|
|
3
|
+
export function FileOpenRoot({ onOpened, onError, ...rest }) {
|
|
4
|
+
const editor = useEditor();
|
|
5
|
+
const handleChange = (event) => {
|
|
6
|
+
const file = event.currentTarget.files?.[0];
|
|
7
|
+
event.currentTarget.value = "";
|
|
8
|
+
if (!file)
|
|
9
|
+
return;
|
|
10
|
+
void (async () => {
|
|
11
|
+
try {
|
|
12
|
+
const bytes = new Uint8Array(await file.arrayBuffer());
|
|
13
|
+
onOpened?.(await editor.openFile(bytes, file.name));
|
|
14
|
+
}
|
|
15
|
+
catch (cause) {
|
|
16
|
+
if (onError)
|
|
17
|
+
onError(cause);
|
|
18
|
+
else
|
|
19
|
+
console.warn("catchlight: opening the file failed", cause);
|
|
20
|
+
}
|
|
21
|
+
})();
|
|
22
|
+
};
|
|
23
|
+
return (_jsx("input", { type: "file", accept: ".clm", "data-catchlight-file-open": "", onChange: handleChange, ...rest }));
|
|
24
|
+
}
|
|
25
|
+
export const FileOpen = { Root: FileOpenRoot };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Save, then download the bytes when this tab can read them back. A backend
|
|
3
|
+
* that keeps no readable copy skips the download, and the outcome says so.
|
|
4
|
+
*/
|
|
5
|
+
import type { Session } from "@catchlight/editor-core";
|
|
6
|
+
import type { ComponentProps, ReactNode } from "react";
|
|
7
|
+
export interface SaveOutcome {
|
|
8
|
+
key: string;
|
|
9
|
+
downloaded: boolean;
|
|
10
|
+
}
|
|
11
|
+
export interface FileSaver {
|
|
12
|
+
/** Without `name`, saves where the model was opened from. */
|
|
13
|
+
save(name?: string): Promise<SaveOutcome>;
|
|
14
|
+
}
|
|
15
|
+
export declare function useFileSave(session: Session | undefined): FileSaver;
|
|
16
|
+
export interface FileSaveRootProps extends Omit<ComponentProps<"form">, "onSubmit" | "onError"> {
|
|
17
|
+
session: Session;
|
|
18
|
+
defaultName?: string | undefined;
|
|
19
|
+
onSaved?: (outcome: SaveOutcome) => void;
|
|
20
|
+
onError?: (cause: unknown) => void;
|
|
21
|
+
children?: ReactNode;
|
|
22
|
+
}
|
|
23
|
+
export declare function FileSaveRoot({ session, defaultName, onSaved, onError, children, ...rest }: FileSaveRootProps): import("react").JSX.Element;
|
|
24
|
+
export declare const FileSave: {
|
|
25
|
+
Root: typeof FileSaveRoot;
|
|
26
|
+
};
|
|
27
|
+
export declare function saveKey(name: string): string;
|
|
28
|
+
export declare function downloadName(key: string): string;
|
|
29
|
+
export declare function downloadBytes(bytes: Uint8Array, name: string): void;
|