@seafile/sdoc-editor 0.1.44 → 0.1.45
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/basic-sdk/cursor/decorate.js +35 -0
- package/dist/basic-sdk/cursor/helper.js +40 -0
- package/dist/basic-sdk/editor.js +4 -1
- package/dist/basic-sdk/extension/plugins/text-style/caret.js +47 -0
- package/dist/basic-sdk/extension/plugins/text-style/render-elem.js +6 -1
- package/dist/basic-sdk/socket/helpers.js +10 -4
- package/dist/basic-sdk/socket/socket-client.js +4 -2
- package/dist/basic-sdk/socket/socket-manager.js +3 -2
- package/dist/basic-sdk/socket/with-socket-io.js +7 -3
- package/package.json +2 -1
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
2
|
+
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
|
3
|
+
import { Text, Path, Range } from '@seafile/slate';
|
|
4
|
+
export var getDecorate = function getDecorate(editor) {
|
|
5
|
+
return function (nodeEntry) {
|
|
6
|
+
var ranges = [];
|
|
7
|
+
var _nodeEntry = _slicedToArray(nodeEntry, 2),
|
|
8
|
+
node = _nodeEntry[0],
|
|
9
|
+
path = _nodeEntry[1];
|
|
10
|
+
var cursors = Object.values(editor.cursors || {});
|
|
11
|
+
if (Text.isText(node) && (cursors === null || cursors === void 0 ? void 0 : cursors.length)) {
|
|
12
|
+
cursors.forEach(function (cursor) {
|
|
13
|
+
if (Range.includes(cursor, path)) {
|
|
14
|
+
var focus = cursor.focus,
|
|
15
|
+
anchor = cursor.anchor,
|
|
16
|
+
isForward = cursor.isForward;
|
|
17
|
+
var isFocusNode = Path.equals(focus.path, path);
|
|
18
|
+
var isAnchorNode = Path.equals(anchor.path, path);
|
|
19
|
+
ranges.push(_objectSpread(_objectSpread({}, cursor), {}, {
|
|
20
|
+
isCaret: isFocusNode,
|
|
21
|
+
anchor: {
|
|
22
|
+
path: path,
|
|
23
|
+
offset: isAnchorNode ? anchor.offset : isForward ? 0 : node.text.length
|
|
24
|
+
},
|
|
25
|
+
focus: {
|
|
26
|
+
path: path,
|
|
27
|
+
offset: isFocusNode ? focus.offset : isForward ? node.text.length : 0
|
|
28
|
+
}
|
|
29
|
+
}));
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
return ranges;
|
|
34
|
+
};
|
|
35
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
2
|
+
import { Range } from '@seafile/slate';
|
|
3
|
+
import randomColor from 'randomcolor';
|
|
4
|
+
|
|
5
|
+
// selection: { anchor, focus }
|
|
6
|
+
// cursor: { anchor, focus }
|
|
7
|
+
|
|
8
|
+
export var setCursor = function setCursor(id, editor, operations, cursorData) {
|
|
9
|
+
var cursorOps = operations.filter(function (operation) {
|
|
10
|
+
return operation.type === 'set_selection';
|
|
11
|
+
});
|
|
12
|
+
if (!editor.cursors) editor.cursors = {};
|
|
13
|
+
var oldCursor = editor.cursors[id] ? editor.cursors[id] : {};
|
|
14
|
+
var lastCursorOp = cursorOps[cursorOps.length - 1];
|
|
15
|
+
var selection = editor.selection;
|
|
16
|
+
if (selection) {
|
|
17
|
+
var newCursor = lastCursorOp && lastCursorOp.newProperties || {};
|
|
18
|
+
var newCursorData = _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, oldCursor), newCursor), selection), _objectSpread(_objectSpread({}, cursorData), {}, {
|
|
19
|
+
isForward: Range.isForward(selection)
|
|
20
|
+
}));
|
|
21
|
+
editor.cursors[id] = newCursorData;
|
|
22
|
+
} else {
|
|
23
|
+
delete editor.cursors[id];
|
|
24
|
+
}
|
|
25
|
+
return editor;
|
|
26
|
+
};
|
|
27
|
+
export var generateCursorData = function generateCursorData(config) {
|
|
28
|
+
var user = config.user;
|
|
29
|
+
var options = {
|
|
30
|
+
luminosity: 'dark',
|
|
31
|
+
format: 'rgba',
|
|
32
|
+
alpha: 1
|
|
33
|
+
};
|
|
34
|
+
var color = randomColor(options);
|
|
35
|
+
return {
|
|
36
|
+
name: user.name,
|
|
37
|
+
color: color,
|
|
38
|
+
alphaColor: color.slice(0, -2) + '0.2)'
|
|
39
|
+
};
|
|
40
|
+
};
|
package/dist/basic-sdk/editor.js
CHANGED
|
@@ -13,6 +13,7 @@ import withNodeId from './node-id';
|
|
|
13
13
|
import SDocOutline from './outline';
|
|
14
14
|
import EventProxy from './utils/event-handler';
|
|
15
15
|
import { focusEditor } from './extension/core';
|
|
16
|
+
import { getDecorate } from './cursor/decorate';
|
|
16
17
|
import './assets/css/layout.css';
|
|
17
18
|
import './assets/css/sdoc-editor-plugins.css';
|
|
18
19
|
var SDocEditor = /*#__PURE__*/function (_React$Component) {
|
|
@@ -84,6 +85,7 @@ var SDocEditor = /*#__PURE__*/function (_React$Component) {
|
|
|
84
85
|
var slateValue = this.state.slateValue;
|
|
85
86
|
var config = this.props.config;
|
|
86
87
|
var docUuid = config.docUuid;
|
|
88
|
+
var decorate = getDecorate(editor);
|
|
87
89
|
return /*#__PURE__*/React.createElement("div", {
|
|
88
90
|
className: "sdoc-editor-container"
|
|
89
91
|
}, /*#__PURE__*/React.createElement(Toolbar, {
|
|
@@ -109,7 +111,8 @@ var SDocEditor = /*#__PURE__*/function (_React$Component) {
|
|
|
109
111
|
return _renderLeaf(props, _this2.editor);
|
|
110
112
|
},
|
|
111
113
|
onKeyDown: this.eventProxy.onKeyDown,
|
|
112
|
-
onDOMBeforeInput: this.onDOMBeforeInput
|
|
114
|
+
onDOMBeforeInput: this.onDOMBeforeInput,
|
|
115
|
+
decorate: decorate
|
|
113
116
|
}))))));
|
|
114
117
|
}
|
|
115
118
|
}]);
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
2
|
+
import React from 'react';
|
|
3
|
+
var cursorStyleBase = {
|
|
4
|
+
position: 'absolute',
|
|
5
|
+
top: -2,
|
|
6
|
+
pointerEvents: 'none',
|
|
7
|
+
userSelect: 'none',
|
|
8
|
+
transform: 'translateY(-100%)',
|
|
9
|
+
fontSize: 10,
|
|
10
|
+
color: 'white',
|
|
11
|
+
background: 'palevioletred',
|
|
12
|
+
whiteSpace: 'nowrap'
|
|
13
|
+
};
|
|
14
|
+
var caretStyleBase = {
|
|
15
|
+
position: 'absolute',
|
|
16
|
+
pointerEvents: 'none',
|
|
17
|
+
userSelect: 'none',
|
|
18
|
+
height: '1.2em',
|
|
19
|
+
width: 2,
|
|
20
|
+
background: 'palevioletred'
|
|
21
|
+
};
|
|
22
|
+
var Caret = function Caret(_ref) {
|
|
23
|
+
var color = _ref.color,
|
|
24
|
+
isForward = _ref.isForward,
|
|
25
|
+
name = _ref.name;
|
|
26
|
+
var cursorStyles = _objectSpread(_objectSpread({}, cursorStyleBase), {}, {
|
|
27
|
+
background: color,
|
|
28
|
+
left: isForward ? '100%' : '0%'
|
|
29
|
+
});
|
|
30
|
+
var caretStyles = _objectSpread(_objectSpread({}, caretStyleBase), {}, {
|
|
31
|
+
background: color,
|
|
32
|
+
left: isForward ? '100%' : '0%'
|
|
33
|
+
});
|
|
34
|
+
caretStyles[isForward ? 'bottom' : 'top'] = 0;
|
|
35
|
+
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("span", {
|
|
36
|
+
contentEditable: false,
|
|
37
|
+
style: caretStyles
|
|
38
|
+
}, /*#__PURE__*/React.createElement("span", {
|
|
39
|
+
style: {
|
|
40
|
+
position: 'relative'
|
|
41
|
+
}
|
|
42
|
+
}, /*#__PURE__*/React.createElement("span", {
|
|
43
|
+
contentEditable: false,
|
|
44
|
+
style: cursorStyles
|
|
45
|
+
}, name))));
|
|
46
|
+
};
|
|
47
|
+
export default Caret;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
+
import Caret from './caret';
|
|
2
3
|
var renderText = function renderText(props, editor) {
|
|
3
4
|
var attributes = props.attributes,
|
|
4
5
|
children = props.children,
|
|
@@ -26,6 +27,10 @@ var renderText = function renderText(props, editor) {
|
|
|
26
27
|
}
|
|
27
28
|
return /*#__PURE__*/React.createElement("span", Object.assign({
|
|
28
29
|
"data-id": leaf.id
|
|
29
|
-
}, attributes
|
|
30
|
+
}, attributes, {
|
|
31
|
+
style: {
|
|
32
|
+
position: 'relative'
|
|
33
|
+
}
|
|
34
|
+
}), leaf.isCaret ? /*#__PURE__*/React.createElement(Caret, leaf) : null, markedChildren);
|
|
30
35
|
};
|
|
31
36
|
export default renderText;
|
|
@@ -3,6 +3,7 @@ import deepCopy from 'deep-copy';
|
|
|
3
3
|
import { Editor, Operation } from '@seafile/slate';
|
|
4
4
|
import { getNode } from '../extension/core';
|
|
5
5
|
import * as OPERATION from '../node-id/constants';
|
|
6
|
+
import { setCursor } from '../cursor/helper';
|
|
6
7
|
export var getNodePathById = function getNodePathById(rootNode, nodeId) {
|
|
7
8
|
var path = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
|
|
8
9
|
if (rootNode.id === nodeId) return path;
|
|
@@ -250,11 +251,16 @@ export var reExecRevertOperationList = function reExecRevertOperationList(editor
|
|
|
250
251
|
_loop2();
|
|
251
252
|
}
|
|
252
253
|
};
|
|
253
|
-
export var syncRemoteOperations = function syncRemoteOperations(editor, remoteOperations) {
|
|
254
|
+
export var syncRemoteOperations = function syncRemoteOperations(editor, remoteOperations, user) {
|
|
254
255
|
if (remoteOperations.length === 0) return;
|
|
255
256
|
Editor.withoutNormalizing(editor, function () {
|
|
256
|
-
remoteOperations.
|
|
257
|
-
|
|
258
|
-
|
|
257
|
+
for (var i = 0; i < remoteOperations.length; i++) {
|
|
258
|
+
var op = remoteOperations[i];
|
|
259
|
+
if (op.type === 'set_selection') {
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
editor.apply(op);
|
|
263
|
+
}
|
|
264
|
+
setCursor(user.username, editor, remoteOperations);
|
|
259
265
|
});
|
|
260
266
|
};
|
|
@@ -11,10 +11,12 @@ 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
|
|
14
|
+
user = _this$config.user,
|
|
15
|
+
cursorData = _this$config.cursorData;
|
|
15
16
|
return _objectSpread({
|
|
16
17
|
doc_uuid: docUuid,
|
|
17
|
-
user: user
|
|
18
|
+
user: user,
|
|
19
|
+
cursor_data: cursorData
|
|
18
20
|
}, params);
|
|
19
21
|
};
|
|
20
22
|
this.onConnected = function () {
|
|
@@ -83,10 +83,11 @@ var SocketManager = /*#__PURE__*/_createClass(function SocketManager(editor, doc
|
|
|
83
83
|
}
|
|
84
84
|
|
|
85
85
|
// 2. execute operations
|
|
86
|
-
var operations = params.operations
|
|
86
|
+
var operations = params.operations,
|
|
87
|
+
user = params.user;
|
|
87
88
|
// 2.1 Update content & version
|
|
88
89
|
debug('execute remote operations: %O', operations);
|
|
89
|
-
syncRemoteOperations(_this.editor, operations);
|
|
90
|
+
syncRemoteOperations(_this.editor, operations, user);
|
|
90
91
|
|
|
91
92
|
// 2.2 Update document
|
|
92
93
|
_this.document.version = serverVersion;
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
var _this = this;
|
|
2
|
+
import { generateCursorData, setCursor } from '../cursor/helper';
|
|
2
3
|
import SocketManager from './socket-manager';
|
|
3
4
|
var withSocketIO = function withSocketIO(editor, options) {
|
|
4
5
|
var onChange = editor.onChange;
|
|
5
6
|
var newEditor = editor;
|
|
7
|
+
var cursorData = generateCursorData(options.config);
|
|
6
8
|
var socketManager = null;
|
|
7
9
|
newEditor.openConnection = function () {
|
|
8
10
|
var document = options.document,
|
|
9
11
|
config = options.config;
|
|
12
|
+
config['cursorData'] = cursorData;
|
|
10
13
|
socketManager = SocketManager.getInstance(newEditor, document, config);
|
|
11
14
|
};
|
|
12
15
|
newEditor.closeConnection = function () {
|
|
@@ -14,12 +17,13 @@ var withSocketIO = function withSocketIO(editor, options) {
|
|
|
14
17
|
};
|
|
15
18
|
newEditor.onChange = function () {
|
|
16
19
|
var operations = newEditor.operations;
|
|
17
|
-
operations = operations.filter(
|
|
18
|
-
return item.type !== 'set_selection';
|
|
19
|
-
});
|
|
20
|
+
// operations = operations.filter(item => item.type !== 'set_selection');
|
|
20
21
|
if (!newEditor.isRemote && operations.length > 0) {
|
|
21
22
|
var _socketManager = SocketManager.getInstance();
|
|
22
23
|
_socketManager.addOperations && _socketManager.addOperations(operations);
|
|
24
|
+
var config = options.config;
|
|
25
|
+
var clientId = config.user.username;
|
|
26
|
+
setCursor(clientId, editor, operations, cursorData);
|
|
23
27
|
}
|
|
24
28
|
onChange();
|
|
25
29
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@seafile/sdoc-editor",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.45",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "This is a sdoc editor",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
"deep-copy": "1.4.2",
|
|
15
15
|
"is-hotkey": "0.2.0",
|
|
16
16
|
"is-url": "^1.2.4",
|
|
17
|
+
"randomcolor": "0.6.2",
|
|
17
18
|
"react-cookies": "0.1.1",
|
|
18
19
|
"reactstrap": "8.9.0",
|
|
19
20
|
"slugid": "3.2.0",
|