@kids-reporter/draft-editor 1.0.7 → 1.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kids-reporter/draft-editor",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
@@ -23,7 +23,7 @@
23
23
  ],
24
24
  "license": "MIT",
25
25
  "dependencies": {
26
- "@kids-reporter/draft-renderer": "1.0.5",
26
+ "@kids-reporter/draft-renderer": "1.0.7",
27
27
  "@twreporter/errors": "^1.1.2",
28
28
  "draft-js": "^0.11.7"
29
29
  },
@@ -1,279 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = exports.decorator = exports.RichTextEditor = void 0;
7
- var _react = _interopRequireDefault(require("react"));
8
- var _draftJs = require("draft-js");
9
- var _draftRenderer = require("@kids-reporter/draft-renderer");
10
- var _btNames = _interopRequireDefault(require("./buttons/bt-names"));
11
- var _blockRendererFn = require("./block-renderer-fn");
12
- var _styled = require("./styled");
13
- var _buttons = require("./buttons");
14
- var _imageSelector = require("./buttons/selector/image-selector");
15
- var _annotation = require("./buttons/annotation");
16
- var _infoBox = require("./buttons/info-box");
17
- var _bgColor = require("./buttons/bg-color");
18
- var _fontColor = require("./buttons/font-color");
19
- var _tocAnchor = require("./entity-decorators/toc-anchor");
20
- var _anchor = require("./entity-decorators/anchor");
21
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
22
- function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
23
- const styleSource = ['https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css', 'https://storage.googleapis.com/static-readr-tw-dev/cdn/draft-js/rich-editor.css', 'https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css'].map((src, index) => /*#__PURE__*/_react.default.createElement("link", {
24
- key: `style-src-${index}`,
25
- href: src,
26
- rel: "stylesheet",
27
- type: "text/css"
28
- }));
29
- const decorator = exports.decorator = new _draftJs.CompositeDecorator([_draftRenderer.annotationDecorator, _draftRenderer.linkDecorator]);
30
- class RichTextEditor extends _react.default.Component {
31
- editorRef = null;
32
- constructor(props) {
33
- super(props);
34
- this.state = {
35
- isEnlarged: false,
36
- readOnly: false
37
- };
38
- this.editorDecorator = new _draftJs.CompositeDecorator([_draftRenderer.annotationDecorator, _draftRenderer.linkDecorator, {
39
- ..._tocAnchor.editableTOCAnchorDecorator,
40
- props: {
41
- ...this.customEditProps
42
- }
43
- }, {
44
- ..._anchor.editableAnchorDecorator,
45
- props: {
46
- ...this.customEditProps
47
- }
48
- }]);
49
- }
50
- onChange = editorState => {
51
- this.props.onChange(editorState);
52
- };
53
- handleKeyCommand = (command, editorState) => {
54
- const newState = _draftJs.RichUtils.handleKeyCommand(editorState, command);
55
- if (newState) {
56
- this.onChange(newState);
57
- return 'handled';
58
- }
59
- return 'not-handled';
60
- };
61
- handleReturn = event => {
62
- if (_draftJs.KeyBindingUtil.isSoftNewlineEvent(event)) {
63
- const {
64
- onChange,
65
- editorState
66
- } = this.props;
67
- onChange(_draftJs.RichUtils.insertSoftNewline(editorState));
68
- return 'handled';
69
- }
70
- return 'not-handled';
71
- };
72
- mapKeyToEditorCommand = e => {
73
- if (e.keyCode === 9 /* TAB */) {
74
- const newEditorState = _draftJs.RichUtils.onTab(e, this.props.editorState, 4 /* maxDepth */);
75
- if (newEditorState !== this.props.editorState) {
76
- this.onChange(newEditorState);
77
- }
78
- return null;
79
- }
80
- return (0, _draftJs.getDefaultKeyBinding)(e);
81
- };
82
- toggleBlockType = blockType => {
83
- this.onChange(_draftJs.RichUtils.toggleBlockType(this.props.editorState, blockType));
84
- };
85
- toggleInlineStyle = inlineStyle => {
86
- this.onChange(_draftJs.RichUtils.toggleInlineStyle(this.props.editorState, inlineStyle));
87
- };
88
- getEntityType = editorState => {
89
- const contentState = editorState.getCurrentContent();
90
- const selection = editorState.getSelection();
91
- const startOffset = selection.getStartOffset();
92
- const startBlock = editorState.getCurrentContent().getBlockForKey(selection.getStartKey());
93
- const endOffset = selection.getEndOffset();
94
- let entityInstance;
95
- let entityKey;
96
- if (!selection.isCollapsed()) {
97
- entityKey = startBlock.getEntityAt(startOffset);
98
- } else {
99
- entityKey = startBlock.getEntityAt(endOffset);
100
- }
101
- if (entityKey !== null) {
102
- entityInstance = contentState.getEntity(entityKey);
103
- }
104
- let entityType = '';
105
- if (entityInstance) {
106
- entityType = entityInstance.getType();
107
- }
108
- return entityType;
109
- };
110
- toggleEnlarge = () => {
111
- this.setState({
112
- isEnlarged: !this.state.isEnlarged
113
- });
114
- };
115
- commonEditProps = {
116
- onEditStart: () => {
117
- this.setState({
118
- // If custom block renderer requires mouse interaction,
119
- // [Draft.js document](https://draftjs.org/docs/advanced-topics-block-components#recommendations-and-other-notes)
120
- // suggests that we should temporarily set Editor
121
- // to readOnly={true} during the interaction.
122
- // In readOnly={true} condition, the user does not
123
- // trigger any selection changes within the editor
124
- // while interacting with custom block.
125
- // If we don't set readOnly={true},
126
- // it will cause some subtle bugs in InfoBox button.
127
- readOnly: true
128
- });
129
- },
130
- onEditFinish: () => {
131
- this.setState({
132
- readOnly: false
133
- });
134
- }
135
- };
136
- customEditProps = {
137
- onEditStart: this.commonEditProps.onEditStart,
138
- onEditFinish: ({
139
- entityKey,
140
- entityData
141
- } = {}) => {
142
- if (entityKey && entityData) {
143
- const oldContentState = this.props.editorState.getCurrentContent();
144
- const newContentState = oldContentState.replaceEntityData(entityKey, entityData);
145
- this.onChange(_draftJs.EditorState.set(this.props.editorState, {
146
- currentContent: newContentState
147
- }));
148
- }
149
- this.commonEditProps.onEditFinish();
150
- }
151
- };
152
- blockRendererFn = block => {
153
- const atomicBlockObj = (0, _blockRendererFn.atomicBlockRenderer)(block);
154
- if (atomicBlockObj) {
155
- // `onEditStart` and `onEditFinish` will be passed
156
- // into custom block component.
157
- // We can get them via `props.blockProps.onEditStart`
158
- // and `props.blockProps.onEditFinish` in the custom block components.
159
- atomicBlockObj['props'] = {
160
- ...this.customEditProps,
161
- RichTextEditorComponent: RichTextEditor,
162
- decorator,
163
- getMainEditorReadOnly: () => this.state.readOnly
164
- };
165
- }
166
- return atomicBlockObj;
167
- };
168
- render() {
169
- const {
170
- disabledButtons = []
171
- } = this.props;
172
- let {
173
- editorState
174
- } = this.props;
175
- if (!(editorState instanceof _draftJs.EditorState)) {
176
- editorState = _draftJs.EditorState.createEmpty(this.editorDecorator);
177
- } else {
178
- editorState = _draftJs.EditorState.set(editorState, {
179
- decorator: this.editorDecorator
180
- });
181
- }
182
- const {
183
- isEnlarged,
184
- readOnly
185
- } = this.state;
186
- const entityType = this.getEntityType(editorState);
187
- const commonProps = {
188
- editorState: editorState,
189
- onChange: this.onChange,
190
- readOnly: this.state.readOnly
191
- };
192
- return /*#__PURE__*/_react.default.createElement(_styled.DraftEditorContainer, {
193
- isEnlarged: isEnlarged
194
- }, /*#__PURE__*/_react.default.createElement(_styled.DraftEditorWrapper, null, styleSource, /*#__PURE__*/_react.default.createElement(_styled.DraftEditorControls, null, /*#__PURE__*/_react.default.createElement(_styled.DraftEditorControlsWrapper, null, /*#__PURE__*/_react.default.createElement(_buttons.BlockStyleControls, {
195
- disabledButtons: disabledButtons,
196
- editorState: editorState,
197
- onToggle: this.toggleBlockType,
198
- readOnly: this.state.readOnly
199
- }), /*#__PURE__*/_react.default.createElement(_buttons.InlineStyleControls, {
200
- disabledButtons: disabledButtons,
201
- editorState: editorState,
202
- onToggle: this.toggleInlineStyle,
203
- readOnly: this.state.readOnly
204
- }), /*#__PURE__*/_react.default.createElement(_styled.EnlargeButtonWrapper, null, /*#__PURE__*/_react.default.createElement(_buttons.CustomEnlargeButton, {
205
- onToggle: this.toggleEnlarge,
206
- isEnlarged: isEnlarged
207
- })), /*#__PURE__*/_react.default.createElement(_buttons.CustomTOCAnchorButton, _extends({
208
- isDisabled: disabledButtons.includes(_btNames.default.tocAnchor),
209
- isActive: entityType === _draftRenderer.ENTITY.TOCAnchor
210
- }, commonProps)), /*#__PURE__*/_react.default.createElement(_buttons.CustomAnchorButton, _extends({
211
- isDisabled: disabledButtons.includes(_btNames.default.anchor),
212
- isActive: entityType === _draftRenderer.ENTITY.Anchor
213
- }, commonProps, this.commonEditProps)), /*#__PURE__*/_react.default.createElement(_buttons.CustomLinkButton, _extends({
214
- isDisabled: disabledButtons.includes(_btNames.default.link),
215
- isActive: entityType === _draftRenderer.ENTITY.Link
216
- }, commonProps, this.commonEditProps)), /*#__PURE__*/_react.default.createElement(_buttons.CustomBackgroundColorButton, _extends({
217
- isDisabled: disabledButtons.includes(_btNames.default.backgroundColor),
218
- isActive: editorState.getCurrentInlineStyle().find(styleName => typeof styleName === 'string' && styleName.startsWith(_bgColor.customStylePrefix)) !== undefined
219
- }, commonProps, this.commonEditProps)), /*#__PURE__*/_react.default.createElement(_buttons.CustomFontColorButton, _extends({
220
- isDisabled: disabledButtons.includes(_btNames.default.fontColor),
221
- isActive: editorState.getCurrentInlineStyle().find(styleName => typeof styleName === 'string' && styleName.startsWith(_fontColor.customStylePrefix)) !== undefined
222
- }, commonProps, this.commonEditProps)), /*#__PURE__*/_react.default.createElement(_buttons.CustomBlockquoteButton, _extends({
223
- isDisabled: disabledButtons.includes(_btNames.default.blockquote)
224
- }, commonProps)), /*#__PURE__*/_react.default.createElement(CustomAnnotationButton, _extends({
225
- isDisabled: disabledButtons.includes(_btNames.default.annotation),
226
- isActive: entityType === _draftRenderer.ENTITY.Annotation
227
- }, commonProps, this.commonEditProps)), /*#__PURE__*/_react.default.createElement(_buttons.CustomImageButton, _extends({
228
- isDisabled: disabledButtons.includes(_btNames.default.image),
229
- ImageSelector: _imageSelector.ImageSelector
230
- }, commonProps)), /*#__PURE__*/_react.default.createElement(_buttons.CustomSlideshowButton, _extends({
231
- isDisabled: disabledButtons.includes(_btNames.default.slideshow),
232
- ImageSelector: _imageSelector.ImageSelector
233
- }, commonProps)), /*#__PURE__*/_react.default.createElement(CustomInfoBoxButton, _extends({
234
- isDisabled: disabledButtons.includes(_btNames.default.infoBox)
235
- }, commonProps)), /*#__PURE__*/_react.default.createElement(_buttons.CustomEmbeddedCodeButton, _extends({
236
- isDisabled: disabledButtons.includes(_btNames.default.embed)
237
- }, commonProps)), /*#__PURE__*/_react.default.createElement(_buttons.CustomNewsReadingButton, _extends({
238
- isDisabled: disabledButtons.includes(_btNames.default.newsReading)
239
- }, commonProps)), /*#__PURE__*/_react.default.createElement(_buttons.CustomDividerButton, _extends({
240
- isDisabled: disabledButtons.includes(_btNames.default.divider)
241
- }, commonProps)))), /*#__PURE__*/_react.default.createElement(_styled.TextEditorWrapper, {
242
- onClick: () => {
243
- if (this.editorRef) {
244
- var _this$editorRef;
245
- // eslint-disable-next-line prettier/prettier
246
- (_this$editorRef = this.editorRef) === null || _this$editorRef === void 0 || _this$editorRef.focus();
247
- }
248
- }
249
- }, /*#__PURE__*/_react.default.createElement(_draftJs.Editor, {
250
- blockRenderMap: _draftRenderer.blockRenderMap,
251
- blockRendererFn: this.blockRendererFn,
252
- customStyleFn: _draftRenderer.customStyleFn,
253
- editorState: editorState,
254
- handleKeyCommand: this.handleKeyCommand,
255
- handleReturn: this.handleReturn,
256
- keyBindingFn: this.mapKeyToEditorCommand,
257
- onChange: this.onChange,
258
- placeholder: "Tell a story...",
259
- ref: this.editorRef,
260
- spellCheck: true,
261
- readOnly: readOnly
262
- }))));
263
- }
264
- }
265
- exports.RichTextEditor = RichTextEditor;
266
- const AnnotationButton = (0, _annotation.createAnnotationButton)({
267
- InnerEditor: RichTextEditor,
268
- decorator
269
- });
270
- const InfoBoxButton = (0, _infoBox.createInfoBoxButton)({
271
- InnerEditor: RichTextEditor,
272
- decorator
273
- });
274
- const CustomAnnotationButton = (0, _buttons.withStyle)(AnnotationButton);
275
- const CustomInfoBoxButton = (0, _buttons.withStyle)(InfoBoxButton);
276
- var _default = exports.default = {
277
- RichTextEditor,
278
- decorator
279
- };
@@ -1,5 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });