Skip to content

Commit ab61a73

Browse files
hootlexAkryum
authored andcommitted
feat: Editable vuex state (#724)
* make vuex state editable * add test for updating the stage * fix: don't clear history on edit * chore: misc * test: split vuex edit * test: fix e2e
1 parent 9fc2c9d commit ab61a73

File tree

10 files changed

+118
-14
lines changed

10 files changed

+118
-14
lines changed

cypress/integration/vuex-edit.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { suite } from '../utils/suite'
2+
3+
suite('vuex edit', () => {
4+
it('should edit state', () => {
5+
cy.get('.vuex-tab').click()
6+
// using the decrease button
7+
cy.get('.data-field').eq(0)
8+
.find('.actions .vue-ui-button').eq(1)
9+
.click({ force: true })
10+
.click({ force: true })
11+
12+
cy.get('#target').iframe().then(({ get }) => {
13+
get('#counter p').contains('-2')
14+
})
15+
16+
// using the increase button
17+
cy.get('.data-field').eq(0).click()
18+
.find('.actions .vue-ui-button').eq(2)
19+
.click({ force: true })
20+
.click({ force: true })
21+
22+
cy.get('#target').iframe().then(({ get }) => {
23+
get('#counter p').contains('0')
24+
})
25+
26+
// using the edit input
27+
cy.get('.data-field').eq(0).click()
28+
.find('.actions .vue-ui-button').eq(0).click({ force: true })
29+
cy.get('.edit-input').type('12')
30+
cy.get('.edit-overlay > .actions > :nth-child(2) > .content > .vue-ui-icon').click()
31+
32+
cy.get('#target').iframe().then(({ get }) => {
33+
get('#counter p').contains('12')
34+
})
35+
36+
// change count back to 1
37+
cy.get('.data-field').eq(0).click()
38+
.find('.actions .vue-ui-button').eq(0).click({ force: true })
39+
cy.get('.edit-input').type('0')
40+
cy.get('.edit-overlay > .actions > :nth-child(2) > .content > .vue-ui-icon').click()
41+
42+
cy.get('#target').iframe().then(({ get }) => {
43+
get('#counter p').contains('0')
44+
})
45+
})
46+
})

shells/dev/target/NativeTypes.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const proxy1 = new Proxy(sum, handler)
6060
export default {
6161
components: {
6262
TestComponent: {
63-
props: { bar: { default: 'hey' }},
63+
props: { bar: { default: 'hey' } },
6464
data: () => ({ foo: '42' }),
6565
computed: {
6666
parentComp () { return this.$parent }

src/backend/vuex.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { stringify, parse } from 'src/util'
22
import SharedData from 'src/shared-data'
3+
import { set } from '../util'
34

45
export function initVuexBackend (hook, bridge) {
56
const store = hook.store
@@ -159,6 +160,18 @@ export function initVuexBackend (hook, bridge) {
159160
return resultState
160161
}
161162

163+
bridge.on('vuex:edit-state', ({ index, value, path }) => {
164+
let parsedValue
165+
if (value) {
166+
parsedValue = parse(value, true)
167+
}
168+
set(store.state, path, parsedValue)
169+
bridge.send('vuex:inspected-state', {
170+
index,
171+
snapshot: getSnapshot()
172+
})
173+
})
174+
162175
function takeSnapshot (index) {
163176
snapshots.push({
164177
index,

src/devtools/components/DataField.vue

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@
182182
:removable="isSubfieldsEditable"
183183
:renamable="editable && valueType === 'plain-object'"
184184
:force-collapse="forceCollapse"
185+
:is-state-field="isStateField"
185186
/>
186187
<span
187188
v-if="formattedSubFields.length > limit"
@@ -201,6 +202,7 @@
201202
:force-collapse="forceCollapse"
202203
editable
203204
removable
205+
:is-state-field="isStateField"
204206
@cancel-edit="addingValue = false"
205207
@submit-edit="addingValue = false"
206208
/>
@@ -261,6 +263,10 @@ export default {
261263
forceCollapse: {
262264
type: String,
263265
default: null
266+
},
267+
isStateField: {
268+
type: Boolean,
269+
default: false
264270
}
265271
},
266272

src/devtools/components/StateInspector.vue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
:path="field.key"
3636
:editable="field.editable"
3737
:force-collapse="forceCollapse"
38+
:is-state-field="isStateField(field)"
3839
/>
3940
</template>
4041
<template v-else>
@@ -140,6 +141,10 @@ export default {
140141
this.forceCollapse = value ? 'expand' : 'collapse'
141142
Vue.set(this.expandedState, key, value)
142143
})
144+
},
145+
146+
isStateField (field) {
147+
return field && field.type === 'state'
143148
}
144149
}
145150
}

src/devtools/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,10 @@ function initApp (shell) {
141141
bridge.on('vuex:inspected-state', ({ index, snapshot }) => {
142142
snapshotsCache.set(index, snapshot)
143143
store.commit('vuex/RECEIVE_STATE', snapshot)
144-
if (store.state.vuex.inspectedIndex === index) {
144+
145+
if (index === -1) {
146+
store.commit('vuex/UPDATE_BASE_STATE', snapshot)
147+
} else if (store.state.vuex.inspectedIndex === index) {
145148
store.commit('vuex/UPDATE_INSPECTED_STATE', snapshot)
146149
}
147150

src/devtools/mixins/data-field-edit.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,18 @@ export default {
167167
},
168168

169169
sendEdit (args) {
170-
bridge.send('set-instance-data', {
171-
id: this.inspectedInstance.id,
172-
path: this.path,
173-
...args
174-
})
170+
if (this.isStateField) {
171+
this.$store.dispatch('vuex/editState', {
172+
path: this.path,
173+
args
174+
})
175+
} else {
176+
bridge.send('set-instance-data', {
177+
id: this.inspectedInstance.id,
178+
path: this.path,
179+
...args
180+
})
181+
}
175182
},
176183

177184
transformSpecialTokens (str, display) {

src/devtools/views/vuex/VuexStateInspector.vue

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,25 @@ export default {
131131
filteredState () {
132132
const inspectedState = [].concat(
133133
...Object.keys(this.inspectedState).map(
134-
type => Object.keys(this.inspectedState[type]).map(
135-
key => ({
136-
key,
137-
type,
138-
value: this.inspectedState[type][key]
139-
})
140-
)
134+
type => {
135+
let processedState
136+
if (!Array.isArray(this.inspectedState[type])) {
137+
processedState = Object.keys(this.inspectedState[type]).map(key => ({
138+
key,
139+
editable: type === 'state',
140+
value: this.inspectedState[type][key]
141+
}))
142+
} else {
143+
processedState = this.inspectedState[type]
144+
}
145+
146+
return processedState.map(
147+
item => ({
148+
type,
149+
...item
150+
})
151+
)
152+
}
141153
)
142154
)
143155

src/devtools/views/vuex/actions.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ export function updateFilter ({ commit }, filter) {
7676
commit('UPDATE_FILTER', filter)
7777
}
7878

79+
export function editState ({ state }, { path, args }) {
80+
bridge.send('vuex:edit-state', {
81+
index: state.inspectedIndex,
82+
path,
83+
...args
84+
})
85+
}
86+
7987
function travelTo (state, commit, index, apply = true) {
8088
return new Promise((resolve) => {
8189
Resolve.travel = resolve

src/devtools/views/vuex/module.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ const mutations = {
7272
state.lastReceivedState = value
7373
},
7474

75+
'UPDATE_BASE_STATE' (state, value) {
76+
state.base = value
77+
},
78+
7579
'TIME_TRAVEL' (state, index) {
7680
state.activeIndex = index
7781
},

0 commit comments

Comments
 (0)