File size: 3,067 Bytes
4d70170 |
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 |
import { inDoc, isBrowser, target } from '@vue-devtools/shared-utils'
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
}
/**
* Get the client rect for an instance.
*/
export function getInstanceOrVnodeRect(instance) {
const el = instance.$el || instance.elm
if (!isBrowser) {
// TODO: Find position from instance or a vnode (for functional components).
return
}
if (!inDoc(el)) {
return
}
if (instance._isFragment) {
return addIframePosition(getLegacyFragmentRect(instance), getElWindow(instance.$root.$el))
}
else if (el.nodeType === 1) {
return addIframePosition(el.getBoundingClientRect(), getElWindow(el))
}
}
/**
* Highlight a fragment instance.
* Loop over its node range and determine its bounding box.
*/
function getLegacyFragmentRect({ _fragmentStart, _fragmentEnd }) {
const rect = createRect()
util().mapNodeRange(_fragmentStart, _fragmentEnd, (node) => {
let childRect
if (node.nodeType === 1 || node.getBoundingClientRect) {
childRect = node.getBoundingClientRect()
}
else if (node.nodeType === 3 && node.data.trim()) {
childRect = getTextRect(node)
}
if (childRect) {
mergeRects(rect, childRect)
}
})
return rect
}
let range: Range
/**
* Get the bounding rect for a text node using a Range.
*/
function getTextRect(node: Text) {
if (!isBrowser) {
return
}
if (!range) {
range = document.createRange()
}
range.selectNode(node)
return range.getBoundingClientRect()
}
/**
* Get Vue's util
*/
function util() {
return target.__VUE_DEVTOOLS_GLOBAL_HOOK__.Vue.util
}
export function findRelatedComponent(el) {
while (!el.__vue__ && el.parentElement) {
el = el.parentElement
}
return el.__vue__
}
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
}
export function getRootElementsFromComponentInstance(instance) {
if (instance._isFragment) {
const list = []
const { _fragmentStart, _fragmentEnd } = instance
util().mapNodeRange(_fragmentStart, _fragmentEnd, (node) => {
list.push(node)
})
return list
}
return [instance.$el]
}
|