Skip to content

Commit c309065

Browse files
1099511627776Akryum
authored andcommitted
feat: View refs in vue developers panel (vuejs#749)
* Probable fix for issue vuejs#450 * Removed unused code * Added test components and some styles * fixed eslint warnings * npm run test now runs without errors * fix: support refs lists * test: e2e
1 parent 0e8ef31 commit c309065

File tree

6 files changed

+89
-4
lines changed

6 files changed

+89
-4
lines changed

cypress/integration/components-tab.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { suite } from '../utils/suite'
22

3-
const baseInstanceCount = 11
3+
const baseInstanceCount = 12
44

55
suite('components tab', () => {
66
beforeEach(() => cy.reload())
@@ -122,4 +122,13 @@ suite('components tab', () => {
122122
})
123123
cy.get('.left .search input').clear()
124124
})
125+
126+
it('should display $refs', () => {
127+
cy.get('.instance .item-name').contains('RefTester').click()
128+
cy.get('.right .data-wrapper').then(el => {
129+
expect(el.text()).to.contain('list:Array[4]')
130+
expect(el.text()).to.contain('<li>')
131+
expect(el.text()).to.contain('tester:<p id="testing"')
132+
})
133+
})
125134
})

shells/dev/target/RefTester.vue

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<template>
2+
<div id="tester">
3+
<p ref="tester" id="testing" class="test test1">{{ count }}</p>
4+
5+
<ul>
6+
<li
7+
v-for="i in 4"
8+
:key="i"
9+
ref="list"
10+
>{{ i }}</li>
11+
</ul>
12+
</div>
13+
</template>
14+
15+
<script>
16+
export default {
17+
computed: {
18+
count() { return 1 },
19+
}
20+
}
21+
</script>

shells/dev/target/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Target from './Target.vue'
44
import Other from './Other.vue'
55
import Init from './Init.vue'
66
import Counter from './Counter.vue'
7+
import RefTester from './RefTester.vue'
78
import VuexObject from './VuexObject.vue'
89
import NativeTypes from './NativeTypes.vue'
910
import Events from './Events.vue'
@@ -43,7 +44,8 @@ new Vue({
4344
h(Router, { key: [] }),
4445
h(TransitionExample),
4546
h(VuexObject),
46-
h(Init)
47+
h(Init),
48+
h(RefTester)
4749
])
4850
}
4951
}).$mount('#app')

src/backend/index.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { initEventsBackend } from './events'
77
import { initRouterBackend } from './router'
88
import { initPerfBackend } from './perf'
99
import { findRelatedComponent } from './utils'
10-
import { stringify, classify, camelize, set, parse, getComponentName } from '../util'
10+
import { stringify, classify, camelize, set, parse, getComponentName, getCustomRefDetails } from '../util'
1111
import ComponentSelector from './component-selector'
1212
import SharedData, { init as initSharedData } from 'src/shared-data'
1313
import { isBrowser, target } from 'src/devtools/env'
@@ -533,6 +533,7 @@ function getInstanceDetails (id) {
533533
function getInstanceState (instance) {
534534
return processProps(instance).concat(
535535
processState(instance),
536+
processRefs(instance),
536537
processComputed(instance),
537538
processInjected(instance),
538539
processRouteContext(instance),
@@ -678,6 +679,22 @@ function processState (instance) {
678679
}))
679680
}
680681

682+
/**
683+
* Process refs
684+
*
685+
* @param {Vue} instance
686+
* @return {Array}
687+
*/
688+
689+
function processRefs (instance) {
690+
if (Object.keys(instance.$refs).length === 0) {
691+
return []
692+
}
693+
console.log(instance.$refs)
694+
let refs = Object.keys(instance.$refs).map(key => getCustomRefDetails(instance, key, instance.$refs[key]))
695+
return refs.length > 0 ? refs : []
696+
}
697+
681698
/**
682699
* Process the computed properties of an instance.
683700
*

src/devtools/components/DataField.vue

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,15 @@ export default {
471471
if (this.valueType === 'custom' && this.fieldOptions.file) {
472472
return openInEditor(this.fieldOptions.file)
473473
}
474+
if (this.valueType === 'custom' && this.fieldOptions['type'] === '$refs') {
475+
if (this.$isChrome) {
476+
const evl = `inspect(window.__VUE_DEVTOOLS_INSTANCE_MAP__.get("${this.fieldOptions.uid}").$refs["${this.fieldOptions.key}"])`
477+
console.log(evl)
478+
chrome.devtools.inspectedWindow.eval(evl)
479+
} else {
480+
window.alert('DOM inspection is not supported in this shell.')
481+
}
482+
}
474483
475484
// Default action
476485
this.toggle()
@@ -629,7 +638,11 @@ export default {
629638
&.type-component-definition
630639
color $green
631640
>>> span
632-
color $darkGrey
641+
color $darkerGrey
642+
&.type-reference
643+
opacity 0.5
644+
>>> .attr-title
645+
color #800080
633646
.vue-ui-dark-mode &
634647
color #bdc6cf
635648
&.string, &.native

src/util.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,29 @@ export function getCustomFunctionDetails (func) {
269269
}
270270
}
271271

272+
export function getCustomRefDetails (instance, key, ref) {
273+
let value
274+
if (Array.isArray(ref)) {
275+
value = ref.map((r) => getCustomRefDetails(instance, key, r)).map(data => data.value)
276+
} else {
277+
value = {
278+
_custom: {
279+
display: `&lt;${ref.tagName.toLowerCase()}` +
280+
(ref.id ? ` <span class="attr-title">id</span>="${ref.id}"` : '') +
281+
(ref.className ? ` <span class="attr-title">class</span>="${ref.className}"` : '') + '&gt;',
282+
uid: instance.__VUE_DEVTOOLS_UID__,
283+
type: 'reference'
284+
}
285+
}
286+
}
287+
return {
288+
type: '$refs',
289+
key: key,
290+
value,
291+
editable: false
292+
}
293+
}
294+
272295
export function parse (data, revive) {
273296
return revive
274297
? CircularJSON.parse(data, reviver)

0 commit comments

Comments
 (0)