@seafile/comment-editor 0.0.1-alpha.75 → 0.0.1-alpha.76

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.
@@ -30,11 +30,10 @@ var _slateToMd = _interopRequireDefault(require("../slate-convert/slate-to-md"))
30
30
  var _eventBus = _interopRequireDefault(require("../utils/event-bus"));
31
31
  var _eventHandler = _interopRequireDefault(require("../utils/event-handler"));
32
32
  require("./comment-editor.css");
33
- // const getSubmitTip = (type, content) => {
34
- // if (content) return 'Save';
35
- // return type === 'comment' ? 'Comment' : 'Reply';
36
- // };
37
-
33
+ const getSubmitTip = (type, content) => {
34
+ if (content) return 'Save';
35
+ return type === 'comment' ? 'Comment' : 'Reply';
36
+ };
38
37
  const DEFAULT_PLACEHOLDER = 'Enter_comment_shift_enter_for_new_line_Enter_to_send';
39
38
  const CommentEditor = _ref => {
40
39
  let {
@@ -47,223 +46,235 @@ const CommentEditor = _ref => {
47
46
  toolMenus = ['text_style', _constants2.BLOCKQUOTE, _constants2.UNORDERED_LIST, _constants2.ORDERED_LIST, _constants2.LINK, _constants2.IMAGE],
48
47
  closePanel
49
48
  } = _ref;
50
- // const commentWrapperRef = useRef();
49
+ const commentWrapperRef = (0, _react.useRef)();
51
50
  const {
52
51
  t
53
52
  } = (0, _reactI18next.useTranslation)('sdoc-editor');
54
- // const { className, userInfo, type, addParticipants } = useComment();
55
- // const submitTip = useMemo(() => getSubmitTip(type, content), [content, type]);
56
-
57
- // const document = useMemo(() => {
58
- // const cursor = {};
59
- // let elements = null;
60
- // elements = [generateEmptyElement(PARAGRAPH, { placeholder })];
61
- // return { elements, cursor };
62
- // }, [placeholder]);
63
-
64
- // const [slateValue, setSlateValue] = useState(document.elements);
65
- // const commentEditorContainerRef = useRef(null);
66
-
67
- // const editor = useMemo(() => {
68
- // const defaultEditor = createCommentEditor();
69
- // const newEditor = withNodeId(defaultEditor);
70
- // const { cursors } = document;
71
- // newEditor.cursors = cursors || {};
72
- // newEditor.width = COMMENT_EDITOR_EDIT_AREA_WIDTH; // default width
73
- // newEditor.editorType = COMMENT_EDITOR;
74
-
75
- // return newEditor;
76
- // }, [document]);
77
-
78
- // const { cursors } = useCursors(editor);
79
- // const decorate = usePipDecorate(editor);
80
- // // init eventHandler
81
- // // eslint-disable-next-line react-hooks/exhaustive-deps
82
- // const eventProxy = useMemo(() => new EventProxy(editor), []);
83
-
84
- // const onSubmit = useCallback((event) => {
85
- // event && event.stopPropagation();
86
-
87
- // const mdString = slateToMdString(editor.children);
88
- // if (mdString && mdString.trim()) insertContent(mdString);
89
-
90
- // addParticipants && addParticipants(userInfo.username);
91
-
92
- // editor.children = [generateEmptyElement(PARAGRAPH, { placeholder })];
93
- // Transforms.select(editor, Editor.start(editor, []));
94
-
95
- // onContentChange && onContentChange(null);
96
- // closePanel && closePanel();
97
- // // eslint-disable-next-line react-hooks/exhaustive-deps
98
- // }, [editor, insertContent, addParticipants, placeholder, onContentChange, closePanel]);
99
-
100
- // const onSubmitByEnterKey = useCallback((event) => {
101
- // if (!ReactEditor.isFocused(editor)) return;
102
- // onSubmit(event);
103
- // }, [editor, onSubmit]);
104
-
105
- // // addEventListener
106
- // useEffect(() => {
107
- // const eventBus = EventBus.getInstance();
108
- // const unsubscribePostComment = eventBus.subscribe(INTERNAL_EVENT.COMMENT_EDITOR_POST_COMMENT, onSubmitByEnterKey);
109
-
110
- // return () => {
111
- // unsubscribePostComment();
112
- // };
113
- // }, [onSubmitByEnterKey]);
114
-
115
- // const onCancel = useCallback((event) => {
116
- // event.stopPropagation();
117
- // const { type: eventType, keyCode, target } = event;
118
- // if (eventType === 'keydown' && keyCode !== KeyCodes.Esc) return;
119
- // if (eventType === 'click') {
120
- // const isSdocContentWrapper = target.classList.contains('sdoc-content-wrapper');
121
- // const listContainer = window.document.querySelector('#global-comment-list-container');
122
- // const resizeContainer = window.document.querySelector('.sdoc-comment-resize-handler');
123
- // const isClickOnListContainer = listContainer && listContainer.contains(target);
124
- // const isClickOnCommentEditorContainer = commentWrapperRef.current.contains(target);
125
- // const isClickResizeContainer = resizeContainer && resizeContainer.contains(target);
126
- // const isPreventCancel = isClickOnListContainer || isClickOnCommentEditorContainer || isClickResizeContainer || isSdocContentWrapper;
127
- // if (isPreventCancel) return;
128
- // }
129
- // hiddenComment && hiddenComment(false);
130
-
131
- // if (onContentChange) {
132
- // if (editor.children.find(n => Node.string(n).trim())) {
133
- // onContentChange(slateToMdString(editor.children));
134
- // } else {
135
- // onContentChange(null);
136
- // }
137
- // }
138
- // // eslint-disable-next-line react-hooks/exhaustive-deps
139
- // }, []);
140
-
141
- // // set editor children
142
- // useEffect(() => {
143
- // let children = mdStringToSlate(content);
144
- // editor.children = children;
145
- // // Transforms.select(editor, Editor.end(editor, []));
146
- // }, [editor, content]);
147
-
148
- // // useMount: focus editor
149
- // useEffect(() => {
150
- // const [firstNode] = editor.children;
151
- // if (firstNode) {
152
- // const [firstNodeFirstChild] = firstNode.children;
153
-
154
- // if (firstNodeFirstChild) {
155
- // const endOfFirstNode = Editor.end(editor, [0, 0]);
156
- // const range = {
157
- // anchor: endOfFirstNode,
158
- // focus: endOfFirstNode,
159
- // };
160
- // // focusEditor(editor, range);
161
- // }
162
- // // Force refresh to fix comment list
163
- // // setSlateValue([...editor.children]);
164
- // }
165
- // // eslint-disable-next-line react-hooks/exhaustive-deps
166
- // }, []);
167
-
168
- // const handleFocusEditor = (e) => {
169
- // if (e.target === commentEditorContainerRef.current) {
170
- // const focusPoint = Editor.end(editor, []);
171
- // focusEditor(editor, focusPoint);
172
- // }
173
- // };
174
-
175
- // const handleScrollIntoView = useCallback((editor, domRange) => {
176
- // try {
177
- // const { selection } = editor;
178
- // // Do not scroll into view, when focus on image
179
- // const [imageNodeEntry] = Editor.nodes(editor, {
180
- // match: n => [IMAGE, IMAGE_BLOCK].includes(n.type),
181
- // at: selection
182
- // });
183
- // if (imageNodeEntry) return;
184
- // const focusedNode = Node.get(editor, selection.focus.path);
185
- // const domNode = ReactEditor.toDOMNode(editor, focusedNode);
186
- // if (!domNode) return;
187
- // scrollIntoView(domNode, { 'scrollMode': 'if-needed' });
188
- // } catch (error) {
189
- // //
190
- // }
191
- // }, []);
192
-
193
- // const onMouseDown = useCallback((event) => {
194
- // if (event.button === 0) {
195
- // // Compatible with the editor which unload table plugin
196
- // editor.reSetTableSelectedRange && editor.reSetTableSelectedRange();
197
- // const eventBus = EventBus.getInstance();
198
- // eventBus.dispatch(INTERNAL_EVENT.CANCEL_TABLE_SELECT_RANGE);
199
- // }
200
- // // eslint-disable-next-line react-hooks/exhaustive-deps
201
- // }, []);
202
-
203
- // const onKeyDown = useCallback((event) => {
204
- // if (isHotkey('enter', event)) {
205
- // event.preventDefault();
206
- // const eventBus = EventBus.getInstance();
207
- // eventBus.dispatch(INTERNAL_EVENT.COMMENT_EDITOR_POST_COMMENT, event);
208
- // return;
209
- // }
210
-
211
- // if (isHotkey('shift+enter', event)) {
212
- // event.preventDefault();
213
- // Editor.insertBreak(editor);
214
- // return;
215
- // }
53
+ const {
54
+ className,
55
+ userInfo,
56
+ type,
57
+ addParticipants
58
+ } = (0, _useComment.useComment)();
59
+ const submitTip = (0, _react.useMemo)(() => getSubmitTip(type, content), [content, type]);
60
+ const document = (0, _react.useMemo)(() => {
61
+ const cursor = {};
62
+ let elements = null;
63
+ elements = [(0, _core.generateEmptyElement)(_constants2.PARAGRAPH, {
64
+ placeholder
65
+ })];
66
+ return {
67
+ elements,
68
+ cursor
69
+ };
70
+ }, [placeholder]);
71
+ const [slateValue, setSlateValue] = (0, _react.useState)(document.elements);
72
+ const commentEditorContainerRef = (0, _react.useRef)(null);
73
+ const editor = (0, _react.useMemo)(() => {
74
+ const defaultEditor = (0, _extension.createCommentEditor)();
75
+ const newEditor = (0, _nodeId.default)(defaultEditor);
76
+ const {
77
+ cursors
78
+ } = document;
79
+ newEditor.cursors = cursors || {};
80
+ newEditor.width = _constants.COMMENT_EDITOR_EDIT_AREA_WIDTH; // default width
81
+ newEditor.editorType = _constants.COMMENT_EDITOR;
82
+ return newEditor;
83
+ }, [document]);
84
+ const {
85
+ cursors
86
+ } = (0, _useCursors.useCursors)(editor);
87
+ const decorate = (0, _decorates.usePipDecorate)(editor);
88
+ // init eventHandler
89
+ // eslint-disable-next-line react-hooks/exhaustive-deps
90
+ const eventProxy = (0, _react.useMemo)(() => new _eventHandler.default(editor), []);
91
+ const onSubmit = (0, _react.useCallback)(event => {
92
+ event && event.stopPropagation();
93
+ const mdString = (0, _slateToMd.default)(editor.children);
94
+ if (mdString && mdString.trim()) insertContent(mdString);
95
+ addParticipants && addParticipants(userInfo.username);
96
+ editor.children = [(0, _core.generateEmptyElement)(_constants2.PARAGRAPH, {
97
+ placeholder
98
+ })];
99
+ _slate.Transforms.select(editor, _slate.Editor.start(editor, []));
100
+ onContentChange && onContentChange(null);
101
+ closePanel && closePanel();
102
+ // eslint-disable-next-line react-hooks/exhaustive-deps
103
+ }, [editor, insertContent, addParticipants, placeholder, onContentChange, closePanel]);
104
+ const onSubmitByEnterKey = (0, _react.useCallback)(event => {
105
+ if (!_slateReact.ReactEditor.isFocused(editor)) return;
106
+ onSubmit(event);
107
+ }, [editor, onSubmit]);
216
108
 
217
- // eventProxy.onKeyDown(event);
109
+ // addEventListener
110
+ (0, _react.useEffect)(() => {
111
+ const eventBus = _eventBus.default.getInstance();
112
+ const unsubscribePostComment = eventBus.subscribe(_constants.INTERNAL_EVENT.COMMENT_EDITOR_POST_COMMENT, onSubmitByEnterKey);
113
+ return () => {
114
+ unsubscribePostComment();
115
+ };
116
+ }, [onSubmitByEnterKey]);
117
+ const onCancel = (0, _react.useCallback)(event => {
118
+ event.stopPropagation();
119
+ const {
120
+ type: eventType,
121
+ keyCode,
122
+ target
123
+ } = event;
124
+ if (eventType === 'keydown' && keyCode !== _constants.KeyCodes.Esc) return;
125
+ if (eventType === 'click') {
126
+ const isSdocContentWrapper = target.classList.contains('sdoc-content-wrapper');
127
+ const listContainer = window.document.querySelector('#global-comment-list-container');
128
+ const resizeContainer = window.document.querySelector('.sdoc-comment-resize-handler');
129
+ const isClickOnListContainer = listContainer && listContainer.contains(target);
130
+ const isClickOnCommentEditorContainer = commentWrapperRef.current.contains(target);
131
+ const isClickResizeContainer = resizeContainer && resizeContainer.contains(target);
132
+ const isPreventCancel = isClickOnListContainer || isClickOnCommentEditorContainer || isClickResizeContainer || isSdocContentWrapper;
133
+ if (isPreventCancel) return;
134
+ }
135
+ hiddenComment && hiddenComment(false);
136
+ if (onContentChange) {
137
+ if (editor.children.find(n => _slate.Node.string(n).trim())) {
138
+ onContentChange((0, _slateToMd.default)(editor.children));
139
+ } else {
140
+ onContentChange(null);
141
+ }
142
+ }
143
+ // eslint-disable-next-line react-hooks/exhaustive-deps
144
+ }, []);
218
145
 
219
- // }, [eventProxy, editor]);
146
+ // set editor children
147
+ (0, _react.useEffect)(() => {
148
+ let children = (0, _mdToSlate.default)(content);
149
+ editor.children = children;
150
+ // Transforms.select(editor, Editor.end(editor, []));
151
+ }, [editor, content]);
220
152
 
153
+ // useMount: focus editor
154
+ (0, _react.useEffect)(() => {
155
+ const [firstNode] = editor.children;
156
+ if (firstNode) {
157
+ const [firstNodeFirstChild] = firstNode.children;
158
+ if (firstNodeFirstChild) {
159
+ const endOfFirstNode = _slate.Editor.end(editor, [0, 0]);
160
+ const range = {
161
+ anchor: endOfFirstNode,
162
+ focus: endOfFirstNode
163
+ };
164
+ // focusEditor(editor, range);
165
+ }
166
+ // Force refresh to fix comment list
167
+ // setSlateValue([...editor.children]);
168
+ }
169
+ // eslint-disable-next-line react-hooks/exhaustive-deps
170
+ }, []);
171
+ const handleFocusEditor = e => {
172
+ if (e.target === commentEditorContainerRef.current) {
173
+ const focusPoint = _slate.Editor.end(editor, []);
174
+ (0, _core.focusEditor)(editor, focusPoint);
175
+ }
176
+ };
177
+ const handleScrollIntoView = (0, _react.useCallback)((editor, domRange) => {
178
+ try {
179
+ const {
180
+ selection
181
+ } = editor;
182
+ // Do not scroll into view, when focus on image
183
+ const [imageNodeEntry] = _slate.Editor.nodes(editor, {
184
+ match: n => [_constants2.IMAGE, _constants2.IMAGE_BLOCK].includes(n.type),
185
+ at: selection
186
+ });
187
+ if (imageNodeEntry) return;
188
+ const focusedNode = _slate.Node.get(editor, selection.focus.path);
189
+ const domNode = _slateReact.ReactEditor.toDOMNode(editor, focusedNode);
190
+ if (!domNode) return;
191
+ (0, _scrollIntoViewIfNeeded.default)(domNode, {
192
+ 'scrollMode': 'if-needed'
193
+ });
194
+ } catch (error) {
195
+ //
196
+ }
197
+ }, []);
198
+ const onMouseDown = (0, _react.useCallback)(event => {
199
+ if (event.button === 0) {
200
+ // Compatible with the editor which unload table plugin
201
+ editor.reSetTableSelectedRange && editor.reSetTableSelectedRange();
202
+ const eventBus = _eventBus.default.getInstance();
203
+ eventBus.dispatch(_constants.INTERNAL_EVENT.CANCEL_TABLE_SELECT_RANGE);
204
+ }
205
+ // eslint-disable-next-line react-hooks/exhaustive-deps
206
+ }, []);
207
+ const onKeyDown = (0, _react.useCallback)(event => {
208
+ if ((0, _isHotkey.default)('enter', event)) {
209
+ event.preventDefault();
210
+ const eventBus = _eventBus.default.getInstance();
211
+ eventBus.dispatch(_constants.INTERNAL_EVENT.COMMENT_EDITOR_POST_COMMENT, event);
212
+ return;
213
+ }
214
+ if ((0, _isHotkey.default)('shift+enter', event)) {
215
+ event.preventDefault();
216
+ _slate.Editor.insertBreak(editor);
217
+ return;
218
+ }
219
+ eventProxy.onKeyDown(event);
220
+ }, [eventProxy, editor]);
221
221
  console.log('toolMenus', toolMenus);
222
- return /*#__PURE__*/_react.default.createElement("div", null, "123")
223
- // <div className={classNames('comment-editor-wrapper', className)} ref={commentWrapperRef}>
224
- // {type === 'comment' && !hiddenUserInfo && (
225
- // <div className="comment-editor-user-info">
226
- // <div className="comment-editor-user-img">
227
- // <img src={userInfo.avatar_url} alt="" height="100%" width="100%" />
228
- // </div>
229
- // <div className="comment-editor-user-name">{userInfo.name}</div>
230
- // </div>
231
- // )}
232
- // <div className='comment-editor-container'>
233
- // <div className="comment-editor-content">
234
- // <div ref={commentEditorContainerRef} className='article comment-editor' onClick={handleFocusEditor} >
235
- // <ScrollContext.Provider value={{ scrollRef: commentEditorContainerRef }}>
236
- // <Slate editor={editor} value={slateValue} onChange={setSlateValue}>
237
- // <Editable
238
- // id='sdoc-editor'
239
- // scrollSelectionIntoView={handleScrollIntoView}
240
- // cursors={cursors}
241
- // renderElement={(props) => RenderCommentEditorCustomRenderElement({ ...props, commentType: type })}
242
- // renderLeaf={renderLeaf}
243
- // onMouseDown={onMouseDown}
244
- // decorate={decorate}
245
- // onCut={eventProxy.onCut}
246
- // onCopy={eventProxy.onCopy}
247
- // onCompositionStart={eventProxy.onCompositionStart}
248
- // onCompositionUpdate={eventProxy.onCompositionUpdate}
249
- // onCompositionEnd={eventProxy.onCompositionEnd}
250
- // onKeyDown={onKeyDown}
251
- // onBeforeInput={eventProxy.onBeforeInput}
252
- // />
253
- // </Slate>
254
- // </ScrollContext.Provider>
255
- // </div>
256
- // <CommentEditorToolbar
257
- // editor={editor}
258
- // toolMenus={toolMenus}
259
- // onSubmit={onSubmit}
260
- // submitBtnText={t(submitTip)}
261
- // onCancel={onCancel}
262
- // />
263
- // </div>
264
- // </div>
265
- // {toolMenus.includes(IMAGE) && <InsertElementDialog editor={editor} />}
266
- // </div>
267
- ;
222
+ return /*#__PURE__*/_react.default.createElement("div", {
223
+ className: (0, _classnames.default)('comment-editor-wrapper', className),
224
+ ref: commentWrapperRef
225
+ }, type === 'comment' && !hiddenUserInfo && /*#__PURE__*/_react.default.createElement("div", {
226
+ className: "comment-editor-user-info"
227
+ }, /*#__PURE__*/_react.default.createElement("div", {
228
+ className: "comment-editor-user-img"
229
+ }, /*#__PURE__*/_react.default.createElement("img", {
230
+ src: userInfo.avatar_url,
231
+ alt: "",
232
+ height: "100%",
233
+ width: "100%"
234
+ })), /*#__PURE__*/_react.default.createElement("div", {
235
+ className: "comment-editor-user-name"
236
+ }, userInfo.name)), /*#__PURE__*/_react.default.createElement("div", {
237
+ className: "comment-editor-container"
238
+ }, /*#__PURE__*/_react.default.createElement("div", {
239
+ className: "comment-editor-content"
240
+ }, /*#__PURE__*/_react.default.createElement("div", {
241
+ ref: commentEditorContainerRef,
242
+ className: "article comment-editor",
243
+ onClick: handleFocusEditor
244
+ }, /*#__PURE__*/_react.default.createElement(_useScrollContext.ScrollContext.Provider, {
245
+ value: {
246
+ scrollRef: commentEditorContainerRef
247
+ }
248
+ }, /*#__PURE__*/_react.default.createElement(_slateReact.Slate, {
249
+ editor: editor,
250
+ value: slateValue,
251
+ onChange: setSlateValue
252
+ }, /*#__PURE__*/_react.default.createElement(_slateReact.Editable, {
253
+ id: "sdoc-editor",
254
+ scrollSelectionIntoView: handleScrollIntoView,
255
+ cursors: cursors,
256
+ renderElement: props => (0, _renderCommentEditorElement.default)({
257
+ ...props,
258
+ commentType: type
259
+ }),
260
+ renderLeaf: _extension.renderLeaf,
261
+ onMouseDown: onMouseDown,
262
+ decorate: decorate,
263
+ onCut: eventProxy.onCut,
264
+ onCopy: eventProxy.onCopy,
265
+ onCompositionStart: eventProxy.onCompositionStart,
266
+ onCompositionUpdate: eventProxy.onCompositionUpdate,
267
+ onCompositionEnd: eventProxy.onCompositionEnd,
268
+ onKeyDown: onKeyDown,
269
+ onBeforeInput: eventProxy.onBeforeInput
270
+ })))), /*#__PURE__*/_react.default.createElement(_commentEditorToolbar.default, {
271
+ editor: editor,
272
+ toolMenus: toolMenus,
273
+ onSubmit: onSubmit,
274
+ submitBtnText: t(submitTip),
275
+ onCancel: onCancel
276
+ }))), toolMenus.includes(_constants2.IMAGE) && /*#__PURE__*/_react.default.createElement(_insertElementDialog.default, {
277
+ editor: editor
278
+ }));
268
279
  };
269
280
  var _default = exports.default = CommentEditor;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seafile/comment-editor",
3
- "version": "0.0.1-alpha.75",
3
+ "version": "0.0.1-alpha.76",
4
4
  "private": false,
5
5
  "description": "This is a comment editor",
6
6
  "main": "dist/index.js",