@seafile/sdoc-editor 0.1.166 → 0.1.167

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.
@@ -3,23 +3,18 @@ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
3
3
  import React, { useMemo, useEffect, useState, useCallback, useImperativeHandle, forwardRef } from 'react';
4
4
  import deepCopy from 'deep-copy';
5
5
  import context from '../../context';
6
- import { MODE } from '../../constants';
7
6
  import CommonLoading from '../../components/common-loading';
8
7
  import { PAGE_EDIT_AREA_WIDTH } from '../constants';
9
8
  import { createDefaultEditor } from '../extension';
10
9
  import withNodeId from '../node-id';
11
10
  import { withSocketIO } from '../socket';
12
- import DiffViewer from '../views/diff-viewer';
13
11
  import SDocEditor from './sdoc-editor';
14
12
  import './index.css';
15
13
  var Editor = forwardRef(function (_ref, ref) {
16
- var mode = _ref.mode,
17
- document = _ref.document,
18
- setDiffChanges = _ref.setDiffChanges;
14
+ var document = _ref.document;
19
15
  var editor = useMemo(function () {
20
16
  var defaultEditor = createDefaultEditor();
21
17
  var editorConfig = context.getEditorConfig();
22
- defaultEditor.mode = MODE.EDITOR;
23
18
  var newEditor = withNodeId(withSocketIO(defaultEditor, {
24
19
  document: document,
25
20
  config: editorConfig
@@ -31,22 +26,14 @@ var Editor = forwardRef(function (_ref, ref) {
31
26
 
32
27
  // eslint-disable-next-line react-hooks/exhaustive-deps
33
28
  }, []);
34
- var _useState = useState(document.children),
29
+ var _useState = useState(false),
35
30
  _useState2 = _slicedToArray(_useState, 2),
36
- slateValue = _useState2[0],
37
- _setSlateValue = _useState2[1];
38
- var _useState3 = useState(false),
31
+ isLoading = _useState2[0],
32
+ _setLoading = _useState2[1];
33
+ var _useState3 = useState(document.children),
39
34
  _useState4 = _slicedToArray(_useState3, 2),
40
- isLoading = _useState4[0],
41
- _setLoading = _useState4[1];
42
- var _useState5 = useState(null),
43
- _useState6 = _slicedToArray(_useState5, 2),
44
- diffContent = _useState6[0],
45
- _setDiffContent = _useState6[1];
46
- var _useState7 = useState(null),
47
- _useState8 = _slicedToArray(_useState7, 2),
48
- currentContent = _useState8[0],
49
- setCurrentContent = _useState8[1];
35
+ slateValue = _useState4[0],
36
+ _setSlateValue = _useState4[1];
50
37
 
51
38
  // useMount: init socket connection
52
39
  useEffect(function () {
@@ -70,20 +57,6 @@ var Editor = forwardRef(function (_ref, ref) {
70
57
  setLoading: function setLoading(isLoading) {
71
58
  _setLoading(isLoading);
72
59
  },
73
- setDiffContent: function setDiffContent(content) {
74
- setCurrentContent(deepCopy(_objectSpread(_objectSpread({}, document), {}, {
75
- children: slateValue
76
- })));
77
- _setDiffContent(content);
78
- },
79
- setEditorMode: function setEditorMode(mode) {
80
- editor.cursors = {};
81
- editor.selection = null;
82
- if (mode === MODE.EDITOR) {
83
- editor.operations = [];
84
- }
85
- editor.mode = mode;
86
- },
87
60
  setSlateValue: function setSlateValue(document) {
88
61
  _setSlateValue(document.children);
89
62
  },
@@ -105,16 +78,6 @@ var Editor = forwardRef(function (_ref, ref) {
105
78
  className: "h-100 w-100 d-flex align-items-center justify-content-center"
106
79
  }, /*#__PURE__*/React.createElement(CommonLoading, null));
107
80
  }
108
- if (mode === MODE.DIFF_VIEWER) {
109
- return /*#__PURE__*/React.createElement(DiffViewer, {
110
- showToolbar: true,
111
- showOutline: true,
112
- editor: editor,
113
- currentContent: currentContent,
114
- lastContent: diffContent,
115
- didMountCallback: setDiffChanges
116
- });
117
- }
118
81
  return /*#__PURE__*/React.createElement(SDocEditor, {
119
82
  slateValue: slateValue,
120
83
  editor: editor,
@@ -4,7 +4,6 @@ import { Editor, Operation } from '@seafile/slate';
4
4
  import { getNode } from '../extension/core';
5
5
  import * as OPERATION from '../node-id/constants';
6
6
  import { setCursor } from '../cursor/helper';
7
- import { MODE } from '../../constants';
8
7
  export var getNodePathById = function getNodePathById(rootNode, nodeId) {
9
8
  var path = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
10
9
  if (rootNode.id === nodeId) return path;
@@ -253,7 +252,6 @@ export var reExecRevertOperationList = function reExecRevertOperationList(editor
253
252
  }
254
253
  };
255
254
  export var syncRemoteOperations = function syncRemoteOperations(editor, remoteOperations) {
256
- if (editor.mode !== MODE.EDITOR) return;
257
255
  if (remoteOperations.length === 0) return;
258
256
  Editor.withoutNormalizing(editor, function () {
259
257
  for (var i = 0; i < remoteOperations.length; i++) {
@@ -7,7 +7,7 @@ import { syncRemoteOperations, reExecRevertOperationList, revertOperationList, s
7
7
  import SocketClient from './socket-client';
8
8
  import { clientDebug, conflictDebug, serverDebug, stateDebug } from '../utils/debug';
9
9
  import { deleteCursor } from '../cursor/helper';
10
- import { EXTERNAL_EVENT, MODE } from '../../constants';
10
+ import { EXTERNAL_EVENT } from '../../constants';
11
11
 
12
12
  // idle --> sending --> conflict --> idle
13
13
  // --> conflict --> idle
@@ -56,7 +56,6 @@ var SocketManager = /*#__PURE__*/_createClass(function SocketManager(editor, _do
56
56
  _this.sendOperations();
57
57
  };
58
58
  this.sendOperations = function () {
59
- if (_this.editor.mode !== MODE.EDITOR) return;
60
59
  if (_this.state !== STATE.IDLE) return;
61
60
  stateDebug("State changed: ".concat(_this.state, " -> ").concat(STATE.SENDING));
62
61
  _this.state = STATE.SENDING;
@@ -1,4 +1,3 @@
1
- import { MODE } from '../../constants';
2
1
  import { generateCursorData } from '../cursor/helper';
3
2
  import EventBus from '../utils/event-bus';
4
3
  import SocketManager from './socket-manager';
@@ -22,7 +21,6 @@ var withSocketIO = function withSocketIO(editor, options) {
22
21
  newEditor.onChange = function () {
23
22
  var document = options.document,
24
23
  config = options.config;
25
- if (newEditor.mode === MODE.DIFF_VIEWER) return;
26
24
  var operations = newEditor.operations;
27
25
  if (!newEditor.isRemote && operations.length > 0) {
28
26
  var isAllSetSelection = operations.every(function (operation) {
@@ -30,23 +28,18 @@ var withSocketIO = function withSocketIO(editor, options) {
30
28
  });
31
29
  var _socketManager = SocketManager.getInstance(newEditor, document, config);
32
30
  if (!isAllSetSelection) {
33
- if (newEditor.mode === MODE.EDITOR) {
34
- // get update content value operations
35
- var updateOperations = operations.filter(function (operation) {
36
- return operation.type !== 'set_selection';
37
- });
38
- _socketManager.onReceiveLocalOperations(updateOperations);
39
- }
31
+ // get update content value operations
32
+ var updateOperations = operations.filter(function (operation) {
33
+ return operation.type !== 'set_selection';
34
+ });
35
+ _socketManager.onReceiveLocalOperations(updateOperations);
40
36
  }
41
- if (newEditor.mode === MODE.EDITOR) {
42
- _socketManager.sendCursorLocation(editor.selection);
43
- }
44
- }
45
- if (newEditor.mode === MODE.EDITOR) {
46
- // dispatch editor change event
47
- var eventBus = EventBus.getInstance(newEditor, document, config);
48
- eventBus.dispatch('change');
37
+ _socketManager.sendCursorLocation(editor.selection);
49
38
  }
39
+
40
+ // dispatch editor change event
41
+ var eventBus = EventBus.getInstance(newEditor, document, config);
42
+ eventBus.dispatch('change');
50
43
  onChange();
51
44
  };
52
45
  newEditor.rebaseContent = function (document, originFileVersion) {
@@ -1,3 +1,4 @@
1
- import DiffViewer from './diff-viewer';
2
1
  import SDocViewer from './sdoc-viewer';
3
- export { DiffViewer, SDocViewer };
2
+ import DiffViewer from './sdoc-diff-viewer';
3
+ import RevisionDiffViewer from './revision-diff-viewer';
4
+ export { SDocViewer, DiffViewer, RevisionDiffViewer };
@@ -0,0 +1,44 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
2
+ import React, { useEffect, useState } from 'react';
3
+ import context from '../../context';
4
+ import { getDiff } from '../utils/diff';
5
+ import SDocViewer from './sdoc-viewer';
6
+ import Loading from '../../components/loading';
7
+ import '../../assets/css/diff-viewer.css';
8
+ var RevisionDiffViewer = function RevisionDiffViewer(_ref) {
9
+ var revisionContent = _ref.revisionContent,
10
+ didMountCallback = _ref.didMountCallback;
11
+ var _useState = useState(true),
12
+ _useState2 = _slicedToArray(_useState, 2),
13
+ isLoading = _useState2[0],
14
+ setIsLoading = _useState2[1];
15
+ var _useState3 = useState(null),
16
+ _useState4 = _slicedToArray(_useState3, 2),
17
+ diff = _useState4[0],
18
+ setDiff = _useState4[1];
19
+ useEffect(function () {
20
+ setIsLoading(true);
21
+ context.getSeadocOriginFileContent().then(function (res) {
22
+ var originContentString = res.data.content;
23
+ var originContent = JSON.parse(originContentString);
24
+ var diff = getDiff(revisionContent, originContent);
25
+ setDiff(diff);
26
+ didMountCallback && didMountCallback(diff);
27
+ setIsLoading(false);
28
+ }).catch(function (error) {
29
+ console.log('error');
30
+ setIsLoading(false);
31
+ });
32
+ }, [revisionContent, didMountCallback]);
33
+ if (isLoading) {
34
+ return /*#__PURE__*/React.createElement(Loading, null);
35
+ }
36
+ return /*#__PURE__*/React.createElement(SDocViewer, {
37
+ showToolbar: true,
38
+ showOutline: true,
39
+ document: {
40
+ children: diff.value
41
+ }
42
+ });
43
+ };
44
+ export default RevisionDiffViewer;
@@ -0,0 +1,21 @@
1
+ import React, { useEffect } from 'react';
2
+ import { getDiff } from '../utils/diff';
3
+ import SDocViewer from './sdoc-viewer';
4
+ import '../../assets/css/diff-viewer.css';
5
+ var DiffViewer = function DiffViewer(_ref) {
6
+ var currentContent = _ref.currentContent,
7
+ lastContent = _ref.lastContent,
8
+ didMountCallback = _ref.didMountCallback;
9
+ var diff = getDiff(currentContent, lastContent);
10
+ useEffect(function () {
11
+ didMountCallback && didMountCallback(diff);
12
+ }, [didMountCallback, diff]);
13
+ return /*#__PURE__*/React.createElement(SDocViewer, {
14
+ showToolbar: false,
15
+ showOutline: false,
16
+ document: {
17
+ children: diff.value
18
+ }
19
+ });
20
+ };
21
+ export default DiffViewer;
@@ -14,11 +14,6 @@ export var EXTERNAL_EVENT = {
14
14
  REMOVE_DOCUMENT: 'remove_document',
15
15
  REMOVE_DOCUMENT_ERROR: 'remove_document_error'
16
16
  };
17
- export var MODE = {
18
- DIFF_VIEWER: 'diff_viewer',
19
- VIEWER: 'viewer',
20
- EDITOR: 'editor'
21
- };
22
17
  export var TIP_TYPE = {
23
18
  DELETE_NO_CHANGES_REVISION: 'delete_no_changes_revision',
24
19
  MERGE: 'merge',
@@ -11,10 +11,11 @@ import { generateDefaultDocContent } from '../utils';
11
11
  import context from '../context';
12
12
  import ErrorBoundary from './error-boundary';
13
13
  import TipDialog from '../components/tip-dialog';
14
- import { EXTERNAL_EVENT, MODE, TIP_TYPE, REVISION_FIRST_LOAD_KEY, REVISION_FIRST_LOAD_VALUE } from '../constants';
14
+ import { EXTERNAL_EVENT, TIP_TYPE, REVISION_FIRST_LOAD_KEY, REVISION_FIRST_LOAD_VALUE } from '../constants';
15
15
  import { getRebase, hasConflict } from '../basic-sdk/utils/rebase';
16
16
  import toaster from '../components/toast';
17
17
  import { EventBus, SDocEditor } from '../basic-sdk';
18
+ import { RevisionDiffViewer } from '../basic-sdk/views';
18
19
  import '../assets/css/simple-editor.css';
19
20
  var SimpleEditor = function SimpleEditor(_ref) {
20
21
  var isStarred = _ref.isStarred,
@@ -36,12 +37,12 @@ var SimpleEditor = function SimpleEditor(_ref) {
36
37
  setShowChanges = _useState6[1];
37
38
  var _useState7 = useState(null),
38
39
  _useState8 = _slicedToArray(_useState7, 2),
39
- document = _useState8[0],
40
- setDocument = _useState8[1];
41
- var _useState9 = useState(MODE.EDITOR),
40
+ revisionContent = _useState8[0],
41
+ setRevisionContent = _useState8[1];
42
+ var _useState9 = useState(null),
42
43
  _useState10 = _slicedToArray(_useState9, 2),
43
- mode = _useState10[0],
44
- setMode = _useState10[1];
44
+ document = _useState10[0],
45
+ setDocument = _useState10[1];
45
46
  var _useState11 = useState([]),
46
47
  _useState12 = _slicedToArray(_useState11, 2),
47
48
  changes = _useState12[0],
@@ -166,32 +167,11 @@ var SimpleEditor = function SimpleEditor(_ref) {
166
167
  errorCallback && errorCallback(errorMessage);
167
168
  });
168
169
  }, []);
169
- var setDiffContent = useCallback(function () {
170
- context.getSeadocOriginFileContent().then(function (res) {
171
- var originContentString = res.data.content;
172
- var originContent = JSON.parse(originContentString);
173
- editorRef.current.setDiffContent(originContent);
174
- editorRef.current.setLoading(false);
175
- }).catch(function (error) {
176
- console.log('error');
177
- });
178
- }, []);
179
170
  var toggleViewChanges = useCallback(function (isShowChanges) {
180
- var revisionContent = editorRef.current.getSlateValue();
181
- if (isShowChanges && hasConflict(revisionContent.children)) {
182
- setTipType(TIP_TYPE.HAS_CONFLICT_BEFORE_VIEW_CHANGES);
183
- setShowTip(true);
184
- return;
185
- }
186
- editorRef.current.setLoading(true);
187
- setShowChanges(isShowChanges);
188
- var mode = isShowChanges ? MODE.DIFF_VIEWER : MODE.EDITOR;
189
- setMode(mode);
190
- editorRef.current.setEditorMode(mode);
191
171
  if (!isShowChanges) {
192
172
  loadDocument(function (document) {
193
173
  setDocument(document);
194
- editorRef.current.setSlateValue(document);
174
+ setShowChanges(isShowChanges);
195
175
  editorRef.current.setLoading(false);
196
176
  }, function (errorMessage) {
197
177
  setErrorMessage(errorMessage);
@@ -199,10 +179,26 @@ var SimpleEditor = function SimpleEditor(_ref) {
199
179
  });
200
180
  return;
201
181
  }
202
- setDiffContent();
203
182
 
183
+ // Get the contents of the current revision
184
+ var revisionContent = editorRef.current.getSlateValue();
185
+
186
+ // Prevent users from switching if document contains conflicting content
187
+ if (hasConflict(revisionContent.children)) {
188
+ setTipType(TIP_TYPE.HAS_CONFLICT_BEFORE_VIEW_CHANGES);
189
+ setShowTip(true);
190
+ return;
191
+ }
192
+
193
+ // Record the contents of the current revision
194
+ setRevisionContent(revisionContent);
195
+ setShowChanges(isShowChanges);
204
196
  // eslint-disable-next-line react-hooks/exhaustive-deps
205
197
  }, [document, editorRef.current]);
198
+ var setDiffChanges = useCallback(function (diff) {
199
+ setChanges(diff.changes);
200
+ // eslint-disable-next-line react-hooks/exhaustive-deps
201
+ }, [isShowChanges]);
206
202
  var publishRevision = useCallback( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
207
203
  return _regeneratorRuntime().wrap(function _callee$(_context) {
208
204
  while (1) switch (_context.prev = _context.next) {
@@ -260,12 +256,7 @@ var SimpleEditor = function SimpleEditor(_ref) {
260
256
  return _context.stop();
261
257
  }
262
258
  }, _callee);
263
- })), [isShowChanges, mode]);
264
- var setDiffChanges = useCallback(function (diff) {
265
- setChanges(diff.changes);
266
-
267
- // eslint-disable-next-line react-hooks/exhaustive-deps
268
- }, [isShowChanges]);
259
+ })), [isShowChanges]);
269
260
  var jumpToURL = useCallback(function (url) {
270
261
  window.location.href = url;
271
262
  }, []);
@@ -305,11 +296,13 @@ var SimpleEditor = function SimpleEditor(_ref) {
305
296
  context.updateSettings({
306
297
  'originFileVersion': origin_file_version
307
298
  });
299
+ // if revision diff view change to sdoc editor
300
+ if (isShowChanges) {
301
+ setShowChanges(false);
302
+ }
308
303
  editorRef.current.setLoading(true);
309
304
  editorRef.current.setSlateValue(mergeValue);
310
305
  editorRef.current.updateDocumentVersion(mergeValue);
311
- setMode(MODE.EDITOR);
312
- editorRef.current.setEditorMode(MODE.EDITOR);
313
306
  editorRef.current.setLoading(false);
314
307
  setShowTip(false);
315
308
  }).catch(function (error) {
@@ -341,11 +334,12 @@ var SimpleEditor = function SimpleEditor(_ref) {
341
334
  changes: changes,
342
335
  toggleViewChanges: toggleViewChanges,
343
336
  publishRevision: publishRevision
344
- })), /*#__PURE__*/React.createElement(Content, null, /*#__PURE__*/React.createElement(SDocEditor, {
337
+ })), /*#__PURE__*/React.createElement(Content, null, !isShowChanges && /*#__PURE__*/React.createElement(SDocEditor, {
345
338
  ref: editorRef,
346
- document: document,
347
- mode: mode,
348
- setDiffChanges: setDiffChanges
339
+ document: document
340
+ }), isShowChanges && /*#__PURE__*/React.createElement(RevisionDiffViewer, {
341
+ revisionContent: revisionContent,
342
+ didMountCallback: setDiffChanges
349
343
  }))), showTip && /*#__PURE__*/React.createElement(TipDialog, {
350
344
  toggle: closeTip,
351
345
  submit: tipSubmit,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seafile/sdoc-editor",
3
- "version": "0.1.166",
3
+ "version": "0.1.167",
4
4
  "private": false,
5
5
  "description": "This is a sdoc editor",
6
6
  "main": "dist/index.js",
@@ -1,33 +0,0 @@
1
- import React, { useEffect } from 'react';
2
- import context from '../../context';
3
- import { getDiff } from '../utils/diff';
4
- import SDocViewer from './sdoc-viewer';
5
- import '../../assets/css/diff-viewer.css';
6
- var DiffViewer = function DiffViewer(_ref) {
7
- var editor = _ref.editor,
8
- currentContent = _ref.currentContent,
9
- lastContent = _ref.lastContent,
10
- didMountCallback = _ref.didMountCallback,
11
- showToolbar = _ref.showToolbar,
12
- showOutline = _ref.showOutline;
13
- var diff = getDiff(currentContent, lastContent);
14
- context.initSettings();
15
- useEffect(function () {
16
- didMountCallback && didMountCallback(diff);
17
-
18
- // eslint-disable-next-line react-hooks/exhaustive-deps
19
- }, []);
20
- return /*#__PURE__*/React.createElement(SDocViewer, {
21
- editor: editor,
22
- showToolbar: showToolbar,
23
- showOutline: showOutline,
24
- document: {
25
- children: diff.value
26
- }
27
- });
28
- };
29
- DiffViewer.defaultProps = {
30
- showToolbar: false,
31
- showOutline: false
32
- };
33
- export default DiffViewer;