sanity-plugin-studio-scene 0.0.0-stage → 0.1.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 +21 -0
- package/README.md +74 -2
- package/dist/PlacementInput.d.ts +9 -0
- package/dist/PlacementInput.js +244 -0
- package/dist/SceneInput.d.ts +6 -0
- package/dist/SceneInput.js +144 -0
- package/dist/ScenePreview.d.ts +40 -0
- package/dist/ScenePreview.js +211 -0
- package/dist/groq.d.ts +9 -0
- package/dist/groq.js +26 -0
- package/dist/importExport.d.ts +52 -0
- package/dist/importExport.js +60 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +29 -0
- package/dist/merge.d.ts +59 -0
- package/dist/merge.js +68 -0
- package/dist/options.d.ts +60 -0
- package/dist/options.js +49 -0
- package/dist/runtime/scroll-scene.js +16739 -0
- package/dist/schema/motion.d.ts +20 -0
- package/dist/schema/motion.js +63 -0
- package/dist/schema/placement.d.ts +30 -0
- package/dist/schema/placement.js +96 -0
- package/dist/schema/scene.d.ts +43 -0
- package/dist/schema/scene.js +104 -0
- package/dist/schema/style.d.ts +11 -0
- package/dist/schema/style.js +75 -0
- package/dist/styleTokens.d.ts +131 -0
- package/dist/styleTokens.js +207 -0
- package/dist/unzip.d.ts +25 -0
- package/dist/unzip.js +114 -0
- package/dist/useScene.d.ts +21 -0
- package/dist/useScene.js +37 -0
- package/package.json +27 -4
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { Box, Button, Card, Flex, Text } from "@sanity/ui";
|
|
3
|
+
import { useEffect, useLayoutEffect, useRef, useState } from "react";
|
|
4
|
+
import { MOTION_CSS, PADDING, WIDTH, motionVars, pinVars } from "./styleTokens";
|
|
5
|
+
let runtime = null;
|
|
6
|
+
function loadRuntime(url) {
|
|
7
|
+
if (customElements.get("scroll-scene"))
|
|
8
|
+
return Promise.resolve();
|
|
9
|
+
// The bundled copy is code-split, so the Studio only downloads it when a preview opens.
|
|
10
|
+
runtime ??= url ? import(/* @vite-ignore */ url) : import("./runtime/scroll-scene.js");
|
|
11
|
+
return runtime;
|
|
12
|
+
}
|
|
13
|
+
const FRAME_W = 1280, FRAME_H = 720;
|
|
14
|
+
/** "Play" in the preview: --stop-t through a camera move in, a hold, and the move out, then back to the hold. */
|
|
15
|
+
const PLAY = [[0, -1], [1100, 0], [2300, 1], [3100, 1.6], [3500, 1.6], [3800, 0.5]];
|
|
16
|
+
/**
|
|
17
|
+
* The scene, live, with the shot's content groups laid over it the way the page will style and move them.
|
|
18
|
+
* The element is `driven`: the preview moves the camera (goTo a shot, or the scrubber), not page scroll.
|
|
19
|
+
* "Play" runs the content's motion (in, hold, out) with the same CSS as the page.
|
|
20
|
+
*/
|
|
21
|
+
export function ScenePreview(props) {
|
|
22
|
+
const host = useRef(null);
|
|
23
|
+
const frame = useRef(null);
|
|
24
|
+
const layer = useRef(null);
|
|
25
|
+
const el = useRef(null);
|
|
26
|
+
const [scale, setScale] = useState(0.5);
|
|
27
|
+
const [progress, setProgress] = useState(0);
|
|
28
|
+
const [stops, setStops] = useState([]);
|
|
29
|
+
const [error, setError] = useState(null);
|
|
30
|
+
const [playing, setPlaying] = useState(false);
|
|
31
|
+
const [pins, setPins] = useState({});
|
|
32
|
+
useEffect(() => {
|
|
33
|
+
let gone = false;
|
|
34
|
+
void loadRuntime(props.runtimeUrl).then(() => {
|
|
35
|
+
if (gone || !host.current)
|
|
36
|
+
return;
|
|
37
|
+
const e = document.createElement("scroll-scene");
|
|
38
|
+
e.setAttribute("driven", "");
|
|
39
|
+
e.style.cssText = "display:block;position:absolute;inset:0;width:100%;height:100%";
|
|
40
|
+
e.files = props.files;
|
|
41
|
+
const read = () => setStops(e.shots.filter((s) => s.stop).map((s) => ({ slug: s.stop, at: s.at })));
|
|
42
|
+
e.addEventListener("ready", read);
|
|
43
|
+
e.addEventListener("shotchange", read);
|
|
44
|
+
e.addEventListener("progress", (ev) => setProgress(ev.detail.progress));
|
|
45
|
+
e.addEventListener("error", (ev) => setError(String(ev.detail?.message ?? "The scene couldn't load.")));
|
|
46
|
+
host.current.appendChild(e);
|
|
47
|
+
el.current = e;
|
|
48
|
+
}, (err) => setError(`Couldn't load the preview runtime: ${err instanceof Error ? err.message : err}`));
|
|
49
|
+
return () => { gone = true; el.current?.remove(); el.current = null; };
|
|
50
|
+
// Mount once; files and focus are pushed below.
|
|
51
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
52
|
+
}, []);
|
|
53
|
+
useEffect(() => { if (el.current)
|
|
54
|
+
el.current.files = props.files; }, [props.files]);
|
|
55
|
+
useEffect(() => {
|
|
56
|
+
const slug = props.focus;
|
|
57
|
+
if (!slug)
|
|
58
|
+
return;
|
|
59
|
+
let tries = 0;
|
|
60
|
+
// The shot list exists once timeline.json is in; retry briefly after mount or a new export.
|
|
61
|
+
const go = () => {
|
|
62
|
+
const e = el.current;
|
|
63
|
+
if (e && e.shots.some((s) => s.stop === slug)) {
|
|
64
|
+
e.goTo(slug);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
if (tries++ < 40)
|
|
68
|
+
t = setTimeout(go, 150);
|
|
69
|
+
};
|
|
70
|
+
let t = setTimeout(go, 0);
|
|
71
|
+
return () => clearTimeout(t);
|
|
72
|
+
}, [props.focus, props.files]);
|
|
73
|
+
useLayoutEffect(() => {
|
|
74
|
+
const f = frame.current;
|
|
75
|
+
if (!f)
|
|
76
|
+
return;
|
|
77
|
+
const ro = new ResizeObserver(() => setScale(f.clientWidth / FRAME_W));
|
|
78
|
+
ro.observe(f);
|
|
79
|
+
return () => ro.disconnect();
|
|
80
|
+
}, []);
|
|
81
|
+
// Pinned groups follow their point: read where the runtime projects it, every frame while any is pinned.
|
|
82
|
+
const pinned = props.groups.map((g) => g.pin).filter(Boolean).join(",");
|
|
83
|
+
useEffect(() => {
|
|
84
|
+
if (!pinned) {
|
|
85
|
+
setPins({});
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
let raf = 0;
|
|
89
|
+
const tick = () => {
|
|
90
|
+
const e = el.current;
|
|
91
|
+
if (e?.pointPosition) {
|
|
92
|
+
const next = {};
|
|
93
|
+
for (const slug of pinned.split(",")) {
|
|
94
|
+
const p = e.pointPosition(slug);
|
|
95
|
+
if (p)
|
|
96
|
+
next[slug] = p;
|
|
97
|
+
}
|
|
98
|
+
setPins((prev) => (JSON.stringify(prev) === JSON.stringify(next) ? prev : next));
|
|
99
|
+
}
|
|
100
|
+
raf = requestAnimationFrame(tick);
|
|
101
|
+
};
|
|
102
|
+
raf = requestAnimationFrame(tick);
|
|
103
|
+
return () => cancelAnimationFrame(raf);
|
|
104
|
+
}, [pinned]);
|
|
105
|
+
// Each play gets an id; a newer play (or a replay) takes over from one still running.
|
|
106
|
+
const run = useRef(0);
|
|
107
|
+
const play = () => {
|
|
108
|
+
const l = layer.current;
|
|
109
|
+
if (!l)
|
|
110
|
+
return;
|
|
111
|
+
const id = ++run.current;
|
|
112
|
+
setPlaying(true);
|
|
113
|
+
const t0 = performance.now();
|
|
114
|
+
const step = () => {
|
|
115
|
+
if (id !== run.current)
|
|
116
|
+
return;
|
|
117
|
+
const ms = performance.now() - t0;
|
|
118
|
+
let i = 1;
|
|
119
|
+
while (i < PLAY.length - 1 && ms > PLAY[i][0])
|
|
120
|
+
i++;
|
|
121
|
+
const [a, b] = [PLAY[i - 1], PLAY[i]];
|
|
122
|
+
const k = Math.min(1, Math.max(0, (ms - a[0]) / (b[0] - a[0])));
|
|
123
|
+
l.style.setProperty("--stop-t", String(a[1] + (b[1] - a[1]) * k));
|
|
124
|
+
if (ms < PLAY[PLAY.length - 1][0])
|
|
125
|
+
requestAnimationFrame(step);
|
|
126
|
+
else {
|
|
127
|
+
l.style.removeProperty("--stop-t");
|
|
128
|
+
setPlaying(false);
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
requestAnimationFrame(step);
|
|
132
|
+
};
|
|
133
|
+
// Replay when the settings change (not on first show).
|
|
134
|
+
const seen = useRef(props.replay);
|
|
135
|
+
useEffect(() => {
|
|
136
|
+
if (props.replay === seen.current)
|
|
137
|
+
return;
|
|
138
|
+
seen.current = props.replay;
|
|
139
|
+
play();
|
|
140
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
141
|
+
}, [props.replay]);
|
|
142
|
+
const scrub = (v) => { if (el.current)
|
|
143
|
+
el.current.progress = v; };
|
|
144
|
+
return (_jsxs(Card, { radius: 2, overflow: "hidden", border: true, children: [_jsx("style", { children: MOTION_CSS }), _jsxs("div", { ref: frame, style: { position: "relative", aspectRatio: `${FRAME_W} / ${FRAME_H}`, background: "#111", overflow: "hidden" }, children: [_jsx("div", { ref: host, style: { position: "absolute", inset: 0 } }), props.groups.length ? (_jsx("div", { ref: layer, className: "ss-motion", style: { position: "absolute", left: 0, top: 0, width: FRAME_W, height: FRAME_H, transform: `scale(${scale})`, transformOrigin: "0 0", pointerEvents: "none", display: "grid", padding: 64, boxSizing: "border-box" }, children: props.groups.map((g) => {
|
|
145
|
+
const at = g.pin ? pins[g.pin] : undefined;
|
|
146
|
+
return (_jsx(Group, { group: g, pinAt: g.pin ? (at ? { x: at.x / scale, y: at.y / scale, visible: at.visible } : { x: FRAME_W / 2, y: FRAME_H / 2, visible: true }) : undefined }, g.key));
|
|
147
|
+
}) })) : null, error ? (_jsx(Box, { padding: 3, style: { position: "absolute", inset: "auto 0 0 0", background: "rgba(0,0,0,.7)" }, children: _jsx(Text, { size: 1, style: { color: "#fff" }, children: error }) })) : null] }), _jsxs(Flex, { gap: 2, padding: 2, align: "center", children: [props.groups.length ? _jsx(Button, { text: playing ? "Playing…" : "▶ Play animation", mode: "ghost", padding: 2, fontSize: 1, onClick: play }) : null, _jsx("input", { type: "range", min: 0, max: 1000, value: Math.round(progress * 1000), onChange: (e) => scrub(Number(e.currentTarget.value) / 1000), style: { flex: 1 }, "aria-label": "Scrub the scene" }), stops.map((s, i) => (_jsx(Button, { text: String(i + 1), mode: props.focus === s.slug ? "default" : "ghost", padding: 2, fontSize: 1, title: `Show shot ${s.slug}`, onClick: () => (props.onPick ? props.onPick(s.slug) : el.current?.goTo(s.slug)) }, s.slug)))] })] }));
|
|
148
|
+
}
|
|
149
|
+
/** One group, as the page draws it by default (the site's CSS has the last word on the real page). */
|
|
150
|
+
function Group({ group: g, pinAt }) {
|
|
151
|
+
const s = g.style;
|
|
152
|
+
const card = s.card ?? "card";
|
|
153
|
+
const cardColor = s.cardColor ?? "#1b1b1f";
|
|
154
|
+
const opacity = s.cardOpacity ?? 82;
|
|
155
|
+
const box = {
|
|
156
|
+
...motionVars(g.motion),
|
|
157
|
+
gridArea: "1 / 1",
|
|
158
|
+
justifySelf: pinAt ? undefined : s.align ?? "start",
|
|
159
|
+
alignSelf: pinAt ? undefined : s.verticalAlign ?? "center",
|
|
160
|
+
width: `min(${WIDTH[s.width ?? "medium"]}, 100%)`,
|
|
161
|
+
boxSizing: "border-box",
|
|
162
|
+
padding: PADDING[s.padding ?? "medium"],
|
|
163
|
+
borderRadius: s.radius ?? 16,
|
|
164
|
+
color: s.textColor ?? "#f4f4f5",
|
|
165
|
+
fontSize: 18 * (s.fontScale ?? 1),
|
|
166
|
+
lineHeight: 1.5,
|
|
167
|
+
textAlign: s.textAlign === "end" ? "right" : s.textAlign === "center" ? "center" : "left",
|
|
168
|
+
background: card === "card" ? `color-mix(in srgb, ${cardColor} ${opacity}%, transparent)` : "transparent",
|
|
169
|
+
border: card === "outline" ? `1px solid ${s.textColor ?? "rgba(255,255,255,.5)"}` : undefined,
|
|
170
|
+
backdropFilter: card === "card" && (s.blur ?? 12) > 0 ? `blur(${s.blur ?? 12}px)` : undefined,
|
|
171
|
+
fontFamily: "system-ui, sans-serif",
|
|
172
|
+
outline: g.selected ? "2px dashed rgba(34,118,252,.7)" : undefined,
|
|
173
|
+
outlineOffset: 4,
|
|
174
|
+
};
|
|
175
|
+
const inner = (_jsx("div", { className: "ss-group", "data-stagger": g.motion.stagger ? "" : undefined, style: box, children: g.blocks.map((b, i) => _jsx("div", { className: "ss-unit", style: { "--i": i }, children: b }, i)) }));
|
|
176
|
+
if (!pinAt)
|
|
177
|
+
return inner;
|
|
178
|
+
// Pinned: the card sits on its side of the point; a callout adds the line and the dot (MOTION_CSS, as on the page).
|
|
179
|
+
const vars = {
|
|
180
|
+
...pinVars(g.ownStyle, g.line, card),
|
|
181
|
+
"--px": `${pinAt.x}px`, "--py": `${pinAt.y}px`, "--p-on": pinAt.visible ? "1" : "0",
|
|
182
|
+
"--slot-width": "360px", "--slot-color": s.textColor ?? "#f4f4f5", "--slot-card-color": cardColor,
|
|
183
|
+
};
|
|
184
|
+
return (_jsxs("div", { className: "ss-pin", "data-pinned": "", style: { ...vars, position: "absolute" }, children: [g.line ? (_jsxs(_Fragment, { children: [_jsx("span", { className: "ss-pin-line" }), _jsx("span", { className: "ss-pin-dot" })] })) : null, _jsx("div", { className: "ss-pin-box", children: inner })] }));
|
|
185
|
+
}
|
|
186
|
+
const ptText = (b) => (b.children ?? []).map((c) => c.text ?? "").join("");
|
|
187
|
+
/**
|
|
188
|
+
* Content blocks, roughly, for the preview overlay. It knows Portable Text and reads any other block by its
|
|
189
|
+
* common fields (eyebrow, heading/title, body/text, buttons, items, image), so a site's own blocks show up as
|
|
190
|
+
* a sketch without the plugin knowing them. The real page renders them with the site's components.
|
|
191
|
+
*/
|
|
192
|
+
export function PreviewBlocks({ blocks }) {
|
|
193
|
+
const list = (blocks ?? []);
|
|
194
|
+
if (!list.length)
|
|
195
|
+
return null;
|
|
196
|
+
const para = (key, text, size = 1, weight) => text ? _jsx("p", { style: { margin: "0 0 .5em", fontSize: `${size}em`, fontWeight: weight, lineHeight: size > 1 ? 1.15 : undefined }, children: text }, key) : null;
|
|
197
|
+
return (_jsx(_Fragment, { children: list.map((b, i) => {
|
|
198
|
+
const k = b._key ?? i;
|
|
199
|
+
if (b._type === "block") {
|
|
200
|
+
const size = b.style === "h2" ? 1.8 : b.style === "h3" ? 1.4 : b.style === "h4" ? 1.15 : 1;
|
|
201
|
+
return para(k, `${b.listItem ? "• " : ""}${ptText(b)}`, size, size > 1 ? 650 : undefined);
|
|
202
|
+
}
|
|
203
|
+
const str = (f) => (typeof b[f] === "string" ? b[f] : "");
|
|
204
|
+
const body = Array.isArray(b.body) ? b.body.map(ptText).join(" ") : str("text");
|
|
205
|
+
const size = b.size === "large" ? 2.2 : b.size === "small" ? 1.2 : 1.7;
|
|
206
|
+
const buttons = Array.isArray(b.buttons) ? b.buttons : [];
|
|
207
|
+
const items = Array.isArray(b.items) ? b.items : [];
|
|
208
|
+
const hasImage = !!(b.image || b.video);
|
|
209
|
+
return (_jsxs("div", { style: { marginBottom: ".8em" }, children: [para("e", str("eyebrow"), 0.75, 600), para("h", str("heading") || str("title"), size, 650), para("b", body), buttons.length ? (_jsx("div", { style: { display: "flex", gap: 8, flexWrap: "wrap" }, children: buttons.map((x, j) => (_jsx("span", { style: { padding: "8px 16px", borderRadius: 999, fontSize: ".85em", background: x.style === "secondary" ? "transparent" : "currentColor", border: "1px solid currentColor" }, children: _jsx("span", { style: { filter: x.style === "secondary" ? undefined : "invert(1)" }, children: x.label || "Button" }) }, x._key ?? j))) })) : null, items.length ? (_jsx("div", { style: { display: "grid", gridTemplateColumns: `repeat(${Math.min(Number(b.columns) || 3, items.length)}, 1fr)`, gap: 12 }, children: items.map((x, j) => (_jsxs("div", { style: { border: "1px solid rgba(127,127,127,.35)", borderRadius: 8, padding: 10, fontSize: ".8em" }, children: [_jsx("strong", { children: x.title }), x.text ? _jsx("div", { style: { opacity: 0.75 }, children: x.text }) : null] }, x._key ?? j))) })) : null, hasImage ? _jsxs("div", { style: { aspectRatio: "16 / 9", borderRadius: 8, background: "rgba(127,127,127,.3)", display: "grid", placeItems: "center", fontSize: ".8em" }, children: [b.video ? "Video" : "Image", str("caption") ? ` · ${str("caption")}` : ""] }) : null] }, k));
|
|
210
|
+
}) }));
|
|
211
|
+
}
|
package/dist/groq.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GROQ for a placed scene, ready for a frontend: the scene's files as a <scroll-scene files> map source,
|
|
3
|
+
* its shots (`stops`, in scroll order), and this page's text per shot (`slots`, keyed by shot id) and look.
|
|
4
|
+
*
|
|
5
|
+
* *[_type == "page" && slug.current == $slug][0]{ ..., sections[]{ ..., _type == "studioScenePlacement" => ${studioScenePlacementProjection()} } }
|
|
6
|
+
*
|
|
7
|
+
* Pass your own projection for content if it holds references or images to resolve, e.g. `content[]{ ..., asset-> }`.
|
|
8
|
+
*/
|
|
9
|
+
export declare function studioScenePlacementProjection(content?: string): string;
|
package/dist/groq.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GROQ for a placed scene, ready for a frontend: the scene's files as a <scroll-scene files> map source,
|
|
3
|
+
* its shots (`stops`, in scroll order), and this page's text per shot (`slots`, keyed by shot id) and look.
|
|
4
|
+
*
|
|
5
|
+
* *[_type == "page" && slug.current == $slug][0]{ ..., sections[]{ ..., _type == "studioScenePlacement" => ${studioScenePlacementProjection()} } }
|
|
6
|
+
*
|
|
7
|
+
* Pass your own projection for content if it holds references or images to resolve, e.g. `content[]{ ..., asset-> }`.
|
|
8
|
+
*/
|
|
9
|
+
export function studioScenePlacementProjection(content = "content") {
|
|
10
|
+
const c = content === "content" ? "content" : `"content": ${content}`;
|
|
11
|
+
return /* groq */ `{
|
|
12
|
+
_key,
|
|
13
|
+
_type,
|
|
14
|
+
style,
|
|
15
|
+
motion,
|
|
16
|
+
"slots": slots[]{ _key, style, motion, pin, line, ${c}, "groups": groups[]{ _key, style, motion, pin, line, ${c} } },
|
|
17
|
+
"scene": scene->{
|
|
18
|
+
_id,
|
|
19
|
+
title,
|
|
20
|
+
"posterUrl": export.posterUrl,
|
|
21
|
+
"files": files[]{ path, "url": asset.asset->url },
|
|
22
|
+
"stops": stops[]{ "id": _key, slug, label, camera, thumbUrl },
|
|
23
|
+
"points": points[]{ name, slug }
|
|
24
|
+
}
|
|
25
|
+
}`;
|
|
26
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { SanityClient } from "sanity";
|
|
2
|
+
import { type ManifestStop, type SceneStop } from "./merge";
|
|
3
|
+
import { type ZipFile } from "./unzip";
|
|
4
|
+
export interface Timeline {
|
|
5
|
+
version: number;
|
|
6
|
+
stops?: ManifestStop[];
|
|
7
|
+
poster?: string;
|
|
8
|
+
points?: {
|
|
9
|
+
name: string;
|
|
10
|
+
slug: string;
|
|
11
|
+
}[];
|
|
12
|
+
}
|
|
13
|
+
export interface ReadExport {
|
|
14
|
+
file: File;
|
|
15
|
+
timeline: Timeline;
|
|
16
|
+
keep: ZipFile[];
|
|
17
|
+
bytes: number;
|
|
18
|
+
}
|
|
19
|
+
export interface ExportMeta {
|
|
20
|
+
fileName?: string;
|
|
21
|
+
importedAt?: string;
|
|
22
|
+
timelineUrl?: string;
|
|
23
|
+
posterUrl?: string;
|
|
24
|
+
bytes?: number;
|
|
25
|
+
stops?: number;
|
|
26
|
+
added?: number;
|
|
27
|
+
removed?: number;
|
|
28
|
+
}
|
|
29
|
+
export interface Uploaded {
|
|
30
|
+
files: {
|
|
31
|
+
_type: string;
|
|
32
|
+
_key: string;
|
|
33
|
+
path: string;
|
|
34
|
+
asset: unknown;
|
|
35
|
+
}[];
|
|
36
|
+
stops: SceneStop[];
|
|
37
|
+
/** Web points marked in STUDIO, for pinning content to the 3D. */
|
|
38
|
+
points: {
|
|
39
|
+
_type: string;
|
|
40
|
+
_key: string;
|
|
41
|
+
name: string;
|
|
42
|
+
slug: string;
|
|
43
|
+
}[];
|
|
44
|
+
meta: ExportMeta;
|
|
45
|
+
}
|
|
46
|
+
export declare const mb: (n: number) => string;
|
|
47
|
+
export declare const titleFrom: (fileName: string) => string;
|
|
48
|
+
/** Unzip and check a STUDIO export. Nothing is uploaded yet. */
|
|
49
|
+
export declare function readExport(file: File): Promise<ReadExport>;
|
|
50
|
+
/** Upload what the page needs as Sanity assets; returns the scene fields to write. */
|
|
51
|
+
export declare function uploadExport(client: SanityClient, r: ReadExport, onStatus: (text: string) => void): Promise<Uploaded>;
|
|
52
|
+
export declare const historyKey: (path: string) => string;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { checkKept, keepFile, sceneStops } from "./merge";
|
|
2
|
+
import { T } from "./options";
|
|
3
|
+
import { exportPaths, unzip } from "./unzip";
|
|
4
|
+
/** Same cap as the STUDIO editor's JSON documents. */
|
|
5
|
+
const TIMELINE_MAX = 8 * 1024 * 1024;
|
|
6
|
+
const keyOf = (path) => path.replace(/[^a-zA-Z0-9]/g, "_").slice(-60) || "file";
|
|
7
|
+
export const mb = (n) => `${(n / 1024 / 1024).toFixed(1)} MB`;
|
|
8
|
+
export const titleFrom = (fileName) => fileName.replace(/\.zip$/i, "").replace(/[-_]+/g, " ").trim() || "Scroll scene";
|
|
9
|
+
/** Unzip and check a STUDIO export. Nothing is uploaded yet. */
|
|
10
|
+
export async function readExport(file) {
|
|
11
|
+
const all = exportPaths(await unzip(await file.arrayBuffer()));
|
|
12
|
+
const tl = all.find((f) => f.path === "timeline.json");
|
|
13
|
+
if (!tl)
|
|
14
|
+
throw new Error("No timeline.json in this zip. Use the scroll-scene.zip that STUDIO exports.");
|
|
15
|
+
if (tl.data.byteLength > TIMELINE_MAX)
|
|
16
|
+
throw new Error(`timeline.json is larger than ${mb(TIMELINE_MAX)}.`);
|
|
17
|
+
let timeline;
|
|
18
|
+
try {
|
|
19
|
+
timeline = JSON.parse(new TextDecoder().decode(tl.data));
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
throw new Error("timeline.json isn't valid JSON. Use the scroll-scene.zip that STUDIO exports.");
|
|
23
|
+
}
|
|
24
|
+
if (!timeline || typeof timeline !== "object")
|
|
25
|
+
throw new Error("timeline.json isn't a STUDIO timeline.");
|
|
26
|
+
if (timeline.version !== 1)
|
|
27
|
+
throw new Error(`This export's format (version ${timeline.version}) isn't supported by this plugin.`);
|
|
28
|
+
if (!Array.isArray(timeline.stops))
|
|
29
|
+
throw new Error("This export is from an older STUDIO version. Export it again.");
|
|
30
|
+
const keep = all.filter((f) => keepFile(f.path));
|
|
31
|
+
for (const f of keep)
|
|
32
|
+
checkKept(f.path, f.data);
|
|
33
|
+
return { file, timeline, keep, bytes: keep.reduce((n, f) => n + f.data.byteLength, 0) };
|
|
34
|
+
}
|
|
35
|
+
/** Upload what the page needs as Sanity assets; returns the scene fields to write. */
|
|
36
|
+
export async function uploadExport(client, r, onStatus) {
|
|
37
|
+
const urls = new Map();
|
|
38
|
+
const files = [];
|
|
39
|
+
for (const [i, f] of r.keep.entries()) {
|
|
40
|
+
onStatus(`Uploading ${i + 1} of ${r.keep.length}: ${f.path}`);
|
|
41
|
+
const doc = await client.assets.upload("file", new Blob([f.data]), { filename: f.path.split("/").pop(), source: { name: "studio-scene", id: f.path } });
|
|
42
|
+
files.push({ _type: T.sceneFile, _key: keyOf(f.path), path: f.path, asset: { _type: "file", asset: { _type: "reference", _ref: doc._id } } });
|
|
43
|
+
urls.set(f.path, doc.url);
|
|
44
|
+
}
|
|
45
|
+
const stops = r.timeline.stops;
|
|
46
|
+
return {
|
|
47
|
+
files,
|
|
48
|
+
stops: sceneStops(stops, (p) => urls.get(p)),
|
|
49
|
+
points: (r.timeline.points ?? []).map((p) => ({ _type: T.point, _key: p.slug, name: p.name, slug: p.slug })),
|
|
50
|
+
meta: {
|
|
51
|
+
fileName: r.file.name,
|
|
52
|
+
importedAt: new Date().toISOString(),
|
|
53
|
+
timelineUrl: urls.get("timeline.json"),
|
|
54
|
+
posterUrl: r.timeline.poster ? urls.get(r.timeline.poster) : undefined,
|
|
55
|
+
bytes: r.bytes,
|
|
56
|
+
stops: stops.length,
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
export const historyKey = keyOf;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type StudioSceneOptions } from "./options";
|
|
2
|
+
export declare const studioScene: import("sanity").Plugin<void | StudioSceneOptions>;
|
|
3
|
+
export type { PaletteColor, StudioSceneOptions, StyleControl, StylePreset } from "./options";
|
|
4
|
+
export { DEFAULT_PRESETS, STYLE_CONTROLS, T as studioSceneTypeNames } from "./options";
|
|
5
|
+
export { studioScenePlacementProjection } from "./groq";
|
|
6
|
+
export { styleVars, mergeStyle, motionVars, mergeMotion, MOTION_CSS, type SlotStyle, type SlotMotion } from "./styleTokens";
|
|
7
|
+
export { diffStops, keepFile, moveSlot } from "./merge";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* studio-scene: STUDIO scroll scenes in Sanity.
|
|
3
|
+
*
|
|
4
|
+
* plugins: [studioScene({ stopContent: [{ type: "block" }], styles: { colors: [...] } })]
|
|
5
|
+
*
|
|
6
|
+
* Adds two types:
|
|
7
|
+
* studioScene a document per STUDIO export (the scene library): drop the zip, its files become assets
|
|
8
|
+
* studioScenePlacement a scene placed on a page (a field or a page-builder section): pick a scene (or upload
|
|
9
|
+
* one), pick a shot on the strip and write the text over it, with a live preview.
|
|
10
|
+
* Text on a shot stops the camera there while it scrolls past.
|
|
11
|
+
* Published as `sanity-plugin-studio-scene` (studio/packages/sanity-plugin-studio-scene); the ctb Studio uses this source directly.
|
|
12
|
+
*/
|
|
13
|
+
import { definePlugin } from "sanity";
|
|
14
|
+
import { resolveOptions } from "./options";
|
|
15
|
+
import { placementTypes } from "./schema/placement";
|
|
16
|
+
import { sceneTypes } from "./schema/scene";
|
|
17
|
+
import { styleType } from "./schema/style";
|
|
18
|
+
import { motionType } from "./schema/motion";
|
|
19
|
+
export const studioScene = definePlugin((o) => {
|
|
20
|
+
const opts = resolveOptions(o);
|
|
21
|
+
return {
|
|
22
|
+
name: "studio-scene",
|
|
23
|
+
schema: { types: [...sceneTypes(), styleType(opts), motionType(), ...placementTypes(opts)] },
|
|
24
|
+
};
|
|
25
|
+
});
|
|
26
|
+
export { DEFAULT_PRESETS, STYLE_CONTROLS, T as studioSceneTypeNames } from "./options";
|
|
27
|
+
export { studioScenePlacementProjection } from "./groq";
|
|
28
|
+
export { styleVars, mergeStyle, motionVars, mergeMotion, MOTION_CSS } from "./styleTokens";
|
|
29
|
+
export { diffStops, keepFile, moveSlot } from "./merge";
|
package/dist/merge.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stops and slots. A scene document lists its stops (from the export's timeline.json), keyed by the shot id;
|
|
3
|
+
* each page placement keys its slots on the same ids. So re-exporting never touches page content: slots of
|
|
4
|
+
* stops that still exist carry on, new stops have no slot yet, and slots of removed stops are "orphans" the
|
|
5
|
+
* editor can move or delete. Nothing an editor wrote is deleted by an upload.
|
|
6
|
+
*/
|
|
7
|
+
export interface ManifestStop {
|
|
8
|
+
id: string;
|
|
9
|
+
slug: string;
|
|
10
|
+
label: string;
|
|
11
|
+
camera?: string;
|
|
12
|
+
thumb?: string;
|
|
13
|
+
}
|
|
14
|
+
/** A stop as stored on the scene document. */
|
|
15
|
+
export interface SceneStop {
|
|
16
|
+
_key: string;
|
|
17
|
+
_type: "studioSceneStop";
|
|
18
|
+
slug: string;
|
|
19
|
+
label: string;
|
|
20
|
+
camera?: string;
|
|
21
|
+
thumb?: string;
|
|
22
|
+
thumbUrl?: string;
|
|
23
|
+
}
|
|
24
|
+
export interface Slot {
|
|
25
|
+
_key: string;
|
|
26
|
+
_type: "studioSlot";
|
|
27
|
+
hidden?: boolean;
|
|
28
|
+
content?: unknown[];
|
|
29
|
+
style?: Record<string, unknown>;
|
|
30
|
+
}
|
|
31
|
+
export declare function sceneStops(stops: ManifestStop[], urlOf: (path: string) => string | undefined): SceneStop[];
|
|
32
|
+
export interface StopDiff {
|
|
33
|
+
kept: ManifestStop[];
|
|
34
|
+
added: ManifestStop[];
|
|
35
|
+
removed: {
|
|
36
|
+
_key: string;
|
|
37
|
+
label: string;
|
|
38
|
+
}[];
|
|
39
|
+
renamed: {
|
|
40
|
+
from: string;
|
|
41
|
+
to: string;
|
|
42
|
+
}[];
|
|
43
|
+
}
|
|
44
|
+
export declare function diffStops(prev: {
|
|
45
|
+
_key: string;
|
|
46
|
+
label: string;
|
|
47
|
+
slug: string;
|
|
48
|
+
}[] | undefined, next: ManifestStop[]): StopDiff;
|
|
49
|
+
export declare const hasContent: (s: Slot | undefined) => boolean;
|
|
50
|
+
/** Slots in the scene's stop order, then orphans. */
|
|
51
|
+
export declare function orderSlots(slots: Slot[], stops: {
|
|
52
|
+
_key: string;
|
|
53
|
+
}[]): Slot[];
|
|
54
|
+
/** Move an orphan's content (and style) to another stop's slot, appending to what is there. */
|
|
55
|
+
export declare function moveSlot(slots: Slot[], from: string, to: string): Slot[];
|
|
56
|
+
/** Is this one of the files a page loads? */
|
|
57
|
+
export declare const keepFile: (path: string) => boolean;
|
|
58
|
+
/** Throws if a kept file isn't what its name says (e.g. a renamed HTML or SVG file). */
|
|
59
|
+
export declare function checkKept(path: string, data: Uint8Array): void;
|
package/dist/merge.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
export function sceneStops(stops, urlOf) {
|
|
2
|
+
return stops.map((s) => ({
|
|
3
|
+
_key: s.id,
|
|
4
|
+
_type: "studioSceneStop",
|
|
5
|
+
slug: s.slug,
|
|
6
|
+
label: s.label || s.camera || s.slug,
|
|
7
|
+
...(s.camera ? { camera: s.camera } : {}),
|
|
8
|
+
...(s.thumb ? { thumb: s.thumb, ...(urlOf(s.thumb) ? { thumbUrl: urlOf(s.thumb) } : {}) } : {}),
|
|
9
|
+
}));
|
|
10
|
+
}
|
|
11
|
+
export function diffStops(prev, next) {
|
|
12
|
+
const before = new Map((prev ?? []).map((s) => [s._key, s]));
|
|
13
|
+
const ids = new Set(next.map((s) => s.id));
|
|
14
|
+
const kept = next.filter((s) => before.has(s.id));
|
|
15
|
+
return {
|
|
16
|
+
kept,
|
|
17
|
+
added: next.filter((s) => !before.has(s.id)),
|
|
18
|
+
removed: (prev ?? []).filter((s) => !ids.has(s._key)).map((s) => ({ _key: s._key, label: s.label })),
|
|
19
|
+
renamed: kept.filter((s) => before.get(s.id).slug !== s.slug).map((s) => ({ from: before.get(s.id).slug, to: s.slug })),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
export const hasContent = (s) => !!s?.content?.length;
|
|
23
|
+
/** Slots in the scene's stop order, then orphans. */
|
|
24
|
+
export function orderSlots(slots, stops) {
|
|
25
|
+
const at = new Map(stops.map((s, i) => [s._key, i]));
|
|
26
|
+
return [...slots].sort((a, b) => (at.get(a._key) ?? 1e9) - (at.get(b._key) ?? 1e9));
|
|
27
|
+
}
|
|
28
|
+
/** Move an orphan's content (and style) to another stop's slot, appending to what is there. */
|
|
29
|
+
export function moveSlot(slots, from, to) {
|
|
30
|
+
const src = slots.find((s) => s._key === from);
|
|
31
|
+
if (!src)
|
|
32
|
+
return slots;
|
|
33
|
+
const dst = slots.find((s) => s._key === to);
|
|
34
|
+
const merged = dst
|
|
35
|
+
? { ...dst, content: [...(dst.content ?? []), ...(src.content ?? [])], style: dst.style ?? src.style }
|
|
36
|
+
: { ...src, _key: to };
|
|
37
|
+
const rest = slots.filter((s) => s._key !== from && s._key !== to);
|
|
38
|
+
return [...rest, merged];
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Files the Studio keeps from an export: only what the page loads, by name and by content. Uploads become public
|
|
42
|
+
* files on the asset CDN, so anything else in the zip (the runtime script, docs, demo page, or files someone
|
|
43
|
+
* added) is never uploaded. The runtime script comes from the site.
|
|
44
|
+
*/
|
|
45
|
+
const KEEP = [
|
|
46
|
+
{ name: /^timeline\.json$/, looksRight: (b) => b[0] === 0x7b, what: "JSON" },
|
|
47
|
+
{ name: /^scene\.glb$/, looksRight: (b) => ascii(b, 0, "glTF"), what: "a glTF binary" },
|
|
48
|
+
{ name: /^(poster|shots\/[a-z0-9-]{1,120})\.webp$/, looksRight: (b) => ascii(b, 0, "RIFF") && ascii(b, 8, "WEBP"), what: "a WebP image" },
|
|
49
|
+
{ name: /^env\.hdr$/, looksRight: (b) => ascii(b, 0, "#?RADIANCE") || ascii(b, 0, "#?RGBE"), what: "a Radiance HDR" },
|
|
50
|
+
{ name: /^media\/[A-Za-z0-9_]{1,64}\.(mp4|mov)$/, looksRight: (b) => ascii(b, 4, "ftyp"), what: "an MP4 or QuickTime video" },
|
|
51
|
+
{ name: /^media\/[A-Za-z0-9_]{1,64}\.webm$/, looksRight: (b) => b[0] === 0x1a && b[1] === 0x45 && b[2] === 0xdf && b[3] === 0xa3, what: "a WebM video" },
|
|
52
|
+
];
|
|
53
|
+
function ascii(b, at, s) {
|
|
54
|
+
if (b.length < at + s.length)
|
|
55
|
+
return false;
|
|
56
|
+
for (let i = 0; i < s.length; i++)
|
|
57
|
+
if (b[at + i] !== s.charCodeAt(i))
|
|
58
|
+
return false;
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
/** Is this one of the files a page loads? */
|
|
62
|
+
export const keepFile = (path) => KEEP.some((k) => k.name.test(path));
|
|
63
|
+
/** Throws if a kept file isn't what its name says (e.g. a renamed HTML or SVG file). */
|
|
64
|
+
export function checkKept(path, data) {
|
|
65
|
+
const k = KEEP.find((x) => x.name.test(path));
|
|
66
|
+
if (k && !k.looksRight(data))
|
|
67
|
+
throw new Error(`${path} isn't ${k.what}. Use the scroll-scene.zip that STUDIO exports.`);
|
|
68
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { ArrayOfType } from "sanity";
|
|
2
|
+
import type { SlotStyle } from "./styleTokens";
|
|
3
|
+
/** A named look editors pick with one click. Only the fields it sets are written; others keep inheriting. */
|
|
4
|
+
export interface StylePreset {
|
|
5
|
+
title: string;
|
|
6
|
+
style: Pick<SlotStyle, "card" | "cardColor" | "textColor" | "cardOpacity" | "blur">;
|
|
7
|
+
}
|
|
8
|
+
export declare const DEFAULT_PRESETS: StylePreset[];
|
|
9
|
+
/** A named colour editors can pick. `value` is any CSS colour, including var(--token). */
|
|
10
|
+
export interface PaletteColor {
|
|
11
|
+
title: string;
|
|
12
|
+
value: string;
|
|
13
|
+
/** What the Studio shows for it when `value` is a var(--token) the Studio can't resolve. */
|
|
14
|
+
preview?: string;
|
|
15
|
+
}
|
|
16
|
+
/** Every style control a slot can have. Hide the ones your site doesn't want editors to touch. */
|
|
17
|
+
export declare const STYLE_CONTROLS: readonly ["align", "verticalAlign", "textAlign", "width", "card", "textColor", "cardColor", "cardOpacity", "blur", "radius", "padding", "fontScale", "pause", "move", "animation"];
|
|
18
|
+
export type StyleControl = (typeof STYLE_CONTROLS)[number];
|
|
19
|
+
export interface StudioSceneOptions {
|
|
20
|
+
/** What editors can put on a shot: the site's own block types (the same ones its pages use). Default: rich text. */
|
|
21
|
+
stopContent?: ArrayOfType[];
|
|
22
|
+
styles?: {
|
|
23
|
+
/** Swatches for the colour controls. Without them, editors type any CSS colour. */
|
|
24
|
+
colors?: PaletteColor[];
|
|
25
|
+
/** Only allow the palette (no free colours). Default false. */
|
|
26
|
+
paletteOnly?: boolean;
|
|
27
|
+
/** Which controls "Customise" shows. Default: all of them. */
|
|
28
|
+
controls?: StyleControl[];
|
|
29
|
+
/** One-click looks. Default: Dark card, Light card, Outline, Text only. */
|
|
30
|
+
presets?: StylePreset[];
|
|
31
|
+
};
|
|
32
|
+
/** URL of scroll-scene.js for the Studio preview. Default: the copy bundled with the plugin. */
|
|
33
|
+
runtimeUrl?: string;
|
|
34
|
+
/** What editors see the placed scene called (in "Add item" menus and previews). Default: "Scroll scene". */
|
|
35
|
+
placementTitle?: string;
|
|
36
|
+
}
|
|
37
|
+
export interface ResolvedOptions {
|
|
38
|
+
stopContent: ArrayOfType[];
|
|
39
|
+
colors: PaletteColor[];
|
|
40
|
+
paletteOnly: boolean;
|
|
41
|
+
controls: Set<StyleControl>;
|
|
42
|
+
presets: StylePreset[];
|
|
43
|
+
runtimeUrl?: string;
|
|
44
|
+
placementTitle: string;
|
|
45
|
+
}
|
|
46
|
+
export declare function resolveOptions(o: StudioSceneOptions | void): ResolvedOptions;
|
|
47
|
+
/** Type names. Fixed so the GROQ fragment and frontends can rely on them. */
|
|
48
|
+
export declare const T: {
|
|
49
|
+
readonly scene: "studioScene";
|
|
50
|
+
readonly sceneStop: "studioSceneStop";
|
|
51
|
+
readonly sceneFile: "studioSceneFile";
|
|
52
|
+
readonly sceneExport: "studioSceneExport";
|
|
53
|
+
readonly placement: "studioScenePlacement";
|
|
54
|
+
readonly slot: "studioSlot";
|
|
55
|
+
readonly style: "studioSlotStyle";
|
|
56
|
+
readonly motion: "studioSlotMotion";
|
|
57
|
+
readonly group: "studioSlotGroup";
|
|
58
|
+
readonly point: "studioScenePoint";
|
|
59
|
+
};
|
|
60
|
+
export declare const API_VERSION = "2026-10-04";
|
package/dist/options.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
export const DEFAULT_PRESETS = [
|
|
2
|
+
{ title: "Dark card", style: { card: "card" } },
|
|
3
|
+
{ title: "Light card", style: { card: "card", cardColor: "#ffffff", textColor: "#111111", cardOpacity: 92 } },
|
|
4
|
+
{ title: "Outline", style: { card: "outline" } },
|
|
5
|
+
{ title: "Text only", style: { card: "plain" } },
|
|
6
|
+
];
|
|
7
|
+
/** Every style control a slot can have. Hide the ones your site doesn't want editors to touch. */
|
|
8
|
+
export const STYLE_CONTROLS = [
|
|
9
|
+
"align",
|
|
10
|
+
"verticalAlign",
|
|
11
|
+
"textAlign",
|
|
12
|
+
"width",
|
|
13
|
+
"card",
|
|
14
|
+
"textColor",
|
|
15
|
+
"cardColor",
|
|
16
|
+
"cardOpacity",
|
|
17
|
+
"blur",
|
|
18
|
+
"radius",
|
|
19
|
+
"padding",
|
|
20
|
+
"fontScale",
|
|
21
|
+
"pause",
|
|
22
|
+
"move",
|
|
23
|
+
"animation",
|
|
24
|
+
];
|
|
25
|
+
export function resolveOptions(o) {
|
|
26
|
+
return {
|
|
27
|
+
stopContent: o?.stopContent ?? [{ type: "block" }],
|
|
28
|
+
colors: o?.styles?.colors ?? [],
|
|
29
|
+
paletteOnly: !!o?.styles?.paletteOnly && !!o?.styles?.colors?.length,
|
|
30
|
+
controls: new Set(o?.styles?.controls ?? STYLE_CONTROLS),
|
|
31
|
+
presets: o?.styles?.presets ?? DEFAULT_PRESETS,
|
|
32
|
+
runtimeUrl: o?.runtimeUrl,
|
|
33
|
+
placementTitle: o?.placementTitle ?? "Scroll scene",
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
/** Type names. Fixed so the GROQ fragment and frontends can rely on them. */
|
|
37
|
+
export const T = {
|
|
38
|
+
scene: "studioScene",
|
|
39
|
+
sceneStop: "studioSceneStop",
|
|
40
|
+
sceneFile: "studioSceneFile",
|
|
41
|
+
sceneExport: "studioSceneExport",
|
|
42
|
+
placement: "studioScenePlacement",
|
|
43
|
+
slot: "studioSlot",
|
|
44
|
+
style: "studioSlotStyle",
|
|
45
|
+
motion: "studioSlotMotion",
|
|
46
|
+
group: "studioSlotGroup",
|
|
47
|
+
point: "studioScenePoint",
|
|
48
|
+
};
|
|
49
|
+
export const API_VERSION = "2026-10-04";
|