@senseihq/emaki-vue 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/README.md CHANGED
@@ -22,9 +22,14 @@ import { MotionScene, MotionShow, ScenePlayer, ShowPage } from '@senseihq/emaki-
22
22
 
23
23
  Every component takes `mode`: `light`, `dark` or `auto` (the default). Auto follows the page's `data-mode` (as rokkit's
24
24
  `themable` sets it), else the OS, and the scenes redraw when it changes. A film draws dark only if it has a dark look
25
- (see @senseihq/emaki); `ShowPage` captures light unless asked.
25
+ (see @senseihq/emaki); `ShowPage` captures light unless asked, and its `src` (`json`/`js`) forces where scenes come
26
+ from.
27
+
28
+ `MotionShow` props: `film`, `locale`, `layout`, `mode`, `poster` (cover frame first; `title`, `length` on it), `cover` (an
29
+ image URL shown with the play button; the show is created on the first click, sound on), `fullscreenHref`, `controls`,
30
+ `slide`, `loop`; `@ready(show)`. Its controls show a progress tick per scene.
26
31
 
27
32
  Call `setupMotion({ source, site, logos })` once on the client first. Composables: `useScenePlayer(player)` (a
28
- reactive snapshot of a `ScenePlayerState`) and `useShowState(showRef)`.
33
+ reactive snapshot of a `ScenePlayerState`) and `useShowState(showRef)` (`{ index, local, playing, sound, layout, started }`, the playhead followed once a frame).
29
34
 
30
35
  MIT © SenecaGlobal
@@ -102,7 +102,9 @@ export declare const ScenePlayer: import("vue").DefineComponent<import("vue").Ex
102
102
  mode: Mode;
103
103
  controls: boolean;
104
104
  }, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
105
- /** The story player: a film's scenes as slides, fitted to its box. Keys: ←/→, Space, M (sound), O (orientation). */
105
+ /** The story player: a film's scenes as slides, fitted to its box. Keys: ←/→ scene, Space play/pause, M sound,
106
+ * O orientation, R replay, Home from the top, F fullscreen. `poster` opens on the cover frame; `cover` paints an
107
+ * image and creates nothing until the first click. */
106
108
  export declare const MotionShow: import("vue").DefineComponent<import("vue").ExtractPropTypes<{
107
109
  film: {
108
110
  type: StringConstructor;
@@ -132,6 +134,30 @@ export declare const MotionShow: import("vue").DefineComponent<import("vue").Ext
132
134
  type: BooleanConstructor;
133
135
  default: boolean;
134
136
  };
137
+ /** Open on the film's cover frame with a play button; the first click plays it from the top with sound on. */
138
+ poster: {
139
+ type: BooleanConstructor;
140
+ default: boolean;
141
+ };
142
+ /** An image (URL) to show with the play button; the show is created on the first click (then plays, sound on). */
143
+ cover: {
144
+ type: StringConstructor;
145
+ default: string;
146
+ };
147
+ /** The film's name and running time, for the play button's label and chip. */
148
+ title: {
149
+ type: StringConstructor;
150
+ default: string;
151
+ };
152
+ length: {
153
+ type: StringConstructor;
154
+ default: string;
155
+ };
156
+ /** Where the full-screen link goes (e.g. /show/<film>); no link when empty. */
157
+ fullscreenHref: {
158
+ type: StringConstructor;
159
+ default: string;
160
+ };
135
161
  }>, () => import("vue").VNode<import("vue").RendererNode, import("vue").RendererElement, {
136
162
  [key: string]: any;
137
163
  }>, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {
@@ -165,6 +191,30 @@ export declare const MotionShow: import("vue").DefineComponent<import("vue").Ext
165
191
  type: BooleanConstructor;
166
192
  default: boolean;
167
193
  };
194
+ /** Open on the film's cover frame with a play button; the first click plays it from the top with sound on. */
195
+ poster: {
196
+ type: BooleanConstructor;
197
+ default: boolean;
198
+ };
199
+ /** An image (URL) to show with the play button; the show is created on the first click (then plays, sound on). */
200
+ cover: {
201
+ type: StringConstructor;
202
+ default: string;
203
+ };
204
+ /** The film's name and running time, for the play button's label and chip. */
205
+ title: {
206
+ type: StringConstructor;
207
+ default: string;
208
+ };
209
+ length: {
210
+ type: StringConstructor;
211
+ default: string;
212
+ };
213
+ /** Where the full-screen link goes (e.g. /show/<film>); no link when empty. */
214
+ fullscreenHref: {
215
+ type: StringConstructor;
216
+ default: string;
217
+ };
168
218
  }>> & Readonly<{
169
219
  onReady?: ((_show: {
170
220
  film: import("@senseihq/emaki").FilmMeta;
@@ -174,10 +224,12 @@ export declare const MotionShow: import("vue").DefineComponent<import("vue").Ext
174
224
  start: number;
175
225
  design: number;
176
226
  holds: [number, number][];
227
+ voice?: number;
177
228
  id: string;
178
229
  name: string;
179
230
  dur: number;
180
231
  desc?: string;
232
+ settle?: number;
181
233
  }[];
182
234
  };
183
235
  readonly state: {
@@ -186,7 +238,11 @@ export declare const MotionShow: import("vue").DefineComponent<import("vue").Ext
186
238
  playing: boolean;
187
239
  sound: boolean;
188
240
  layout: Layout;
241
+ started: boolean;
189
242
  };
243
+ start(): Promise<void>;
244
+ replay(): void;
245
+ restart(): void;
190
246
  fit(w: number, h: number): void;
191
247
  go: (i: number) => void;
192
248
  next: () => void;
@@ -204,9 +260,14 @@ export declare const MotionShow: import("vue").DefineComponent<import("vue").Ext
204
260
  layout: Layout;
205
261
  locale: string;
206
262
  mode: Mode;
263
+ length: string;
264
+ title: string;
207
265
  controls: boolean;
208
266
  slide: number;
209
267
  loop: boolean;
268
+ poster: boolean;
269
+ cover: string;
270
+ fullscreenHref: string;
210
271
  }, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
211
272
  /** The /show/<film> page the video pipeline drives: with `capture`, the stage at 1:1, no chrome, window.__show. */
212
273
  export declare const ShowPage: import("vue").DefineComponent<import("vue").ExtractPropTypes<{
@@ -227,6 +288,11 @@ export declare const ShowPage: import("vue").DefineComponent<import("vue").Extra
227
288
  type: PropType<Mode>;
228
289
  default: undefined;
229
290
  };
291
+ /** Force scenes from their JSON or their code modules (parity checks). */
292
+ src: {
293
+ type: PropType<"json" | "js">;
294
+ default: undefined;
295
+ };
230
296
  capture: {
231
297
  type: BooleanConstructor;
232
298
  default: boolean;
@@ -251,6 +317,11 @@ export declare const ShowPage: import("vue").DefineComponent<import("vue").Extra
251
317
  type: PropType<Mode>;
252
318
  default: undefined;
253
319
  };
320
+ /** Force scenes from their JSON or their code modules (parity checks). */
321
+ src: {
322
+ type: PropType<"json" | "js">;
323
+ default: undefined;
324
+ };
254
325
  capture: {
255
326
  type: BooleanConstructor;
256
327
  default: boolean;
@@ -259,5 +330,6 @@ export declare const ShowPage: import("vue").DefineComponent<import("vue").Extra
259
330
  layout: Layout;
260
331
  locale: string;
261
332
  mode: Mode;
333
+ src: "json" | "js";
262
334
  capture: boolean;
263
335
  }, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
@@ -1,7 +1,7 @@
1
1
  // Vue 3 components over @senseihq/emaki, written as render functions so they need no SFC compiler.
2
2
  // Client-only (they need the DOM): in Nuxt wrap them in <ClientOnly>. Call setupMotion(...) once on the client first.
3
- import { defineComponent, h, onBeforeUnmount, onMounted, ref, shallowRef, watch } from 'vue';
4
- import { attachScene, createShow, figurePlayer, installCapture } from '@senseihq/emaki';
3
+ import { defineComponent, h, onBeforeUnmount, onMounted, ref, shallowRef, toRaw, watch } from 'vue';
4
+ import { attachScene, createShow, figurePlayer, installCapture, showKey, ticks } from '@senseihq/emaki';
5
5
  import { useScenePlayer, useShowState, useSize } from './composables.js';
6
6
  function attach(player, locale, mode, autoplay) {
7
7
  const box = ref(null), host = ref(null);
@@ -43,11 +43,12 @@ export const ScenePlayer = defineComponent({
43
43
  controls: { type: Boolean, default: false },
44
44
  },
45
45
  setup(props) {
46
- const st = useScenePlayer(props.player);
47
- const { box, host } = attach(() => props.player, () => props.locale, () => props.mode, false);
46
+ const player = () => toRaw(props.player);
47
+ const st = useScenePlayer(player());
48
+ const { box, host } = attach(player, () => props.locale, () => props.mode, false);
48
49
  const msg = 'position:absolute;inset:0;display:grid;place-items:center;margin:0;font:14px system-ui';
49
50
  return () => {
50
- const s = st.value, p = props.player;
51
+ const s = st.value, p = player();
51
52
  return h('div', { ref: box, style: 'position:relative;width:100%' }, [
52
53
  h('div', { ref: host }),
53
54
  s.status === 'error' ? h('p', { role: 'alert', style: msg + ';color:#b42318' }, s.error) : null,
@@ -64,7 +65,20 @@ export const ScenePlayer = defineComponent({
64
65
  };
65
66
  },
66
67
  });
67
- /** The story player: a film's scenes as slides, fitted to its box. Keys: ←/→, Space, M (sound), O (orientation). */
68
+ const btn = 'min-width:34px;height:34px;border-radius:17px;border:1px solid currentColor;background:none;color:inherit;cursor:pointer;padding:0 10px';
69
+ const tickTrack = 'linear-gradient(color-mix(in srgb, currentColor 18%, transparent), color-mix(in srgb, currentColor 18%, transparent)) center / 100% 4px no-repeat';
70
+ /** The play button over a poster or a cover image: the label and the chip name the film and its length. */
71
+ function playButton(title, length, onClick, cover) {
72
+ return h('button', { onClick, 'aria-label': `Play the film${title ? `: ${title}` : ''}`,
73
+ style: 'position:absolute;inset:0;border:0;padding:0;background:none;cursor:pointer;color:inherit' }, [
74
+ cover ? h('img', { src: cover, alt: '', style: 'position:absolute;inset:0;width:100%;height:100%;object-fit:cover' }) : null,
75
+ h('span', { 'aria-hidden': 'true', style: 'position:absolute;right:18px;bottom:14px;width:64px;height:64px;border-radius:50%;background:color-mix(in srgb, currentColor 85%, transparent);display:grid;place-items:center' }, [h('svg', { viewBox: '0 0 24 24', style: 'width:28px;height:28px;fill:Canvas;margin-left:3px' }, [h('path', { d: 'M8 5.5v13l11-6.5z' })])]),
76
+ h('span', { style: 'position:absolute;left:16px;bottom:14px;padding:6px 12px;border-radius:999px;font:11px/1 system-ui;letter-spacing:.1em;text-transform:uppercase;background:color-mix(in srgb, Canvas 88%, transparent);border:1px solid color-mix(in srgb, currentColor 20%, transparent)' }, `Play the film${length ? ` · ${length}` : ''}`),
77
+ ]);
78
+ }
79
+ /** The story player: a film's scenes as slides, fitted to its box. Keys: ←/→ scene, Space play/pause, M sound,
80
+ * O orientation, R replay, Home from the top, F fullscreen. `poster` opens on the cover frame; `cover` paints an
81
+ * image and creates nothing until the first click. */
68
82
  export const MotionShow = defineComponent({
69
83
  name: 'MotionShow',
70
84
  props: {
@@ -75,6 +89,15 @@ export const MotionShow = defineComponent({
75
89
  controls: { type: Boolean, default: true },
76
90
  slide: { type: Number, default: 1 },
77
91
  loop: { type: Boolean, default: false },
92
+ /** Open on the film's cover frame with a play button; the first click plays it from the top with sound on. */
93
+ poster: { type: Boolean, default: false },
94
+ /** An image (URL) to show with the play button; the show is created on the first click (then plays, sound on). */
95
+ cover: { type: String, default: '' },
96
+ /** The film's name and running time, for the play button's label and chip. */
97
+ title: { type: String, default: '' },
98
+ length: { type: String, default: '' },
99
+ /** Where the full-screen link goes (e.g. /show/<film>); no link when empty. */
100
+ fullscreenHref: { type: String, default: '' },
78
101
  },
79
102
  emits: { ready: (_show) => true },
80
103
  setup(props, { emit, expose }) {
@@ -82,53 +105,59 @@ export const MotionShow = defineComponent({
82
105
  const show = shallowRef(null);
83
106
  const st = useShowState(show);
84
107
  const size = useSize(fit);
108
+ // With a cover nothing is created until the reader clicks; that click then starts the show (sound on). The poster
109
+ // shows only until the reader first plays: a language or mode change after that restarts without it. Each start
110
+ // gets a ticket, so a slow createShow that a newer one overtook is thrown away.
111
+ const waiting = ref(!!props.cover);
112
+ let autoplay = false, begun = false, ticket = 0;
113
+ watch(() => st.value?.started, (on) => { if (on)
114
+ begun = true; });
85
115
  async function start() {
116
+ if (waiting.value)
117
+ return;
118
+ const mine = ++ticket, play = autoplay;
119
+ autoplay = false;
86
120
  show.value?.destroy();
87
121
  show.value = null;
88
- const s = await createShow(stage.value, props.film, { locale: props.locale, layout: props.layout, mode: props.mode, slide: props.slide, loop: props.loop });
122
+ const s = await createShow(stage.value, props.film, { locale: props.locale, layout: props.layout, mode: props.mode, slide: props.slide, loop: props.loop, poster: (props.poster || play) && !begun });
123
+ if (mine !== ticket)
124
+ return s.destroy();
89
125
  show.value = s;
90
126
  s.fit(size.value.w, size.value.h);
91
127
  emit('ready', s);
128
+ if (play)
129
+ await s.start();
92
130
  }
131
+ const open = () => { autoplay = true; waiting.value = false; start(); };
93
132
  onMounted(start);
94
- onBeforeUnmount(() => show.value?.destroy());
133
+ onBeforeUnmount(() => { ticket++; show.value?.destroy(); });
95
134
  watch(() => [props.film, props.locale, props.mode], start);
96
135
  watch(() => props.layout, (l) => show.value?.setLayout(l));
97
136
  watch([size, () => st.value?.layout], () => show.value?.fit(size.value.w, size.value.h));
98
137
  expose({ show });
99
- const onKeydown = (e) => {
100
- const s = show.value;
101
- if (!s)
102
- return;
103
- const k = e.key.toLowerCase();
104
- if (k === 'arrowright')
105
- s.next();
106
- else if (k === 'arrowleft')
107
- s.prev();
108
- else if (k === ' ') {
109
- s.toggle();
110
- e.preventDefault();
111
- }
112
- else if (k === 'm')
113
- s.setSound(!s.state.sound);
114
- else if (k === 'o')
115
- s.setLayout(s.state.layout === 'landscape' ? 'portrait' : 'landscape');
116
- };
117
- const btn = 'min-width:34px;height:34px;border-radius:17px;border:1px solid currentColor;background:none;color:inherit;cursor:pointer;padding:0 10px';
138
+ // Every key (←/→, Space, M, O, R, Home, F) from the core, so all players answer the same way.
139
+ const onKeydown = (e) => { if (show.value && showKey(show.value, e, e.currentTarget))
140
+ e.preventDefault(); };
141
+ const bar = (s, x) => h('div', { style: 'display:flex;gap:8px;align-items:center;justify-content:center;padding:8px;font:13px system-ui' }, [
142
+ h('button', { style: btn, onClick: () => s.prev(), 'aria-label': 'Previous scene' }, '‹'),
143
+ h('button', { style: btn, onClick: () => s.toggle(), 'aria-label': x.playing ? 'Pause' : 'Play' }, x.playing ? '❚❚' : '▶'),
144
+ h('button', { style: btn, onClick: () => s.next(), 'aria-label': 'Next scene' }, '›'),
145
+ h('div', { style: 'flex:1;display:flex;gap:3px;max-width:360px' }, ticks(s).map((tk, i) => h('button', { key: tk.id, onClick: () => s.go(i), 'aria-label': tk.name,
146
+ style: `flex:${tk.dur};min-width:0;height:18px;padding:7px 0;border:0;border-radius:0;background:${tickTrack};cursor:pointer;color:inherit` }, [h('span', { style: `display:block;height:4px;border-radius:2px;background:currentColor;width:${tk.fill * 100}%` })]))),
147
+ h('span', { style: 'opacity:.7;font-variant-numeric:tabular-nums' }, `${x.index + 1} / ${s.timeline.scenes.length}`),
148
+ h('button', { style: btn, onClick: () => s.setSound(!x.sound), 'aria-pressed': x.sound }, x.sound ? 'Sound on' : 'Sound off'),
149
+ props.fullscreenHref
150
+ ? h('a', { href: props.fullscreenHref, 'aria-label': 'Open full screen', title: 'Full screen', style: `${btn};display:grid;place-items:center;text-decoration:none;padding:0` }, '⤢')
151
+ : null,
152
+ ]);
118
153
  return () => {
119
154
  const s = show.value, x = st.value;
120
155
  return h('div', { tabindex: 0, role: 'application', 'aria-label': 'Story player', onKeydown,
121
156
  style: 'position:relative;width:100%;height:100%;display:flex;flex-direction:column;outline:none' }, [
122
157
  h('div', { ref: fit, style: 'position:relative;flex:1;min-height:0;display:grid;place-items:center;overflow:hidden' }, [h('div', { ref: stage, style: 'position:relative' })]),
123
- props.controls && s && x
124
- ? h('div', { style: 'display:flex;gap:8px;align-items:center;justify-content:center;padding:8px;font:13px system-ui' }, [
125
- h('button', { style: btn, onClick: () => s.prev(), 'aria-label': 'Previous scene' }, '‹'),
126
- h('button', { style: btn, onClick: () => s.toggle(), 'aria-label': x.playing ? 'Pause' : 'Play' }, x.playing ? '❚❚' : '▶'),
127
- h('button', { style: btn, onClick: () => s.next(), 'aria-label': 'Next scene' }, '›'),
128
- h('span', { style: 'opacity:.7' }, `${x.index + 1} / ${s.timeline.scenes.length}`),
129
- h('button', { style: btn, onClick: () => s.setSound(!x.sound), 'aria-pressed': x.sound }, x.sound ? 'Sound on' : 'Sound off'),
130
- ])
131
- : null,
158
+ waiting.value ? playButton(props.title, props.length, open, props.cover)
159
+ : s && x && !x.started ? playButton(props.title, props.length, () => s.start())
160
+ : props.controls && s && x ? bar(s, x) : null,
132
161
  ]);
133
162
  };
134
163
  },
@@ -142,13 +171,15 @@ export const ShowPage = defineComponent({
142
171
  locale: { type: String, default: 'en' },
143
172
  /** Capture renders light unless asked; the interactive player follows the page (auto). */
144
173
  mode: { type: String, default: undefined },
174
+ /** Force scenes from their JSON or their code modules (parity checks). */
175
+ src: { type: String, default: undefined },
145
176
  capture: { type: Boolean, default: false },
146
177
  },
147
178
  setup(props) {
148
179
  const host = ref(null);
149
180
  let s = null;
150
181
  onMounted(async () => { if (props.capture)
151
- s = await installCapture(host.value, props.film, props.layout, props.locale, props.mode ?? 'light'); });
182
+ s = await installCapture(host.value, props.film, props.layout, props.locale, { mode: props.mode ?? 'light', src: props.src }); });
152
183
  onBeforeUnmount(() => s?.destroy());
153
184
  return () => props.capture
154
185
  ? h('div', { ref: host })
@@ -1,7 +1,9 @@
1
1
  import { type Ref } from 'vue';
2
2
  import type { createShow, Layout, ScenePlayerState } from '@senseihq/emaki';
3
- /** A reactive view of a ScenePlayerState: read `state.value.t` etc. in templates; call methods on the player. */
4
- export declare function useScenePlayer(player: ScenePlayerState): import("vue").ShallowRef<{
3
+ /** A reactive view of a ScenePlayerState: read `state.value.t` etc. in templates; call methods on the player.
4
+ * A player kept in a ref or reactive() arrives as a proxy, which cannot reach the class's private fields: use the
5
+ * object behind it. */
6
+ export declare function useScenePlayer(held: ScenePlayerState): import("vue").ShallowRef<{
5
7
  t: number;
6
8
  dur: number;
7
9
  status: import("@senseihq/emaki").PlayerStatus;
@@ -25,8 +27,10 @@ export interface ShowState {
25
27
  playing: boolean;
26
28
  sound: boolean;
27
29
  layout: Layout;
30
+ started: boolean;
28
31
  }
29
- /** Reactive state of a story player controller (from <MotionShow @ready>). */
32
+ /** Reactive state of a story player controller (from <MotionShow @ready>), following the playhead (read once a
33
+ * frame; it moves without notifications). `started` is false on a poster. */
30
34
  export declare function useShowState(show: Ref<Show | null>): import("vue").ShallowRef<ShowState | null, ShowState | null>;
31
35
  /** Size of an element via ResizeObserver. */
32
36
  export declare function useSize(el: Ref<HTMLElement | null>): import("vue").ShallowRef<{
@@ -1,7 +1,10 @@
1
1
  // Vue reactivity over the core: the core notifies, a shallowRef version makes reads tracked.
2
- import { onBeforeUnmount, shallowRef, ref, watch } from 'vue';
3
- /** A reactive view of a ScenePlayerState: read `state.value.t` etc. in templates; call methods on the player. */
4
- export function useScenePlayer(player) {
2
+ import { onBeforeUnmount, shallowRef, ref, toRaw, watch } from 'vue';
3
+ /** A reactive view of a ScenePlayerState: read `state.value.t` etc. in templates; call methods on the player.
4
+ * A player kept in a ref or reactive() arrives as a proxy, which cannot reach the class's private fields: use the
5
+ * object behind it. */
6
+ export function useScenePlayer(held) {
7
+ const player = toRaw(held);
5
8
  const version = ref(player.version);
6
9
  const off = player.subscribe(() => { version.value = player.version; });
7
10
  onBeforeUnmount(off);
@@ -10,18 +13,28 @@ export function useScenePlayer(player) {
10
13
  watch(version, () => { state.value = snap(); });
11
14
  return state;
12
15
  }
13
- /** Reactive state of a story player controller (from <MotionShow @ready>). */
16
+ /** Reactive state of a story player controller (from <MotionShow @ready>), following the playhead (read once a
17
+ * frame; it moves without notifications). `started` is false on a poster. */
14
18
  export function useShowState(show) {
15
19
  const state = shallowRef(null);
16
- let off = null;
20
+ let off = null, raf = 0;
21
+ const follow = () => {
22
+ const s = show.value, x = state.value;
23
+ if (s && x && s.state.local !== x.local)
24
+ state.value = { ...x, local: s.state.local };
25
+ raf = requestAnimationFrame(follow);
26
+ };
17
27
  watch(show, (s) => {
18
28
  off?.();
19
29
  off = null;
30
+ cancelAnimationFrame(raf);
20
31
  state.value = s ? { ...s.state } : null;
21
- if (s)
32
+ if (s) {
22
33
  off = s.on((x) => { state.value = { ...x }; });
34
+ raf = requestAnimationFrame(follow);
35
+ }
23
36
  }, { immediate: true });
24
- onBeforeUnmount(() => off?.());
37
+ onBeforeUnmount(() => { off?.(); cancelAnimationFrame(raf); });
25
38
  return state;
26
39
  }
27
40
  /** Size of an element via ResizeObserver. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@senseihq/emaki-vue",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Vue 3 components and composables for motion stories: article figures, the story player, a controlled scene player and the video capture page.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -31,7 +31,7 @@
31
31
  "vue": "^3.4.0"
32
32
  },
33
33
  "dependencies": {
34
- "@senseihq/emaki": "0.1.0"
34
+ "@senseihq/emaki": "0.1.2"
35
35
  },
36
36
  "devDependencies": {
37
37
  "vue": "^3.5.13"
package/src/components.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  // Vue 3 components over @senseihq/emaki, written as render functions so they need no SFC compiler.
2
2
  // Client-only (they need the DOM): in Nuxt wrap them in <ClientOnly>. Call setupMotion(...) once on the client first.
3
- import { defineComponent, h, onBeforeUnmount, onMounted, ref, shallowRef, watch, type PropType } from 'vue';
4
- import { attachScene, createShow, figurePlayer, installCapture, type AttachedScene, type Layout, type Mode, type ScenePlayerState } from '@senseihq/emaki';
5
- import { useScenePlayer, useShowState, useSize, type Show } from './composables.js';
3
+ import { defineComponent, h, onBeforeUnmount, onMounted, ref, shallowRef, toRaw, watch, type PropType } from 'vue';
4
+ import { attachScene, createShow, figurePlayer, installCapture, showKey, ticks, type AttachedScene, type Layout, type Mode, type ScenePlayerState } from '@senseihq/emaki';
5
+ import { useScenePlayer, useShowState, useSize, type Show, type ShowState } from './composables.js';
6
6
 
7
7
  function attach(player: () => ScenePlayerState, locale: () => string, mode: () => Mode, autoplay: boolean) {
8
8
  const box = ref<HTMLElement | null>(null), host = ref<HTMLElement | null>(null);
@@ -45,11 +45,12 @@ export const ScenePlayer = defineComponent({
45
45
  controls: { type: Boolean, default: false },
46
46
  },
47
47
  setup(props) {
48
- const st = useScenePlayer(props.player);
49
- const { box, host } = attach(() => props.player, () => props.locale, () => props.mode, false);
48
+ const player = () => toRaw(props.player);
49
+ const st = useScenePlayer(player());
50
+ const { box, host } = attach(player, () => props.locale, () => props.mode, false);
50
51
  const msg = 'position:absolute;inset:0;display:grid;place-items:center;margin:0;font:14px system-ui';
51
52
  return () => {
52
- const s = st.value, p = props.player;
53
+ const s = st.value, p = player();
53
54
  return h('div', { ref: box, style: 'position:relative;width:100%' }, [
54
55
  h('div', { ref: host }),
55
56
  s.status === 'error' ? h('p', { role: 'alert', style: msg + ';color:#b42318' }, s.error) : null,
@@ -67,7 +68,24 @@ export const ScenePlayer = defineComponent({
67
68
  },
68
69
  });
69
70
 
70
- /** The story player: a film's scenes as slides, fitted to its box. Keys: ←/→, Space, M (sound), O (orientation). */
71
+ const btn = 'min-width:34px;height:34px;border-radius:17px;border:1px solid currentColor;background:none;color:inherit;cursor:pointer;padding:0 10px';
72
+ const tickTrack = 'linear-gradient(color-mix(in srgb, currentColor 18%, transparent), color-mix(in srgb, currentColor 18%, transparent)) center / 100% 4px no-repeat';
73
+
74
+ /** The play button over a poster or a cover image: the label and the chip name the film and its length. */
75
+ function playButton(title: string, length: string, onClick: () => void, cover?: string) {
76
+ return h('button', { onClick, 'aria-label': `Play the film${title ? `: ${title}` : ''}`,
77
+ style: 'position:absolute;inset:0;border:0;padding:0;background:none;cursor:pointer;color:inherit' }, [
78
+ cover ? h('img', { src: cover, alt: '', style: 'position:absolute;inset:0;width:100%;height:100%;object-fit:cover' }) : null,
79
+ h('span', { 'aria-hidden': 'true', style: 'position:absolute;right:18px;bottom:14px;width:64px;height:64px;border-radius:50%;background:color-mix(in srgb, currentColor 85%, transparent);display:grid;place-items:center' },
80
+ [h('svg', { viewBox: '0 0 24 24', style: 'width:28px;height:28px;fill:Canvas;margin-left:3px' }, [h('path', { d: 'M8 5.5v13l11-6.5z' })])]),
81
+ h('span', { style: 'position:absolute;left:16px;bottom:14px;padding:6px 12px;border-radius:999px;font:11px/1 system-ui;letter-spacing:.1em;text-transform:uppercase;background:color-mix(in srgb, Canvas 88%, transparent);border:1px solid color-mix(in srgb, currentColor 20%, transparent)' },
82
+ `Play the film${length ? ` · ${length}` : ''}`),
83
+ ]);
84
+ }
85
+
86
+ /** The story player: a film's scenes as slides, fitted to its box. Keys: ←/→ scene, Space play/pause, M sound,
87
+ * O orientation, R replay, Home from the top, F fullscreen. `poster` opens on the cover frame; `cover` paints an
88
+ * image and creates nothing until the first click. */
71
89
  export const MotionShow = defineComponent({
72
90
  name: 'MotionShow',
73
91
  props: {
@@ -78,6 +96,15 @@ export const MotionShow = defineComponent({
78
96
  controls: { type: Boolean, default: true },
79
97
  slide: { type: Number, default: 1 },
80
98
  loop: { type: Boolean, default: false },
99
+ /** Open on the film's cover frame with a play button; the first click plays it from the top with sound on. */
100
+ poster: { type: Boolean, default: false },
101
+ /** An image (URL) to show with the play button; the show is created on the first click (then plays, sound on). */
102
+ cover: { type: String, default: '' },
103
+ /** The film's name and running time, for the play button's label and chip. */
104
+ title: { type: String, default: '' },
105
+ length: { type: String, default: '' },
106
+ /** Where the full-screen link goes (e.g. /show/<film>); no link when empty. */
107
+ fullscreenHref: { type: String, default: '' },
81
108
  },
82
109
  emits: { ready: (_show: Show) => true },
83
110
  setup(props, { emit, expose }) {
@@ -85,40 +112,53 @@ export const MotionShow = defineComponent({
85
112
  const show = shallowRef<Show | null>(null);
86
113
  const st = useShowState(show);
87
114
  const size = useSize(fit);
115
+ // With a cover nothing is created until the reader clicks; that click then starts the show (sound on). The poster
116
+ // shows only until the reader first plays: a language or mode change after that restarts without it. Each start
117
+ // gets a ticket, so a slow createShow that a newer one overtook is thrown away.
118
+ const waiting = ref(!!props.cover);
119
+ let autoplay = false, begun = false, ticket = 0;
120
+ watch(() => st.value?.started, (on) => { if (on) begun = true; });
88
121
  async function start() {
122
+ if (waiting.value) return;
123
+ const mine = ++ticket, play = autoplay;
124
+ autoplay = false;
89
125
  show.value?.destroy(); show.value = null;
90
- const s = await createShow(stage.value!, props.film, { locale: props.locale, layout: props.layout, mode: props.mode, slide: props.slide, loop: props.loop });
126
+ const s = await createShow(stage.value!, props.film, { locale: props.locale, layout: props.layout, mode: props.mode, slide: props.slide, loop: props.loop, poster: (props.poster || play) && !begun });
127
+ if (mine !== ticket) return s.destroy();
91
128
  show.value = s; s.fit(size.value.w, size.value.h); emit('ready', s);
129
+ if (play) await s.start();
92
130
  }
131
+ const open = () => { autoplay = true; waiting.value = false; start(); };
93
132
  onMounted(start);
94
- onBeforeUnmount(() => show.value?.destroy());
133
+ onBeforeUnmount(() => { ticket++; show.value?.destroy(); });
95
134
  watch(() => [props.film, props.locale, props.mode], start);
96
135
  watch(() => props.layout, (l) => show.value?.setLayout(l));
97
136
  watch([size, () => st.value?.layout], () => show.value?.fit(size.value.w, size.value.h));
98
137
  expose({ show });
99
- const onKeydown = (e: KeyboardEvent) => {
100
- const s = show.value; if (!s) return;
101
- const k = e.key.toLowerCase();
102
- if (k === 'arrowright') s.next(); else if (k === 'arrowleft') s.prev();
103
- else if (k === ' ') { s.toggle(); e.preventDefault(); }
104
- else if (k === 'm') s.setSound(!s.state.sound);
105
- else if (k === 'o') s.setLayout(s.state.layout === 'landscape' ? 'portrait' : 'landscape');
106
- };
107
- const btn = 'min-width:34px;height:34px;border-radius:17px;border:1px solid currentColor;background:none;color:inherit;cursor:pointer;padding:0 10px';
138
+ // Every key (←/→, Space, M, O, R, Home, F) from the core, so all players answer the same way.
139
+ const onKeydown = (e: KeyboardEvent) => { if (show.value && showKey(show.value, e, e.currentTarget as Element)) e.preventDefault(); };
140
+ const bar = (s: Show, x: ShowState) => h('div', { style: 'display:flex;gap:8px;align-items:center;justify-content:center;padding:8px;font:13px system-ui' }, [
141
+ h('button', { style: btn, onClick: () => s.prev(), 'aria-label': 'Previous scene' }, '‹'),
142
+ h('button', { style: btn, onClick: () => s.toggle(), 'aria-label': x.playing ? 'Pause' : 'Play' }, x.playing ? '❚❚' : '▶'),
143
+ h('button', { style: btn, onClick: () => s.next(), 'aria-label': 'Next scene' }, '›'),
144
+ h('div', { style: 'flex:1;display:flex;gap:3px;max-width:360px' }, ticks(s).map((tk, i) =>
145
+ h('button', { key: tk.id, onClick: () => s.go(i), 'aria-label': tk.name,
146
+ style: `flex:${tk.dur};min-width:0;height:18px;padding:7px 0;border:0;border-radius:0;background:${tickTrack};cursor:pointer;color:inherit` },
147
+ [h('span', { style: `display:block;height:4px;border-radius:2px;background:currentColor;width:${tk.fill * 100}%` })]))),
148
+ h('span', { style: 'opacity:.7;font-variant-numeric:tabular-nums' }, `${x.index + 1} / ${s.timeline.scenes.length}`),
149
+ h('button', { style: btn, onClick: () => s.setSound(!x.sound), 'aria-pressed': x.sound }, x.sound ? 'Sound on' : 'Sound off'),
150
+ props.fullscreenHref
151
+ ? h('a', { href: props.fullscreenHref, 'aria-label': 'Open full screen', title: 'Full screen', style: `${btn};display:grid;place-items:center;text-decoration:none;padding:0` }, '⤢')
152
+ : null,
153
+ ]);
108
154
  return () => {
109
155
  const s = show.value, x = st.value;
110
156
  return h('div', { tabindex: 0, role: 'application', 'aria-label': 'Story player', onKeydown,
111
157
  style: 'position:relative;width:100%;height:100%;display:flex;flex-direction:column;outline:none' }, [
112
158
  h('div', { ref: fit, style: 'position:relative;flex:1;min-height:0;display:grid;place-items:center;overflow:hidden' }, [h('div', { ref: stage, style: 'position:relative' })]),
113
- props.controls && s && x
114
- ? h('div', { style: 'display:flex;gap:8px;align-items:center;justify-content:center;padding:8px;font:13px system-ui' }, [
115
- h('button', { style: btn, onClick: () => s.prev(), 'aria-label': 'Previous scene' }, '‹'),
116
- h('button', { style: btn, onClick: () => s.toggle(), 'aria-label': x.playing ? 'Pause' : 'Play' }, x.playing ? '❚❚' : '▶'),
117
- h('button', { style: btn, onClick: () => s.next(), 'aria-label': 'Next scene' }, '›'),
118
- h('span', { style: 'opacity:.7' }, `${x.index + 1} / ${s.timeline.scenes.length}`),
119
- h('button', { style: btn, onClick: () => s.setSound(!x.sound), 'aria-pressed': x.sound }, x.sound ? 'Sound on' : 'Sound off'),
120
- ])
121
- : null,
159
+ waiting.value ? playButton(props.title, props.length, open, props.cover)
160
+ : s && x && !x.started ? playButton(props.title, props.length, () => s.start())
161
+ : props.controls && s && x ? bar(s, x) : null,
122
162
  ]);
123
163
  };
124
164
  },
@@ -133,12 +173,14 @@ export const ShowPage = defineComponent({
133
173
  locale: { type: String, default: 'en' },
134
174
  /** Capture renders light unless asked; the interactive player follows the page (auto). */
135
175
  mode: { type: String as PropType<Mode>, default: undefined },
176
+ /** Force scenes from their JSON or their code modules (parity checks). */
177
+ src: { type: String as PropType<'json' | 'js'>, default: undefined },
136
178
  capture: { type: Boolean, default: false },
137
179
  },
138
180
  setup(props) {
139
181
  const host = ref<HTMLElement | null>(null);
140
182
  let s: Show | null = null;
141
- onMounted(async () => { if (props.capture) s = await installCapture(host.value!, props.film, props.layout, props.locale, props.mode ?? 'light'); });
183
+ onMounted(async () => { if (props.capture) s = await installCapture(host.value!, props.film, props.layout, props.locale, { mode: props.mode ?? 'light', src: props.src }); });
142
184
  onBeforeUnmount(() => s?.destroy());
143
185
  return () => props.capture
144
186
  ? h('div', { ref: host })
@@ -1,9 +1,12 @@
1
1
  // Vue reactivity over the core: the core notifies, a shallowRef version makes reads tracked.
2
- import { onBeforeUnmount, shallowRef, ref, watch, type Ref } from 'vue';
2
+ import { onBeforeUnmount, shallowRef, ref, toRaw, watch, type Ref } from 'vue';
3
3
  import type { createShow, Layout, ScenePlayerState } from '@senseihq/emaki';
4
4
 
5
- /** A reactive view of a ScenePlayerState: read `state.value.t` etc. in templates; call methods on the player. */
6
- export function useScenePlayer(player: ScenePlayerState) {
5
+ /** A reactive view of a ScenePlayerState: read `state.value.t` etc. in templates; call methods on the player.
6
+ * A player kept in a ref or reactive() arrives as a proxy, which cannot reach the class's private fields: use the
7
+ * object behind it. */
8
+ export function useScenePlayer(held: ScenePlayerState) {
9
+ const player = toRaw(held);
7
10
  const version = ref(player.version);
8
11
  const off = player.subscribe(() => { version.value = player.version; });
9
12
  onBeforeUnmount(off);
@@ -14,18 +17,24 @@ export function useScenePlayer(player: ScenePlayerState) {
14
17
  }
15
18
 
16
19
  export type Show = Awaited<ReturnType<typeof createShow>>;
17
- export interface ShowState { index: number; local: number; playing: boolean; sound: boolean; layout: Layout }
20
+ export interface ShowState { index: number; local: number; playing: boolean; sound: boolean; layout: Layout; started: boolean }
18
21
 
19
- /** Reactive state of a story player controller (from <MotionShow @ready>). */
22
+ /** Reactive state of a story player controller (from <MotionShow @ready>), following the playhead (read once a
23
+ * frame; it moves without notifications). `started` is false on a poster. */
20
24
  export function useShowState(show: Ref<Show | null>) {
21
25
  const state = shallowRef<ShowState | null>(null);
22
- let off: (() => void) | null = null;
26
+ let off: (() => void) | null = null, raf = 0;
27
+ const follow = () => {
28
+ const s = show.value, x = state.value;
29
+ if (s && x && s.state.local !== x.local) state.value = { ...x, local: s.state.local };
30
+ raf = requestAnimationFrame(follow);
31
+ };
23
32
  watch(show, (s) => {
24
- off?.(); off = null;
33
+ off?.(); off = null; cancelAnimationFrame(raf);
25
34
  state.value = s ? { ...s.state } : null;
26
- if (s) off = s.on((x) => { state.value = { ...x }; }) as () => void;
35
+ if (s) { off = s.on((x) => { state.value = { ...x }; }) as () => void; raf = requestAnimationFrame(follow); }
27
36
  }, { immediate: true });
28
- onBeforeUnmount(() => off?.());
37
+ onBeforeUnmount(() => { off?.(); cancelAnimationFrame(raf); });
29
38
  return state;
30
39
  }
31
40