@mirrormedia/lilith-draft-editor 1.0.0-beta4 → 1.0.1

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 (65) hide show
  1. package/README.md +51 -0
  2. package/lib/draft-js/buttons/annotation.js +17 -17
  3. package/lib/draft-js/buttons/background-color.js +6 -7
  4. package/lib/draft-js/buttons/background-image.js +18 -17
  5. package/lib/draft-js/buttons/background-video.js +18 -17
  6. package/lib/draft-js/buttons/color-box.js +19 -18
  7. package/lib/draft-js/buttons/font-color.js +6 -7
  8. package/lib/draft-js/buttons/info-box.js +18 -17
  9. package/lib/draft-js/buttons/text-align.js +1 -1
  10. package/lib/draft-js/const.js +18 -0
  11. package/lib/draft-js/{editor/draft-converter → draft-converter}/api-data-instance.js +0 -0
  12. package/lib/draft-js/{editor/draft-converter → draft-converter}/atomic-block-processor.js +0 -0
  13. package/lib/draft-js/{editor/draft-converter → draft-converter}/entities.js +0 -0
  14. package/lib/draft-js/{editor/draft-converter → draft-converter}/index.js +0 -0
  15. package/lib/draft-js/{editor/draft-converter → draft-converter}/inline-styles-processor.js +6 -8
  16. package/lib/draft-js/{editor/modifier.js → modifier.js} +0 -0
  17. package/lib/index.js +1 -1
  18. package/lib/website/{readr/custom → mirrormedia}/block-renderer/background-image-block.js +7 -5
  19. package/lib/website/{readr/custom → mirrormedia}/block-renderer/background-video-block.js +7 -5
  20. package/lib/website/{readr/custom → mirrormedia}/block-renderer/color-box-block.js +7 -5
  21. package/lib/website/mirrormedia/{custom/block-renderer → block-renderer}/info-box-block.js +7 -5
  22. package/lib/website/mirrormedia/{custom/block-renderer → block-renderer}/side-index-block.js +3 -3
  23. package/lib/website/mirrormedia/{custom/block-renderer → block-renderer}/table-block.js +0 -0
  24. package/lib/website/mirrormedia/{draft-editor/block-renderer-fn.js → block-renderer-fn.js} +10 -8
  25. package/lib/website/mirrormedia/{draft-editor/index.js → draft-editor.js} +126 -33
  26. package/lib/website/mirrormedia/{draft-editor/entity-decorator.js → entity-decorator.js} +4 -2
  27. package/lib/website/mirrormedia/{custom/selector → selector}/align-selector.js +0 -0
  28. package/lib/website/mirrormedia/{custom/selector → selector}/image-selector.js +0 -0
  29. package/lib/website/mirrormedia/{custom/selector → selector}/pagination.js +0 -0
  30. package/lib/website/mirrormedia/{custom/selector → selector}/post-selector.js +0 -0
  31. package/lib/website/mirrormedia/{custom/selector → selector}/search-box.js +0 -0
  32. package/lib/website/mirrormedia/{custom/selector → selector}/video-selector.js +0 -0
  33. package/lib/website/{mirrormedia/custom → readr}/block-renderer/background-image-block.js +7 -5
  34. package/lib/website/{mirrormedia/custom → readr}/block-renderer/background-video-block.js +7 -5
  35. package/lib/website/{mirrormedia/custom → readr}/block-renderer/color-box-block.js +7 -5
  36. package/lib/website/readr/{custom/block-renderer → block-renderer}/info-box-block.js +7 -5
  37. package/lib/website/readr/{custom/block-renderer → block-renderer}/side-index-block.js +3 -3
  38. package/lib/website/readr/{custom/block-renderer → block-renderer}/table-block.js +0 -0
  39. package/lib/website/readr/{draft-editor/block-renderer-fn.js → block-renderer-fn.js} +10 -8
  40. package/lib/website/readr/{draft-editor/index.js → draft-editor.js} +126 -33
  41. package/lib/website/readr/{draft-editor/entity-decorator.js → entity-decorator.js} +4 -2
  42. package/lib/website/readr/{custom/selector → selector}/align-selector.js +0 -0
  43. package/lib/website/readr/{custom/selector → selector}/image-selector.js +0 -0
  44. package/lib/website/readr/{custom/selector → selector}/pagination.js +0 -0
  45. package/lib/website/readr/{custom/selector → selector}/post-selector.js +0 -0
  46. package/lib/website/readr/{custom/selector → selector}/search-box.js +0 -0
  47. package/lib/website/readr/{custom/selector → selector}/video-selector.js +0 -0
  48. package/package.json +4 -4
  49. package/lib/draft-js/block-renderer/background-image-block.js +0 -123
  50. package/lib/draft-js/block-renderer/background-video-block.js +0 -133
  51. package/lib/draft-js/block-renderer/color-box-block.js +0 -92
  52. package/lib/draft-js/block-renderer/divider-block.js +0 -24
  53. package/lib/draft-js/block-renderer/embedded-code-block.js +0 -63
  54. package/lib/draft-js/block-renderer/image-block.js +0 -47
  55. package/lib/draft-js/block-renderer/info-box-block.js +0 -89
  56. package/lib/draft-js/block-renderer/media-block.js +0 -65
  57. package/lib/draft-js/block-renderer/related-post-block.js +0 -51
  58. package/lib/draft-js/block-renderer/side-index-block.js +0 -103
  59. package/lib/draft-js/block-renderer/slideshow-block.js +0 -71
  60. package/lib/draft-js/block-renderer/table-block.js +0 -408
  61. package/lib/draft-js/editor/basic-editor.js +0 -366
  62. package/lib/draft-js/editor/block-renderer-fn.js +0 -117
  63. package/lib/draft-js/editor/entity-decorator.js +0 -16
  64. package/lib/draft-js/entity-decorator/annotation-decorator.js +0 -86
  65. package/lib/draft-js/entity-decorator/link-decorator.js +0 -39
@@ -1,51 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.RelatedPostBlock = RelatedPostBlock;
7
-
8
- var _react = _interopRequireDefault(require("react"));
9
-
10
- var _styledComponents = _interopRequireDefault(require("styled-components"));
11
-
12
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
-
14
- const RelatedPostRenderWrapper = _styledComponents.default.div`
15
- display: flex;
16
- width: 100%;
17
- `;
18
- const RelatedPostItem = _styledComponents.default.div`
19
- flex: 0 0 33.3333%;
20
- border: 1px solid rgba(0, 0, 0, 0.05);
21
- `;
22
- const RelatedPostImage = _styledComponents.default.img`
23
- display: block;
24
- width: 100%;
25
- aspect-ratio: 2;
26
- object-fit: cover;
27
- `;
28
- const RelatedPostTitle = _styledComponents.default.p`
29
- margin: 0;
30
- padding: 12px;
31
- `;
32
-
33
- function RelatedPostBlock(entity) {
34
- const {
35
- posts
36
- } = entity.getData();
37
- return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(RelatedPostRenderWrapper, null, posts.map(post => {
38
- var _post$heroImage, _post$heroImage$resiz;
39
-
40
- return /*#__PURE__*/_react.default.createElement(RelatedPostItem, {
41
- key: post.id
42
- }, /*#__PURE__*/_react.default.createElement(RelatedPostImage, {
43
- src: (_post$heroImage = post.heroImage) === null || _post$heroImage === void 0 ? void 0 : (_post$heroImage$resiz = _post$heroImage.resized) === null || _post$heroImage$resiz === void 0 ? void 0 : _post$heroImage$resiz.original,
44
- onError: e => {
45
- var _post$heroImage2, _post$heroImage2$imag;
46
-
47
- return e.currentTarget.src = (_post$heroImage2 = post.heroImage) === null || _post$heroImage2 === void 0 ? void 0 : (_post$heroImage2$imag = _post$heroImage2.imageFile) === null || _post$heroImage2$imag === void 0 ? void 0 : _post$heroImage2$imag.url;
48
- }
49
- }), /*#__PURE__*/_react.default.createElement(RelatedPostTitle, null, post.name));
50
- })));
51
- }
@@ -1,103 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.SideIndexBlock = SideIndexBlock;
7
-
8
- var _react = _interopRequireWildcard(require("react"));
9
-
10
- var _styledComponents = _interopRequireDefault(require("styled-components"));
11
-
12
- var _sideIndex = require("../buttons/side-index");
13
-
14
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
-
16
- 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); }
17
-
18
- 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; }
19
-
20
- const SideIndexBlockWrapper = _styledComponents.default.div`
21
- display: flex;
22
- align-items: center;
23
- `;
24
- const SideIndexBlockButton = _styledComponents.default.div`
25
- cursor: pointer;
26
- margin-left: 20px;
27
- `;
28
- const SideIndex = _styledComponents.default.span`
29
- font-size: 16px;
30
- margin-left: 20px;
31
- `;
32
-
33
- function SideIndexBlock(props) {
34
- const [toShowInput, setToShowInput] = (0, _react.useState)(false);
35
- const {
36
- block,
37
- blockProps,
38
- contentState
39
- } = props;
40
- const {
41
- onEditStart,
42
- onEditFinish
43
- } = blockProps;
44
- const entityKey = block.getEntityAt(0);
45
- const entity = contentState.getEntity(entityKey);
46
- const {
47
- h2Text,
48
- sideIndexText,
49
- sideIndexUrl,
50
- sideIndexImage
51
- } = entity.getData();
52
-
53
- const onChange = ({
54
- h2Text,
55
- sideIndexText,
56
- sideIndexUrl,
57
- sideIndexImage
58
- }) => {
59
- // close `SideIndexInput`
60
- setToShowInput(false);
61
- onEditFinish({
62
- entityKey,
63
- entityData: {
64
- h2Text,
65
- sideIndexText,
66
- sideIndexUrl,
67
- sideIndexImage
68
- }
69
- });
70
- };
71
-
72
- let sideIndexBlock;
73
-
74
- if (sideIndexUrl) {
75
- sideIndexBlock = /*#__PURE__*/_react.default.createElement("a", {
76
- href: sideIndexUrl
77
- }, /*#__PURE__*/_react.default.createElement(SideIndex, null, "\u5074\u6B04\uFF1A ", sideIndexText ? sideIndexText : h2Text));
78
- } else {
79
- sideIndexBlock = /*#__PURE__*/_react.default.createElement("h2", null, h2Text, /*#__PURE__*/_react.default.createElement(SideIndex, null, "\u5074\u6B04\uFF1A ", sideIndexText ? sideIndexText : h2Text));
80
- }
81
-
82
- return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_sideIndex.SideIndexInput, {
83
- h2Text: h2Text,
84
- sideIndexText: sideIndexText,
85
- sideIndexUrl: sideIndexUrl,
86
- sideIndexImage: sideIndexImage,
87
- onChange: onChange,
88
- onCancel: () => {
89
- onEditFinish({});
90
- setToShowInput(false);
91
- },
92
- isOpen: toShowInput
93
- }), /*#__PURE__*/_react.default.createElement(SideIndexBlockWrapper, null, sideIndexBlock, /*#__PURE__*/_react.default.createElement(SideIndexBlockButton, {
94
- onClick: () => {
95
- // call `onEditStart` prop as we are trying to update the SideIndex entity
96
- onEditStart(); // open `SideIndexInput`
97
-
98
- setToShowInput(true);
99
- }
100
- }, /*#__PURE__*/_react.default.createElement("i", {
101
- className: "fa-solid fa-pen"
102
- }), /*#__PURE__*/_react.default.createElement("span", null, "Modify"))));
103
- }
@@ -1,71 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.SlideshowBlock = SlideshowBlock;
7
- exports.SlideshowBlockV2 = SlideshowBlockV2;
8
-
9
- var _react = _interopRequireDefault(require("react"));
10
-
11
- var _styledComponents = _interopRequireDefault(require("styled-components"));
12
-
13
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
14
-
15
- const Image = _styledComponents.default.img`
16
- width: 100%;
17
- `;
18
- const SlideshowCount = _styledComponents.default.div`
19
- position: absolute;
20
- top: 50%;
21
- left: 50%;
22
- border-radius: 100%;
23
- border: black 1px solid;
24
- transform: translate(-50%, -50%);
25
- background-color: white;
26
- display: flex;
27
- align-items: center;
28
- justify-content: center;
29
- flex-direction: column;
30
- aspect-ratio: 1;
31
- min-height: 66px;
32
- padding: 10px;
33
- `;
34
- const Figure = _styledComponents.default.figure`
35
- position: relative;
36
- margin-block: unset;
37
- margin-inline: unset;
38
- margin: 0 10px;
39
- `; // support old version of slideshow without delay propertiy
40
-
41
- function SlideshowBlock(entity) {
42
- var _images$, _images$$resized;
43
-
44
- const images = entity.getData();
45
- return /*#__PURE__*/_react.default.createElement(Figure, null, /*#__PURE__*/_react.default.createElement(Image, {
46
- src: images === null || images === void 0 ? void 0 : (_images$ = images[0]) === null || _images$ === void 0 ? void 0 : (_images$$resized = _images$.resized) === null || _images$$resized === void 0 ? void 0 : _images$$resized.w800,
47
- onError: e => {
48
- var _images$2, _images$2$imageFile;
49
-
50
- return e.currentTarget.src = images === null || images === void 0 ? void 0 : (_images$2 = images[0]) === null || _images$2 === void 0 ? void 0 : (_images$2$imageFile = _images$2.imageFile) === null || _images$2$imageFile === void 0 ? void 0 : _images$2$imageFile.url;
51
- }
52
- }), /*#__PURE__*/_react.default.createElement(SlideshowCount, null, "+", images.length));
53
- } // 202206 latest version of slideshow, support delay property
54
-
55
-
56
- function SlideshowBlockV2(entity) {
57
- var _images$3, _images$3$resized;
58
-
59
- const {
60
- images,
61
- delay
62
- } = entity.getData();
63
- return /*#__PURE__*/_react.default.createElement(Figure, null, /*#__PURE__*/_react.default.createElement(Image, {
64
- src: images === null || images === void 0 ? void 0 : (_images$3 = images[0]) === null || _images$3 === void 0 ? void 0 : (_images$3$resized = _images$3.resized) === null || _images$3$resized === void 0 ? void 0 : _images$3$resized.w800,
65
- onError: e => {
66
- var _images$4, _images$4$imageFile;
67
-
68
- return e.currentTarget.src = images === null || images === void 0 ? void 0 : (_images$4 = images[0]) === null || _images$4 === void 0 ? void 0 : (_images$4$imageFile = _images$4.imageFile) === null || _images$4$imageFile === void 0 ? void 0 : _images$4$imageFile.url;
69
- }
70
- }), /*#__PURE__*/_react.default.createElement(SlideshowCount, null, /*#__PURE__*/_react.default.createElement("div", null, "+", images.length), delay && /*#__PURE__*/_react.default.createElement("div", null, `${delay}s`)));
71
- }
@@ -1,408 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.TableBlock = 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 TableBlock = 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.TableBlock = TableBlock;