@seafile/sdoc-editor 0.1.28-beta3 → 0.1.28-beta5
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.
|
@@ -1,17 +1,16 @@
|
|
|
1
|
+
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
|
|
1
2
|
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
2
|
-
// import shallowEqual from 'shallowequal';
|
|
3
3
|
import { DIFF_TYPE, ELEMENT_TYPE } from '../../basic-sdk/extension/constants';
|
|
4
4
|
import ObjectUtils from './object-utils';
|
|
5
|
-
var getElementIndexInDiffDocument = function getElementIndexInDiffDocument(
|
|
6
|
-
return
|
|
5
|
+
var getElementIndexInDiffDocument = function getElementIndexInDiffDocument(diffValue, element) {
|
|
6
|
+
return diffValue.findIndex(function (item) {
|
|
7
7
|
return item.id === element.id;
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
var getNearestElementIndex = function getNearestElementIndex(
|
|
11
|
-
var
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
10
|
+
var getNearestElementIndex = function getNearestElementIndex(diff, oldDocument, elementId) {
|
|
11
|
+
var diffValue = diff.value;
|
|
12
|
+
var elementInDiffDocumentIdx = getElementIndexInDiffDocument(diffValue, elementId);
|
|
13
|
+
if (elementInDiffDocumentIdx > -1) return elementInDiffDocumentIdx + 1;
|
|
15
14
|
var oldElementCount = oldDocument.length;
|
|
16
15
|
var elementInOldDocumentIndex = oldDocument.findIndex(function (item) {
|
|
17
16
|
return item.id === elementId;
|
|
@@ -20,27 +19,36 @@ var getNearestElementIndex = function getNearestElementIndex(diffDocument, oldDo
|
|
|
20
19
|
leftIndex = leftIndex === -1 ? 0 : leftIndex;
|
|
21
20
|
var rightIndex = elementInOldDocumentIndex + 1;
|
|
22
21
|
rightIndex = rightIndex === oldElementCount - 1 ? oldElementCount - 1 : rightIndex;
|
|
23
|
-
while (
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
22
|
+
while (elementInDiffDocumentIdx === -1 && leftIndex > -2 && rightIndex < oldElementCount + 1) {
|
|
23
|
+
if (leftIndex > -1) {
|
|
24
|
+
var oldLeftElement = oldDocument[leftIndex];
|
|
25
|
+
elementInDiffDocumentIdx = getElementIndexInDiffDocument(diffValue, oldLeftElement);
|
|
26
|
+
if (elementInDiffDocumentIdx > -1) {
|
|
27
|
+
return elementInDiffDocumentIdx + 1;
|
|
28
|
+
}
|
|
28
29
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
30
|
+
if (rightIndex < oldElementCount) {
|
|
31
|
+
var oldLastElement = oldDocument[rightIndex];
|
|
32
|
+
elementInDiffDocumentIdx = getElementIndexInDiffDocument(diffValue, oldLastElement);
|
|
33
|
+
if (elementInDiffDocumentIdx > -1) {
|
|
34
|
+
return elementInDiffDocumentIdx;
|
|
35
|
+
}
|
|
33
36
|
}
|
|
34
37
|
leftIndex--;
|
|
35
38
|
rightIndex++;
|
|
39
|
+
if (leftIndex === -2 && rightIndex < oldElementCount) {
|
|
40
|
+
leftIndex = -1;
|
|
41
|
+
}
|
|
42
|
+
if (rightIndex === oldElementCount && leftIndex > -1) {
|
|
43
|
+
rightIndex = oldElementCount;
|
|
44
|
+
}
|
|
36
45
|
}
|
|
37
|
-
|
|
38
|
-
return elementInDiffDocument;
|
|
46
|
+
return diffValue.length - 1;
|
|
39
47
|
};
|
|
40
|
-
var
|
|
41
|
-
if (!
|
|
48
|
+
var updateDiffValue = function updateDiffValue(diff, element, oldElement) {
|
|
49
|
+
if (!diff || !element || !oldElement) return;
|
|
42
50
|
if (ObjectUtils.isSameObject(element, oldElement)) {
|
|
43
|
-
|
|
51
|
+
diff.value.push(_objectSpread(_objectSpread({}, element), {}, {
|
|
44
52
|
diff_type: DIFF_TYPE.COMMON
|
|
45
53
|
}));
|
|
46
54
|
return;
|
|
@@ -48,10 +56,11 @@ var updateDiffDocument = function updateDiffDocument(diffDocument, element, oldE
|
|
|
48
56
|
var currentElementType = element.type;
|
|
49
57
|
var oldElementType = oldElement.type;
|
|
50
58
|
if (currentElementType !== oldElementType) {
|
|
51
|
-
|
|
59
|
+
diff.changes.push(oldElement.id);
|
|
60
|
+
diff.value.push(_objectSpread(_objectSpread({}, oldElement), {}, {
|
|
52
61
|
diff_type: DIFF_TYPE.DELETE
|
|
53
62
|
}));
|
|
54
|
-
|
|
63
|
+
diff.value.push(_objectSpread(_objectSpread({}, element), {}, {
|
|
55
64
|
diff_type: DIFF_TYPE.ADD
|
|
56
65
|
}));
|
|
57
66
|
return;
|
|
@@ -70,10 +79,11 @@ var updateDiffDocument = function updateDiffDocument(diffDocument, element, oldE
|
|
|
70
79
|
case ELEMENT_TYPE.CODE_BLOCK:
|
|
71
80
|
case ELEMENT_TYPE.IMAGE:
|
|
72
81
|
{
|
|
73
|
-
|
|
82
|
+
diff.changes.push(oldElement.id);
|
|
83
|
+
diff.value.push(_objectSpread(_objectSpread({}, oldElement), {}, {
|
|
74
84
|
diff_type: DIFF_TYPE.DELETE
|
|
75
85
|
}));
|
|
76
|
-
|
|
86
|
+
diff.value.push(_objectSpread(_objectSpread({}, element), {}, {
|
|
77
87
|
diff_type: DIFF_TYPE.ADD
|
|
78
88
|
}));
|
|
79
89
|
break;
|
|
@@ -83,65 +93,93 @@ var updateDiffDocument = function updateDiffDocument(diffDocument, element, oldE
|
|
|
83
93
|
case ELEMENT_TYPE.LIST_ITEM:
|
|
84
94
|
case ELEMENT_TYPE.LIST_LIC:
|
|
85
95
|
{
|
|
86
|
-
var
|
|
87
|
-
|
|
88
|
-
|
|
96
|
+
var _diff$changes;
|
|
97
|
+
var _getElementDiffValue = getElementDiffValue(element.children, oldElement.children),
|
|
98
|
+
diffValue = _getElementDiffValue.value,
|
|
99
|
+
diffChanges = _getElementDiffValue.changes;
|
|
100
|
+
(_diff$changes = diff.changes).push.apply(_diff$changes, _toConsumableArray(diffChanges));
|
|
101
|
+
diff.value.push(_objectSpread(_objectSpread({}, element), {}, {
|
|
102
|
+
children: diffValue,
|
|
89
103
|
diff_type: DIFF_TYPE.MODIFY
|
|
90
104
|
}));
|
|
91
105
|
break;
|
|
92
106
|
}
|
|
93
107
|
default:
|
|
94
108
|
{
|
|
95
|
-
|
|
109
|
+
diff.changes.push(oldElement.id);
|
|
110
|
+
diff.value.push(_objectSpread(_objectSpread({}, oldElement), {}, {
|
|
96
111
|
diff_type: DIFF_TYPE.DELETE
|
|
97
112
|
}));
|
|
98
|
-
|
|
113
|
+
diff.value.push(_objectSpread(_objectSpread({}, element), {}, {
|
|
99
114
|
diff_type: DIFF_TYPE.ADD
|
|
100
115
|
}));
|
|
101
116
|
break;
|
|
102
117
|
}
|
|
103
118
|
}
|
|
104
119
|
};
|
|
105
|
-
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* params:
|
|
123
|
+
* currentValue: current version document content
|
|
124
|
+
* oldValue: last version document content
|
|
125
|
+
* return { value: [], change: [] }
|
|
126
|
+
*/
|
|
127
|
+
var getElementDiffValue = function getElementDiffValue(currentContent, oldContent) {
|
|
106
128
|
// init
|
|
107
|
-
var
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
129
|
+
var diff = {
|
|
130
|
+
value: [],
|
|
131
|
+
changes: []
|
|
132
|
+
};
|
|
133
|
+
var currentContentMap = {};
|
|
134
|
+
var oldContentMap = {};
|
|
135
|
+
currentContent.forEach(function (element) {
|
|
136
|
+
currentContentMap[element.id] = element;
|
|
112
137
|
});
|
|
113
|
-
|
|
114
|
-
|
|
138
|
+
oldContent.forEach(function (element) {
|
|
139
|
+
oldContentMap[element.id] = element;
|
|
115
140
|
});
|
|
116
141
|
|
|
117
142
|
// generator diff elements
|
|
118
|
-
for (var i = 0; i <
|
|
119
|
-
var element =
|
|
120
|
-
var isAdded = !
|
|
143
|
+
for (var i = 0; i < currentContent.length; i++) {
|
|
144
|
+
var element = currentContent[i];
|
|
145
|
+
var isAdded = !oldContentMap[element.id]; // added
|
|
121
146
|
if (isAdded) {
|
|
122
|
-
|
|
147
|
+
diff.changes.push(element.id);
|
|
148
|
+
diff.value.push(_objectSpread(_objectSpread({}, element), {}, {
|
|
123
149
|
diff_type: DIFF_TYPE.ADD
|
|
124
150
|
}));
|
|
125
151
|
continue;
|
|
126
152
|
}
|
|
127
|
-
|
|
153
|
+
updateDiffValue(diff, element, oldContentMap[element.id]);
|
|
128
154
|
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
155
|
+
oldContent.forEach(function (oldElement) {
|
|
156
|
+
var oldElementId = oldElement.id;
|
|
157
|
+
if (!currentContentMap[oldElementId]) {
|
|
158
|
+
// deleted
|
|
159
|
+
diff.changes.push(oldElementId);
|
|
160
|
+
var elementIndex = getNearestElementIndex(diff, oldContent, oldElementId);
|
|
161
|
+
diff.value.splice(elementIndex, 0, _objectSpread(_objectSpread({}, oldElement), {}, {
|
|
134
162
|
diff_type: DIFF_TYPE.DELETE
|
|
135
163
|
}));
|
|
136
164
|
}
|
|
137
165
|
});
|
|
138
|
-
return
|
|
166
|
+
return diff;
|
|
139
167
|
};
|
|
140
|
-
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* params:
|
|
171
|
+
* currentValue: current version document
|
|
172
|
+
* oldValue: last version document
|
|
173
|
+
* return { value: [], change: [] }
|
|
174
|
+
*/
|
|
175
|
+
export var getDiff = function getDiff(currentValue, oldValue) {
|
|
141
176
|
var currentVersion = currentValue.version,
|
|
142
|
-
|
|
177
|
+
currentContent = currentValue.children;
|
|
143
178
|
var oldVersion = oldValue.version,
|
|
144
|
-
|
|
145
|
-
if (currentVersion === oldVersion) return
|
|
146
|
-
|
|
179
|
+
oldContent = oldValue.children;
|
|
180
|
+
if (currentVersion === oldVersion) return {
|
|
181
|
+
value: currentContent,
|
|
182
|
+
changes: []
|
|
183
|
+
};
|
|
184
|
+
return getElementDiffValue(currentContent, oldContent);
|
|
147
185
|
};
|
|
@@ -21,7 +21,7 @@ var ObjectUtils = /*#__PURE__*/function () {
|
|
|
21
21
|
}
|
|
22
22
|
}, {
|
|
23
23
|
key: "isObjectChanged",
|
|
24
|
-
value: function isObjectChanged(source, comparison) {
|
|
24
|
+
value: function isObjectChanged(source, comparison, notIncludeKeys) {
|
|
25
25
|
var _this = this;
|
|
26
26
|
if (!this.iterable(source)) {
|
|
27
27
|
throw new Error("source should be a Object or Array , but got ".concat(this.getDataType(source)));
|
|
@@ -30,7 +30,9 @@ var ObjectUtils = /*#__PURE__*/function () {
|
|
|
30
30
|
return true;
|
|
31
31
|
}
|
|
32
32
|
var sourceKeys = Object.keys(source);
|
|
33
|
-
var comparisonKeys = Object.keys(_objectSpread(_objectSpread({}, source), comparison))
|
|
33
|
+
var comparisonKeys = Object.keys(_objectSpread(_objectSpread({}, source), comparison)).filter(function (key) {
|
|
34
|
+
return !notIncludeKeys.includes(key);
|
|
35
|
+
});
|
|
34
36
|
if (sourceKeys.length !== comparisonKeys.length) {
|
|
35
37
|
return true;
|
|
36
38
|
}
|
|
@@ -45,8 +47,9 @@ var ObjectUtils = /*#__PURE__*/function () {
|
|
|
45
47
|
}, {
|
|
46
48
|
key: "isSameObject",
|
|
47
49
|
value: function isSameObject(source, comparison) {
|
|
50
|
+
var notIncludeKeys = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
|
|
48
51
|
if (!source || !comparison) return false;
|
|
49
|
-
return !this.isObjectChanged(source, comparison);
|
|
52
|
+
return !this.isObjectChanged(source, comparison, notIncludeKeys);
|
|
50
53
|
}
|
|
51
54
|
}]);
|
|
52
55
|
return ObjectUtils;
|
|
@@ -7,7 +7,7 @@ import React, { Component } from 'react';
|
|
|
7
7
|
import { Editable, Slate } from '@seafile/slate-react';
|
|
8
8
|
import editor, { renderLeaf, renderElement } from '../../basic-sdk/extension';
|
|
9
9
|
import withNodeId from '../../basic-sdk/node-id';
|
|
10
|
-
import {
|
|
10
|
+
import { getDiff } from '../../basic-sdk/utils/diff';
|
|
11
11
|
import { DIFF_TYPE, ELEMENT_TYPE } from '../../basic-sdk/extension/constants';
|
|
12
12
|
import '../../assets/css/diff-viewer.css';
|
|
13
13
|
var DiffViewer = /*#__PURE__*/function (_Component) {
|
|
@@ -65,7 +65,7 @@ var DiffViewer = /*#__PURE__*/function (_Component) {
|
|
|
65
65
|
}), _this.editor);
|
|
66
66
|
}
|
|
67
67
|
return /*#__PURE__*/React.createElement("div", {
|
|
68
|
-
className: "sdoc-diff
|
|
68
|
+
className: "sdoc-diff-modify"
|
|
69
69
|
}, renderElement(_objectSpread(_objectSpread({}, props), {}, {
|
|
70
70
|
attributes: _objectSpread(_objectSpread({}, props.attributes), {}, {
|
|
71
71
|
className: 'sdoc-diff-modify'
|
|
@@ -74,16 +74,20 @@ var DiffViewer = /*#__PURE__*/function (_Component) {
|
|
|
74
74
|
}
|
|
75
75
|
return renderElement(props, _this.editor);
|
|
76
76
|
};
|
|
77
|
+
var currentContent = _props.currentContent,
|
|
78
|
+
lastContent = _props.lastContent;
|
|
79
|
+
_this.diff = getDiff(currentContent, lastContent);
|
|
77
80
|
_this.editor = withNodeId(editor);
|
|
78
81
|
return _this;
|
|
79
82
|
}
|
|
80
83
|
_createClass(DiffViewer, [{
|
|
84
|
+
key: "componentDidMount",
|
|
85
|
+
value: function componentDidMount() {
|
|
86
|
+
this.props.didMountCallback && this.props.didMountCallback(this.diff);
|
|
87
|
+
}
|
|
88
|
+
}, {
|
|
81
89
|
key: "render",
|
|
82
90
|
value: function render() {
|
|
83
|
-
var _this$props = this.props,
|
|
84
|
-
currentContent = _this$props.currentContent,
|
|
85
|
-
lastContent = _this$props.lastContent;
|
|
86
|
-
var value = getDiffValue(currentContent, lastContent);
|
|
87
91
|
return /*#__PURE__*/React.createElement("div", {
|
|
88
92
|
className: "sdoc-editor-container"
|
|
89
93
|
}, /*#__PURE__*/React.createElement("div", {
|
|
@@ -92,7 +96,7 @@ var DiffViewer = /*#__PURE__*/function (_Component) {
|
|
|
92
96
|
className: "flex-fill o-auto"
|
|
93
97
|
}, /*#__PURE__*/React.createElement(Slate, {
|
|
94
98
|
editor: this.editor,
|
|
95
|
-
value: value,
|
|
99
|
+
value: this.diff.value,
|
|
96
100
|
onChange: function onChange() {}
|
|
97
101
|
}, /*#__PURE__*/React.createElement("div", {
|
|
98
102
|
className: "article mx-auto"
|