Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Probable fix for issue #450 (View refs in vue developers panel) #749

Merged
merged 8 commits into from
Sep 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion cypress/integration/components-tab.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { suite } from '../utils/suite'

const baseInstanceCount = 11
const baseInstanceCount = 12

suite('components tab', () => {
beforeEach(() => cy.reload())
Expand Down Expand Up @@ -122,4 +122,13 @@ suite('components tab', () => {
})
cy.get('.left .search input').clear()
})

it('should display $refs', () => {
cy.get('.instance .item-name').contains('RefTester').click()
cy.get('.right .data-wrapper').then(el => {
expect(el.text()).to.contain('list:Array[4]')
expect(el.text()).to.contain('<li>')
expect(el.text()).to.contain('tester:<p id="testing"')
})
})
})
21 changes: 21 additions & 0 deletions shells/dev/target/RefTester.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<template>
<div id="tester">
<p ref="tester" id="testing" class="test test1">{{ count }}</p>

<ul>
<li
v-for="i in 4"
:key="i"
ref="list"
>{{ i }}</li>
</ul>
</div>
</template>

<script>
export default {
computed: {
count() { return 1 },
}
}
</script>
4 changes: 3 additions & 1 deletion shells/dev/target/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Target from './Target.vue'
import Other from './Other.vue'
import Init from './Init.vue'
import Counter from './Counter.vue'
import RefTester from './RefTester.vue'
import VuexObject from './VuexObject.vue'
import NativeTypes from './NativeTypes.vue'
import Events from './Events.vue'
Expand Down Expand Up @@ -43,7 +44,8 @@ new Vue({
h(Router, { key: [] }),
h(TransitionExample),
h(VuexObject),
h(Init)
h(Init),
h(RefTester)
])
}
}).$mount('#app')
Expand Down
19 changes: 18 additions & 1 deletion src/backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { initEventsBackend } from './events'
import { initRouterBackend } from './router'
import { initPerfBackend } from './perf'
import { findRelatedComponent } from './utils'
import { stringify, classify, camelize, set, parse, getComponentName } from '../util'
import { stringify, classify, camelize, set, parse, getComponentName, getCustomRefDetails } from '../util'
import ComponentSelector from './component-selector'
import SharedData, { init as initSharedData } from 'src/shared-data'
import { isBrowser, target } from 'src/devtools/env'
Expand Down Expand Up @@ -533,6 +533,7 @@ function getInstanceDetails (id) {
function getInstanceState (instance) {
return processProps(instance).concat(
processState(instance),
processRefs(instance),
processComputed(instance),
processInjected(instance),
processRouteContext(instance),
Expand Down Expand Up @@ -678,6 +679,22 @@ function processState (instance) {
}))
}

/**
* Process refs
*
* @param {Vue} instance
* @return {Array}
*/

function processRefs (instance) {
if (Object.keys(instance.$refs).length === 0) {
return []
}
console.log(instance.$refs)
let refs = Object.keys(instance.$refs).map(key => getCustomRefDetails(instance, key, instance.$refs[key]))
return refs.length > 0 ? refs : []
}

/**
* Process the computed properties of an instance.
*
Expand Down
15 changes: 14 additions & 1 deletion src/devtools/components/DataField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,15 @@ export default {
if (this.valueType === 'custom' && this.fieldOptions.file) {
return openInEditor(this.fieldOptions.file)
}
if (this.valueType === 'custom' && this.fieldOptions['type'] === '$refs') {
if (this.$isChrome) {
const evl = `inspect(window.__VUE_DEVTOOLS_INSTANCE_MAP__.get("${this.fieldOptions.uid}").$refs["${this.fieldOptions.key}"])`
console.log(evl)
chrome.devtools.inspectedWindow.eval(evl)
} else {
window.alert('DOM inspection is not supported in this shell.')
}
}

// Default action
this.toggle()
Expand Down Expand Up @@ -629,7 +638,11 @@ export default {
&.type-component-definition
color $green
>>> span
color $darkGrey
color $darkerGrey
&.type-reference
opacity 0.5
>>> .attr-title
color #800080
.vue-ui-dark-mode &
color #bdc6cf
&.string, &.native
Expand Down
23 changes: 23 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,29 @@ export function getCustomFunctionDetails (func) {
}
}

export function getCustomRefDetails (instance, key, ref) {
let value
if (Array.isArray(ref)) {
value = ref.map((r) => getCustomRefDetails(instance, key, r)).map(data => data.value)
} else {
value = {
_custom: {
display: `&lt;${ref.tagName.toLowerCase()}` +
(ref.id ? ` <span class="attr-title">id</span>="${ref.id}"` : '') +
(ref.className ? ` <span class="attr-title">class</span>="${ref.className}"` : '') + '&gt;',
uid: instance.__VUE_DEVTOOLS_UID__,
type: 'reference'
}
}
}
return {
type: '$refs',
key: key,
value,
editable: false
}
}

export function parse (data, revive) {
return revive
? CircularJSON.parse(data, reviver)
Expand Down