@kids-reporter/draft-editor 0.1.0

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.
Files changed (55) hide show
  1. package/README.md +51 -0
  2. package/lib/.selector/align-selector.js +71 -0
  3. package/lib/.selector/audio-selector.js +268 -0
  4. package/lib/.selector/image-selector.js +430 -0
  5. package/lib/.selector/pagination.js +82 -0
  6. package/lib/.selector/post-selector.js +311 -0
  7. package/lib/.selector/search-box.js +46 -0
  8. package/lib/.selector/video-selector.js +285 -0
  9. package/lib/block-renderer/background-image-block.js +141 -0
  10. package/lib/block-renderer/background-video-block.js +151 -0
  11. package/lib/block-renderer/color-box-block.js +86 -0
  12. package/lib/block-renderer/info-box-block.js +86 -0
  13. package/lib/block-renderer/side-index-block.js +90 -0
  14. package/lib/block-renderer/table-block.js +408 -0
  15. package/lib/block-renderer-fn.js +131 -0
  16. package/lib/buttons/annotation.js +117 -0
  17. package/lib/buttons/audio.js +65 -0
  18. package/lib/buttons/background-color.js +122 -0
  19. package/lib/buttons/background-image.js +223 -0
  20. package/lib/buttons/background-video.js +223 -0
  21. package/lib/buttons/color-box.js +173 -0
  22. package/lib/buttons/divider.js +63 -0
  23. package/lib/buttons/embedded-code.js +109 -0
  24. package/lib/buttons/enlarge.js +24 -0
  25. package/lib/buttons/font-color.js +115 -0
  26. package/lib/buttons/image.js +70 -0
  27. package/lib/buttons/info-box.js +148 -0
  28. package/lib/buttons/link.js +107 -0
  29. package/lib/buttons/media.js +121 -0
  30. package/lib/buttons/related-post.js +71 -0
  31. package/lib/buttons/selector/align-selector.js +71 -0
  32. package/lib/buttons/selector/audio-selector.js +279 -0
  33. package/lib/buttons/selector/image-selector.js +417 -0
  34. package/lib/buttons/selector/pagination.js +82 -0
  35. package/lib/buttons/selector/post-selector.js +317 -0
  36. package/lib/buttons/selector/search-box.js +46 -0
  37. package/lib/buttons/selector/video-selector.js +281 -0
  38. package/lib/buttons/side-index.js +200 -0
  39. package/lib/buttons/slideshow.js +71 -0
  40. package/lib/buttons/table.js +67 -0
  41. package/lib/buttons/text-align.js +88 -0
  42. package/lib/buttons/video.js +65 -0
  43. package/lib/buttons/youtube.js +147 -0
  44. package/lib/const.js +18 -0
  45. package/lib/draft-converter/api-data-instance.js +58 -0
  46. package/lib/draft-converter/atomic-block-processor.js +233 -0
  47. package/lib/draft-converter/entities.js +76 -0
  48. package/lib/draft-converter/index.js +201 -0
  49. package/lib/draft-converter/inline-styles-processor.js +236 -0
  50. package/lib/draft-editor.js +918 -0
  51. package/lib/entity-decorator.js +20 -0
  52. package/lib/index.js +15 -0
  53. package/lib/modifier.js +68 -0
  54. package/lib/theme/index.js +39 -0
  55. package/package.json +41 -0
@@ -0,0 +1,408 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.TableEditorBlock = void 0;
7
+
8
+ var _react = _interopRequireWildcard(require("react"));
9
+
10
+ var _styledComponents = _interopRequireDefault(require("styled-components"));
11
+
12
+ var _draftJs = require("draft-js");
13
+
14
+ var _cloneDeep = _interopRequireDefault(require("lodash/cloneDeep"));
15
+
16
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
+
18
+ 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); }
19
+
20
+ 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; }
21
+
22
+ const _ = {
23
+ cloneDeep: _cloneDeep.default
24
+ };
25
+ var ActionType;
26
+
27
+ (function (ActionType) {
28
+ ActionType["Insert"] = "insert";
29
+ ActionType["Delete"] = "delete";
30
+ ActionType["Update"] = "update";
31
+ })(ActionType || (ActionType = {}));
32
+
33
+ var TableEnum;
34
+
35
+ (function (TableEnum) {
36
+ TableEnum["Row"] = "row";
37
+ TableEnum["Column"] = "column";
38
+ })(TableEnum || (TableEnum = {}));
39
+
40
+ function createEmptyRow(colLen = 0, emptyValue) {
41
+ const rtn = [];
42
+
43
+ for (let i = 0; i < colLen; i++) {
44
+ rtn.push(emptyValue);
45
+ }
46
+
47
+ return rtn;
48
+ }
49
+
50
+ function resolveTableStyles(action, tableStyles) {
51
+ switch (action === null || action === void 0 ? void 0 : action.type) {
52
+ case ActionType.Insert:
53
+ {
54
+ if (action.target === TableEnum.Row) {
55
+ const rows = [...tableStyles.rows.slice(0, action.index), {}, ...tableStyles.rows.slice(action.index)];
56
+ return Object.assign({}, tableStyles, {
57
+ rows
58
+ });
59
+ } // TODO: handle target === TableEnum.Column if needed
60
+
61
+
62
+ return tableStyles;
63
+ }
64
+
65
+ case ActionType.Delete:
66
+ {
67
+ if (action.target === TableEnum.Row) {
68
+ const rows = [...tableStyles.rows.slice(0, action.index), ...tableStyles.rows.slice(action.index + 1)];
69
+ return Object.assign({}, tableStyles, {
70
+ rows
71
+ });
72
+ } // TODO: handle target === TableEnum.Column if needed
73
+
74
+
75
+ return tableStyles;
76
+ }
77
+ // TODO: handle action.type === ActionType.Update if needed
78
+
79
+ default:
80
+ {
81
+ return tableStyles;
82
+ }
83
+ }
84
+ }
85
+
86
+ function resolveTableData(action, tableData) {
87
+ switch (action === null || action === void 0 ? void 0 : action.type) {
88
+ case ActionType.Insert:
89
+ {
90
+ var _tableData$;
91
+
92
+ if (typeof (action === null || action === void 0 ? void 0 : action.index) !== 'number') {
93
+ return tableData;
94
+ }
95
+
96
+ if ((action === null || action === void 0 ? void 0 : action.target) === TableEnum.Column) {
97
+ // add the new column at specific position in each row
98
+ return tableData.map(r => [...r.slice(0, action === null || action === void 0 ? void 0 : action.index), _draftJs.EditorState.createEmpty(), ...r.slice(action === null || action === void 0 ? void 0 : action.index)]);
99
+ } // add the new row
100
+
101
+
102
+ return [...tableData.slice(0, action === null || action === void 0 ? void 0 : action.index), createEmptyRow(tableData === null || tableData === void 0 ? void 0 : (_tableData$ = tableData[0]) === null || _tableData$ === void 0 ? void 0 : _tableData$.length, _draftJs.EditorState.createEmpty()), ...tableData.slice(action === null || action === void 0 ? void 0 : action.index)];
103
+ }
104
+
105
+ case ActionType.Delete:
106
+ {
107
+ if (typeof (action === null || action === void 0 ? void 0 : action.index) !== 'number') {
108
+ return tableData;
109
+ }
110
+
111
+ if ((action === null || action === void 0 ? void 0 : action.target) === 'column') {
112
+ // delete the column at specific position in each row
113
+ return tableData.map(r => [...r.slice(0, action.index), ...r.slice(action.index + 1)]);
114
+ } // delete the column
115
+
116
+
117
+ return [...tableData.slice(0, action.index), ...tableData.slice(action.index + 1)];
118
+ }
119
+
120
+ case ActionType.Update:
121
+ {
122
+ // The reason we copy the array is to make sure
123
+ // that React component re-renders.
124
+ const copiedData = [...tableData];
125
+
126
+ if (typeof (action === null || action === void 0 ? void 0 : action.rIndex) !== 'number' || typeof (action === null || action === void 0 ? void 0 : action.cIndex) !== 'number') {
127
+ return copiedData;
128
+ }
129
+
130
+ copiedData[action.rIndex][action.cIndex] = action === null || action === void 0 ? void 0 : action.value;
131
+ return copiedData;
132
+ }
133
+
134
+ default:
135
+ {
136
+ return tableData;
137
+ }
138
+ }
139
+ }
140
+
141
+ function convertTableDataFromRaw(rawTableData) {
142
+ return rawTableData.map(rowData => {
143
+ return rowData.map(colData => {
144
+ const contentState = (0, _draftJs.convertFromRaw)(colData);
145
+ return _draftJs.EditorState.createWithContent(contentState);
146
+ });
147
+ });
148
+ }
149
+
150
+ function convertTableDataToRaw(tableData) {
151
+ return tableData.map(rowData => {
152
+ return rowData.map(colData => {
153
+ return (0, _draftJs.convertToRaw)(colData.getCurrentContent());
154
+ });
155
+ });
156
+ }
157
+
158
+ const Table = _styledComponents.default.div`
159
+ display: table;
160
+ width: 95%;
161
+ border-collapse: collapse;
162
+ `;
163
+ const Tr = _styledComponents.default.div`
164
+ display: table-row;
165
+ `;
166
+ const Td = _styledComponents.default.div`
167
+ display: table-cell;
168
+ border-width: 1px;
169
+ min-width: 100px;
170
+ min-height: 40px;
171
+ padding: 10px;
172
+ `;
173
+ const StyledFirstRow = _styledComponents.default.div`
174
+ display: table-row;
175
+ height: 10px;
176
+
177
+ div {
178
+ display: table-cell;
179
+ position: relative;
180
+ }
181
+
182
+ span {
183
+ cursor: pointer;
184
+ line-height: 10px;
185
+ }
186
+
187
+ span:first-child {
188
+ position: absolute;
189
+ right: 50%;
190
+ transform: translateX(50%);
191
+ }
192
+
193
+ span:first-child:before {
194
+ content: '•';
195
+ }
196
+
197
+ span:first-child:hover:before {
198
+ content: '➖';
199
+ }
200
+
201
+ span:last-child {
202
+ position: absolute;
203
+ right: -5px;
204
+ }
205
+
206
+ span:last-child:before {
207
+ content: '•';
208
+ }
209
+
210
+ span:last-child:hover:before {
211
+ content: '➕';
212
+ }
213
+ `;
214
+ const StyledFirstColumn = _styledComponents.default.div`
215
+ display: table-cell;
216
+ width: 10px;
217
+ position: relative;
218
+
219
+ span {
220
+ cursor: pointer;
221
+ }
222
+
223
+ span:first-child {
224
+ position: absolute;
225
+ bottom: 50%;
226
+ right: 0px;
227
+ transform: translateY(50%);
228
+ }
229
+
230
+ span:first-child:before {
231
+ content: '•';
232
+ }
233
+
234
+ span:first-child:hover:before {
235
+ content: '➖';
236
+ }
237
+
238
+ span:last-child {
239
+ position: absolute;
240
+ bottom: -10px;
241
+ right: 0px;
242
+ }
243
+
244
+ span:last-child:before {
245
+ content: '•';
246
+ }
247
+
248
+ span:last-child:hover:before {
249
+ content: '➕';
250
+ }
251
+ `;
252
+ const TableBlockContainer = _styledComponents.default.div`
253
+ margin: 15px 0;
254
+ position: relative;
255
+ overflow: scroll;
256
+ padding: 15px;
257
+ `;
258
+
259
+ const TableEditorBlock = props => {
260
+ var _tableData$2;
261
+
262
+ const {
263
+ block,
264
+ blockProps,
265
+ contentState
266
+ } = props;
267
+ const {
268
+ onEditStart,
269
+ onEditFinish,
270
+ getMainEditorReadOnly
271
+ } = blockProps;
272
+ const entityKey = block.getEntityAt(0);
273
+ const entity = contentState.getEntity(entityKey);
274
+ const {
275
+ tableData: rawTableData,
276
+ tableStyles: _tableStyles
277
+ } = entity.getData();
278
+ const [tableData, setTableData] = (0, _react.useState)(convertTableDataFromRaw(rawTableData)); // deep clone `_tableStyles` to prevent updating the entity data directly
279
+
280
+ const [tableStyles, setTableStyles] = (0, _react.useState)(_.cloneDeep(_tableStyles));
281
+ const tableRef = (0, _react.useRef)(null); // `TableBlock` will render other inner/nested DraftJS Editors inside the main Editor.
282
+ // However, main Editor's `readOnly` needs to be mutually exclusive with nested Editors' `readOnly`.
283
+ // If the main Editor and nested Editor are editable (`readOnly={false}`) at the same time,
284
+ // there will be a DraftJS Edtior Selection bug.
285
+
286
+ const [cellEditorReadOnly, setCellEditorReadOnly] = (0, _react.useState)(!getMainEditorReadOnly()); // The user clicks the table for editing
287
+
288
+ const onTableClick = () => {
289
+ // call `onEditStart` function to tell the main DraftJS Editor
290
+ // that we are going to interact with the custom atomic block.
291
+ onEditStart(); // make nested DraftJS Editors editable
292
+
293
+ setCellEditorReadOnly(false);
294
+ };
295
+
296
+ (0, _react.useEffect)(() => {
297
+ // The user clicks other places except the table,
298
+ // so we think he/she doesn't want to edit the table anymore.
299
+ // Therefore, we call `onEditFinish` function to pass modified table data
300
+ // back to the main DraftJS Edtior.
301
+ function handleClickOutside(event) {
302
+ // `!cellEditorReadOnly` condition is needed.
303
+ // If there are two tables in the main Editor,
304
+ // this `handleClickOutside` will only handle the just updated one.
305
+ if (tableRef.current && !tableRef.current.contains(event.target) && !cellEditorReadOnly) {
306
+ // make inner DraftJS Editors NOT editable
307
+ setCellEditorReadOnly(true); // call `onEditFinish` function tell the main DraftJS Editor
308
+ // that we are finishing interacting with the custom atomic block.
309
+
310
+ onEditFinish({
311
+ entityKey,
312
+ entityData: {
313
+ tableData: convertTableDataToRaw(tableData),
314
+ tableStyles
315
+ }
316
+ });
317
+ }
318
+ }
319
+
320
+ console.debug('(rich-text-editor/table): add click outside event listener');
321
+ document.addEventListener('mousedown', handleClickOutside);
322
+ return () => {
323
+ // Unbind the event listener on clean up
324
+ console.debug('(rich-text-editor/table): remove click outside event listener');
325
+ document.removeEventListener('mousedown', handleClickOutside);
326
+ };
327
+ }, // Skip running effect if `tableData` and `cellEditorReadOnly` are not changed.
328
+ [tableData, cellEditorReadOnly]);
329
+ return /*#__PURE__*/_react.default.createElement(TableBlockContainer, null, /*#__PURE__*/_react.default.createElement(Table, {
330
+ key: entityKey,
331
+ onClick: onTableClick,
332
+ ref: tableRef
333
+ }, /*#__PURE__*/_react.default.createElement(StyledFirstRow, null, /*#__PURE__*/_react.default.createElement("div", null), tableData === null || tableData === void 0 ? void 0 : (_tableData$2 = tableData[0]) === null || _tableData$2 === void 0 ? void 0 : _tableData$2.map((colData, cIndex) => {
334
+ return /*#__PURE__*/_react.default.createElement("div", {
335
+ key: `col_${cIndex + 1}`
336
+ }, /*#__PURE__*/_react.default.createElement("span", {
337
+ onClick: () => {
338
+ const deleteColumn = {
339
+ type: ActionType.Delete,
340
+ target: TableEnum.Column,
341
+ index: cIndex
342
+ };
343
+ const updatedTableData = resolveTableData(deleteColumn, tableData);
344
+ setTableData(updatedTableData);
345
+ }
346
+ }), /*#__PURE__*/_react.default.createElement("span", {
347
+ onClick: () => {
348
+ const insertColumn = {
349
+ type: ActionType.Insert,
350
+ target: TableEnum.Column,
351
+ index: cIndex + 1
352
+ };
353
+ const updatedTableData = resolveTableData(insertColumn, tableData);
354
+ setTableData(updatedTableData);
355
+ }
356
+ }));
357
+ })), tableData.map((rowData, rIndex) => {
358
+ var _tableStyles$rows;
359
+
360
+ const colsJsx = rowData.map((colData, cIndex) => {
361
+ return /*#__PURE__*/_react.default.createElement(Td, {
362
+ key: `col_${cIndex}`
363
+ }, /*#__PURE__*/_react.default.createElement(_draftJs.Editor, {
364
+ onChange: editorState => {
365
+ const updateAction = {
366
+ type: ActionType.Update,
367
+ cIndex,
368
+ rIndex,
369
+ value: editorState
370
+ };
371
+ const updatedTableData = resolveTableData(updateAction, tableData);
372
+ setTableData(updatedTableData);
373
+ },
374
+ editorState: colData,
375
+ readOnly: cellEditorReadOnly
376
+ }));
377
+ });
378
+ return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, {
379
+ key: `row_${rIndex}`
380
+ }, /*#__PURE__*/_react.default.createElement(Tr, {
381
+ style: tableStyles === null || tableStyles === void 0 ? void 0 : (_tableStyles$rows = tableStyles.rows) === null || _tableStyles$rows === void 0 ? void 0 : _tableStyles$rows[rIndex]
382
+ }, /*#__PURE__*/_react.default.createElement(StyledFirstColumn, null, /*#__PURE__*/_react.default.createElement("span", {
383
+ onClick: () => {
384
+ const deleteRowAction = {
385
+ type: ActionType.Delete,
386
+ target: TableEnum.Row,
387
+ index: rIndex
388
+ };
389
+ const updatedTableData = resolveTableData(deleteRowAction, tableData);
390
+ setTableData(updatedTableData);
391
+ setTableStyles(resolveTableStyles(deleteRowAction, tableStyles));
392
+ }
393
+ }), /*#__PURE__*/_react.default.createElement("span", {
394
+ onClick: () => {
395
+ const addRowAction = {
396
+ type: ActionType.Insert,
397
+ target: TableEnum.Row,
398
+ index: rIndex + 1
399
+ };
400
+ const updatedTableData = resolveTableData(addRowAction, tableData);
401
+ setTableData(updatedTableData);
402
+ setTableStyles(resolveTableStyles(addRowAction, tableStyles));
403
+ }
404
+ })), colsJsx));
405
+ })));
406
+ };
407
+
408
+ exports.TableEditorBlock = TableEditorBlock;
@@ -0,0 +1,131 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.atomicBlockRenderer = atomicBlockRenderer;
7
+
8
+ var _infoBoxBlock = require("./block-renderer/info-box-block");
9
+
10
+ var _tableBlock = require("./block-renderer/table-block");
11
+
12
+ var _colorBoxBlock = require("./block-renderer/color-box-block");
13
+
14
+ var _backgroundImageBlock = require("./block-renderer/background-image-block");
15
+
16
+ var _backgroundVideoBlock = require("./block-renderer/background-video-block");
17
+
18
+ var _sideIndexBlock = require("./block-renderer/side-index-block");
19
+
20
+ var _draftRenderer = _interopRequireDefault(require("@kids-reporter/draft-renderer"));
21
+
22
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
+
24
+ const {
25
+ EmbeddedCodeBlock,
26
+ MediaBlock,
27
+ ImageBlock,
28
+ SlideshowBlock,
29
+ SlideshowBlockV2,
30
+ DividerBlock,
31
+ RelatedPostBlock,
32
+ VideoBlock,
33
+ AudioBlock
34
+ } = _draftRenderer.default.blockRenderers;
35
+
36
+ const AtomicBlock = props => {
37
+ const entity = props.contentState.getEntity(props.block.getEntityAt(0));
38
+ const entityType = entity.getType();
39
+
40
+ switch (entityType) {
41
+ case 'audioLink':
42
+ case 'imageLink':
43
+ case 'videoLink':
44
+ {
45
+ return MediaBlock(entity);
46
+ }
47
+
48
+ case 'image':
49
+ {
50
+ return ImageBlock(entity);
51
+ }
52
+
53
+ case 'slideshow':
54
+ {
55
+ return SlideshowBlock(entity);
56
+ }
57
+
58
+ case 'slideshow-v2':
59
+ {
60
+ return SlideshowBlockV2(entity);
61
+ }
62
+
63
+ case 'EMBEDDEDCODE':
64
+ {
65
+ return EmbeddedCodeBlock(entity);
66
+ }
67
+
68
+ case 'INFOBOX':
69
+ {
70
+ return (0, _infoBoxBlock.InfoBoxEditorBlock)(props);
71
+ }
72
+
73
+ case 'DIVIDER':
74
+ {
75
+ return DividerBlock();
76
+ }
77
+
78
+ case 'TABLE':
79
+ {
80
+ return (0, _tableBlock.TableEditorBlock)(props);
81
+ }
82
+
83
+ case 'COLORBOX':
84
+ {
85
+ return (0, _colorBoxBlock.ColorBoxEditorBlock)(props);
86
+ }
87
+
88
+ case 'BACKGROUNDIMAGE':
89
+ {
90
+ return (0, _backgroundImageBlock.BGImageEditorBlock)(props);
91
+ }
92
+
93
+ case 'BACKGROUNDVIDEO':
94
+ {
95
+ return (0, _backgroundVideoBlock.BGVideoEditorBlock)(props);
96
+ }
97
+
98
+ case 'RELATEDPOST':
99
+ {
100
+ return RelatedPostBlock(entity);
101
+ }
102
+
103
+ case 'SIDEINDEX':
104
+ {
105
+ return (0, _sideIndexBlock.SideIndexEditorBlock)(props);
106
+ }
107
+
108
+ case 'VIDEO':
109
+ {
110
+ return VideoBlock(entity);
111
+ }
112
+
113
+ case 'AUDIO':
114
+ {
115
+ return AudioBlock(entity);
116
+ }
117
+ }
118
+
119
+ return null;
120
+ };
121
+
122
+ function atomicBlockRenderer(block) {
123
+ if (block.getType() === 'atomic') {
124
+ return {
125
+ component: AtomicBlock,
126
+ editable: false
127
+ };
128
+ }
129
+
130
+ return null;
131
+ }
@@ -0,0 +1,117 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.AnnotationButton = AnnotationButton;
7
+
8
+ var _react = _interopRequireWildcard(require("react"));
9
+
10
+ var _draftJs = require("draft-js");
11
+
12
+ var _modals = require("@keystone-ui/modals");
13
+
14
+ var _draftConverter = _interopRequireDefault(require("../draft-converter"));
15
+
16
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
+
18
+ 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); }
19
+
20
+ 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; }
21
+
22
+ function escapeHTML(s) {
23
+ return s.replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/'/g, '&#39;');
24
+ }
25
+
26
+ function AnnotationButton(props) {
27
+ const toggleEntity = _draftJs.RichUtils.toggleLink;
28
+ const {
29
+ isActive,
30
+ editorState,
31
+ onChange,
32
+ renderBasicEditor,
33
+ decorators
34
+ } = props;
35
+ const [toShowInput, setToShowInput] = (0, _react.useState)(false);
36
+ const [inputValue, setInputValue] = (0, _react.useState)({
37
+ editorStateOfBasicEditor: _draftJs.EditorState.createEmpty(decorators)
38
+ });
39
+
40
+ const promptForAnnotation = e => {
41
+ e.preventDefault();
42
+ const selection = editorState.getSelection();
43
+
44
+ if (!selection.isCollapsed()) {
45
+ setToShowInput(true);
46
+ }
47
+ };
48
+
49
+ const confirmAnnotation = () => {
50
+ const contentState = editorState.getCurrentContent();
51
+ const rawContentState = (0, _draftJs.convertToRaw)(inputValue.editorStateOfBasicEditor.getCurrentContent());
52
+
53
+ const bodyHTML = _draftConverter.default.convertToHtml(rawContentState);
54
+
55
+ const contentStateWithEntity = contentState.createEntity('ANNOTATION', 'MUTABLE', {
56
+ rawContentState,
57
+ bodyHTML,
58
+ bodyEscapedHTML: escapeHTML(bodyHTML)
59
+ });
60
+ const entityKey = contentStateWithEntity.getLastCreatedEntityKey();
61
+
62
+ const newEditorState = _draftJs.EditorState.set(editorState, {
63
+ currentContent: contentStateWithEntity
64
+ });
65
+
66
+ onChange(toggleEntity(newEditorState, newEditorState.getSelection(), entityKey));
67
+ setToShowInput(false);
68
+ setInputValue({
69
+ editorStateOfBasicEditor: _draftJs.EditorState.createEmpty(decorators)
70
+ });
71
+ };
72
+
73
+ const removeAnnotation = () => {
74
+ const selection = editorState.getSelection();
75
+
76
+ if (!selection.isCollapsed()) {
77
+ onChange(toggleEntity(editorState, selection, null));
78
+ }
79
+
80
+ setToShowInput(false);
81
+ setInputValue({
82
+ editorStateOfBasicEditor: _draftJs.EditorState.createEmpty(decorators)
83
+ });
84
+ };
85
+
86
+ const basicEditorJsx = renderBasicEditor({
87
+ editorState: inputValue.editorStateOfBasicEditor,
88
+ onChange: editorStateOfBasicEditor => {
89
+ setInputValue({
90
+ editorStateOfBasicEditor
91
+ });
92
+ }
93
+ });
94
+
95
+ const urlInput = /*#__PURE__*/_react.default.createElement(_modals.DrawerController, {
96
+ isOpen: toShowInput
97
+ }, /*#__PURE__*/_react.default.createElement(_modals.Drawer, {
98
+ title: "Insert Annotation",
99
+ actions: {
100
+ cancel: {
101
+ label: 'Cancel',
102
+ action: removeAnnotation
103
+ },
104
+ confirm: {
105
+ label: 'Confirm',
106
+ action: confirmAnnotation
107
+ }
108
+ }
109
+ }, basicEditorJsx));
110
+
111
+ return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, urlInput, /*#__PURE__*/_react.default.createElement("div", {
112
+ className: props.className,
113
+ onMouseDown: isActive ? removeAnnotation : promptForAnnotation
114
+ }, /*#__PURE__*/_react.default.createElement("i", {
115
+ className: "far"
116
+ }), /*#__PURE__*/_react.default.createElement("span", null, "Annotation")));
117
+ }