ink-framework-camera 0.1.0-alpha.12 → 0.1.0-alpha.14
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 +5 -4
- package/src/shared.ts +7 -15
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ink-framework-camera",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.14",
|
|
4
4
|
"exports": {
|
|
5
5
|
".": "./src/index.ts",
|
|
6
6
|
"./internal": "./src/shared.ts"
|
|
7
7
|
},
|
|
8
8
|
"peerDependencies": {
|
|
9
|
-
"@ink/files": "npm:ink-framework-files@0.1.0-alpha.
|
|
10
|
-
"ink": "npm:ink-framework@0.1.0-alpha.
|
|
9
|
+
"@ink/files": "npm:ink-framework-files@0.1.0-alpha.14",
|
|
10
|
+
"ink": "npm:ink-framework@0.1.0-alpha.14",
|
|
11
11
|
"react": "19.2.8"
|
|
12
12
|
},
|
|
13
13
|
"license": "MIT",
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
"files": [
|
|
20
20
|
"src",
|
|
21
21
|
"*.json",
|
|
22
|
-
"*.ts"
|
|
22
|
+
"*.ts",
|
|
23
|
+
"*.md"
|
|
23
24
|
]
|
|
24
25
|
}
|
package/src/shared.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { createElement, useEffect, useMemo,
|
|
1
|
+
import { createElement, useEffect, useMemo, useState } from "ink/internal/react";
|
|
2
2
|
import { Button, Field, Image, Stack, useAction } from "ink";
|
|
3
3
|
import { NativeError } from "ink/native";
|
|
4
|
-
import {
|
|
4
|
+
import { useNativeController } from "ink/internal/controller";
|
|
5
5
|
import type { FileRef } from "@ink/files";
|
|
6
6
|
|
|
7
7
|
const previewController = Symbol("previewController");
|
|
@@ -22,12 +22,12 @@ function initial<T>(): CameraState<T> { return { status: "idle", ready: false, v
|
|
|
22
22
|
export function useSession<T>(kind: "photo" | "scanner", decode: (value: unknown) => T, config = "{}") {
|
|
23
23
|
const [state, setState] = useState<CameraState<T>>(initial);
|
|
24
24
|
const [previewId, setPreviewId] = useState<number | null>(null);
|
|
25
|
-
const controller =
|
|
25
|
+
const controller = useNativeController("camera");
|
|
26
26
|
useEffect(() => {
|
|
27
27
|
setState(initial());
|
|
28
28
|
setPreviewId(null);
|
|
29
29
|
const fail = (error: unknown) => setState({ ...initial<T>(), status: "error", error: error instanceof Error ? error : new Error(String(error)) });
|
|
30
|
-
|
|
30
|
+
return controller.attach({ kind, config: JSON.parse(config) }, value => {
|
|
31
31
|
try {
|
|
32
32
|
if (typeof value !== "object" || value === null || !("status" in value)) throw new NativeError("protocol", "Invalid camera state");
|
|
33
33
|
if ("reviewSource" in value) {
|
|
@@ -49,23 +49,15 @@ export function useSession<T>(kind: "photo" | "scanner", decode: (value: unknown
|
|
|
49
49
|
setState({ ...initial<T>(), status, ready: status === "active" });
|
|
50
50
|
} else throw new NativeError("protocol", "Invalid camera status");
|
|
51
51
|
} catch (error) { fail(error); }
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
void attachment.ready.then(() => {
|
|
55
|
-
if (controller.current === attachment) setPreviewId(attachment.id);
|
|
56
|
-
}, error => { if (controller.current === attachment) fail(error); });
|
|
57
|
-
return () => {
|
|
58
|
-
controller.current = null;
|
|
59
|
-
void attachment.dispose().catch(error => console.error("Could not release camera", error));
|
|
60
|
-
};
|
|
61
|
-
}, [kind, decode, config]);
|
|
52
|
+
}, fail, setPreviewId);
|
|
53
|
+
}, [kind, decode, config, controller]);
|
|
62
54
|
const commands = useMemo(() => {
|
|
63
55
|
const call = async (operation: string) => {
|
|
64
56
|
if (!controller.current) throw new NativeError("unavailable", "Camera is not attached");
|
|
65
57
|
await controller.current.call(operation);
|
|
66
58
|
};
|
|
67
59
|
return { capture: () => call("capture"), open: () => call("open"), retake: () => call("retake"), accept: () => call("use-photo") };
|
|
68
|
-
}, []);
|
|
60
|
+
}, [controller]);
|
|
69
61
|
return { kind, state, [previewController]: previewId, ...commands };
|
|
70
62
|
}
|
|
71
63
|
export function CameraPreview({ controller }: { controller: ReturnType<typeof useSession<CapturedPhoto>> | ReturnType<typeof useSession<CodeScan>> }) {
|