@seafile/sdoc-editor 0.1.14 → 0.1.15

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.
@@ -82,7 +82,6 @@ var SDocEditor = /*#__PURE__*/function (_React$Component) {
82
82
  renderLeaf: function renderLeaf(props) {
83
83
  return _renderLeaf(props, _this2.editor);
84
84
  },
85
- onDOMBeforeInput: function onDOMBeforeInput(event) {},
86
85
  onKeyDown: this.eventProxy.onKeyDown
87
86
  })))));
88
87
  }
@@ -35,6 +35,12 @@ export var getNode = function getNode(editor, path) {
35
35
  export var getNodeType = function getNodeType(node) {
36
36
  return Element.isElement(node) ? node.type : '';
37
37
  };
38
+ export var getNodes = function getNodes(node, options) {
39
+ return Node.nodes(node, options);
40
+ };
41
+ export var getCommonNode = function getCommonNode(root, path, ancestor) {
42
+ return Node.common(root, path, ancestor);
43
+ };
38
44
  export var getSelectedNodeByType = function getSelectedNodeByType(editor, type) {
39
45
  var match = function match(n) {
40
46
  return getNodeType(n) === type;
@@ -8,18 +8,18 @@ export var isMenuDisabled = function isMenuDisabled(editor) {
8
8
  match: function match(n) {
9
9
  var type = getNodeType(n);
10
10
 
11
- // 只可用于 p blockquote
11
+ // Only available for p and blockquote
12
12
  if (type === 'paragraph') return true;
13
13
  if (type === 'blockquote') return true;
14
14
  return false;
15
15
  },
16
16
  universal: true,
17
- mode: 'highest' // 匹配最高层级
17
+ mode: 'highest' // Match top level
18
18
  }),
19
19
  _Editor$nodes2 = _slicedToArray(_Editor$nodes, 1),
20
20
  nodeEntry = _Editor$nodes2[0];
21
21
 
22
- // 匹配到 p blockquote ,不禁用
22
+ // Match to p blockquote, do not disable
23
23
  if (nodeEntry) {
24
24
  return false;
25
25
  }
@@ -23,13 +23,13 @@ var withBlockquote = function withBlockquote(editor) {
23
23
  var quotePath = ReactEditor.findPath(editor, quoteElem);
24
24
  var quoteEndLocation = Editor.end(editor, quotePath);
25
25
  if (Point.equals(quoteEndLocation, selection.focus)) {
26
- // 光标位于 blockquote 最后
26
+ // Cursor is at the end of blockquote
27
27
  var str = Node.string(quoteElem);
28
- // blockquote 文本最后一个是 \n;
28
+ // The last of the blockquote text is \n;
29
29
  if (str && str.slice(-1) === '\n') {
30
- // 第一步:删除最后一个 \n
30
+ // Step 1: Remove the last \n
31
31
  editor.deleteBackward('character');
32
- // 第二步:插入一个 paragraph
32
+ // Step 2: Insert a paragraph
33
33
  var p = generateEmptyElement(PARAGRAPH);
34
34
  Transforms.insertNodes(newEditor, p, {
35
35
  mode: 'highest'
@@ -38,7 +38,7 @@ var withBlockquote = function withBlockquote(editor) {
38
38
  }
39
39
  }
40
40
 
41
- // 其他情况,插入换行符
41
+ // In other cases, insert a newline
42
42
  insertText('\n');
43
43
  };
44
44
  return newEditor;
@@ -1,5 +1,5 @@
1
1
  import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
2
- import { Editor, Transforms, Node } from '@seafile/slate';
2
+ import { Transforms, Node } from '@seafile/slate';
3
3
  import { getSelectedNodeByType, getNodeType, isLastNode, genEmptyParagraph } from '../../core';
4
4
  import { CODE_BLOCK } from '../../constants';
5
5
  var getLastTextLineBeforeSelection = function getLastTextLineBeforeSelection(codeNode, editor) {
@@ -15,8 +15,7 @@ var getLastTextLineBeforeSelection = function getLastTextLineBeforeSelection(cod
15
15
  };
16
16
  var withCodeBlock = function withCodeBlock(editor) {
17
17
  var insertBreak = editor.insertBreak,
18
- normalizeNode = editor.normalizeNode,
19
- insertData = editor.insertData;
18
+ normalizeNode = editor.normalizeNode;
20
19
  var newEditor = editor;
21
20
 
22
21
  // Rewrite insertBreak
@@ -71,19 +70,6 @@ var withCodeBlock = function withCodeBlock(editor) {
71
70
  // Perform default behavior
72
71
  return normalizeNode([node, path]);
73
72
  };
74
-
75
- // Rewrite insertData - Paste text
76
- newEditor.insertData = function (data) {
77
- var codeNode = getSelectedNodeByType(newEditor, CODE_BLOCK);
78
- if (codeNode == null) {
79
- insertData(data); // Perform default insertData
80
- return;
81
- }
82
-
83
- // Gets the text and inserts it into the code block
84
- var text = data.getData('text/plain');
85
- Editor.insertText(newEditor, text);
86
- };
87
73
  return newEditor;
88
74
  };
89
75
  export default withCodeBlock;
@@ -1,8 +1,9 @@
1
1
  import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
2
- import { Editor, Transforms, Node } from '@seafile/slate';
2
+ import { Transforms, Node } from '@seafile/slate';
3
3
  import isUrl from 'is-url';
4
- import { isMenuDisabled, insertLink } from './helpers';
4
+ import { genLinkNode } from './helpers';
5
5
  import { getNodeType } from '../../core';
6
+ import { isImage } from '../../utils';
6
7
  var withLink = function withLink(editor) {
7
8
  var normalizeNode = editor.normalizeNode,
8
9
  isInline = editor.isInline,
@@ -17,22 +18,22 @@ var withLink = function withLink(editor) {
17
18
  }
18
19
  return isInline(elem);
19
20
  };
20
-
21
- // Rewrite insertData, Paste insert link
22
21
  newEditor.insertData = function (data) {
22
+ // Paste content from sdoc document
23
+ if (newEditor.insertFragmentData(data)) return;
24
+
25
+ // Paste content from other document
23
26
  var text = data.getData('text/plain');
24
- if (!isUrl(text)) {
25
- // unlinked
27
+ if (!isUrl(text) && !isImage(text)) {
28
+ // not a link content
26
29
  insertData(data);
27
30
  return;
28
31
  }
29
-
30
- // Insert link
31
- if (isMenuDisabled(newEditor)) return; // disabled
32
- var selection = newEditor.selection;
33
- if (selection == null) return;
34
- var selectedText = Editor.string(newEditor, selection); // Gets the selected text
35
- insertLink(newEditor, selectedText, text);
32
+ var link = genLinkNode(text, text);
33
+ Transforms.insertNodes(newEditor, [link, {
34
+ text: ' '
35
+ }]);
36
+ return;
36
37
  };
37
38
 
38
39
  // Rewrite normalizeNode
@@ -1,6 +1,7 @@
1
1
  import { insertBreakList } from './insert-break-list';
2
2
  import { onTabHandle } from './on-tab-handle';
3
3
  import { normalizeList } from './normalize-list';
4
+ import { insertFragmentList } from './insert-fragment-list';
4
5
  var withList = function withList(editor) {
5
6
  var insertBreak = editor.insertBreak,
6
7
  deleteBackWord = editor.deleteBackWord,
@@ -28,6 +29,7 @@ var withList = function withList(editor) {
28
29
  if (onTabHandle(newEditor, event)) return;
29
30
  handleTab && handleTab();
30
31
  };
32
+ newEditor.insertFragment = insertFragmentList(newEditor);
31
33
  newEditor.normalizeNode = normalizeList(editor);
32
34
  return newEditor;
33
35
  };
@@ -0,0 +1,191 @@
1
+ import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
2
+ import _toArray from "@babel/runtime/helpers/esm/toArray";
3
+ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
4
+ import { Element, Path, Transforms, Node, Editor } from '@seafile/slate';
5
+ import { LIST_ITEM, LIST_LIC } from '../../../constants';
6
+ import { findNode, getCommonNode, getNode, getNodes } from '../../../core';
7
+ import { getListTypes } from '../queries';
8
+ var isListRoot = function isListRoot(node) {
9
+ return Element.isElement(node) && getListTypes().includes(node.type);
10
+ };
11
+ var getFirstAncestorOfType = function getFirstAncestorOfType(root, entry, _ref) {
12
+ var type = _ref.type;
13
+ var ancestor = Path.parent(entry[1]);
14
+ while (getNode(root, ancestor).type !== type) {
15
+ ancestor = Path.parent(ancestor);
16
+ }
17
+ return [getNode(root, ancestor), ancestor];
18
+ };
19
+ var findListItemsWithContent = function findListItemsWithContent(first) {
20
+ var prev = null;
21
+ var node = first;
22
+ while (isListRoot(node) || node.type === LIST_ITEM && node.children[0].type !== LIST_LIC) {
23
+ prev = node;
24
+ var _node$children = _slicedToArray(node.children, 1);
25
+ node = _node$children[0];
26
+ }
27
+ return prev ? prev.children : [node];
28
+ };
29
+ var trimList = function trimList(listRoot) {
30
+ if (!isListRoot(listRoot)) {
31
+ return [listRoot];
32
+ }
33
+ var _texts = Node.texts(listRoot);
34
+ var textEntries = Array.from(_texts);
35
+ var commonAncestorEntry = textEntries.reduce(function (commonAncestor, textEntry) {
36
+ return Path.isAncestor(commonAncestor[1], textEntry[1]) ? commonAncestor : getCommonNode(listRoot, textEntry[1], commonAncestor[1]);
37
+ }, getFirstAncestorOfType(listRoot, textEntries[0], {
38
+ type: LIST_ITEM
39
+ }));
40
+
41
+ // is ul/ol: return children
42
+ // is not ul/ol
43
+ var _ref2 = isListRoot(commonAncestorEntry[0]) ? commonAncestorEntry[0].children : [commonAncestorEntry[0]],
44
+ _ref3 = _toArray(_ref2),
45
+ first = _ref3[0],
46
+ rest = _ref3.slice(1);
47
+ return [].concat(_toConsumableArray(findListItemsWithContent(first)), _toConsumableArray(rest));
48
+ };
49
+ var wrapNodeIntoListItem = function wrapNodeIntoListItem(node) {
50
+ return node.type === LIST_ITEM ? node : {
51
+ type: LIST_ITEM,
52
+ children: [node]
53
+ };
54
+ };
55
+ var isSingleLic = function isSingleLic(fragment) {
56
+ var isFragmentOnlyListRoot = fragment.length === 1 && isListRoot(fragment[0]);
57
+ return isFragmentOnlyListRoot && _toConsumableArray(getNodes({
58
+ children: fragment
59
+ })).filter(function (entry) {
60
+ return Element.isElement(entry[0]);
61
+ }).filter(function (_ref4) {
62
+ var _ref5 = _slicedToArray(_ref4, 1),
63
+ node = _ref5[0];
64
+ return node.type === LIST_LIC;
65
+ }).length === 1;
66
+ };
67
+ export var getTextAndListItemNodes = function getTextAndListItemNodes(editor, fragment, liEntry, licEntry) {
68
+ var _liEntry = _slicedToArray(liEntry, 2),
69
+ liPath = _liEntry[1];
70
+ var _licEntry = _slicedToArray(licEntry, 2),
71
+ licNode = _licEntry[0],
72
+ licPath = _licEntry[1];
73
+ var isEmptyNode = !Node.string(licNode);
74
+ // format copied fragment
75
+ // first: lic, path: [...liPath, 0, 0]
76
+ // rest: children, path: [...liPath, 1, 0]
77
+ var _fragment$flatMap$map = fragment.flatMap(trimList).map(wrapNodeIntoListItem),
78
+ _fragment$flatMap$map2 = _toArray(_fragment$flatMap$map),
79
+ first = _fragment$flatMap$map2[0],
80
+ rest = _fragment$flatMap$map2.slice(1);
81
+ var textNode = null;
82
+ var listItemNodes = [];
83
+ if (isListRoot(fragment[0])) {
84
+ if (isSingleLic(fragment)) {
85
+ textNode = first;
86
+ listItemNodes = rest;
87
+ } else if (isEmptyNode) {
88
+ var li = getNode(editor, liPath);
89
+ var _li$children = _toArray(li.children),
90
+ currentSubLists = _li$children.slice(1); // old
91
+
92
+ var _first$children = _toArray(first.children),
93
+ newLic = _first$children[0],
94
+ newSubLists = _first$children.slice(1); // copied
95
+ // insert copied contents
96
+ Transforms.insertNodes(editor, newLic, {
97
+ at: Path.next(licPath),
98
+ select: true
99
+ });
100
+ Transforms.removeNodes(editor, {
101
+ at: licPath
102
+ });
103
+ if (newSubLists.length) {
104
+ if (currentSubLists.length) {
105
+ var path = [].concat(_toConsumableArray(liPath), [1, 0]);
106
+ Transforms.insertNodes(editor, newSubLists[0].children, {
107
+ at: path,
108
+ select: true
109
+ });
110
+ } else {
111
+ Transforms.insertNodes(editor, newSubLists, {
112
+ at: Path.next(licPath),
113
+ select: true
114
+ });
115
+ }
116
+ }
117
+ textNode = {
118
+ text: ''
119
+ };
120
+ listItemNodes = rest;
121
+ } else {
122
+ textNode = {
123
+ text: ''
124
+ };
125
+ listItemNodes = [first].concat(_toConsumableArray(rest));
126
+ }
127
+ } else {
128
+ textNode = first;
129
+ listItemNodes = rest;
130
+ return {
131
+ textNode: textNode,
132
+ listItemNodes: listItemNodes
133
+ };
134
+ }
135
+ return {
136
+ textNode: textNode,
137
+ listItemNodes: listItemNodes
138
+ };
139
+ };
140
+ export var insertFragmentList = function insertFragmentList(editor) {
141
+ var _insertFragment = editor.insertFragment;
142
+ return function (fragment) {
143
+ Editor.withoutNormalizing(editor, function () {
144
+ var liEntry = findNode(editor, {
145
+ match: {
146
+ type: LIST_ITEM
147
+ },
148
+ mode: 'lowest'
149
+ });
150
+ if (!liEntry) {
151
+ var nodes = isListRoot(fragment) ? [{
152
+ text: ''
153
+ }].concat(_toConsumableArray(fragment)) : fragment;
154
+ return _insertFragment(nodes);
155
+ }
156
+ Transforms.insertFragment(editor, [{
157
+ text: ''
158
+ }]); // need ' '
159
+
160
+ liEntry = findNode(editor, {
161
+ match: {
162
+ type: LIST_ITEM
163
+ },
164
+ mode: 'lowest'
165
+ });
166
+ var licEntry = findNode(editor, {
167
+ match: {
168
+ type: LIST_LIC
169
+ },
170
+ mode: 'lowest'
171
+ });
172
+ if (!licEntry) {
173
+ var _nodes = isListRoot(fragment) ? [{
174
+ text: ''
175
+ }].concat(_toConsumableArray(fragment)) : fragment;
176
+ return _insertFragment(_nodes);
177
+ }
178
+ var _getTextAndListItemNo = getTextAndListItemNodes(editor, fragment, liEntry, licEntry),
179
+ textNode = _getTextAndListItemNo.textNode,
180
+ listItemNodes = _getTextAndListItemNo.listItemNodes;
181
+ Transforms.insertFragment(editor, [textNode]);
182
+ var _liEntry2 = liEntry,
183
+ _liEntry3 = _slicedToArray(_liEntry2, 2),
184
+ liPath = _liEntry3[1];
185
+ return Transforms.insertNodes(editor, listItemNodes, {
186
+ at: Path.next(liPath),
187
+ select: true
188
+ });
189
+ });
190
+ };
191
+ };
@@ -0,0 +1,9 @@
1
+ import isUrl from 'is-url';
2
+ export var IMAGE_TYPES = ['png', 'jpg', 'gif'];
3
+ export var isImage = function isImage(url) {
4
+ if (!url) return false;
5
+ if (!isUrl(url)) return false;
6
+ var suffix = url.split('.')[1]; // http://xx/mm/*.png
7
+ if (!suffix) return false;
8
+ return IMAGE_TYPES.includes(suffix.toLowerCase());
9
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seafile/sdoc-editor",
3
- "version": "0.1.14",
3
+ "version": "0.1.15",
4
4
  "private": false,
5
5
  "description": "This is a sdoc editor",
6
6
  "main": "dist/index.js",
package/dist/config.js DELETED
@@ -1,16 +0,0 @@
1
- var serverConfig = {
2
- //serviceUrl: "http://127.0.0.1:8000",
3
- serviceUrl: "http://192.168.1.100:8000",
4
- username: "lj@11.com",
5
- password: "11",
6
- repoID: "79d1fa93-4b5f-4d6c-8fb5-ad3958e1fa47",
7
- userInfo: {
8
- username: 'lj@11.com',
9
- name: 'lj-',
10
- contact_email: 'lj@11.com'
11
- },
12
- filePath: '/xxx.md',
13
- fileName: 'xxx.md',
14
- dirPath: '/'
15
- };
16
- export { serverConfig };