ink-framework-video 0.1.0-alpha.12 → 0.1.0-alpha.13
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/package.json +4 -3
- package/src/index.ts +55 -28
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ink-framework-video",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.13",
|
|
4
4
|
"exports": {
|
|
5
5
|
".": "./src/index.ts"
|
|
6
6
|
},
|
|
7
7
|
"peerDependencies": {
|
|
8
|
-
"ink": "npm:ink-framework@0.1.0-alpha.
|
|
8
|
+
"ink": "npm:ink-framework@0.1.0-alpha.13",
|
|
9
9
|
"react": "19.2.8"
|
|
10
10
|
},
|
|
11
11
|
"license": "MIT",
|
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
"files": [
|
|
18
18
|
"src",
|
|
19
19
|
"*.json",
|
|
20
|
-
"*.ts"
|
|
20
|
+
"*.ts",
|
|
21
|
+
"*.md"
|
|
21
22
|
]
|
|
22
23
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,48 +1,75 @@
|
|
|
1
|
-
import { createElement, useEffect,
|
|
1
|
+
import { createElement, useEffect, useState } from "ink/internal/react";
|
|
2
2
|
import { ErrorState } from "ink";
|
|
3
|
-
import {
|
|
3
|
+
import { useNativeController } from "ink/internal/controller";
|
|
4
4
|
|
|
5
|
-
type Playback = { ready: boolean; rendered: boolean; position: number; duration: number };
|
|
6
|
-
const initial: Playback = { ready: false, rendered: false, position: 0, duration: 0 };
|
|
5
|
+
type Playback = { ready: boolean; playing: boolean; rendered: boolean; position: number; duration: number };
|
|
6
|
+
const initial: Playback = { ready: false, playing: false, rendered: false, position: 0, duration: 0 };
|
|
7
7
|
|
|
8
8
|
export function Video({ src }: { src: string }) {
|
|
9
|
+
const [attempt, setAttempt] = useState(0);
|
|
10
|
+
const video = usePlayback(src, true, attempt);
|
|
11
|
+
if (video.error) return createElement(ErrorState, { message: video.error.message, onRetry: () => setAttempt(value => value + 1) });
|
|
12
|
+
const loading = video.id === null || !video.ready || !video.rendered;
|
|
13
|
+
return createElement("PlayingLayout", { hideControls: loading, bleed: true },
|
|
14
|
+
createElement("VideoView", { controller: video.id, loading }),
|
|
15
|
+
createElement(VideoProgress, { video }),
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function usePlayback(src: string | null, controls: boolean, attempt = 0) {
|
|
9
20
|
const [state, setState] = useState(initial);
|
|
10
21
|
const [error, setError] = useState<Error | null>(null);
|
|
11
|
-
const [attempt, setAttempt] = useState(0);
|
|
12
22
|
const [id, setId] = useState<number | null>(null);
|
|
13
|
-
const controller =
|
|
23
|
+
const controller = useNativeController("video");
|
|
14
24
|
useEffect(() => {
|
|
15
|
-
let active = true;
|
|
16
25
|
setState(initial);
|
|
17
26
|
setError(null);
|
|
18
27
|
setId(null);
|
|
19
|
-
|
|
20
|
-
|
|
28
|
+
if (!src) return;
|
|
29
|
+
return controller.attach({ src, controls }, value => {
|
|
21
30
|
if (typeof value !== "object" || value === null) return;
|
|
22
31
|
if ("error" in value && typeof value.error === "string") setError(new Error(value.error));
|
|
23
32
|
else if ("ready" in value && typeof value.ready === "boolean"
|
|
33
|
+
&& "playing" in value && typeof value.playing === "boolean"
|
|
24
34
|
&& "rendered" in value && typeof value.rendered === "boolean"
|
|
25
35
|
&& "position" in value && typeof value.position === "number"
|
|
26
36
|
&& "duration" in value && typeof value.duration === "number") {
|
|
27
|
-
setState({ ready: value.ready, rendered: value.rendered, position: value.position, duration: value.duration });
|
|
37
|
+
setState({ ready: value.ready, playing: value.playing, rendered: value.rendered, position: value.position, duration: value.duration });
|
|
28
38
|
}
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
active = false;
|
|
34
|
-
controller.current = null;
|
|
35
|
-
void attachment.dispose().catch(failure => console.error("Could not release video", failure));
|
|
36
|
-
};
|
|
37
|
-
}, [src, attempt]);
|
|
38
|
-
function seek(position: number) {
|
|
39
|
-
void controller.current?.call("seek", { position }).catch(setError);
|
|
39
|
+
}, setError, setId);
|
|
40
|
+
}, [src, controls, attempt, controller]);
|
|
41
|
+
function seek(position: number, exact = true) {
|
|
42
|
+
void controller.current?.call("seek", { position, exact }).catch(setError);
|
|
40
43
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
44
|
+
const toggle = () => { void controller.current?.call(state.playing ? "pause" : "play").catch(setError); };
|
|
45
|
+
return { id, ...state, error, seek, toggle };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export type InlineVideo = {
|
|
49
|
+
/** Pass to `Image`'s `videoController` to draw the video over the image. */
|
|
50
|
+
id: number | null;
|
|
51
|
+
playing: boolean;
|
|
52
|
+
/** Whether a frame is showing; until then the poster is visible. */
|
|
53
|
+
rendered: boolean;
|
|
54
|
+
/** Seconds. */
|
|
55
|
+
position: number;
|
|
56
|
+
duration: number;
|
|
57
|
+
/** Seeks to seconds; quick seeks may land on a nearby keyframe. */
|
|
58
|
+
seek: (position: number, exact?: boolean) => void;
|
|
59
|
+
error: Error | null;
|
|
60
|
+
toggle: () => void;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* A paused video without controls for drawing over an image. Touches reach the app, so the
|
|
65
|
+
* image can still be swiped; call `toggle` from its `onPress`.
|
|
66
|
+
*/
|
|
67
|
+
export function useVideo(src: string | null): InlineVideo {
|
|
68
|
+
return usePlayback(src, false);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Seekable progress for a video from `useVideo`, updated natively while it plays. */
|
|
72
|
+
export function VideoProgress({ video }: { video: InlineVideo }) {
|
|
73
|
+
return createElement("PlayingProgress", { clock: video.id, position: video.position, duration: video.duration,
|
|
74
|
+
showTimes: true, onSeek: video.seek });
|
|
48
75
|
}
|