react-a11y-auto-caption 1.0.0 → 1.0.3
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 +152 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +213 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +185 -0
- package/dist/index.mjs.map +1 -0
- package/dist/next.d.ts +13 -0
- package/dist/next.js +221 -0
- package/dist/next.js.map +1 -0
- package/dist/next.mjs +186 -0
- package/dist/next.mjs.map +1 -0
- package/dist/useAICaption.d.ts +30 -0
- package/package.json +55 -41
- package/src/index.tsx +0 -161
- package/src/next-env.d.ts +0 -2
- package/src/next.tsx +0 -154
- package/tsconfig.json +0 -19
- package/tsup.config.ts +0 -11
package/src/index.tsx
DELETED
|
@@ -1,161 +0,0 @@
|
|
|
1
|
-
import React, { createContext, useContext, useState, useEffect, ImgHTMLAttributes } from "react";
|
|
2
|
-
|
|
3
|
-
interface SmartImageContextProps {
|
|
4
|
-
apiEndpoint?: string;
|
|
5
|
-
disableAI?: boolean;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export const SmartImageContext = createContext<SmartImageContextProps | undefined>(undefined);
|
|
9
|
-
|
|
10
|
-
export const SmartImageProvider: React.FC<{
|
|
11
|
-
value: SmartImageContextProps;
|
|
12
|
-
children: React.ReactNode;
|
|
13
|
-
}> = ({ value, children }) => {
|
|
14
|
-
return <SmartImageContext.Provider value={value}>{children}</SmartImageContext.Provider>;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
const captionCache = new Map<string, string>();
|
|
18
|
-
const pendingRequestCache = new Map<string, Promise<string>>();
|
|
19
|
-
|
|
20
|
-
export interface SmartImageProps extends ImgHTMLAttributes<HTMLImageElement> {
|
|
21
|
-
apiEndpoint?: string;
|
|
22
|
-
fallbackAlt?: string;
|
|
23
|
-
onCaptionGenerated?: (caption: string) => void;
|
|
24
|
-
disableAI?: boolean;
|
|
25
|
-
announceLive?: boolean;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export const SmartImage = ({
|
|
29
|
-
src,
|
|
30
|
-
alt,
|
|
31
|
-
apiEndpoint: propsEndpoint,
|
|
32
|
-
fallbackAlt = "Image loading or caption unavailable",
|
|
33
|
-
onCaptionGenerated,
|
|
34
|
-
disableAI: propsDisableAI,
|
|
35
|
-
announceLive = false,
|
|
36
|
-
...props
|
|
37
|
-
}: SmartImageProps) => {
|
|
38
|
-
const context = useContext(SmartImageContext);
|
|
39
|
-
|
|
40
|
-
const apiEndpoint = propsEndpoint || context?.apiEndpoint;
|
|
41
|
-
const disableAI = propsDisableAI ?? context?.disableAI ?? false;
|
|
42
|
-
|
|
43
|
-
const [generatedAlt, setGeneratedAlt] = useState("");
|
|
44
|
-
const [isGenerating, setIsGenerating] = useState(false);
|
|
45
|
-
|
|
46
|
-
useEffect(() => {
|
|
47
|
-
if (alt) {
|
|
48
|
-
setGeneratedAlt(alt);
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
if (!src) return;
|
|
53
|
-
|
|
54
|
-
if (disableAI) {
|
|
55
|
-
setGeneratedAlt("[Testing mode: AI caption generation disabled]");
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
if (!apiEndpoint) {
|
|
60
|
-
console.warn(
|
|
61
|
-
"[SmartImage] Missing 'apiEndpoint' prop. Please provide a backend API URL via props or SmartImageProvider to enable AI caption generation.",
|
|
62
|
-
);
|
|
63
|
-
setGeneratedAlt(fallbackAlt);
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const imageUrl = src as string;
|
|
68
|
-
|
|
69
|
-
if (captionCache.has(imageUrl)) {
|
|
70
|
-
console.log("[SmartImage] Cache hit: Reusing existing caption.");
|
|
71
|
-
const cachedCaption = captionCache.get(imageUrl)!;
|
|
72
|
-
setGeneratedAlt(cachedCaption);
|
|
73
|
-
|
|
74
|
-
if (onCaptionGenerated) onCaptionGenerated(cachedCaption);
|
|
75
|
-
return;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
const generateCaption = async () => {
|
|
79
|
-
setIsGenerating(true);
|
|
80
|
-
try {
|
|
81
|
-
if (pendingRequestCache.has(imageUrl)) {
|
|
82
|
-
console.log("[SmartImage] Pending request detected. Waiting for the existing API call to complete.");
|
|
83
|
-
const caption = await pendingRequestCache.get(imageUrl)!;
|
|
84
|
-
setGeneratedAlt(caption);
|
|
85
|
-
if (onCaptionGenerated) onCaptionGenerated(caption);
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const fetchPromise = (async () => {
|
|
90
|
-
const imageResponse = await fetch(imageUrl);
|
|
91
|
-
const imageBlob = await imageResponse.blob();
|
|
92
|
-
|
|
93
|
-
const imageFile = new File([imageBlob], "image.jpg", {
|
|
94
|
-
type: imageBlob.type || "image/jpeg",
|
|
95
|
-
});
|
|
96
|
-
const formData = new FormData();
|
|
97
|
-
|
|
98
|
-
formData.append("file", imageFile);
|
|
99
|
-
|
|
100
|
-
const response = await fetch(apiEndpoint, {
|
|
101
|
-
method: "POST",
|
|
102
|
-
body: formData,
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
if (!response.ok) throw new Error("AI API request failed");
|
|
106
|
-
const data = await response.json();
|
|
107
|
-
|
|
108
|
-
if (data.caption) return data.caption;
|
|
109
|
-
throw new Error("No caption returned from the API.");
|
|
110
|
-
})();
|
|
111
|
-
|
|
112
|
-
pendingRequestCache.set(imageUrl, fetchPromise);
|
|
113
|
-
|
|
114
|
-
const newCaption = await fetchPromise;
|
|
115
|
-
|
|
116
|
-
pendingRequestCache.delete(imageUrl);
|
|
117
|
-
captionCache.set(imageUrl, newCaption);
|
|
118
|
-
|
|
119
|
-
setGeneratedAlt(newCaption);
|
|
120
|
-
|
|
121
|
-
if (onCaptionGenerated) onCaptionGenerated(newCaption);
|
|
122
|
-
} catch (error) {
|
|
123
|
-
console.error("[SmartImage] Caption Error:", error);
|
|
124
|
-
pendingRequestCache.delete(imageUrl);
|
|
125
|
-
setGeneratedAlt(fallbackAlt);
|
|
126
|
-
} finally {
|
|
127
|
-
setIsGenerating(false);
|
|
128
|
-
}
|
|
129
|
-
};
|
|
130
|
-
|
|
131
|
-
generateCaption();
|
|
132
|
-
}, [src, alt, apiEndpoint, fallbackAlt, onCaptionGenerated, disableAI]);
|
|
133
|
-
|
|
134
|
-
const srOnlyStyle: React.CSSProperties = {
|
|
135
|
-
position: "absolute",
|
|
136
|
-
width: "1px",
|
|
137
|
-
height: "1px",
|
|
138
|
-
padding: 0,
|
|
139
|
-
margin: "-1px",
|
|
140
|
-
overflow: "hidden",
|
|
141
|
-
clip: "rect(0, 0, 0, 0)",
|
|
142
|
-
whiteSpace: "nowrap",
|
|
143
|
-
borderWidth: 0,
|
|
144
|
-
};
|
|
145
|
-
|
|
146
|
-
return (
|
|
147
|
-
<>
|
|
148
|
-
{announceLive && (
|
|
149
|
-
<span style={srOnlyStyle} aria-live="polite" aria-atomic="true">
|
|
150
|
-
{isGenerating
|
|
151
|
-
? "Generating image description. Please wait..."
|
|
152
|
-
: generatedAlt
|
|
153
|
-
? `Image description generated: ${generatedAlt}`
|
|
154
|
-
: ""}
|
|
155
|
-
</span>
|
|
156
|
-
)}
|
|
157
|
-
|
|
158
|
-
<img src={src} alt={generatedAlt} aria-busy={isGenerating} {...props} />
|
|
159
|
-
</>
|
|
160
|
-
);
|
|
161
|
-
};
|
package/src/next-env.d.ts
DELETED
package/src/next.tsx
DELETED
|
@@ -1,154 +0,0 @@
|
|
|
1
|
-
import React, { useState, useEffect, useContext } from "react";
|
|
2
|
-
import Image, { ImageProps } from "next/image";
|
|
3
|
-
|
|
4
|
-
import { SmartImageContext } from "./index";
|
|
5
|
-
|
|
6
|
-
function isStaticImport(source: any): source is { src: string } {
|
|
7
|
-
return typeof source === "object" && source !== null && "src" in source;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
const captionCache = new Map<string, string>();
|
|
11
|
-
const pendingRequestCache = new Map<string, Promise<string>>();
|
|
12
|
-
|
|
13
|
-
export interface SmartNextImageProps extends Omit<ImageProps, "alt"> {
|
|
14
|
-
alt?: string;
|
|
15
|
-
apiEndpoint?: string;
|
|
16
|
-
fallbackAlt?: string;
|
|
17
|
-
onCaptionGenerated?: (caption: string) => void;
|
|
18
|
-
disableAI?: boolean;
|
|
19
|
-
announceLive?: boolean;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export const SmartNextImage = ({
|
|
23
|
-
src,
|
|
24
|
-
alt,
|
|
25
|
-
apiEndpoint: propsEndpoint,
|
|
26
|
-
fallbackAlt = "Image loading or caption unavailable",
|
|
27
|
-
onCaptionGenerated,
|
|
28
|
-
disableAI: propsDisableAI,
|
|
29
|
-
announceLive = false,
|
|
30
|
-
...props
|
|
31
|
-
}: SmartNextImageProps) => {
|
|
32
|
-
const context = useContext(SmartImageContext);
|
|
33
|
-
const apiEndpoint = propsEndpoint || context?.apiEndpoint;
|
|
34
|
-
const disableAI = propsDisableAI ?? context?.disableAI ?? false;
|
|
35
|
-
|
|
36
|
-
const [generatedAlt, setGeneratedAlt] = useState("");
|
|
37
|
-
const [isGenerating, setIsGenerating] = useState(false);
|
|
38
|
-
|
|
39
|
-
const imageUrl = isStaticImport(src) ? src.src : (src as string);
|
|
40
|
-
|
|
41
|
-
useEffect(() => {
|
|
42
|
-
if (alt) {
|
|
43
|
-
setGeneratedAlt(alt);
|
|
44
|
-
return;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
if (!src) return;
|
|
48
|
-
|
|
49
|
-
if (disableAI) {
|
|
50
|
-
setGeneratedAlt("[Testing mode: AI caption generation disabled]");
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
if (!apiEndpoint) {
|
|
55
|
-
console.warn(
|
|
56
|
-
"[SmartNextImage] Missing 'apiEndpoint' prop. Please provide a backend API URL via props or SmartImageProvider to enable AI caption generation.",
|
|
57
|
-
);
|
|
58
|
-
setGeneratedAlt(fallbackAlt);
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
if (captionCache.has(imageUrl)) {
|
|
63
|
-
console.log("[SmartNextImage] Cache hit: Reusing existing caption.");
|
|
64
|
-
const cachedCaption = captionCache.get(imageUrl)!;
|
|
65
|
-
setGeneratedAlt(cachedCaption);
|
|
66
|
-
|
|
67
|
-
if (onCaptionGenerated) onCaptionGenerated(cachedCaption);
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
const generateCaption = async () => {
|
|
72
|
-
setIsGenerating(true);
|
|
73
|
-
try {
|
|
74
|
-
if (pendingRequestCache.has(imageUrl)) {
|
|
75
|
-
console.log("[SmartNextImage] Pending request detected. Waiting for the existing API call to complete.");
|
|
76
|
-
const caption = await pendingRequestCache.get(imageUrl)!;
|
|
77
|
-
setGeneratedAlt(caption);
|
|
78
|
-
if (onCaptionGenerated) onCaptionGenerated(caption);
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const fetchPromise = (async () => {
|
|
83
|
-
const imageResponse = await fetch(imageUrl);
|
|
84
|
-
const imageBlob = await imageResponse.blob();
|
|
85
|
-
|
|
86
|
-
const imageFile = new File([imageBlob], "image.jpg", {
|
|
87
|
-
type: imageBlob.type || "image/jpeg",
|
|
88
|
-
});
|
|
89
|
-
const formData = new FormData();
|
|
90
|
-
|
|
91
|
-
formData.append("file", imageFile);
|
|
92
|
-
|
|
93
|
-
const response = await fetch(apiEndpoint, {
|
|
94
|
-
method: "POST",
|
|
95
|
-
body: formData,
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
if (!response.ok) throw new Error("AI API request failed");
|
|
99
|
-
const data = await response.json();
|
|
100
|
-
|
|
101
|
-
if (data.caption) return data.caption;
|
|
102
|
-
throw new Error("No caption returned from the API.");
|
|
103
|
-
})();
|
|
104
|
-
|
|
105
|
-
pendingRequestCache.set(imageUrl, fetchPromise);
|
|
106
|
-
|
|
107
|
-
const newCaption = await fetchPromise;
|
|
108
|
-
|
|
109
|
-
pendingRequestCache.delete(imageUrl);
|
|
110
|
-
captionCache.set(imageUrl, newCaption);
|
|
111
|
-
|
|
112
|
-
setGeneratedAlt(newCaption);
|
|
113
|
-
|
|
114
|
-
if (onCaptionGenerated) onCaptionGenerated(newCaption);
|
|
115
|
-
} catch (error) {
|
|
116
|
-
console.error("[SmartNextImage] Caption Error:", error);
|
|
117
|
-
pendingRequestCache.delete(imageUrl);
|
|
118
|
-
setGeneratedAlt(fallbackAlt);
|
|
119
|
-
} finally {
|
|
120
|
-
setIsGenerating(false);
|
|
121
|
-
}
|
|
122
|
-
};
|
|
123
|
-
|
|
124
|
-
generateCaption();
|
|
125
|
-
}, [src, alt, apiEndpoint, fallbackAlt, onCaptionGenerated, disableAI, imageUrl]);
|
|
126
|
-
|
|
127
|
-
const srOnlyStyle: React.CSSProperties = {
|
|
128
|
-
position: "absolute",
|
|
129
|
-
width: "1px",
|
|
130
|
-
height: "1px",
|
|
131
|
-
padding: 0,
|
|
132
|
-
margin: "-1px",
|
|
133
|
-
overflow: "hidden",
|
|
134
|
-
clip: "rect(0, 0, 0, 0)",
|
|
135
|
-
whiteSpace: "nowrap",
|
|
136
|
-
borderWidth: 0,
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
return (
|
|
140
|
-
<>
|
|
141
|
-
{announceLive && (
|
|
142
|
-
<span style={srOnlyStyle} aria-live="polite" aria-atomic="true">
|
|
143
|
-
{isGenerating
|
|
144
|
-
? "Generating image description. Please wait..."
|
|
145
|
-
: generatedAlt
|
|
146
|
-
? `Image description generated: ${generatedAlt}`
|
|
147
|
-
: ""}
|
|
148
|
-
</span>
|
|
149
|
-
)}
|
|
150
|
-
|
|
151
|
-
<Image src={src} alt={generatedAlt || ""} aria-busy={isGenerating} {...props} />
|
|
152
|
-
</>
|
|
153
|
-
);
|
|
154
|
-
};
|
package/tsconfig.json
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"rootDir": "./src",
|
|
4
|
-
"target": "es2020",
|
|
5
|
-
"lib": ["dom", "dom.iterable", "esnext"],
|
|
6
|
-
"allowJs": false,
|
|
7
|
-
"skipLibCheck": true,
|
|
8
|
-
"esModuleInterop": true,
|
|
9
|
-
"allowSyntheticDefaultImports": true,
|
|
10
|
-
"strict": true,
|
|
11
|
-
"forceConsistentCasingInFileNames": true,
|
|
12
|
-
"module": "esnext",
|
|
13
|
-
"moduleResolution": "bundler",
|
|
14
|
-
"resolveJsonModule": true,
|
|
15
|
-
"isolatedModules": true,
|
|
16
|
-
"jsx": "react-jsx"
|
|
17
|
-
},
|
|
18
|
-
"include": ["src"]
|
|
19
|
-
}
|
package/tsup.config.ts
DELETED