@seafile/sdoc-editor 0.1.129 → 0.1.131

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.
@@ -23,7 +23,8 @@
23
23
  .sdoc-code-block-code .sdoc-code-line::before {
24
24
  content: counter(number);
25
25
  color: #ccc;
26
- margin-right: 24px;
26
+ display: inline-block;
27
+ width: 40px;
27
28
  }
28
29
 
29
30
  .sdoc-code-line
@@ -121,4 +121,11 @@ export var setClipboardData = function setClipboardData(value) {
121
121
  data.setData('text/code-block', JSON.stringify(value));
122
122
  }
123
123
  });
124
+ };
125
+ export var deleteBackwardByLength = function deleteBackwardByLength(editor, len) {
126
+ var i = len >= 4 ? 4 : len;
127
+ while (i > 0) {
128
+ Editor.deleteBackward(editor, 'word');
129
+ i--;
130
+ }
124
131
  };
@@ -1,9 +1,12 @@
1
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
1
2
  import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
2
3
  import _toArray from "@babel/runtime/helpers/esm/toArray";
3
4
  import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
4
5
  import slugid from 'slugid';
5
- import { Transforms, Node, Range, Editor } from '@seafile/slate';
6
+ import isHotkey from 'is-hotkey';
7
+ import { Transforms, Node, Range, Editor, Path } from '@seafile/slate';
6
8
  import { getNodeType, isLastNode, getSelectedNodeByType, generateEmptyElement } from '../../core';
9
+ import { deleteBackwardByLength } from './helpers';
7
10
  import { CODE_BLOCK, PARAGRAPH, CODE_LINE } from '../../constants';
8
11
  var withCodeBlock = function withCodeBlock(editor) {
9
12
  var normalizeNode = editor.normalizeNode,
@@ -22,6 +25,25 @@ var withCodeBlock = function withCodeBlock(editor) {
22
25
  return insertText(data);
23
26
  };
24
27
  newEditor.insertData = function (data) {
28
+ if (!newEditor.insertFragmentData(data)) {
29
+ var plaintext = data.getData('text/plain') || '';
30
+ if (plaintext) {
31
+ var fragmentData = [];
32
+ plaintext.split('\n').forEach(function (item) {
33
+ var codeLine = {
34
+ id: slugid.nice(),
35
+ type: CODE_LINE,
36
+ children: [{
37
+ text: item,
38
+ id: slugid.nice()
39
+ }]
40
+ };
41
+ fragmentData.push(codeLine);
42
+ });
43
+ newEditor.insertFragment(fragmentData);
44
+ return;
45
+ }
46
+ }
25
47
  if (data.types.includes('text/code-block') && !getSelectedNodeByType(editor, CODE_BLOCK)) {
26
48
  var codeBlockNode = JSON.parse(data.getData('text/code-block'));
27
49
  return insertNode(codeBlockNode);
@@ -147,6 +169,34 @@ var withCodeBlock = function withCodeBlock(editor) {
147
169
  // Perform default behavior
148
170
  return normalizeNode([node, path]);
149
171
  };
172
+ newEditor.codeBlockOnKeyDown = function (event) {
173
+ if (isHotkey('command+enter', event)) {
174
+ if (newEditor.selection && !Range.isExpanded(newEditor.selection)) {
175
+ var path = Editor.path(newEditor, newEditor.selection);
176
+ var p = generateEmptyElement(PARAGRAPH);
177
+ Transforms.insertNodes(newEditor, p, {
178
+ at: [path[0] + 1]
179
+ });
180
+ Transforms.select(newEditor, [path[0] + 1]);
181
+ }
182
+ }
183
+ if (isHotkey('tab', event)) {
184
+ newEditor.insertText(' ');
185
+ }
186
+ if (isHotkey('shift+tab', event)) {
187
+ var range = {
188
+ anchor: {
189
+ offset: 0,
190
+ path: newEditor.selection.focus.path
191
+ },
192
+ focus: _objectSpread({}, newEditor.selection.focus)
193
+ };
194
+ var str = Editor.string(newEditor, range);
195
+ if (str.trim() === '') {
196
+ deleteBackwardByLength(newEditor, str.length);
197
+ }
198
+ }
199
+ };
150
200
  return newEditor;
151
201
  };
152
202
  export default withCodeBlock;
@@ -1,9 +1,11 @@
1
1
  import { deserializeHtml } from './helper';
2
+ import { getSelectedNodeByType } from '../../core';
3
+ import { CODE_BLOCK } from '../../constants';
2
4
  var withHtml = function withHtml(editor) {
3
5
  var insertData = editor.insertData;
4
6
  var newEditor = editor;
5
7
  newEditor.insertData = function (data) {
6
- if (!newEditor.insertFragmentData(data)) {
8
+ if (!newEditor.insertFragmentData(data) && !getSelectedNodeByType(editor, CODE_BLOCK)) {
7
9
  var htmlContent = data.getData('text/html') || '';
8
10
  if (htmlContent) {
9
11
  var content = deserializeHtml(htmlContent);
@@ -79,6 +79,7 @@ var EventProxy = /*#__PURE__*/_createClass(function EventProxy(_editor) {
79
79
  _this.editor.imageOnKeyDown(event);
80
80
  }
81
81
  if (getSelectedNodeByType(editor, ELEMENT_TYPE.CODE_BLOCK)) {
82
+ _this.editor.codeBlockOnKeyDown(event);
82
83
  var eventBus = EventBus.getInstance();
83
84
  eventBus.dispatch(INTERNAL_EVENT.HIDDEN_CODE_BLOCK_HOVER_MENU);
84
85
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seafile/sdoc-editor",
3
- "version": "0.1.129",
3
+ "version": "0.1.131",
4
4
  "private": false,
5
5
  "description": "This is a sdoc editor",
6
6
  "main": "dist/index.js",