@vonaffenfels/slate-editor 1.1.27 → 1.1.29

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vonaffenfels/slate-editor",
3
- "version": "1.1.27",
3
+ "version": "1.1.29",
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": "78986d6bb0100171800ced4a7c77c3d08de9e002",
75
+ "gitHead": "6f766a57107e33b358372874a69e2199dc14d8f6",
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;
@@ -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={activeConfig?.domain && slug && (
386
- <a href={`${activeConfig.domain}/${slug}`} target="_blank" rel="noreferrer">
387
- <button className='button button--secondary'>Live-Seite öffnen</button>
388
- </a>
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,116 @@
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 [loading, setLoading] = useState(false);
17
+ const [error, setError] = useState(null);
18
+ const enabled = sdk?.parameters?.installation?.usersWithTranslation?.includes(sdk?.user?.email) && sdk?.parameters?.installation?.translationService;
19
+
20
+ if (sdk.locales.available.length < 2 || !enabled) {
21
+ return null;
22
+ }
23
+
24
+ const handleOnChange = (e) => {
25
+ setLocale(e.target.value);
26
+ };
27
+
28
+ const translateEditorValue = async (editorContent, targetLanguage) => {
29
+ if (!sdk?.parameters?.installation?.translationService) {
30
+ console.error(`no translation service configured!`);
31
+ return;
32
+ }
33
+
34
+ try {
35
+ setLoading(true);
36
+ const response = await fetch(sdk?.parameters?.installation?.translationService, {
37
+ method: "POST",
38
+ body: JSON.stringify({
39
+ editorContent,
40
+ targetLanguage,
41
+ sourceLanguage: translateFromLocale,
42
+ }),
43
+ });
44
+
45
+ const translatedValue = await response.json();
46
+ setLoading(false);
47
+
48
+ if (translatedValue?.translation) {
49
+ setEditorValue(translatedValue.translation);
50
+ }
51
+ } catch (e) {
52
+ console.error(e);
53
+ setLoading(false);
54
+ setError({
55
+ title: "Error",
56
+ message: "Übersetzunggservice nicht erreichbar",
57
+ });
58
+ }
59
+ };
60
+
61
+ const onAutoTranslateClick = () => {
62
+ const translateFromValue = sdk.entry.fields?.[sdk.parameters.instance.contentField]?.getForLocale(translateFromLocale)?.getValue();
63
+ const currentValue = sdk.entry.fields?.[sdk.parameters.instance.contentField]?.getForLocale(locale)?.getValue();
64
+
65
+ if (!translateFromValue) {
66
+ console.error(`No value for field ${sdk.parameters.instance.contentField} in locale ${translateFromLocale}`);
67
+ return;
68
+ }
69
+
70
+ if (currentValue) {
71
+ // ask for overwrite?
72
+ }
73
+
74
+ translateEditorValue(translateFromValue, locale);
75
+ };
76
+
77
+ return <div className="flex gap-2">
78
+ {!!error && <ErrorOverlay title={error.title} message={error.message} />}
79
+ {locale !== sdk.locales.default &&
80
+ <Button
81
+ variant="secondary"
82
+ onClick={onAutoTranslateClick}
83
+ size="small">{loading ? "Wird übersetzt..." : "Automatisch Übersetzen"}</Button>}
84
+ <Select
85
+ value={locale}
86
+ onChange={handleOnChange}
87
+ >
88
+ {Object.keys(sdk?.locales?.names || {}).map((locale) => {
89
+ return <Select.Option key={locale} value={locale}>{sdk.locales.names[locale]}</Select.Option>;
90
+ })}
91
+ </Select>
92
+ </div>;
93
+ };
94
+
95
+ const ErrorOverlay = ({
96
+ title,
97
+ subtitle,
98
+ message,
99
+ }) => {
100
+ const [isShown, setShown] = useState(true);
101
+
102
+ return <Modal onClose={() => setShown(false)} isShown={isShown}>
103
+ {() => (
104
+ <>
105
+ <Modal.Header
106
+ title={title}
107
+ subtitle={subtitle}
108
+ onClose={() => setShown(false)}
109
+ />
110
+ <Modal.Content>
111
+ <Paragraph>{message}</Paragraph>
112
+ </Modal.Content>
113
+ </>
114
+ )}
115
+ </Modal>;
116
+ };