@seafile/sdoc-editor 0.2.8 → 0.2.9

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,7 +1,9 @@
1
+ import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
2
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
1
3
  import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
2
4
  import { Editor, Transforms, Element } from '@seafile/slate';
3
5
  import slugid from 'slugid';
4
- import { BLOCKQUOTE, CHECK_LIST_ITEM, IMAGE, ORDERED_LIST, PARAGRAPH, UNORDERED_LIST } from '../../constants';
6
+ import { BLOCKQUOTE, CHECK_LIST_ITEM, IMAGE, ORDERED_LIST, PARAGRAPH, UNORDERED_LIST, CODE_BLOCK, TABLE } from '../../constants';
5
7
  import { focusEditor, getNodeType } from '../../core';
6
8
  export var isMenuDisabled = function isMenuDisabled(editor, readonly) {
7
9
  if (readonly) return true;
@@ -68,4 +70,48 @@ export var setBlockQuoteType = function setBlockQuoteType(editor, active) {
68
70
  });
69
71
  }
70
72
  focusEditor(editor);
73
+ };
74
+ export var getFormattedElements = function getFormattedElements(data) {
75
+ var elements = [];
76
+ var arr = [];
77
+ data.forEach(function (item) {
78
+ if ([CODE_BLOCK, TABLE, BLOCKQUOTE].includes(item === null || item === void 0 ? void 0 : item.type)) {
79
+ // Insert quote block
80
+ if (arr.length !== 0) {
81
+ var blockquoteNode = {
82
+ id: slugid.nice(),
83
+ type: BLOCKQUOTE
84
+ };
85
+ blockquoteNode['children'] = arr;
86
+ elements.push(blockquoteNode);
87
+ arr = [];
88
+ }
89
+ // Merge quote block
90
+ var preElement = elements[elements.length - 1];
91
+ if ((preElement === null || preElement === void 0 ? void 0 : preElement.type) === BLOCKQUOTE && (item === null || item === void 0 ? void 0 : item.type) === BLOCKQUOTE) {
92
+ elements[elements.length - 1] = _objectSpread(_objectSpread({}, preElement), {}, {
93
+ children: [].concat(_toConsumableArray(preElement.children), _toConsumableArray(item.children))
94
+ });
95
+ } else {
96
+ elements.push(item);
97
+ }
98
+ } else {
99
+ arr.push(item);
100
+ }
101
+ });
102
+ return elements;
103
+ };
104
+ export var getFormattedRestElements = function getFormattedRestElements(data) {
105
+ var restElements = data.slice(0);
106
+ // Contains quote block type elements
107
+ if (data.find(function (item) {
108
+ return (item === null || item === void 0 ? void 0 : item.type) === BLOCKQUOTE;
109
+ })) {
110
+ data.forEach(function (item, index) {
111
+ if ((item === null || item === void 0 ? void 0 : item.type) === BLOCKQUOTE) {
112
+ restElements.splice.apply(restElements, [index, 1].concat(_toConsumableArray(item.children)));
113
+ }
114
+ });
115
+ }
116
+ return restElements;
71
117
  };
@@ -2,12 +2,13 @@ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
2
2
  import { Editor, Element, Point, Transforms, Node } from '@seafile/slate';
3
3
  import { ReactEditor } from '@seafile/slate-react';
4
4
  import { generateEmptyElement, getSelectedNodeByType, isSelectionAtBlockStart } from '../../core';
5
- import { BLOCKQUOTE, PARAGRAPH } from '../../constants';
6
- import { setBlockQuoteType } from './helpers';
5
+ import { BLOCKQUOTE, PARAGRAPH, CODE_BLOCK, TABLE } from '../../constants';
6
+ import { setBlockQuoteType, getFormattedElements, getFormattedRestElements } from './helpers';
7
7
  var withBlockquote = function withBlockquote(editor) {
8
8
  var insertBreak = editor.insertBreak,
9
9
  insertText = editor.insertText,
10
- deleteBackward = editor.deleteBackward;
10
+ deleteBackward = editor.deleteBackward,
11
+ insertFragment = editor.insertFragment;
11
12
  var newEditor = editor;
12
13
  newEditor.insertBreak = function () {
13
14
  var selection = editor.selection;
@@ -58,6 +59,39 @@ var withBlockquote = function withBlockquote(editor) {
58
59
  }
59
60
  deleteBackward(unit);
60
61
  };
62
+ newEditor.insertFragment = function (data) {
63
+ // Paste into quote block
64
+ if (getSelectedNodeByType(newEditor, BLOCKQUOTE)) {
65
+ var lastIndex = data.findLastIndex(function (item) {
66
+ return [CODE_BLOCK, TABLE].includes(item === null || item === void 0 ? void 0 : item.type);
67
+ });
68
+ var elements = getFormattedElements(data.slice(0, lastIndex + 1));
69
+ var restElements = getFormattedRestElements(data.slice(lastIndex + 1));
70
+ var path = Editor.path(newEditor, newEditor.selection);
71
+
72
+ // Insert elements of quote block
73
+ if (restElements.length !== 0) {
74
+ // Insert text when inserting a single line paragraph
75
+ if (restElements.length === 1 && restElements[0].type === PARAGRAPH) {
76
+ var string = Node.string(restElements[0]);
77
+ Editor.insertText(newEditor, string);
78
+ } else {
79
+ Transforms.insertNodes(newEditor, restElements, {
80
+ at: [path[0], path[1]]
81
+ });
82
+ }
83
+ }
84
+
85
+ // Insert elements above the quoted block
86
+ if (elements.length !== 0) {
87
+ Transforms.insertNodes(newEditor, elements, {
88
+ at: [path[0]]
89
+ });
90
+ }
91
+ return;
92
+ }
93
+ return insertFragment(data);
94
+ };
61
95
  return newEditor;
62
96
  };
63
97
  export default withBlockquote;
@@ -137,7 +137,7 @@ export var changeToPlainText = function changeToPlainText(editor) {
137
137
  mode: 'highest'
138
138
  });
139
139
  };
140
- export var setClipboardData = function setClipboardData(value) {
140
+ export var setClipboardCodeBlockData = function setClipboardCodeBlockData(value) {
141
141
  // Insert text into the clipboard for use on other pages
142
142
  var text = value.children.map(function (line) {
143
143
  return Node.string(line);
@@ -1,20 +1,19 @@
1
- import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2
1
  import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
3
2
  import _toArray from "@babel/runtime/helpers/esm/toArray";
4
3
  import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
4
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
5
5
  import slugid from 'slugid';
6
6
  import isHotkey from 'is-hotkey';
7
7
  import { Transforms, Node, Range, Editor } from '@seafile/slate';
8
8
  import { getNodeType, isLastNode, getSelectedNodeByType, generateEmptyElement } from '../../core';
9
9
  import { deleteBackwardByLength } from './helpers';
10
- import { CODE_BLOCK, PARAGRAPH, CODE_LINE } from '../../constants';
10
+ import { CODE_BLOCK, PARAGRAPH, CODE_LINE, BLOCKQUOTE } from '../../constants';
11
11
  var withCodeBlock = function withCodeBlock(editor) {
12
12
  var normalizeNode = editor.normalizeNode,
13
13
  insertFragment = editor.insertFragment,
14
14
  insertText = editor.insertText,
15
15
  insertBreak = editor.insertBreak,
16
- insertData = editor.insertData,
17
- insertNode = editor.insertNode;
16
+ insertData = editor.insertData;
18
17
  var newEditor = editor;
19
18
 
20
19
  // If you enter two Spaces in quick succession, a period and a space appear (Default Settings for mac)
@@ -25,14 +24,21 @@ var withCodeBlock = function withCodeBlock(editor) {
25
24
  return insertText(data);
26
25
  };
27
26
  newEditor.insertData = function (data) {
27
+ // Paste a single code block element somewhere other than a code block
28
28
  if (data.types.includes('text/code-block') && !getSelectedNodeByType(editor, CODE_BLOCK)) {
29
+ var options = {};
30
+ // Paste into quote block
31
+ if (getSelectedNodeByType(newEditor, BLOCKQUOTE)) {
32
+ var path = Editor.path(newEditor, newEditor.selection);
33
+ options['at'] = [path[0]];
34
+ }
29
35
  var codeBlockNode = JSON.parse(data.getData('text/code-block'));
30
- return insertNode(codeBlockNode);
36
+ return Transforms.insertNodes(newEditor, codeBlockNode, _objectSpread({}, options));
31
37
  }
32
38
  insertData(data);
33
39
  };
34
40
  newEditor.insertFragment = function (data) {
35
- // only selected code block content
41
+ // Only selected code block content
36
42
  if (data.length === 1 && data[0].type === CODE_BLOCK && !getSelectedNodeByType(editor, CODE_BLOCK)) {
37
43
  data.forEach(function (node, index) {
38
44
  if (node.type === CODE_BLOCK) {
@@ -47,9 +53,8 @@ var withCodeBlock = function withCodeBlock(editor) {
47
53
  });
48
54
  return insertFragment(data);
49
55
  } else {
56
+ // Paste into code block
50
57
  if (getSelectedNodeByType(editor, CODE_BLOCK)) {
51
- // Paste into code block
52
-
53
58
  // Pasted data is code block split with code-line
54
59
  data.forEach(function (node, index) {
55
60
  if (node.type === CODE_BLOCK) {
@@ -7,7 +7,7 @@ import EventBus from '../../../utils/event-bus';
7
7
  import { focusEditor } from '../../core';
8
8
  import { useScrollContext } from '../../../hooks/use-scroll-context';
9
9
  import CodeBlockHoverMenu from './hover-menu';
10
- import { setClipboardData } from './helpers';
10
+ import { setClipboardCodeBlockData } from './helpers';
11
11
  import { INTERNAL_EVENT } from '../../../constants';
12
12
  import '../../../assets/css/code-block.css';
13
13
  var CodeBlock = function CodeBlock(_ref) {
@@ -57,7 +57,7 @@ var CodeBlock = function CodeBlock(_ref) {
57
57
  // eslint-disable-next-line react-hooks/exhaustive-deps
58
58
  }, []);
59
59
  var onCopyCodeBlock = useCallback(function () {
60
- setClipboardData(element);
60
+ setClipboardCodeBlockData(element);
61
61
  // eslint-disable-next-line react-hooks/exhaustive-deps
62
62
  }, [element]);
63
63
  var onDeleteCodeBlock = useCallback(function () {
@@ -4,6 +4,7 @@ import { ReactEditor } from '@seafile/slate-react';
4
4
  import copy from 'copy-to-clipboard';
5
5
  import { toggleList } from '../../plugins/list/transforms';
6
6
  import { generateEmptyElement } from '../../core';
7
+ import { setClipboardCodeBlockData } from '../../plugins/code-block/helpers';
7
8
  import { ORDERED_LIST, UNORDERED_LIST, PARAGRAPH, CHECK_LIST_ITEM, IMAGE, TABLE, CODE_BLOCK, BLOCKQUOTE, LIST_ITEM_CORRELATION_TYPE, ADD_POSITION_OFFSET_TYPE, INSERT_POSITION, ELEMENT_TYPE } from '../../constants';
8
9
  import { EMPTY_SELECTED_RANGE } from '../../plugins/table/constants';
9
10
  export var onSetNodeType = function onSetNodeType(editor, element, type) {
@@ -42,6 +43,10 @@ export var setSelection = function setSelection(editor, element) {
42
43
  }
43
44
  };
44
45
  export var onCopyNode = function onCopyNode(editor, element) {
46
+ if (element.type === ELEMENT_TYPE.CODE_BLOCK) {
47
+ setClipboardCodeBlockData(element);
48
+ return;
49
+ }
45
50
  if (element.type === ELEMENT_TYPE.TABLE) {
46
51
  var tableSize = [element.children.length, element.children[0].children.length];
47
52
  var tableSelectedRange = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seafile/sdoc-editor",
3
- "version": "0.2.8",
3
+ "version": "0.2.9",
4
4
  "private": false,
5
5
  "description": "This is a sdoc editor",
6
6
  "main": "dist/index.js",