@vonaffenfels/slate-editor 1.0.54 → 1.0.56

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.0.54",
3
+ "version": "1.0.56",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -71,7 +71,7 @@
71
71
  "cssnano": "^5.0.1",
72
72
  "escape-html": "^1.0.3"
73
73
  },
74
- "gitHead": "a76b8b1b997741f81c733ee80e54cdba34c7ff9a",
74
+ "gitHead": "46c4cdded137ff7fdd53011ee6b999972131dd9c",
75
75
  "publishConfig": {
76
76
  "access": "public"
77
77
  }
@@ -6,13 +6,14 @@
6
6
  width: 100%;
7
7
  height: 100%;
8
8
  position: absolute;
9
- z-index: 10;
9
+ z-index: 20;
10
10
  background-color: rgba(255, 255, 255, 0.7);
11
11
  color: black;
12
12
  font-size: 1em;
13
13
  align-items: center;
14
14
  justify-content: center;
15
15
  content-visibility: auto;
16
+ outline: 3px dashed #036fe3;
16
17
  }
17
18
 
18
19
  &.storybook-inline {
@@ -1,5 +1,6 @@
1
1
  import {IconButton} from "../SidebarEditor";
2
2
  import {swapArrayElements} from "../helper/array";
3
+ import {reduceContentfulResponse} from "../util/reduceContentfulResponse";
3
4
  import {
4
5
  Asset, AssetList,
5
6
  } from "./AssetList";
@@ -36,6 +37,10 @@ export const SidebarEditorField = ({
36
37
  };
37
38
 
38
39
  const renderFieldSelectOptions = field => {
40
+ if (field.options) {
41
+ field.control.options = field.options;
42
+ }
43
+
39
44
  if (field?.control?.labels) {
40
45
  field.control.options = field.control.labels;
41
46
  }
@@ -50,7 +55,7 @@ export const SidebarEditorField = ({
50
55
  ));
51
56
  } else {
52
57
  return Object.keys(field.control.options).map(key => (
53
- <option key={`select-option-${key}`} value={field.control.options[key]}>{key}</option>
58
+ <option key={`select-option-${key}`} value={field.control.options[key]}>{field.control.options[key]}</option>
54
59
  ));
55
60
  }
56
61
  };
@@ -235,9 +240,7 @@ export const SidebarEditorField = ({
235
240
 
236
241
  onChange(fieldKey, v);
237
242
  }}>
238
- {Object.keys(field.control.options).map(key => (
239
- <option key={`select-option-${key}`} value={field.control.options[key]}>{key}</option>
240
- ))}
243
+ {renderFieldSelectOptions(field)}
241
244
  </select>
242
245
  );
243
246
  case "contentful-content":
@@ -248,7 +251,7 @@ export const SidebarEditorField = ({
248
251
  className="button"
249
252
  onClick={() => {
250
253
  sdk.dialogs.selectSingleEntry({contentTypes: field.control.contentTypes}).then(content => {
251
- onChange(fieldKey, content);
254
+ onChange(fieldKey, reduceContentfulResponse(content, field.control.paths));
252
255
  });
253
256
  }}
254
257
  >Auswählen</button>
@@ -267,7 +270,7 @@ export const SidebarEditorField = ({
267
270
  onChange={v => onChange(fieldKey, v)}
268
271
  onAddClick={() => {
269
272
  sdk.dialogs.selectMultipleEntries({contentTypes: field.control.contentTypes}).then(contents => {
270
- onChange(fieldKey, [...value, ...contents]);
273
+ onChange(fieldKey, reduceContentfulResponse([...value, ...contents], field.control.paths));
271
274
  });
272
275
  }} />
273
276
  </div>
@@ -277,7 +280,7 @@ export const SidebarEditorField = ({
277
280
  className="button"
278
281
  onClick={() => {
279
282
  sdk.dialogs.selectMultipleEntries({contentTypes: field.control.contentTypes}).then(contents => {
280
- onChange(fieldKey, contents);
283
+ onChange(fieldKey, reduceContentfulResponse(contents, field.control.paths));
281
284
  });
282
285
  }}
283
286
  >Auswählen</button>
@@ -0,0 +1,65 @@
1
+ export const reduceContentfulResponse = (item, paths) => {
2
+
3
+ if (Array.isArray(item)) {
4
+ return item.map(v => reduceContentfulResponse(v, paths));
5
+ }
6
+
7
+ if (!paths?.length || !item) {
8
+ return item;
9
+ }
10
+
11
+ const defaultPaths = [
12
+ "__typename",
13
+ "sys.id",
14
+ "sys.contentType.sys.id",
15
+ ];
16
+ const mergedPaths = [...defaultPaths, ...paths];
17
+
18
+ const result = {
19
+ sys: {
20
+ id: item?.sys?.id,
21
+ },
22
+ };
23
+
24
+ for (let i = 0; i < mergedPaths.length; i++) {
25
+ const path = mergedPaths[i];
26
+
27
+ setByPath(result, path, getByPath(item, path));
28
+ }
29
+
30
+ return result;
31
+ };
32
+
33
+ export const getByPath = (obj, path) => {
34
+ if (!obj || !path) {
35
+ return;
36
+ }
37
+
38
+ const pathParts = path.split(".");
39
+
40
+ if (pathParts.length === 1) {
41
+ return obj?.[pathParts[0]];
42
+ } else {
43
+ const currPart = pathParts.shift();
44
+ return getByPath(obj?.[currPart], pathParts.join("."));
45
+ }
46
+ };
47
+
48
+ export const setByPath = (obj, path, value) => {
49
+ const pathParts = path.split(".");
50
+ let objPointer = obj;
51
+
52
+ for (let i = 0; i < pathParts.length; i++) {
53
+ const pathPart = pathParts[i];
54
+
55
+ if (i === pathParts.length - 1) {
56
+ objPointer[pathPart] = value;
57
+ } else if (objPointer[pathPart] === undefined) {
58
+ objPointer[pathPart] = {};
59
+ }
60
+
61
+ objPointer = objPointer[pathPart];
62
+ }
63
+
64
+ return obj;
65
+ };