@vonaffenfels/slate-editor 1.2.29 → 1.2.39

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.2.29",
3
+ "version": "1.2.39",
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": "564cc567d33fd5e681c7503a86a41f54a9d5c502",
75
+ "gitHead": "46607d2d7b190f03ad2b2fa84d466d7133dea27b",
76
76
  "publishConfig": {
77
77
  "access": "public"
78
78
  }
@@ -1,182 +1,186 @@
1
- import {
2
- useEffect, useState,
3
- } from "react";
4
- import {IconButton} from "../SidebarEditor";
5
- import {swapArrayElements} from "../helper/array";
6
-
7
- // Hallo :)
8
- export const AssetList = ({
9
- assets,
10
- onChange,
11
- sdk,
12
- cloudinary = false,
13
- onAddClick,
14
- }) => {
15
- const renderAssets = assets.filter(Boolean).map((asset, index) => (
16
- <>
17
- <Asset
18
- key={asset.sys?.id}
19
- asset={asset}
20
- index={index}
21
- sdk={sdk}
22
- cloudinary={cloudinary}
23
- assetsLength={assets.length}
24
- onMoveClick={(direction) => handleMoveClick(direction, index)}
25
- onDeleteClick={() => handleDeleteClick(index)}
26
- onChange={handleChange}
27
- />
28
- <hr className="my-2" style={{borderColor: "#cfd9e0"}}/>
29
- {onAddClick && index === assets.length - 1 && <button onClick={onAddClick} className="button button--secondary">Hinzufügen</button>}
30
- </>
31
- ));
32
-
33
- const handleMoveClick = (direction, index) => {
34
- let newIndex = direction === "up" ? index - 1 : index + 1;
35
-
36
- if (direction === "up" && index === 0) {
37
- return;
38
- } else if (direction === "down" && index === assets.length - 1) {
39
- return;
40
- }
41
-
42
- onChange(swapArrayElements(assets, index, newIndex));
43
- };
44
-
45
- const handleDeleteClick = (index) => {
46
- let newAssets = assets;
47
-
48
- newAssets.splice(index, 1);
49
-
50
- onChange(newAssets);
51
- };
52
-
53
- const handleChange = (newAsset) => {
54
- let newAssets = assets.map(asset => {
55
- if (asset?.sys?.id === newAsset?.sys?.id) {
56
- return newAsset;
57
- }
58
-
59
- return asset;
60
- });
61
-
62
- onChange(newAssets);
63
- };
64
-
65
- return (
66
- <div>{renderAssets}</div>
67
- );
68
- };
69
-
70
- export const Asset = ({
71
- asset,
72
- index,
73
- assetsLength,
74
- onDeleteClick,
75
- onMoveClick,
76
- onChange,
77
- sdk,
78
- cloudinary = false,
79
- }) => {
80
- const [mediaUrl, setMediaUrl] = useState(null);
81
-
82
- let id = asset?.sys?.id;
83
- let title = asset?.fields?.title?.["en-US"] || asset?.fields?.title?.["de"] || asset?.fields?.title?.toString() || asset?.title?.toString();
84
- let mediaAssetId = asset?.fields?.media?.["en-US"]?.sys?.id || asset?.fields?.media?.["de"]?.sys?.id || asset?.fields?.media?.sys?.id;
85
-
86
- if (cloudinary) {
87
- title = asset.public_id || asset.title;
88
- }
89
-
90
- useEffect(() => {
91
- if (cloudinary) {
92
- setMediaUrl(asset.url || asset?.media?.url);
93
-
94
- return;
95
- }
96
-
97
- if (!mediaAssetId) {
98
- if (asset?.media?.url) {
99
- let params = new URLSearchParams({
100
- w: "512",
101
- q: "80",
102
- });
103
-
104
- setMediaUrl(`${asset?.media?.url}?${params}`);
105
- }
106
-
107
- return;
108
- }
109
-
110
- sdk.space.getAsset(mediaAssetId)
111
- .then(a => {
112
- let url = a?.fields?.file?.["en-US"]?.url || a?.fields?.file?.["de"]?.url;
113
-
114
- if (url) {
115
- let params = new URLSearchParams({
116
- w: "512",
117
- q: "80",
118
- });
119
-
120
- setMediaUrl(`${url}?${params}`);
121
- }
122
- });
123
- }, [mediaAssetId, sdk, asset, cloudinary]);
124
-
125
- if (!asset) {
126
- return null;
127
- }
128
-
129
- let assetType = "image";
130
-
131
- if (mediaUrl?.endsWith(".mp3")) {
132
- assetType = "audio";
133
- }
134
-
135
- if (mediaUrl?.endsWith(".mp4") || mediaUrl?.endsWith(".mov")) {
136
- assetType = "video";
137
- }
138
-
139
- const renderMedia = () => {
140
- switch (assetType) {
141
- case "audio":
142
- return <audio src={mediaUrl} className="mt-2 w-full" controls />;
143
- case "video":
144
- return <video src={mediaUrl} className="mt-2 w-full rounded-md" controls />;
145
- case "image":
146
- default:
147
- return <img src={mediaUrl} width="100%" className="mt-2 rounded-md" />;
148
- }
149
- };
150
-
151
- return (
152
- <div className="flex flex-col">
153
- <div className="flex">
154
- <div className="grow">
155
- <b className="mt-[3px] block" style={{wordBreak: "break-word"}}>{title}</b>
156
- </div>
157
- <div className="ml-2 shrink-0">
158
- {!!onMoveClick && (
159
- <div className="icon-button-group mr-1">
160
- <IconButton size="small" onClick={() => onMoveClick("up")} disabled={index === 0}>↑</IconButton>
161
- <IconButton size="small" onClick={() => onMoveClick("down")} disabled={index === assetsLength - 1}>↓</IconButton>
162
- </div>
163
- )}
164
- {!cloudinary && (
165
- <IconButton
166
- size="small"
167
- className="mr-1"
168
- onClick={() => {
169
- sdk.navigator.openEntry(id, {slideIn: {waitForClose: true}}).then(({entity}) => {
170
- if (onChange) {
171
- onChange(entity);
172
- }
173
- });
174
- }}>✎</IconButton>
175
- )}
176
- <IconButton size="small" onClick={onDeleteClick}>⨯</IconButton>
177
- </div>
178
- </div>
179
- {!!mediaUrl && renderMedia()}
180
- </div>
181
- );
1
+ import {
2
+ useEffect, useState,
3
+ } from "react";
4
+ import {IconButton} from "../SidebarEditor";
5
+ import {swapArrayElements} from "../helper/array";
6
+
7
+ // Hallo :)
8
+ export const AssetList = ({
9
+ assets,
10
+ onChange,
11
+ sdk,
12
+ cloudinary = false,
13
+ onAddClick,
14
+ displayField,
15
+ }) => {
16
+ const renderAssets = assets.filter(Boolean).map((asset, index) => (
17
+ <>
18
+ <Asset
19
+ key={asset.sys?.id}
20
+ asset={asset}
21
+ index={index}
22
+ sdk={sdk}
23
+ cloudinary={cloudinary}
24
+ assetsLength={assets.length}
25
+ onMoveClick={(direction) => handleMoveClick(direction, index)}
26
+ onDeleteClick={() => handleDeleteClick(index)}
27
+ onChange={handleChange}
28
+ displayField={displayField}
29
+ />
30
+ <hr className="my-2" style={{borderColor: "#cfd9e0"}}/>
31
+ {onAddClick && index === assets.length - 1 && <button onClick={onAddClick} className="button button--secondary">Hinzufügen</button>}
32
+ </>
33
+ ));
34
+
35
+ const handleMoveClick = (direction, index) => {
36
+ let newIndex = direction === "up" ? index - 1 : index + 1;
37
+
38
+ if (direction === "up" && index === 0) {
39
+ return;
40
+ } else if (direction === "down" && index === assets.length - 1) {
41
+ return;
42
+ }
43
+
44
+ onChange(swapArrayElements(assets, index, newIndex));
45
+ };
46
+
47
+ const handleDeleteClick = (index) => {
48
+ let newAssets = assets;
49
+
50
+ newAssets.splice(index, 1);
51
+
52
+ onChange(newAssets);
53
+ };
54
+
55
+ const handleChange = (newAsset) => {
56
+ let newAssets = assets.map(asset => {
57
+ if (asset?.sys?.id === newAsset?.sys?.id) {
58
+ return newAsset;
59
+ }
60
+
61
+ return asset;
62
+ });
63
+
64
+ onChange(newAssets);
65
+ };
66
+
67
+ return (
68
+ <div>{renderAssets}</div>
69
+ );
70
+ };
71
+
72
+ export const Asset = ({
73
+ asset,
74
+ index,
75
+ assetsLength,
76
+ onDeleteClick,
77
+ onMoveClick,
78
+ onChange,
79
+ sdk,
80
+ cloudinary = false,
81
+ displayField = "title",
82
+ }) => {
83
+ const [mediaUrl, setMediaUrl] = useState(null);
84
+
85
+ let id = asset?.sys?.id;
86
+ let fieldValue = asset?.fields?.[displayField];
87
+ let title = fieldValue?.["en-US"] || fieldValue?.["de"] || (typeof fieldValue === "string" ? fieldValue : null) || asset?.[displayField]?.toString() || asset?.fields?.title?.["en-US"] || asset?.fields?.title?.["de"];
88
+ let mediaAssetId = asset?.fields?.media?.["en-US"]?.sys?.id || asset?.fields?.media?.["de"]?.sys?.id || asset?.fields?.media?.sys?.id;
89
+
90
+ if (cloudinary) {
91
+ title = asset.public_id || asset.title;
92
+ }
93
+
94
+ useEffect(() => {
95
+ if (cloudinary) {
96
+ setMediaUrl(asset.url || asset?.media?.url);
97
+
98
+ return;
99
+ }
100
+
101
+ if (!mediaAssetId) {
102
+ if (asset?.media?.url) {
103
+ let params = new URLSearchParams({
104
+ w: "512",
105
+ q: "80",
106
+ });
107
+
108
+ setMediaUrl(`${asset?.media?.url}?${params}`);
109
+ }
110
+
111
+ return;
112
+ }
113
+
114
+ sdk.space.getAsset(mediaAssetId)
115
+ .then(a => {
116
+ let url = a?.fields?.file?.["en-US"]?.url || a?.fields?.file?.["de"]?.url;
117
+
118
+ if (url) {
119
+ let params = new URLSearchParams({
120
+ w: "512",
121
+ q: "80",
122
+ });
123
+
124
+ setMediaUrl(`${url}?${params}`);
125
+ }
126
+ });
127
+ }, [mediaAssetId, sdk, asset, cloudinary]);
128
+
129
+ if (!asset) {
130
+ return null;
131
+ }
132
+
133
+ let assetType = "image";
134
+
135
+ if (mediaUrl?.endsWith(".mp3")) {
136
+ assetType = "audio";
137
+ }
138
+
139
+ if (mediaUrl?.endsWith(".mp4") || mediaUrl?.endsWith(".mov")) {
140
+ assetType = "video";
141
+ }
142
+
143
+ const renderMedia = () => {
144
+ switch (assetType) {
145
+ case "audio":
146
+ return <audio src={mediaUrl} className="mt-2 w-full" controls />;
147
+ case "video":
148
+ return <video src={mediaUrl} className="mt-2 w-full rounded-md" controls />;
149
+ case "image":
150
+ default:
151
+ return <img src={mediaUrl} width="100%" className="mt-2 rounded-md" />;
152
+ }
153
+ };
154
+
155
+ return (
156
+ <div className="flex flex-col">
157
+ <div className="flex">
158
+ <div className="grow">
159
+ <b className="mt-[3px] block" style={{wordBreak: "break-word"}}>{title}</b>
160
+ </div>
161
+ <div className="ml-2 shrink-0">
162
+ {!!onMoveClick && (
163
+ <div className="icon-button-group mr-1">
164
+ <IconButton size="small" onClick={() => onMoveClick("up")} disabled={index === 0}>↑</IconButton>
165
+ <IconButton size="small" onClick={() => onMoveClick("down")} disabled={index === assetsLength - 1}>↓</IconButton>
166
+ </div>
167
+ )}
168
+ {!cloudinary && (
169
+ <IconButton
170
+ size="small"
171
+ className="mr-1"
172
+ onClick={() => {
173
+ sdk.navigator.openEntry(id, {slideIn: {waitForClose: true}}).then(({entity}) => {
174
+ if (onChange) {
175
+ onChange(entity);
176
+ }
177
+ });
178
+ }}>✎</IconButton>
179
+ )}
180
+ <IconButton size="small" onClick={onDeleteClick}>⨯</IconButton>
181
+ </div>
182
+ </div>
183
+ {!!mediaUrl && renderMedia()}
184
+ </div>
185
+ );
182
186
  };
@@ -1,63 +1,64 @@
1
- import {
2
- Asset, AssetList,
3
- } from "../AssetList";
4
- import {reduceContentfulResponse} from "../../util/reduceContentfulResponse";
5
-
6
- export const ContentfulContentSelect = ({
7
- value,
8
- onChange,
9
- sdk,
10
- multiple,
11
- field,
12
- }) => {
13
- if (multiple) {
14
- return (
15
- <>
16
- {value && value.length > 0 && (
17
- <details className="mb-2">
18
- <summary>{value.length || 0} {value.length === 1 ? "Element" : "Elemente"}</summary>
19
- <div className="mt-4">
20
- <AssetList
21
- assets={value}
22
- sdk={sdk}
23
- onChange={onChange}
24
- onAddClick={() => {
25
- sdk.dialogs.selectMultipleEntries({contentTypes: field.control.contentTypes}).then(contents => {
26
- onChange(reduceContentfulResponse([...value, ...contents], field.control.paths));
27
- });
28
- }}/>
29
- </div>
30
- </details>
31
- )}
32
- <button
33
- className="button"
34
- onClick={() => {
35
- sdk.dialogs.selectMultipleEntries({contentTypes: field.control.contentTypes}).then(contents => {
36
- onChange(reduceContentfulResponse(contents, field.control.paths));
37
- });
38
- }}
39
- >Auswählen
40
- </button>
41
- </>
42
- );
43
- }
44
-
45
- return (
46
- <>
47
- {!!value && <div className="mb-2"><Asset
48
- sdk={sdk}
49
- asset={value}
50
- onDeleteClick={() => onChange(null)}
51
- onChange={onChange}/></div>}
52
- <button
53
- className="button"
54
- onClick={() => {
55
- sdk.dialogs.selectSingleEntry({contentTypes: field.control.contentTypes}).then(content => {
56
- onChange(reduceContentfulResponse(content, field.control.paths));
57
- });
58
- }}
59
- >Auswählen
60
- </button>
61
- </>
62
- );
1
+ import {
2
+ Asset, AssetList,
3
+ } from "../AssetList";
4
+ import {reduceContentfulResponse} from "../../util/reduceContentfulResponse";
5
+
6
+ export const ContentfulContentSelect = ({
7
+ value,
8
+ onChange,
9
+ sdk,
10
+ multiple,
11
+ field,
12
+ }) => {
13
+ if (multiple) {
14
+ return (
15
+ <>
16
+ {value && value.length > 0 && (
17
+ <details className="mb-2">
18
+ <summary>{value.length || 0} {value.length === 1 ? "Element" : "Elemente"}</summary>
19
+ <div className="mt-4">
20
+ <AssetList
21
+ assets={value}
22
+ sdk={sdk}
23
+ onChange={onChange}
24
+ displayField={field.control.displayField}
25
+ onAddClick={() => {
26
+ sdk.dialogs.selectMultipleEntries({contentTypes: field.control.contentTypes}).then(contents => {
27
+ onChange(reduceContentfulResponse([...value, ...contents], field.control.paths));
28
+ });
29
+ }}/>
30
+ </div>
31
+ </details>
32
+ )}
33
+ <button
34
+ className="button"
35
+ onClick={() => {
36
+ sdk.dialogs.selectMultipleEntries({contentTypes: field.control.contentTypes}).then(contents => {
37
+ onChange(reduceContentfulResponse(contents, field.control.paths));
38
+ });
39
+ }}
40
+ >Auswählen
41
+ </button>
42
+ </>
43
+ );
44
+ }
45
+
46
+ return (
47
+ <>
48
+ {!!value && <div className="mb-2"><Asset
49
+ sdk={sdk}
50
+ asset={value}
51
+ onDeleteClick={() => onChange(null)}
52
+ onChange={onChange}/></div>}
53
+ <button
54
+ className="button"
55
+ onClick={() => {
56
+ sdk.dialogs.selectSingleEntry({contentTypes: field.control.contentTypes}).then(content => {
57
+ onChange(reduceContentfulResponse(content, field.control.paths));
58
+ });
59
+ }}
60
+ >Auswählen
61
+ </button>
62
+ </>
63
+ );
63
64
  };