@mirrormedia/lilith-draft-editor 3.0.8 → 3.0.9

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.
@@ -0,0 +1,312 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.ImageUploader = ImageUploader;
7
+
8
+ var _apollo = require("@keystone-6/core/admin-ui/apollo");
9
+
10
+ var _modals = require("@keystone-ui/modals");
11
+
12
+ var _react = _interopRequireWildcard(require("react"));
13
+
14
+ var _styledComponents = _interopRequireDefault(require("styled-components"));
15
+
16
+ var _fileConvert = require("../utils/file-convert");
17
+
18
+ var _button = require("@keystone-ui/button");
19
+
20
+ var _fields = require("@keystone-ui/fields");
21
+
22
+ var _lodash = _interopRequireDefault(require("lodash"));
23
+
24
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
25
+
26
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
27
+
28
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
29
+
30
+ const imagesMutation = (0, _apollo.gql)`
31
+ mutation AddPhotos($data: [PhotoCreateInput!]!) {
32
+ photos: createPhotos(data: $data) {
33
+ id
34
+ name
35
+ imageFile {
36
+ url
37
+ width
38
+ height
39
+ }
40
+ resized {
41
+ original
42
+ w480
43
+ w800
44
+ w1200
45
+ w1600
46
+ w2400
47
+ }
48
+ resizedWebp {
49
+ original
50
+ w480
51
+ w800
52
+ w1200
53
+ w1600
54
+ w2400
55
+ }
56
+ }
57
+ }
58
+ `;
59
+ const CustomButton = (0, _styledComponents.default)(_button.Button)`
60
+ margin-top: 10px;
61
+ `;
62
+ const HiddenInput = _styledComponents.default.input`
63
+ display: none;
64
+ `;
65
+ const ImagesWrapper = _styledComponents.default.div`
66
+ overflow: auto;
67
+ margin-top: 10px;
68
+ `;
69
+ const ImageFilesWrapper = _styledComponents.default.div`
70
+ display: flex;
71
+ flex-wrap: wrap;
72
+ overflow: auto;
73
+ `;
74
+ const ImageFileWrapper = _styledComponents.default.div`
75
+ width: 33%;
76
+ cursor: pointer;
77
+ padding: 0 10px 10px;
78
+ `;
79
+ const Image = _styledComponents.default.img`
80
+ display: block;
81
+ width: 100%;
82
+ aspect-ratio: 2;
83
+ object-fit: contain;
84
+ `;
85
+ const ImageName = _styledComponents.default.p`
86
+ text-align: center;
87
+ `;
88
+ const Label = _styledComponents.default.label`
89
+ display: block;
90
+ margin: 10px 0;
91
+ font-weight: 600;
92
+ `;
93
+ const CustomCheckbox = (0, _styledComponents.default)(_fields.Checkbox)`
94
+ display: flex;
95
+ align-items: center;
96
+ cursor: pointer;
97
+ `;
98
+ const WatermarkTypeWrapper = _styledComponents.default.div`
99
+ margin-top: 10px;
100
+ `;
101
+ const WatermarkToggleWrapper = _styledComponents.default.div`
102
+ margin-top: 10px;
103
+ padding: 10px;
104
+ background-color: #f5f5f5;
105
+ border-radius: 4px;
106
+ `;
107
+
108
+ const AddImages = ({
109
+ onAddImages
110
+ }) => {
111
+ const inputRef = (0, _react.useRef)(null);
112
+
113
+ const inputChangeHandler = async event => {
114
+ const files = event.target.files;
115
+ event.target.files = null;
116
+ if (!files) return;
117
+ const imagesFiles = await (0, _fileConvert.convertFilesToImageData)(files);
118
+ console.log('imagesFiles', imagesFiles);
119
+ onAddImages(imagesFiles);
120
+ };
121
+
122
+ const onUploadButton = () => {
123
+ var _inputRef$current;
124
+
125
+ (_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 ? void 0 : _inputRef$current.click();
126
+ };
127
+
128
+ return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(CustomButton, {
129
+ onClick: onUploadButton
130
+ }, "\u65B0\u589E\u5716\u7247"), /*#__PURE__*/_react.default.createElement(HiddenInput, {
131
+ type: "file",
132
+ multiple: true,
133
+ accept: "image/*",
134
+ ref: inputRef,
135
+ onChange: inputChangeHandler
136
+ }));
137
+ };
138
+
139
+ const ImageFiles = ({
140
+ files,
141
+ onChange
142
+ }) => {
143
+ return /*#__PURE__*/_react.default.createElement(ImageFilesWrapper, null, files.map(file => /*#__PURE__*/_react.default.createElement(ImageFile, {
144
+ key: file.uid,
145
+ file: file,
146
+ onChange: onChange
147
+ })));
148
+ };
149
+
150
+ const ImageFile = ({
151
+ file,
152
+ onChange
153
+ }) => {
154
+ return /*#__PURE__*/_react.default.createElement(ImageFileWrapper, null, /*#__PURE__*/_react.default.createElement(Image, {
155
+ src: file.blobURL
156
+ }), /*#__PURE__*/_react.default.createElement(Label, {
157
+ htmlFor: "name"
158
+ }, "Image Name"), /*#__PURE__*/_react.default.createElement(_fields.TextInput, {
159
+ id: "name",
160
+ type: "text",
161
+ defaultValue: file.name,
162
+ onChange: _lodash.default.debounce(e => {
163
+ onChange({ ...file,
164
+ name: e.target.value
165
+ });
166
+ })
167
+ }), /*#__PURE__*/_react.default.createElement(ImageName, null, file.name), /*#__PURE__*/_react.default.createElement(CustomCheckbox, {
168
+ defaultChecked: file.shouldSetWatermark,
169
+ onChange: _lodash.default.debounce(e => {
170
+ onChange({ ...file,
171
+ shouldSetWatermark: e.target.checked
172
+ });
173
+ })
174
+ }, "\u6D6E\u6C34\u5370"), file.shouldSetWatermark && /*#__PURE__*/_react.default.createElement(WatermarkTypeWrapper, null, /*#__PURE__*/_react.default.createElement(_fields.Select, {
175
+ value: {
176
+ label: file.watermarkType === 'mirrordaily' ? '鏡報' : '鏡週刊',
177
+ value: file.watermarkType
178
+ },
179
+ onChange: option => {
180
+ onChange({ ...file,
181
+ watermarkType: (option === null || option === void 0 ? void 0 : option.value) || 'mirrormedia'
182
+ });
183
+ },
184
+ options: [{
185
+ label: '鏡週刊',
186
+ value: 'mirrormedia'
187
+ }, {
188
+ label: '鏡報',
189
+ value: 'mirrordaily'
190
+ }]
191
+ })));
192
+ };
193
+
194
+ function ImageUploader({
195
+ onChange
196
+ }) {
197
+ const [files, setFiles] = (0, _react.useState)([]);
198
+ const [watermarkAll, setWatermarkAll] = (0, _react.useState)(true);
199
+ const [watermarkTypeAll, setWatermarkTypeAll] = (0, _react.useState)('mirrormedia');
200
+ const [addPhotos, {
201
+ data: uploadData,
202
+ loading: uploadLoading,
203
+ error: uploadError
204
+ }] = (0, _apollo.useMutation)(imagesMutation);
205
+
206
+ const onAddImages = rawFiles => {
207
+ const existedImageUids = new Set(files.map(file => file.uid));
208
+ const newFiles = rawFiles.filter(file => !existedImageUids.has(file.uid)).map(rawFile => ({ ...rawFile,
209
+ shouldSetWatermark: watermarkAll,
210
+ watermarkType: watermarkTypeAll
211
+ }));
212
+ setFiles(prev => prev.concat(newFiles));
213
+ };
214
+
215
+ const toggleAllWatermarks = checked => {
216
+ setWatermarkAll(checked);
217
+ setFiles(prev => prev.map(file => ({ ...file,
218
+ shouldSetWatermark: checked
219
+ })));
220
+ };
221
+
222
+ const changeAllWatermarkTypes = type => {
223
+ setWatermarkTypeAll(type);
224
+ setFiles(prev => prev.map(file => ({ ...file,
225
+ watermarkType: type
226
+ })));
227
+ };
228
+
229
+ const onConfirm = async () => {
230
+ if (!files.length) return onChange([]);
231
+ const tasks = files.map(async data => ({ ...data,
232
+ file: await (0, _fileConvert.convertStringToFile)(data.blobURL, data.name, data.type)
233
+ }));
234
+ const data = (await Promise.allSettled(tasks)).filter(function (settledResult) {
235
+ return settledResult.status === 'fulfilled';
236
+ }).map(result => result.value);
237
+ addPhotos({
238
+ variables: {
239
+ data: data.map(d => ({
240
+ name: d.name,
241
+ imageFile: {
242
+ upload: d.file
243
+ },
244
+ waterMark: d.shouldSetWatermark,
245
+ watermarkType: d.watermarkType
246
+ }))
247
+ }
248
+ });
249
+ };
250
+
251
+ const onImageFileChang = file => {
252
+ const newFiles = Array.from(files);
253
+ const foundIndex = newFiles.findIndex(ele => ele.uid === file.uid);
254
+
255
+ if (foundIndex !== -1) {
256
+ newFiles[foundIndex] = file;
257
+ setFiles(newFiles);
258
+ }
259
+ };
260
+
261
+ (0, _react.useEffect)(() => {
262
+ var _uploadData$photos;
263
+
264
+ if (uploadData !== null && uploadData !== void 0 && (_uploadData$photos = uploadData.photos) !== null && _uploadData$photos !== void 0 && _uploadData$photos.length) {
265
+ onChange(uploadData.photos);
266
+ }
267
+ }, [uploadData, onChange]);
268
+ return /*#__PURE__*/_react.default.createElement(_modals.DrawerController, {
269
+ isOpen: true
270
+ }, /*#__PURE__*/_react.default.createElement(_modals.Drawer, {
271
+ title: "Upload images",
272
+ actions: {
273
+ cancel: {
274
+ label: 'Cancel',
275
+ action: () => {
276
+ onChange([]);
277
+ }
278
+ },
279
+ confirm: {
280
+ label: 'Confirm',
281
+ action: onConfirm
282
+ }
283
+ },
284
+ width: "narrow"
285
+ }, /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement(AddImages, {
286
+ onAddImages: onAddImages
287
+ }), files.length > 0 && /*#__PURE__*/_react.default.createElement(WatermarkToggleWrapper, null, /*#__PURE__*/_react.default.createElement(CustomCheckbox, {
288
+ checked: watermarkAll,
289
+ onChange: e => {
290
+ toggleAllWatermarks(e.target.checked);
291
+ }
292
+ }, "\u5168\u90E8\u52A0\u4E0A\u6D6E\u6C34\u5370"), watermarkAll && /*#__PURE__*/_react.default.createElement(WatermarkTypeWrapper, null, /*#__PURE__*/_react.default.createElement(_fields.Select, {
293
+ value: {
294
+ label: watermarkTypeAll === 'mirrordaily' ? '鏡報' : '鏡週刊',
295
+ value: watermarkTypeAll
296
+ },
297
+ onChange: option => {
298
+ const type = (option === null || option === void 0 ? void 0 : option.value) || 'mirrormedia';
299
+ changeAllWatermarkTypes(type);
300
+ },
301
+ options: [{
302
+ label: '鏡週刊',
303
+ value: 'mirrormedia'
304
+ }, {
305
+ label: '鏡報',
306
+ value: 'mirrordaily'
307
+ }]
308
+ }))), /*#__PURE__*/_react.default.createElement(ImagesWrapper, null, /*#__PURE__*/_react.default.createElement(ImageFiles, {
309
+ files: files,
310
+ onChange: onImageFileChang
311
+ })), uploadLoading && /*#__PURE__*/_react.default.createElement("div", null, "loading"), uploadError && /*#__PURE__*/_react.default.createElement("div", null, uploadError.message))));
312
+ }
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.convertBlobToString = convertBlobToString;
7
+ exports.convertFilesToImageData = convertFilesToImageData;
8
+ exports.convertStringToFile = convertStringToFile;
9
+
10
+ var _jsSha = require("js-sha256");
11
+
12
+ async function convertFilesToMediaData(fileList, isValidFile, wrapResult) {
13
+ const files = Array.from(fileList);
14
+ const fileNamePostfix = new Intl.DateTimeFormat('fr-CA', {
15
+ year: 'numeric',
16
+ month: '2-digit',
17
+ day: '2-digit'
18
+ }).format(Date.now());
19
+ const tasks = files.map(async file => {
20
+ if (!isValidFile(file)) return;
21
+ const uInt8Data = await file.arrayBuffer();
22
+ const name = file.name;
23
+ const positionOfLastDot = name.lastIndexOf('.');
24
+ const filename = positionOfLastDot > -1 ? `${name.slice(0, positionOfLastDot)}_${fileNamePostfix}${name.slice(positionOfLastDot)}` : `${name}_${fileNamePostfix}`;
25
+ return wrapResult({
26
+ uid: (0, _jsSha.sha256)(uInt8Data),
27
+ name: filename,
28
+ type: file.type,
29
+ blobURL: convertBlobToString(file)
30
+ });
31
+ });
32
+ const results = await Promise.allSettled(tasks);
33
+ return results.filter(r => r.status === 'fulfilled').map(r => r.value).filter(r => Boolean(r));
34
+ }
35
+
36
+ function isImageFile(file) {
37
+ return file.type.startsWith('image/');
38
+ }
39
+
40
+ async function convertFilesToImageData(fileList) {
41
+ return convertFilesToMediaData(fileList, isImageFile, ({
42
+ uid,
43
+ name,
44
+ type,
45
+ blobURL
46
+ }) => ({
47
+ uid,
48
+ name,
49
+ type,
50
+ blobURL
51
+ }));
52
+ }
53
+
54
+ function blobToFile(blob, fileName, extension) {
55
+ return new File([blob], fileName, {
56
+ type: extension
57
+ });
58
+ }
59
+
60
+ function convertBlobToString(blob) {
61
+ return URL.createObjectURL(blob);
62
+ }
63
+
64
+ async function convertStringToFile(str, fileName, extension) {
65
+ const request = new Request(str);
66
+ return await fetch(request).then(response => response.blob()).then(blob => blobToFile(blob, fileName, extension ?? blob.type));
67
+ }
@@ -77,6 +77,8 @@ const buttonNames = {
77
77
  // inline styles
78
78
  bold: 'bold',
79
79
  italic: 'italic',
80
+ sup: 'sup',
81
+ sub: 'sub',
80
82
  underline: 'underline',
81
83
  code: 'code',
82
84
  // block styles
@@ -439,6 +441,12 @@ class RichTextEditor extends _react.default.Component {
439
441
  fontFamily: '"Inconsolata", "Menlo", "Consolas", monospace',
440
442
  fontSize: 16,
441
443
  padding: 2
444
+ },
445
+ SUP: {
446
+ verticalAlign: 'super'
447
+ },
448
+ SUB: {
449
+ verticalAlign: 'sub'
442
450
  }
443
451
  };
444
452
  }
@@ -920,6 +928,14 @@ const inlineStyles = [{
920
928
  label: 'Italic',
921
929
  style: 'ITALIC',
922
930
  icon: 'fas fa-italic'
931
+ }, {
932
+ label: 'Superscript',
933
+ style: 'SUP',
934
+ icon: 'fas fa-superscript'
935
+ }, {
936
+ label: 'Subscript',
937
+ style: 'SUB',
938
+ icon: 'fas fa-subscript'
923
939
  }, {
924
940
  label: 'Underline',
925
941
  style: 'UNDERLINE',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mirrormedia/lilith-draft-editor",
3
- "version": "3.0.8",
3
+ "version": "3.0.9",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
@@ -22,6 +22,7 @@
22
22
  "dependencies": {
23
23
  "@mirrormedia/lilith-draft-renderer": "^1.4.3",
24
24
  "draft-js": "^0.11.7",
25
+ "js-sha256": "^0.11.0",
25
26
  "usehooks-ts": "^2"
26
27
  },
27
28
  "peerDependencies": {
@@ -36,4 +37,4 @@
36
37
  "files": [
37
38
  "lib"
38
39
  ]
39
- }
40
+ }