@seafile/sdoc-editor 0.5.40 → 0.5.41

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,18 +1,48 @@
1
1
  import { ELEMENT_TYPE } from '../../constants';
2
+ import { TABLE_CELL_MIN_WIDTH, TABLE_ROW_MIN_HEIGHT } from './constants';
2
3
  class Table {
3
4
  constructor(options) {
4
5
  this.type = options.type || ELEMENT_TYPE.TABLE;
5
6
  this.children = options.children || [
6
7
  // 1 x 1
7
8
  {
9
+ id: '',
8
10
  type: ELEMENT_TYPE.TABLE_ROW,
9
11
  children: [{
12
+ id: '',
10
13
  type: ELEMENT_TYPE.TABLE_CELL,
11
14
  children: [{
12
- text: ''
13
- }]
14
- }]
15
+ text: '',
16
+ id: ''
17
+ }],
18
+ style: {
19
+ text_align: 'left',
20
+ alignItems: 'center',
21
+ background_color: ''
22
+ },
23
+ // inherit from table,when insert new cell
24
+ inherit_style: {
25
+ text_align: 'left',
26
+ background_color: ''
27
+ }
28
+ }],
29
+ style: {
30
+ 'min_height': 42
31
+ }
15
32
  }];
33
+ this.columns = options.columns || [{
34
+ width: TABLE_CELL_MIN_WIDTH
35
+ }];
36
+ this.ui = options.ui || {
37
+ alternate_highlight: true,
38
+ // alternate row highlight
39
+ alternate_highlight_color: ''
40
+ };
41
+ this.style = options.style || {
42
+ gridTemplateColumns: "repeat(1, ".concat(TABLE_CELL_MIN_WIDTH, "}px)"),
43
+ // grid
44
+ gridAutoRows: "minmax(".concat(TABLE_ROW_MIN_HEIGHT, "}px, auto)")
45
+ };
16
46
  }
17
47
  }
18
48
  export default Table;
@@ -3,8 +3,8 @@ import isHotkey from 'is-hotkey';
3
3
  import { Editor, Transforms, Path, Element } from '@seafile/slate';
4
4
  import { ReactEditor } from '@seafile/slate-react';
5
5
  import { getNodeType, getParentNode, getSelectedNodeByType, isLastNode, generateEmptyElement, focusEditor } from '../../core';
6
- import { ELEMENT_TYPE, KEYBOARD, PARAGRAPH, CLIPBOARD_FORMAT_KEY, CHECK_LIST_ITEM, ORDERED_LIST, UNORDERED_LIST } from '../../constants';
7
- import { TABLE_MAX_ROWS, EMPTY_SELECTED_RANGE, TABLE_ELEMENT, TABLE_ELEMENT_POSITION } from './constants';
6
+ import { ELEMENT_TYPE, KEYBOARD, PARAGRAPH, CLIPBOARD_FORMAT_KEY, CHECK_LIST_ITEM, ORDERED_LIST, UNORDERED_LIST, TABLE_ROW, TABLE, TABLE_CELL } from '../../constants';
7
+ import { TABLE_MAX_ROWS, EMPTY_SELECTED_RANGE, TABLE_ELEMENT, TABLE_ELEMENT_POSITION, TABLE_CELL_MIN_WIDTH, TABLE_ROW_MIN_HEIGHT } from './constants';
8
8
  import ObjectUtils from '../../../utils/object-utils';
9
9
  import { getSelectedInfo, insertTableElement, removeTable, insertMultipleRowsAndColumns, setTableFragmentData, deleteTableRangeData, focusCell, deleteHandler, isTableLocation, isLastTableCell } from './helpers';
10
10
  import EventBus from '../../../utils/event-bus';
@@ -302,12 +302,108 @@ const withTable = editor => {
302
302
 
303
303
  // Rewrite normalizeNode
304
304
  newEditor.normalizeNode = _ref => {
305
+ var _node$children$, _node$children$$child, _node$children$2, _node$children$2$chil;
305
306
  let [node, path] = _ref;
306
307
  const type = getNodeType(node);
308
+ if (node.type === TABLE_ROW) {
309
+ const parentEntry = Editor.parent(editor, path);
310
+ if ((parentEntry === null || parentEntry === void 0 ? void 0 : parentEntry[0].type) !== TABLE) {
311
+ Transforms.unwrapNodes(editor, {
312
+ at: path
313
+ });
314
+ return;
315
+ }
316
+ }
317
+ if (node.type === TABLE_CELL) {
318
+ const parentEntry = Editor.parent(editor, path);
319
+ if ((parentEntry === null || parentEntry === void 0 ? void 0 : parentEntry[0].type) !== TABLE_ROW) {
320
+ Transforms.unwrapNodes(editor, {
321
+ at: path
322
+ });
323
+ return;
324
+ }
325
+ }
307
326
  if (type !== ELEMENT_TYPE.TABLE) {
308
327
  return normalizeNode([node, path]);
309
328
  }
310
329
 
330
+ // Check field integrity
331
+ // Check table - columns
332
+ const isMissColumn = !node.columns;
333
+ if (isMissColumn) {
334
+ if (!node.columns) {
335
+ const columnCount = node.children[0].children.length;
336
+ const width = Math.max(TABLE_CELL_MIN_WIDTH, parseInt(editor.width / columnCount));
337
+ const columns = Array(node.children[0].children.length).fill({
338
+ width
339
+ });
340
+ Transforms.setNodes(newEditor, {
341
+ columns
342
+ }, {
343
+ at: path
344
+ });
345
+ }
346
+ }
347
+
348
+ // Check table - style
349
+ if (!node.style) {
350
+ const columnCount = node.children[0].children.length;
351
+ const columnWidth = Math.max(TABLE_CELL_MIN_WIDTH, parseInt(editor.width / columnCount));
352
+ Transforms.setNodes(newEditor, {
353
+ style: {
354
+ gridTemplateColumns: "repeat(".concat(columnCount, ", ").concat(columnWidth, "px)"),
355
+ gridAutoRows: "minmax(".concat(TABLE_ROW_MIN_HEIGHT, "}px, auto)")
356
+ }
357
+ }, {
358
+ at: path
359
+ });
360
+ }
361
+
362
+ // Check table - ui
363
+ if (!node.ui) {
364
+ Transforms.setNodes(newEditor, {
365
+ ui: {
366
+ alternate_highlight: false
367
+ }
368
+ }, {
369
+ at: path
370
+ });
371
+ }
372
+
373
+ // Check row - style
374
+ if (!node.children[0].style) {
375
+ const style = {
376
+ min_height: TABLE_ROW_MIN_HEIGHT
377
+ };
378
+ node.children.forEach((row, index) => {
379
+ if (!row.style) {
380
+ Transforms.setNodes(newEditor, {
381
+ style
382
+ }, {
383
+ at: path.concat(index)
384
+ });
385
+ }
386
+ });
387
+ }
388
+
389
+ // Check cell - style & inherit_style
390
+ if ((node === null || node === void 0 ? void 0 : (_node$children$ = node.children[0]) === null || _node$children$ === void 0 ? void 0 : (_node$children$$child = _node$children$.children[0]) === null || _node$children$$child === void 0 ? void 0 : _node$children$$child.style) || (node === null || node === void 0 ? void 0 : (_node$children$2 = node.children[0]) === null || _node$children$2 === void 0 ? void 0 : (_node$children$2$chil = _node$children$2.children[0]) === null || _node$children$2$chil === void 0 ? void 0 : _node$children$2$chil.inherit_style)) {
391
+ node.children.forEach((row, rowIndex) => {
392
+ row.children.forEach((cell, cellIndex) => {
393
+ if (!cell.style || !cell.inherit_style) {
394
+ const style = cell.style || {};
395
+ const inherit_style = cell.inherit_style || {};
396
+ Transforms.setNodes(newEditor, {
397
+ style,
398
+ inherit_style
399
+ }, {
400
+ at: path.concat(rowIndex, cellIndex)
401
+ });
402
+ }
403
+ });
404
+ });
405
+ }
406
+
311
407
  // insert empty node,continue editor
312
408
  const isLast = isLastNode(newEditor, node);
313
409
  if (isLast) {
@@ -1,5 +1,7 @@
1
1
  import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
2
2
  import ObjectUtils from './object-utils';
3
+ const extendedWordChars = 'a-zA-Z\\u{C0}-\\u{FF}\\u{D8}-\\u{F6}\\u{F8}-\\u{2C6}\\u{2C8}-\\u{2D7}\\u{2DE}-\\u{2FF}\\u{1E00}-\\u{1EFF}\\u{4e00}-\\u{9fa5}';
4
+ const tokenizeIncludingWhitespace = new RegExp("[".concat(extendedWordChars, "]+|\\s+|[^").concat(extendedWordChars, "]"), 'ug');
3
5
  const buildValues = (diff, components, newString, oldString, valueType, useLongestToken) => {
4
6
  let componentPos = 0;
5
7
  let componentLen = components.length;
@@ -158,11 +160,11 @@ class DiffText {
158
160
  return oldPos;
159
161
  });
160
162
  _defineProperty(this, "equals", (left, right) => {
161
- if (this.options.comparator) {
162
- return this.options.comparator(left, right);
163
- } else {
164
- return left === right || this.options.ignoreCase && left.toLowerCase() === right.toLowerCase();
163
+ if (this.options.ignoreCase) {
164
+ left = left.toLowerCase();
165
+ right = right.toLowerCase();
165
166
  }
167
+ return left.trim() === right.trim();
166
168
  });
167
169
  _defineProperty(this, "removeEmpty", (array, type) => {
168
170
  if (type === 'Array') return array;
@@ -175,12 +177,46 @@ class DiffText {
175
177
  return ret;
176
178
  });
177
179
  _defineProperty(this, "tokenize", (value, valueType) => {
178
- if (valueType === 'Array') return value.slice();
179
- return value.split('');
180
+ if (valueType === 'Array') {
181
+ return value.slice();
182
+ }
183
+ let parts = value.match(tokenizeIncludingWhitespace) || [];
184
+ const tokens = [];
185
+ let prevPart = null;
186
+ parts.forEach(part => {
187
+ if (/\s/.test(part)) {
188
+ if (prevPart == null) {
189
+ tokens.push(part);
190
+ } else {
191
+ tokens.push(tokens.pop() + part);
192
+ }
193
+ } else if (/\s/.test(prevPart)) {
194
+ if (tokens[tokens.length - 1] === prevPart) {
195
+ tokens.push(tokens.pop() + part);
196
+ } else {
197
+ tokens.push(prevPart + part);
198
+ }
199
+ } else {
200
+ tokens.push(part);
201
+ }
202
+ prevPart = part;
203
+ });
204
+ return tokens;
180
205
  });
181
206
  _defineProperty(this, "join", (value, valueType) => {
182
207
  if (valueType === 'Array') return value;
183
- return value.join('');
208
+ // Tokens being joined here will always have appeared consecutively in the
209
+ // same text, so we can simply strip off the leading whitespace from all the
210
+ // tokens except the first (and except any whitespace-only tokens - but such
211
+ // a token will always be the first and only token anyway) and then join them
212
+ // and the whitespace around words and punctuation will end up correct.
213
+ return value.map((token, i) => {
214
+ if (i === 0) {
215
+ return token;
216
+ } else {
217
+ return token.replace(/^\s+/, '');
218
+ }
219
+ }).join('');
184
220
  });
185
221
  _defineProperty(this, "getDiffs", () => {
186
222
  if (!this.canCompare) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seafile/sdoc-editor",
3
- "version": "0.5.40",
3
+ "version": "0.5.41",
4
4
  "private": false,
5
5
  "description": "This is a sdoc editor",
6
6
  "main": "dist/index.js",