@seafile/sdoc-editor 0.1.146 → 0.1.148-beta

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.
Files changed (35) hide show
  1. package/dist/api/sdoc-server-api.js +10 -0
  2. package/dist/api/seafile-api.js +27 -5
  3. package/dist/basic-sdk/comment/comment/comment-editor.js +4 -4
  4. package/dist/basic-sdk/constants/index.js +14 -1
  5. package/dist/basic-sdk/editor/common-editor.js +50 -0
  6. package/dist/basic-sdk/editor/index.css +29 -0
  7. package/dist/basic-sdk/editor/index.js +129 -0
  8. package/dist/basic-sdk/{slate-editor.js → editor/slate-editor.js} +21 -16
  9. package/dist/basic-sdk/extension/constants/element-type.js +4 -1
  10. package/dist/basic-sdk/extension/core/transforms/replace-node-children.js +26 -0
  11. package/dist/basic-sdk/extension/plugins/font/helpers.js +1 -1
  12. package/dist/basic-sdk/extension/plugins/link/hover/index.js +6 -2
  13. package/dist/basic-sdk/extension/plugins/table/render/render-cell.js +4 -2
  14. package/dist/basic-sdk/extension/render/render-element.js +209 -1
  15. package/dist/basic-sdk/socket/helpers.js +2 -0
  16. package/dist/basic-sdk/socket/socket-client.js +38 -0
  17. package/dist/basic-sdk/socket/socket-manager.js +34 -4
  18. package/dist/basic-sdk/socket/with-socket-io.js +35 -12
  19. package/dist/basic-sdk/utils/diff.js +2 -2
  20. package/dist/basic-sdk/utils/rebase.js +197 -0
  21. package/dist/basic-sdk/views/diff-viewer.js +3 -1
  22. package/dist/basic-sdk/views/viewer.js +8 -11
  23. package/dist/components/doc-operations/index.js +4 -2
  24. package/dist/components/doc-operations/revision-operations/index.js +5 -2
  25. package/dist/components/doc-operations/revision-operations/publish-button.js +6 -13
  26. package/dist/components/tip-dialog/index.css +0 -0
  27. package/dist/components/tip-dialog/index.js +49 -0
  28. package/dist/components/tip-dialog/tip-content.js +56 -0
  29. package/dist/constants/index.js +23 -2
  30. package/dist/context.js +35 -4
  31. package/dist/pages/simple-editor.js +247 -83
  32. package/package.json +1 -1
  33. package/public/locales/en/sdoc-editor.json +11 -1
  34. package/public/locales/zh_CN/sdoc-editor.json +10 -1
  35. package/dist/basic-sdk/editor.js +0 -105
@@ -1,10 +1,16 @@
1
+ import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
2
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
1
3
  import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
2
4
  import React, { useCallback } from 'react';
3
5
  import { useSlateStatic } from '@seafile/slate-react';
6
+ import { Transforms } from '@seafile/slate';
7
+ import deepCopy from 'deep-copy';
8
+ import { useTranslation } from 'react-i18next';
4
9
  import { BLOCKQUOTE, LINK, CHECK_LIST_ITEM, HEADER1, HEADER2, HEADER3, HEADER4, HEADER5, HEADER6, LIST_ITEM, LIST_LIC, ORDERED_LIST, PARAGRAPH, UNORDERED_LIST, CODE_BLOCK, CODE_LINE, IMAGE, ELEMENT_TYPE, SDOC_LINK, TITLE, SUBTITLE } from '../constants';
5
10
  import { BlockquotePlugin, LinkPlugin, CheckListPlugin, HeaderPlugin, ListPlugin, CodeBlockPlugin, ImagePlugin, TablePlugin, SdocLinkPlugin, ParagraphPlugin } from '../plugins';
6
11
  import EventBus from '../../utils/event-bus';
7
- import { INTERNAL_EVENT } from '../../constants';
12
+ import { INTERNAL_EVENT, REBASE_TYPE, REBASE_MARKS } from '../../constants';
13
+ import { findPath, getNode, replaceNode, generateEmptyElement, deleteNodeMark } from '../../extension/core';
8
14
  var CustomElement = function CustomElement(props) {
9
15
  var editor = useSlateStatic();
10
16
  var element = props.element,
@@ -140,6 +146,208 @@ var CustomElement = function CustomElement(props) {
140
146
  }
141
147
  };
142
148
  var RenderElement = function RenderElement(props) {
149
+ var editor = useSlateStatic();
150
+ var _useTranslation = useTranslation(),
151
+ t = _useTranslation.t;
152
+ var updateParentNodeByPath = useCallback(function (path) {
153
+ var parentPath = path.slice(0, -1);
154
+ var parentNode = getNode(editor, parentPath);
155
+ if (parentNode.children.filter(function (item) {
156
+ return item.rebaseType;
157
+ }).length === 0) {
158
+ var newParentElement = _objectSpread({}, parentNode);
159
+ newParentElement['rebaseType'] && delete newParentElement['rebaseType'];
160
+ newParentElement['oldElement'] && delete newParentElement['oldElement'];
161
+ newParentElement['origin'] && delete newParentElement['origin'];
162
+ newParentElement['children'] = newParentElement['children'].map(function (item) {
163
+ item['rebaseType'] && delete item['rebaseType'];
164
+ item['oldElement'] && delete item['oldElement'];
165
+ item['origin'] && delete item['origin'];
166
+ return item;
167
+ });
168
+ replaceNode(editor, {
169
+ at: parentPath,
170
+ nodes: newParentElement
171
+ });
172
+ }
173
+
174
+ // eslint-disable-next-line react-hooks/exhaustive-deps
175
+ }, [editor]);
176
+ var deleteElement = useCallback(function (element) {
177
+ var path = findPath(editor, element);
178
+ Transforms.removeNodes(editor, {
179
+ at: path
180
+ });
181
+ if (element.type === ELEMENT_TYPE.LIST_ITEM) {
182
+ updateParentNodeByPath(path);
183
+ }
184
+
185
+ // eslint-disable-next-line react-hooks/exhaustive-deps
186
+ }, [editor]);
187
+ var deleteMark = useCallback(function (element) {
188
+ var path = findPath(editor, element);
189
+ deleteNodeMark(editor, path, element, REBASE_MARKS);
190
+ if (element.type === ELEMENT_TYPE.LIST_ITEM) {
191
+ updateParentNodeByPath(path);
192
+ }
193
+
194
+ // eslint-disable-next-line react-hooks/exhaustive-deps
195
+ }, [editor]);
196
+ var addDeletedElement = useCallback(function (element) {
197
+ var path = findPath(editor, element);
198
+ deleteNodeMark(editor, path, element, REBASE_MARKS);
199
+ if (element.type !== ELEMENT_TYPE.LIST_ITEM) {
200
+ var emptyNode = generateEmptyElement(ELEMENT_TYPE.PARAGRAPH);
201
+ Transforms.insertNodes(editor, emptyNode, {
202
+ at: [path[0]]
203
+ });
204
+ } else {
205
+ var _emptyNode = generateEmptyElement(ELEMENT_TYPE.LIST_ITEM);
206
+ _emptyNode.children[0] = generateEmptyElement(ELEMENT_TYPE.LIST_LIC);
207
+ var parentPath = path.slice(0, -1);
208
+ var parentNode = getNode(editor, parentPath);
209
+ var newChildren = _toConsumableArray(parentNode.children);
210
+ var index = path[path.length - 1];
211
+ newChildren.splice(index, 0, _emptyNode);
212
+ replaceNode(editor, {
213
+ at: parentPath,
214
+ nodes: _objectSpread(_objectSpread({}, parentNode), {}, {
215
+ children: newChildren
216
+ })
217
+ });
218
+ }
219
+ if (element.type === ELEMENT_TYPE.LIST_ITEM) {
220
+ updateParentNodeByPath(path);
221
+ }
222
+
223
+ // eslint-disable-next-line react-hooks/exhaustive-deps
224
+ }, [editor]);
225
+ var useMasterChanges = useCallback(function (element) {
226
+ var path = findPath(editor, element);
227
+ deleteNodeMark(editor, path, element, REBASE_MARKS);
228
+ var nextElementPath = _toConsumableArray(path);
229
+ nextElementPath[path.length - 1] = path[path.length - 1] + 1;
230
+ Transforms.removeNodes(editor, {
231
+ at: nextElementPath
232
+ });
233
+ if (element.type === ELEMENT_TYPE.LIST_ITEM) {
234
+ updateParentNodeByPath(path);
235
+ }
236
+
237
+ // eslint-disable-next-line react-hooks/exhaustive-deps
238
+ }, [editor]);
239
+ var useCurrentChanges = useCallback(function (element) {
240
+ var path = findPath(editor, element);
241
+ var currentElementPath = _toConsumableArray(path);
242
+ currentElementPath[path.length - 1] = path[path.length - 1] + 1;
243
+ var currentElement = getNode(editor, currentElementPath);
244
+ var newCurrentElement = deepCopy(currentElement);
245
+ deleteNodeMark(editor, currentElementPath, newCurrentElement, REBASE_MARKS);
246
+ Transforms.removeNodes(editor, {
247
+ at: path
248
+ });
249
+ if (element.type === ELEMENT_TYPE.LIST_ITEM) {
250
+ updateParentNodeByPath(path);
251
+ }
252
+
253
+ // eslint-disable-next-line react-hooks/exhaustive-deps
254
+ }, [editor]);
255
+ var useBothChanges = useCallback(function (element) {
256
+ // delete element marks
257
+ var path = findPath(editor, element);
258
+ deleteNodeMark(editor, path, element, REBASE_MARKS);
259
+
260
+ // delete next element marks
261
+ var nextElementPath = [].concat(_toConsumableArray(path.slice(0, -1)), [path[path.length - 1] + 1]);
262
+ var nextElement = getNode(editor, nextElementPath);
263
+ deleteNodeMark(editor, nextElementPath, nextElement, REBASE_MARKS);
264
+ if (element.type === ELEMENT_TYPE.LIST_ITEM) {
265
+ updateParentNodeByPath(path);
266
+ }
267
+
268
+ // eslint-disable-next-line react-hooks/exhaustive-deps
269
+ }, [editor]);
270
+ var element = props.element;
271
+ if (!element.rebaseType) {
272
+ return /*#__PURE__*/React.createElement(CustomElement, props);
273
+ }
274
+ if (element.rebaseType === REBASE_TYPE.DELETE_MODIFY) {
275
+ return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
276
+ className: "w-100 d-flex sdoc-rebase-btn-group",
277
+ contentEditable: false
278
+ }, /*#__PURE__*/React.createElement("div", {
279
+ className: "sdoc-rebase-btn",
280
+ onClick: function onClick() {
281
+ return deleteElement(element);
282
+ }
283
+ }, t('Keep_other_modification')), /*#__PURE__*/React.createElement("div", {
284
+ className: "mr-2 ml-2"
285
+ }, '|'), /*#__PURE__*/React.createElement("div", {
286
+ className: "sdoc-rebase-btn",
287
+ onClick: function onClick() {
288
+ return deleteMark(element);
289
+ }
290
+ }, t('Keep_my_modification')), /*#__PURE__*/React.createElement("div", {
291
+ className: "mr-2 ml-2"
292
+ }, '|'), /*#__PURE__*/React.createElement("div", {
293
+ className: "sdoc-rebase-btn",
294
+ onClick: function onClick() {
295
+ return addDeletedElement(element);
296
+ }
297
+ }, t('Keep_both_modification'))), /*#__PURE__*/React.createElement("div", {
298
+ className: "w-100 sdoc-rebase-current-changes-start",
299
+ contentEditable: false
300
+ }, '<<<<<<<'), /*#__PURE__*/React.createElement("div", {
301
+ className: "w-100",
302
+ contentEditable: false
303
+ }, '======='), /*#__PURE__*/React.createElement("div", {
304
+ className: "w-100 sdoc-rebase-incoming-changes"
305
+ }, /*#__PURE__*/React.createElement(CustomElement, props)), /*#__PURE__*/React.createElement("div", {
306
+ className: "w-100 sdoc-rebase-incoming-changes-end",
307
+ contentEditable: false
308
+ }, '>>>>>>>'));
309
+ }
310
+ if (element.rebaseType === REBASE_TYPE.MODIFY_MODIFY) {
311
+ if (element.origin === 'master') {
312
+ return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
313
+ className: "w-100 d-flex sdoc-rebase-btn-group",
314
+ contentEditable: false
315
+ }, /*#__PURE__*/React.createElement("div", {
316
+ className: "sdoc-rebase-btn",
317
+ onClick: function onClick() {
318
+ return useMasterChanges(element);
319
+ }
320
+ }, t('Keep_other_modification')), /*#__PURE__*/React.createElement("div", {
321
+ className: "mr-2 ml-2"
322
+ }, '|'), /*#__PURE__*/React.createElement("div", {
323
+ className: "sdoc-rebase-btn",
324
+ onClick: function onClick() {
325
+ return useCurrentChanges(element);
326
+ }
327
+ }, t('Keep_my_modification')), /*#__PURE__*/React.createElement("div", {
328
+ className: "mr-2 ml-2"
329
+ }, '|'), /*#__PURE__*/React.createElement("div", {
330
+ className: "sdoc-rebase-btn",
331
+ onClick: function onClick() {
332
+ return useBothChanges(element);
333
+ }
334
+ }, t('Keep_both_modification'))), /*#__PURE__*/React.createElement("div", {
335
+ className: "w-100 sdoc-rebase-current-changes-start",
336
+ contentEditable: false
337
+ }, '<<<<<<<'), /*#__PURE__*/React.createElement("div", {
338
+ className: "w-100 sdoc-rebase-current-changes"
339
+ }, /*#__PURE__*/React.createElement(CustomElement, props)));
340
+ }
341
+ return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
342
+ className: "w-100",
343
+ contentEditable: false
344
+ }, '======='), /*#__PURE__*/React.createElement("div", {
345
+ className: "w-100 sdoc-rebase-incoming-changes"
346
+ }, /*#__PURE__*/React.createElement(CustomElement, props)), /*#__PURE__*/React.createElement("div", {
347
+ className: "w-100 sdoc-rebase-incoming-changes-end",
348
+ contentEditable: false
349
+ }, '>>>>>>>'));
350
+ }
143
351
  return /*#__PURE__*/React.createElement(CustomElement, props);
144
352
 
145
353
  // const { element } = props;
@@ -4,6 +4,7 @@ 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';
7
8
  export var getNodePathById = function getNodePathById(rootNode, nodeId) {
8
9
  var path = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
9
10
  if (rootNode.id === nodeId) return path;
@@ -252,6 +253,7 @@ export var reExecRevertOperationList = function reExecRevertOperationList(editor
252
253
  }
253
254
  };
254
255
  export var syncRemoteOperations = function syncRemoteOperations(editor, remoteOperations) {
256
+ if (editor.mode !== MODE.EDITOR) return;
255
257
  if (remoteOperations.length === 0) return;
256
258
  Editor.withoutNormalizing(editor, function () {
257
259
  for (var i = 0; i < remoteOperations.length; i++) {
@@ -119,6 +119,38 @@ var SocketClient = /*#__PURE__*/_createClass(function SocketClient(config) {
119
119
  this.disconnectWithServer = function () {
120
120
  _this.socket.disconnect();
121
121
  };
122
+ this.sendPublishDocument = function (originDocUuid, originDocName, isNeedUpdateOriginDoc, callback) {
123
+ _this.socket.emit('publish-document', _this.getParams({
124
+ origin_doc_uuid: originDocUuid,
125
+ origin_doc_name: originDocName,
126
+ update_origin_doc: isNeedUpdateOriginDoc
127
+ }), function (result) {
128
+ callback && callback(result);
129
+ });
130
+ };
131
+ this.receivePublishDocument = function () {
132
+ var socketManager = SocketManager.getInstance();
133
+ socketManager.receivePublishDocument();
134
+ };
135
+ this.receivePublishDocumentError = function () {
136
+ var socketManager = SocketManager.getInstance();
137
+ socketManager.receivePublishDocumentError();
138
+ };
139
+ this.sendReplaceDocument = function (document, callback) {
140
+ _this.socket.emit('replace-doc', _this.getParams({
141
+ document: document
142
+ }), function (result) {
143
+ callback && callback(result);
144
+ });
145
+ };
146
+ this.receiveDocumentReplaced = function () {
147
+ var socketManager = SocketManager.getInstance();
148
+ socketManager.receiveDocumentReplaced();
149
+ };
150
+ this.receiveDocumentReplacedError = function () {
151
+ var socketManager = SocketManager.getInstance();
152
+ socketManager.receiveDocumentReplacedError();
153
+ };
122
154
  this.config = config;
123
155
  this.isReconnect = false;
124
156
  this.socket = io(config.sdocServer, {
@@ -133,6 +165,12 @@ var SocketClient = /*#__PURE__*/_createClass(function SocketClient(config) {
133
165
  this.socket.on('join-room', this.onJoinRoom);
134
166
  this.socket.on('leave-room', this.onLeaveRoom);
135
167
  this.socket.on('update-document', this.onReceiveRemoteOperations);
168
+
169
+ // doc-replaced
170
+ this.socket.on('doc-replaced', this.receiveDocumentReplaced);
171
+ this.socket.on('doc-replaced-error', this.receiveDocumentReplacedError);
172
+ this.socket.on('publish-document', this.receivePublishDocument);
173
+ this.socket.on('publish-document-error', this.receivePublishDocumentError);
136
174
  this.socket.on('update-cursor', this.receiveCursorLocation);
137
175
  this.socket.io.on('reconnect', this.onReconnect);
138
176
  this.socket.io.on('reconnect_attempt', this.onReconnectAttempt);
@@ -5,8 +5,9 @@ var _class;
5
5
  import EventBus from '../utils/event-bus';
6
6
  import { syncRemoteOperations, reExecRevertOperationList, revertOperationList, syncRemoteCursorLocation } from './helpers';
7
7
  import SocketClient from './socket-client';
8
- import { conflictDebug, serverDebug, stateDebug } from '../utils/debug';
8
+ import { clientDebug, conflictDebug, serverDebug, stateDebug } from '../utils/debug';
9
9
  import { deleteCursor } from '../cursor/helper';
10
+ import { EXTERNAL_EVENT, MODE } from '../../constants';
10
11
 
11
12
  // idle --> sending --> conflict --> idle
12
13
  // --> conflict --> idle
@@ -19,13 +20,34 @@ var STATE = {
19
20
  DISCONNECT: 'disconnect',
20
21
  NEED_RELOAD: 'need_reload'
21
22
  };
22
- var SocketManager = /*#__PURE__*/_createClass(function SocketManager(editor, document, config) {
23
+ var SocketManager = /*#__PURE__*/_createClass(function SocketManager(editor, _document, config) {
23
24
  var _this = this;
24
25
  _classCallCheck(this, SocketManager);
25
26
  this.getDocumentVersion = function () {
26
27
  var version = _this.document.version;
27
28
  return version;
28
29
  };
30
+ this.sendPublishDocument = function (originDocUuid, originDocName, isNeedUpdateOriginDoc) {
31
+ _this.socketClient.sendPublishDocument(originDocUuid, originDocName, isNeedUpdateOriginDoc);
32
+ };
33
+ this.receivePublishDocument = function () {
34
+ _this.eventBus.dispatch(EXTERNAL_EVENT.PUBLISH_DOCUMENT);
35
+ };
36
+ this.receivePublishDocumentError = function () {
37
+ _this.eventBus.dispatch(EXTERNAL_EVENT.PUBLISH_DOCUMENT_ERROR);
38
+ };
39
+ this.sendReplaceDocument = function (document) {
40
+ _this.socketClient.sendReplaceDocument(document, function (result) {
41
+ var serverVersion = result.version;
42
+ _this.document['version'] = serverVersion;
43
+ });
44
+ };
45
+ this.receiveDocumentReplaced = function () {
46
+ _this.eventBus.dispatch(EXTERNAL_EVENT.DOCUMENT_REPLACED);
47
+ };
48
+ this.receiveDocumentReplacedError = function () {
49
+ _this.eventBus.dispatch(EXTERNAL_EVENT.DOCUMENT_REPLACED_ERROR);
50
+ };
29
51
  this.onReceiveLocalOperations = function (operations) {
30
52
  _this.pendingOperationList.push(operations);
31
53
  if (_this.pendingOperationList.length > 5) {
@@ -34,6 +56,7 @@ var SocketManager = /*#__PURE__*/_createClass(function SocketManager(editor, doc
34
56
  _this.sendOperations();
35
57
  };
36
58
  this.sendOperations = function () {
59
+ if (_this.editor.mode !== MODE.EDITOR) return;
37
60
  if (_this.state !== STATE.IDLE) return;
38
61
  stateDebug("State changed: ".concat(_this.state, " -> ").concat(STATE.SENDING));
39
62
  _this.state = STATE.SENDING;
@@ -148,6 +171,12 @@ var SocketManager = /*#__PURE__*/_createClass(function SocketManager(editor, doc
148
171
  // The version consistency indicates that there is no conflict and no processing is required
149
172
  stateDebug("State Changed: ".concat(_this.state, " -> ").concat(STATE.IDLE));
150
173
  _this.state = STATE.IDLE;
174
+ if (_this.pendingOperationList.length > 0) {
175
+ clientDebug('After reconnection, manually trigger the execution of ops.');
176
+ stateDebug("State Changed: ".concat(_this.state, " -> ").concat(STATE.SENDING));
177
+ _this.state = STATE.SENDING;
178
+ _this.sendNextOperations();
179
+ }
151
180
  };
152
181
  this.onConflictHappen = function () {
153
182
  stateDebug("State Changed: ".concat(_this.state, " -> ").concat(STATE.CONFLICT));
@@ -272,11 +301,12 @@ var SocketManager = /*#__PURE__*/_createClass(function SocketManager(editor, doc
272
301
  _this.editor.onCursor && _this.editor.onCursor(_this.editor.cursors);
273
302
  }
274
303
  if (type === 'disconnect') {
304
+ // current state is sending
275
305
  if (_this._sendingOperations) {
276
306
  _this.pendingOperationList.unshift(_this._sendingOperations.slice());
277
307
  _this._sendingOperations = null;
278
308
  }
279
- stateDebug("State Changed: ".concat(_this.state, " -> ").concat(STATE.IDLE));
309
+ stateDebug("State Changed: ".concat(_this.state, " -> ").concat(STATE.DISCONNECT));
280
310
  _this.state = STATE.DISCONNECT;
281
311
  }
282
312
  _this.eventBus.dispatch(type, message);
@@ -285,7 +315,7 @@ var SocketManager = /*#__PURE__*/_createClass(function SocketManager(editor, doc
285
315
  _this.socketClient.disconnectWithServer();
286
316
  };
287
317
  this.editor = editor;
288
- this.document = document;
318
+ this.document = _document;
289
319
  this.socketClient = new SocketClient(config);
290
320
  this.pendingOperationList = []; // Two-dimensional arrays: [operations, operations, ...]
291
321
  this.remoteOperationsList = []; // Same with pending operations
@@ -1,3 +1,4 @@
1
+ import { MODE } from '../../constants';
1
2
  import { generateCursorData } from '../cursor/helper';
2
3
  import EventBus from '../utils/event-bus';
3
4
  import SocketManager from './socket-manager';
@@ -19,28 +20,50 @@ var withSocketIO = function withSocketIO(editor, options) {
19
20
  SocketManager.destroy();
20
21
  };
21
22
  newEditor.onChange = function () {
22
- if (newEditor.readonly) return;
23
+ var document = options.document,
24
+ config = options.config;
25
+ if (newEditor.mode === MODE.DIFF_VIEWER) return;
23
26
  var operations = newEditor.operations;
24
27
  if (!newEditor.isRemote && operations.length > 0) {
25
28
  var isAllSetSelection = operations.every(function (operation) {
26
29
  return operation.type === 'set_selection';
27
30
  });
28
- var _socketManager = SocketManager.getInstance();
31
+ var _socketManager = SocketManager.getInstance(newEditor, document, config);
29
32
  if (!isAllSetSelection) {
30
- // get update content value operations
31
- var updateOperations = operations.filter(function (operation) {
32
- return operation.type !== 'set_selection';
33
- });
34
- _socketManager.onReceiveLocalOperations(updateOperations);
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
+ }
35
40
  }
36
- _socketManager.sendCursorLocation(editor.selection);
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
49
  }
38
-
39
- // dispatch editor change event
40
- var eventBus = EventBus.getInstance();
41
- eventBus.dispatch('change');
42
50
  onChange();
43
51
  };
52
+ newEditor.rebaseContent = function (document, originFileVersion) {
53
+ var config = options.config;
54
+ var socketManager = SocketManager.getInstance(newEditor, document, config);
55
+ socketManager.sendRebaseContent(document, originFileVersion);
56
+ };
57
+ newEditor.publishDocument = function (originDocUuid, originDocName, isNeedUpdateOriginDoc) {
58
+ var config = options.config;
59
+ var socketManager = SocketManager.getInstance(newEditor, document, config);
60
+ socketManager.sendPublishDocument(originDocUuid, originDocName, isNeedUpdateOriginDoc);
61
+ };
62
+ newEditor.replaceDocument = function (document) {
63
+ var config = options.config;
64
+ var socketManager = SocketManager.getInstance(newEditor, document, config);
65
+ socketManager.sendReplaceDocument(document);
66
+ };
44
67
  return newEditor;
45
68
  };
46
69
  export default withSocketIO;
@@ -31,7 +31,7 @@ var generatorDiffElement = function generatorDiffElement(element, diffType) {
31
31
  return generatorDiffElement(item, diffType, style);
32
32
  })), _objectSpread3));
33
33
  };
34
- var generateIdMapAndIds = function generateIdMapAndIds(elements) {
34
+ export var generateIdMapAndIds = function generateIdMapAndIds(elements) {
35
35
  var map = {};
36
36
  var ids = [];
37
37
  if (!Array.isArray(elements) || elements.length === 0) return {
@@ -54,7 +54,7 @@ var hasChildren = function hasChildren(element) {
54
54
  };
55
55
 
56
56
  // id diffs
57
- var getIdDiffs = function getIdDiffs(oldIds, newIds) {
57
+ export var getIdDiffs = function getIdDiffs(oldIds, newIds) {
58
58
  var diff = new DiffText(oldIds, newIds);
59
59
  return diff.getDiffs();
60
60
  };
@@ -0,0 +1,197 @@
1
+ import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
2
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
3
+ import { generateIdMapAndIds, getIdDiffs } from './diff';
4
+ import ObjectUtils from './object-utils';
5
+ import { MODIFY_TYPE, REBASE_TYPE } from '../constants';
6
+ import { ELEMENT_TYPE } from '../extension/constants';
7
+ import { replaceNodeId } from '../node-id/helpers';
8
+ var getRevisionChanges = function getRevisionChanges(masterContent, currentContent) {
9
+ var _generateIdMapAndIds = generateIdMapAndIds(currentContent.children),
10
+ currentContentMap = _generateIdMapAndIds.map,
11
+ currentIds = _generateIdMapAndIds.ids;
12
+ var _generateIdMapAndIds2 = generateIdMapAndIds(masterContent.children),
13
+ masterContentMap = _generateIdMapAndIds2.map,
14
+ masterIds = _generateIdMapAndIds2.ids;
15
+ var idDiffs = getIdDiffs(masterIds, currentIds);
16
+ var content = [];
17
+ idDiffs.forEach(function (idDiff) {
18
+ var value = idDiff.value,
19
+ added = idDiff.added,
20
+ removed = idDiff.removed;
21
+ if (added) {
22
+ var addedList = value.map(function (item) {
23
+ return _objectSpread(_objectSpread({}, currentContentMap[item]), {}, {
24
+ modifyType: MODIFY_TYPE.ADD
25
+ });
26
+ });
27
+ content.push.apply(content, _toConsumableArray(addedList));
28
+ } else if (removed) {
29
+ var deletedList = value.map(function (item) {
30
+ return _objectSpread(_objectSpread({}, masterContentMap[item]), {}, {
31
+ modifyType: MODIFY_TYPE.DELETE
32
+ });
33
+ });
34
+ content.push.apply(content, _toConsumableArray(deletedList));
35
+ } else {
36
+ value.forEach(function (elementId) {
37
+ if (ObjectUtils.isSameObject(masterContentMap[elementId], currentContentMap[elementId])) {
38
+ content.push(currentContentMap[elementId]);
39
+ } else {
40
+ var oldElement = masterContentMap[elementId];
41
+ var currentElement = currentContentMap[elementId];
42
+ var newElement = _objectSpread(_objectSpread({}, currentElement), {}, {
43
+ modifyType: MODIFY_TYPE.MODIFY,
44
+ oldElement: oldElement
45
+ });
46
+ if (currentElement.type === oldElement.type) {
47
+ var elementType = currentElement.type;
48
+ if ([ELEMENT_TYPE.UNORDERED_LIST, ELEMENT_TYPE.ORDERED_LIST].includes(elementType)) {
49
+ var listContent = getRevisionChanges(oldElement, currentElement);
50
+ newElement['modifyType'] = MODIFY_TYPE.CHILDREN_MODIFY;
51
+ newElement['children'] = listContent;
52
+ }
53
+ }
54
+ content.push(newElement);
55
+ }
56
+ });
57
+ }
58
+ });
59
+ return content;
60
+ };
61
+ var getMasterChanges = function getMasterChanges(masterContent, revisionChanges) {
62
+ var canMerge = true;
63
+ var _generateIdMapAndIds3 = generateIdMapAndIds(revisionChanges),
64
+ currentContentMap = _generateIdMapAndIds3.map,
65
+ currentIds = _generateIdMapAndIds3.ids;
66
+ var _generateIdMapAndIds4 = generateIdMapAndIds(masterContent.children),
67
+ masterContentMap = _generateIdMapAndIds4.map,
68
+ masterIds = _generateIdMapAndIds4.ids;
69
+ var idDiffs = getIdDiffs(masterIds, currentIds);
70
+ var content = [];
71
+ idDiffs.forEach(function (idDiff) {
72
+ var value = idDiff.value,
73
+ added = idDiff.added,
74
+ removed = idDiff.removed;
75
+ if (added) {
76
+ value.forEach(function (elementId) {
77
+ var element = currentContentMap[elementId];
78
+ var newElement = {
79
+ id: element.id,
80
+ children: element.children,
81
+ type: element.type
82
+ };
83
+ // The content has been deleted from the master, but this branch has been modified. At this time, rebase is required, and the user needs to confirm the selected content
84
+ // ==> delete | use modification
85
+ if (element.modifyType === MODIFY_TYPE.MODIFY) {
86
+ newElement['rebaseType'] = REBASE_TYPE.DELETE_MODIFY;
87
+ content.push(newElement);
88
+ canMerge = false;
89
+ } else {
90
+ // new direct append
91
+ content.push(newElement);
92
+ }
93
+ });
94
+ } else if (removed) {
95
+ var deletedList = value.map(function (item) {
96
+ return _objectSpread({}, masterContentMap[item]);
97
+ });
98
+ content.push.apply(content, _toConsumableArray(deletedList));
99
+ } else {
100
+ value.forEach(function (elementId) {
101
+ var currentElement = currentContentMap[elementId];
102
+ var masterElement = masterContentMap[elementId];
103
+
104
+ // Unchanged
105
+ if (ObjectUtils.isSameObject(masterElement, currentElement)) {
106
+ content.push(currentElement);
107
+
108
+ // The content of the master branch is modified in the current branch, but the content of the master branch is the same as that of origin
109
+ } else if (ObjectUtils.isSameObject(masterElement, currentElement.oldElement)) {
110
+ var newElement = _objectSpread({}, currentElement);
111
+ newElement['oldElement'] && delete newElement['oldElement'];
112
+ newElement['modifyType'] && delete newElement['modifyType'];
113
+ content.push(newElement);
114
+
115
+ // Modify at the same time
116
+ } else if (currentElement.modifyType === MODIFY_TYPE.MODIFY) {
117
+ var oldMasterElement = replaceNodeId(_objectSpread(_objectSpread({}, masterElement), {}, {
118
+ rebaseType: REBASE_TYPE.MODIFY_MODIFY,
119
+ origin: 'master'
120
+ }));
121
+ content.push(oldMasterElement);
122
+ var _newElement = _objectSpread(_objectSpread({}, currentElement), {}, {
123
+ rebaseType: REBASE_TYPE.MODIFY_MODIFY,
124
+ origin: 'current'
125
+ });
126
+ _newElement['modifyType'] && delete _newElement['modifyType'];
127
+ _newElement['oldElement'] && delete _newElement['oldElement'];
128
+ content.push(_newElement);
129
+ canMerge = false;
130
+
131
+ // children modify
132
+ } else if (currentElement.modifyType === MODIFY_TYPE.CHILDREN_MODIFY) {
133
+ canMerge = false;
134
+ if (currentElement.type === masterElement.type) {
135
+ var _getMasterChanges = getMasterChanges(masterElement, currentElement.children),
136
+ childrenContent = _getMasterChanges.content;
137
+ content.push(_objectSpread(_objectSpread({}, masterElement), {}, {
138
+ children: childrenContent,
139
+ rebaseType: REBASE_TYPE.CHILDREN_MODIFY
140
+ }));
141
+ } else {
142
+ var _oldMasterElement = replaceNodeId(_objectSpread(_objectSpread({}, masterElement), {}, {
143
+ rebaseType: REBASE_TYPE.MODIFY_MODIFY,
144
+ origin: 'master'
145
+ }));
146
+ content.push(_oldMasterElement);
147
+ var _newElement2 = _objectSpread(_objectSpread({}, currentElement.oldElement), {}, {
148
+ rebaseType: REBASE_TYPE.MODIFY_MODIFY,
149
+ origin: 'current'
150
+ });
151
+ content.push(_newElement2);
152
+ }
153
+ } else {
154
+ var _newElement3 = _objectSpread({}, currentElement);
155
+ _newElement3['oldElement'] && delete _newElement3['oldElement'];
156
+ _newElement3['modifyType'] && delete _newElement3['modifyType'];
157
+ content.push(_newElement3);
158
+ }
159
+ });
160
+ }
161
+ });
162
+ return {
163
+ content: content,
164
+ canMerge: canMerge
165
+ };
166
+ };
167
+ export var getRebase = function getRebase(masterContent, revisionMasterContent, revisionCurrentContent) {
168
+ // master no changes, merged directly
169
+ if (masterContent.version === revisionMasterContent.version) {
170
+ return {
171
+ canMerge: true,
172
+ isNeedReplaceMaster: true,
173
+ value: revisionCurrentContent
174
+ };
175
+ }
176
+
177
+ // The revision content has not changed
178
+ if (revisionMasterContent.version === revisionCurrentContent.version) {
179
+ return {
180
+ canMerge: true,
181
+ isNeedReplaceMaster: false,
182
+ value: masterContent
183
+ };
184
+ }
185
+ var revisionChanges = getRevisionChanges(revisionMasterContent, revisionCurrentContent);
186
+ var _getMasterChanges2 = getMasterChanges(masterContent, revisionChanges),
187
+ canMerge = _getMasterChanges2.canMerge,
188
+ content = _getMasterChanges2.content;
189
+ return {
190
+ canMerge: canMerge,
191
+ isNeedReplaceMaster: true,
192
+ value: _objectSpread(_objectSpread({}, revisionCurrentContent), {}, {
193
+ children: content,
194
+ version: Math.max(masterContent.version, revisionCurrentContent.version) + 1
195
+ })
196
+ };
197
+ };