@joycostudio/studio 0.1.0 → 0.1.2

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/react.js CHANGED
@@ -14,15 +14,35 @@ import {
14
14
  import { useEffect as useEffect4, useRef as useRef3, useState as useState7 } from "react";
15
15
 
16
16
  // package.json
17
- var version = "0.1.0";
17
+ var version = "0.1.2";
18
18
 
19
19
  // src/client/studio.ts
20
- import { button } from "@joycostudio/xyz/debug";
20
+ import {
21
+ button,
22
+ schemaValues
23
+ } from "@joycostudio/xyz/debug";
21
24
  var COMPARE_MESSAGE = "joyco-studio:compare";
22
25
  function postCompare(target, tool, showSaved) {
23
26
  const message = { type: COMPARE_MESSAGE, tool, showSaved };
24
27
  target?.postMessage(message, location.origin);
25
28
  }
29
+ function resetSlidersOnDoubleClick(handle) {
30
+ return (folder, { fields }) => {
31
+ const sliders = fields().filter(({ field }) => field.kind === "slider");
32
+ const onDoubleClick = (event) => {
33
+ if (handle.comparing || event.target.closest("input")) return;
34
+ const slider = sliders.find(({ blade }) => blade.element.contains(event.target));
35
+ if (slider?.field.kind !== "slider") return;
36
+ const saved = slider.path.reduce(
37
+ (values, key) => values?.[key],
38
+ schemaValues(handle.definition.controls(structuredClone(handle.saved)))
39
+ );
40
+ if (typeof saved === "number") slider.field.write(saved);
41
+ };
42
+ folder.element.addEventListener("dblclick", onDoubleClick);
43
+ return () => folder.element.removeEventListener("dblclick", onDoubleClick);
44
+ };
45
+ }
26
46
  var instances = globalThis.__joycoStudio ??= /* @__PURE__ */ new Map();
27
47
 
28
48
  // src/react/hooks.ts
@@ -49,16 +69,6 @@ import { bindDebugSchema } from "@joycostudio/xyz/debug";
49
69
  import { SquareSplitHorizontal } from "lucide-react";
50
70
  import { useEffect, useRef, useState } from "react";
51
71
 
52
- // src/core/diff.ts
53
- function changedPaths(before, after, path = "") {
54
- const nested = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
55
- if (nested(before) && nested(after)) {
56
- const keys = /* @__PURE__ */ new Set([...Object.keys(before), ...Object.keys(after)]);
57
- return [...keys].flatMap((key) => changedPaths(before[key], after[key], path ? `${path}.${key}` : key));
58
- }
59
- return JSON.stringify(before) === JSON.stringify(after) ? [] : [path];
60
- }
61
-
62
72
  // src/react/theme.ts
63
73
  var tweakpaneTheme = {
64
74
  "--tp-base-background-color": "transparent",
@@ -111,9 +121,9 @@ function ToolIcon({ icon = SlidersHorizontal, ...props }) {
111
121
  "aria-hidden": true,
112
122
  ...props,
113
123
  children: icon.map(([Tag, attrs], index) => {
114
- const Element2 = Tag;
124
+ const Element = Tag;
115
125
  return /* @__PURE__ */ jsx(
116
- Element2,
126
+ Element,
117
127
  {
118
128
  ...Object.fromEntries(Object.entries(attrs).map(([name, value]) => [camel(name), value]))
119
129
  },
@@ -310,9 +320,9 @@ function PanelFooter({ className, ...props }) {
310
320
  import { Fragment, jsx as jsx7, jsxs as jsxs2 } from "react/jsx-runtime";
311
321
  function describe(studio, handle) {
312
322
  const { state } = handle;
323
+ if (handle.comparing) return { tone: "text-sky-400", text: "Showing saved" };
313
324
  if (studio.connection.phase !== "connected") return { tone: "text-faint", text: "Not persisting" };
314
325
  if (!state) return { tone: "text-destructive", text: "Restart studio dev" };
315
- if (handle.comparing) return { tone: "text-sky-400", text: "Showing saved" };
316
326
  if (state.conflict) return { tone: "text-destructive", text: "Changed on disk" };
317
327
  if (state.dirty) return { tone: "text-warning", text: "Unsaved" };
318
328
  return { tone: "text-success", text: state.saved.hash ? "Saved" : "Not on disk yet" };
@@ -337,7 +347,10 @@ function Inspector({
337
347
  if (disposed || !container.current) return;
338
348
  const instance = new Pane({ container: container.current });
339
349
  pane.current = instance;
340
- const bound = bindDebugSchema(instance, { schema: () => handle.controls() });
350
+ const bound = bindDebugSchema(instance, {
351
+ schema: () => handle.controls(),
352
+ extend: resetSlidersOnDoubleClick(handle)
353
+ });
341
354
  const unsubscribe = handle.subscribe(() => bound.refresh());
342
355
  teardown = () => {
343
356
  unsubscribe();
@@ -367,7 +380,7 @@ function Inspector({
367
380
  const ConfigFile = fileHref ? "a" : "span";
368
381
  const status = describe(studio, handle);
369
382
  const editable2 = studio.connection.phase === "connected" && !!state?.dirty && !handle.comparing;
370
- const changes = (state?.dirty ? changedPaths(state.saved.value, state.draft.value) : []).map(
383
+ const changes = handle.changes.map(
371
384
  (path) => path.split(".").map((key) => key.charAt(0).toUpperCase() + key.slice(1)).join(" \u203A ")
372
385
  );
373
386
  return /* @__PURE__ */ jsxs2(Fragment, { children: [
@@ -424,7 +437,7 @@ function Inspector({
424
437
  size: "icon-sm",
425
438
  "aria-label": "Hold to compare with saved",
426
439
  "aria-pressed": handle.comparing,
427
- disabled: !state?.dirty,
440
+ disabled: !handle.dirty && !handle.comparing,
428
441
  className: "shrink-0 aria-pressed:bg-primary aria-pressed:text-primary-foreground",
429
442
  onPointerDown: (event) => {
430
443
  event.currentTarget.setPointerCapture(event.pointerId);
@@ -1037,8 +1050,13 @@ function StudioFooter({ connection }) {
1037
1050
  }
1038
1051
  var SHORTCUTS = /* @__PURE__ */ new Set(["b", "s", "k"]);
1039
1052
  var COMPARE_KEY = "\\";
1040
- var editable = (target) => target instanceof Element && target.matches("input, textarea, select, [contenteditable]");
1041
- var isShortcut = (event) => (event.metaKey || event.ctrlKey) && SHORTCUTS.has(event.key.toLowerCase()) || event.key === COMPARE_KEY && !editable(event.target);
1053
+ var editable = (target) => !!target?.matches?.("input, textarea, select, [contenteditable]");
1054
+ var historyAction = (event) => {
1055
+ if (!(event.metaKey || event.ctrlKey) || event.altKey || editable(event.target)) return null;
1056
+ const key = event.key.toLowerCase();
1057
+ return key === "z" ? event.shiftKey ? "redo" : "undo" : key === "y" ? "redo" : null;
1058
+ };
1059
+ var isShortcut = (event) => (event.metaKey || event.ctrlKey) && SHORTCUTS.has(event.key.toLowerCase()) || event.key === COMPARE_KEY && !editable(event.target) || !!historyAction(event);
1042
1060
  function forwardShortcuts(frame) {
1043
1061
  const forward = (event) => {
1044
1062
  if (event.defaultPrevented || !isShortcut(event)) return;
@@ -1089,7 +1107,7 @@ function Studio({ studio, previewComponent, getConfigFileHref }) {
1089
1107
  if (event.key !== COMPARE_KEY || editable(event.target) || event.repeat) return;
1090
1108
  event.preventDefault();
1091
1109
  const { handle: handle2, compare: compare2 } = current.current;
1092
- compare2(event.type === "keydown" && !!handle2?.state?.dirty);
1110
+ compare2(event.type === "keydown" && !!handle2?.dirty);
1093
1111
  };
1094
1112
  const release = () => current.current.compare(false);
1095
1113
  addEventListener("keydown", onKey);
@@ -1102,6 +1120,16 @@ function Studio({ studio, previewComponent, getConfigFileHref }) {
1102
1120
  removeEventListener("blur", release);
1103
1121
  };
1104
1122
  }, []);
1123
+ useEffect4(() => {
1124
+ const onKey = (event) => {
1125
+ const action = event.defaultPrevented ? null : historyAction(event);
1126
+ if (!action) return;
1127
+ event.preventDefault();
1128
+ current.current.handle?.[action]();
1129
+ };
1130
+ addEventListener("keydown", onKey);
1131
+ return () => removeEventListener("keydown", onKey);
1132
+ }, []);
1105
1133
  useEffect4(() => {
1106
1134
  const fromHash = () => {
1107
1135
  const id = decodeURIComponent(location.hash.slice(1));
@@ -1195,7 +1223,7 @@ function Studio({ studio, previewComponent, getConfigFileHref }) {
1195
1223
  version
1196
1224
  ] })
1197
1225
  ] }) }),
1198
- /* @__PURE__ */ jsx13(SidebarContent, { children: /* @__PURE__ */ jsx13(SidebarGroup, { children: /* @__PURE__ */ jsx13(SidebarMenu, { children: tools.map(({ definition, state }) => {
1226
+ /* @__PURE__ */ jsx13(SidebarContent, { children: /* @__PURE__ */ jsx13(SidebarGroup, { children: /* @__PURE__ */ jsx13(SidebarMenu, { children: tools.map(({ definition, state, dirty }) => {
1199
1227
  const title = definition.title ?? definition.id;
1200
1228
  const isPinned = pinned.has(definition.id);
1201
1229
  const pinLabel = `${isPinned ? "Unpin" : "Pin"} ${title}`;
@@ -1209,13 +1237,13 @@ function Studio({ studio, previewComponent, getConfigFileHref }) {
1209
1237
  children: [
1210
1238
  /* @__PURE__ */ jsxs7("span", { className: "relative", children: [
1211
1239
  /* @__PURE__ */ jsx13(ToolIcon, { icon: definition.icon, className: "size-4" }),
1212
- state?.dirty && /* @__PURE__ */ jsx13(
1240
+ dirty && /* @__PURE__ */ jsx13(
1213
1241
  "span",
1214
1242
  {
1215
- "aria-label": state.conflict ? "Changed on disk" : "Unsaved",
1243
+ "aria-label": state?.conflict ? "Changed on disk" : "Unsaved",
1216
1244
  className: cn(
1217
1245
  "absolute -top-0.5 -right-0.5 size-1.5 rounded-full",
1218
- state.conflict ? "bg-destructive" : "bg-warning"
1246
+ state?.conflict ? "bg-destructive" : "bg-warning"
1219
1247
  )
1220
1248
  }
1221
1249
  )