@seafile/sdoc-editor 0.1.2 → 0.1.3
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/assets/css/textlink-hovermenu.css +51 -0
- package/dist/editor.js +13 -20
- package/dist/{slate-extension → extension}/constants/index.js +4 -0
- package/dist/{slate-extension → extension}/core/queries/index.js +44 -15
- package/dist/{slate-extension → extension}/core/transforms/focus-editor.js +2 -2
- package/dist/extension/core/transforms/index.js +3 -0
- package/dist/{slate-extension → extension}/core/transforms/move-children.js +1 -1
- package/dist/extension/core/transforms/remove-node-children.js +24 -0
- package/dist/{slate-extension → extension}/core/utils/index.js +10 -0
- package/dist/{slate-extension → extension}/index.js +2 -2
- package/dist/{slate-extension → extension}/plugins/blockquote/helpers.js +1 -1
- package/dist/{slate-extension → extension}/plugins/blockquote/menu/index.js +1 -2
- package/dist/{slate-extension → extension}/plugins/blockquote/plugin.js +5 -9
- package/dist/extension/plugins/blockquote/render-elem.js +10 -0
- package/dist/{slate-extension → extension}/plugins/check-list/helpers.js +1 -1
- package/dist/{slate-extension → extension}/plugins/check-list/plugin.js +1 -1
- package/dist/{slate-extension → extension}/plugins/check-list/render-elem.js +5 -3
- package/dist/{slate-extension → extension}/plugins/header/helpers.js +1 -1
- package/dist/{slate-extension → extension}/plugins/header/plugin.js +4 -7
- package/dist/{slate-extension → extension}/plugins/header/render-elem.js +3 -1
- package/dist/extension/plugins/index.js +9 -0
- package/dist/extension/plugins/link/helpers.js +152 -0
- package/dist/extension/plugins/link/index.js +14 -0
- package/dist/extension/plugins/link/menu/add-link-dialog.js +146 -0
- package/dist/extension/plugins/link/menu/hover-link-dialog.js +49 -0
- package/dist/extension/plugins/link/menu/index.js +67 -0
- package/dist/extension/plugins/link/model.js +13 -0
- package/dist/extension/plugins/link/plugin.js +60 -0
- package/dist/extension/plugins/link/render-elem.js +114 -0
- package/dist/{slate-extension → extension}/plugins/list/helpers.js +1 -1
- package/dist/{slate-extension → extension}/plugins/list/menu/index.js +1 -1
- package/dist/{slate-extension → extension}/plugins/list/plugin/normalize-list.js +1 -1
- package/dist/{slate-extension → extension}/plugins/list/plugin/on-tab-handle.js +1 -1
- package/dist/{slate-extension → extension}/plugins/list/queries/index.js +1 -1
- package/dist/{slate-extension → extension}/plugins/list/render-elem.js +11 -4
- package/dist/{slate-extension → extension}/plugins/list/transforms/insert-list-item.js +17 -22
- package/dist/{slate-extension → extension}/plugins/list/transforms/move-list-item-down.js +1 -1
- package/dist/{slate-extension → extension}/plugins/list/transforms/move-list-item-up.js +1 -1
- package/dist/{slate-extension → extension}/plugins/list/transforms/move-list-items-to-list.js +1 -1
- package/dist/{slate-extension → extension}/plugins/list/transforms/move-list-items.js +1 -1
- package/dist/{slate-extension → extension}/plugins/list/transforms/normalize-list-item.js +1 -1
- package/dist/{slate-extension → extension}/plugins/list/transforms/normalize-nested-list.js +1 -1
- package/dist/{slate-extension → extension}/plugins/list/transforms/toggle-list.js +1 -1
- package/dist/{slate-extension → extension}/plugins/list/transforms/unwrap-list.js +1 -1
- package/dist/{slate-extension → extension}/plugins/text-style/render-elem.js +3 -1
- package/dist/{slate-extension → extension}/render/render-element.js +14 -4
- package/dist/{slate-extension → extension}/toolbar/index.js +4 -1
- package/dist/node-id/constants.js +18 -0
- package/dist/node-id/helpers.js +80 -0
- package/dist/node-id/index.js +11 -0
- package/dist/socket/helpers.js +258 -0
- package/dist/socket/index.js +3 -0
- package/dist/socket/socket-client.js +138 -0
- package/dist/socket/socket-manager.js +180 -0
- package/dist/socket/with-socket-io.js +18 -0
- package/package.json +9 -6
- package/public/locales/en/seafile-editor.json +4 -1
- package/public/locales/zh-CN/seafile-editor.json +4 -1
- package/dist/slate-extension/core/transforms/index.js +0 -2
- package/dist/slate-extension/plugins/blockquote/render-elem.js +0 -7
- package/dist/slate-extension/plugins/index.js +0 -9
- package/dist/slate-extension/plugins/socket/index.js +0 -6
- package/dist/slate-extension/plugins/socket/plugin.js +0 -23
- package/dist/slate-extension/socket/socket-client.js +0 -94
- package/dist/slate-extension/socket/socket-manager.js +0 -63
- /package/dist/{slate-extension → extension}/core/index.js +0 -0
- /package/dist/{slate-extension → extension}/menu/index.js +0 -0
- /package/dist/{slate-extension → extension}/menu/menu-group.js +0 -0
- /package/dist/{slate-extension → extension}/menu/menu-item.js +0 -0
- /package/dist/{slate-extension → extension}/menu/menu.css +0 -0
- /package/dist/{slate-extension → extension}/plugins/blockquote/index.js +0 -0
- /package/dist/{slate-extension → extension}/plugins/blockquote/model.js +0 -0
- /package/dist/{slate-extension → extension}/plugins/check-list/index.js +0 -0
- /package/dist/{slate-extension → extension}/plugins/check-list/menu/index.js +0 -0
- /package/dist/{slate-extension → extension}/plugins/check-list/model.js +0 -0
- /package/dist/{slate-extension → extension}/plugins/header/index.js +0 -0
- /package/dist/{slate-extension → extension}/plugins/header/menu/index.js +0 -0
- /package/dist/{slate-extension → extension}/plugins/list/index.js +0 -0
- /package/dist/{slate-extension → extension}/plugins/list/model.js +0 -0
- /package/dist/{slate-extension → extension}/plugins/list/plugin/index.js +0 -0
- /package/dist/{slate-extension → extension}/plugins/list/plugin/insert-break-list.js +0 -0
- /package/dist/{slate-extension → extension}/plugins/list/transforms/index.js +0 -0
- /package/dist/{slate-extension → extension}/plugins/list/transforms/remove-first-list-item.js +0 -0
- /package/dist/{slate-extension → extension}/plugins/text-style/index.js +0 -0
- /package/dist/{slate-extension → extension}/plugins/text-style/model.js +0 -0
- /package/dist/{slate-extension → extension}/render/render-leaf.js +0 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Editor, Transforms, Element, Node } from 'slate';
|
|
1
|
+
import { Editor, Transforms, Element, Node } from '@seafile/slate';
|
|
2
2
|
import { LIST_ITEM, LIST_LIC, PARAGRAPH } from '../../../constants';
|
|
3
3
|
import { getAboveBlockNode, getAboveNode, getNodeType } from '../../../core';
|
|
4
4
|
import { getListTypes } from '../queries';
|
|
@@ -24,6 +24,8 @@ var renderText = function renderText(props, editor) {
|
|
|
24
24
|
className: "token ".concat(leaf.type)
|
|
25
25
|
}, markedChildren);
|
|
26
26
|
}
|
|
27
|
-
return /*#__PURE__*/React.createElement("span",
|
|
27
|
+
return /*#__PURE__*/React.createElement("span", Object.assign({
|
|
28
|
+
"data-id": leaf.id
|
|
29
|
+
}, attributes), markedChildren);
|
|
28
30
|
};
|
|
29
31
|
export default renderText;
|
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
|
2
2
|
import React from 'react';
|
|
3
|
-
import { BLOCKQUOTE, CHECK_LIST_ITEM, HEADER1, HEADER2, HEADER3, HEADER4, HEADER5, HEADER6, LIST_ITEM, LIST_LIC, ORDERED_LIST, PARAGRAPH, UNORDERED_LIST } from '../constants';
|
|
4
|
-
import { BlockquotePlugin, CheckListPlugin, HeaderPlugin, ListPlugin } from '../plugins';
|
|
3
|
+
import { BLOCKQUOTE, LINK, CHECK_LIST_ITEM, HEADER1, HEADER2, HEADER3, HEADER4, HEADER5, HEADER6, LIST_ITEM, LIST_LIC, ORDERED_LIST, PARAGRAPH, UNORDERED_LIST } from '../constants';
|
|
4
|
+
import { BlockquotePlugin, LinkPlugin, CheckListPlugin, HeaderPlugin, ListPlugin } from '../plugins';
|
|
5
5
|
var renderElement = function renderElement(props, editor) {
|
|
6
6
|
var attributes = props.attributes,
|
|
7
7
|
children = props.children,
|
|
8
8
|
element = props.element;
|
|
9
9
|
switch (element.type) {
|
|
10
10
|
case PARAGRAPH:
|
|
11
|
-
return /*#__PURE__*/React.createElement("p",
|
|
11
|
+
return /*#__PURE__*/React.createElement("p", Object.assign({
|
|
12
|
+
"data-id": element.id
|
|
13
|
+
}, attributes), children);
|
|
12
14
|
case HEADER1:
|
|
13
15
|
case HEADER2:
|
|
14
16
|
case HEADER3:
|
|
@@ -20,6 +22,12 @@ var renderElement = function renderElement(props, editor) {
|
|
|
20
22
|
renderHeader = _HeaderPlugin$renderE[0];
|
|
21
23
|
return renderHeader(props, editor);
|
|
22
24
|
}
|
|
25
|
+
case LINK:
|
|
26
|
+
{
|
|
27
|
+
var _LinkPlugin$renderEle = _slicedToArray(LinkPlugin.renderElements, 1),
|
|
28
|
+
renderLink = _LinkPlugin$renderEle[0];
|
|
29
|
+
return renderLink(props, editor);
|
|
30
|
+
}
|
|
23
31
|
case BLOCKQUOTE:
|
|
24
32
|
{
|
|
25
33
|
var _BlockquotePlugin$ren = _slicedToArray(BlockquotePlugin.renderElements, 1),
|
|
@@ -55,7 +63,9 @@ var renderElement = function renderElement(props, editor) {
|
|
|
55
63
|
}
|
|
56
64
|
default:
|
|
57
65
|
{
|
|
58
|
-
return /*#__PURE__*/React.createElement("p",
|
|
66
|
+
return /*#__PURE__*/React.createElement("p", Object.assign({
|
|
67
|
+
"data-id": element.id
|
|
68
|
+
}, attributes), children);
|
|
59
69
|
}
|
|
60
70
|
}
|
|
61
71
|
};
|
|
@@ -4,6 +4,7 @@ import _inherits from "@babel/runtime/helpers/esm/inherits";
|
|
|
4
4
|
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
|
|
5
5
|
import React from 'react';
|
|
6
6
|
import QuoteMenu from '../plugins/blockquote/menu';
|
|
7
|
+
import LinkMenu from '../plugins/link/menu';
|
|
7
8
|
import ListMenu from '../plugins/list/menu';
|
|
8
9
|
import { ORDERED_LIST, UNORDERED_LIST } from '../constants';
|
|
9
10
|
import HeaderMenu from '../plugins/header/menu';
|
|
@@ -24,7 +25,9 @@ var Toolbar = /*#__PURE__*/function (_React$Component) {
|
|
|
24
25
|
className: "sf-editor-toolbar"
|
|
25
26
|
}, /*#__PURE__*/React.createElement(HeaderMenu, {
|
|
26
27
|
editor: this.props.editor
|
|
27
|
-
}), /*#__PURE__*/React.createElement(MenuGroup, null, /*#__PURE__*/React.createElement(
|
|
28
|
+
}), /*#__PURE__*/React.createElement(MenuGroup, null, /*#__PURE__*/React.createElement(LinkMenu, {
|
|
29
|
+
editor: this.props.editor
|
|
30
|
+
}), /*#__PURE__*/React.createElement(QuoteMenu, {
|
|
28
31
|
editor: this.props.editor
|
|
29
32
|
}), /*#__PURE__*/React.createElement(ListMenu, {
|
|
30
33
|
editor: this.props.editor,
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// insert_node & remove_node
|
|
2
|
+
export var INSERT_TEXT = 'insert_text';
|
|
3
|
+
export var REMOVE_TEXT = 'remove_text';
|
|
4
|
+
|
|
5
|
+
// insert_node & remove_node
|
|
6
|
+
export var INSERT_NODE = 'insert_node';
|
|
7
|
+
export var REMOVE_NODE = 'remove_node';
|
|
8
|
+
|
|
9
|
+
// merge_node & split_node
|
|
10
|
+
export var MERGE_NODE = 'merge_node';
|
|
11
|
+
export var SPLIT_NODE = 'split_node';
|
|
12
|
+
|
|
13
|
+
// set_node & move_node
|
|
14
|
+
export var SET_NODE = 'set_node';
|
|
15
|
+
export var MOVE_NODE = 'move_node';
|
|
16
|
+
|
|
17
|
+
// set_selection
|
|
18
|
+
export var SET_SELECTION = 'set_selection';
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { Node } from '@seafile/slate';
|
|
2
|
+
import deepCopy from 'deep-copy';
|
|
3
|
+
import slugid from 'slugid';
|
|
4
|
+
import * as OPERATION from './constants';
|
|
5
|
+
export var decorateOperation = function decorateOperation(editor, operation) {
|
|
6
|
+
var newOperation = deepCopy(operation);
|
|
7
|
+
var type = newOperation.type;
|
|
8
|
+
switch (type) {
|
|
9
|
+
case OPERATION.INSERT_TEXT:
|
|
10
|
+
case OPERATION.REMOVE_TEXT:
|
|
11
|
+
{
|
|
12
|
+
var path = newOperation.path;
|
|
13
|
+
var node = Node.get(editor, path);
|
|
14
|
+
newOperation['node_id'] = node.id;
|
|
15
|
+
break;
|
|
16
|
+
}
|
|
17
|
+
case OPERATION.INSERT_NODE:
|
|
18
|
+
{
|
|
19
|
+
var _path = newOperation.path,
|
|
20
|
+
_node = newOperation.node;
|
|
21
|
+
if (!_node.id) _node.id = slugid.nice(); // generate an id for insert node
|
|
22
|
+
|
|
23
|
+
var parent = Node.parent(editor, _path);
|
|
24
|
+
newOperation['parent_node_id'] = parent.id;
|
|
25
|
+
break;
|
|
26
|
+
}
|
|
27
|
+
case OPERATION.REMOVE_NODE:
|
|
28
|
+
{
|
|
29
|
+
var _path2 = newOperation.path;
|
|
30
|
+
var _node2 = Node.get(editor, _path2);
|
|
31
|
+
newOperation['node_id'] = _node2.id; // remove node's id
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
case OPERATION.MERGE_NODE:
|
|
35
|
+
{
|
|
36
|
+
// merge next node into prev node
|
|
37
|
+
var _path3 = newOperation.path;
|
|
38
|
+
var _node3 = Node.get(editor, _path3);
|
|
39
|
+
newOperation['node_id'] = _node3.id;
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
case OPERATION.SPLIT_NODE:
|
|
43
|
+
{
|
|
44
|
+
// child: split [7, 0] -> [[7, 0],[7, 1]]
|
|
45
|
+
// parent: split [[7, 0], [7, 1]] -> [[7], [8]]
|
|
46
|
+
var _path4 = newOperation.path,
|
|
47
|
+
_newOperation$propert = newOperation.properties,
|
|
48
|
+
properties = _newOperation$propert === void 0 ? {} : _newOperation$propert;
|
|
49
|
+
// need generate a new id for new node
|
|
50
|
+
properties.id = slugid.nice();
|
|
51
|
+
var _node4 = Node.get(editor, _path4);
|
|
52
|
+
newOperation['node_id'] = _node4.id; // split node's id
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
case OPERATION.SET_NODE:
|
|
56
|
+
{
|
|
57
|
+
var _properties = newOperation.properties,
|
|
58
|
+
newProperties = newOperation.newProperties;
|
|
59
|
+
if (!_properties.id) _properties.id = slugid.nice();
|
|
60
|
+
// newProperties none has id attribute, keep the original id
|
|
61
|
+
newProperties.id = _properties.id;
|
|
62
|
+
newOperation['node_id'] = _properties.id; // the node which will be set to another type
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
65
|
+
case OPERATION.MOVE_NODE:
|
|
66
|
+
{
|
|
67
|
+
var _path5 = newOperation.path;
|
|
68
|
+
var _node5 = Node.get(editor, _path5);
|
|
69
|
+
// move operation not change the node's id
|
|
70
|
+
newOperation['node_id'] = _node5.id; // moved node's id
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
default:
|
|
74
|
+
{
|
|
75
|
+
// set_selection
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return newOperation;
|
|
80
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { decorateOperation } from './helpers';
|
|
2
|
+
var withNodeId = function withNodeId(editor) {
|
|
3
|
+
var apply = editor.apply;
|
|
4
|
+
var newEditor = editor;
|
|
5
|
+
newEditor.apply = function (op) {
|
|
6
|
+
var newOp = decorateOperation(newEditor, op);
|
|
7
|
+
apply(newOp);
|
|
8
|
+
};
|
|
9
|
+
return newEditor;
|
|
10
|
+
};
|
|
11
|
+
export default withNodeId;
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
2
|
+
import deepCopy from 'deep-copy';
|
|
3
|
+
import { Editor, Operation } from '@seafile/slate';
|
|
4
|
+
import { getNode } from '../extension/core';
|
|
5
|
+
import * as OPERATION from '../node-id/constants';
|
|
6
|
+
export var getNodePathById = function getNodePathById(rootNode, nodeId) {
|
|
7
|
+
var path = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
|
|
8
|
+
if (rootNode.id === nodeId) return path;
|
|
9
|
+
var _rootNode$children = rootNode.children,
|
|
10
|
+
children = _rootNode$children === void 0 ? [] : _rootNode$children;
|
|
11
|
+
for (var i = 0; i < children.length; i++) {
|
|
12
|
+
var child = children[i];
|
|
13
|
+
path.push(i);
|
|
14
|
+
var nodePath = getNodePathById(child, nodeId, path);
|
|
15
|
+
if (nodePath) return nodePath;
|
|
16
|
+
path.pop();
|
|
17
|
+
}
|
|
18
|
+
return null;
|
|
19
|
+
};
|
|
20
|
+
export var validateOperation = function validateOperation(editor, operation) {
|
|
21
|
+
var isValid = false;
|
|
22
|
+
var newOperation = deepCopy(operation);
|
|
23
|
+
var type = newOperation.type;
|
|
24
|
+
switch (type) {
|
|
25
|
+
case OPERATION.INSERT_TEXT:
|
|
26
|
+
case OPERATION.REMOVE_TEXT:
|
|
27
|
+
{
|
|
28
|
+
var node_id = newOperation.node_id,
|
|
29
|
+
path = newOperation.path;
|
|
30
|
+
var node = getNode(editor, path);
|
|
31
|
+
// node is exist and node path is not changed
|
|
32
|
+
if (node && node.id === node_id) {
|
|
33
|
+
isValid = true;
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// node is exist but node path is changed
|
|
38
|
+
var nodePath = getNodePathById(editor, node_id);
|
|
39
|
+
if (nodePath) {
|
|
40
|
+
isValid = true;
|
|
41
|
+
newOperation.path = nodePath;
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
// node is not exist
|
|
45
|
+
isValid = false;
|
|
46
|
+
break;
|
|
47
|
+
}
|
|
48
|
+
case OPERATION.INSERT_NODE:
|
|
49
|
+
{
|
|
50
|
+
var parent_node_id = newOperation.parent_node_id,
|
|
51
|
+
_path = newOperation.path;
|
|
52
|
+
var parentNodePath = getNodePathById(editor, parent_node_id);
|
|
53
|
+
if (!parentNodePath) {
|
|
54
|
+
isValid = false;
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
var parentPath = _path.slice(0, _path.length - 1);
|
|
58
|
+
if (parentPath.join() === parentNodePath.join()) {
|
|
59
|
+
isValid = true;
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// reset insert node path
|
|
64
|
+
var parentNode = Node.get(editor, _path);
|
|
65
|
+
var childLength = parentNode.children.length;
|
|
66
|
+
var index = Math.min(_path[_path.length - 1], childLength);
|
|
67
|
+
newOperation.path = parentNodePath.concat([index]);
|
|
68
|
+
isValid = true;
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
case OPERATION.REMOVE_NODE:
|
|
72
|
+
{
|
|
73
|
+
var _node_id = newOperation.node_id,
|
|
74
|
+
_path2 = newOperation.path;
|
|
75
|
+
var _node = getNode(editor, _path2);
|
|
76
|
+
// node is exist and node path is not changed
|
|
77
|
+
if (_node && _node.id === _node_id) {
|
|
78
|
+
isValid = true;
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// node is exist but node path is changed
|
|
83
|
+
var _nodePath = getNodePathById(editor, _node_id);
|
|
84
|
+
if (_nodePath) {
|
|
85
|
+
isValid = true;
|
|
86
|
+
newOperation.path = _nodePath;
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// node is not exist
|
|
91
|
+
isValid = false;
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
case OPERATION.MERGE_NODE:
|
|
95
|
+
{
|
|
96
|
+
// merge next node into prev node
|
|
97
|
+
var _node_id2 = newOperation.node_id,
|
|
98
|
+
_path3 = newOperation.path;
|
|
99
|
+
var _node2 = getNode(editor, _path3);
|
|
100
|
+
// node is exist and node path is not changed
|
|
101
|
+
if (_node2 && _node2.id === _node_id2) {
|
|
102
|
+
isValid = true;
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// node is exist but node path is changed
|
|
107
|
+
var _nodePath2 = getNodePathById(editor, _node_id2);
|
|
108
|
+
if (_nodePath2) {
|
|
109
|
+
isValid = true;
|
|
110
|
+
newOperation.path = _nodePath2;
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// node is not exist
|
|
115
|
+
isValid = false;
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
case OPERATION.SPLIT_NODE:
|
|
119
|
+
{
|
|
120
|
+
var _node_id3 = newOperation.node_id,
|
|
121
|
+
_path4 = newOperation.path;
|
|
122
|
+
var _node3 = getNode(editor, _path4);
|
|
123
|
+
// node is exist and node path is not changed
|
|
124
|
+
if (_node3 && _node3.id === _node_id3) {
|
|
125
|
+
isValid = true;
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// node is exist but node path is changed
|
|
130
|
+
var _nodePath3 = getNodePathById(editor, _node_id3);
|
|
131
|
+
if (_nodePath3) {
|
|
132
|
+
isValid = true;
|
|
133
|
+
newOperation.path = _nodePath3;
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// node is not exist
|
|
138
|
+
isValid = false;
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
case OPERATION.SET_NODE:
|
|
142
|
+
{
|
|
143
|
+
var _node_id4 = newOperation.node_id,
|
|
144
|
+
_path5 = newOperation.path,
|
|
145
|
+
properties = newOperation.properties;
|
|
146
|
+
var _node4 = getNode(editor, _path5);
|
|
147
|
+
// node is exist and node path is not changed
|
|
148
|
+
if (_node4 && _node4.id === _node_id4) {
|
|
149
|
+
isValid = true;
|
|
150
|
+
newOperation.properties = _objectSpread(_objectSpread({}, properties), {}, {
|
|
151
|
+
type: _node4.type
|
|
152
|
+
});
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// node is exist but node path is changed
|
|
157
|
+
var _nodePath4 = getNodePathById(editor, _node_id4);
|
|
158
|
+
if (_nodePath4) {
|
|
159
|
+
isValid = true;
|
|
160
|
+
var _node5 = getNode(editor, _nodePath4);
|
|
161
|
+
newOperation.properties = _objectSpread(_objectSpread({}, properties), {}, {
|
|
162
|
+
type: _node5.type
|
|
163
|
+
});
|
|
164
|
+
newOperation.path = _nodePath4;
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// node is not exist
|
|
169
|
+
isValid = false;
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
case OPERATION.MOVE_NODE:
|
|
173
|
+
{
|
|
174
|
+
var _node_id5 = newOperation.node_id,
|
|
175
|
+
_path6 = newOperation.path;
|
|
176
|
+
var _node6 = getNode(editor, _path6);
|
|
177
|
+
// node is exist and node path is not changed
|
|
178
|
+
if (_node6 && _node6.id === _node_id5) {
|
|
179
|
+
isValid = true;
|
|
180
|
+
break;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// TODO: newPath can not calculate by nodePath
|
|
184
|
+
// node is exist but node path is changed
|
|
185
|
+
// const nodePath = getNodePathById(editor, node_id);
|
|
186
|
+
// if (nodePath) {}
|
|
187
|
+
|
|
188
|
+
// node is not exist
|
|
189
|
+
isValid = false;
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
default:
|
|
193
|
+
{
|
|
194
|
+
// set_selection
|
|
195
|
+
break;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
if (isValid) return newOperation;
|
|
199
|
+
return isValid;
|
|
200
|
+
};
|
|
201
|
+
export var getRevertOperationList = function getRevertOperationList(operationList) {
|
|
202
|
+
if (operationList.length === 0) return [];
|
|
203
|
+
|
|
204
|
+
// Generate a duplicate operationList, The original value cannot be modified here
|
|
205
|
+
var revertOperationList = deepCopy(operationList);
|
|
206
|
+
revertOperationList = revertOperationList.reverse();
|
|
207
|
+
return revertOperationList.map(function (operations) {
|
|
208
|
+
var ops = operations.reverse();
|
|
209
|
+
return ops.map(function (item) {
|
|
210
|
+
return Operation.inverse(item);
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
};
|
|
214
|
+
export var revertOperationList = function revertOperationList(editor, operationList) {
|
|
215
|
+
if (operationList.length === 0) return [];
|
|
216
|
+
var revertOperationList = getRevertOperationList(operationList);
|
|
217
|
+
|
|
218
|
+
// Cancel locale execute operations
|
|
219
|
+
var _loop = function _loop() {
|
|
220
|
+
var operations = revertOperationList[i];
|
|
221
|
+
Editor.withoutNormalizing(editor, function () {
|
|
222
|
+
for (var j = 0; j < operations.length; j++) {
|
|
223
|
+
var op = operations[j];
|
|
224
|
+
editor.apply(op);
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
};
|
|
228
|
+
for (var i = 0; i < revertOperationList.length; i++) {
|
|
229
|
+
_loop();
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
export var reExecRevertOperationList = function reExecRevertOperationList(editor, revertOperationList) {
|
|
233
|
+
if (revertOperationList.length === 0) return;
|
|
234
|
+
|
|
235
|
+
// Re-execute revert operations
|
|
236
|
+
var _loop2 = function _loop2() {
|
|
237
|
+
var operations = revertOperationList[i];
|
|
238
|
+
Editor.withoutNormalizing(editor, function () {
|
|
239
|
+
for (var j = 0; j < operations.length; j++) {
|
|
240
|
+
var op = validateOperation(editor, operations[j]);
|
|
241
|
+
if (op) {
|
|
242
|
+
editor.apply(op);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
};
|
|
247
|
+
for (var i = 0; i < revertOperationList.length; i++) {
|
|
248
|
+
_loop2();
|
|
249
|
+
}
|
|
250
|
+
};
|
|
251
|
+
export var syncRemoteOperations = function syncRemoteOperations(editor, remoteOperations) {
|
|
252
|
+
if (remoteOperations.length === 0) return;
|
|
253
|
+
Editor.withoutNormalizing(editor, function () {
|
|
254
|
+
remoteOperations.forEach(function (item) {
|
|
255
|
+
editor.apply(item);
|
|
256
|
+
});
|
|
257
|
+
});
|
|
258
|
+
};
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
2
|
+
import _createClass from "@babel/runtime/helpers/esm/createClass";
|
|
3
|
+
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
|
|
4
|
+
import io from 'socket.io-client';
|
|
5
|
+
import Debug from 'debug';
|
|
6
|
+
import SocketManager from './socket-manager';
|
|
7
|
+
var debug = Debug('sdoc:socket-client');
|
|
8
|
+
var SocketClient = /*#__PURE__*/_createClass(function SocketClient(config) {
|
|
9
|
+
var _this = this;
|
|
10
|
+
_classCallCheck(this, SocketClient);
|
|
11
|
+
this.getParams = function () {
|
|
12
|
+
var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
13
|
+
var docUuid = _this.config.docUuid;
|
|
14
|
+
return _objectSpread({
|
|
15
|
+
doc_uuid: docUuid
|
|
16
|
+
}, params);
|
|
17
|
+
};
|
|
18
|
+
this.onConnected = function () {
|
|
19
|
+
var _this$config = _this.config,
|
|
20
|
+
doc_uuid = _this$config.docUuid,
|
|
21
|
+
doc_path = _this$config.docPath,
|
|
22
|
+
doc_name = _this$config.docName,
|
|
23
|
+
access_token = _this$config.accessToken;
|
|
24
|
+
var params = {
|
|
25
|
+
doc_uuid: doc_uuid,
|
|
26
|
+
doc_path: doc_path,
|
|
27
|
+
doc_name: doc_name,
|
|
28
|
+
access_token: access_token
|
|
29
|
+
};
|
|
30
|
+
// join room
|
|
31
|
+
_this.socket.emit('join-room', params, function (result) {
|
|
32
|
+
var socketManager = SocketManager.getInstance();
|
|
33
|
+
if (result.success) {
|
|
34
|
+
// sync operations or document
|
|
35
|
+
if (_this.isReconnect) {
|
|
36
|
+
_this.isReconnect = false;
|
|
37
|
+
var serverVersion = result.version;
|
|
38
|
+
var clientVersion = socketManager.getDocumentVersion();
|
|
39
|
+
if (serverVersion !== clientVersion) {
|
|
40
|
+
_this.syncDocumentBySocket();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
socketManager.dispatchConnectState('onConnected', result);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Disconnect the server in the client side. There will be no reconnection.
|
|
48
|
+
_this.socket.disconnect();
|
|
49
|
+
socketManager.dispatchConnectState('connect-error', result);
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
this.onReconnect = function (data) {
|
|
53
|
+
debug('reconnect.');
|
|
54
|
+
_this.isReconnect = true;
|
|
55
|
+
var socketManager = SocketManager.getInstance();
|
|
56
|
+
socketManager.dispatchConnectState('reconnect');
|
|
57
|
+
};
|
|
58
|
+
this.onReconnectAttempt = function (attemptNumber) {
|
|
59
|
+
debug('reconnect_attempt. %s', attemptNumber);
|
|
60
|
+
var socketManager = SocketManager.getInstance();
|
|
61
|
+
socketManager.dispatchConnectState('reconnect_attempt', attemptNumber);
|
|
62
|
+
};
|
|
63
|
+
this.onReconnectError = function () {
|
|
64
|
+
debug('reconnect_error. %s');
|
|
65
|
+
var socketManager = SocketManager.getInstance();
|
|
66
|
+
socketManager.dispatchConnectState('reconnect_error');
|
|
67
|
+
};
|
|
68
|
+
this.onDisconnected = function (data) {
|
|
69
|
+
debug('disconnect message: %s', data);
|
|
70
|
+
var socketManager = SocketManager.getInstance();
|
|
71
|
+
socketManager.dispatchConnectState('disconnect');
|
|
72
|
+
};
|
|
73
|
+
this.onJoinRoom = function (username) {
|
|
74
|
+
debug('%s joined room success.', username);
|
|
75
|
+
var socketManager = SocketManager.getInstance();
|
|
76
|
+
socketManager.dispatchConnectState('join-room', username);
|
|
77
|
+
};
|
|
78
|
+
this.onLeaveRoom = function (username) {
|
|
79
|
+
debug('%s leaved room success.', username);
|
|
80
|
+
var socketManager = SocketManager.getInstance();
|
|
81
|
+
socketManager.dispatchConnectState('leave-room', username);
|
|
82
|
+
};
|
|
83
|
+
this.sendOperations = function (operations, version, callback) {
|
|
84
|
+
debug('=========== send operations ==========');
|
|
85
|
+
debug('%O', operations);
|
|
86
|
+
debug('======================================');
|
|
87
|
+
_this.socket.emit('update-document', _this.getParams({
|
|
88
|
+
operations: operations,
|
|
89
|
+
version: version
|
|
90
|
+
}), function (result) {
|
|
91
|
+
callback && callback(result);
|
|
92
|
+
});
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* receive remote broadcast operations
|
|
96
|
+
* @param {*} params {operations, version}
|
|
97
|
+
*/
|
|
98
|
+
this.receiveOperations = function (params) {
|
|
99
|
+
debug('********* receive operations *********');
|
|
100
|
+
debug('%O', params);
|
|
101
|
+
debug('**************************************');
|
|
102
|
+
var socketManager = SocketManager.getInstance();
|
|
103
|
+
socketManager.receiveOperations(params);
|
|
104
|
+
};
|
|
105
|
+
this.disconnectWithServer = function () {
|
|
106
|
+
_this.socket.disconnect();
|
|
107
|
+
};
|
|
108
|
+
this.syncDocumentBySocket = function () {
|
|
109
|
+
var docUuid = _this.config.docUuid;
|
|
110
|
+
var socketManager = SocketManager.getInstance();
|
|
111
|
+
var clientVersion = socketManager.getDocumentVersion();
|
|
112
|
+
_this.socket.emit('sync-document', {
|
|
113
|
+
doc_uuid: docUuid,
|
|
114
|
+
version: clientVersion
|
|
115
|
+
}, function (result) {
|
|
116
|
+
if (result.success) {
|
|
117
|
+
socketManager.syncDocumentBySocket(result);
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
};
|
|
121
|
+
this.config = config;
|
|
122
|
+
this.isReconnect = false;
|
|
123
|
+
this.socket = io(config.sdocServer, {
|
|
124
|
+
reconnection: true,
|
|
125
|
+
query: {
|
|
126
|
+
doc_uuid: config.docUuid
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
this.socket.on('connect', this.onConnected);
|
|
130
|
+
this.socket.on('disconnect', this.onDisconnected);
|
|
131
|
+
this.socket.on('join-room', this.onJoinRoom);
|
|
132
|
+
this.socket.on('leave-room', this.onLeaveRoom);
|
|
133
|
+
this.socket.on('update-document', this.receiveOperations);
|
|
134
|
+
this.socket.io.on('reconnect', this.onReconnect);
|
|
135
|
+
this.socket.io.on('reconnect_attempt', this.onReconnectAttempt);
|
|
136
|
+
this.socket.io.on('reconnect_error', this.onReconnectError);
|
|
137
|
+
});
|
|
138
|
+
export default SocketClient;
|