@pixerate/editor-react 0.157.0 → 0.159.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.
@@ -1,9 +1,10 @@
1
1
  import {
2
2
  EffectParameters,
3
+ EffectTemplates,
3
4
  EffectsBrowser,
4
5
  useEffectDescription,
5
6
  useEffects
6
- } from "../chunk-IDUWADYT.js";
7
+ } from "../chunk-TOYSB4DK.js";
7
8
  import "../chunk-5RPB4WGT.js";
8
9
  import "../chunk-KIRIU53A.js";
9
10
  import "../chunk-3I7FOSLF.js";
@@ -88,7 +89,7 @@ function AnimationStage({ controller, values, onionSkin, motionPaths, onPick, hi
88
89
  controller.toSvg(time, { pretty: false, idPrefix: "ae-", values, onionSkin, motionPaths }).replace(/<svg ([^>]*?)width="[^"]*" height="[^"]*"/, '<svg $1width="100%" height="100%"'),
89
90
  highlight ?? []
90
91
  ),
91
- [controller, scene, time, samples, values, onionSkin, motionPaths, key, state.effectsProvider]
92
+ [controller, scene, time, samples, values, onionSkin, motionPaths, key, state.effectsProvider, state.layerPictures]
92
93
  );
93
94
  const stageRef = useRef2(null);
94
95
  const view = useMemo2(() => motionCameraViewAt(controller.viewScene(), time, state.effectsProvider), [controller, scene, time, samples, state.effectsProvider]);
@@ -1433,6 +1434,7 @@ function AnimationEffectsPanel({ controller, node, className = "" }) {
1433
1434
  ] }),
1434
1435
  !provider && /* @__PURE__ */ jsx13("p", { className: "text-[11px] text-muted-foreground", children: "No effects provider is registered. Commands effects made are kept." }),
1435
1436
  browse === "motion" && /* @__PURE__ */ jsx13(EffectsBrowser, { provider, uses: ["motion"], kinds: ["motion"], title: `Motion for ${target}`, onPick: (e) => (setBrowse(null), report(controller.applyMotionEffect(e.id, [target]))) }),
1437
+ /* @__PURE__ */ jsx13(EffectTemplates, { provider, disabled, fillLabel: "New scene from template", onFill: (t, v) => controller.fillTemplate(t, v).then((r) => (r.ok && r.notes.length && setMessage(r.notes.join(" ")), r)) }),
1436
1438
  browse === "vector" && /* @__PURE__ */ jsx13(EffectsBrowser, { provider, kinds: ["vector"], title: "Vector effects", onPick: (e) => (setBrowse(null), report(controller.addVectorEffect(e.id))) }),
1437
1439
  /* @__PURE__ */ jsx13("ol", { className: "space-y-1", children: applied.map((e) => /* @__PURE__ */ jsxs11("li", { className: "rounded border border-border p-1", "data-motion-effect": e.instance, children: [
1438
1440
  /* @__PURE__ */ jsxs11("div", { className: "flex items-center gap-1", children: [
@@ -1517,10 +1517,6 @@ function useCanvasCapture(options) {
1517
1517
  // src/canvas/useCanvasEditorDocument.ts
1518
1518
  var import_react16 = require("react");
1519
1519
  var import_canvas11 = require("@pixerate/editor/canvas");
1520
- var contentKey = (nodes, edges) => JSON.stringify([
1521
- nodes.map(({ selected: _s, dragging: _d, measured: _m, resizing: _r, ...rest }) => rest),
1522
- edges.map(({ selected: _s, ...rest }) => rest)
1523
- ]);
1524
1520
  function useCanvasEditorDocument(options) {
1525
1521
  const latest = (0, import_react16.useRef)(options);
1526
1522
  latest.current = options;
@@ -1534,19 +1530,29 @@ function useCanvasEditorDocument(options) {
1534
1530
  history: options.history,
1535
1531
  scenes: options.scenes,
1536
1532
  readOnly: () => latest.current.readOnly?.() ?? false,
1533
+ // Only offered when given, so a document without it saves no viewport.
1534
+ ...options.getViewport ? { getViewport: () => latest.current.getViewport() } : {},
1535
+ setViewport: (viewport) => latest.current.setViewport?.(viewport),
1536
+ validateNode: (node, index) => latest.current.validateNode?.(node, index),
1537
+ normalize: (document2) => latest.current.normalize ? latest.current.normalize(document2) : document2,
1538
+ omitNodeKeys: options.omitNodeKeys,
1539
+ omitEdgeKeys: options.omitEdgeKeys,
1540
+ formatVersion: options.formatVersion,
1541
+ migrations: options.migrations,
1542
+ assets: options.assets,
1537
1543
  subscribe: (listener) => {
1538
1544
  listeners.current.add(listener);
1539
1545
  return () => listeners.current.delete(listener);
1540
1546
  }
1541
1547
  }),
1542
1548
  // eslint-disable-next-line react-hooks/exhaustive-deps
1543
- [options.history, options.scenes]
1549
+ [options.history, options.scenes, options.assets]
1544
1550
  );
1545
1551
  (0, import_react16.useEffect)(() => () => doc.destroy(), [doc]);
1546
1552
  const lastKey = (0, import_react16.useRef)(null);
1547
1553
  (0, import_react16.useEffect)(() => {
1548
1554
  if (options.nodes.some((n) => n.dragging)) return;
1549
- const key = contentKey(options.nodes, options.edges);
1555
+ const key = (0, import_canvas11.canvasDocumentContentKey)(options.nodes, options.edges, latest.current);
1550
1556
  if (lastKey.current !== null && key !== lastKey.current) listeners.current.forEach((listener) => listener());
1551
1557
  lastKey.current = key;
1552
1558
  }, [options.nodes, options.edges]);
@@ -2,7 +2,7 @@ import {
2
2
  EffectParameters,
3
3
  EffectsBrowser,
4
4
  useEffectDescription
5
- } from "../chunk-IDUWADYT.js";
5
+ } from "../chunk-TOYSB4DK.js";
6
6
  import "../chunk-5RPB4WGT.js";
7
7
  import "../chunk-KIRIU53A.js";
8
8
  import "../chunk-3I7FOSLF.js";
@@ -1059,12 +1059,9 @@ function useCanvasCapture(options) {
1059
1059
  // src/canvas/useCanvasEditorDocument.ts
1060
1060
  import { useEffect as useEffect6, useMemo as useMemo6, useRef as useRef10 } from "react";
1061
1061
  import {
1062
+ canvasDocumentContentKey,
1062
1063
  createCanvasEditorDocument
1063
1064
  } from "@pixerate/editor/canvas";
1064
- var contentKey = (nodes, edges) => JSON.stringify([
1065
- nodes.map(({ selected: _s, dragging: _d, measured: _m, resizing: _r, ...rest }) => rest),
1066
- edges.map(({ selected: _s, ...rest }) => rest)
1067
- ]);
1068
1065
  function useCanvasEditorDocument(options) {
1069
1066
  const latest = useRef10(options);
1070
1067
  latest.current = options;
@@ -1078,19 +1075,29 @@ function useCanvasEditorDocument(options) {
1078
1075
  history: options.history,
1079
1076
  scenes: options.scenes,
1080
1077
  readOnly: () => latest.current.readOnly?.() ?? false,
1078
+ // Only offered when given, so a document without it saves no viewport.
1079
+ ...options.getViewport ? { getViewport: () => latest.current.getViewport() } : {},
1080
+ setViewport: (viewport) => latest.current.setViewport?.(viewport),
1081
+ validateNode: (node, index) => latest.current.validateNode?.(node, index),
1082
+ normalize: (document2) => latest.current.normalize ? latest.current.normalize(document2) : document2,
1083
+ omitNodeKeys: options.omitNodeKeys,
1084
+ omitEdgeKeys: options.omitEdgeKeys,
1085
+ formatVersion: options.formatVersion,
1086
+ migrations: options.migrations,
1087
+ assets: options.assets,
1081
1088
  subscribe: (listener) => {
1082
1089
  listeners.current.add(listener);
1083
1090
  return () => listeners.current.delete(listener);
1084
1091
  }
1085
1092
  }),
1086
1093
  // eslint-disable-next-line react-hooks/exhaustive-deps
1087
- [options.history, options.scenes]
1094
+ [options.history, options.scenes, options.assets]
1088
1095
  );
1089
1096
  useEffect6(() => () => doc.destroy(), [doc]);
1090
1097
  const lastKey = useRef10(null);
1091
1098
  useEffect6(() => {
1092
1099
  if (options.nodes.some((n) => n.dragging)) return;
1093
- const key = contentKey(options.nodes, options.edges);
1100
+ const key = canvasDocumentContentKey(options.nodes, options.edges, latest.current);
1094
1101
  if (lastKey.current !== null && key !== lastKey.current) listeners.current.forEach((listener) => listener());
1095
1102
  lastKey.current = key;
1096
1103
  }, [options.nodes, options.edges]);
@@ -1,6 +1,6 @@
1
1
  import { type CanvasEdge, type CanvasEdgeLike, type CanvasEditorDocument, type CanvasEditorDocumentOptions, type CanvasNode } from '@pixerate/editor/canvas';
2
2
  import type { EditorDocument } from '@pixerate/editor/persistence';
3
- export interface UseCanvasEditorDocumentOptions<TNode extends CanvasNode = CanvasNode, TEdge extends CanvasEdgeLike = CanvasEdge> extends Pick<CanvasEditorDocumentOptions<TNode, TEdge>, 'history' | 'scenes' | 'readOnly'> {
3
+ export interface UseCanvasEditorDocumentOptions<TNode extends CanvasNode = CanvasNode, TEdge extends CanvasEdgeLike = CanvasEdge> extends Pick<CanvasEditorDocumentOptions<TNode, TEdge>, 'history' | 'scenes' | 'readOnly' | 'getViewport' | 'setViewport' | 'omitNodeKeys' | 'omitEdgeKeys' | 'validateNode' | 'normalize' | 'formatVersion' | 'migrations' | 'assets'> {
4
4
  nodes: TNode[];
5
5
  edges: TEdge[];
6
6
  setNodes: (nodes: TNode[]) => void;
@@ -9,7 +9,9 @@ export interface UseCanvasEditorDocumentOptions<TNode extends CanvasNode = Canva
9
9
  /**
10
10
  * The save / load / undo contract (kind `canvas`) for nodes and edges held in React state, for
11
11
  * `useEditorAutosave` or `createDocumentToolProvider`. Besides the history, it counts changes
12
- * the history does not see (a finished drag, an edge removed by React Flow), but not selection
13
- * or measuring, and not the frames of a drag in progress. Pass a stable `history` and `scenes`.
12
+ * the history does not see (a finished drag, an edge removed by React Flow), but not selection,
13
+ * measuring, the omitted keys or the viewport, and not the frames of a drag in progress. Pass a
14
+ * stable `history`, `scenes` and `assets`; `omitNodeKeys`, `omitEdgeKeys`, `formatVersion` and
15
+ * `migrations` are read once. The functions are always called in their latest version.
14
16
  */
15
17
  export declare function useCanvasEditorDocument<TNode extends CanvasNode = CanvasNode, TEdge extends CanvasEdgeLike = CanvasEdge>(options: UseCanvasEditorDocumentOptions<TNode, TEdge>): EditorDocument<CanvasEditorDocument<TNode, TEdge>>;
@@ -1,6 +1,6 @@
1
1
  import { type CanvasEdge, type CanvasEdgeLike, type CanvasEditorDocument, type CanvasEditorDocumentOptions, type CanvasNode } from '@pixerate/editor/canvas';
2
2
  import type { EditorDocument } from '@pixerate/editor/persistence';
3
- export interface UseCanvasEditorDocumentOptions<TNode extends CanvasNode = CanvasNode, TEdge extends CanvasEdgeLike = CanvasEdge> extends Pick<CanvasEditorDocumentOptions<TNode, TEdge>, 'history' | 'scenes' | 'readOnly'> {
3
+ export interface UseCanvasEditorDocumentOptions<TNode extends CanvasNode = CanvasNode, TEdge extends CanvasEdgeLike = CanvasEdge> extends Pick<CanvasEditorDocumentOptions<TNode, TEdge>, 'history' | 'scenes' | 'readOnly' | 'getViewport' | 'setViewport' | 'omitNodeKeys' | 'omitEdgeKeys' | 'validateNode' | 'normalize' | 'formatVersion' | 'migrations' | 'assets'> {
4
4
  nodes: TNode[];
5
5
  edges: TEdge[];
6
6
  setNodes: (nodes: TNode[]) => void;
@@ -9,7 +9,9 @@ export interface UseCanvasEditorDocumentOptions<TNode extends CanvasNode = Canva
9
9
  /**
10
10
  * The save / load / undo contract (kind `canvas`) for nodes and edges held in React state, for
11
11
  * `useEditorAutosave` or `createDocumentToolProvider`. Besides the history, it counts changes
12
- * the history does not see (a finished drag, an edge removed by React Flow), but not selection
13
- * or measuring, and not the frames of a drag in progress. Pass a stable `history` and `scenes`.
12
+ * the history does not see (a finished drag, an edge removed by React Flow), but not selection,
13
+ * measuring, the omitted keys or the viewport, and not the frames of a drag in progress. Pass a
14
+ * stable `history`, `scenes` and `assets`; `omitNodeKeys`, `omitEdgeKeys`, `formatVersion` and
15
+ * `migrations` are read once. The functions are always called in their latest version.
14
16
  */
15
17
  export declare function useCanvasEditorDocument<TNode extends CanvasNode = CanvasNode, TEdge extends CanvasEdgeLike = CanvasEdge>(options: UseCanvasEditorDocumentOptions<TNode, TEdge>): EditorDocument<CanvasEditorDocument<TNode, TEdge>>;
@@ -6,7 +6,7 @@ import {
6
6
  EffectsBrowser,
7
7
  useEffectDescription,
8
8
  useEffects
9
- } from "./chunk-IDUWADYT.js";
9
+ } from "./chunk-TOYSB4DK.js";
10
10
  import {
11
11
  resolveParameterComponents,
12
12
  useParameterComponents
@@ -683,6 +683,123 @@ function DurationField({ duration, onCommit }) {
683
683
  );
684
684
  }
685
685
 
686
+ // src/effects/EffectTemplates.tsx
687
+ import { useEffect as useEffect5, useState as useState5 } from "react";
688
+ import { checkTemplateValues, isSubjectTemplateSlot } from "@pixerate/editor/effects";
689
+ import { jsx as jsx5, jsxs as jsxs4 } from "react/jsx-runtime";
690
+ var input = "w-full min-w-0 rounded border border-border bg-transparent px-1 py-0.5 text-xs";
691
+ var btn = "rounded border border-border px-2 py-0.5 text-[11px] disabled:opacity-40";
692
+ function SlotField({ slot, value, error, onChange }) {
693
+ const id = `template-slot-${slot.name}`;
694
+ let field;
695
+ if (slot.type === "color") field = /* @__PURE__ */ jsx5("input", { id, className: "h-6 w-10", type: "color", value: String(value), onChange: (e) => onChange(e.target.value) });
696
+ else if (slot.type === "number")
697
+ field = /* @__PURE__ */ jsx5("input", { id, className: input, type: "number", min: slot.min, max: slot.max, step: slot.integer ? 1 : "any", value: String(value), onChange: (e) => onChange(e.target.value === "" ? "" : Number(e.target.value)) });
698
+ else if (slot.type === "text")
699
+ field = /* @__PURE__ */ jsxs4("div", { className: "flex items-center gap-1", children: [
700
+ /* @__PURE__ */ jsx5("input", { id, className: input, maxLength: slot.maxLength, value: String(value), onChange: (e) => onChange(e.target.value) }),
701
+ slot.maxLength !== void 0 && /* @__PURE__ */ jsxs4("span", { className: "text-[10px] text-muted-foreground", children: [
702
+ String(value).length,
703
+ "/",
704
+ slot.maxLength
705
+ ] })
706
+ ] });
707
+ else
708
+ field = /* @__PURE__ */ jsx5(
709
+ "input",
710
+ {
711
+ id,
712
+ className: input,
713
+ placeholder: slot.hasDefault ? "URL or <svg> (default: a placeholder)" : "URL or <svg> (empty: left open)",
714
+ value: typeof value === "string" ? value : "",
715
+ onChange: (e) => onChange(e.target.value)
716
+ }
717
+ );
718
+ return /* @__PURE__ */ jsxs4("div", { className: "flex flex-col gap-0.5", "data-template-slot": slot.name, children: [
719
+ /* @__PURE__ */ jsx5("label", { htmlFor: id, className: "text-muted-foreground", title: slot.description, children: slot.label }),
720
+ field,
721
+ error && /* @__PURE__ */ jsx5("p", { className: "text-[11px] text-destructive", children: error })
722
+ ] });
723
+ }
724
+ function EffectTemplates({ provider, onFill, fillLabel = "Use template", disabled = false, className = "" }) {
725
+ const [templates, setTemplates] = useState5([]);
726
+ const [chosen, setChosen] = useState5("");
727
+ const [description, setDescription] = useState5(null);
728
+ const [values, setValues] = useState5({});
729
+ const [message, setMessage] = useState5(null);
730
+ const [busy, setBusy] = useState5(false);
731
+ const supported = !!provider?.capabilities.supportsTemplates && !!provider.listTemplates && !!provider.describeTemplate;
732
+ useEffect5(() => {
733
+ let live = true;
734
+ setTemplates([]);
735
+ setChosen("");
736
+ if (supported) void Promise.resolve(provider.listTemplates()).then((t) => live && setTemplates(t), () => live && setTemplates([]));
737
+ return () => {
738
+ live = false;
739
+ };
740
+ }, [provider, supported]);
741
+ useEffect5(() => {
742
+ let live = true;
743
+ setDescription(null);
744
+ setMessage(null);
745
+ if (!chosen || !supported) return;
746
+ void Promise.resolve(provider.describeTemplate(chosen)).then(
747
+ (d) => {
748
+ if (!live) return;
749
+ setDescription(d);
750
+ setValues(Object.fromEntries(d.slots.map((s) => [s.name, isSubjectTemplateSlot(s) ? "" : s.default])));
751
+ },
752
+ (e) => live && setMessage(e.message)
753
+ );
754
+ return () => {
755
+ live = false;
756
+ };
757
+ }, [provider, supported, chosen]);
758
+ if (!supported || !templates.length) return null;
759
+ const given = Object.fromEntries(Object.entries(values).filter(([, v]) => v !== ""));
760
+ const problems = description ? checkTemplateValues(description.slots, given) : [];
761
+ const problemFor = (slot) => problems.find((p) => p.startsWith(`Slot "${slot}"`));
762
+ const fill = async () => {
763
+ if (!description || problems.length) return;
764
+ setBusy(true);
765
+ const r = await onFill(description.id, given);
766
+ setBusy(false);
767
+ if (r.ok) setChosen("");
768
+ else setMessage(r.error);
769
+ };
770
+ return /* @__PURE__ */ jsxs4("div", { className: `flex flex-col gap-1 text-xs ${className}`, "data-effect-templates": "", children: [
771
+ /* @__PURE__ */ jsxs4("select", { className: input, "aria-label": "Template", disabled, value: chosen, onChange: (e) => setChosen(e.target.value), children: [
772
+ /* @__PURE__ */ jsx5("option", { value: "", children: "Templates\u2026" }),
773
+ templates.map((t) => /* @__PURE__ */ jsxs4("option", { value: t.id, children: [
774
+ t.name,
775
+ " (",
776
+ t.shots,
777
+ " shots)"
778
+ ] }, t.id))
779
+ ] }),
780
+ description && /* @__PURE__ */ jsxs4(
781
+ "form",
782
+ {
783
+ className: "flex flex-col gap-1 rounded border border-border p-1",
784
+ "data-template-form": description.id,
785
+ onSubmit: (e) => {
786
+ e.preventDefault();
787
+ void fill();
788
+ },
789
+ children: [
790
+ /* @__PURE__ */ jsx5("p", { className: "text-[11px] text-muted-foreground", children: description.description }),
791
+ description.slots.map((slot) => /* @__PURE__ */ jsx5(SlotField, { slot, value: values[slot.name] ?? "", error: problemFor(slot.name), onChange: (v) => setValues({ ...values, [slot.name]: v }) }, slot.name)),
792
+ /* @__PURE__ */ jsxs4("div", { className: "flex gap-1", children: [
793
+ /* @__PURE__ */ jsx5("button", { type: "submit", className: btn, disabled: disabled || busy || problems.length > 0, children: fillLabel }),
794
+ /* @__PURE__ */ jsx5("button", { type: "button", className: btn, onClick: () => setChosen(""), children: "Cancel" })
795
+ ] })
796
+ ]
797
+ }
798
+ ),
799
+ message && /* @__PURE__ */ jsx5("p", { role: "alert", className: "text-[11px] text-destructive", children: message })
800
+ ] });
801
+ }
802
+
686
803
  export {
687
804
  useEffects,
688
805
  useEffectDescription,
@@ -690,5 +807,6 @@ export {
690
807
  EffectsBrowser,
691
808
  EffectParameters,
692
809
  useEffectAuthoring,
693
- EffectAuthoringEditor
810
+ EffectAuthoringEditor,
811
+ EffectTemplates
694
812
  };
@@ -0,0 +1,26 @@
1
+ import React from 'react';
2
+ import { type EffectProvider } from '@pixerate/editor/effects';
3
+ export interface EffectTemplatesProps {
4
+ /** The effects provider; nothing shows unless it has templates. */
5
+ provider: EffectProvider | null | undefined;
6
+ /**
7
+ * Fills the chosen template (the editor's `fillTemplate`). Resolves `{ ok: false, error }` to
8
+ * show the refusal; on success the form closes.
9
+ */
10
+ onFill: (template: string, values: Record<string, unknown>) => Promise<{
11
+ ok: true;
12
+ } | {
13
+ ok: false;
14
+ error: string;
15
+ }>;
16
+ /** The button that fills (default "Use template"). */
17
+ fillLabel?: string;
18
+ disabled?: boolean;
19
+ className?: string;
20
+ }
21
+ /**
22
+ * The provider's sequence templates: pick one, fill its slots in a form (built from
23
+ * `describeTemplate`, checked with `checkTemplateValues` as you type) and use it. Hidden when the
24
+ * provider has no templates.
25
+ */
26
+ export declare function EffectTemplates({ provider, onFill, fillLabel, disabled, className }: EffectTemplatesProps): React.JSX.Element | null;
@@ -0,0 +1,26 @@
1
+ import React from 'react';
2
+ import { type EffectProvider } from '@pixerate/editor/effects';
3
+ export interface EffectTemplatesProps {
4
+ /** The effects provider; nothing shows unless it has templates. */
5
+ provider: EffectProvider | null | undefined;
6
+ /**
7
+ * Fills the chosen template (the editor's `fillTemplate`). Resolves `{ ok: false, error }` to
8
+ * show the refusal; on success the form closes.
9
+ */
10
+ onFill: (template: string, values: Record<string, unknown>) => Promise<{
11
+ ok: true;
12
+ } | {
13
+ ok: false;
14
+ error: string;
15
+ }>;
16
+ /** The button that fills (default "Use template"). */
17
+ fillLabel?: string;
18
+ disabled?: boolean;
19
+ className?: string;
20
+ }
21
+ /**
22
+ * The provider's sequence templates: pick one, fill its slots in a form (built from
23
+ * `describeTemplate`, checked with `checkTemplateValues` as you type) and use it. Hidden when the
24
+ * provider has no templates.
25
+ */
26
+ export declare function EffectTemplates({ provider, onFill, fillLabel, disabled, className }: EffectTemplatesProps): React.JSX.Element | null;
@@ -33,6 +33,7 @@ var effects_exports = {};
33
33
  __export(effects_exports, {
34
34
  EffectAuthoringEditor: () => EffectAuthoringEditor,
35
35
  EffectParameters: () => EffectParameters,
36
+ EffectTemplates: () => EffectTemplates,
36
37
  EffectThumbnail: () => EffectThumbnail,
37
38
  EffectsBrowser: () => EffectsBrowser,
38
39
  useEffectAuthoring: () => useEffectAuthoring,
@@ -1307,10 +1308,10 @@ function ParameterNumberInput(props) {
1307
1308
  const resolved = resolveParameterComponents(components, ctx);
1308
1309
  const CustomInput = resolved.input;
1309
1310
  const plain = plainNumber(field, value);
1310
- const handleCommit = (text, input) => {
1311
+ const handleCommit = (text, input2) => {
1311
1312
  const n = (0, import_parameters.parseParameterNumberInput)(field, text);
1312
1313
  if (n === null || n === value) {
1313
- if (input) input.value = plain;
1314
+ if (input2) input2.value = plain;
1314
1315
  } else {
1315
1316
  onChange(n);
1316
1317
  }
@@ -2252,10 +2253,128 @@ function DurationField({ duration, onCommit }) {
2252
2253
  }
2253
2254
  );
2254
2255
  }
2256
+
2257
+ // src/effects/EffectTemplates.tsx
2258
+ var import_react12 = require("react");
2259
+ var import_effects5 = require("@pixerate/editor/effects");
2260
+ var import_jsx_runtime9 = require("react/jsx-runtime");
2261
+ var input = "w-full min-w-0 rounded border border-border bg-transparent px-1 py-0.5 text-xs";
2262
+ var btn = "rounded border border-border px-2 py-0.5 text-[11px] disabled:opacity-40";
2263
+ function SlotField({ slot, value, error, onChange }) {
2264
+ const id = `template-slot-${slot.name}`;
2265
+ let field;
2266
+ if (slot.type === "color") field = /* @__PURE__ */ (0, import_jsx_runtime9.jsx)("input", { id, className: "h-6 w-10", type: "color", value: String(value), onChange: (e) => onChange(e.target.value) });
2267
+ else if (slot.type === "number")
2268
+ field = /* @__PURE__ */ (0, import_jsx_runtime9.jsx)("input", { id, className: input, type: "number", min: slot.min, max: slot.max, step: slot.integer ? 1 : "any", value: String(value), onChange: (e) => onChange(e.target.value === "" ? "" : Number(e.target.value)) });
2269
+ else if (slot.type === "text")
2270
+ field = /* @__PURE__ */ (0, import_jsx_runtime9.jsxs)("div", { className: "flex items-center gap-1", children: [
2271
+ /* @__PURE__ */ (0, import_jsx_runtime9.jsx)("input", { id, className: input, maxLength: slot.maxLength, value: String(value), onChange: (e) => onChange(e.target.value) }),
2272
+ slot.maxLength !== void 0 && /* @__PURE__ */ (0, import_jsx_runtime9.jsxs)("span", { className: "text-[10px] text-muted-foreground", children: [
2273
+ String(value).length,
2274
+ "/",
2275
+ slot.maxLength
2276
+ ] })
2277
+ ] });
2278
+ else
2279
+ field = /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(
2280
+ "input",
2281
+ {
2282
+ id,
2283
+ className: input,
2284
+ placeholder: slot.hasDefault ? "URL or <svg> (default: a placeholder)" : "URL or <svg> (empty: left open)",
2285
+ value: typeof value === "string" ? value : "",
2286
+ onChange: (e) => onChange(e.target.value)
2287
+ }
2288
+ );
2289
+ return /* @__PURE__ */ (0, import_jsx_runtime9.jsxs)("div", { className: "flex flex-col gap-0.5", "data-template-slot": slot.name, children: [
2290
+ /* @__PURE__ */ (0, import_jsx_runtime9.jsx)("label", { htmlFor: id, className: "text-muted-foreground", title: slot.description, children: slot.label }),
2291
+ field,
2292
+ error && /* @__PURE__ */ (0, import_jsx_runtime9.jsx)("p", { className: "text-[11px] text-destructive", children: error })
2293
+ ] });
2294
+ }
2295
+ function EffectTemplates({ provider, onFill, fillLabel = "Use template", disabled = false, className = "" }) {
2296
+ const [templates, setTemplates] = (0, import_react12.useState)([]);
2297
+ const [chosen, setChosen] = (0, import_react12.useState)("");
2298
+ const [description, setDescription] = (0, import_react12.useState)(null);
2299
+ const [values, setValues] = (0, import_react12.useState)({});
2300
+ const [message, setMessage] = (0, import_react12.useState)(null);
2301
+ const [busy, setBusy] = (0, import_react12.useState)(false);
2302
+ const supported = !!provider?.capabilities.supportsTemplates && !!provider.listTemplates && !!provider.describeTemplate;
2303
+ (0, import_react12.useEffect)(() => {
2304
+ let live = true;
2305
+ setTemplates([]);
2306
+ setChosen("");
2307
+ if (supported) void Promise.resolve(provider.listTemplates()).then((t) => live && setTemplates(t), () => live && setTemplates([]));
2308
+ return () => {
2309
+ live = false;
2310
+ };
2311
+ }, [provider, supported]);
2312
+ (0, import_react12.useEffect)(() => {
2313
+ let live = true;
2314
+ setDescription(null);
2315
+ setMessage(null);
2316
+ if (!chosen || !supported) return;
2317
+ void Promise.resolve(provider.describeTemplate(chosen)).then(
2318
+ (d) => {
2319
+ if (!live) return;
2320
+ setDescription(d);
2321
+ setValues(Object.fromEntries(d.slots.map((s) => [s.name, (0, import_effects5.isSubjectTemplateSlot)(s) ? "" : s.default])));
2322
+ },
2323
+ (e) => live && setMessage(e.message)
2324
+ );
2325
+ return () => {
2326
+ live = false;
2327
+ };
2328
+ }, [provider, supported, chosen]);
2329
+ if (!supported || !templates.length) return null;
2330
+ const given = Object.fromEntries(Object.entries(values).filter(([, v]) => v !== ""));
2331
+ const problems = description ? (0, import_effects5.checkTemplateValues)(description.slots, given) : [];
2332
+ const problemFor = (slot) => problems.find((p) => p.startsWith(`Slot "${slot}"`));
2333
+ const fill = async () => {
2334
+ if (!description || problems.length) return;
2335
+ setBusy(true);
2336
+ const r = await onFill(description.id, given);
2337
+ setBusy(false);
2338
+ if (r.ok) setChosen("");
2339
+ else setMessage(r.error);
2340
+ };
2341
+ return /* @__PURE__ */ (0, import_jsx_runtime9.jsxs)("div", { className: `flex flex-col gap-1 text-xs ${className}`, "data-effect-templates": "", children: [
2342
+ /* @__PURE__ */ (0, import_jsx_runtime9.jsxs)("select", { className: input, "aria-label": "Template", disabled, value: chosen, onChange: (e) => setChosen(e.target.value), children: [
2343
+ /* @__PURE__ */ (0, import_jsx_runtime9.jsx)("option", { value: "", children: "Templates\u2026" }),
2344
+ templates.map((t) => /* @__PURE__ */ (0, import_jsx_runtime9.jsxs)("option", { value: t.id, children: [
2345
+ t.name,
2346
+ " (",
2347
+ t.shots,
2348
+ " shots)"
2349
+ ] }, t.id))
2350
+ ] }),
2351
+ description && /* @__PURE__ */ (0, import_jsx_runtime9.jsxs)(
2352
+ "form",
2353
+ {
2354
+ className: "flex flex-col gap-1 rounded border border-border p-1",
2355
+ "data-template-form": description.id,
2356
+ onSubmit: (e) => {
2357
+ e.preventDefault();
2358
+ void fill();
2359
+ },
2360
+ children: [
2361
+ /* @__PURE__ */ (0, import_jsx_runtime9.jsx)("p", { className: "text-[11px] text-muted-foreground", children: description.description }),
2362
+ description.slots.map((slot) => /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(SlotField, { slot, value: values[slot.name] ?? "", error: problemFor(slot.name), onChange: (v) => setValues({ ...values, [slot.name]: v }) }, slot.name)),
2363
+ /* @__PURE__ */ (0, import_jsx_runtime9.jsxs)("div", { className: "flex gap-1", children: [
2364
+ /* @__PURE__ */ (0, import_jsx_runtime9.jsx)("button", { type: "submit", className: btn, disabled: disabled || busy || problems.length > 0, children: fillLabel }),
2365
+ /* @__PURE__ */ (0, import_jsx_runtime9.jsx)("button", { type: "button", className: btn, onClick: () => setChosen(""), children: "Cancel" })
2366
+ ] })
2367
+ ]
2368
+ }
2369
+ ),
2370
+ message && /* @__PURE__ */ (0, import_jsx_runtime9.jsx)("p", { role: "alert", className: "text-[11px] text-destructive", children: message })
2371
+ ] });
2372
+ }
2255
2373
  // Annotate the CommonJS export names for ESM import in node:
2256
2374
  0 && (module.exports = {
2257
2375
  EffectAuthoringEditor,
2258
2376
  EffectParameters,
2377
+ EffectTemplates,
2259
2378
  EffectThumbnail,
2260
2379
  EffectsBrowser,
2261
2380
  useEffectAuthoring,
@@ -4,3 +4,4 @@ export * from './EffectsBrowser.cjs';
4
4
  export * from './EffectParameters.cjs';
5
5
  export * from './useEffectAuthoring.cjs';
6
6
  export * from './EffectAuthoringEditor.cjs';
7
+ export * from './EffectTemplates.cjs';
@@ -4,3 +4,4 @@ export * from './EffectsBrowser.js';
4
4
  export * from './EffectParameters.js';
5
5
  export * from './useEffectAuthoring.js';
6
6
  export * from './EffectAuthoringEditor.js';
7
+ export * from './EffectTemplates.js';
@@ -1,18 +1,20 @@
1
1
  import {
2
2
  EffectAuthoringEditor,
3
3
  EffectParameters,
4
+ EffectTemplates,
4
5
  EffectThumbnail,
5
6
  EffectsBrowser,
6
7
  useEffectAuthoring,
7
8
  useEffectDescription,
8
9
  useEffects
9
- } from "../chunk-IDUWADYT.js";
10
+ } from "../chunk-TOYSB4DK.js";
10
11
  import "../chunk-5RPB4WGT.js";
11
12
  import "../chunk-KIRIU53A.js";
12
13
  import "../chunk-3I7FOSLF.js";
13
14
  export {
14
15
  EffectAuthoringEditor,
15
16
  EffectParameters,
17
+ EffectTemplates,
16
18
  EffectThumbnail,
17
19
  EffectsBrowser,
18
20
  useEffectAuthoring,
@@ -36,9 +36,9 @@ import {
36
36
  useImageEditorControls,
37
37
  useImageEffectsTools,
38
38
  useImageHistogram
39
- } from "../chunk-W7455SH4.js";
39
+ } from "../chunk-65EL63YS.js";
40
40
  import "../chunk-AJCWF6SN.js";
41
- import "../chunk-IDUWADYT.js";
41
+ import "../chunk-TOYSB4DK.js";
42
42
  import "../chunk-5RPB4WGT.js";
43
43
  import "../chunk-KIRIU53A.js";
44
44
  import "../chunk-3I7FOSLF.js";
package/dist/index.js CHANGED
@@ -60,9 +60,9 @@ import {
60
60
  useImageEditorControls,
61
61
  useImageEffectsTools,
62
62
  useImageHistogram
63
- } from "./chunk-W7455SH4.js";
63
+ } from "./chunk-65EL63YS.js";
64
64
  import "./chunk-AJCWF6SN.js";
65
- import "./chunk-IDUWADYT.js";
65
+ import "./chunk-TOYSB4DK.js";
66
66
  import "./chunk-5RPB4WGT.js";
67
67
  import "./chunk-KIRIU53A.js";
68
68
  import "./chunk-3I7FOSLF.js";
@@ -5,7 +5,7 @@ import {
5
5
  EffectParameters,
6
6
  EffectsBrowser,
7
7
  useEffectDescription
8
- } from "../chunk-IDUWADYT.js";
8
+ } from "../chunk-TOYSB4DK.js";
9
9
  import "../chunk-5RPB4WGT.js";
10
10
  import "../chunk-KIRIU53A.js";
11
11
  import "../chunk-3I7FOSLF.js";
@@ -3,7 +3,7 @@ import {
3
3
  EffectsBrowser,
4
4
  useEffectDescription,
5
5
  useEffects
6
- } from "../chunk-IDUWADYT.js";
6
+ } from "../chunk-TOYSB4DK.js";
7
7
  import "../chunk-5RPB4WGT.js";
8
8
  import "../chunk-KIRIU53A.js";
9
9
  import "../chunk-3I7FOSLF.js";