@vonaffenfels/contentful-slate-editor 1.1.28 → 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/contentful-slate-editor",
3
- "version": "1.1.28",
3
+ "version": "1.1.29",
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.28",
96
+ "@vonaffenfels/slate-editor": "^1.1.29",
97
97
  "webpack": "5.88.2"
98
98
  },
99
- "gitHead": "a600552bfcbd6e605f8ababe0f4ef6f3222ac58a",
99
+ "gitHead": "6f766a57107e33b358372874a69e2199dc14d8f6",
100
100
  "publishConfig": {
101
101
  "access": "public"
102
102
  }
@@ -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,119 @@
1
+ import {
2
+ useEffect, useState,
3
+ } from "react";
4
+ import {
5
+ Button, Table,
6
+ } from "@contentful/f36-components";
7
+
8
+ export const DialogTranslation = ({
9
+ sdk,
10
+ activeConfig,
11
+ }) => {
12
+ const {entryId} = sdk.parameters.invocation;
13
+ const [entry, setEntry] = useState(null);
14
+ const [loading, setLoading] = useState({});
15
+ const [contentType, setContentType] = useState(null);
16
+
17
+ useEffect(() => {
18
+ sdk.space.getEntry(entryId).then(entry => {
19
+ sdk.space.getContentType(entry.sys.contentType.sys.id).then(contentType => {
20
+ setEntry(entry);
21
+ setContentType(contentType);
22
+ });
23
+ });
24
+ }, []);
25
+
26
+ if (!contentType) {
27
+ return null;
28
+ }
29
+
30
+ const translateValue = async (content, targetLanguage, translateFromLocale = "de") => {
31
+ if (!sdk?.parameters?.installation?.translationService) {
32
+ console.error(`no translation service configured!`);
33
+ return;
34
+ }
35
+
36
+ try {
37
+ const response = await fetch(sdk?.parameters?.installation?.translationService, {
38
+ method: "POST",
39
+ body: JSON.stringify({
40
+ editorContent: typeof content === "object" ? content : undefined,
41
+ text: typeof content === "string" ? content : undefined,
42
+ targetLanguage,
43
+ sourceLanguage: translateFromLocale,
44
+ }),
45
+ });
46
+
47
+ const translatedValue = await response.json();
48
+ return translatedValue?.translation || "";
49
+ } catch (e) {
50
+ console.error(e);
51
+ }
52
+ };
53
+
54
+ const localizedFields = contentType.fields.filter(field => field.localized);
55
+ const onTranslateClick = async (targetLanguage) => {
56
+ setLoading({[targetLanguage]: true});
57
+ const newEntry = {...entry};
58
+ for (let i = 0; i < localizedFields.length; i++) {
59
+ const localizedField = localizedFields[i];
60
+ const newValue = await translateValue(entry?.fields[localizedField.id]?.[sdk.locales.default], targetLanguage);
61
+
62
+ newEntry.fields[localizedField.id][targetLanguage] = newValue;
63
+ }
64
+
65
+ await sdk.space.updateEntry(newEntry);
66
+ setEntry(await sdk.space.getEntry(entryId));
67
+ setLoading({});
68
+ };
69
+
70
+
71
+ return <div className="flex flex-col gap-2 p-4">
72
+ <Table>
73
+ <Table.Head>
74
+ <Table.Row>
75
+ <Table.Cell />
76
+ {sdk.locales.available.map(locale => {
77
+ return <Table.Cell key={locale}>
78
+ {locale !== "de" && <Button size="small" variant="primary" onClick={e => onTranslateClick(locale)}>Übersetzen</Button>}
79
+ </Table.Cell>;
80
+ })}
81
+ </Table.Row>
82
+ </Table.Head>
83
+ <Table.Body>
84
+ <Table.Row>
85
+ <Table.Cell />
86
+ {sdk.locales.available.map(locale => {
87
+ const name = sdk.locales.names[locale];
88
+
89
+ return <Table.Cell key={locale}>
90
+ {name}
91
+ </Table.Cell>;
92
+ })}
93
+ </Table.Row>
94
+ {localizedFields.map(field => {
95
+ return <Table.Row key={field.id}>
96
+ <Table.Cell>
97
+ {field.name}
98
+ </Table.Cell>
99
+ {sdk.locales.available.map(locale => {
100
+ let preview = entry?.fields[field.id]?.[locale];
101
+
102
+ if (!preview) {
103
+ preview = "";
104
+ }
105
+
106
+ if (typeof preview === "object") {
107
+ preview = "Keine Vorschau...";
108
+ }
109
+
110
+ return <Table.Cell key={locale}>
111
+ {preview}
112
+ </Table.Cell>;
113
+ })}
114
+ </Table.Row>;
115
+ })}
116
+ </Table.Body>
117
+ </Table>
118
+ </div>;
119
+ };
@@ -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) => {