forked from Gitjinfeiyang/easy-canvas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelement.js
438 lines (375 loc) · 13.1 KB
/
element.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import STYLES from './constants'
import pxUtil from './px'
import { isExact, walk, isOuter, parseOuter, walkParent, isEndNode, isAuto, findRelativeTo, needReflow, floor } from './utils'
import Line from './line'
import FlexBox from './flex-box'
import TreeNode from './tree-node'
import completeStyles from './complete-styles'
/**
* Element类实现盒模型以及定位,不具备绘制
* 其他类继承实现
*
*/
export default class Element extends TreeNode {
constructor(options, children) {
super(children)
this.options = Object.assign({ attrs: {}, styles: {}, on: {} }, options)
this.styles = null
this.renderStyles = null
this.x = 0
this.y = 0
this.render = null
this.container = null
this.visible = true
}
init() {
this._initStyles()
this.initEvent()
}
initEvent() {
if (this.options.on) {
Object.keys(this.options.on).forEach(eventName => {
if (this.getLayer().eventManager.EVENTS.includes(eventName)) {
this.getLayer().eventManager.addEventListener(eventName, this.options.on[eventName], this)
}
})
}
}
removeEvent() {
this.getLayer().eventManager.removeElement(this)
}
getLayer() {
return this.root.layer
}
getRender() {
return this.root.layer.render
}
_paint() {
}
mount(layer) {
layer.mountNode(this)
}
_initStyles() {
this.styles = Object.assign({}, this._getDefaultStyles(), this._getParentStyles(this.options.styles), this.options.styles || {})
this._completeStyles()
this._initRenderStyles()
}
_initRenderStyles() {
const renderStyles = { ...this.styles }
const parentWidth = this._getContainerLayout().contentWidth
const parentHeight = this._getContainerLayout().contentHeight
if (isAuto(renderStyles.width)) {
renderStyles.paddingWidth = 0
} else if (isOuter(renderStyles.width)) {
renderStyles.paddingWidth = parseOuter(renderStyles.width) * parentWidth - renderStyles.marginLeft - renderStyles.marginRight
} else {
renderStyles.paddingWidth = renderStyles.width
}
if (isAuto(renderStyles.height)) {
renderStyles.paddingHeight = 0
} else if (isOuter(renderStyles.height)) {
renderStyles.paddingHeight = parseOuter(renderStyles.height) * parentHeight - renderStyles.marginTop - renderStyles.marginBottom
} else {
renderStyles.paddingHeight = renderStyles.height
}
if (!renderStyles.paddingWidth) renderStyles.paddingWidth = 0
if (!renderStyles.paddingHeight) renderStyles.paddingHeight = 0
// 初始化contentWidth
renderStyles.contentWidth = renderStyles.paddingWidth - renderStyles.paddingLeft - renderStyles.paddingRight
renderStyles.contentHeight = renderStyles.paddingHeight - renderStyles.paddingTop - renderStyles.paddingBottom
renderStyles.width = renderStyles.paddingWidth + renderStyles.marginLeft + renderStyles.marginRight + this._getTotalBorderWidth(renderStyles)
renderStyles.height = renderStyles.paddingHeight + renderStyles.marginTop + renderStyles.marginBottom + this._getTotalBorderHeight(renderStyles)
this.renderStyles = renderStyles
if (this._InFlexBox()) {
this._bindFlexBox()
} else if (!this.isInFlow()) {
this.relativeTo = findRelativeTo(this)
}
}
/**
* 需要继承的styles放在这里
*/
_getParentStyles(curStyles) {
let { textAlign, lineHeight, fontSize, color, fontFamily, alignItems, visible = true } = this.parent && this.parent.renderStyles || {}
let extendStyles = {}
if (textAlign) extendStyles.textAlign = textAlign
if (fontSize) extendStyles.fontSize = fontSize
if (color) extendStyles.color = color
if (fontFamily) extendStyles.fontFamily = fontFamily
if (alignItems && !curStyles.alignSelf) extendStyles.alignSelf = alignItems
extendStyles.visible = visible
return extendStyles
}
_completeStyles() {
completeStyles(this)
}
_getDefaultStyles() {
return STYLES.DEFAULT_STYLES
}
// 获取文档流中的子节点
_getChildrenInFlow() {
return this._getChildren().filter(item => item.isInFlow())
}
// 是否在文档流中
isInFlow() {
const { position, display } = this.styles
return position !== STYLES.POSITION.ABSOLUTE && position !== STYLES.POSITION.FIXED
}
isVisible() {
return this.renderStyles.visible && this.visible
}
_generateRender() {
return this
}
getCtx() {
return this.root.layer.ctx
}
/**
* 实现文档流 需要知道上一个兄弟节点
*/
_reflow() {
}
_initWidthHeight() {
const { width, height, display, flex, marginLeft, marginRight, marginTop, marginBottom } = this.styles
if (isAuto(width) || isAuto(height)) {
// 这一步需要遍历,判断一下
const layout = this._measureLayout()
// 初始化宽度高度
if (isAuto(width)) {
this.renderStyles.contentWidth = floor(layout.width)
}
if (isAuto(height)) {
// 不填就是auto
this.renderStyles.contentHeight = floor(layout.height)
}
}
this._refreshLayoutWithContent()
if (this._InFlexBox()) {
this.line.refreshWidthHeight(this)
} else if (display === STYLES.DISPLAY.INLINE_BLOCK) {
// 如果是inline-block 这里仅计算高度
this._bindLine()
}
}
_initPosition() {
let { contentX } = this._getContainerLayout()
const { paddingLeft, paddingTop, borderLeftWidth, borderTopWidth, marginLeft, marginTop } = this.renderStyles
// 初始化ctx位置
if (!this.isInFlow()) {
// 不在文档流中
let { contentX, contentY, contentWidth, contentHeight } = this._getContainerLayout(this.relativeTo)
let { top, bottom, right, left, width, height } = this.renderStyles
if (isOuter(top)) top = parseOuter(top) * contentHeight
if (isOuter(bottom)) bottom = parseOuter(bottom) * contentHeight
if (isOuter(left)) left = parseOuter(left) * contentWidth
if (isOuter(right)) right = parseOuter(right) * contentWidth
if (isExact(top)) {
this.y = contentY + top
} else if (isExact(bottom)) {
this.y = contentY + contentHeight - bottom - height
}
if (isExact(left)) {
this.x = contentX + left
} else if (isExact(right)) {
this.x = contentX + contentWidth - right - width
}
} else if (this._InFlexBox()) {
this.line.refreshElementPosition(this)
} else if (this.renderStyles.display === STYLES.DISPLAY.INLINE_BLOCK) {
// inline-block到line里计算
// this._bindLine()
this.line.refreshElementPosition(this)
} else {
this.x = contentX
this.y = this._getPreLayout().y + this._getPreLayout().height
}
this.x = floor(this.x)
this.y = floor(this.y)
this.contentX = this.x + paddingLeft + borderLeftWidth + marginLeft
this.contentY = this.y + paddingTop + borderTopWidth + marginTop
}
_InFlexBox() {
if (!this.isInFlow()) return false
if (!this.parent) return false
if (this.parent && this.parent.renderStyles.display === STYLES.DISPLAY.FLEX) return true
}
// 父元素根据子元素撑开content后,再计算width
_refreshLayoutWithContent() {
this.renderStyles.height = floor(this.renderStyles.contentHeight + this.renderStyles.paddingTop + this.renderStyles.paddingBottom + this.renderStyles.marginTop + this.renderStyles.marginBottom + this._getTotalBorderHeight())
this.renderStyles.width = floor(this.renderStyles.contentWidth + this.renderStyles.paddingLeft + this.renderStyles.paddingRight + this.renderStyles.marginLeft + this.renderStyles.marginRight + this._getTotalBorderWidth())
this.renderStyles.paddingWidth = floor(this.renderStyles.contentWidth + this.renderStyles.paddingLeft + this.renderStyles.paddingRight)
this.renderStyles.paddingHeight = floor(this.renderStyles.contentHeight + this.renderStyles.paddingTop + this.renderStyles.paddingBottom)
}
// 父元素根据子元素撑开content后,再计算width
_refreshContentWithLayout() {
this.renderStyles.contentHeight = this.renderStyles.height - this.renderStyles.paddingTop - this.renderStyles.paddingBottom - this.renderStyles.marginTop - this.renderStyles.marginBottom - this._getTotalBorderHeight()
this.renderStyles.contentWidth = this.renderStyles.width - this.renderStyles.paddingLeft - this.renderStyles.paddingRight - this.renderStyles.marginLeft - this.renderStyles.marginRight - this._getTotalBorderWidth()
this.renderStyles.paddingWidth = floor(this.renderStyles.contentWidth + this.renderStyles.paddingLeft + this.renderStyles.paddingRight)
this.renderStyles.paddingHeight = floor(this.renderStyles.contentHeight + this.renderStyles.paddingTop + this.renderStyles.paddingBottom)
}
_getTotalBorderWidth(renderStyles = this.renderStyles) {
return renderStyles.borderLeftWidth + renderStyles.borderRightWidth
}
_getTotalBorderHeight(renderStyles = this.renderStyles) {
return renderStyles.borderTopWidth + renderStyles.borderBottomWidth
}
_bindLine() {
if (this.pre && this.pre.line && this.pre.line.canIEnter(this)) {
this.pre.line.add(this)
} else {
// 新行
new Line().bind(this)
}
}
_bindFlexBox() {
if (this.pre && this.pre.line) {
this.pre.line.add(this)
} else {
// 新行
new FlexBox().bind(this)
}
}
_getContainerLayout(container = this.parent) {
if (!container) {
// root
if (!this.container) {
debugger
}
container = {
renderStyles: {
width: this.container.width,
height: this.container.height,
paddingTop: 0,
paddingBottom: 0,
paddingLeft: 0,
paddingRight: 0,
marginLeft: 0,
marginRight: 0,
marginTop: 0,
marginBottom: 0,
contentWidth: this.container.width,
contentHeight: this.container.height
},
x: 0,
y: 0,
contentX: 0,
contentY: 0
}
}
return {
width: container.renderStyles.width,
height: container.renderStyles.height,
x: container.x,
y: container.y,
paddingTop: container.renderStyles.paddingTop,
paddingBottom: container.renderStyles.paddingBottom,
paddingLeft: container.renderStyles.paddingLeft,
paddingRight: container.renderStyles.paddingRight,
marginLeft: container.renderStyles.marginLeft,
marginRight: container.renderStyles.marginRight,
marginTop: container.renderStyles.marginTop,
marginBottom: container.renderStyles.marginBottom,
contentX: container.contentX,
contentY: container.contentY,
contentWidth: container.renderStyles.contentWidth,
contentHeight: container.renderStyles.contentHeight
}
}
// 这里前一个节点必须在文档流中
_getPreLayout() {
let cur = this.pre
while (cur && !cur.isInFlow()) {
cur = cur.pre
}
// 如果没有前一个或者前面的都不在文档流中,获取容器的
if (cur) {
return {
width: cur.renderStyles.width,
height: cur.renderStyles.height,
x: cur.x,
y: cur.y
}
} else {
return {
width: 0,
height: 0,
x: this._getContainerLayout().contentX,
y: this._getContainerLayout().contentY
}
}
}
// 计算自身的高度
_measureLayout() {
let width = 0 // 需要考虑原本的宽度
let height = 0
this._getChildrenInFlow().forEach(child => {
if (child.line) {
if (child.line.start === child) {
if (child.line.width > width) {
width = child.line.width
}
height += child.line.height
}
} else if (child.renderStyles.width > width) {
width = child.renderStyles.width
height += child.renderStyles.height
} else {
height += child.renderStyles.height
}
})
return { width, height }
}
// 获取元素,只会找该元素子级
getElementBy(key, value) {
let match = []
walk(this, (element) => {
if (element.options.attrs[key] === value) {
match.push(element)
}
})
return match
}
// 添加在最后
appendChild(element) {
super.appendChild(element)
this.getLayer().onElementAdd(element)
return element
}
//
prependChild(element) {
super.prependChild(element)
this.getLayer().onElementAdd(element)
return element
}
removeChild(element) {
super.removeChild(element)
this.getLayer().onElementRemove(element)
}
append(element) {
super.append(element)
this.getLayer().onElementAdd(element)
}
prepend(element) {
super.prepend(element)
this.getLayer().onElementAdd(element)
}
setStyles(styles) {
let _needReflow = false
Object.keys(styles).forEach(key => {
if (needReflow(key)) {
_needReflow = true
} else {
this.renderStyles[key] = styles[key]
}
})
if (_needReflow) {
Object.keys(styles).forEach(key => {
this.options.styles[key] = styles[key]
})
this.getLayer().reflowElement(this, this)
// console.warn('实验性功能')
} else {
this.getRender().requestRepaint()
}
}
}