@seafile/sdoc-editor 0.4.37 → 0.5.0

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.
@@ -29,15 +29,50 @@ export const getSelectCodeElem = editor => {
29
29
  if (codeNode == null) return null;
30
30
  return codeNode;
31
31
  };
32
+ export const getCodeBlockNode = language => {
33
+ const node = {
34
+ id: slugid.nice(),
35
+ type: CODE_BLOCK,
36
+ language,
37
+ style: {
38
+ white_space: 'nowrap'
39
+ },
40
+ // default nowrap
41
+ children: [{
42
+ id: slugid.nice(),
43
+ type: CODE_LINE,
44
+ children: [{
45
+ text: '',
46
+ id: slugid.nice()
47
+ }]
48
+ }]
49
+ };
50
+ return node;
51
+ };
32
52
  export const changeToCodeBlock = function (editor) {
33
53
  let language = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
34
54
  let position = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : INSERT_POSITION.CURRENT;
35
- // Summarizes the strings for the selected highest-level node
36
- let strArr = [];
37
- let path = Editor.path(editor, editor.selection);
55
+ if (!editor.selection) return;
56
+ let strArr = []; // Summarizes the strings for the selected highest-level node
57
+ const path = Editor.path(editor, editor.selection, {
58
+ edge: 'start'
59
+ });
60
+ const newCodeBlockNode = getCodeBlockNode(language); // New code-block node
61
+
62
+ // Insert after
38
63
  if (position === INSERT_POSITION.AFTER) {
39
64
  strArr = [''];
40
- } else {
65
+ newCodeBlockNode.children[0].children[0].text = strArr.join('\n');
66
+ Transforms.insertNodes(editor, newCodeBlockNode, {
67
+ mode: 'highest',
68
+ at: [path[0] + 1]
69
+ });
70
+ Transforms.select(editor, [path[0] + 1, 0, 0]);
71
+ return;
72
+ }
73
+
74
+ // Insert current
75
+ if (position === INSERT_POSITION.CURRENT) {
41
76
  // Select the plain text of the node
42
77
  const nodeEntries = Editor.nodes(editor, {
43
78
  match: n => editor.children.includes(n),
@@ -48,55 +83,33 @@ export const changeToCodeBlock = function (editor) {
48
83
  const [n] = nodeEntry;
49
84
  if (n) strArr.push(Node.string(n));
50
85
  }
51
-
52
86
  // Deletes the selected node at the highest level
53
87
  Transforms.removeNodes(editor, {
54
88
  mode: 'highest'
55
89
  });
56
- }
57
-
58
- // Insert the codeBlockNode node
59
- const newCodeBlockNode = {
60
- id: slugid.nice(),
61
- type: CODE_BLOCK,
62
- language,
63
- style: {
64
- white_space: 'nowrap' // default nowrap
65
- },
66
90
 
67
- children: [{
68
- id: slugid.nice(),
69
- type: CODE_LINE,
70
- children: [{
71
- text: strArr.join('\n'),
72
- id: slugid.nice()
73
- }]
74
- }]
75
- };
76
- if (position === INSERT_POSITION.AFTER) {
91
+ // Modify location
92
+ const atPath = [path[0]];
93
+ const atPoint = {
94
+ anchor: {
95
+ offset: 0,
96
+ path: [path[0], 0, 0]
97
+ },
98
+ focus: {
99
+ offset: 0,
100
+ path: [path[0], 0, 0]
101
+ }
102
+ };
103
+ // Insert new node
104
+ newCodeBlockNode.children[0].children[0].text = strArr.join('\n');
77
105
  Transforms.insertNodes(editor, newCodeBlockNode, {
78
106
  mode: 'highest',
79
- at: [path[0] + 1]
107
+ at: atPath
108
+ });
109
+ queueMicrotask(() => {
110
+ Transforms.select(editor, atPoint);
80
111
  });
81
- Transforms.select(editor, [path[0] + 1, 0, 0]);
82
- return;
83
112
  }
84
- const atPath = path ? [path[0]] : editor.selection;
85
- const atPoint = path ? {
86
- anchor: {
87
- offset: 0,
88
- path: [path[0], 0, 0]
89
- },
90
- focus: {
91
- offset: 0,
92
- path: [path[0], 0, 0]
93
- }
94
- } : editor.selection;
95
- Transforms.insertNodes(editor, newCodeBlockNode, {
96
- mode: 'highest',
97
- at: atPath
98
- });
99
- Transforms.select(editor, atPoint);
100
113
  };
101
114
  export const changeToPlainText = editor => {
102
115
  const elem = getSelectCodeElem(editor);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seafile/sdoc-editor",
3
- "version": "0.4.37",
3
+ "version": "0.5.0",
4
4
  "private": false,
5
5
  "description": "This is a sdoc editor",
6
6
  "main": "dist/index.js",