-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
el.ts
146 lines (127 loc) · 3.44 KB
/
el.ts
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
import { inDoc, isBrowser } from '@vue-devtools/shared-utils'
import { isFragment } from './util'
export function getComponentInstanceFromElement (element) {
return element.__vueParentComponent
}
export function getRootElementsFromComponentInstance (instance) {
if (isFragment(instance)) {
return getFragmentRootElements(instance.subTree)
}
if (!instance.subTree) return []
return [instance.subTree.el]
}
function getFragmentRootElements (vnode): any[] {
if (!vnode.children) return []
const list = []
for (let i = 0, l = vnode.children.length; i < l; i++) {
const childVnode = vnode.children[i]
if (childVnode.component) {
list.push(...getRootElementsFromComponentInstance(childVnode.component))
} else if (childVnode.el) {
list.push(childVnode.el)
}
}
return list
}
/**
* Get the client rect for an instance.
*
* @param {Vue|Vnode} instance
* @return {Object}
*/
export function getInstanceOrVnodeRect (instance) {
const el = instance.subTree.el
if (!isBrowser) {
// @TODO: Find position from instance or a vnode (for functional components).
return
}
if (!inDoc(el)) {
return
}
if (isFragment(instance)) {
return addIframePosition(getFragmentRect(instance.subTree), getElWindow(el))
} else if (el.nodeType === 1) {
return addIframePosition(el.getBoundingClientRect(), getElWindow(el))
} else if (instance.subTree.component) {
return getInstanceOrVnodeRect(instance.subTree.component)
}
}
function createRect () {
const rect = {
top: 0,
bottom: 0,
left: 0,
right: 0,
get width () { return rect.right - rect.left },
get height () { return rect.bottom - rect.top },
}
return rect
}
function mergeRects (a, b) {
if (!a.top || b.top < a.top) {
a.top = b.top
}
if (!a.bottom || b.bottom > a.bottom) {
a.bottom = b.bottom
}
if (!a.left || b.left < a.left) {
a.left = b.left
}
if (!a.right || b.right > a.right) {
a.right = b.right
}
return a
}
let range
/**
* Get the bounding rect for a text node using a Range.
*
* @param {Text} node
* @return {Rect}
*/
function getTextRect (node) {
if (!isBrowser) return
if (!range) range = document.createRange()
range.selectNode(node)
return range.getBoundingClientRect()
}
function getFragmentRect (vnode) {
const rect = createRect()
if (!vnode.children) return rect
for (let i = 0, l = vnode.children.length; i < l; i++) {
const childVnode = vnode.children[i]
let childRect
if (childVnode.component) {
childRect = getInstanceOrVnodeRect(childVnode.component)
} else if (childVnode.el) {
const el = childVnode.el
if (el.nodeType === 1 || el.getBoundingClientRect) {
childRect = el.getBoundingClientRect()
} else if (el.nodeType === 3 && el.data.trim()) {
childRect = getTextRect(el)
}
}
if (childRect) {
mergeRects(rect, childRect)
}
}
return rect
}
function getElWindow (el: HTMLElement) {
return el.ownerDocument.defaultView
}
function addIframePosition (bounds, win: any) {
if (win.__VUE_DEVTOOLS_IFRAME__) {
const rect = mergeRects(createRect(), bounds)
const iframeBounds = win.__VUE_DEVTOOLS_IFRAME__.getBoundingClientRect()
rect.top += iframeBounds.top
rect.bottom += iframeBounds.top
rect.left += iframeBounds.left
rect.right += iframeBounds.left
if (win.parent) {
return addIframePosition(rect, win.parent)
}
return rect
}
return bounds
}