vue-editify 0.2.17 → 0.2.18

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. package/examples/App.vue +289 -13
  2. package/lib/components/colors/colors.vue.d.ts +9 -0
  3. package/lib/components/colors/props.d.ts +4 -0
  4. package/lib/core/function.d.ts +120 -45
  5. package/lib/core/rule.d.ts +23 -17
  6. package/lib/core/tool.d.ts +1 -13
  7. package/lib/editify/editify.vue.d.ts +10 -1
  8. package/lib/editify/props.d.ts +1 -1
  9. package/lib/editify/toolbar/props.d.ts +1 -1
  10. package/lib/editify/toolbar/toolbar.vue.d.ts +3 -3
  11. package/lib/editify.es.js +13640 -13799
  12. package/lib/editify.umd.js +2 -2
  13. package/lib/feature/align.d.ts +0 -14
  14. package/lib/feature/heading.d.ts +0 -14
  15. package/lib/feature/lineHeight.d.ts +0 -14
  16. package/lib/feature/orderList.d.ts +1 -3
  17. package/lib/feature/task.d.ts +0 -14
  18. package/lib/feature/unorderList.d.ts +1 -3
  19. package/lib/index.d.ts +12 -3
  20. package/package.json +2 -2
  21. package/src/components/button/button.vue +3 -3
  22. package/src/components/checkbox/checkbox.vue +1 -1
  23. package/src/components/colors/colors.vue +4 -4
  24. package/src/components/colors/props.ts +6 -1
  25. package/src/components/insertAttachment/insertAttachment.vue +1 -1
  26. package/src/components/insertImage/insertImage.vue +1 -1
  27. package/src/components/insertLink/insertLink.vue +1 -1
  28. package/src/components/insertVideo/insertVideo.vue +1 -1
  29. package/src/components/layer/layer.vue +9 -3
  30. package/src/components/tooltip/tooltip.vue +1 -1
  31. package/src/components/updateLink/updateLink.vue +1 -1
  32. package/src/core/function.ts +961 -475
  33. package/src/core/rule.ts +85 -367
  34. package/src/core/tool.ts +8 -114
  35. package/src/editify/editify.less +88 -14
  36. package/src/editify/editify.vue +117 -65
  37. package/src/editify/props.ts +1 -1
  38. package/src/editify/toolbar/props.ts +2 -2
  39. package/src/editify/toolbar/toolbar.vue +12 -12
  40. package/src/feature/align.ts +1 -61
  41. package/src/feature/attachment.ts +13 -26
  42. package/src/feature/backColor.ts +1 -0
  43. package/src/feature/foreColor.ts +1 -0
  44. package/src/feature/heading.ts +2 -73
  45. package/src/feature/infoBlock.ts +4 -35
  46. package/src/feature/lineHeight.ts +1 -77
  47. package/src/feature/mathformula.ts +3 -50
  48. package/src/feature/orderList.ts +166 -35
  49. package/src/feature/panel.ts +4 -49
  50. package/src/feature/sub.ts +1 -1
  51. package/src/feature/super.ts +1 -1
  52. package/src/feature/task.ts +1 -55
  53. package/src/feature/unorderList.ts +106 -35
  54. package/src/feature/video.ts +1 -1
  55. package/src/icon/iconfont.css +40 -0
  56. package/src/icon/iconfont.ttf +0 -0
  57. package/src/icon/iconfont.woff +0 -0
  58. package/src/index.ts +14 -8
  59. package/src/locale/en_US.ts +112 -110
  60. package/src/locale/zh_CN.ts +11 -9
package/src/core/rule.ts CHANGED
@@ -1,313 +1,99 @@
1
1
  import { AlexEditor, AlexElement, AlexElementCreateConfigType } from 'alex-editor'
2
2
  import { common as DapCommon, color as DapColor } from 'dap-util'
3
3
  import { LanguagesItemType, getHljsHtml } from '@/hljs'
4
- import { getTableSize, getCellSpanNumber, elementIsList, elementIsTask, elementIsAttachment, elementIsMathformula, elementIsInfoBlock, elementIsPanel } from './function'
4
+ import { getTableSize, getCellSpanNumber, elementIsList, elementIsTask, elementIsAttachment, elementIsMathformula, elementIsInfoBlock, elementIsPanel, autocompleteTableCells, autoHideMergedTableCells, updateRangeInPre, addSpaceTextToBothSides } from './function'
5
5
 
6
6
  /**
7
- * 自动补全表格行和列
7
+ * 有序列表和无序列表的格式化处理
8
8
  * @param editor
9
- * @param rowElements
10
- * @param rowNumber
11
- * @param columnNumber
9
+ * @param element
12
10
  */
13
- const autocompleteTableCells = (editor: AlexEditor, rowElements: AlexElement[], rowNumber: number, columnNumber: number) => {
14
- //遍历所有的单元格
15
- AlexElement.flatElements(rowElements).forEach(item => {
16
- if (item.parsedom == 'td' && item.hasMarks()) {
17
- //删除被合并的标识
18
- if (item.marks!['data-editify-merged']) {
19
- delete item.marks!['data-editify-merged']
20
- }
21
- //获取colspan
22
- const colspan = isNaN(Number(item.marks!['colspan'])) ? 1 : Number(item.marks!['colspan'])
23
- //获取rowspan
24
- const rowspan = isNaN(Number(item.marks!['rowspan'])) ? 1 : Number(item.marks!['rowspan'])
25
- //针对colspan>1的单元格在后面补全隐藏的单元格
26
- if (colspan > 1) {
27
- let i = 1
28
- //补全的数量小于需要补全的数量并且列总数量小于理论数量
29
- while (i < colspan && item.parent!.children!.length < columnNumber) {
30
- const column = AlexElement.create({
31
- type: 'inblock',
32
- parsedom: 'td',
33
- marks: {
34
- 'data-editify-merged': 'true'
35
- },
36
- children: [
37
- {
38
- type: 'closed',
39
- parsedom: 'br'
40
- }
41
- ]
42
- })
43
- editor.addElementAfter(column, item)
44
- i++
45
- }
46
- }
47
- //针对rowspan>1的单元格在后面的行中对应位置补全隐藏的单元格
48
- if (rowspan > 1) {
49
- let el = item
50
- let i = 1
51
- while (i < rowspan && editor.getNextElement(el.parent!) && editor.getNextElement(el.parent!)!.children!.length < columnNumber) {
52
- //下一行
53
- const nextRow = editor.getNextElement(el.parent!)!
54
- //单元格在行中的序列
55
- const index = el.parent!.children!.findIndex(item => item.isEqual(el))
56
- //下一行对应的单元格
57
- const nextCell = nextRow.children![index]
58
- //根据当前单元格的跨列数补充符合跨列数的隐藏单元格
59
- for (let j = 0; j < colspan; j++) {
60
- const column = AlexElement.create({
61
- type: 'inblock',
62
- parsedom: 'td',
63
- marks: {
64
- 'data-editify-merged': 'true'
65
- },
66
- children: [
67
- {
68
- type: 'closed',
69
- parsedom: 'br'
70
- }
71
- ]
72
- })
73
- if (nextCell) {
74
- editor.addElementBefore(column, nextCell)
75
- } else {
76
- editor.addElementTo(column, nextRow, nextRow.children!.length)
77
- }
78
- }
79
- el = nextRow.children![index]
80
- i++
11
+ export const listHandle = (editor: AlexEditor, element: AlexElement) => {
12
+ //ol标签和ul标签转为div
13
+ if ((element.parsedom == 'ol' || element.parsedom == 'ul') && element.hasChildren()) {
14
+ element.children!.forEach(el => {
15
+ el.type = element.type
16
+ el.parsedom = 'div'
17
+ if (el.hasMarks()) {
18
+ el.marks!['data-editify-list'] = element.parsedom
19
+ } else {
20
+ el.marks = {
21
+ 'data-editify-list': element.parsedom
81
22
  }
82
23
  }
83
- }
84
- })
85
- //遍历每一行,如果还缺少列则在后面补全列
86
- rowElements.forEach(rowElement => {
87
- //遍历该行的单元格获取总列数
88
- const number = rowElement.children!.length
89
- if (number < columnNumber) {
90
- for (let i = 0; i < columnNumber - number; i++) {
91
- const column = AlexElement.create({
92
- type: 'inblock',
93
- parsedom: 'td',
94
- children: [
95
- {
96
- type: 'closed',
97
- parsedom: 'br'
98
- }
99
- ]
100
- })
101
- editor.addElementTo(column, rowElement, rowElement.children!.length)
102
- }
103
- }
104
- })
105
- //获取总行数
106
- const length = rowElements.length
107
- //判断总行数是否小于实际行数则补全行
108
- if (length < rowNumber) {
109
- for (let i = 0; i < rowNumber - length; i++) {
110
- const children: AlexElementCreateConfigType[] = []
111
- for (let j = 0; j < columnNumber; j++) {
112
- children.push({
113
- type: 'inblock',
114
- parsedom: 'td',
115
- children: [
116
- {
117
- type: 'closed',
118
- parsedom: 'br'
119
- }
120
- ]
121
- })
122
- }
123
- const row = AlexElement.create({
124
- type: 'inblock',
125
- parsedom: 'tr',
126
- children
127
- })
128
- rowElements.push(row)
129
- }
24
+ //如果是有序列表添加唯一标记
25
+ el.marks!['data-editify-element'] = el.key
26
+ //插入到该元素之前
27
+ editor.addElementBefore(el, element)
28
+ })
29
+ element.children = []
30
+ }
31
+ //有序列表和无序列表添加唯一标记
32
+ if (!element.isEmpty() && (elementIsList(element, true) || elementIsList(element, false))) {
33
+ element.marks!['data-editify-element'] = element.key
130
34
  }
131
35
  }
132
36
 
133
37
  /**
134
- * 自动隐藏被合并的单元格
135
- * @param editor
136
- * @param rowElements
137
- */
138
- const autoHideMergedTableCells = (editor: AlexEditor, rowElements: AlexElement[]) => {
139
- const cells = AlexElement.flatElements(rowElements).filter(item => item.parsedom == 'td')
140
- cells.forEach(cell => {
141
- if (cell.hasMarks() && !cell.marks!['data-editify-merged']) {
142
- //获取colspan
143
- const colspan = isNaN(Number(cell.marks!['colspan'])) ? 1 : Number(cell.marks!['colspan'])
144
- //获取rowspan
145
- const rowspan = isNaN(Number(cell.marks!['rowspan'])) ? 1 : Number(cell.marks!['rowspan'])
146
- //如果是跨列单元格,隐藏该单元格同行后的colspan-1个单元格
147
- if (colspan > 1) {
148
- let el = cell
149
- let i = 1
150
- while (i < colspan) {
151
- const nextCell = editor.getNextElement(el)!
152
- if (nextCell) {
153
- if (nextCell.hasMarks()) {
154
- nextCell.marks!['data-editify-merged'] = 'true'
155
- } else {
156
- nextCell.marks = {
157
- 'data-editify-merged': 'true'
158
- }
159
- }
160
- el = nextCell
161
- i++
162
- } else {
163
- break
164
- }
165
- }
166
- }
167
- //如果是跨行单元格,隐藏该单元格同列后的rowspan-1行单元格
168
- if (rowspan > 1) {
169
- const index = cell.parent!.children!.findIndex(item => item.isEqual(cell))
170
- let el = cell
171
- let i = 1
172
- while (i < rowspan && el && editor.getNextElement(el.parent!)) {
173
- const nextRow = editor.getNextElement(el.parent!)!
174
- //根据跨行单元格占据的列数,在后的rowspan-1行中隐藏colspan个单元格
175
- for (let j = index; j < index + colspan; j++) {
176
- const current = nextRow.children![j]
177
- if (current) {
178
- if (current.hasMarks()) {
179
- current.marks!['data-editify-merged'] = 'true'
180
- } else {
181
- current.marks = {
182
- 'data-editify-merged': 'true'
183
- }
184
- }
185
- }
186
- }
187
- el = nextRow.children![index]
188
- i++
189
- }
190
- }
191
- }
192
- })
193
- }
194
-
195
- /**
196
- * 更新代码块内的光标位置
38
+ * 图片格式化处理
197
39
  * @param editor
198
40
  * @param element
199
- * @param originalTextElements
200
- * @param newElements
201
- * @returns
202
41
  */
203
- const updateRangeInPre = (editor: AlexEditor, element: AlexElement, originalTextElements: AlexElement[], newElements: AlexElement[]) => {
204
- if (!editor.range) {
205
- return
206
- }
207
- //如果虚拟光标的起点在代码块内对虚拟光标的起点进行重新定位
208
- if (editor.range.anchor.element.getBlock().isEqual(element)) {
209
- //获取起点所在文本元素的在所有文本元素中的序列
210
- const elIndex = originalTextElements.findIndex(el => editor.range!.anchor.element.isEqual(el))
211
- //起点在整个代码内容中的位置
212
- const offset = originalTextElements.filter((_el, i) => i < elIndex).reduce((total, item) => total + item.textContent!.length, 0) + editor.range.anchor.offset
213
- //获取pre下新的子孙元素中全部的文本元素
214
- const newTextElements = AlexElement.flatElements(newElements).filter(el => el.isText() && !el.isEmpty())
215
- let i = 0
216
- let index = 0
217
- //遍历
218
- while (i < newTextElements.length) {
219
- let newIndex = index + newTextElements[i].textContent!.length
220
- if (offset >= index && offset <= newIndex) {
221
- editor.range.anchor.element = newTextElements[i]
222
- editor.range.anchor.offset = offset - index
223
- break
224
- }
225
- i++
226
- index = newIndex
42
+ export const imageHandle = (_editor: AlexEditor, element: AlexElement) => {
43
+ if (!element.isEmpty() && element.parsedom == 'img') {
44
+ //添加唯一标记
45
+ const marks = {
46
+ 'data-editify-element': element.key
227
47
  }
228
- }
229
- //如果虚拟光标的终点在代码块内需要对虚拟光标的终点进行重新定位
230
- if (editor.range.focus.element.getBlock().isEqual(element)) {
231
- //获取终点所在文本元素的在所有文本元素中的序列
232
- const elIndex = originalTextElements.findIndex(el => editor.range!.focus.element.isEqual(el))
233
- //终点在整个代码内容中的位置
234
- const offset = originalTextElements.filter((_el, i) => i < elIndex).reduce((total, item) => total + item.textContent!.length, 0) + editor.range.focus.offset
235
- //获取全部的新文本元素
236
- const newTextElements = AlexElement.flatElements(newElements).filter(el => el.isText() && !el.isEmpty())
237
- let i = 0
238
- let index = 0
239
- //遍历
240
- while (i < newTextElements.length) {
241
- let newIndex = index + newTextElements[i].textContent!.length
242
- if (offset >= index && offset <= newIndex) {
243
- editor.range.focus.element = newTextElements[i]
244
- editor.range.focus.offset = offset - index
245
- break
246
- }
247
- i++
248
- index = newIndex
48
+ if (element.hasMarks()) {
49
+ Object.assign(element.marks!, marks)
50
+ } else {
51
+ element.marks = marks
249
52
  }
250
53
  }
251
54
  }
252
55
 
253
56
  /**
254
- * 元素格式化时转换ol和li标签
57
+ * 视频格式化处理
255
58
  * @param editor
256
59
  * @param element
257
60
  */
258
- export const parseList = (editor: AlexEditor, element: AlexElement) => {
259
- //ol标签和ul标签转为div
260
- if (element.parsedom == 'ol' || element.parsedom == 'ul') {
261
- if (element.hasChildren()) {
262
- element.children!.forEach(el => {
263
- const newEl = el.clone()
264
- newEl.parsedom = 'div'
265
- newEl.type = element.type
266
- if (newEl.hasMarks()) {
267
- newEl.marks!['data-editify-list'] = element.parsedom
268
- } else {
269
- newEl.marks = {
270
- 'data-editify-list': element.parsedom
271
- }
272
- }
273
- //插入到该元素之前
274
- editor.addElementBefore(newEl, element)
275
- })
61
+ export const videoHandle = (editor: AlexEditor, element: AlexElement) => {
62
+ if (!element.isEmpty() && element.parsedom == 'video') {
63
+ //添加唯一标记
64
+ const marks = {
65
+ 'data-editify-element': element.key
66
+ }
67
+ if (element.hasMarks()) {
68
+ Object.assign(element.marks!, marks)
69
+ } else {
70
+ element.marks = marks
276
71
  }
277
- element.toEmpty()
72
+ //两侧加上空白元素
73
+ addSpaceTextToBothSides(editor, element)
278
74
  }
279
75
  }
280
76
 
281
77
  /**
282
- * 元素格式化时处理有序列表的序号值
78
+ * 分隔线格式化处理
283
79
  * @param editor
284
80
  * @param element
285
81
  */
286
- export const orderdListHandle = (editor: AlexEditor, element: AlexElement) => {
287
- //有序列表的序号处理
288
- if (!element.isEmpty() && elementIsList(element, true)) {
289
- //获取前一个元素
290
- const previousElement = editor.getPreviousElement(element)
291
- //如果前一个元素存在并且也是有序列表
292
- if (previousElement && !previousElement.isEmpty() && elementIsList(previousElement, true)) {
293
- const previousValue = Number(previousElement.marks!['data-editify-value'])
294
- element.marks!['data-editify-value'] = previousValue + 1
295
- }
296
- //前一个元素不是有序列表,则从0开始
297
- else {
298
- element.marks!['data-editify-value'] = 1
299
- }
82
+ export const separatorHandle = (editor: AlexEditor, element: AlexElement) => {
83
+ //两侧加上空白元素
84
+ if (!element.isEmpty() && element.parsedom == 'hr') {
85
+ addSpaceTextToBothSides(editor, element)
300
86
  }
301
87
  }
302
88
 
303
89
  /**
304
- * 元素格式化时处理常规元素(图片、视频、分隔线、行内代码)
90
+ * 链接格式化处理
305
91
  * @param editor
306
92
  * @param element
307
93
  */
308
- export const commonElementHandle = (editor: AlexEditor, element: AlexElement) => {
309
- //图片、视频和链接设置marks
310
- if (element.parsedom == 'img' || element.parsedom == 'video' || element.parsedom == 'a') {
94
+ export const linkHandle = (_editor: AlexEditor, element: AlexElement) => {
95
+ //添加唯一标记
96
+ if (!element.isEmpty() && element.parsedom == 'a') {
311
97
  const marks = {
312
98
  'data-editify-element': element.key
313
99
  }
@@ -317,32 +103,16 @@ export const commonElementHandle = (editor: AlexEditor, element: AlexElement) =>
317
103
  element.marks = marks
318
104
  }
319
105
  }
106
+ }
320
107
 
321
- //视频或者分隔线的特殊处理,两侧无元素时在两侧加上空白文本
322
- if (element.parsedom == 'video' || element.parsedom == 'hr') {
323
- const previousElement = editor.getPreviousElement(element)
324
- const newTextElement = editor.getNextElement(element)
325
- //如果不存在前一个元素或者前一个元素不是空白元素则设置空白元素
326
- if (!previousElement || !previousElement.isSpaceText()) {
327
- const spaceText = AlexElement.getSpaceElement()
328
- editor.addElementBefore(spaceText, element)
329
- }
330
- //如果不存在后一个元素或者后一个元素不是空白元素则设置空白元素
331
- if (!newTextElement || !newTextElement.isSpaceText()) {
332
- const spaceText = AlexElement.getSpaceElement()
333
- editor.addElementAfter(spaceText, element)
334
- }
335
- //如果光标在视频上则更新光标位置
336
- if (editor.range && element.isContains(editor.range.anchor.element)) {
337
- editor.range.anchor.moveToEnd(editor.getNextElement(element)!)
338
- }
339
- if (editor.range && element.isContains(editor.range.focus.element)) {
340
- editor.range.focus.moveToEnd(editor.getNextElement(element)!)
341
- }
342
- }
343
-
108
+ /**
109
+ * 行内代码格式化处理
110
+ * @param _editor
111
+ * @param element
112
+ */
113
+ export const codeHandle = (_editor: AlexEditor, element: AlexElement) => {
344
114
  //将code转为span[data-editify-code]
345
- if (element.parsedom == 'code') {
115
+ if (!element.isEmpty() && element.parsedom == 'code') {
346
116
  element.parsedom = 'span'
347
117
  const marks = {
348
118
  'data-editify-code': true
@@ -356,22 +126,16 @@ export const commonElementHandle = (editor: AlexEditor, element: AlexElement) =>
356
126
  }
357
127
 
358
128
  /**
359
- * 元素格式化时处理表格:th转为td
129
+ * 表格格式化处理
360
130
  * @param editor
361
131
  * @param element
362
132
  */
363
- export const tableThTdHandle = (_editor: AlexEditor, element: AlexElement) => {
364
- if (element.parsedom == 'th') {
133
+ export const tableHandle = (editor: AlexEditor, element: AlexElement) => {
134
+ //处理th转为td
135
+ if (!element.isEmpty() && element.parsedom == 'th') {
365
136
  element.parsedom = 'td'
366
137
  }
367
- }
368
-
369
- /**
370
- * 元素格式化时处理表格:格式化表格
371
- * @param editor
372
- * @param element
373
- */
374
- export const tableFormatHandle = (editor: AlexEditor, element: AlexElement) => {
138
+ //格式化表格结构和内容
375
139
  if (element.hasChildren() && element.parsedom == 'table') {
376
140
  //设置key到marks上
377
141
  if (element.hasMarks()) {
@@ -458,16 +222,8 @@ export const tableFormatHandle = (editor: AlexEditor, element: AlexElement) => {
458
222
  //对表格单元格合并状态进行处理
459
223
  autoHideMergedTableCells(editor, rows)
460
224
  }
461
- }
462
-
463
- /**
464
- * 元素格式化时处理表格:处理光标在表格隐藏单元格内的情况
465
- * @param editor
466
- * @param element
467
- */
468
- export const tableRangeMergedHandle = (editor: AlexEditor, element: AlexElement) => {
469
- //如果元素是被隐藏的单元格,并且光标在该单元格内
470
- if (element.parsedom == 'td' && element.hasMarks() && element.marks!['data-editify-merged'] && editor.range) {
225
+ //处理光标在表格隐藏单元格内的情况
226
+ if (!element.isEmpty() && element.parsedom == 'td' && element.hasMarks() && element.marks!['data-editify-merged'] && editor.range) {
471
227
  //单元格向左查找设置焦点
472
228
  const queryLeftSetRange = (_element: AlexElement, callback: (ele: AlexElement) => void) => {
473
229
  //是否已查找到
@@ -548,7 +304,7 @@ export const tableRangeMergedHandle = (editor: AlexEditor, element: AlexElement)
548
304
  }
549
305
 
550
306
  /**
551
- * 元素格式化时处理pre,将pre的内容根据语言进行样式处理
307
+ * 代码块格式化处理
552
308
  * @param editor
553
309
  * @param element
554
310
  * @param highlight
@@ -556,7 +312,8 @@ export const tableRangeMergedHandle = (editor: AlexEditor, element: AlexElement)
556
312
  */
557
313
  export const preHandle = (editor: AlexEditor, element: AlexElement, highlight: boolean, languages: (string | LanguagesItemType)[]) => {
558
314
  //如果是代码块进行处理
559
- if (element.parsedom == 'pre') {
315
+ if (!element.isEmpty() && element.parsedom == 'pre') {
316
+ //设置key属性
560
317
  const marks = {
561
318
  'data-editify-element': element.key
562
319
  }
@@ -573,9 +330,9 @@ export const preHandle = (editor: AlexEditor, element: AlexElement, highlight: b
573
330
  //语言类型是否是列表内的
574
331
  const flag = languages.some(item => {
575
332
  if (DapCommon.isObject(item)) {
576
- return (<LanguagesItemType>item).value == language
333
+ return (item as LanguagesItemType).value == language
577
334
  }
578
- return <string>item == language
335
+ return (item as string) == language
579
336
  })
580
337
  //如果不是列表内的则清除
581
338
  if (!flag) {
@@ -618,7 +375,7 @@ export const preHandle = (editor: AlexEditor, element: AlexElement, highlight: b
618
375
  }
619
376
 
620
377
  /**
621
- * 元素格式化时处理附件元素
378
+ * 附件格式化处理
622
379
  * @param editor
623
380
  * @param element
624
381
  * @param $editTrans
@@ -631,70 +388,31 @@ export const attachmentHandle = (editor: AlexEditor, element: AlexElement, $edit
631
388
  if (!element.marks!['data-editify-attachment-name']) {
632
389
  element.marks!['data-editify-attachment-name'] = $editTrans('attachmentDefaultName')
633
390
  }
634
- //前一个元素
635
- const previousElement = editor.getPreviousElement(element)
636
- //后一个元素
637
- const newTextElement = editor.getNextElement(element)
638
- //如果不存在前一个元素或者前一个元素不是空白元素则设置空白元素
639
- if (!previousElement || !previousElement.isSpaceText()) {
640
- const spaceText = AlexElement.getSpaceElement()
641
- editor.addElementBefore(spaceText, element)
642
- }
643
- //如果不存在后一个元素或者后一个元素不是空白元素则设置空白元素
644
- if (!newTextElement || !newTextElement.isSpaceText()) {
645
- const spaceText = AlexElement.getSpaceElement()
646
- editor.addElementAfter(spaceText, element)
647
- }
648
- //如果光标在元素上则更新光标位置
649
- if (editor.range && element.isContains(editor.range.anchor.element)) {
650
- editor.range.anchor.moveToEnd(editor.getNextElement(element)!)
651
- }
652
- if (editor.range && element.isContains(editor.range.focus.element)) {
653
- editor.range.focus.moveToEnd(editor.getNextElement(element)!)
654
- }
391
+ //两侧设置空白元素
392
+ addSpaceTextToBothSides(editor, element)
655
393
  }
656
394
  }
657
395
 
658
396
  /**
659
- * 元素格式化时处理数学公式元素
397
+ * 数学公式格式化处理
660
398
  * @param editor
661
399
  * @param element
662
400
  */
663
401
  export const mathformulaHandle = (editor: AlexEditor, element: AlexElement) => {
664
- //给元素设置两侧的空白字符
402
+ //两侧设置空白元素
665
403
  if (!element.isEmpty() && elementIsMathformula(element)) {
666
- //前一个元素
667
- const previousElement = editor.getPreviousElement(element)
668
- //后一个元素
669
- const newTextElement = editor.getNextElement(element)
670
- //如果不存在前一个元素或者前一个元素不是空白元素则设置空白元素
671
- if (!previousElement || !previousElement.isSpaceText()) {
672
- const spaceText = AlexElement.getSpaceElement()
673
- editor.addElementBefore(spaceText, element)
674
- }
675
- //如果不存在后一个元素或者后一个元素不是空白元素则设置空白元素
676
- if (!newTextElement || !newTextElement.isSpaceText()) {
677
- const spaceText = AlexElement.getSpaceElement()
678
- editor.addElementAfter(spaceText, element)
679
- }
680
- //如果光标在元素上则更新光标位置
681
- if (editor.range && element.isContains(editor.range.anchor.element)) {
682
- editor.range.anchor.moveToEnd(editor.getNextElement(element)!)
683
- }
684
- if (editor.range && element.isContains(editor.range.focus.element)) {
685
- editor.range.focus.moveToEnd(editor.getNextElement(element)!)
686
- }
404
+ addSpaceTextToBothSides(editor, element)
687
405
  }
688
406
  }
689
407
 
690
408
  /**
691
- * 元素格式化时处理信息块元素
409
+ * 信息块格式化处理
692
410
  * @param editor
693
411
  * @param element
694
412
  * @param color
695
413
  */
696
414
  export const infoBlockHandle = (_editor: AlexEditor, element: AlexElement, color: string) => {
697
- if (elementIsInfoBlock(element) && color) {
415
+ if (!element.isEmpty() && elementIsInfoBlock(element) && color) {
698
416
  const parseColor = DapColor.hex2rgb(color)
699
417
  if (element.hasStyles()) {
700
418
  element.styles!['background-color'] = `rgba(${parseColor[0]},${parseColor[1]},${parseColor[2]},0.15)`
@@ -709,7 +427,7 @@ export const infoBlockHandle = (_editor: AlexEditor, element: AlexElement, color
709
427
  }
710
428
 
711
429
  /**
712
- * 元素格式化时处理一些特殊的内部块元素,转为根级块元素
430
+ * 一些特殊的内部块元素,转为根级块元素
713
431
  * @param editor
714
432
  * @param element
715
433
  */