Skip to content

Commit b0741bc

Browse files
committed
[wip] Added a select to change targets
Recently added recursivity and broke things. I'm not sure about what I should handle to make it work on the dev shell. I haven't tried it yet in a real world scenario, namely in storybook
1 parent 874b22b commit b0741bc

File tree

8 files changed

+114
-8
lines changed

8 files changed

+114
-8
lines changed

shells/dev/iframe.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>iframe</title>
6+
</head>
7+
<body>
8+
<div id="app"></div>
9+
<div id="shadow"></div>
10+
<script src="build/hook.js"></script>
11+
<script src="build/target.js"></script>
12+
</body>
13+
</html>

shells/dev/simple-iframe.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>iframe</title>
6+
</head>
7+
<body>
8+
<script src="https://unpkg.com/vue"></script>
9+
<div id="app">
10+
<h1>My value: {{ counter }}</h1>
11+
<button @click="counter++">+</button>
12+
<button @click="counter--">-</button>
13+
</div>
14+
15+
<script>
16+
new Vue({
17+
el: '#app',
18+
data: {
19+
counter: 0
20+
}
21+
})
22+
</script>
23+
</body>
24+
</html>

shells/dev/target.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
<body>
88
<div id="app"></div>
99
<div id="shadow"></div>
10+
<iframe width="100%" height="400px" src="iframe.html"></iframe>
11+
<iframe width="100%" height="400px" src="simple-iframe.html"></iframe>
1012
<script src="build/hook.js"></script>
1113
<script src="build/target.js"></script>
1214
</body>

src/backend/highlighter.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { inDoc } from '../util'
2+
import { getDocumentTarget } from './target-document'
23

34
const overlay = document.createElement('div')
45
overlay.style.backgroundColor = 'rgba(104, 182, 255, 0.35)'
@@ -26,7 +27,7 @@ export function highlight (instance) {
2627

2728
export function unHighlight () {
2829
if (overlay.parentNode) {
29-
document.body.removeChild(overlay)
30+
getDocumentTarget().body.removeChild(overlay)
3031
}
3132
}
3233

@@ -95,7 +96,7 @@ function getFragmentRect ({ _fragmentStart, _fragmentEnd }) {
9596
* @return {Rect}
9697
*/
9798

98-
const range = document.createRange()
99+
const range = getDocumentTarget().createRange()
99100
function getTextRect (node) {
100101
range.selectNode(node)
101102
return range.getBoundingClientRect()
@@ -112,7 +113,7 @@ function showOverlay ({ width = 0, height = 0, top = 0, left = 0 }) {
112113
overlay.style.height = ~~height + 'px'
113114
overlay.style.top = ~~top + 'px'
114115
overlay.style.left = ~~left + 'px'
115-
document.body.appendChild(overlay)
116+
getDocumentTarget().body.appendChild(overlay)
116117
}
117118

118119
/**

src/backend/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { highlight, unHighlight, getInstanceRect } from './highlighter'
55
import { initVuexBackend } from './vuex'
66
import { initEventsBackend } from './events'
77
import { stringify, classify, camelize } from '../util'
8+
import { getDocumentTarget, setDocumentTarget, getAllTargets } from './target-document'
89
import path from 'path'
910

1011
// Use a custom basename functions instead of the shimed version
@@ -76,6 +77,10 @@ function connect () {
7677
flush()
7778
})
7879

80+
bridge.on('change-target', (target) => {
81+
setDocumentTarget(getAllTargets()[target])
82+
scan()
83+
})
7984
bridge.on('refresh', scan)
8085
bridge.on('enter-instance', id => highlight(instanceMap.get(id)))
8186
bridge.on('leave-instance', unHighlight)
@@ -106,7 +111,7 @@ function scan () {
106111
rootInstances.length = 0
107112
let inFragment = false
108113
let currentFragment = null
109-
walk(document, function (node) {
114+
walk(getDocumentTarget(), function (node) {
110115
if (inFragment) {
111116
if (node === currentFragment._fragmentEnd) {
112117
inFragment = false

src/backend/target-document.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
let target = document
2+
// XXX test
3+
console.log('INIT IT BOY')
4+
5+
export function setDocumentTarget (newTarget) {
6+
target = newTarget
7+
}
8+
9+
export function getDocumentTarget () {
10+
return target
11+
}
12+
13+
export function getAllTargets () {
14+
const targets = [document]
15+
return targets.concat(findTargetsInElement(document))
16+
}
17+
18+
function findTargetsInElement (el) {
19+
const iframes = [...el.getElementsByTagName('iframe')].map(i => i.contentDocument)
20+
return iframes.concat(...iframes.map(findTargetsInElement))
21+
}

src/devtools/App.vue

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
<div class="header">
66
<img class="logo" src="./assets/logo.png" alt="Vue">
77
<span class="message-container">
8-
<transition name="slide-up">
9-
<span class="message" :key="message">{{ message }}</span>
10-
</transition>
8+
<span v-if="false" class="message" :key="message">{{ message }}</span>
9+
<target-selector/>
1110
</span>
1211
<a class="button components"
1312
:class="{ active: tab === 'components'}"
@@ -47,6 +46,7 @@
4746
import ComponentsTab from './views/components/ComponentsTab.vue'
4847
import EventsTab from './views/events/EventsTab.vue'
4948
import VuexTab from './views/vuex/VuexTab.vue'
49+
import TargetSelector from './components/TargetSelector.vue'
5050
5151
import { mapState } from 'vuex'
5252
@@ -62,7 +62,8 @@ export default {
6262
components: {
6363
components: ComponentsTab,
6464
vuex: VuexTab,
65-
events: EventsTab
65+
events: EventsTab,
66+
TargetSelector
6667
},
6768
computed: mapState({
6869
message: state => state.message,
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<template>
2+
<div class="target-selector">
3+
<select v-if="targets.length" @click="refreshTargets" v-model="currentTarget" @change="setTarget">
4+
<option v-for="(target, i) in targets" :value="i"
5+
>{{ target.location.pathname }}</option>
6+
</select>
7+
</div>
8+
</template>
9+
10+
<script>
11+
import {
12+
getDocumentTarget,
13+
setDocumentTarget,
14+
getAllTargets
15+
} from '../../backend/target-document'
16+
17+
export default {
18+
data () {
19+
return {
20+
currentTarget: 0,
21+
targets: []
22+
}
23+
},
24+
25+
created () {
26+
// TODO check this works on safari and firefox
27+
this.refreshTargets()
28+
},
29+
30+
methods: {
31+
refreshTargets () {
32+
this.targets = getAllTargets()
33+
},
34+
setTarget () {
35+
bridge.send('change-target', this.currentTarget)
36+
}
37+
}
38+
}
39+
</script>

0 commit comments

Comments
 (0)