@seafile/sdoc-editor 0.1.50 → 0.1.51

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.
@@ -4,17 +4,12 @@ import randomColor from 'randomcolor';
4
4
  // selection: { anchor, focus }
5
5
  // cursor: { anchor, focus }
6
6
 
7
- export var setCursor = function setCursor(editor, operations, user, selection, cursorData) {
7
+ export var setCursor = function setCursor(editor, user, location, cursorData) {
8
8
  var clientId = user.username;
9
- var cursorOps = operations.filter(function (operation) {
10
- return operation.type === 'set_selection';
11
- });
12
9
  if (!editor.cursors) editor.cursors = {};
13
- var oldCursor = editor.cursors[clientId] ? editor.cursors[clientId] : {};
14
- var lastCursorOp = cursorOps[cursorOps.length - 1];
15
- if (selection) {
16
- var newCursor = lastCursorOp && lastCursorOp.newProperties || {};
17
- var newCursorData = _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, oldCursor), newCursor), selection), cursorData);
10
+ if (location) {
11
+ var oldCursor = editor.cursors[clientId] ? editor.cursors[clientId] : {};
12
+ var newCursorData = _objectSpread(_objectSpread(_objectSpread({}, oldCursor), location), cursorData);
18
13
  editor.cursors[clientId] = newCursorData;
19
14
  } else {
20
15
  delete editor.cursors[clientId];
@@ -251,7 +251,7 @@ export var reExecRevertOperationList = function reExecRevertOperationList(editor
251
251
  _loop2();
252
252
  }
253
253
  };
254
- export var syncRemoteOperations = function syncRemoteOperations(editor, remoteOperations, user, selection, cursorData) {
254
+ export var syncRemoteOperations = function syncRemoteOperations(editor, remoteOperations) {
255
255
  if (remoteOperations.length === 0) return;
256
256
  Editor.withoutNormalizing(editor, function () {
257
257
  for (var i = 0; i < remoteOperations.length; i++) {
@@ -261,11 +261,14 @@ export var syncRemoteOperations = function syncRemoteOperations(editor, remoteOp
261
261
  }
262
262
  editor.apply(op);
263
263
  }
264
- var currentUser = editor.user;
265
- if (user && user.username !== currentUser.username) {
266
- setCursor(editor, remoteOperations, user, selection, cursorData);
267
- // sync cursor position
268
- editor.onCursor && editor.onCursor(editor.cursors);
269
- }
270
264
  });
265
+ };
266
+ export var syncRemoteCursorLocation = function syncRemoteCursorLocation(editor, user, location, cursorData) {
267
+ var currentUser = editor.user;
268
+ if (user && user.username !== currentUser.username) {
269
+ setCursor(editor, user, location, cursorData);
270
+
271
+ // sync cursor position
272
+ editor.onCursor && editor.onCursor(editor.cursors);
273
+ }
271
274
  };
@@ -11,12 +11,10 @@ var SocketClient = /*#__PURE__*/_createClass(function SocketClient(config) {
11
11
  var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
12
12
  var _this$config = _this.config,
13
13
  docUuid = _this$config.docUuid,
14
- user = _this$config.user,
15
- cursorData = _this$config.cursorData;
14
+ user = _this$config.user;
16
15
  return _objectSpread({
17
16
  doc_uuid: docUuid,
18
- user: user,
19
- cursor_data: cursorData
17
+ user: user
20
18
  }, params);
21
19
  };
22
20
  this.onConnected = function () {
@@ -114,6 +112,17 @@ var SocketClient = /*#__PURE__*/_createClass(function SocketClient(config) {
114
112
  }
115
113
  });
116
114
  };
115
+ this.sendCursorLocation = function (location) {
116
+ var cursor_data = _this.config.cursorData;
117
+ _this.socket.emit('update-cursor', _this.getParams({
118
+ location: location,
119
+ cursor_data: cursor_data
120
+ }));
121
+ };
122
+ this.receiveCursorLocation = function (params) {
123
+ var socketManager = SocketManager.getInstance();
124
+ socketManager.receiveCursorLocation(params);
125
+ };
117
126
  this.disconnectWithServer = function () {
118
127
  _this.socket.disconnect();
119
128
  };
@@ -131,6 +140,7 @@ var SocketClient = /*#__PURE__*/_createClass(function SocketClient(config) {
131
140
  this.socket.on('join-room', this.onJoinRoom);
132
141
  this.socket.on('leave-room', this.onLeaveRoom);
133
142
  this.socket.on('update-document', this.receiveOperations);
143
+ this.socket.on('update-cursor', this.receiveCursorLocation);
134
144
  this.socket.io.on('reconnect', this.onReconnect);
135
145
  this.socket.io.on('reconnect_attempt', this.onReconnectAttempt);
136
146
  this.socket.io.on('reconnect_error', this.onReconnectError);
@@ -1,7 +1,7 @@
1
1
  import _createClass from "@babel/runtime/helpers/esm/createClass";
2
2
  import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
3
3
  import EventBus from '../utils/event-bus';
4
- import { syncRemoteOperations, reExecRevertOperationList, revertOperationList } from './helpers';
4
+ import { syncRemoteOperations, reExecRevertOperationList, revertOperationList, syncRemoteCursorLocation } from './helpers';
5
5
  import SocketClient from './socket-client';
6
6
  import debug from '../utils/debug';
7
7
  import { deleteCursor } from '../cursor/helper';
@@ -85,13 +85,10 @@ var SocketManager = /*#__PURE__*/_createClass(function SocketManager(editor, doc
85
85
  }
86
86
 
87
87
  // 2. execute operations
88
- var operations = params.operations,
89
- user = params.user,
90
- selection = params.selection,
91
- cursorData = params.cursor_data;
88
+ var operations = params.operations;
92
89
  // 2.1 Update content & version
93
90
  debug('execute remote operations: %O', operations);
94
- syncRemoteOperations(_this.editor, operations, user, selection, cursorData);
91
+ syncRemoteOperations(_this.editor, operations);
95
92
 
96
93
  // 2.2 Update document
97
94
  _this.document.version = serverVersion;
@@ -161,6 +158,16 @@ var SocketManager = /*#__PURE__*/_createClass(function SocketManager(editor, doc
161
158
  _this.editor.isRemote = false;
162
159
  });
163
160
  };
161
+ this.sendCursorLocation = function (location) {
162
+ _this.socketClient.sendCursorLocation(location);
163
+ };
164
+ this.receiveCursorLocation = function (params) {
165
+ var user = params.user,
166
+ location = params.location,
167
+ cursorData = params.cursor_data;
168
+ syncRemoteCursorLocation(_this.editor, user, location, cursorData);
169
+ return;
170
+ };
164
171
  this.dispatchConnectState = function (type, message) {
165
172
  if (type === 'leave-room') {
166
173
  deleteCursor(_this.editor, message);
@@ -20,8 +20,18 @@ var withSocketIO = function withSocketIO(editor, options) {
20
20
  newEditor.onChange = function () {
21
21
  var operations = newEditor.operations;
22
22
  if (!newEditor.isRemote && operations.length > 0) {
23
+ var isAllSetSelection = operations.every(function (operation) {
24
+ return operation.type === 'set_selection';
25
+ });
23
26
  var _socketManager = SocketManager.getInstance();
24
- _socketManager.addOperations && _socketManager.addOperations(operations);
27
+ if (!isAllSetSelection) {
28
+ // get update content value operations
29
+ var updateOperations = operations.filter(function (operation) {
30
+ return operation.type !== 'set_selection';
31
+ });
32
+ _socketManager.addOperations && _socketManager.addOperations(updateOperations);
33
+ }
34
+ _socketManager.sendCursorLocation(editor.selection);
25
35
  }
26
36
  onChange();
27
37
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seafile/sdoc-editor",
3
- "version": "0.1.50",
3
+ "version": "0.1.51",
4
4
  "private": false,
5
5
  "description": "This is a sdoc editor",
6
6
  "main": "dist/index.js",