@seafile/sdoc-editor 0.1.27-beta2 → 0.1.27-beta3

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.
@@ -0,0 +1,7 @@
1
+ .sdoc-diff-added {
2
+ background-color: #e6ffed;
3
+ }
4
+
5
+ .sdoc-diff-removed {
6
+ background-color: #ffeef0;
7
+ }
@@ -1,5 +1,42 @@
1
1
  import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2
+ import shallowEqual from 'shallowequal';
2
3
  import { DIFF_TYPE } from '../../constants';
4
+ var getElementIndexInDiffDocument = function getElementIndexInDiffDocument(diffDocument, elementId) {
5
+ return diffDocument.findIndex(function (element) {
6
+ return element.id === elementId;
7
+ });
8
+ };
9
+ var getNearestElementIndex = function getNearestElementIndex(diffDocument, oldDocument, elementId) {
10
+ var elementInDiffDocument = getElementIndexInDiffDocument(diffDocument, elementId);
11
+ if (elementInDiffDocument > -1) {
12
+ return elementInDiffDocument;
13
+ }
14
+ var oldDocumentIds = oldDocument.map(function (item) {
15
+ return typeof item === 'string' ? item : item.id;
16
+ });
17
+ var oldElementCount = oldDocumentIds.length;
18
+ var elementInOldDocumentIndex = oldDocumentIds.indexOf(elementId);
19
+ var leftIndex = elementInOldDocumentIndex - 1;
20
+ leftIndex = leftIndex === -1 ? 0 : leftIndex;
21
+ var rightIndex = elementInOldDocumentIndex + 1;
22
+ rightIndex = rightIndex === oldElementCount - 1 ? oldElementCount - 1 : rightIndex;
23
+ while (elementInDiffDocument === -1 && leftIndex > -1 && rightIndex < oldElementCount) {
24
+ var oldLeftElementId = oldDocumentIds[leftIndex];
25
+ elementInDiffDocument = getElementIndexInDiffDocument(diffDocument, oldLeftElementId);
26
+ if (elementInDiffDocument > -1) {
27
+ break;
28
+ }
29
+ var oldLastElementId = oldDocumentIds[rightIndex];
30
+ elementInDiffDocument = getElementIndexInDiffDocument(diffDocument, oldLastElementId);
31
+ if (elementInDiffDocument > -1) {
32
+ break;
33
+ }
34
+ leftIndex--;
35
+ rightIndex++;
36
+ }
37
+ if (elementInDiffDocument === -1) return diffDocument.length - 1;
38
+ return elementInDiffDocument;
39
+ };
3
40
  export var getDiffValue = function getDiffValue(currentValue, oldValue) {
4
41
  var currentVersion = currentValue.version,
5
42
  currentChildren = currentValue.children;
@@ -7,10 +44,61 @@ export var getDiffValue = function getDiffValue(currentValue, oldValue) {
7
44
  oldChildren = oldValue.children;
8
45
  if (currentVersion === oldVersion) return currentChildren;
9
46
  var diffDocument = [];
10
- diffDocument = currentChildren.map(function (item) {
11
- return _objectSpread(_objectSpread({}, item), {}, {
12
- diff_type: DIFF_TYPE.MODIFY
13
- });
47
+
48
+ // init
49
+ var currentChildrenMap = {};
50
+ var oldChildrenMap = {};
51
+ var currentChildrenIds = [];
52
+ var oldChildrenIds = [];
53
+ currentChildren.forEach(function (element) {
54
+ currentChildrenMap[element.id] = element;
55
+ currentChildrenIds.push(element.id);
56
+ });
57
+ oldChildren.forEach(function (element) {
58
+ oldChildrenMap[element.id] = element;
59
+ oldChildrenIds.push(element.id);
14
60
  });
61
+
62
+ // element ids
63
+ var addedChildrenIds = currentChildrenIds.filter(function (item) {
64
+ return !oldChildren.includes(item);
65
+ }); // 增加的
66
+ var deletedChildrenIds = oldChildrenIds.filter(function (item) {
67
+ return !currentChildrenIds.includes(item);
68
+ }); // 删除的
69
+
70
+ // generator diff elements
71
+ for (var i = 0; i < currentChildren.length; i++) {
72
+ var element = currentChildren[i];
73
+ var isAdded = addedChildrenIds.includes(element.id);
74
+ if (isAdded) {
75
+ diffDocument.push(_objectSpread(_objectSpread({}, element), {}, {
76
+ diff_type: DIFF_TYPE.ADD
77
+ }));
78
+ continue;
79
+ }
80
+ var oldElement = oldChildrenMap[element.id];
81
+ if (shallowEqual(oldElement, element)) {
82
+ diffDocument.push(_objectSpread(_objectSpread({}, element), {}, {
83
+ diff_type: DIFF_TYPE.COMMON
84
+ }));
85
+ } else {
86
+ // 深度比较: 需要修改编辑器,暂时不做
87
+ diffDocument.push(_objectSpread(_objectSpread({}, element), {}, {
88
+ diff_type: DIFF_TYPE.ADD
89
+ }));
90
+ diffDocument.push(_objectSpread(_objectSpread({}, oldElement), {}, {
91
+ diff_type: DIFF_TYPE.DELETE
92
+ }));
93
+ }
94
+ }
95
+ for (var _i = 0; _i < deletedChildrenIds.length; _i++) {
96
+ var deletedElementId = deletedChildrenIds[_i];
97
+ var deletedElement = oldChildrenMap[deletedElementId];
98
+ var elementIndex = getNearestElementIndex(diffDocument, oldChildrenIds, deletedElementId);
99
+ diffDocument.splice(elementIndex, 0, _objectSpread(_objectSpread({}, deletedElement), {}, {
100
+ diff_type: DIFF_TYPE.DELETE
101
+ }));
102
+ }
15
103
  return diffDocument;
16
104
  };
@@ -1,5 +1,6 @@
1
1
  export var DIFF_TYPE = {
2
2
  ADD: 'add',
3
3
  DELETE: 'delete',
4
- MODIFY: 'modify'
4
+ MODIFY: 'modify',
5
+ COMMON: 'common'
5
6
  };
@@ -7,6 +7,8 @@ import { Editable, Slate } from '@seafile/slate-react';
7
7
  import editor, { renderLeaf, renderElement } from '../../basic-sdk/extension';
8
8
  import withNodeId from '../../basic-sdk/node-id';
9
9
  import { getDiffValue } from '../../basic-sdk/utils/diff';
10
+ import { DIFF_TYPE } from '../../constants';
11
+ import '../../assets/css/diff-viewer.css';
10
12
  var DiffViewer = /*#__PURE__*/function (_Component) {
11
13
  _inherits(DiffViewer, _Component);
12
14
  var _super = _createSuper(DiffViewer);
@@ -15,11 +17,26 @@ var DiffViewer = /*#__PURE__*/function (_Component) {
15
17
  _classCallCheck(this, DiffViewer);
16
18
  _this = _super.call(this, _props);
17
19
  _this.renderLeaf = function (props) {
18
- console.log('renderLeaf params: ', props);
19
20
  return renderLeaf(props, _this.editor);
20
21
  };
21
22
  _this.renderElement = function (props) {
22
- console.log('renderElement params: ', props);
23
+ var element = props.element;
24
+ var diff_type = element.diff_type;
25
+ if (diff_type === DIFF_TYPE.ADD) {
26
+ return /*#__PURE__*/React.createElement("div", {
27
+ className: "sdoc-diff-added"
28
+ }, renderElement(props, _this.editor));
29
+ }
30
+ if (diff_type === DIFF_TYPE.DELETE) {
31
+ return /*#__PURE__*/React.createElement("div", {
32
+ className: "sdoc-diff-removed"
33
+ }, renderElement(props, _this.editor));
34
+ }
35
+ if (diff_type === DIFF_TYPE.MODIFY) {
36
+ return /*#__PURE__*/React.createElement("div", {
37
+ className: "sdoc-diff-modify"
38
+ }, renderElement(props, _this.editor));
39
+ }
23
40
  return renderElement(props, _this.editor);
24
41
  };
25
42
  _this.editor = withNodeId(editor);
@@ -32,7 +49,13 @@ var DiffViewer = /*#__PURE__*/function (_Component) {
32
49
  currentContent = _this$props.currentContent,
33
50
  lastContent = _this$props.lastContent;
34
51
  var value = getDiffValue(currentContent, lastContent);
35
- return /*#__PURE__*/React.createElement(Slate, {
52
+ return /*#__PURE__*/React.createElement("div", {
53
+ className: "sdoc-editor-container"
54
+ }, /*#__PURE__*/React.createElement("div", {
55
+ className: "sdoc-editor-content"
56
+ }, /*#__PURE__*/React.createElement("div", {
57
+ className: "flex-fill o-auto"
58
+ }, /*#__PURE__*/React.createElement(Slate, {
36
59
  editor: this.editor,
37
60
  value: value,
38
61
  onChange: function onChange() {}
@@ -45,7 +68,7 @@ var DiffViewer = /*#__PURE__*/function (_Component) {
45
68
  renderLeaf: this.renderLeaf,
46
69
  onDOMBeforeInput: function onDOMBeforeInput() {},
47
70
  onKeyDown: function onKeyDown() {}
48
- })));
71
+ }))))));
49
72
  }
50
73
  }]);
51
74
  return DiffViewer;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seafile/sdoc-editor",
3
- "version": "0.1.27beta2",
3
+ "version": "0.1.27beta3",
4
4
  "private": false,
5
5
  "description": "This is a sdoc editor",
6
6
  "main": "dist/index.js",
@@ -16,6 +16,7 @@
16
16
  "is-url": "^1.2.4",
17
17
  "react-cookies": "0.1.1",
18
18
  "reactstrap": "8.9.0",
19
+ "shallowequal": "^1.1.0",
19
20
  "slugid": "3.2.0",
20
21
  "socket.io-client": "4.6.1",
21
22
  "url-parse": "1.5.10",