@seafile/sdoc-editor 0.1.60 → 0.1.61

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,5 +1,5 @@
1
1
  import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
2
- import React, { useCallback, useEffect, useMemo, useState } from 'react';
2
+ import React, { useCallback, useEffect, useMemo, useState, useRef } from 'react';
3
3
  import { Node } from '@seafile/slate';
4
4
  import { Editable, Slate, ReactEditor } from '@seafile/slate-react';
5
5
  import defaultEditor, { renderLeaf, renderElement, Toolbar, ContextMenu } from './extension';
@@ -19,6 +19,8 @@ import './assets/css/sdoc-editor-plugins.css';
19
19
  var SDocEditor = function SDocEditor(_ref) {
20
20
  var document = _ref.document,
21
21
  config = _ref.config;
22
+ var articleRef = useRef(null);
23
+
22
24
  // init editor
23
25
  var editor = useMemo(function () {
24
26
  var newEditor = withNodeId(withSocketIO(defaultEditor, {
@@ -57,6 +59,7 @@ var SDocEditor = function SDocEditor(_ref) {
57
59
  // useMount: init socket connection
58
60
  useEffect(function () {
59
61
  editor.openConnection();
62
+ editor.width = articleRef.current.children[0].clientWidth;
60
63
  return function () {
61
64
  editor.closeConnection();
62
65
  };
@@ -111,7 +114,8 @@ var SDocEditor = function SDocEditor(_ref) {
111
114
  value: slateValue,
112
115
  onChange: onChange
113
116
  }, /*#__PURE__*/React.createElement("div", {
114
- className: "article"
117
+ className: "article",
118
+ ref: articleRef
115
119
  }, /*#__PURE__*/React.createElement(SetNodeToDecorations, null), /*#__PURE__*/React.createElement(Editable, {
116
120
  renderElement: renderElement,
117
121
  renderLeaf: renderLeaf,
@@ -113,4 +113,12 @@ export var TABLE_ELEMENT_POSITION = {
113
113
  AFTER: 'after',
114
114
  BEFORE: 'before'
115
115
  };
116
+ export var DELETED_STYLE = {
117
+ background_color: '#ffeef0',
118
+ color: 'rgb(165, 32, 21)'
119
+ };
120
+ export var ADDED_STYLE = {
121
+ background_color: '#e6ffed',
122
+ color: 'rgb(137, 181, 66)'
123
+ };
116
124
  export { BLOCKQUOTE, HEADER, HEADER1, HEADER2, HEADER3, HEADER4, HEADER5, HEADER6, PARAGRAPH, BOLD, ITALIC, UNDERLINE, STRIKETHROUGH, ORDERED_LIST, UNORDERED_LIST, LIST_ITEM, LIST_LIC, CHECK_LIST, CHECK_LIST_ITEM, LINK, HTML, CODE_BLOCK, CODE_LINE, IMAGE, TABLE, TABLE_CELL, TABLE_ROW, FORMULA, COLUMN, TEXT_STYLE, BOLD_ITALIC, TEXT_ALIGN, ALIGN_LEFT, ALIGN_RIGHT, ALIGN_CENTER, ELEMENT_TYPE, KEYBOARD };
@@ -18,13 +18,16 @@ export var match = function match(node, path, predicate) {
18
18
  }
19
19
  return predicate(node, path);
20
20
  };
21
+ export var generateDefaultText = function generateDefaultText() {
22
+ return {
23
+ id: slugid.nice(),
24
+ text: ''
25
+ };
26
+ };
21
27
  export var generateEmptyElement = function generateEmptyElement(type) {
22
28
  return {
23
29
  type: type,
24
- children: [{
25
- id: slugid.nice(),
26
- text: ''
27
- }]
30
+ children: [generateDefaultText()]
28
31
  };
29
32
  };
30
33
  export function Placeholder(props) {
@@ -68,7 +68,8 @@ export var changeToCodeBlock = function changeToCodeBlock(editor, language) {
68
68
  id: slugid.nice(),
69
69
  type: CODE_LINE,
70
70
  children: [{
71
- text: strArr.join('\n')
71
+ text: strArr.join('\n'),
72
+ id: slugid.nice()
72
73
  } // Select the plain text of the node
73
74
  ]
74
75
  }]
@@ -95,8 +96,10 @@ export var changeToPlainText = function changeToPlainText(editor) {
95
96
  return {
96
97
  type: 'paragraph',
97
98
  children: [{
98
- text: s
99
- }]
99
+ text: s,
100
+ id: slugid.nice()
101
+ }],
102
+ id: elem.id
100
103
  };
101
104
  });
102
105
  Transforms.insertNodes(editor, pList, {
@@ -99,7 +99,8 @@ var CodeBlock = function CodeBlock(_ref) {
99
99
  document.getElementById('sdoc-editor-article-container').removeEventListener('scroll', onScroll);
100
100
  }
101
101
  return function () {
102
- document.getElementById('sdoc-editor-article-container').removeEventListener('scroll', onScroll);
102
+ var articleContainer = document.getElementById('sdoc-editor-article-container');
103
+ articleContainer && articleContainer.removeEventListener('scroll', onScroll);
103
104
  };
104
105
  // eslint-disable-next-line react-hooks/exhaustive-deps
105
106
  }, [showHoverMenu]);
@@ -6,7 +6,5 @@ export var EMPTY_SELECTED_RANGE = {
6
6
  minColIndex: -1,
7
7
  maxColIndex: -1
8
8
  };
9
- export var TABLE_MAX_WIDTH = 672; // 673 = 794 - 2[borderLeft + borderRight] - 120[paddingLeft + paddingRight]
10
-
11
9
  export var TABLE_ROW_MIN_HEIGHT = 42;
12
10
  export var TABLE_CELL_MIN_WIDTH = 35;
@@ -5,7 +5,7 @@ import slugid from 'slugid';
5
5
  import { Editor, Range, Transforms } from '@seafile/slate';
6
6
  import { getNodeType, getParentNode, getSelectedNodeByType, isTextNode, getSelectedElems, focusEditor, getNode, findPath } from '../../core';
7
7
  import { ELEMENT_TYPE, TABLE_ELEMENT, TABLE_ELEMENT_POSITION } from '../../constants';
8
- import { TABLE_MAX_ROWS, TABLE_MAX_COLUMNS, EMPTY_SELECTED_RANGE, TABLE_ROW_MIN_HEIGHT, TABLE_CELL_MIN_WIDTH, TABLE_MAX_WIDTH } from './constants';
8
+ import { TABLE_MAX_ROWS, TABLE_MAX_COLUMNS, EMPTY_SELECTED_RANGE, TABLE_ROW_MIN_HEIGHT, TABLE_CELL_MIN_WIDTH } from './constants';
9
9
  import EventBus from '../../../utils/event-bus';
10
10
  import { EXTERNAL_EVENT } from '../../../../constants';
11
11
  import ObjectUtils from '../../../utils/object-utils';
@@ -65,8 +65,8 @@ export var generateTableRow = function generateTableRow(colsCount) {
65
65
  }
66
66
  };
67
67
  };
68
- export var generateEmptyTable = function generateEmptyTable() {
69
- var size = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [0, 0];
68
+ export var generateEmptyTable = function generateEmptyTable(editor) {
69
+ var size = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [0, 0];
70
70
  var rowsCount = size[0];
71
71
  var colsCount = size[1];
72
72
  var children = [];
@@ -74,7 +74,7 @@ export var generateEmptyTable = function generateEmptyTable() {
74
74
  var tableRow = generateTableRow(colsCount);
75
75
  children.push(tableRow);
76
76
  }
77
- var columnWidth = Math.max(TABLE_CELL_MIN_WIDTH, parseInt(TABLE_MAX_WIDTH / colsCount));
77
+ var columnWidth = Math.max(TABLE_CELL_MIN_WIDTH, parseInt(editor.width / colsCount));
78
78
  var columns = [];
79
79
  for (var _i = 0; _i < colsCount; _i++) {
80
80
  columns.push({
@@ -91,7 +91,7 @@ export var generateEmptyTable = function generateEmptyTable() {
91
91
  export var insertTable = function insertTable(editor, size, selection) {
92
92
  if (!size) return;
93
93
  if (isTableMenuDisabled(editor)) return;
94
- var tableNode = generateEmptyTable(size);
94
+ var tableNode = generateEmptyTable(editor, size);
95
95
  Transforms.insertNodes(editor, tableNode, {
96
96
  at: selection
97
97
  });
@@ -351,7 +351,7 @@ export var getTableColumnsAfterInsertColumn = function getTableColumnsAfterInser
351
351
  var targetInsertColumnsWidth = targetColumn.width * insertColumnCount;
352
352
 
353
353
  // Currently in scrolling state, insert directly
354
- if (totalColumnsWidth > TABLE_MAX_WIDTH) {
354
+ if (totalColumnsWidth > editor.width) {
355
355
  for (var i = 0; i < insertColumnCount; i++) {
356
356
  newColumns.splice(targetColumnIndex, 0, targetColumn);
357
357
  }
@@ -360,7 +360,7 @@ export var getTableColumnsAfterInsertColumn = function getTableColumnsAfterInser
360
360
 
361
361
  // Not currently scrolling
362
362
  // It is not a scroll state after inserting a new column
363
- if (totalColumnsWidth + targetInsertColumnsWidth < TABLE_MAX_WIDTH) {
363
+ if (totalColumnsWidth + targetInsertColumnsWidth < editor.width) {
364
364
  for (var _i5 = 0; _i5 < insertColumnCount; _i5++) {
365
365
  newColumns.push(targetColumn);
366
366
  }
@@ -372,7 +372,7 @@ export var getTableColumnsAfterInsertColumn = function getTableColumnsAfterInser
372
372
  totalColumnsWidth += targetColumn.width;
373
373
  newColumns.splice(targetColumnIndex, 0, targetColumn);
374
374
  }
375
- var proportion = totalColumnsWidth / (TABLE_MAX_WIDTH - 1);
375
+ var proportion = totalColumnsWidth / (editor.width - 1);
376
376
  return newColumns.map(function (column) {
377
377
  return _objectSpread(_objectSpread({}, column), {}, {
378
378
  width: Math.max(parseInt(column.width / proportion), TABLE_CELL_MIN_WIDTH)
@@ -395,7 +395,7 @@ export var getTableColumns = function getTableColumns(editor, element) {
395
395
  var initColumns = [];
396
396
  for (var i = 0; i < columnsCount; i++) {
397
397
  var column = {
398
- width: Math.max(TABLE_CELL_MIN_WIDTH, parseInt(TABLE_MAX_WIDTH / columnsCount))
398
+ width: Math.max(TABLE_CELL_MIN_WIDTH, parseInt(editor.width / columnsCount))
399
399
  };
400
400
  initColumns.push(column);
401
401
  }
@@ -418,7 +418,7 @@ export var getCellColumn = function getCellColumn(editor, cellElement) {
418
418
  } else {
419
419
  var columnsCount = tableElement.children[0].children.length;
420
420
  column = {
421
- width: Math.max(TABLE_CELL_MIN_WIDTH, parseInt(TABLE_MAX_WIDTH / columnsCount))
421
+ width: Math.max(TABLE_CELL_MIN_WIDTH, parseInt(editor.width / columnsCount))
422
422
  };
423
423
  }
424
424
  return column;
@@ -74,6 +74,7 @@ function renderTableCell(props) {
74
74
  var width = column.width;
75
75
  return /*#__PURE__*/React.createElement("div", Object.assign({}, attributes, {
76
76
  style: _objectSpread(_objectSpread(_objectSpread({}, style), element.style), {}, {
77
+ minWidth: width,
77
78
  width: width
78
79
  }),
79
80
  className: classnames('table-cell', attributes.className),
@@ -1,13 +1,15 @@
1
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
1
2
  import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
2
- import React, { useState, useRef } from 'react';
3
+ import React, { useState, useRef, useCallback } from 'react';
3
4
  import classnames from 'classnames';
4
- import { TABLE_MAX_WIDTH } from '../constants';
5
+ import { useSlateStatic } from '@seafile/slate-react';
5
6
  import { TableRootContext } from './hooks';
6
7
  var TableRoot = function TableRoot(_ref) {
7
8
  var attributes = _ref.attributes,
8
9
  _ref$columns = _ref.columns,
9
10
  columns = _ref$columns === void 0 ? [] : _ref$columns,
10
11
  children = _ref.children;
12
+ var editor = useSlateStatic();
11
13
  var tableScrollWrapper = useRef(null);
12
14
  var _useState = useState(0),
13
15
  _useState2 = _slicedToArray(_useState, 2),
@@ -16,19 +18,22 @@ var TableRoot = function TableRoot(_ref) {
16
18
  var allWidth = columns.reduce(function (pre, cur) {
17
19
  return pre + cur.width;
18
20
  }, 0);
19
- var onScroll = function onScroll(event) {
21
+ var onScroll = useCallback(function (event) {
20
22
  setScrollLeft(event.target.scrollLeft);
21
- };
23
+ }, []);
22
24
  return /*#__PURE__*/React.createElement(TableRootContext.Provider, {
23
25
  value: tableScrollWrapper.current
24
- }, /*#__PURE__*/React.createElement("div", Object.assign({
25
- className: classnames('sdoc-table-wrapper', {
26
- 'scroll position-relative': allWidth > TABLE_MAX_WIDTH
26
+ }, /*#__PURE__*/React.createElement("div", Object.assign({}, attributes, {
27
+ className: classnames('sdoc-table-wrapper', attributes.className, {
28
+ 'scroll position-relative': allWidth > editor.width
29
+ }),
30
+ style: _objectSpread(_objectSpread({}, attributes.style), {}, {
31
+ maxWidth: editor.width
27
32
  })
28
- }, attributes), /*#__PURE__*/React.createElement("div", {
33
+ }), /*#__PURE__*/React.createElement("div", {
29
34
  className: classnames('sdoc-table-scroll-wrapper', {
30
- 'scroll-at-center': scrollLeft + TABLE_MAX_WIDTH !== allWidth && scrollLeft > 0,
31
- 'scroll-at-right': scrollLeft + TABLE_MAX_WIDTH === allWidth,
35
+ 'scroll-at-center': scrollLeft + editor.width !== allWidth && scrollLeft > 0,
36
+ 'scroll-at-right': scrollLeft + editor.width === allWidth,
32
37
  'scroll-at-left': scrollLeft === 0
33
38
  }),
34
39
  ref: tableScrollWrapper,
@@ -9,6 +9,18 @@ var renderText = function renderText(props, editor) {
9
9
  var text = leaf.text,
10
10
  rest = _objectWithoutProperties(leaf, _excluded);
11
11
  var markedChildren = React.cloneElement(children);
12
+ var style = {};
13
+ if (leaf.isCaret) {
14
+ style['position'] = 'relative';
15
+ style['display'] = 'inline-block';
16
+ style['minWidth'] = '2px';
17
+ }
18
+ if (leaf.background_color) {
19
+ style['backgroundColor'] = leaf.background_color;
20
+ }
21
+ if (leaf.color) {
22
+ style['color'] = leaf.color;
23
+ }
12
24
  if (leaf.BOLD) {
13
25
  markedChildren = /*#__PURE__*/React.createElement("strong", null, markedChildren);
14
26
  }
@@ -36,20 +48,13 @@ var renderText = function renderText(props, editor) {
36
48
  markedChildren = /*#__PURE__*/React.createElement("del", null, markedChildren);
37
49
  }
38
50
  if (leaf.ADD) {
39
- markedChildren = /*#__PURE__*/React.createElement("ins", null, markedChildren);
51
+ markedChildren = /*#__PURE__*/React.createElement("span", null, markedChildren);
40
52
  }
41
53
  if (leaf.decoration) {
42
54
  markedChildren = /*#__PURE__*/React.createElement("span", {
43
55
  className: "token ".concat(leaf.type)
44
56
  }, markedChildren);
45
57
  }
46
- var style = {
47
- position: 'relative'
48
- };
49
- if (leaf.isCaret) {
50
- style['display'] = 'inline-block';
51
- style['minWidth'] = '2px';
52
- }
53
58
  return /*#__PURE__*/React.createElement("span", Object.assign({
54
59
  "data-id": leaf.id
55
60
  }, attributes, {
@@ -0,0 +1,255 @@
1
+ import _createClass from "@babel/runtime/helpers/esm/createClass";
2
+ import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
3
+ import ObjectUtils from './object-utils';
4
+ var buildValues = function buildValues(diff, components, newString, oldString, valueType, useLongestToken) {
5
+ var componentPos = 0;
6
+ var componentLen = components.length;
7
+ var newPos = 0;
8
+ var oldPos = 0;
9
+ for (; componentPos < componentLen; componentPos++) {
10
+ var component = components[componentPos];
11
+ if (!component.removed) {
12
+ if (!component.added && useLongestToken) {
13
+ var value = newString.slice(newPos, newPos + component.count);
14
+ // eslint-disable-next-line no-loop-func
15
+ value = value.map(function (value, i) {
16
+ var oldValue = oldString[oldPos + i];
17
+ return oldValue.length > value.length ? oldValue : value;
18
+ });
19
+ component.value = diff.join(value, valueType);
20
+ } else {
21
+ component.value = diff.join(newString.slice(newPos, newPos + component.count), valueType);
22
+ }
23
+ newPos += component.count;
24
+
25
+ // Common case
26
+ if (!component.added) {
27
+ oldPos += component.count;
28
+ }
29
+ } else {
30
+ component.value = diff.join(oldString.slice(oldPos, oldPos + component.count), valueType);
31
+ oldPos += component.count;
32
+
33
+ // Reverse add and remove so removes are output first to match common convention
34
+ // The diffing algorithm is tied to add then remove output and this is the simplest
35
+ // route to get the desired output with minimal overhead.
36
+ if (componentPos && components[componentPos - 1].added) {
37
+ var tmp = components[componentPos - 1];
38
+ components[componentPos - 1] = components[componentPos];
39
+ components[componentPos] = tmp;
40
+ }
41
+ }
42
+ }
43
+
44
+ // Special case handle for when one terminal is ignored (i.e. whitespace).
45
+ // For this case we merge the terminal into the prior string and drop the change.
46
+ // This is only available for string mode.
47
+ var lastComponent = components[componentLen - 1];
48
+ if (componentLen > 1 && typeof lastComponent.value === 'string' && (lastComponent.added || lastComponent.removed) && diff.equals('', lastComponent.value)) {
49
+ components[componentLen - 2].value += lastComponent.value;
50
+ components.pop();
51
+ }
52
+ return components;
53
+ };
54
+ var clonePath = function clonePath(path) {
55
+ return {
56
+ newPos: path.newPos,
57
+ components: path.components.slice(0)
58
+ };
59
+ };
60
+ var DiffText = /*#__PURE__*/_createClass(function DiffText(oldValue, newValue) {
61
+ var _this = this;
62
+ var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
63
+ _classCallCheck(this, DiffText);
64
+ this.done = function (value) {
65
+ if (_this.callback) {
66
+ setTimeout(function () {
67
+ this.callback(undefined, value);
68
+ }, 0);
69
+ return true;
70
+ }
71
+ return value;
72
+ };
73
+ // Main worker method. checks all permutations of a given edit length for acceptance.
74
+ this.execCompareLength = function (bestPath) {
75
+ for (var diagonalPath = -1 * _this.comparePath; diagonalPath <= _this.comparePath; diagonalPath += 2) {
76
+ var basePath = void 0;
77
+ var addPath = bestPath[diagonalPath - 1];
78
+ var removePath = bestPath[diagonalPath + 1];
79
+ var oldPos = (removePath ? removePath.newPos : 0) - diagonalPath;
80
+ if (addPath) {
81
+ // No one else is going to attempt to use this value, clear it
82
+ bestPath[diagonalPath - 1] = undefined;
83
+ }
84
+ var canAdd = addPath && addPath.newPos + 1 < _this.newLen;
85
+ var canRemove = removePath && 0 <= oldPos && oldPos < _this.oldLen;
86
+ if (!canAdd && !canRemove) {
87
+ // If this path is a terminal then prune
88
+ bestPath[diagonalPath] = undefined;
89
+ continue;
90
+ }
91
+
92
+ // Select the diagonal that we want to branch from. We select the prior
93
+ // path whose position in the new string is the farthest from the origin
94
+ // and does not pass the bounds of the diff graph
95
+ if (!canAdd || canRemove && addPath.newPos < removePath.newPos) {
96
+ basePath = clonePath(removePath);
97
+ _this.pushComponent(basePath.components, undefined, true);
98
+ } else {
99
+ basePath = addPath; // No need to clone, we've pulled it from the list
100
+ basePath.newPos++;
101
+ _this.pushComponent(basePath.components, true, undefined);
102
+ }
103
+ oldPos = _this.extractCommon(basePath, _this.newValue, _this.oldValue, diagonalPath);
104
+
105
+ // If we have hit the end of both strings, then we are done
106
+ if (basePath.newPos + 1 >= _this.newLen && oldPos + 1 >= _this.oldLen) {
107
+ return _this.done(buildValues(_this, basePath.components, _this.newValue, _this.oldValue, _this.valueType, _this.useLongestToken));
108
+ } else {
109
+ // Otherwise track this path as a potential candidate and continue.
110
+ bestPath[diagonalPath] = basePath;
111
+ }
112
+ }
113
+ _this.comparePath++;
114
+ };
115
+ this.exec = function (bestPath) {
116
+ setTimeout(function () {
117
+ if (this.comparePath > this.maxEditLength) {
118
+ return this.callback();
119
+ }
120
+ if (!this.execCompareLength(bestPath)) {
121
+ this.exec(bestPath);
122
+ }
123
+ }, 0);
124
+ };
125
+ this.pushComponent = function (components, added, removed) {
126
+ var last = components[components.length - 1];
127
+ if (last && last.added === added && last.removed === removed) {
128
+ // We need to clone here as the component clone operation is just
129
+ // as shallow array clone
130
+ components[components.length - 1] = {
131
+ count: last.count + 1,
132
+ added: added,
133
+ removed: removed
134
+ };
135
+ } else {
136
+ components.push({
137
+ count: 1,
138
+ added: added,
139
+ removed: removed
140
+ });
141
+ }
142
+ };
143
+ this.extractCommon = function (basePath, newString, oldString, diagonalPath) {
144
+ var newLen = newString.length;
145
+ var oldLen = oldString.length;
146
+ var newPos = basePath.newPos;
147
+ var oldPos = newPos - diagonalPath;
148
+ var commonCount = 0;
149
+ while (newPos + 1 < newLen && oldPos + 1 < oldLen && _this.equals(newString[newPos + 1], oldString[oldPos + 1])) {
150
+ newPos++;
151
+ oldPos++;
152
+ commonCount++;
153
+ }
154
+ if (commonCount) {
155
+ basePath.components.push({
156
+ count: commonCount
157
+ });
158
+ }
159
+ basePath.newPos = newPos;
160
+ return oldPos;
161
+ };
162
+ this.equals = function (left, right) {
163
+ if (_this.options.comparator) {
164
+ return _this.options.comparator(left, right);
165
+ } else {
166
+ return left === right || _this.options.ignoreCase && left.toLowerCase() === right.toLowerCase();
167
+ }
168
+ };
169
+ this.removeEmpty = function (array, type) {
170
+ if (type === 'Array') return array;
171
+ var ret = [];
172
+ for (var i = 0; i < array.length; i++) {
173
+ if (array[i]) {
174
+ ret.push(array[i]);
175
+ }
176
+ }
177
+ return ret;
178
+ };
179
+ this.tokenize = function (value, valueType) {
180
+ if (valueType === 'Array') return value.slice();
181
+ return value.split('');
182
+ };
183
+ this.join = function (value, valueType) {
184
+ if (valueType === 'Array') return value;
185
+ return value.join('');
186
+ };
187
+ this.getDiffs = function () {
188
+ if (!_this.canCompare) {
189
+ return [{
190
+ value: _this.oldValue,
191
+ removed: true
192
+ }, {
193
+ value: _this.newValue,
194
+ added: true
195
+ }];
196
+ }
197
+ var bestPath = [{
198
+ newPos: -1,
199
+ components: []
200
+ }];
201
+
202
+ // Seed editLength = 0, i.e. the content starts with the same values
203
+ var oldPos = _this.extractCommon(bestPath[0], _this.newValue, _this.oldValue, 0);
204
+ if (bestPath[0].newPos + 1 >= _this.newLen && oldPos + 1 >= _this.oldLen) {
205
+ // Identity per the equality and tokenizer
206
+ return _this.done([{
207
+ value: _this.join(_this.newValue, _this.valueType),
208
+ count: _this.oldValue.length
209
+ }]);
210
+ }
211
+
212
+ // Performs the length of edit iteration. Is a bit fugly as this has to support the
213
+ // sync and async mode which is never fun. Loops over execCompareLength until a value
214
+ // is produced, or until the edit length exceeds options.maxEditLength (if given),
215
+ // in which case it will return undefined.
216
+ if (_this.callback) {
217
+ _this.exec(bestPath);
218
+ } else {
219
+ while (_this.comparePath <= _this.maxEditLength) {
220
+ var ret = _this.execCompareLength(bestPath);
221
+ if (ret) {
222
+ return ret;
223
+ }
224
+ }
225
+ }
226
+ };
227
+ this.oldValue = oldValue;
228
+ this.newValue = newValue;
229
+ var oldValueType = ObjectUtils.getDataType(oldValue);
230
+ var newValueType = ObjectUtils.getDataType(newValue);
231
+ this.canCompare = true;
232
+ if (oldValueType !== newValueType) {
233
+ this.canCompare = false;
234
+ return;
235
+ }
236
+ this.valueType = newValueType;
237
+ this.callback = options.callback;
238
+ var optionsType = ObjectUtils.getDataType(options);
239
+ if (optionsType === 'function') {
240
+ this.callback = options;
241
+ this.options = {};
242
+ } else {
243
+ this.options = {};
244
+ }
245
+ this.comparePath = 1;
246
+ this.oldValue = this.removeEmpty(this.tokenize(oldValue, oldValueType), oldValueType);
247
+ this.oldLen = this.oldValue.length;
248
+ this.newValue = this.removeEmpty(this.tokenize(newValue, newValueType), newValueType);
249
+ this.newLen = this.newValue.length;
250
+ this.maxEditLength = this.newLen + this.oldLen;
251
+ if (this.options.maxEditLength) {
252
+ this.maxEditLength = Math.min(this.maxEditLength, this.options.maxEditLength);
253
+ }
254
+ });
255
+ export default DiffText;
@@ -1,122 +1,206 @@
1
1
  import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
2
+ import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
2
3
  import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
3
- import { DIFF_TYPE, ELEMENT_TYPE } from '../../basic-sdk/extension/constants';
4
+ import slugid from 'slugid';
5
+ import { ELEMENT_TYPE, ADDED_STYLE, DELETED_STYLE } from '../../basic-sdk/extension/constants';
4
6
  import ObjectUtils from './object-utils';
5
- var getElementIndexInDiffDocument = function getElementIndexInDiffDocument(diffValue, element) {
6
- if (!diffValue || !element) return -1;
7
- return diffValue.findIndex(function (item) {
8
- return item.id === element.id;
9
- });
7
+ import DiffText from './diff-text';
8
+ import { normalizeChildren } from './document-utils';
9
+
10
+ // ignore
11
+ var IGNORE_KEYS = ['BOLD', 'ITALIC', 'columns', 'minHeight', 'language', 'white_space'];
12
+ var generatorDiffTextElement = function generatorDiffTextElement(textElement, diffType) {
13
+ var style = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
14
+ if (!textElement) return null;
15
+ if (!diffType) return textElement;
16
+ var id = textElement.id;
17
+ return _objectSpread(_objectSpread({}, textElement), {}, _defineProperty({
18
+ id: id || slugid.nice()
19
+ }, diffType, true), style);
20
+ };
21
+
22
+ // Depth facilitates each child node, adding diffType to each end node
23
+ var generatorDiffElement = function generatorDiffElement(element, diffType) {
24
+ var _objectSpread3;
25
+ var style = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
26
+ if (!element) return null;
27
+ if (!Array.isArray(element.children) || element.children.length === 0) {
28
+ return generatorDiffTextElement(element, diffType, style);
29
+ }
30
+ return _objectSpread(_objectSpread({}, element), {}, (_objectSpread3 = {}, _defineProperty(_objectSpread3, diffType, true), _defineProperty(_objectSpread3, "children", element.children.map(function (item) {
31
+ return generatorDiffElement(item, diffType, style);
32
+ })), _objectSpread3));
10
33
  };
11
- var getNearestElementIndex = function getNearestElementIndex(diff, oldDocument, elementId) {
12
- var diffValue = diff.value;
13
- var elementInDiffDocumentIdx = getElementIndexInDiffDocument(diffValue, elementId);
14
- if (elementInDiffDocumentIdx > -1) return elementInDiffDocumentIdx + 1;
15
- var oldElementCount = oldDocument.length;
16
- var elementInOldDocumentIndex = oldDocument.findIndex(function (item) {
17
- return item.id === elementId;
34
+ var generateIdMapAndIds = function generateIdMapAndIds(elements) {
35
+ var map = {};
36
+ var ids = [];
37
+ if (!Array.isArray(elements) || elements.length === 0) return {
38
+ map: map,
39
+ ids: ids
40
+ };
41
+ elements.forEach(function (element) {
42
+ ids.push(element.id);
43
+ map[element.id] = element;
18
44
  });
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 (elementInDiffDocumentIdx === -1 && leftIndex > -2 && rightIndex < oldElementCount + 1) {
24
- if (leftIndex > -1) {
25
- var oldLeftElement = oldDocument[leftIndex];
26
- elementInDiffDocumentIdx = getElementIndexInDiffDocument(diffValue, oldLeftElement);
27
- if (elementInDiffDocumentIdx > -1) {
28
- return elementInDiffDocumentIdx + 1;
29
- }
30
- }
31
- if (rightIndex < oldElementCount) {
32
- var oldLastElement = oldDocument[rightIndex];
33
- elementInDiffDocumentIdx = getElementIndexInDiffDocument(diffValue, oldLastElement);
34
- if (elementInDiffDocumentIdx > -1) {
35
- return elementInDiffDocumentIdx;
36
- }
37
- }
38
- leftIndex--;
39
- rightIndex++;
40
- if (leftIndex === -2 && rightIndex < oldElementCount) {
41
- leftIndex = -1;
42
- }
43
- if (rightIndex === oldElementCount && leftIndex > -1) {
44
- rightIndex = oldElementCount;
45
+ return {
46
+ map: map,
47
+ ids: ids
48
+ };
49
+ };
50
+ var hasChildren = function hasChildren(element) {
51
+ if (!element) return false;
52
+ if (!Array.isArray(element.children) || element.children.length === 0) return false;
53
+ return true;
54
+ };
55
+
56
+ // id diffs
57
+ var getIdDiffs = function getIdDiffs(oldIds, newIds) {
58
+ var diff = new DiffText(oldIds, newIds);
59
+ return diff.getDiffs();
60
+ };
61
+
62
+ // text diffs
63
+ var getTextDiff = function getTextDiff(element, oldElement, diff) {
64
+ var newText = element.text;
65
+ var oldText = oldElement.text;
66
+ var textDiff = new DiffText(oldText || '', newText || '');
67
+ var textDiffs = textDiff.getDiffs();
68
+ var newChildren = [];
69
+ textDiffs.forEach(function (item, index) {
70
+ var id = "".concat(element.id || slugid.nice(), "-").concat(index);
71
+ var elementItem = {
72
+ id: id,
73
+ text: item.value
74
+ };
75
+ if (item.added) {
76
+ diff.changes.push(id);
77
+ var commonChild = generatorDiffTextElement(elementItem, 'ADD', ADDED_STYLE);
78
+ newChildren.push(commonChild);
79
+ } else if (item.removed) {
80
+ diff.changes.push(id);
81
+ var _commonChild = generatorDiffTextElement(elementItem, 'DELETE', DELETED_STYLE);
82
+ newChildren.push(_commonChild);
83
+ } else {
84
+ var _commonChild2 = generatorDiffTextElement(elementItem);
85
+ newChildren.push(_commonChild2);
45
86
  }
87
+ });
88
+ return newChildren;
89
+ };
90
+ var getCommonDiff = function getCommonDiff(element, oldElement, diff) {
91
+ if (!hasChildren(element) && !hasChildren(oldElement)) {
92
+ var _newChildren = getTextDiff(element, oldElement, diff);
93
+ return [_objectSpread(_objectSpread({}, element), {}, {
94
+ children: _newChildren
95
+ })];
46
96
  }
47
- return diffValue.length - 1;
97
+ if (!hasChildren(element) || !hasChildren(oldElement)) {
98
+ var elementId = element.id || slugid.nice();
99
+ diff.changes.push("".concat(elementId, "_delete"));
100
+ return [generatorDiffElement(_objectSpread(_objectSpread({}, oldElement), {}, {
101
+ id: "".concat(elementId, "_delete")
102
+ }), 'DELETE', DELETED_STYLE), generatorDiffElement(_objectSpread(_objectSpread({}, element), {}, {
103
+ id: "".concat(elementId, "_add")
104
+ }), 'ADD', ADDED_STYLE)];
105
+ }
106
+
107
+ // Content does not change
108
+ if (ObjectUtils.isSameObject(element, oldElement, [].concat(IGNORE_KEYS, ['type']))) {
109
+ return [element];
110
+ }
111
+
112
+ // Compare content
113
+ var currentChildren = element.children;
114
+ var oldChildren = oldElement.children;
115
+ var _generateIdMapAndIds = generateIdMapAndIds(currentChildren),
116
+ currentMap = _generateIdMapAndIds.map,
117
+ currentIds = _generateIdMapAndIds.ids;
118
+ var _generateIdMapAndIds2 = generateIdMapAndIds(oldChildren),
119
+ oldMap = _generateIdMapAndIds2.map,
120
+ oldIds = _generateIdMapAndIds2.ids;
121
+ var idDiffs = getIdDiffs(oldIds, currentIds);
122
+ var newChildren = [];
123
+ idDiffs.forEach(function (idDiff) {
124
+ var ids = idDiff.value;
125
+ var isAdded = idDiff.added;
126
+ var isDelete = idDiff.removed;
127
+ ids.forEach(function (id) {
128
+ var newChildrenElement = currentMap[id];
129
+ var oldChildrenElement = oldMap[id];
130
+ if (isAdded) {
131
+ diff.changes.push(id);
132
+ newChildren.push(generatorDiffElement(newChildrenElement, 'ADD', ADDED_STYLE));
133
+ } else if (isDelete) {
134
+ diff.changes.push(id);
135
+ newChildren.push(generatorDiffElement(oldChildrenElement, 'DELETE', DELETED_STYLE));
136
+ } else {
137
+ if (ObjectUtils.isSameObject(newChildrenElement, oldChildrenElement, IGNORE_KEYS)) {
138
+ newChildren.push(newChildrenElement);
139
+ } else {
140
+ if (newChildrenElement.type === oldChildrenElement.type) {
141
+ if (!newChildrenElement.type) {
142
+ var textDiffs = getTextDiff(newChildrenElement, oldChildrenElement, diff);
143
+ newChildren.push.apply(newChildren, _toConsumableArray(textDiffs));
144
+ } else if (newChildrenElement.type === ELEMENT_TYPE.IMAGE) {
145
+ if (newChildrenElement.data.src === oldChildrenElement.data.src) {
146
+ newChildren.push(newChildrenElement);
147
+ } else {
148
+ newChildren.push(generatorDiffTextElement(_objectSpread(_objectSpread({}, element), {}, {
149
+ id: element.id + '_add'
150
+ }), 'ADD', ADDED_STYLE));
151
+ newChildren.push(generatorDiffTextElement(_objectSpread(_objectSpread({}, oldChildrenElement), {}, {
152
+ id: element.id + '_delete'
153
+ }), 'DELETE', DELETED_STYLE));
154
+ }
155
+ } else if (newChildrenElement.type === ELEMENT_TYPE.LINK) {
156
+ if (newChildrenElement.title !== oldChildrenElement.title) {
157
+ var diffElements = getCommonDiff(newChildrenElement, oldChildrenElement, diff);
158
+ newChildren.push.apply(newChildren, _toConsumableArray(diffElements));
159
+ } else if (newChildrenElement.href !== oldChildrenElement.href) {
160
+ diff.changes.push(oldChildrenElement.id + '_delete');
161
+ newChildren.push(generatorDiffTextElement(_objectSpread(_objectSpread({}, oldChildrenElement), {}, {
162
+ id: oldChildrenElement.id + '_delete'
163
+ }), 'DELETE', DELETED_STYLE));
164
+ newChildren.push(generatorDiffTextElement(_objectSpread(_objectSpread({}, newChildrenElement), {}, {
165
+ id: newChildrenElement.id + '_add'
166
+ }), 'ADD', ADDED_STYLE));
167
+ } else {
168
+ newChildren.push(newChildrenElement);
169
+ }
170
+ } else if (newChildrenElement.type === ELEMENT_TYPE.TABLE_ROW || newChildrenElement.type === ELEMENT_TYPE.TABLE_CELL) {
171
+ var newRows = getCommonDiff(newChildrenElement, oldChildrenElement, diff);
172
+ newChildren.push.apply(newChildren, _toConsumableArray(newRows));
173
+ } else {
174
+ var commonDiffs = getCommonDiff(newChildrenElement, oldChildrenElement, diff);
175
+ newChildren.push.apply(newChildren, _toConsumableArray(commonDiffs));
176
+ }
177
+ } else {
178
+ diff.changes.push(oldChildrenElement.id + '_delete');
179
+ newChildren.push(generatorDiffTextElement(_objectSpread(_objectSpread({}, oldChildrenElement), {}, {
180
+ id: oldChildrenElement.id + '_delete'
181
+ }), 'DELETE', DELETED_STYLE));
182
+ newChildren.push(generatorDiffTextElement(_objectSpread(_objectSpread({}, newChildrenElement), {}, {
183
+ id: newChildrenElement.id + '_add'
184
+ }), 'ADD', ADDED_STYLE));
185
+ }
186
+ }
187
+ }
188
+ });
189
+ });
190
+ return [_objectSpread(_objectSpread({}, element), {}, {
191
+ children: newChildren
192
+ })];
48
193
  };
49
194
  var updateDiffValue = function updateDiffValue(diff, element, oldElement) {
50
195
  if (!diff || !element || !oldElement) return;
51
- if (ObjectUtils.isSameObject(element, oldElement)) {
52
- diff.value.push(_objectSpread(_objectSpread({}, element), {}, {
53
- diff_type: DIFF_TYPE.COMMON
54
- }));
55
- return;
56
- }
57
- var currentElementType = element.type;
58
- var oldElementType = oldElement.type;
59
- if (currentElementType !== oldElementType) {
60
- diff.changes.push(oldElement.id);
61
- diff.value.push(_objectSpread(_objectSpread({}, oldElement), {}, {
62
- diff_type: DIFF_TYPE.DELETE
63
- }));
64
- diff.value.push(_objectSpread(_objectSpread({}, element), {}, {
65
- diff_type: DIFF_TYPE.ADD
66
- }));
196
+ if (ObjectUtils.isSameObject(element, oldElement, IGNORE_KEYS)) {
197
+ diff.value.push(element);
67
198
  return;
68
199
  }
69
- switch (currentElementType) {
70
- case ELEMENT_TYPE.PARAGRAPH:
71
- case ELEMENT_TYPE.HEADER1:
72
- case ELEMENT_TYPE.HEADER2:
73
- case ELEMENT_TYPE.HEADER3:
74
- case ELEMENT_TYPE.HEADER4:
75
- case ELEMENT_TYPE.HEADER5:
76
- case ELEMENT_TYPE.HEADER6:
77
- case ELEMENT_TYPE.BLOCKQUOTE:
78
- case ELEMENT_TYPE.LINK:
79
- case ELEMENT_TYPE.CHECK_LIST_ITEM:
80
- case ELEMENT_TYPE.CODE_BLOCK:
81
- case ELEMENT_TYPE.IMAGE:
82
- {
83
- diff.changes.push(oldElement.id);
84
- diff.value.push(_objectSpread(_objectSpread({}, oldElement), {}, {
85
- diff_type: DIFF_TYPE.DELETE
86
- }));
87
- diff.value.push(_objectSpread(_objectSpread({}, element), {}, {
88
- diff_type: DIFF_TYPE.ADD
89
- }));
90
- break;
91
- }
92
- case ELEMENT_TYPE.ORDERED_LIST:
93
- case ELEMENT_TYPE.UNORDERED_LIST:
94
- case ELEMENT_TYPE.LIST_ITEM:
95
- case ELEMENT_TYPE.LIST_LIC:
96
- {
97
- var _diff$changes;
98
- var _getElementDiffValue = getElementDiffValue(element.children, oldElement.children),
99
- diffValue = _getElementDiffValue.value,
100
- diffChanges = _getElementDiffValue.changes;
101
- (_diff$changes = diff.changes).push.apply(_diff$changes, _toConsumableArray(diffChanges));
102
- diff.value.push(_objectSpread(_objectSpread({}, element), {}, {
103
- children: diffValue,
104
- diff_type: DIFF_TYPE.MODIFY
105
- }));
106
- break;
107
- }
108
- default:
109
- {
110
- diff.changes.push(oldElement.id);
111
- diff.value.push(_objectSpread(_objectSpread({}, oldElement), {}, {
112
- diff_type: DIFF_TYPE.DELETE
113
- }));
114
- diff.value.push(_objectSpread(_objectSpread({}, element), {}, {
115
- diff_type: DIFF_TYPE.ADD
116
- }));
117
- break;
118
- }
119
- }
200
+ var elements = getCommonDiff(element, oldElement, diff);
201
+ elements.forEach(function (item) {
202
+ diff.value.push(item);
203
+ });
120
204
  };
121
205
 
122
206
  /**
@@ -131,37 +215,34 @@ var getElementDiffValue = function getElementDiffValue(currentContent, oldConten
131
215
  value: [],
132
216
  changes: []
133
217
  };
134
- var currentContentMap = {};
135
- var oldContentMap = {};
136
- currentContent.forEach(function (element) {
137
- currentContentMap[element.id] = element;
138
- });
139
- oldContent.forEach(function (element) {
140
- oldContentMap[element.id] = element;
141
- });
142
-
143
- // generator diff elements
144
- for (var i = 0; i < currentContent.length; i++) {
145
- var element = currentContent[i];
146
- var isAdded = !oldContentMap[element.id]; // added
147
- if (isAdded) {
148
- diff.changes.push(element.id);
149
- diff.value.push(_objectSpread(_objectSpread({}, element), {}, {
150
- diff_type: DIFF_TYPE.ADD
151
- }));
152
- continue;
153
- }
154
- updateDiffValue(diff, element, oldContentMap[element.id]);
155
- }
156
- oldContent.forEach(function (oldElement) {
157
- var oldElementId = oldElement.id;
158
- if (!currentContentMap[oldElementId]) {
159
- // deleted
160
- diff.changes.push(oldElementId);
161
- var elementIndex = getNearestElementIndex(diff, oldContent, oldElementId);
162
- diff.value.splice(elementIndex, 0, _objectSpread(_objectSpread({}, oldElement), {}, {
163
- diff_type: DIFF_TYPE.DELETE
164
- }));
218
+ var _generateIdMapAndIds3 = generateIdMapAndIds(currentContent),
219
+ currentContentMap = _generateIdMapAndIds3.map,
220
+ currentIds = _generateIdMapAndIds3.ids;
221
+ var _generateIdMapAndIds4 = generateIdMapAndIds(oldContent),
222
+ oldContentMap = _generateIdMapAndIds4.map,
223
+ oldIds = _generateIdMapAndIds4.ids;
224
+ var diffs = getIdDiffs(oldIds, currentIds);
225
+ diffs.forEach(function (diffItem) {
226
+ var elementIds = diffItem.value;
227
+ if (diffItem.removed) {
228
+ elementIds.forEach(function (elementId) {
229
+ diff.changes.push(elementId);
230
+ var element = oldContentMap[elementId];
231
+ var diffElement = generatorDiffElement(element, 'DELETE', DELETED_STYLE);
232
+ diff.value.push(diffElement);
233
+ });
234
+ } else if (diffItem.added) {
235
+ elementIds.forEach(function (elementId) {
236
+ diff.changes.push(elementId);
237
+ var element = currentContentMap[elementId];
238
+ var diffElement = generatorDiffElement(element, 'ADD', ADDED_STYLE);
239
+ diff.value.push(diffElement);
240
+ });
241
+ } else {
242
+ elementIds.forEach(function (elementId) {
243
+ var element = currentContentMap[elementId];
244
+ updateDiffValue(diff, element, oldContentMap[element.id]);
245
+ });
165
246
  }
166
247
  });
167
248
  return diff;
@@ -180,10 +261,16 @@ export var getDiff = function getDiff() {
180
261
  var oldValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
181
262
  children: []
182
263
  };
183
- var currentVersion = currentValue.version,
184
- currentContent = currentValue.children;
185
- var oldVersion = oldValue.version,
186
- oldContent = oldValue.children;
264
+ var _currentValue$childre = _objectSpread(_objectSpread({}, currentValue), {}, {
265
+ children: normalizeChildren(currentValue.children)
266
+ }),
267
+ currentVersion = _currentValue$childre.version,
268
+ currentContent = _currentValue$childre.children;
269
+ var _oldValue$children = _objectSpread(_objectSpread({}, oldValue), {}, {
270
+ children: normalizeChildren(oldValue.children)
271
+ }),
272
+ oldVersion = _oldValue$children.version,
273
+ oldContent = _oldValue$children.children;
187
274
  if (currentVersion === oldVersion) return {
188
275
  value: currentContent,
189
276
  changes: []
@@ -0,0 +1,18 @@
1
+ import ObjectUtils from './object-utils';
2
+ import { generateDefaultText } from '../../basic-sdk/extension/core/utils/index';
3
+ export var normalizeChildren = function normalizeChildren(children) {
4
+ // text
5
+ if (!Array.isArray(children)) return children;
6
+
7
+ // element
8
+ if (Array.isArray(children) && children.length === 0) return [generateDefaultText()];
9
+ return children.map(function (child) {
10
+ // child is text
11
+ if (ObjectUtils.hasProperty(child, 'text') && !ObjectUtils.hasProperty(child, 'children')) {
12
+ return child;
13
+ }
14
+ // child is element
15
+ child.children = normalizeChildren(child.children);
16
+ return child;
17
+ });
18
+ };
@@ -1,74 +1,56 @@
1
- import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
2
- import _createClass from "@babel/runtime/helpers/esm/createClass";
3
- import _inherits from "@babel/runtime/helpers/esm/inherits";
4
- import _createSuper from "@babel/runtime/helpers/esm/createSuper";
5
- import React from 'react';
1
+ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
2
+ import React, { useRef, useEffect, useState } from 'react';
6
3
  import { Editable, Slate } from '@seafile/slate-react';
7
- import editor, { renderLeaf as _renderLeaf, renderElement as _renderElement } from './extension';
4
+ import defaultEditor, { renderLeaf as _renderLeaf, renderElement as _renderElement } from './extension';
8
5
  import { withSocketIO } from './socket';
9
6
  import withNodeId from './node-id';
10
7
  import './assets/css/layout.css';
11
8
  import './assets/css/sdoc-editor-plugins.css';
12
9
  import { generateDefaultDocContent } from '../utils';
13
- var SDocViewer = /*#__PURE__*/function (_React$Component) {
14
- _inherits(SDocViewer, _React$Component);
15
- var _super = _createSuper(SDocViewer);
16
- function SDocViewer(props) {
17
- var _this;
18
- _classCallCheck(this, SDocViewer);
19
- _this = _super.call(this, props);
20
- var _ref = props.document || generateDefaultDocContent(),
21
- children = _ref.children;
22
- _this.state = {
23
- slateValue: children,
24
- isLoading: true
25
- };
26
- _this.socketManager = null;
27
- if (!props.isOpenSocket) {
28
- _this.editor = withNodeId(editor);
29
- } else {
30
- var document = props.document,
31
- config = props.config;
32
- _this.editor = withSocketIO(withNodeId(editor, {
33
- document: document,
34
- config: config
35
- }));
36
- }
37
- return _this;
38
- }
39
- _createClass(SDocViewer, [{
40
- key: "render",
41
- value: function render() {
42
- var _this2 = this;
43
- var slateValue = this.state.slateValue;
44
- return /*#__PURE__*/React.createElement("div", {
45
- className: "sdoc-editor-container"
46
- }, /*#__PURE__*/React.createElement("div", {
47
- className: "sdoc-editor-content"
48
- }, /*#__PURE__*/React.createElement("div", {
49
- className: "flex-fill o-auto"
50
- }, /*#__PURE__*/React.createElement(Slate, {
51
- editor: this.editor,
52
- value: slateValue,
53
- onChange: function onChange() {}
54
- }, /*#__PURE__*/React.createElement("div", {
55
- className: "article mx-auto"
56
- }, /*#__PURE__*/React.createElement(Editable, {
57
- readOnly: true,
58
- placeholder: "",
59
- renderElement: function renderElement(props) {
60
- return _renderElement(props, _this2.editor);
61
- },
62
- renderLeaf: function renderLeaf(props) {
63
- return _renderLeaf(props, _this2.editor);
64
- },
65
- onDOMBeforeInput: function onDOMBeforeInput(event) {}
66
- }))))));
67
- }
68
- }]);
69
- return SDocViewer;
70
- }(React.Component);
71
- SDocViewer.defaultProps = {
72
- isOpenSocket: false
10
+ var SDocViewer = function SDocViewer(_ref) {
11
+ var isOpenSocket = _ref.isOpenSocket,
12
+ document = _ref.document,
13
+ config = _ref.config,
14
+ customRenderLeaf = _ref.renderLeaf,
15
+ customRenderElement = _ref.renderElement;
16
+ var editor = !isOpenSocket ? withNodeId(defaultEditor) : withSocketIO(withNodeId(defaultEditor, {
17
+ document: document,
18
+ config: config
19
+ }));
20
+ var slateValue = (document || generateDefaultDocContent()).children;
21
+ var articleRef = useRef(null);
22
+ var _useState = useState(false),
23
+ _useState2 = _slicedToArray(_useState, 2),
24
+ setUpdate = _useState2[1];
25
+ useEffect(function () {
26
+ editor.width = articleRef.current.children[0].clientWidth;
27
+ setUpdate(true);
28
+ // eslint-disable-next-line react-hooks/exhaustive-deps
29
+ }, []);
30
+ return /*#__PURE__*/React.createElement("div", {
31
+ className: "sdoc-editor-container"
32
+ }, /*#__PURE__*/React.createElement("div", {
33
+ className: "sdoc-editor-content"
34
+ }, /*#__PURE__*/React.createElement("div", {
35
+ className: "flex-fill o-auto sdoc-editor-article-container",
36
+ id: "sdoc-editor-article-container"
37
+ }, /*#__PURE__*/React.createElement(Slate, {
38
+ editor: editor,
39
+ value: slateValue,
40
+ onChange: function onChange() {}
41
+ }, /*#__PURE__*/React.createElement("div", {
42
+ className: "article mx-auto",
43
+ ref: articleRef
44
+ }, /*#__PURE__*/React.createElement(Editable, {
45
+ readOnly: true,
46
+ placeholder: "",
47
+ renderElement: function renderElement(props) {
48
+ return (customRenderElement || _renderElement)(props, editor);
49
+ },
50
+ renderLeaf: function renderLeaf(props) {
51
+ return (customRenderLeaf || _renderLeaf)(props, editor);
52
+ },
53
+ onDOMBeforeInput: function onDOMBeforeInput(event) {}
54
+ }))))));
73
55
  };
74
56
  export default SDocViewer;
@@ -1,120 +1,49 @@
1
- import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2
- import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
3
- import _createClass from "@babel/runtime/helpers/esm/createClass";
4
- import _inherits from "@babel/runtime/helpers/esm/inherits";
5
- import _createSuper from "@babel/runtime/helpers/esm/createSuper";
6
- import React, { Component } from 'react';
7
- import { Editable, Slate } from '@seafile/slate-react';
8
- import editor, { renderLeaf, renderElement } from '../../basic-sdk/extension';
9
- import withNodeId from '../../basic-sdk/node-id';
1
+ import React, { useCallback, useEffect } from 'react';
2
+ import { renderLeaf, renderElement } from '../../basic-sdk/extension';
10
3
  import { getDiff } from '../../basic-sdk/utils/diff';
11
- import { DIFF_TYPE, ELEMENT_TYPE } from '../../basic-sdk/extension/constants';
12
4
  import context from '../../context';
5
+ import { ELEMENT_TYPE, ADDED_STYLE, DELETED_STYLE } from '../../basic-sdk/extension/constants';
6
+ import { SDocViewer } from '../../basic-sdk';
13
7
  import '../../assets/css/diff-viewer.css';
14
- var DiffViewer = /*#__PURE__*/function (_Component) {
15
- _inherits(DiffViewer, _Component);
16
- var _super = _createSuper(DiffViewer);
17
- function DiffViewer(_props) {
18
- var _this;
19
- _classCallCheck(this, DiffViewer);
20
- _this = _super.call(this, _props);
21
- _this.renderLeaf = function (props) {
22
- return renderLeaf(props, _this.editor);
23
- };
24
- _this.renderElement = function (props) {
25
- var element = props.element;
26
- var diff_type = element.diff_type,
27
- type = element.type;
28
- if (diff_type === DIFF_TYPE.ADD) {
29
- if (type === ELEMENT_TYPE.LIST_ITEM) {
30
- return renderElement(_objectSpread(_objectSpread({}, props), {}, {
31
- attributes: _objectSpread(_objectSpread({}, props.attributes), {}, {
32
- className: 'sdoc-diff-added'
33
- })
34
- }), _this.editor);
35
- }
36
- return /*#__PURE__*/React.createElement("div", {
37
- className: "sdoc-diff sdoc-diff-added"
38
- }, renderElement(_objectSpread(_objectSpread({}, props), {}, {
39
- attributes: _objectSpread(_objectSpread({}, props.attributes), {}, {
40
- className: 'sdoc-diff-added'
41
- })
42
- }), _this.editor));
43
- }
44
- if (diff_type === DIFF_TYPE.DELETE) {
45
- if (type === ELEMENT_TYPE.LIST_ITEM) {
46
- return renderElement(_objectSpread(_objectSpread({}, props), {}, {
47
- attributes: _objectSpread(_objectSpread({}, props.attributes), {}, {
48
- className: 'sdoc-diff-removed'
49
- })
50
- }), _this.editor);
51
- }
52
- return /*#__PURE__*/React.createElement("div", {
53
- className: "sdoc-diff sdoc-diff-removed"
54
- }, renderElement(_objectSpread(_objectSpread({}, props), {}, {
55
- attributes: _objectSpread(_objectSpread({}, props.attributes), {}, {
56
- className: 'sdoc-diff-removed'
57
- })
58
- }), _this.editor));
59
- }
60
- if (diff_type === DIFF_TYPE.MODIFY) {
61
- if (type === ELEMENT_TYPE.ORDERED_LIST || type === ELEMENT_TYPE.UNORDERED_LIST) {
62
- return renderElement(_objectSpread(_objectSpread({}, props), {}, {
63
- attributes: _objectSpread(_objectSpread({}, props.attributes), {}, {
64
- className: 'sdoc-diff-modify'
65
- })
66
- }), _this.editor);
67
- }
68
- return /*#__PURE__*/React.createElement("div", {
69
- className: "sdoc-diff-modify"
70
- }, renderElement(_objectSpread(_objectSpread({}, props), {}, {
71
- attributes: _objectSpread(_objectSpread({}, props.attributes), {}, {
72
- className: 'sdoc-diff-modify'
73
- })
74
- }), _this.editor));
75
- }
76
- return renderElement(props, _this.editor);
77
- };
78
- var currentContent = _props.currentContent,
79
- lastContent = _props.lastContent;
80
- _this.diff = currentContent ? getDiff(currentContent, lastContent) : {
81
- value: [],
82
- changes: []
83
- };
84
- _this.editor = withNodeId(editor);
85
- context.initSettings();
86
- return _this;
87
- }
88
- _createClass(DiffViewer, [{
89
- key: "componentDidMount",
90
- value: function componentDidMount() {
91
- this.props.didMountCallback && this.props.didMountCallback(this.diff);
92
- }
93
- }, {
94
- key: "render",
95
- value: function render() {
8
+ var DiffViewer = function DiffViewer(_ref) {
9
+ var currentContent = _ref.currentContent,
10
+ lastContent = _ref.lastContent,
11
+ didMountCallback = _ref.didMountCallback;
12
+ var diff = currentContent ? getDiff(currentContent, lastContent) : {
13
+ value: [],
14
+ changes: []
15
+ };
16
+ context.initSettings();
17
+ useEffect(function () {
18
+ didMountCallback && didMountCallback(diff);
19
+
20
+ // eslint-disable-next-line react-hooks/exhaustive-deps
21
+ }, []);
22
+ var customRenderLeaf = useCallback(function (props, editor) {
23
+ return renderLeaf(props, editor);
24
+ }, []);
25
+ var customRenderElement = useCallback(function (props, editor) {
26
+ var element = props.element;
27
+ var type = element.type;
28
+ if (type === ELEMENT_TYPE.IMAGE && (element.ADD || element.DELETE)) {
29
+ var style = element.ADD ? ADDED_STYLE : DELETED_STYLE;
96
30
  return /*#__PURE__*/React.createElement("div", {
97
- className: "sdoc-editor-container"
98
- }, /*#__PURE__*/React.createElement("div", {
99
- className: "sdoc-editor-content"
100
- }, /*#__PURE__*/React.createElement("div", {
101
- className: "flex-fill o-auto"
102
- }, /*#__PURE__*/React.createElement(Slate, {
103
- editor: this.editor,
104
- value: this.diff.value,
105
- onChange: function onChange() {}
106
- }, /*#__PURE__*/React.createElement("div", {
107
- className: "article mx-auto"
108
- }, /*#__PURE__*/React.createElement(Editable, {
109
- readOnly: true,
110
- placeholder: "",
111
- renderElement: this.renderElement,
112
- renderLeaf: this.renderLeaf,
113
- onDOMBeforeInput: function onDOMBeforeInput() {},
114
- onKeyDown: function onKeyDown() {}
115
- }))))));
31
+ className: "p-1",
32
+ style: {
33
+ backgroundColor: style.background_color,
34
+ width: 'fit-content',
35
+ height: 'fit-content'
36
+ }
37
+ }, renderElement(props, editor));
116
38
  }
117
- }]);
118
- return DiffViewer;
119
- }(Component);
39
+ return renderElement(props, editor);
40
+ }, []);
41
+ return /*#__PURE__*/React.createElement(SDocViewer, {
42
+ document: {
43
+ children: diff.value
44
+ },
45
+ renderLeaf: customRenderLeaf,
46
+ renderElement: customRenderElement
47
+ });
48
+ };
120
49
  export default DiffViewer;
@@ -1,10 +1,15 @@
1
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
1
2
  import React from 'react';
2
3
  import { SDocViewer } from '../../basic-sdk';
3
4
  import { generateDefaultDocContent } from '../../utils';
5
+ import { normalizeChildren } from '../../basic-sdk/utils/document-utils';
4
6
  function HistoryVersionViewer(props) {
5
7
  var document = props.document;
8
+ var validDocument = document || generateDefaultDocContent();
6
9
  return /*#__PURE__*/React.createElement(SDocViewer, {
7
- document: document || generateDefaultDocContent()
10
+ document: _objectSpread(_objectSpread({}, validDocument), {}, {
11
+ children: normalizeChildren(validDocument.children)
12
+ })
8
13
  });
9
14
  }
10
15
  export default HistoryVersionViewer;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seafile/sdoc-editor",
3
- "version": "0.1.60",
3
+ "version": "0.1.61",
4
4
  "private": false,
5
5
  "description": "This is a sdoc editor",
6
6
  "main": "dist/index.js",
@@ -116,7 +116,7 @@
116
116
  "resolve": "1.12.0",
117
117
  "resolve-url-loader": "3.1.5",
118
118
  "sass-loader": "7.2.0",
119
- "seafile-js": "0.2.190",
119
+ "seafile-js": "0.2.202",
120
120
  "semver": "6.3.0",
121
121
  "style-loader": "^1.0.1",
122
122
  "terser-webpack-plugin": "4.2.3",