@seafile/sdoc-editor 0.1.167-beta → 0.1.168
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.
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
2
|
-
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
|
|
3
2
|
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
|
4
3
|
import React, { useMemo, useEffect, useState, useCallback, useImperativeHandle, forwardRef } from 'react';
|
|
5
4
|
import deepCopy from 'deep-copy';
|
|
@@ -12,9 +11,7 @@ import { withSocketIO } from '../socket';
|
|
|
12
11
|
import SDocEditor from './sdoc-editor';
|
|
13
12
|
import './index.css';
|
|
14
13
|
var Editor = forwardRef(function (_ref, ref) {
|
|
15
|
-
var document = _ref.document
|
|
16
|
-
_ref$isReloading = _ref.isReloading,
|
|
17
|
-
isReloading = _ref$isReloading === void 0 ? false : _ref$isReloading;
|
|
14
|
+
var document = _ref.document;
|
|
18
15
|
var editor = useMemo(function () {
|
|
19
16
|
var defaultEditor = createDefaultEditor();
|
|
20
17
|
var editorConfig = context.getEditorConfig();
|
|
@@ -29,10 +26,14 @@ var Editor = forwardRef(function (_ref, ref) {
|
|
|
29
26
|
|
|
30
27
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
31
28
|
}, []);
|
|
32
|
-
var _useState = useState(
|
|
29
|
+
var _useState = useState(false),
|
|
33
30
|
_useState2 = _slicedToArray(_useState, 2),
|
|
34
|
-
|
|
35
|
-
|
|
31
|
+
isLoading = _useState2[0],
|
|
32
|
+
_setLoading = _useState2[1];
|
|
33
|
+
var _useState3 = useState(document.children),
|
|
34
|
+
_useState4 = _slicedToArray(_useState3, 2),
|
|
35
|
+
slateValue = _useState4[0],
|
|
36
|
+
_setSlateValue = _useState4[1];
|
|
36
37
|
|
|
37
38
|
// useMount: init socket connection
|
|
38
39
|
useEffect(function () {
|
|
@@ -52,10 +53,12 @@ var Editor = forwardRef(function (_ref, ref) {
|
|
|
52
53
|
// The parent component can call the method of this component through ref
|
|
53
54
|
useImperativeHandle(ref, function () {
|
|
54
55
|
return {
|
|
56
|
+
// set value
|
|
57
|
+
setLoading: function setLoading(isLoading) {
|
|
58
|
+
_setLoading(isLoading);
|
|
59
|
+
},
|
|
55
60
|
setSlateValue: function setSlateValue(document) {
|
|
56
|
-
_setSlateValue(
|
|
57
|
-
// Force update of editor's child elements
|
|
58
|
-
editor.children = document.children;
|
|
61
|
+
_setSlateValue(document.children);
|
|
59
62
|
},
|
|
60
63
|
updateDocumentVersion: function updateDocumentVersion(document) {
|
|
61
64
|
editor.updateDocumentVersion(document);
|
|
@@ -70,7 +73,7 @@ var Editor = forwardRef(function (_ref, ref) {
|
|
|
70
73
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
71
74
|
};
|
|
72
75
|
}, [document, editor, slateValue]);
|
|
73
|
-
if (
|
|
76
|
+
if (isLoading) {
|
|
74
77
|
return /*#__PURE__*/React.createElement("div", {
|
|
75
78
|
className: "h-100 w-100 d-flex align-items-center justify-content-center"
|
|
76
79
|
}, /*#__PURE__*/React.createElement(CommonLoading, null));
|
|
@@ -9,7 +9,9 @@ var DiffViewer = function DiffViewer(_ref) {
|
|
|
9
9
|
var diff = getDiff(currentContent, lastContent);
|
|
10
10
|
useEffect(function () {
|
|
11
11
|
didMountCallback && didMountCallback(diff);
|
|
12
|
-
|
|
12
|
+
|
|
13
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
14
|
+
}, []);
|
|
13
15
|
return /*#__PURE__*/React.createElement(SDocViewer, {
|
|
14
16
|
showToolbar: false,
|
|
15
17
|
showOutline: false,
|
|
@@ -7,6 +7,7 @@ import Loading from '../components/loading';
|
|
|
7
7
|
import DocInfo from '../components/doc-info';
|
|
8
8
|
import DocOperations from '../components/doc-operations';
|
|
9
9
|
import Layout, { Header, Content } from '../layout';
|
|
10
|
+
import { generateDefaultDocContent } from '../utils';
|
|
10
11
|
import context from '../context';
|
|
11
12
|
import ErrorBoundary from './error-boundary';
|
|
12
13
|
import TipDialog from '../components/tip-dialog';
|
|
@@ -15,7 +16,6 @@ import { getRebase, hasConflict } from '../basic-sdk/utils/rebase';
|
|
|
15
16
|
import toaster from '../components/toast';
|
|
16
17
|
import { EventBus, SDocEditor } from '../basic-sdk';
|
|
17
18
|
import { RevisionDiffViewer } from '../basic-sdk/views';
|
|
18
|
-
import { useDocument } from '../hooks/use-document';
|
|
19
19
|
import '../assets/css/simple-editor.css';
|
|
20
20
|
var SimpleEditor = function SimpleEditor(_ref) {
|
|
21
21
|
var isStarred = _ref.isStarred,
|
|
@@ -23,37 +23,56 @@ var SimpleEditor = function SimpleEditor(_ref) {
|
|
|
23
23
|
t = _ref.t;
|
|
24
24
|
context.initApi();
|
|
25
25
|
var editorRef = useRef(null);
|
|
26
|
-
var _useState = useState(
|
|
26
|
+
var _useState = useState(true),
|
|
27
27
|
_useState2 = _slicedToArray(_useState, 2),
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
var _useState3 = useState(
|
|
28
|
+
isLoadingSdoc = _useState2[0],
|
|
29
|
+
setLoadingSdoc = _useState2[1];
|
|
30
|
+
var _useState3 = useState(''),
|
|
31
31
|
_useState4 = _slicedToArray(_useState3, 2),
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
var _useState5 = useState(
|
|
32
|
+
errorMessage = _useState4[0],
|
|
33
|
+
setErrorMessage = _useState4[1];
|
|
34
|
+
var _useState5 = useState(false),
|
|
35
35
|
_useState6 = _slicedToArray(_useState5, 2),
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
var _useState7 = useState(
|
|
36
|
+
isShowChanges = _useState6[0],
|
|
37
|
+
setShowChanges = _useState6[1];
|
|
38
|
+
var _useState7 = useState(null),
|
|
39
39
|
_useState8 = _slicedToArray(_useState7, 2),
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
var _useState9 = useState(
|
|
40
|
+
revisionContent = _useState8[0],
|
|
41
|
+
setRevisionContent = _useState8[1];
|
|
42
|
+
var _useState9 = useState(null),
|
|
43
43
|
_useState10 = _slicedToArray(_useState9, 2),
|
|
44
|
-
|
|
45
|
-
|
|
44
|
+
document = _useState10[0],
|
|
45
|
+
setDocument = _useState10[1];
|
|
46
46
|
var _useState11 = useState([]),
|
|
47
47
|
_useState12 = _slicedToArray(_useState11, 2),
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
var
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
48
|
+
changes = _useState12[0],
|
|
49
|
+
setChanges = _useState12[1];
|
|
50
|
+
var _useState13 = useState(false),
|
|
51
|
+
_useState14 = _slicedToArray(_useState13, 2),
|
|
52
|
+
showTip = _useState14[0],
|
|
53
|
+
setShowTip = _useState14[1];
|
|
54
|
+
var _useState15 = useState(''),
|
|
55
|
+
_useState16 = _slicedToArray(_useState15, 2),
|
|
56
|
+
tipType = _useState16[0],
|
|
57
|
+
setTipType = _useState16[1];
|
|
58
|
+
var _useState17 = useState([]),
|
|
59
|
+
_useState18 = _slicedToArray(_useState17, 2),
|
|
60
|
+
mergeValue = _useState18[0],
|
|
61
|
+
setMergeValue = _useState18[1];
|
|
62
|
+
useEffect(function () {
|
|
63
|
+
loadDocument(function (document) {
|
|
64
|
+
setDocument(document);
|
|
65
|
+
setLoadingSdoc(false);
|
|
66
|
+
}, function (errorMessage) {
|
|
67
|
+
setErrorMessage(errorMessage);
|
|
68
|
+
setDocument(null);
|
|
69
|
+
setLoadingSdoc(false);
|
|
70
|
+
}, function () {
|
|
71
|
+
setLoadingSdoc(true);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
75
|
+
}, []);
|
|
57
76
|
|
|
58
77
|
// useMount: reset title
|
|
59
78
|
useEffect(function () {
|
|
@@ -122,10 +141,42 @@ var SimpleEditor = function SimpleEditor(_ref) {
|
|
|
122
141
|
|
|
123
142
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
124
143
|
}, []);
|
|
144
|
+
var loadDocument = useCallback(function (successCallback, errorCallback, callback) {
|
|
145
|
+
callback && callback();
|
|
146
|
+
context.getFileContent().then(function (res) {
|
|
147
|
+
var result = res.data || generateDefaultDocContent();
|
|
148
|
+
if (result && !result.children) {
|
|
149
|
+
result = {
|
|
150
|
+
version: 0,
|
|
151
|
+
children: result.content,
|
|
152
|
+
cursors: result.cursors || {}
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
successCallback && successCallback(result);
|
|
156
|
+
}).catch(function (error) {
|
|
157
|
+
// eslint-disable-next-line
|
|
158
|
+
console.log(error);
|
|
159
|
+
var errorMessage = 'Load_doc_content_error';
|
|
160
|
+
if (error && error.response) {
|
|
161
|
+
var _ref2 = error.response.data || {},
|
|
162
|
+
error_type = _ref2.error_type;
|
|
163
|
+
if (error_type === 'content_invalid') {
|
|
164
|
+
errorMessage = 'Sdoc_format_invalid';
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
errorCallback && errorCallback(errorMessage);
|
|
168
|
+
});
|
|
169
|
+
}, []);
|
|
125
170
|
var toggleViewChanges = useCallback(function (isShowChanges) {
|
|
126
171
|
if (!isShowChanges) {
|
|
127
|
-
|
|
128
|
-
|
|
172
|
+
loadDocument(function (document) {
|
|
173
|
+
setDocument(document);
|
|
174
|
+
setShowChanges(isShowChanges);
|
|
175
|
+
editorRef.current.setLoading(false);
|
|
176
|
+
}, function (errorMessage) {
|
|
177
|
+
setErrorMessage(errorMessage);
|
|
178
|
+
editorRef.current.setLoading(false);
|
|
179
|
+
});
|
|
129
180
|
return;
|
|
130
181
|
}
|
|
131
182
|
|
|
@@ -149,62 +200,63 @@ var SimpleEditor = function SimpleEditor(_ref) {
|
|
|
149
200
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
150
201
|
}, [isShowChanges]);
|
|
151
202
|
var publishRevision = useCallback( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
152
|
-
var revisionPromise, baseVersionPromise, originVersionPromise;
|
|
153
203
|
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
154
204
|
while (1) switch (_context.prev = _context.next) {
|
|
155
205
|
case 0:
|
|
156
206
|
setTipType(TIP_TYPE.CHECKING);
|
|
157
207
|
setShowTip(true);
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
originVersionPromise = context.getSeadocOriginFileContent();
|
|
161
|
-
Promise.all([revisionPromise, baseVersionPromise, originVersionPromise]).then(function (results) {
|
|
162
|
-
var _results = _slicedToArray(results, 3),
|
|
163
|
-
revisionContent = _results[0],
|
|
164
|
-
baseRes = _results[1],
|
|
165
|
-
masterRes = _results[2];
|
|
166
|
-
var baseContent = JSON.parse(baseRes.data.content);
|
|
167
|
-
var masterContent = JSON.parse(masterRes.data.content);
|
|
208
|
+
loadDocument(function (document) {
|
|
209
|
+
var revisionContent = document;
|
|
168
210
|
if (hasConflict(revisionContent.children)) {
|
|
169
211
|
setTipType(TIP_TYPE.HAS_CONFLICT_BEFORE_PUBLISH);
|
|
170
212
|
setShowTip(true);
|
|
171
213
|
return;
|
|
172
214
|
}
|
|
173
|
-
var
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
215
|
+
var baseContent;
|
|
216
|
+
var masterContent;
|
|
217
|
+
context.getRevisionBaseVersionContent().then(function (res) {
|
|
218
|
+
var baseVersionContentString = res.data.content; // commit content
|
|
219
|
+
baseContent = JSON.parse(baseVersionContentString);
|
|
220
|
+
return context.getSeadocOriginFileContent();
|
|
221
|
+
}).then(function (res) {
|
|
222
|
+
var masterContentString = res.data.content;
|
|
223
|
+
masterContent = JSON.parse(masterContentString);
|
|
224
|
+
var _getRebase = getRebase(masterContent, baseContent, revisionContent),
|
|
225
|
+
canMerge = _getRebase.canMerge,
|
|
226
|
+
isNeedReplaceMaster = _getRebase.isNeedReplaceMaster,
|
|
227
|
+
value = _getRebase.value;
|
|
228
|
+
if (canMerge && isNeedReplaceMaster) {
|
|
229
|
+
setTipType(TIP_TYPE.PUBLISHING);
|
|
230
|
+
context.publishRevision().then(function (res) {
|
|
231
|
+
setTipType(TIP_TYPE.HAS_BEEN_PUBLISHED);
|
|
232
|
+
setShowTip(true);
|
|
233
|
+
}).catch(function (error) {
|
|
234
|
+
toaster.danger(t('Error'));
|
|
235
|
+
});
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
if (canMerge && !isNeedReplaceMaster) {
|
|
239
|
+
setTipType(TIP_TYPE.DELETE_NO_CHANGES_REVISION);
|
|
181
240
|
setShowTip(true);
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
}
|
|
187
|
-
if (canMerge && !isNeedReplaceMaster) {
|
|
188
|
-
setTipType(TIP_TYPE.DELETE_NO_CHANGES_REVISION);
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
setMergeValue(value);
|
|
244
|
+
setTipType(TIP_TYPE.MERGE);
|
|
189
245
|
setShowTip(true);
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
}).catch(function (error) {
|
|
196
|
-
if (typeof error === 'string') {
|
|
197
|
-
toaster.danger(t(error));
|
|
198
|
-
return;
|
|
199
|
-
}
|
|
200
|
-
toaster.danger(t('Error'));
|
|
246
|
+
}).catch(function (error) {
|
|
247
|
+
toaster.danger(t('Error'));
|
|
248
|
+
});
|
|
249
|
+
}, function (errorMessage) {
|
|
250
|
+
toaster.danger(t(errorMessage));
|
|
201
251
|
});
|
|
202
|
-
|
|
252
|
+
|
|
253
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
254
|
+
case 3:
|
|
203
255
|
case "end":
|
|
204
256
|
return _context.stop();
|
|
205
257
|
}
|
|
206
258
|
}, _callee);
|
|
207
|
-
})), [
|
|
259
|
+
})), [isShowChanges]);
|
|
208
260
|
var jumpToURL = useCallback(function (url) {
|
|
209
261
|
window.location.href = url;
|
|
210
262
|
}, []);
|
|
@@ -248,8 +300,10 @@ var SimpleEditor = function SimpleEditor(_ref) {
|
|
|
248
300
|
if (isShowChanges) {
|
|
249
301
|
setShowChanges(false);
|
|
250
302
|
}
|
|
303
|
+
editorRef.current.setLoading(true);
|
|
251
304
|
editorRef.current.setSlateValue(mergeValue);
|
|
252
305
|
editorRef.current.updateDocumentVersion(mergeValue);
|
|
306
|
+
editorRef.current.setLoading(false);
|
|
253
307
|
setShowTip(false);
|
|
254
308
|
}).catch(function (error) {
|
|
255
309
|
callback && callback(false);
|
|
@@ -261,7 +315,7 @@ var SimpleEditor = function SimpleEditor(_ref) {
|
|
|
261
315
|
|
|
262
316
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
263
317
|
}, [tipType, mergeValue, editorRef.current, document]);
|
|
264
|
-
if (
|
|
318
|
+
if (isLoadingSdoc) {
|
|
265
319
|
return /*#__PURE__*/React.createElement(Loading, null);
|
|
266
320
|
}
|
|
267
321
|
if (errorMessage) {
|
|
@@ -282,7 +336,6 @@ var SimpleEditor = function SimpleEditor(_ref) {
|
|
|
282
336
|
publishRevision: publishRevision
|
|
283
337
|
})), /*#__PURE__*/React.createElement(Content, null, !isShowChanges && /*#__PURE__*/React.createElement(SDocEditor, {
|
|
284
338
|
ref: editorRef,
|
|
285
|
-
isReloading: isReloading,
|
|
286
339
|
document: document
|
|
287
340
|
}), isShowChanges && /*#__PURE__*/React.createElement(RevisionDiffViewer, {
|
|
288
341
|
revisionContent: revisionContent,
|
package/package.json
CHANGED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
|
2
|
-
import { useCallback, useEffect, useState } from 'react';
|
|
3
|
-
import context from '../context';
|
|
4
|
-
export var useDocument = function useDocument() {
|
|
5
|
-
var _useState = useState(true),
|
|
6
|
-
_useState2 = _slicedToArray(_useState, 2),
|
|
7
|
-
isFirstLoading = _useState2[0],
|
|
8
|
-
setIsFirstLoading = _useState2[1];
|
|
9
|
-
var _useState3 = useState(false),
|
|
10
|
-
_useState4 = _slicedToArray(_useState3, 2),
|
|
11
|
-
isReloading = _useState4[0],
|
|
12
|
-
setIsReloading = _useState4[1];
|
|
13
|
-
var _useState5 = useState(true),
|
|
14
|
-
_useState6 = _slicedToArray(_useState5, 2),
|
|
15
|
-
document = _useState6[0],
|
|
16
|
-
setDocument = _useState6[1];
|
|
17
|
-
var _useState7 = useState(''),
|
|
18
|
-
_useState8 = _slicedToArray(_useState7, 2),
|
|
19
|
-
errorMessage = _useState8[0],
|
|
20
|
-
setErrorMessage = _useState8[1];
|
|
21
|
-
var loadDocument = useCallback(function () {
|
|
22
|
-
return new Promise(function (resolve, reject) {
|
|
23
|
-
context.getFileContent().then(function (res) {
|
|
24
|
-
var result = res.data;
|
|
25
|
-
if (result && !result.children) {
|
|
26
|
-
result = {
|
|
27
|
-
version: 0,
|
|
28
|
-
children: result.content,
|
|
29
|
-
cursors: result.cursors || {}
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
resolve(result);
|
|
33
|
-
}).catch(function (error) {
|
|
34
|
-
// eslint-disable-next-line
|
|
35
|
-
console.log(error);
|
|
36
|
-
var errorMessage = 'Load_doc_content_error';
|
|
37
|
-
if (error && error.response) {
|
|
38
|
-
var _ref = error.response.data || {},
|
|
39
|
-
error_type = _ref.error_type;
|
|
40
|
-
if (error_type === 'content_invalid') {
|
|
41
|
-
errorMessage = 'Sdoc_format_invalid';
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
reject(errorMessage);
|
|
45
|
-
});
|
|
46
|
-
});
|
|
47
|
-
}, []);
|
|
48
|
-
var reloadDocument = useCallback(function () {
|
|
49
|
-
setIsReloading(true);
|
|
50
|
-
loadDocument().then(function (document) {
|
|
51
|
-
setDocument(document);
|
|
52
|
-
setIsReloading(false);
|
|
53
|
-
}).catch(function (errorMessage) {
|
|
54
|
-
setIsReloading(false);
|
|
55
|
-
setErrorMessage(errorMessage);
|
|
56
|
-
setDocument(null);
|
|
57
|
-
setIsReloading(false);
|
|
58
|
-
});
|
|
59
|
-
}, [loadDocument]);
|
|
60
|
-
useEffect(function () {
|
|
61
|
-
loadDocument().then(function (document) {
|
|
62
|
-
setDocument(document);
|
|
63
|
-
setIsFirstLoading(false);
|
|
64
|
-
}).catch(function (errorMessage) {
|
|
65
|
-
setErrorMessage(errorMessage);
|
|
66
|
-
setDocument(null);
|
|
67
|
-
setIsFirstLoading(false);
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
71
|
-
}, []);
|
|
72
|
-
return {
|
|
73
|
-
isFirstLoading: isFirstLoading,
|
|
74
|
-
isReloading: isReloading,
|
|
75
|
-
errorMessage: errorMessage,
|
|
76
|
-
document: document,
|
|
77
|
-
loadDocument: loadDocument,
|
|
78
|
-
reloadDocument: reloadDocument
|
|
79
|
-
};
|
|
80
|
-
};
|