@seafile/sdoc-editor 0.1.60 → 0.1.62
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/dist/api/sdoc-server-api.js +52 -0
- package/dist/basic-sdk/assets/css/layout.css +32 -0
- package/dist/basic-sdk/comment/comment-context-provider.js +27 -0
- package/dist/basic-sdk/comment/comment-decorate.js +29 -0
- package/dist/basic-sdk/comment/comment-editor.js +73 -0
- package/dist/basic-sdk/comment/comment-item.js +93 -0
- package/dist/basic-sdk/comment/comment-list.js +128 -0
- package/dist/basic-sdk/comment/comment.js +64 -0
- package/dist/basic-sdk/comment/helper.js +44 -0
- package/dist/basic-sdk/comment/hooks/use-comment-context.js +11 -0
- package/dist/basic-sdk/comment/hooks/use-comment-mount.js +55 -0
- package/dist/basic-sdk/comment/index.js +12 -0
- package/dist/basic-sdk/comment/reducer/comment-reducer.js +82 -0
- package/dist/basic-sdk/comment/style.css +137 -0
- package/dist/basic-sdk/decorates/index.js +21 -0
- package/dist/basic-sdk/editor.js +23 -12
- package/dist/basic-sdk/extension/constants/index.js +8 -0
- package/dist/basic-sdk/extension/core/utils/index.js +7 -4
- package/dist/basic-sdk/extension/plugins/code-block/helpers.js +6 -3
- package/dist/basic-sdk/extension/plugins/code-block/render-elem.js +7 -2
- package/dist/basic-sdk/extension/plugins/table/constants/index.js +0 -2
- package/dist/basic-sdk/extension/plugins/table/helpers.js +13 -11
- package/dist/basic-sdk/extension/plugins/table/render/render-cell.js +1 -0
- package/dist/basic-sdk/extension/plugins/table/render/table-root.js +15 -10
- package/dist/basic-sdk/extension/plugins/text-style/render-elem.js +15 -7
- package/dist/basic-sdk/extension/render/render-element.js +17 -2
- package/dist/basic-sdk/extension/toolbar/index.js +3 -16
- package/dist/basic-sdk/highlight-decorate/index.js +5 -11
- package/dist/basic-sdk/hooks/use-scroll-context.js +10 -0
- package/dist/basic-sdk/hooks/use-selection-element.js +20 -0
- package/dist/basic-sdk/hooks/use-selection-position.js +34 -0
- package/dist/basic-sdk/hooks/use-selection-update.js +18 -0
- package/dist/basic-sdk/utils/diff-text.js +255 -0
- package/dist/basic-sdk/utils/diff.js +229 -142
- package/dist/basic-sdk/utils/document-utils.js +18 -0
- package/dist/basic-sdk/viewer.js +48 -66
- package/dist/constants/index.js +2 -1
- package/dist/context.js +20 -0
- package/dist/pages/diff-viewer/diff-viewer.js +43 -114
- package/dist/pages/diff-viewer/history-version-viewer.js +6 -1
- package/package.json +2 -2
|
@@ -58,6 +58,58 @@ var SDocServerApi = /*#__PURE__*/function () {
|
|
|
58
58
|
}
|
|
59
59
|
});
|
|
60
60
|
}
|
|
61
|
+
}, {
|
|
62
|
+
key: "listComments",
|
|
63
|
+
value: function listComments() {
|
|
64
|
+
var server = this.server,
|
|
65
|
+
docUuid = this.docUuid,
|
|
66
|
+
accessToken = this.accessToken;
|
|
67
|
+
var url = "".concat(server, "/api/v1/docs/").concat(docUuid, "/comment/");
|
|
68
|
+
return axios.get(url, {
|
|
69
|
+
headers: {
|
|
70
|
+
Authorization: "Token ".concat(accessToken)
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
}, {
|
|
75
|
+
key: "insertComment",
|
|
76
|
+
value: function insertComment(comment) {
|
|
77
|
+
var server = this.server,
|
|
78
|
+
docUuid = this.docUuid,
|
|
79
|
+
accessToken = this.accessToken;
|
|
80
|
+
var url = "".concat(server, "/api/v1/docs/").concat(docUuid, "/comment/");
|
|
81
|
+
return axios.post(url, comment, {
|
|
82
|
+
headers: {
|
|
83
|
+
Authorization: "Token ".concat(accessToken)
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
}, {
|
|
88
|
+
key: "deleteComment",
|
|
89
|
+
value: function deleteComment(commentId) {
|
|
90
|
+
var server = this.server,
|
|
91
|
+
docUuid = this.docUuid,
|
|
92
|
+
accessToken = this.accessToken;
|
|
93
|
+
var url = "".concat(server, "/api/v1/docs/").concat(docUuid, "/comment/").concat(commentId, "/");
|
|
94
|
+
return axios.delete(url, {
|
|
95
|
+
headers: {
|
|
96
|
+
Authorization: "Token ".concat(accessToken)
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}, {
|
|
101
|
+
key: "updateComment",
|
|
102
|
+
value: function updateComment(commentId, newComment) {
|
|
103
|
+
var server = this.server,
|
|
104
|
+
docUuid = this.docUuid,
|
|
105
|
+
accessToken = this.accessToken;
|
|
106
|
+
var url = "".concat(server, "/api/v1/docs/").concat(docUuid, "/comment/").concat(commentId, "/");
|
|
107
|
+
return axios.put(url, newComment, {
|
|
108
|
+
headers: {
|
|
109
|
+
Authorization: "Token ".concat(accessToken)
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
}
|
|
61
113
|
}]);
|
|
62
114
|
return SDocServerApi;
|
|
63
115
|
}();
|
|
@@ -23,6 +23,12 @@
|
|
|
23
23
|
overflow: auto;
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
+
.sdoc-editor-container .sdoc-editor-article-container {
|
|
27
|
+
flex: 1 1 auto;
|
|
28
|
+
overflow: auto;
|
|
29
|
+
position: relative;
|
|
30
|
+
}
|
|
31
|
+
|
|
26
32
|
.sdoc-editor-container .sdoc-editor-content .article {
|
|
27
33
|
width: 794px;
|
|
28
34
|
min-height: calc(100% - 40px);
|
|
@@ -36,3 +42,29 @@
|
|
|
36
42
|
.sdoc-editor-container .sdoc-editor-content .article > div {
|
|
37
43
|
caret-color: blue;
|
|
38
44
|
}
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
.sdoc-editor-container .seafile-block-container {
|
|
48
|
+
position: relative;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
.sdoc-editor-container .seafile-block-container .comment-count {
|
|
52
|
+
position: absolute;
|
|
53
|
+
right: -55px;
|
|
54
|
+
top: 0px;
|
|
55
|
+
border-radius: 4px;
|
|
56
|
+
width: 15px;
|
|
57
|
+
height: 15px;
|
|
58
|
+
line-height: 18px;
|
|
59
|
+
pointer-events: none;
|
|
60
|
+
display: flex;
|
|
61
|
+
align-items: center;
|
|
62
|
+
justify-content: center;
|
|
63
|
+
color: #fff;
|
|
64
|
+
background: rgba(0, 0, 0, .3);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
.sdoc-editor-container .seafile-block-container .comment-count__btn {
|
|
68
|
+
font-size: 12px;
|
|
69
|
+
transform: scale(.75);
|
|
70
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
2
|
+
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
|
3
|
+
import React, { useEffect, useReducer } from 'react';
|
|
4
|
+
import { commentReducer, initCommentsInfo } from './reducer/comment-reducer';
|
|
5
|
+
import { useCommentsMount } from './hooks/use-comment-mount';
|
|
6
|
+
import { CommentContext } from './hooks/use-comment-context';
|
|
7
|
+
import { useSlateStatic } from '@seafile/slate-react';
|
|
8
|
+
var CommentContextProvider = function CommentContextProvider(_ref) {
|
|
9
|
+
var children = _ref.children;
|
|
10
|
+
var _useReducer = useReducer(commentReducer, initCommentsInfo),
|
|
11
|
+
_useReducer2 = _slicedToArray(_useReducer, 2),
|
|
12
|
+
commentsInfo = _useReducer2[0],
|
|
13
|
+
dispatch = _useReducer2[1];
|
|
14
|
+
useCommentsMount(dispatch);
|
|
15
|
+
var editor = useSlateStatic();
|
|
16
|
+
useEffect(function () {
|
|
17
|
+
editor.comments_map = _objectSpread({}, commentsInfo.comments_map);
|
|
18
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
19
|
+
}, [commentsInfo]);
|
|
20
|
+
return /*#__PURE__*/React.createElement(CommentContext.Provider, {
|
|
21
|
+
value: {
|
|
22
|
+
commentsInfo: commentsInfo,
|
|
23
|
+
dispatch: dispatch
|
|
24
|
+
}
|
|
25
|
+
}, children);
|
|
26
|
+
};
|
|
27
|
+
export default CommentContextProvider;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
|
2
|
+
import { Node } from '@seafile/slate';
|
|
3
|
+
export var commentDecorate = function commentDecorate(editor) {
|
|
4
|
+
return function (_ref) {
|
|
5
|
+
var _editor$comments_map;
|
|
6
|
+
var _ref2 = _slicedToArray(_ref, 2),
|
|
7
|
+
node = _ref2[0],
|
|
8
|
+
path = _ref2[1];
|
|
9
|
+
var decorations = [];
|
|
10
|
+
var comments = ((_editor$comments_map = editor.comments_map) === null || _editor$comments_map === void 0 ? void 0 : _editor$comments_map[node.id]) || [];
|
|
11
|
+
if (comments && comments.length > 0) {
|
|
12
|
+
var decoration = {
|
|
13
|
+
anchor: {
|
|
14
|
+
path: path,
|
|
15
|
+
offset: 0
|
|
16
|
+
},
|
|
17
|
+
focus: {
|
|
18
|
+
path: path,
|
|
19
|
+
offset: Node.string(node).length
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
// rgba prevents occlusion of the cursor
|
|
23
|
+
decoration['bg_color'] = 'rgba(129, 237, 247, 0.5)';
|
|
24
|
+
decoration['comment_count'] = editor.comments_map[node.id].length;
|
|
25
|
+
decorations.push(decoration);
|
|
26
|
+
}
|
|
27
|
+
return decorations;
|
|
28
|
+
};
|
|
29
|
+
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
2
|
+
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
|
3
|
+
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
4
|
+
import dayjs from 'dayjs';
|
|
5
|
+
import context from '../../context';
|
|
6
|
+
var CommentEditor = function CommentEditor(_ref) {
|
|
7
|
+
var comment = _ref.comment,
|
|
8
|
+
placeholder = _ref.placeholder,
|
|
9
|
+
selectionElement = _ref.selectionElement,
|
|
10
|
+
insertComment = _ref.insertComment,
|
|
11
|
+
updateComment = _ref.updateComment;
|
|
12
|
+
var user = useMemo(function () {
|
|
13
|
+
return context.getUserInfo();
|
|
14
|
+
}, []);
|
|
15
|
+
var _useState = useState(comment ? comment.comment : ''),
|
|
16
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
17
|
+
value = _useState2[0],
|
|
18
|
+
setValue = _useState2[1];
|
|
19
|
+
var onChange = useCallback(function (event) {
|
|
20
|
+
setValue(event.target.value);
|
|
21
|
+
}, []);
|
|
22
|
+
var updateValue = useCallback(function (value, comment) {
|
|
23
|
+
if (!value || value.trim() === '') return;
|
|
24
|
+
var time = dayjs().format('YYYY-MM-DD HH:mm:ss');
|
|
25
|
+
var elementId = selectionElement === null || selectionElement === void 0 ? void 0 : selectionElement.id;
|
|
26
|
+
if (!comment) {
|
|
27
|
+
var _comment = {
|
|
28
|
+
comment: value,
|
|
29
|
+
detail: {
|
|
30
|
+
element_id: elementId,
|
|
31
|
+
comment: value
|
|
32
|
+
},
|
|
33
|
+
author: user.username,
|
|
34
|
+
updated_at: time
|
|
35
|
+
};
|
|
36
|
+
insertComment(elementId, _comment);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
var newComment = {
|
|
40
|
+
comment: value,
|
|
41
|
+
detail: _objectSpread(_objectSpread({}, comment.detail), {}, {
|
|
42
|
+
comment: value
|
|
43
|
+
}),
|
|
44
|
+
updated_at: time
|
|
45
|
+
};
|
|
46
|
+
var eleId = newComment.detail.element_id;
|
|
47
|
+
updateComment(eleId, comment.id, newComment);
|
|
48
|
+
}, [insertComment, selectionElement, updateComment, user.username]);
|
|
49
|
+
var onBlur = useCallback(function () {
|
|
50
|
+
updateValue(value, comment);
|
|
51
|
+
}, [comment, updateValue, value]);
|
|
52
|
+
var onKeyDown = useCallback(function (event) {
|
|
53
|
+
if (event.keyCode === 13) {
|
|
54
|
+
onBlur();
|
|
55
|
+
setValue('');
|
|
56
|
+
}
|
|
57
|
+
}, [onBlur]);
|
|
58
|
+
var inputRef = useRef();
|
|
59
|
+
useEffect(function () {
|
|
60
|
+
if (comment) inputRef.current.focus();
|
|
61
|
+
}, [comment]);
|
|
62
|
+
placeholder = placeholder ? placeholder : '请输入评论';
|
|
63
|
+
return /*#__PURE__*/React.createElement("input", {
|
|
64
|
+
ref: inputRef,
|
|
65
|
+
className: "comment-editor",
|
|
66
|
+
value: value,
|
|
67
|
+
placeholder: placeholder,
|
|
68
|
+
onChange: onChange,
|
|
69
|
+
onKeyDown: onKeyDown,
|
|
70
|
+
onBlur: onBlur
|
|
71
|
+
});
|
|
72
|
+
};
|
|
73
|
+
export default CommentEditor;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
|
2
|
+
import React, { Fragment, useCallback, useMemo, useRef, useState } from 'react';
|
|
3
|
+
import { withTranslation } from 'react-i18next';
|
|
4
|
+
import { PopoverBody, UncontrolledPopover } from 'reactstrap';
|
|
5
|
+
import dayjs from 'dayjs';
|
|
6
|
+
import CommentEditor from './comment-editor';
|
|
7
|
+
var CommentItem = function CommentItem(_ref) {
|
|
8
|
+
var comment = _ref.comment,
|
|
9
|
+
updateComment = _ref.updateComment,
|
|
10
|
+
deleteComment = _ref.deleteComment,
|
|
11
|
+
t = _ref.t;
|
|
12
|
+
var popoverRef = useRef(null);
|
|
13
|
+
var _useState = useState(false),
|
|
14
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
15
|
+
isActive = _useState2[0],
|
|
16
|
+
setActive = _useState2[1];
|
|
17
|
+
var onMouseEnter = useCallback(function () {
|
|
18
|
+
setActive(true);
|
|
19
|
+
}, []);
|
|
20
|
+
var onMouseLeave = useCallback(function () {
|
|
21
|
+
setActive(false);
|
|
22
|
+
}, []);
|
|
23
|
+
|
|
24
|
+
// const [isShowItemMenu, setIsShowItemMenu] = useState(false);
|
|
25
|
+
var _useState3 = useState(false),
|
|
26
|
+
_useState4 = _slicedToArray(_useState3, 2),
|
|
27
|
+
isEditing = _useState4[0],
|
|
28
|
+
setIsEditing = _useState4[1];
|
|
29
|
+
var onEditToggle = useCallback(function () {
|
|
30
|
+
setIsEditing(true);
|
|
31
|
+
popoverRef.current.toggle();
|
|
32
|
+
}, []);
|
|
33
|
+
var onDeleteToggle = useCallback(function () {
|
|
34
|
+
deleteComment(comment.id);
|
|
35
|
+
popoverRef.current.toggle();
|
|
36
|
+
}, [comment, deleteComment]);
|
|
37
|
+
var _updateComment = useCallback(function (elementId, commentId, newComment) {
|
|
38
|
+
if (newComment.comment !== comment.comment) {
|
|
39
|
+
updateComment(elementId, commentId, newComment);
|
|
40
|
+
}
|
|
41
|
+
setIsEditing(false);
|
|
42
|
+
}, [updateComment, comment]);
|
|
43
|
+
var menuId = useMemo(function () {
|
|
44
|
+
return "comment_".concat(comment.id);
|
|
45
|
+
}, [comment]);
|
|
46
|
+
return /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement("li", {
|
|
47
|
+
className: "comment-item",
|
|
48
|
+
onMouseEnter: onMouseEnter,
|
|
49
|
+
onMouseLeave: onMouseLeave
|
|
50
|
+
}, /*#__PURE__*/React.createElement("div", {
|
|
51
|
+
className: "comment-header"
|
|
52
|
+
}, /*#__PURE__*/React.createElement("span", {
|
|
53
|
+
className: "comment-author"
|
|
54
|
+
}, /*#__PURE__*/React.createElement("span", {
|
|
55
|
+
className: "comment-author__avatar"
|
|
56
|
+
}, /*#__PURE__*/React.createElement("img", {
|
|
57
|
+
alt: "",
|
|
58
|
+
src: comment.avatar_url
|
|
59
|
+
})), /*#__PURE__*/React.createElement("span", {
|
|
60
|
+
className: "comment-author__name"
|
|
61
|
+
}, comment.user_name)), isActive && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("span", {
|
|
62
|
+
id: menuId,
|
|
63
|
+
className: "comment-operation"
|
|
64
|
+
}, /*#__PURE__*/React.createElement("i", {
|
|
65
|
+
className: "sdocfont sdoc-more mr-1"
|
|
66
|
+
})), /*#__PURE__*/React.createElement(UncontrolledPopover, {
|
|
67
|
+
ref: popoverRef,
|
|
68
|
+
trigger: "legacy",
|
|
69
|
+
target: menuId,
|
|
70
|
+
placement: "bottom-end",
|
|
71
|
+
hideArrow: true
|
|
72
|
+
}, /*#__PURE__*/React.createElement(PopoverBody, {
|
|
73
|
+
style: {
|
|
74
|
+
padding: '5px'
|
|
75
|
+
}
|
|
76
|
+
}, /*#__PURE__*/React.createElement("div", {
|
|
77
|
+
className: "sdoc-popover-menu"
|
|
78
|
+
}, /*#__PURE__*/React.createElement("div", {
|
|
79
|
+
className: "sdoc-popover-menu__item",
|
|
80
|
+
onClick: onEditToggle
|
|
81
|
+
}, t('edit')), /*#__PURE__*/React.createElement("div", {
|
|
82
|
+
className: "sdoc-popover-menu__item",
|
|
83
|
+
onClick: onDeleteToggle
|
|
84
|
+
}, t('delete')))))), !isActive && /*#__PURE__*/React.createElement("span", {
|
|
85
|
+
className: "comment-time"
|
|
86
|
+
}, dayjs(comment.updated_at).format('HH:mm'))), /*#__PURE__*/React.createElement("div", {
|
|
87
|
+
className: "comment-content"
|
|
88
|
+
}, isEditing && /*#__PURE__*/React.createElement(CommentEditor, {
|
|
89
|
+
comment: comment,
|
|
90
|
+
updateComment: _updateComment
|
|
91
|
+
}), !isEditing && /*#__PURE__*/React.createElement(React.Fragment, null, comment.comment))));
|
|
92
|
+
};
|
|
93
|
+
export default withTranslation('sdoc-editor')(CommentItem);
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
2
|
+
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
|
|
3
|
+
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return exports; }; var exports = {}, Op = Object.prototype, hasOwn = Op.hasOwnProperty, defineProperty = Object.defineProperty || function (obj, key, desc) { obj[key] = desc.value; }, $Symbol = "function" == typeof Symbol ? Symbol : {}, iteratorSymbol = $Symbol.iterator || "@@iterator", asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator", toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag"; function define(obj, key, value) { return Object.defineProperty(obj, key, { value: value, enumerable: !0, configurable: !0, writable: !0 }), obj[key]; } try { define({}, ""); } catch (err) { define = function define(obj, key, value) { return obj[key] = value; }; } function wrap(innerFn, outerFn, self, tryLocsList) { var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator, generator = Object.create(protoGenerator.prototype), context = new Context(tryLocsList || []); return defineProperty(generator, "_invoke", { value: makeInvokeMethod(innerFn, self, context) }), generator; } function tryCatch(fn, obj, arg) { try { return { type: "normal", arg: fn.call(obj, arg) }; } catch (err) { return { type: "throw", arg: err }; } } exports.wrap = wrap; var ContinueSentinel = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var IteratorPrototype = {}; define(IteratorPrototype, iteratorSymbol, function () { return this; }); var getProto = Object.getPrototypeOf, NativeIteratorPrototype = getProto && getProto(getProto(values([]))); NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol) && (IteratorPrototype = NativeIteratorPrototype); var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype); function defineIteratorMethods(prototype) { ["next", "throw", "return"].forEach(function (method) { define(prototype, method, function (arg) { return this._invoke(method, arg); }); }); } function AsyncIterator(generator, PromiseImpl) { function invoke(method, arg, resolve, reject) { var record = tryCatch(generator[method], generator, arg); if ("throw" !== record.type) { var result = record.arg, value = result.value; return value && "object" == typeof value && hasOwn.call(value, "__await") ? PromiseImpl.resolve(value.__await).then(function (value) { invoke("next", value, resolve, reject); }, function (err) { invoke("throw", err, resolve, reject); }) : PromiseImpl.resolve(value).then(function (unwrapped) { result.value = unwrapped, resolve(result); }, function (error) { return invoke("throw", error, resolve, reject); }); } reject(record.arg); } var previousPromise; defineProperty(this, "_invoke", { value: function value(method, arg) { function callInvokeWithMethodAndArg() { return new PromiseImpl(function (resolve, reject) { invoke(method, arg, resolve, reject); }); } return previousPromise = previousPromise ? previousPromise.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(innerFn, self, context) { var state = "suspendedStart"; return function (method, arg) { if ("executing" === state) throw new Error("Generator is already running"); if ("completed" === state) { if ("throw" === method) throw arg; return doneResult(); } for (context.method = method, context.arg = arg;;) { var delegate = context.delegate; if (delegate) { var delegateResult = maybeInvokeDelegate(delegate, context); if (delegateResult) { if (delegateResult === ContinueSentinel) continue; return delegateResult; } } if ("next" === context.method) context.sent = context._sent = context.arg;else if ("throw" === context.method) { if ("suspendedStart" === state) throw state = "completed", context.arg; context.dispatchException(context.arg); } else "return" === context.method && context.abrupt("return", context.arg); state = "executing"; var record = tryCatch(innerFn, self, context); if ("normal" === record.type) { if (state = context.done ? "completed" : "suspendedYield", record.arg === ContinueSentinel) continue; return { value: record.arg, done: context.done }; } "throw" === record.type && (state = "completed", context.method = "throw", context.arg = record.arg); } }; } function maybeInvokeDelegate(delegate, context) { var methodName = context.method, method = delegate.iterator[methodName]; if (undefined === method) return context.delegate = null, "throw" === methodName && delegate.iterator.return && (context.method = "return", context.arg = undefined, maybeInvokeDelegate(delegate, context), "throw" === context.method) || "return" !== methodName && (context.method = "throw", context.arg = new TypeError("The iterator does not provide a '" + methodName + "' method")), ContinueSentinel; var record = tryCatch(method, delegate.iterator, context.arg); if ("throw" === record.type) return context.method = "throw", context.arg = record.arg, context.delegate = null, ContinueSentinel; var info = record.arg; return info ? info.done ? (context[delegate.resultName] = info.value, context.next = delegate.nextLoc, "return" !== context.method && (context.method = "next", context.arg = undefined), context.delegate = null, ContinueSentinel) : info : (context.method = "throw", context.arg = new TypeError("iterator result is not an object"), context.delegate = null, ContinueSentinel); } function pushTryEntry(locs) { var entry = { tryLoc: locs[0] }; 1 in locs && (entry.catchLoc = locs[1]), 2 in locs && (entry.finallyLoc = locs[2], entry.afterLoc = locs[3]), this.tryEntries.push(entry); } function resetTryEntry(entry) { var record = entry.completion || {}; record.type = "normal", delete record.arg, entry.completion = record; } function Context(tryLocsList) { this.tryEntries = [{ tryLoc: "root" }], tryLocsList.forEach(pushTryEntry, this), this.reset(!0); } function values(iterable) { if (iterable) { var iteratorMethod = iterable[iteratorSymbol]; if (iteratorMethod) return iteratorMethod.call(iterable); if ("function" == typeof iterable.next) return iterable; if (!isNaN(iterable.length)) { var i = -1, next = function next() { for (; ++i < iterable.length;) if (hasOwn.call(iterable, i)) return next.value = iterable[i], next.done = !1, next; return next.value = undefined, next.done = !0, next; }; return next.next = next; } } return { next: doneResult }; } function doneResult() { return { value: undefined, done: !0 }; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, defineProperty(Gp, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), defineProperty(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"), exports.isGeneratorFunction = function (genFun) { var ctor = "function" == typeof genFun && genFun.constructor; return !!ctor && (ctor === GeneratorFunction || "GeneratorFunction" === (ctor.displayName || ctor.name)); }, exports.mark = function (genFun) { return Object.setPrototypeOf ? Object.setPrototypeOf(genFun, GeneratorFunctionPrototype) : (genFun.__proto__ = GeneratorFunctionPrototype, define(genFun, toStringTagSymbol, "GeneratorFunction")), genFun.prototype = Object.create(Gp), genFun; }, exports.awrap = function (arg) { return { __await: arg }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, asyncIteratorSymbol, function () { return this; }), exports.AsyncIterator = AsyncIterator, exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) { void 0 === PromiseImpl && (PromiseImpl = Promise); var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl); return exports.isGeneratorFunction(outerFn) ? iter : iter.next().then(function (result) { return result.done ? result.value : iter.next(); }); }, defineIteratorMethods(Gp), define(Gp, toStringTagSymbol, "Generator"), define(Gp, iteratorSymbol, function () { return this; }), define(Gp, "toString", function () { return "[object Generator]"; }), exports.keys = function (val) { var object = Object(val), keys = []; for (var key in object) keys.push(key); return keys.reverse(), function next() { for (; keys.length;) { var key = keys.pop(); if (key in object) return next.value = key, next.done = !1, next; } return next.done = !0, next; }; }, exports.values = values, Context.prototype = { constructor: Context, reset: function reset(skipTempReset) { if (this.prev = 0, this.next = 0, this.sent = this._sent = undefined, this.done = !1, this.delegate = null, this.method = "next", this.arg = undefined, this.tryEntries.forEach(resetTryEntry), !skipTempReset) for (var name in this) "t" === name.charAt(0) && hasOwn.call(this, name) && !isNaN(+name.slice(1)) && (this[name] = undefined); }, stop: function stop() { this.done = !0; var rootRecord = this.tryEntries[0].completion; if ("throw" === rootRecord.type) throw rootRecord.arg; return this.rval; }, dispatchException: function dispatchException(exception) { if (this.done) throw exception; var context = this; function handle(loc, caught) { return record.type = "throw", record.arg = exception, context.next = loc, caught && (context.method = "next", context.arg = undefined), !!caught; } for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i], record = entry.completion; if ("root" === entry.tryLoc) return handle("end"); if (entry.tryLoc <= this.prev) { var hasCatch = hasOwn.call(entry, "catchLoc"), hasFinally = hasOwn.call(entry, "finallyLoc"); if (hasCatch && hasFinally) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } else if (hasCatch) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); } else { if (!hasFinally) throw new Error("try statement without catch or finally"); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } } } }, abrupt: function abrupt(type, arg) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) { var finallyEntry = entry; break; } } finallyEntry && ("break" === type || "continue" === type) && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc && (finallyEntry = null); var record = finallyEntry ? finallyEntry.completion : {}; return record.type = type, record.arg = arg, finallyEntry ? (this.method = "next", this.next = finallyEntry.finallyLoc, ContinueSentinel) : this.complete(record); }, complete: function complete(record, afterLoc) { if ("throw" === record.type) throw record.arg; return "break" === record.type || "continue" === record.type ? this.next = record.arg : "return" === record.type ? (this.rval = this.arg = record.arg, this.method = "return", this.next = "end") : "normal" === record.type && afterLoc && (this.next = afterLoc), ContinueSentinel; }, finish: function finish(finallyLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.finallyLoc === finallyLoc) return this.complete(entry.completion, entry.afterLoc), resetTryEntry(entry), ContinueSentinel; } }, catch: function _catch(tryLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc === tryLoc) { var record = entry.completion; if ("throw" === record.type) { var thrown = record.arg; resetTryEntry(entry); } return thrown; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(iterable, resultName, nextLoc) { return this.delegate = { iterator: values(iterable), resultName: resultName, nextLoc: nextLoc }, "next" === this.method && (this.arg = undefined), ContinueSentinel; } }, exports; }
|
|
4
|
+
import React, { useCallback, useRef } from 'react';
|
|
5
|
+
import CommentItem from './comment-item';
|
|
6
|
+
import CommentEditor from './comment-editor';
|
|
7
|
+
import { useCommentListPosition } from '../hooks/use-selection-position';
|
|
8
|
+
import { useCommentContext } from './hooks/use-comment-context';
|
|
9
|
+
import context from '../../context';
|
|
10
|
+
var CommentList = function CommentList(_ref) {
|
|
11
|
+
var comments = _ref.comments,
|
|
12
|
+
selectionElement = _ref.selectionElement;
|
|
13
|
+
var listRef = useRef(null);
|
|
14
|
+
var position = useCommentListPosition();
|
|
15
|
+
var _useCommentContext = useCommentContext(),
|
|
16
|
+
dispatch = _useCommentContext.dispatch;
|
|
17
|
+
var insertComment = useCallback( /*#__PURE__*/function () {
|
|
18
|
+
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(elementId, comment) {
|
|
19
|
+
var res, returnComment, newComment;
|
|
20
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
21
|
+
while (1) switch (_context.prev = _context.next) {
|
|
22
|
+
case 0:
|
|
23
|
+
_context.next = 2;
|
|
24
|
+
return context.insertComment(comment);
|
|
25
|
+
case 2:
|
|
26
|
+
res = _context.sent;
|
|
27
|
+
returnComment = res.data.comment;
|
|
28
|
+
newComment = _objectSpread(_objectSpread({}, comment), {}, {
|
|
29
|
+
id: returnComment.id,
|
|
30
|
+
user_name: returnComment.user_name,
|
|
31
|
+
avatar_url: returnComment.avatar_url
|
|
32
|
+
});
|
|
33
|
+
dispatch({
|
|
34
|
+
type: 'INSERT_COMMENT',
|
|
35
|
+
payload: {
|
|
36
|
+
element_id: elementId,
|
|
37
|
+
comment: newComment
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
setTimeout(function () {
|
|
41
|
+
listRef.current.scrollTop = 10000;
|
|
42
|
+
}, 0);
|
|
43
|
+
case 7:
|
|
44
|
+
case "end":
|
|
45
|
+
return _context.stop();
|
|
46
|
+
}
|
|
47
|
+
}, _callee);
|
|
48
|
+
}));
|
|
49
|
+
return function (_x, _x2) {
|
|
50
|
+
return _ref2.apply(this, arguments);
|
|
51
|
+
};
|
|
52
|
+
}(), [dispatch]);
|
|
53
|
+
var deleteComment = useCallback( /*#__PURE__*/function () {
|
|
54
|
+
var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(commentId) {
|
|
55
|
+
var elementId;
|
|
56
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
57
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
58
|
+
case 0:
|
|
59
|
+
elementId = selectionElement.id;
|
|
60
|
+
_context2.next = 3;
|
|
61
|
+
return context.deleteComment(commentId);
|
|
62
|
+
case 3:
|
|
63
|
+
dispatch({
|
|
64
|
+
type: 'DELETE_COMMENT',
|
|
65
|
+
payload: {
|
|
66
|
+
element_id: elementId,
|
|
67
|
+
comment_id: commentId
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
case 4:
|
|
71
|
+
case "end":
|
|
72
|
+
return _context2.stop();
|
|
73
|
+
}
|
|
74
|
+
}, _callee2);
|
|
75
|
+
}));
|
|
76
|
+
return function (_x3) {
|
|
77
|
+
return _ref3.apply(this, arguments);
|
|
78
|
+
};
|
|
79
|
+
}(), [dispatch, selectionElement]);
|
|
80
|
+
var updateComment = useCallback( /*#__PURE__*/function () {
|
|
81
|
+
var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(elementId, commentId, newComment) {
|
|
82
|
+
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
|
|
83
|
+
while (1) switch (_context3.prev = _context3.next) {
|
|
84
|
+
case 0:
|
|
85
|
+
_context3.next = 2;
|
|
86
|
+
return context.updateComment(commentId, newComment);
|
|
87
|
+
case 2:
|
|
88
|
+
dispatch({
|
|
89
|
+
type: 'UPDATE_COMMENT',
|
|
90
|
+
payload: {
|
|
91
|
+
element_id: elementId,
|
|
92
|
+
comment_id: commentId,
|
|
93
|
+
comment: newComment
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
case 3:
|
|
97
|
+
case "end":
|
|
98
|
+
return _context3.stop();
|
|
99
|
+
}
|
|
100
|
+
}, _callee3);
|
|
101
|
+
}));
|
|
102
|
+
return function (_x4, _x5, _x6) {
|
|
103
|
+
return _ref4.apply(this, arguments);
|
|
104
|
+
};
|
|
105
|
+
}(), [dispatch]);
|
|
106
|
+
return /*#__PURE__*/React.createElement("div", {
|
|
107
|
+
className: "comment-list-container",
|
|
108
|
+
style: {
|
|
109
|
+
top: position.y
|
|
110
|
+
}
|
|
111
|
+
}, comments.length > 0 && /*#__PURE__*/React.createElement("ul", {
|
|
112
|
+
ref: listRef,
|
|
113
|
+
className: "comment-list"
|
|
114
|
+
}, comments.map(function (comment) {
|
|
115
|
+
return /*#__PURE__*/React.createElement(CommentItem, {
|
|
116
|
+
key: comment.id,
|
|
117
|
+
comment: comment,
|
|
118
|
+
deleteComment: deleteComment,
|
|
119
|
+
updateComment: updateComment
|
|
120
|
+
});
|
|
121
|
+
})), /*#__PURE__*/React.createElement("div", {
|
|
122
|
+
className: "comment-editor-wrapper"
|
|
123
|
+
}, /*#__PURE__*/React.createElement(CommentEditor, {
|
|
124
|
+
insertComment: insertComment,
|
|
125
|
+
selectionElement: selectionElement
|
|
126
|
+
})));
|
|
127
|
+
};
|
|
128
|
+
export default CommentList;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
|
2
|
+
import React, { useCallback, useEffect, useMemo, useState } from 'react';
|
|
3
|
+
import useSelectionUpdate from '../hooks/use-selection-update';
|
|
4
|
+
import { useCursorPosition } from './helper';
|
|
5
|
+
import CommentList from './comment-list';
|
|
6
|
+
import { useSelectionElement } from '../hooks/use-selection-element';
|
|
7
|
+
import { useCommentContext } from './hooks/use-comment-context';
|
|
8
|
+
import './style.css';
|
|
9
|
+
var Comment = function Comment() {
|
|
10
|
+
useSelectionUpdate();
|
|
11
|
+
var selectionElement = useSelectionElement();
|
|
12
|
+
var _useState = useState(false),
|
|
13
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
14
|
+
isShowComments = _useState2[0],
|
|
15
|
+
setIsShowComments = _useState2[1];
|
|
16
|
+
var onAddCommentToggle = useCallback(function () {
|
|
17
|
+
setIsShowComments(true);
|
|
18
|
+
}, []);
|
|
19
|
+
var cursor = useCursorPosition();
|
|
20
|
+
var style = useMemo(function () {
|
|
21
|
+
return {
|
|
22
|
+
top: cursor.y === 0 || isShowComments ? '-99999px' : cursor.y
|
|
23
|
+
};
|
|
24
|
+
}, [cursor, isShowComments]);
|
|
25
|
+
|
|
26
|
+
// When selectionElement update, recalculate comment's panel state
|
|
27
|
+
var _useState3 = useState([]),
|
|
28
|
+
_useState4 = _slicedToArray(_useState3, 2),
|
|
29
|
+
comments = _useState4[0],
|
|
30
|
+
setComments = _useState4[1];
|
|
31
|
+
var comments_map = useCommentContext().commentsInfo.comments_map;
|
|
32
|
+
useEffect(function () {
|
|
33
|
+
var comments = comments_map[selectionElement === null || selectionElement === void 0 ? void 0 : selectionElement.id];
|
|
34
|
+
var hasComments = comments && comments.length > 0;
|
|
35
|
+
if (hasComments) {
|
|
36
|
+
setIsShowComments(true);
|
|
37
|
+
setComments(comments);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
setComments([]);
|
|
41
|
+
setIsShowComments(false);
|
|
42
|
+
}, [comments_map, selectionElement]);
|
|
43
|
+
return /*#__PURE__*/React.createElement("div", {
|
|
44
|
+
className: "sdoc-comment-container"
|
|
45
|
+
}, /*#__PURE__*/React.createElement("div", {
|
|
46
|
+
className: "comment-container-main"
|
|
47
|
+
}), /*#__PURE__*/React.createElement("div", {
|
|
48
|
+
className: "comment-container-right"
|
|
49
|
+
}, /*#__PURE__*/React.createElement("div", {
|
|
50
|
+
className: "comment-add-wrapper",
|
|
51
|
+
style: style
|
|
52
|
+
}, /*#__PURE__*/React.createElement("span", {
|
|
53
|
+
className: "add-comment-icon",
|
|
54
|
+
onClick: onAddCommentToggle
|
|
55
|
+
}, /*#__PURE__*/React.createElement("i", {
|
|
56
|
+
className: "sdocfont sdoc-comment mr-1"
|
|
57
|
+
}))), /*#__PURE__*/React.createElement("div", {
|
|
58
|
+
className: "comment-list-wrapper"
|
|
59
|
+
}, isShowComments && /*#__PURE__*/React.createElement(CommentList, {
|
|
60
|
+
comments: comments,
|
|
61
|
+
selectionElement: selectionElement
|
|
62
|
+
}))));
|
|
63
|
+
};
|
|
64
|
+
export default Comment;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import context from '../../context';
|
|
2
|
+
import { useScrollContext } from '../hooks/use-scroll-context';
|
|
3
|
+
export var getSelectionRange = function getSelectionRange() {
|
|
4
|
+
if (window.getSelection) {
|
|
5
|
+
var sel = window.getSelection();
|
|
6
|
+
if (sel.getRangeAt && sel.rangeCount) {
|
|
7
|
+
return sel.getRangeAt(0);
|
|
8
|
+
}
|
|
9
|
+
} else if (document.selection && document.selection.createRange) {
|
|
10
|
+
return document.selection.createRange();
|
|
11
|
+
}
|
|
12
|
+
return null;
|
|
13
|
+
};
|
|
14
|
+
export var getCursorPosition = function getCursorPosition() {
|
|
15
|
+
var x = 0,
|
|
16
|
+
y = 0;
|
|
17
|
+
var range = getSelectionRange();
|
|
18
|
+
if (range) {
|
|
19
|
+
var rect = range.getBoundingClientRect();
|
|
20
|
+
var headerHeight = 100;
|
|
21
|
+
x = rect.x || 0;
|
|
22
|
+
y = rect.y - headerHeight + (rect.height - 24) / 2 || 0;
|
|
23
|
+
}
|
|
24
|
+
return {
|
|
25
|
+
x: x,
|
|
26
|
+
y: y
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
export var useCursorPosition = function useCursorPosition() {
|
|
30
|
+
var scrollRef = useScrollContext();
|
|
31
|
+
var _ref = scrollRef.current || {},
|
|
32
|
+
_ref$scrollTop = _ref.scrollTop,
|
|
33
|
+
scrollTop = _ref$scrollTop === void 0 ? 0 : _ref$scrollTop;
|
|
34
|
+
var position = getCursorPosition();
|
|
35
|
+
if (position.y !== 0) {
|
|
36
|
+
position.y = position.y + scrollTop;
|
|
37
|
+
}
|
|
38
|
+
return position;
|
|
39
|
+
};
|
|
40
|
+
export var getAvatarUrl = function getAvatarUrl() {
|
|
41
|
+
var server = context.getSetting('serviceUrl');
|
|
42
|
+
var avatarUrl = "".concat(server, "/media/avatars/default.png");
|
|
43
|
+
return avatarUrl;
|
|
44
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import React, { useContext } from 'react';
|
|
2
|
+
export var CommentContext = React.createContext();
|
|
3
|
+
export var useCommentContext = function useCommentContext() {
|
|
4
|
+
var _useContext = useContext(CommentContext),
|
|
5
|
+
commentsInfo = _useContext.commentsInfo,
|
|
6
|
+
dispatch = _useContext.dispatch;
|
|
7
|
+
return {
|
|
8
|
+
commentsInfo: commentsInfo,
|
|
9
|
+
dispatch: dispatch
|
|
10
|
+
};
|
|
11
|
+
};
|