Skip to content

Commit 918df59

Browse files
committed
fix(iframe): correct highlighter position
1 parent 62a8e4c commit 918df59

File tree

3 files changed

+60
-13
lines changed
  • packages
    • app-backend-core/src
    • app-backend-vue2/src/components
    • app-backend-vue3/src/components

3 files changed

+60
-13
lines changed

packages/app-backend-core/src/hook.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,25 @@
88
*
99
* @param {Window|global} target
1010
*/
11-
export function installHook (target, iframe = false) {
11+
export function installHook (target, isIframe = false) {
1212
let listeners = {}
1313

1414
let iframeChecks = 0
1515
function injectToIframes () {
1616
const iframes = document.querySelectorAll<HTMLIFrameElement>('iframe')
1717
for (const iframe of iframes) {
1818
try {
19-
if (iframe.getAttribute('data-vdevtools-injection')) continue
20-
iframe.setAttribute('data-vdevtools-injection', 'true')
21-
const script = iframe.contentDocument.createElement('script')
22-
script.textContent = ';(' + installHook.toString() + ')(window, true)'
23-
iframe.contentDocument.documentElement.appendChild(script)
24-
script.parentNode.removeChild(script)
19+
if ((iframe as any).__vdevtools__injected) continue
20+
(iframe as any).__vdevtools__injected = true
21+
const inject = () => {
22+
(iframe.contentWindow as any).__VUE_DEVTOOLS_IFRAME__ = iframe
23+
const script = iframe.contentDocument.createElement('script')
24+
script.textContent = ';(' + installHook.toString() + ')(window, true)'
25+
iframe.contentDocument.documentElement.appendChild(script)
26+
script.parentNode.removeChild(script)
27+
}
28+
inject()
29+
iframe.addEventListener('load', () => inject())
2530
} catch (e) {
2631
// Ignore
2732
}
@@ -40,7 +45,7 @@ export function installHook (target, iframe = false) {
4045

4146
let hook
4247

43-
if (iframe) {
48+
if (isIframe) {
4449
const sendToParent = cb => {
4550
try {
4651
const hook = (window.parent as any).__VUE_DEVTOOLS_GLOBAL_HOOK__

packages/app-backend-vue2/src/components/el.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@ function mergeRects (a, b) {
2525
if (!a.right || b.right > a.right) {
2626
a.right = b.right
2727
}
28+
return a
2829
}
2930

3031
/**
3132
* Get the client rect for an instance.
3233
*/
3334
export function getInstanceOrVnodeRect (instance) {
34-
const el = instance.subTree ? instance.subTree.el : instance.$el || instance.elm
35+
const el = instance.$el || instance.elm
3536

3637
if (!isBrowser) {
3738
// TODO: Find position from instance or a vnode (for functional components).
@@ -43,9 +44,9 @@ export function getInstanceOrVnodeRect (instance) {
4344
}
4445

4546
if (instance._isFragment) {
46-
return getLegacyFragmentRect(instance)
47+
return addIframePosition(getLegacyFragmentRect(instance), getElWindow(instance.$root.$el))
4748
} else if (el.nodeType === 1) {
48-
return el.getBoundingClientRect()
49+
return addIframePosition(el.getBoundingClientRect(), getElWindow(el))
4950
}
5051
}
5152

@@ -95,3 +96,23 @@ export function findRelatedComponent (el) {
9596
}
9697
return el.__vue__
9798
}
99+
100+
function getElWindow (el: HTMLElement) {
101+
return el.ownerDocument.defaultView
102+
}
103+
104+
function addIframePosition (bounds, win: any) {
105+
if (win.__VUE_DEVTOOLS_IFRAME__) {
106+
const rect = mergeRects(createRect(), bounds)
107+
const iframeBounds = win.__VUE_DEVTOOLS_IFRAME__.getBoundingClientRect()
108+
rect.top += iframeBounds.top
109+
rect.bottom += iframeBounds.top
110+
rect.left += iframeBounds.left
111+
rect.right += iframeBounds.left
112+
if (win.parent) {
113+
return addIframePosition(rect, win.parent)
114+
}
115+
return rect
116+
}
117+
return bounds
118+
}

packages/app-backend-vue3/src/components/el.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ export function getInstanceOrVnodeRect (instance) {
4747
}
4848

4949
if (isFragment(instance)) {
50-
return getFragmentRect(instance.subTree)
50+
return addIframePosition(getFragmentRect(instance.subTree), getElWindow(el))
5151
} else if (el.nodeType === 1) {
52-
return el.getBoundingClientRect()
52+
return addIframePosition(el.getBoundingClientRect(), getElWindow(el))
5353
}
5454
}
5555

@@ -78,6 +78,7 @@ function mergeRects (a, b) {
7878
if (!a.right || b.right > a.right) {
7979
a.right = b.right
8080
}
81+
return a
8182
}
8283

8384
let range
@@ -120,3 +121,23 @@ function getFragmentRect (vnode) {
120121

121122
return rect
122123
}
124+
125+
function getElWindow (el: HTMLElement) {
126+
return el.ownerDocument.defaultView
127+
}
128+
129+
function addIframePosition (bounds, win: any) {
130+
if (win.__VUE_DEVTOOLS_IFRAME__) {
131+
const rect = mergeRects(createRect(), bounds)
132+
const iframeBounds = win.__VUE_DEVTOOLS_IFRAME__.getBoundingClientRect()
133+
rect.top += iframeBounds.top
134+
rect.bottom += iframeBounds.top
135+
rect.left += iframeBounds.left
136+
rect.right += iframeBounds.left
137+
if (win.parent) {
138+
return addIframePosition(rect, win.parent)
139+
}
140+
return rect
141+
}
142+
return bounds
143+
}

0 commit comments

Comments
 (0)