@vonaffenfels/slate-editor 1.1.26 → 1.1.28
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/componentLoader.js +1 -1
- package/dist/BlockEditor.css +2 -2
- package/dist/BlockEditor.js +76 -8
- package/dist/index.css +2 -2
- package/dist/index.js +76 -8
- package/package.json +2 -2
- package/scss/toolbar.scss +0 -19
- package/src/BlockEditor.js +11 -5
- package/src/SidebarEditor/Fields/MultiSelect.js +14 -0
- package/src/SidebarEditor/Fields/RemoteSelect.js +33 -0
- package/src/SidebarEditor/Fields/Select.js +48 -0
- package/src/SidebarEditor/SidebarEditorField.js +13 -50
- package/src/Translation/TranslationToolbarButton.js +124 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vonaffenfels/slate-editor",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.28",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -72,7 +72,7 @@
|
|
|
72
72
|
"cssnano": "^5.0.1",
|
|
73
73
|
"escape-html": "^1.0.3"
|
|
74
74
|
},
|
|
75
|
-
"gitHead": "
|
|
75
|
+
"gitHead": "a600552bfcbd6e605f8ababe0f4ef6f3222ac58a",
|
|
76
76
|
"publishConfig": {
|
|
77
77
|
"access": "public"
|
|
78
78
|
}
|
package/scss/toolbar.scss
CHANGED
|
@@ -9,25 +9,6 @@
|
|
|
9
9
|
position: absolute;
|
|
10
10
|
z-index: 500;
|
|
11
11
|
|
|
12
|
-
input[type="text"],
|
|
13
|
-
input[type="number"],
|
|
14
|
-
input[type="color"],
|
|
15
|
-
input[type="datetime-local"],
|
|
16
|
-
input[type="search"],
|
|
17
|
-
select,
|
|
18
|
-
textarea {
|
|
19
|
-
display: block;
|
|
20
|
-
font-size: 0.875rem !important;
|
|
21
|
-
color: rgb(90, 101, 124) !important;
|
|
22
|
-
background-color: white !important;
|
|
23
|
-
border: 1px solid #cfd9e0 !important;
|
|
24
|
-
border-radius: 6px !important;
|
|
25
|
-
box-shadow: none !important;
|
|
26
|
-
filter: none !important;
|
|
27
|
-
width: 100% !important;
|
|
28
|
-
padding: 8px 0.75rem !important;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
12
|
&.toolbar-static {
|
|
32
13
|
display: block;
|
|
33
14
|
position: fixed;
|
package/src/BlockEditor.js
CHANGED
|
@@ -21,6 +21,7 @@ import {Spinner} from '@contentful/forma-36-react-components';
|
|
|
21
21
|
import {ObjectId} from "./ObjectId";
|
|
22
22
|
import {Resizable} from "./SidebarEditor/Resizable";
|
|
23
23
|
import {Loader} from "./Loader";
|
|
24
|
+
import {TranslationToolbarButton} from "./Translation/TranslationToolbarButton";
|
|
24
25
|
|
|
25
26
|
export default function BlockEditor({
|
|
26
27
|
onChange,
|
|
@@ -33,6 +34,8 @@ export default function BlockEditor({
|
|
|
33
34
|
onSaveClick,
|
|
34
35
|
isLoading,
|
|
35
36
|
activeConfig,
|
|
37
|
+
locale,
|
|
38
|
+
setLocale,
|
|
36
39
|
}) {
|
|
37
40
|
const scrollContainer = useRef(null);
|
|
38
41
|
const [selectedStorybookElement, setSelectedStorybookElement] = useState(null);
|
|
@@ -382,11 +385,14 @@ export default function BlockEditor({
|
|
|
382
385
|
editor={editor}
|
|
383
386
|
sdk={contentfulSdk}
|
|
384
387
|
lastSelection={lastSelection}
|
|
385
|
-
buttons={
|
|
386
|
-
<
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
388
|
+
buttons={<div className="flex gap-2">
|
|
389
|
+
<TranslationToolbarButton sdk={contentfulSdk} setEditorValue={onSlateChange} locale={locale} setLocale={setLocale}/>
|
|
390
|
+
{activeConfig?.domain && slug && (
|
|
391
|
+
<a href={`${activeConfig.domain}/${slug}`} target="_blank" rel="noreferrer">
|
|
392
|
+
<button className='button button--secondary'>Live-Seite öffnen</button>
|
|
393
|
+
</a>
|
|
394
|
+
)}
|
|
395
|
+
</div>}/>
|
|
390
396
|
</div>
|
|
391
397
|
<div className="relative h-full max-h-full overflow-scroll px-8 py-4" ref={scrollContainer}>
|
|
392
398
|
{isLoading && (
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import {renderFieldSelectOptions} from "./Select";
|
|
2
|
+
import {
|
|
3
|
+
useEffect, useState,
|
|
4
|
+
} from "react";
|
|
5
|
+
|
|
6
|
+
export const RemoteSelect = ({
|
|
7
|
+
value,
|
|
8
|
+
field,
|
|
9
|
+
onChange,
|
|
10
|
+
}) => {
|
|
11
|
+
const [options, setOptions] = useState([]);
|
|
12
|
+
|
|
13
|
+
const reloadOptions = async () => {
|
|
14
|
+
setOptions(await fetch(field.control.url).then(res => res.json()));
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
reloadOptions();
|
|
19
|
+
}, [field]);
|
|
20
|
+
|
|
21
|
+
return <select
|
|
22
|
+
value={value || ""}
|
|
23
|
+
onChange={onChange}>
|
|
24
|
+
<option value="">Auswählen</option>
|
|
25
|
+
{renderFieldSelectOptions({
|
|
26
|
+
...field,
|
|
27
|
+
control: {
|
|
28
|
+
...field.control,
|
|
29
|
+
options,
|
|
30
|
+
},
|
|
31
|
+
})}
|
|
32
|
+
</select>;
|
|
33
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
|
|
2
|
+
export const Select = ({
|
|
3
|
+
value,
|
|
4
|
+
field,
|
|
5
|
+
onChange,
|
|
6
|
+
}) => {
|
|
7
|
+
return <select
|
|
8
|
+
value={value || ""}
|
|
9
|
+
onChange={onChange}>
|
|
10
|
+
<option value="">Auswählen</option>
|
|
11
|
+
{renderFieldSelectOptions(field)}
|
|
12
|
+
</select>;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export const renderFieldSelectOptions = field => {
|
|
16
|
+
if (field.options) {
|
|
17
|
+
field.control.options = field.options;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (field?.control?.labels) {
|
|
21
|
+
field.control.options = field.control.labels;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (!field?.control?.options) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (Array.isArray(field.control.options)) {
|
|
29
|
+
return field.control.options.map(option => {
|
|
30
|
+
if (option && typeof option === "object") {
|
|
31
|
+
return <option key={`select-option-${option.value}`} value={option.value}>{option.label}</option>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return <option key={`select-option-${option}`} value={option}>{option}</option>;
|
|
35
|
+
});
|
|
36
|
+
} else {
|
|
37
|
+
return Object.keys(field.control.options).map(key => {
|
|
38
|
+
// Prevents false values being a string, e.g. "false"
|
|
39
|
+
if (!field.control.options[key]) {
|
|
40
|
+
field.control.options[key] = "";
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return (
|
|
44
|
+
<option key={`select-option-${key}`} value={field.control.options[key]}>{key}</option>
|
|
45
|
+
);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
};
|
|
@@ -5,6 +5,9 @@ import {
|
|
|
5
5
|
Asset, AssetList,
|
|
6
6
|
} from "./AssetList";
|
|
7
7
|
import {Switch} from "./Switch";
|
|
8
|
+
import {Select} from "./Fields/Select";
|
|
9
|
+
import {RemoteSelect} from "./Fields/RemoteSelect";
|
|
10
|
+
import {MultiSelect} from "./Fields/MultiSelect";
|
|
8
11
|
|
|
9
12
|
export const SidebarEditorField = ({
|
|
10
13
|
field,
|
|
@@ -37,37 +40,6 @@ export const SidebarEditorField = ({
|
|
|
37
40
|
onChange(fieldKey, swapArrayElements(value, index, newIndex));
|
|
38
41
|
};
|
|
39
42
|
|
|
40
|
-
const renderFieldSelectOptions = field => {
|
|
41
|
-
if (field.options) {
|
|
42
|
-
field.control.options = field.options;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
if (field?.control?.labels) {
|
|
46
|
-
field.control.options = field.control.labels;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
if (!field?.control?.options) {
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
if (Array.isArray(field.control.options)) {
|
|
54
|
-
return field.control.options.map(value => (
|
|
55
|
-
<option key={`select-option-${value}`} value={value}>{value}</option>
|
|
56
|
-
));
|
|
57
|
-
} else {
|
|
58
|
-
return Object.keys(field.control.options).map(key => {
|
|
59
|
-
// Prevents false values being a string, e.g. "false"
|
|
60
|
-
if (!field.control.options[key]) {
|
|
61
|
-
field.control.options[key] = "";
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
return (
|
|
65
|
-
<option key={`select-option-${key}`} value={field.control.options[key]}>{key}</option>
|
|
66
|
-
);
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
|
|
71
43
|
const getInputByField = (field, fieldKey) => {
|
|
72
44
|
if (!field) {
|
|
73
45
|
return null;
|
|
@@ -114,14 +86,9 @@ export const SidebarEditorField = ({
|
|
|
114
86
|
</div>
|
|
115
87
|
);
|
|
116
88
|
case "select":
|
|
117
|
-
return (
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
onChange={e => onChange(fieldKey, e.target.value)}>
|
|
121
|
-
<option value="">Auswählen</option>
|
|
122
|
-
{renderFieldSelectOptions(field)}
|
|
123
|
-
</select>
|
|
124
|
-
);
|
|
89
|
+
return <Select onChange={e => onChange(fieldKey, e.target.value)} field={field} value={value} />;
|
|
90
|
+
case "remote-select":
|
|
91
|
+
return <RemoteSelect onChange={e => onChange(fieldKey, e.target.value)} field={field} value={value} />;
|
|
125
92
|
case "data-stream":
|
|
126
93
|
return (
|
|
127
94
|
<select
|
|
@@ -268,18 +235,14 @@ export const SidebarEditorField = ({
|
|
|
268
235
|
onChange={e => onChange(fieldKey, e.target.value)}/>
|
|
269
236
|
);
|
|
270
237
|
case "multi-select":
|
|
271
|
-
return
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
let v = Array.from(e.target.selectedOptions, option => option.value);
|
|
238
|
+
return <MultiSelect
|
|
239
|
+
value={value}
|
|
240
|
+
field={field}
|
|
241
|
+
onChange={e => {
|
|
242
|
+
let v = Array.from(e.target.selectedOptions, option => option.value);
|
|
277
243
|
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
{renderFieldSelectOptions(field)}
|
|
281
|
-
</select>
|
|
282
|
-
);
|
|
244
|
+
onChange(fieldKey, v);
|
|
245
|
+
}} />;
|
|
283
246
|
case "contentful-content":
|
|
284
247
|
return (
|
|
285
248
|
<>
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import React, {
|
|
2
|
+
useEffect, useState,
|
|
3
|
+
} from "react";
|
|
4
|
+
import {Modal} from '@contentful/f36-components';
|
|
5
|
+
import {
|
|
6
|
+
Select, Button, Paragraph,
|
|
7
|
+
} from '@contentful/f36-components';
|
|
8
|
+
|
|
9
|
+
export const TranslationToolbarButton = ({
|
|
10
|
+
sdk,
|
|
11
|
+
setEditorValue,
|
|
12
|
+
locale,
|
|
13
|
+
setLocale,
|
|
14
|
+
translateFromLocale = "de",
|
|
15
|
+
}) => {
|
|
16
|
+
const [enabled, setEnabled] = useState(false);
|
|
17
|
+
const [loading, setLoading] = useState(false);
|
|
18
|
+
const [error, setError] = useState(null);
|
|
19
|
+
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
if (sdk) {
|
|
22
|
+
if (sdk?.parameters?.installation?.usersWithTranslation?.includes(sdk?.user?.email) && sdk?.parameters?.installation?.translationService) {
|
|
23
|
+
setEnabled(true);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}, [sdk]);
|
|
27
|
+
|
|
28
|
+
if (sdk.locales.available.length < 2 || !enabled) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const handleOnChange = (e) => {
|
|
33
|
+
setLocale(e.target.value);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const translateEditorValue = async (editorContent, targetLanguage) => {
|
|
37
|
+
if (!sdk?.parameters?.installation?.translationService) {
|
|
38
|
+
console.error(`no translation service configured!`);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
setLoading(true);
|
|
44
|
+
const response = await fetch(sdk?.parameters?.installation?.translationService, {
|
|
45
|
+
method: "POST",
|
|
46
|
+
body: JSON.stringify({
|
|
47
|
+
editorContent,
|
|
48
|
+
targetLanguage,
|
|
49
|
+
sourceLanguage: translateFromLocale,
|
|
50
|
+
}),
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const translatedValue = await response.json();
|
|
54
|
+
setLoading(false);
|
|
55
|
+
|
|
56
|
+
if (translatedValue?.translation) {
|
|
57
|
+
setEditorValue(translatedValue.translation);
|
|
58
|
+
}
|
|
59
|
+
} catch (e) {
|
|
60
|
+
console.error(e);
|
|
61
|
+
setLoading(false);
|
|
62
|
+
setError({
|
|
63
|
+
title: "Error",
|
|
64
|
+
message: "Übersetzunggservice nicht erreichbar",
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const onAutoTranslateClick = () => {
|
|
70
|
+
const translateFromValue = sdk.entry.fields?.[sdk.parameters.instance.contentField]?.getForLocale(translateFromLocale)?.getValue();
|
|
71
|
+
const currentValue = sdk.entry.fields?.[sdk.parameters.instance.contentField]?.getForLocale(locale)?.getValue();
|
|
72
|
+
|
|
73
|
+
if (!translateFromValue) {
|
|
74
|
+
console.error(`No value for field ${sdk.parameters.instance.contentField} in locale ${translateFromLocale}`);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (currentValue) {
|
|
79
|
+
// ask for overwrite?
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
translateEditorValue(translateFromValue, locale);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
return <div className="flex gap-2">
|
|
86
|
+
{!!error && <ErrorOverlay title={error.title} message={error.message} />}
|
|
87
|
+
{locale !== sdk.locales.default &&
|
|
88
|
+
<Button
|
|
89
|
+
variant="secondary"
|
|
90
|
+
onClick={onAutoTranslateClick}
|
|
91
|
+
size="small">{loading ? "Wird übersetzt..." : "Automatisch Übersetzen"}</Button>}
|
|
92
|
+
<Select
|
|
93
|
+
value={locale}
|
|
94
|
+
onChange={handleOnChange}
|
|
95
|
+
>
|
|
96
|
+
{Object.keys(sdk?.locales?.names || {}).map((locale) => {
|
|
97
|
+
return <Select.Option key={locale} value={locale}>{sdk.locales.names[locale]}</Select.Option>;
|
|
98
|
+
})}
|
|
99
|
+
</Select>
|
|
100
|
+
</div>;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const ErrorOverlay = ({
|
|
104
|
+
title,
|
|
105
|
+
subtitle,
|
|
106
|
+
message,
|
|
107
|
+
}) => {
|
|
108
|
+
const [isShown, setShown] = useState(true);
|
|
109
|
+
|
|
110
|
+
return <Modal onClose={() => setShown(false)} isShown={isShown}>
|
|
111
|
+
{() => (
|
|
112
|
+
<>
|
|
113
|
+
<Modal.Header
|
|
114
|
+
title={title}
|
|
115
|
+
subtitle={subtitle}
|
|
116
|
+
onClose={() => setShown(false)}
|
|
117
|
+
/>
|
|
118
|
+
<Modal.Content>
|
|
119
|
+
<Paragraph>{message}</Paragraph>
|
|
120
|
+
</Modal.Content>
|
|
121
|
+
</>
|
|
122
|
+
)}
|
|
123
|
+
</Modal>;
|
|
124
|
+
};
|