@@ -9,7 +9,6 @@ import $, { DomElement } from '../../utils/dom-core'
9
9
import Editor from '../../editor/index'
10
10
import { MenuActive } from '../menu-constructors/Menu'
11
11
import lineHeightList from './lineHeightList'
12
- import { UA } from '../../utils/util'
13
12
14
13
class LineHeight extends DropListMenu implements MenuActive {
15
14
constructor ( editor : Editor ) {
@@ -39,163 +38,81 @@ class LineHeight extends DropListMenu implements MenuActive {
39
38
* @param value value
40
39
*/
41
40
public command ( value : string ) : void {
42
- let selection = window . getSelection ? window . getSelection ( ) : document . getSelection ( )
43
- //允许设置dom
44
- const allowArray : string [ ] = [ 'P' ]
45
41
const editor = this . editor
46
- let st : string = ''
47
- //恢复焦点
42
+
43
+ //重置选区
48
44
editor . selection . restoreSelection ( )
49
- const $selectionElem = $ ( editor . selection . getSelectionContainerElem ( ) )
50
45
51
- if ( ! $selectionElem ?. length ) return
46
+ // 获取选区的祖先元素
47
+ const $containerElem = $ ( editor . selection . getSelectionContainerElem ( ) )
52
48
53
- const $selectionAll = $ ( editor . selection . getSelectionContainerElem ( ) )
54
- // let dom:HTMLElement= $selectionElem.elems[0]
55
- let dom : HTMLElement = $ ( editor . selection . getSelectionStartElem ( ) ) . elems [ 0 ]
56
- //获取元素的style
57
- let style : string | null = ''
58
- let styleList : string [ ] = [ ]
59
- //点击默认的时候删除line-height属性 并重新设置 style
60
- let styleStr : string = ''
49
+ if ( ! $containerElem . elems . length ) return
61
50
62
51
//选中多行操作
63
- if ( $selectionElem && editor . $textElem . equal ( $selectionElem ) ) {
64
- let isIE = UA . isIE ( )
65
- //获取range 开头结束的dom在 祖父元素的下标
66
- let indexStore : Array < number > = [ ]
67
- let arrayDom_a : Array < HTMLElement > = [ ]
68
- let arrayDom_b : Array < HTMLElement > = [ ]
52
+ if ( $containerElem && editor . $textElem . equal ( $containerElem ) ) {
53
+ // 标识是否可以设置行高的样式
54
+ let setStyleLock : boolean = false
55
+
69
56
//获取range 开头结束的dom
70
- const StartElem : DomElement = $ ( editor . selection . getSelectionStartElem ( ) )
71
- const EndElem : DomElement = $ ( editor . selection . getSelectionEndElem ( ) )
72
- const childList : NodeListOf < ChildNode > | undefined = editor . selection . getRange ( )
73
- ?. commonAncestorContainer . childNodes
74
- arrayDom_a . push ( this . getDom ( StartElem . elems [ 0 ] ) )
75
- childList ?. forEach ( ( item , index ) => {
76
- if ( item === this . getDom ( StartElem . elems [ 0 ] ) ) {
77
- indexStore . push ( index )
78
- }
79
- if ( item === this . getDom ( EndElem . elems [ 0 ] ) ) {
80
- indexStore . push ( index )
81
- }
82
- } )
83
- //遍历 获取头尾之间的dom元素
84
- let i = 0
85
- let d : HTMLElement
86
- arrayDom_b . push ( this . getDom ( StartElem . elems [ 0 ] ) )
87
- while ( arrayDom_a [ i ] !== this . getDom ( EndElem . elems [ 0 ] ) ) {
88
- d = $ ( arrayDom_a [ i ] . nextElementSibling ) . elems [ 0 ]
89
- if ( allowArray . indexOf ( $ ( d ) . getNodeName ( ) ) !== - 1 ) {
90
- arrayDom_b . push ( d )
91
- arrayDom_a . push ( d )
92
- } else {
93
- arrayDom_a . push ( d )
94
- }
95
- i ++
96
- }
57
+ const selectionStartElem : HTMLElement = $ ( editor . selection . getSelectionStartElem ( ) )
58
+ . elems [ 0 ]
59
+ const SelectionEndElem : HTMLElement = $ ( editor . selection . getSelectionEndElem ( ) ) . elems [ 0 ]
97
60
98
- //设置段落选取 全选
99
- if ( $ ( arrayDom_a [ 0 ] ) . getNodeName ( ) !== 'P' ) {
100
- i = 0
101
- //遍历集合得到第一个p标签的下标
102
- for ( var k = 0 ; k < arrayDom_a . length ; k ++ ) {
103
- if ( $ ( arrayDom_a [ k ] ) . getNodeName ( ) === 'P' ) {
104
- i = k
105
- break
106
- }
107
- }
108
- //i===0 说明选区中没有p段落
109
- if ( i === 0 ) {
110
- return
111
- }
112
- let _i = 0
113
- while ( _i !== i ) {
114
- arrayDom_a . shift ( )
115
- _i ++
61
+ // 获取选区中,在contenteditable下的直接父元素
62
+ const StartElemWrap : HTMLElement = this . getDom ( selectionStartElem )
63
+ const EndElemWrap : HTMLElement = this . getDom ( SelectionEndElem )
64
+
65
+ const containerElemChildren = $containerElem . elems [ 0 ] . children
66
+
67
+ for ( let i = 0 ; i < containerElemChildren . length ; i ++ ) {
68
+ const item : HTMLElement = containerElemChildren [ i ] as HTMLElement
69
+
70
+ // 目前只支持p 段落标签设置行高
71
+ if ( $ ( item ) . getNodeName ( ) !== 'P' ) {
72
+ continue
116
73
}
117
- }
118
- //设置替换的选区
119
- this . setRange ( arrayDom_a [ 0 ] , arrayDom_a [ arrayDom_a . length - 1 ] )
120
- //生成innerHtml html字符串
121
- arrayDom_a . forEach ( item => {
122
- style = item . getAttribute ( 'style' )
123
- styleList = style ? style . split ( ';' ) : [ ]
124
- styleStr = this . styleProcessing ( styleList )
125
-
126
- if ( $ ( item ) . getNodeName ( ) === 'P' ) {
127
- //判断是否 点击默认
128
- if ( value ) {
129
- styleStr += value ? `line-height:${ value } ;` : ''
130
- }
74
+
75
+ if ( item === StartElemWrap ) {
76
+ setStyleLock = true
131
77
}
132
78
133
- if ( ! isIE ) {
134
- st += `<${ $ ( item ) . getNodeName ( ) . toLowerCase ( ) } style="${ styleStr } ">${
135
- item . innerHTML
136
- } </${ $ ( item ) . getNodeName ( ) . toLowerCase ( ) } >`
137
- } else {
79
+ // 证明在区间节点里
80
+ if ( setStyleLock ) {
138
81
$ ( item ) . css ( 'line-height' , value )
139
- }
140
- } )
141
82
142
- if ( st ) {
143
- this . action ( st , editor )
83
+ if ( item === EndElemWrap ) {
84
+ setStyleLock = false
85
+
86
+ // 当设置完选择的EndElemWrap时,就可以退出
87
+ return
88
+ }
89
+ }
144
90
}
145
91
146
- //恢复已选择的选区
147
- dom = $selectionAll . elems [ 0 ]
148
- this . setRange ( dom . children [ indexStore [ 0 ] ] , dom . children [ indexStore [ 1 ] ] )
92
+ //重新设置选区
93
+ editor . selection . createRangeByElems ( selectionStartElem , SelectionEndElem )
94
+
149
95
return
150
96
}
151
97
152
- //遍历dom 获取祖父元素 直到contenteditable属性的div标签
153
- dom = this . getDom ( dom )
98
+ // 单行操作
99
+ // 选中区间的dom元素
100
+ const selectElem = $containerElem . elems [ 0 ]
154
101
155
- //校验允许lineheight设置标签
156
- if ( allowArray . indexOf ( $ ( dom ) . getNodeName ( ) ) === - 1 ) {
157
- return
158
- }
159
- style = dom . getAttribute ( 'style' )
160
- styleList = style ? style . split ( ';' ) : [ ]
161
- //全选 dom下所有的内容
162
- selection ?. selectAllChildren ( dom )
163
- //保存range
164
- editor . selection . saveRange ( )
165
- //判断是否存在value 默认 移除line-height
166
- if ( ! value ) {
167
- if ( style ) {
168
- styleStr = this . styleProcessing ( styleList )
169
- //避免没有其它属性 只留下 ‘style’ 减少代码
170
- if ( styleStr === '' ) {
171
- st = `<${ $ ( dom ) . getNodeName ( ) . toLowerCase ( ) } >${ dom . innerHTML } </${ $ ( dom )
172
- . getNodeName ( )
173
- . toLowerCase ( ) } >`
174
- } else {
175
- st = `<${ $ ( dom ) . getNodeName ( ) . toLowerCase ( ) } style="${ styleStr } ">${
176
- dom . innerHTML
177
- } </${ $ ( dom ) . getNodeName ( ) . toLowerCase ( ) } >`
178
- }
179
- this . action ( st , editor )
180
- }
102
+ // 获取选区中,在contenteditable下的直接父元素
103
+ const selectElemWrapdom = this . getDom ( selectElem )
104
+
105
+ // 目前只支持p 段落标签设置行高
106
+ if ( $ ( selectElemWrapdom ) . getNodeName ( ) !== 'P' ) {
181
107
return
182
108
}
183
- if ( style ) {
184
- //存在style 检索其它style属性
185
- styleStr = this . styleProcessing ( styleList ) + `line-height:${ value } ;`
186
- } else {
187
- styleStr = `line-height:${ value } ;`
188
- }
189
- st = `<${ $ ( dom ) . getNodeName ( ) . toLowerCase ( ) } style="${ styleStr } ">${ dom . innerHTML } </${ $ ( dom )
190
- . getNodeName ( )
191
- . toLowerCase ( ) } >`
192
109
193
- //防止BLOCKQUOTE叠加 or IE下导致P嵌套出现误删
194
- if ( $ ( dom ) . getNodeName ( ) === 'BLOCKQUOTE' || UA . isIE ( ) ) {
195
- $ ( dom ) . css ( 'line-height' , value )
196
- } else {
197
- this . action ( st , editor )
198
- }
110
+ $ ( selectElemWrapdom ) . css ( 'line-height' , value )
111
+
112
+ //重新设置选区
113
+ editor . selection . createRangeByElems ( selectElemWrapdom , selectElemWrapdom )
114
+
115
+ return
199
116
}
200
117
201
118
/**
@@ -220,16 +137,10 @@ class LineHeight extends DropListMenu implements MenuActive {
220
137
return DOM
221
138
}
222
139
223
- /**
224
- * 执行 document.execCommand
225
- *
226
- */
227
- public action ( html_str : string , editor : Editor ) : void {
228
- editor . cmd . do ( 'insertHTML' , html_str )
229
- }
230
-
231
140
/**
232
141
* style 处理
142
+ *
143
+ * 废弃的方法
233
144
*/
234
145
public styleProcessing ( styleList : Array < string > ) : string {
235
146
let styleStr = ''
@@ -243,6 +154,8 @@ class LineHeight extends DropListMenu implements MenuActive {
243
154
244
155
/**
245
156
* 段落全选 比如:避免11变成111
157
+ *
158
+ * 废弃的方法
246
159
*/
247
160
public setRange ( startDom : Node , endDom : Node ) : void {
248
161
const editor = this . editor
0 commit comments