@vonaffenfels/contentful-slate-editor 1.1.28 → 1.1.30
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 +3 -3
- package/src/components/Dialog.js +3 -0
- package/src/components/DialogTranslation.js +298 -0
- package/src/components/Sidebar.js +71 -0
- package/src/index.js +5 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vonaffenfels/contentful-slate-editor",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.30",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"prepublish": "yarn run build",
|
|
6
6
|
"dev": "yarn run start",
|
|
@@ -93,10 +93,10 @@
|
|
|
93
93
|
"webpack-watch-files-plugin": "^1.2.1"
|
|
94
94
|
},
|
|
95
95
|
"dependencies": {
|
|
96
|
-
"@vonaffenfels/slate-editor": "^1.1.
|
|
96
|
+
"@vonaffenfels/slate-editor": "^1.1.29",
|
|
97
97
|
"webpack": "5.88.2"
|
|
98
98
|
},
|
|
99
|
-
"gitHead": "
|
|
99
|
+
"gitHead": "d4bee0dde0eb4aeaaa5881fd11823ccd9258695e",
|
|
100
100
|
"publishConfig": {
|
|
101
101
|
"access": "public"
|
|
102
102
|
}
|
package/src/components/Dialog.js
CHANGED
|
@@ -7,6 +7,7 @@ import componentLoader from "@vonaffenfels/slate-editor/componentLoader";
|
|
|
7
7
|
import BlockEditor from "@vonaffenfels/slate-editor/dist/BlockEditor";
|
|
8
8
|
// eslint-disable-next-line import/no-unresolved
|
|
9
9
|
import storybookStories from "storybookStories";
|
|
10
|
+
import {DialogTranslation} from "./DialogTranslation";
|
|
10
11
|
|
|
11
12
|
const Dialog = ({
|
|
12
13
|
sdk,
|
|
@@ -30,6 +31,8 @@ const Dialog = ({
|
|
|
30
31
|
switch (sdk.parameters.invocation.type) {
|
|
31
32
|
case "cloudinary":
|
|
32
33
|
return <DialogCloudinary sdk={sdk} config={sdk.parameters.invocation} activeConfig={activeConfig} />;
|
|
34
|
+
case "translation":
|
|
35
|
+
return <DialogTranslation sdk={sdk} config={sdk.parameters.invocation} activeConfig={activeConfig} />;
|
|
33
36
|
default:
|
|
34
37
|
return <div className="relative flex h-full max-h-full justify-between">
|
|
35
38
|
<BlockEditor
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useEffect, useState,
|
|
3
|
+
} from "react";
|
|
4
|
+
import {
|
|
5
|
+
Button, Table,
|
|
6
|
+
} from "@contentful/f36-components";
|
|
7
|
+
import getSlug from "speakingurl";
|
|
8
|
+
|
|
9
|
+
export const DialogTranslation = ({
|
|
10
|
+
sdk,
|
|
11
|
+
activeConfig,
|
|
12
|
+
translateFromLocale = "de",
|
|
13
|
+
}) => {
|
|
14
|
+
const {entryId} = sdk.parameters.invocation;
|
|
15
|
+
const [entry, setEntry] = useState(null);
|
|
16
|
+
const [loading, setLoading] = useState({});
|
|
17
|
+
const [usage, setUsage] = useState({});
|
|
18
|
+
const [contentType, setContentType] = useState(null);
|
|
19
|
+
const localizedFields = contentType?.fields?.filter(field => field.localized);
|
|
20
|
+
|
|
21
|
+
const getTranslationUsage = async (content) => {
|
|
22
|
+
if (!sdk?.parameters?.installation?.translationService) {
|
|
23
|
+
console.error(`no translation service configured!`);
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
const response = await fetch(sdk?.parameters?.installation?.translationService + "/usage", {
|
|
29
|
+
method: "POST",
|
|
30
|
+
body: JSON.stringify({
|
|
31
|
+
editorContent: typeof content === "object" ? content : undefined,
|
|
32
|
+
text: typeof content === "string" ? content : undefined,
|
|
33
|
+
}),
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const translatedValue = await response.json();
|
|
37
|
+
return translatedValue?.usage || 0;
|
|
38
|
+
} catch (e) {
|
|
39
|
+
console.error(e);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const updateUsage = async (entry, contentType) => {
|
|
44
|
+
const localizedFields = contentType?.fields?.filter(field => field.localized);
|
|
45
|
+
const newUsage = {};
|
|
46
|
+
for (let i = 0; i < localizedFields.length; i++) {
|
|
47
|
+
const localizedField = localizedFields[i];
|
|
48
|
+
newUsage[localizedField.id] = await getTranslationUsage(entry?.fields[localizedField.id]?.[translateFromLocale]);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return newUsage;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
sdk.space.getEntry(entryId).then(entry => {
|
|
56
|
+
sdk.space.getContentType(entry.sys.contentType.sys.id).then(contentType => {
|
|
57
|
+
updateUsage(entry, contentType).then((usage) => {
|
|
58
|
+
setUsage(usage);
|
|
59
|
+
setEntry(entry);
|
|
60
|
+
setContentType(contentType);
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
}, []);
|
|
65
|
+
|
|
66
|
+
if (!contentType) {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const translateValue = async (content, targetLanguage) => {
|
|
71
|
+
if (!sdk?.parameters?.installation?.translationService) {
|
|
72
|
+
console.error(`no translation service configured!`);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
const response = await fetch(sdk?.parameters?.installation?.translationService, {
|
|
78
|
+
method: "POST",
|
|
79
|
+
body: JSON.stringify({
|
|
80
|
+
editorContent: typeof content === "object" ? content : undefined,
|
|
81
|
+
text: typeof content === "string" ? content : undefined,
|
|
82
|
+
targetLanguage,
|
|
83
|
+
translateFromLocale: translateFromLocale,
|
|
84
|
+
}),
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
const translatedValue = await response.json();
|
|
88
|
+
return translatedValue?.translation || "";
|
|
89
|
+
} catch (e) {
|
|
90
|
+
console.error(e);
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const onTranslateClick = async (targetLanguageOrLanguages, refreshAll = false, onlyFields = []) => {
|
|
95
|
+
if (!Array.isArray(targetLanguageOrLanguages)) {
|
|
96
|
+
targetLanguageOrLanguages = [targetLanguageOrLanguages];
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
let newEntry = entry;
|
|
100
|
+
for (let i = 0; i < targetLanguageOrLanguages.length; i++) {
|
|
101
|
+
if (targetLanguageOrLanguages[i] === translateFromLocale) {
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
newEntry = await translateForLocale(
|
|
106
|
+
targetLanguageOrLanguages[i], newEntry, refreshAll, onlyFields,
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
await sdk.space.updateEntry(newEntry);
|
|
111
|
+
setEntry(await sdk.space.getEntry(entryId));
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const translateForLocale = async (
|
|
115
|
+
targetLanguage, entry = {}, refreshAll = false, onlyFields = [],
|
|
116
|
+
) => {
|
|
117
|
+
setLoading({
|
|
118
|
+
...loading,
|
|
119
|
+
[targetLanguage]: true,
|
|
120
|
+
});
|
|
121
|
+
const newEntry = {...entry};
|
|
122
|
+
for (let i = 0; i < localizedFields.length; i++) {
|
|
123
|
+
const localizedField = localizedFields[i];
|
|
124
|
+
const currentValue = entry?.fields[localizedField.id]?.[targetLanguage];
|
|
125
|
+
if (
|
|
126
|
+
(!refreshAll && !isEmptyValue(currentValue) && !onlyFields.length) ||
|
|
127
|
+
(onlyFields.length && !onlyFields.includes(localizedField.id))
|
|
128
|
+
) {
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
setLoading({
|
|
133
|
+
...loading,
|
|
134
|
+
[targetLanguage + localizedField.id]: true,
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
let newValue = await translateValue(entry?.fields[localizedField.id]?.[sdk.locales.default], targetLanguage);
|
|
138
|
+
|
|
139
|
+
setLoading({
|
|
140
|
+
...loading,
|
|
141
|
+
[targetLanguage + localizedField.id]: false,
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
if (!newValue) {
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (localizedField.id === "slug" && newValue) {
|
|
149
|
+
newValue = getSlug(newValue);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
newEntry.fields[localizedField.id][targetLanguage] = newValue;
|
|
153
|
+
}
|
|
154
|
+
setLoading({
|
|
155
|
+
...loading,
|
|
156
|
+
[targetLanguage]: false,
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
return newEntry;
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
const isEmptyValue = (value) => {
|
|
163
|
+
if (!value) {
|
|
164
|
+
return true;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// editor value
|
|
168
|
+
if (Array.isArray(value)) {
|
|
169
|
+
if (value.length === 1 && value[0].type === "paragraph" && value[0].children.length === 1 && value[0].children[0].text === "") {
|
|
170
|
+
return true;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return false;
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
const isComplete = (locale) => {
|
|
178
|
+
for (let i = 0; i < localizedFields.length; i++) {
|
|
179
|
+
const localizedField = localizedFields[i];
|
|
180
|
+
const value = entry?.fields[localizedField.id]?.[locale];
|
|
181
|
+
|
|
182
|
+
if (isEmptyValue(value)) {
|
|
183
|
+
return false;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return true;
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
const fullUsage = Object.keys(usage || {}).reduce((acc, key) => acc + usage[key], 0);
|
|
191
|
+
|
|
192
|
+
return <div className="flex flex-col gap-2 p-4">
|
|
193
|
+
<Table>
|
|
194
|
+
<Table.Head>
|
|
195
|
+
<Table.Row>
|
|
196
|
+
<Table.Cell style={{width: 250}}>Feld</Table.Cell>
|
|
197
|
+
<Table.Cell style={{width: 100}}>Zeichen</Table.Cell>
|
|
198
|
+
{sdk.locales.available.map(locale => {
|
|
199
|
+
const languageComplete = isComplete(locale);
|
|
200
|
+
const isTranslateFromLocale = locale === translateFromLocale;
|
|
201
|
+
|
|
202
|
+
if (isTranslateFromLocale) {
|
|
203
|
+
return <Table.Cell key={locale}/>;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if (loading[locale]) {
|
|
207
|
+
return <Table.Cell key={locale}><Button size="small" variant="transparent">Wird übersetzt</Button></Table.Cell>;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const buttonLabel = languageComplete ? "Erneut Übersetzen" : "Vervollständigen";
|
|
211
|
+
|
|
212
|
+
return <Table.Cell key={locale}>
|
|
213
|
+
<Button
|
|
214
|
+
size="small"
|
|
215
|
+
className="group"
|
|
216
|
+
variant={languageComplete ? "secondary" : "primary"}
|
|
217
|
+
title={buttonLabel}
|
|
218
|
+
onClick={e => onTranslateClick(locale, languageComplete)}>
|
|
219
|
+
{!languageComplete && <span className="text-orange-500">Fehlende Übersetzen</span>}
|
|
220
|
+
{languageComplete && <span className="text-green-500">Vollständig Übersetzt</span>}
|
|
221
|
+
</Button>
|
|
222
|
+
</Table.Cell>;
|
|
223
|
+
})}
|
|
224
|
+
</Table.Row>
|
|
225
|
+
</Table.Head>
|
|
226
|
+
<Table.Body>
|
|
227
|
+
<Table.Row>
|
|
228
|
+
<Table.Cell/>
|
|
229
|
+
<Table.Cell>∑ {fullUsage}</Table.Cell>
|
|
230
|
+
{sdk.locales.available.map(locale => {
|
|
231
|
+
const name = sdk.locales.names[locale];
|
|
232
|
+
|
|
233
|
+
return <Table.Cell key={locale}>
|
|
234
|
+
{name}
|
|
235
|
+
</Table.Cell>;
|
|
236
|
+
})}
|
|
237
|
+
</Table.Row>
|
|
238
|
+
{localizedFields.map(field => {
|
|
239
|
+
return <Table.Row key={field.id}>
|
|
240
|
+
<Table.Cell>
|
|
241
|
+
<Button
|
|
242
|
+
className="max-w-full truncate"
|
|
243
|
+
style={{maxWidth: 250}}
|
|
244
|
+
size="small"
|
|
245
|
+
variant={"transparent"}
|
|
246
|
+
title={"Alle Übersetzungen Aktualisieren"}
|
|
247
|
+
onClick={e => {
|
|
248
|
+
onTranslateClick(sdk.locales.available, false, [field.id]);
|
|
249
|
+
}}>
|
|
250
|
+
{field.name}
|
|
251
|
+
</Button>
|
|
252
|
+
</Table.Cell>
|
|
253
|
+
<Table.Cell>
|
|
254
|
+
{usage?.[field.id]}
|
|
255
|
+
</Table.Cell>
|
|
256
|
+
{sdk.locales.available.map(locale => {
|
|
257
|
+
let preview = entry?.fields[field.id]?.[locale];
|
|
258
|
+
|
|
259
|
+
if (!preview) {
|
|
260
|
+
preview = "";
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
if (typeof preview === "object") {
|
|
264
|
+
preview = "Keine Vorschau Verfügbar";
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (locale === translateFromLocale) {
|
|
268
|
+
return <Table.Cell key={locale}>
|
|
269
|
+
<div
|
|
270
|
+
className="max-w-full truncate"
|
|
271
|
+
style={{maxWidth: 250}}>
|
|
272
|
+
{preview}
|
|
273
|
+
</div>
|
|
274
|
+
</Table.Cell>;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
if (loading[locale + field.id]) {
|
|
278
|
+
return <Table.Cell key={locale}><Button size="small" variant="transparent">Wird übersetzt</Button></Table.Cell>;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
return <Table.Cell key={locale}>
|
|
282
|
+
<Button
|
|
283
|
+
className="max-w-full truncate"
|
|
284
|
+
style={{maxWidth: 250}}
|
|
285
|
+
size="small"
|
|
286
|
+
variant={"transparent"}
|
|
287
|
+
title={"Übersetzung Aktualisieren"}
|
|
288
|
+
onClick={e => onTranslateClick(locale, false, [field.id])}>
|
|
289
|
+
{preview}
|
|
290
|
+
</Button>
|
|
291
|
+
</Table.Cell>;
|
|
292
|
+
})}
|
|
293
|
+
</Table.Row>;
|
|
294
|
+
})}
|
|
295
|
+
</Table.Body>
|
|
296
|
+
</Table>
|
|
297
|
+
</div>;
|
|
298
|
+
};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import {Button} from "@contentful/f36-components";
|
|
2
|
+
import React, {
|
|
3
|
+
useEffect, useState,
|
|
4
|
+
} from "react";
|
|
5
|
+
|
|
6
|
+
export const Sidebar = ({
|
|
7
|
+
sdk,
|
|
8
|
+
activeConfig,
|
|
9
|
+
}) => {
|
|
10
|
+
return <div className="flex w-full flex-col gap-2">
|
|
11
|
+
<div className="w-full">
|
|
12
|
+
<OpenProdPageLink sdk={sdk} activeConfig={activeConfig}/>
|
|
13
|
+
</div>
|
|
14
|
+
<div className="w-full">
|
|
15
|
+
<SidebarTranslations sdk={sdk}/>
|
|
16
|
+
</div>
|
|
17
|
+
</div>;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const OpenProdPageLink = ({
|
|
21
|
+
activeConfig,
|
|
22
|
+
sdk,
|
|
23
|
+
}) => {
|
|
24
|
+
const [slug, setSlug] = useState(sdk?.entry?.fields?.slug?.getValue());
|
|
25
|
+
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
if (sdk?.entry?.fields?.slug) {
|
|
28
|
+
return sdk.entry.fields.slug.onValueChanged(setSlug);
|
|
29
|
+
}
|
|
30
|
+
}, [sdk]);
|
|
31
|
+
|
|
32
|
+
const disabled = !activeConfig?.domain || !slug;
|
|
33
|
+
const link = `${activeConfig.domain}/${slug}`;
|
|
34
|
+
|
|
35
|
+
const onClick = () => {
|
|
36
|
+
if (disabled) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
window.open(link, "_blank");
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
return <Button isDisabled={disabled} isFullWidth={true} variant="secondary" onClick={onClick}>Live-Seite öffnen</Button>;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const SidebarTranslations = ({sdk}) => {
|
|
46
|
+
const enabled = sdk?.parameters?.installation?.usersWithTranslation?.includes(sdk?.user?.email) && sdk?.parameters?.installation?.translationService;
|
|
47
|
+
|
|
48
|
+
if (!enabled) {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const onClick = () => {
|
|
53
|
+
sdk.dialogs.openCurrentApp({
|
|
54
|
+
title: 'Übersetzen',
|
|
55
|
+
width: 'fullWidth',
|
|
56
|
+
position: "center",
|
|
57
|
+
allowHeightOverflow: true,
|
|
58
|
+
shouldCloseOnOverlayClick: false,
|
|
59
|
+
shouldCloseOnEscapePress: false,
|
|
60
|
+
minHeight: 500,
|
|
61
|
+
parameters: {
|
|
62
|
+
type: "translation",
|
|
63
|
+
entryId: sdk.entry.getSys().id,
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
return <>
|
|
69
|
+
<Button isFullWidth={true} onClick={onClick}>Übersetzen</Button>
|
|
70
|
+
</>;
|
|
71
|
+
};
|
package/src/index.js
CHANGED
|
@@ -14,6 +14,7 @@ import EntryEditor from './components/EntryEditor';
|
|
|
14
14
|
import Config from './components/ConfigScreen';
|
|
15
15
|
import Field from './components/Field';
|
|
16
16
|
import Dialog from "./components/Dialog";
|
|
17
|
+
import {Sidebar} from "./components/Sidebar";
|
|
17
18
|
|
|
18
19
|
export const BaseContentfulApp = (props) => {
|
|
19
20
|
init(async (sdk) => {
|
|
@@ -69,6 +70,10 @@ export const BaseContentfulApp = (props) => {
|
|
|
69
70
|
location: locations.LOCATION_DIALOG,
|
|
70
71
|
component: <Dialog sdk={sdk} elementPropsMap={elementPropsMap} activeConfig={config}/>,
|
|
71
72
|
},
|
|
73
|
+
{
|
|
74
|
+
location: locations.LOCATION_ENTRY_SIDEBAR,
|
|
75
|
+
component: <Sidebar sdk={sdk} activeConfig={config}/>,
|
|
76
|
+
},
|
|
72
77
|
];
|
|
73
78
|
|
|
74
79
|
ComponentLocationSettings.forEach((componentLocationSetting) => {
|