datocms-plugin-alt-text-ai 0.5.0 → 0.5.1
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/.idea/datocms-plugin-alt-text-ai.iml +8 -0
- package/.idea/inspectionProfiles/Project_Default.xml +10 -0
- package/.idea/jsLibraryMappings.xml +6 -0
- package/.idea/modules.xml +8 -0
- package/.idea/prettier.xml +7 -0
- package/.idea/vcs.xml +6 -0
- package/build/asset-manifest.json +3 -3
- package/build/index.html +1 -1
- package/build/static/js/{main.5f1a953a.js → main.d6fa325c.js} +3 -3
- package/build/static/js/main.d6fa325c.js.map +1 -0
- package/package.json +1 -1
- package/src/entrypoints/AltTextAIButton.tsx +38 -7
- package/build/static/js/main.5f1a953a.js.map +0 -1
- /package/build/static/js/{main.5f1a953a.js.LICENSE.txt → main.d6fa325c.js.LICENSE.txt} +0 -0
package/package.json
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { Client, buildClient } from '@datocms/cma-client-browser';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
type FileFieldValue,
|
|
4
|
+
RenderFieldExtensionCtx,
|
|
5
|
+
} from "datocms-plugin-sdk";
|
|
3
6
|
import { Button, Canvas, Spinner } from 'datocms-react-ui';
|
|
4
7
|
import { isArray } from 'lodash';
|
|
5
8
|
import get from 'lodash/get';
|
|
@@ -12,10 +15,16 @@ type PropTypes = {
|
|
|
12
15
|
async function fetchAlt(
|
|
13
16
|
apiKey: string,
|
|
14
17
|
client: Client,
|
|
15
|
-
asset:
|
|
18
|
+
asset: FileFieldValue
|
|
16
19
|
) {
|
|
17
20
|
const { url } = await client.uploads.find(asset.upload_id);
|
|
18
21
|
|
|
22
|
+
// Instead of sending over files that are possibly too big or the wrong format, etc.,
|
|
23
|
+
// we'll use Imgix to pre-transform them into a valid format and smaller size
|
|
24
|
+
let transformedUrl = new URL(url);
|
|
25
|
+
transformedUrl.searchParams.set("fm", "jpeg");
|
|
26
|
+
transformedUrl.searchParams.set("w", "1024");
|
|
27
|
+
|
|
19
28
|
const result = await (
|
|
20
29
|
await fetch('https://alttext.ai/api/v1/images', {
|
|
21
30
|
method: 'POST',
|
|
@@ -25,7 +34,7 @@ async function fetchAlt(
|
|
|
25
34
|
},
|
|
26
35
|
body: JSON.stringify({
|
|
27
36
|
image: {
|
|
28
|
-
url,
|
|
37
|
+
url: transformedUrl.toString(),
|
|
29
38
|
},
|
|
30
39
|
}),
|
|
31
40
|
})
|
|
@@ -39,25 +48,39 @@ async function generateAlts(
|
|
|
39
48
|
ctx: RenderFieldExtensionCtx,
|
|
40
49
|
setIsLoading: React.Dispatch<React.SetStateAction<boolean>>
|
|
41
50
|
) {
|
|
51
|
+
if (!ctx.currentUserAccessToken) {
|
|
52
|
+
await ctx.alert('This plugin needs the currentUserAccessToken to function. Please give it that permission and try again.');
|
|
53
|
+
return
|
|
54
|
+
}
|
|
55
|
+
|
|
42
56
|
setIsLoading(true);
|
|
43
57
|
try {
|
|
44
58
|
const client = buildClient({ apiToken: ctx.currentUserAccessToken || '' });
|
|
45
59
|
const isGallery = isArray(currentFieldValue);
|
|
46
60
|
if (isGallery) {
|
|
61
|
+
const existingAssets = currentFieldValue as FileFieldValue[]
|
|
47
62
|
const newAssets = [];
|
|
48
|
-
for (const asset of
|
|
63
|
+
for (const asset of existingAssets) {
|
|
64
|
+
console.log('asset', asset);
|
|
49
65
|
const result = await fetchAlt(
|
|
50
66
|
ctx.plugin.attributes.parameters.apiKey as string,
|
|
51
67
|
client,
|
|
52
68
|
asset
|
|
53
69
|
);
|
|
54
70
|
|
|
71
|
+
if (result.error_code) {
|
|
72
|
+
ctx.alert(
|
|
73
|
+
`Alt text error for <a href="/media/assets/${asset.upload_id}" target="_blank">image ${asset.upload_id}</a>: <code>${result.error_code}: ${result.errors?.base?.[0] ?? ""}</code>`,
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
|
|
55
78
|
asset.alt = result.alt_text;
|
|
56
79
|
newAssets.push(asset);
|
|
57
80
|
}
|
|
58
81
|
ctx.setFieldValue(ctx.fieldPath, newAssets);
|
|
59
82
|
} else {
|
|
60
|
-
const assetValue = currentFieldValue as
|
|
83
|
+
const assetValue = currentFieldValue as FileFieldValue;
|
|
61
84
|
|
|
62
85
|
const result = await fetchAlt(
|
|
63
86
|
ctx.plugin.attributes.parameters.apiKey as string,
|
|
@@ -65,14 +88,22 @@ async function generateAlts(
|
|
|
65
88
|
assetValue
|
|
66
89
|
);
|
|
67
90
|
|
|
91
|
+
if (result.error_code) {
|
|
92
|
+
await ctx.alert(
|
|
93
|
+
`Error fetching alt text: <code>${result.error_code}: ${result.errors?.base?.[0] ?? ""}</code>`,
|
|
94
|
+
);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
|
|
68
99
|
assetValue.alt = result.alt_text;
|
|
69
100
|
ctx.setFieldValue(ctx.fieldPath, assetValue);
|
|
70
101
|
}
|
|
71
102
|
} catch (error) {
|
|
72
103
|
console.log(error);
|
|
104
|
+
} finally {
|
|
105
|
+
setIsLoading(false);
|
|
73
106
|
}
|
|
74
|
-
|
|
75
|
-
setIsLoading(false);
|
|
76
107
|
}
|
|
77
108
|
|
|
78
109
|
const AltTextAIButton = ({ ctx }: PropTypes) => {
|