From b808fbcac9915d11f5979e1781ce125327a0e6ab Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Thu, 13 May 2021 10:50:24 +0200 Subject: [PATCH] feat(devtools): allow editing stores from components --- src/devtools/plugin.ts | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/devtools/plugin.ts b/src/devtools/plugin.ts index 2716dc89..7bc527e8 100644 --- a/src/devtools/plugin.ts +++ b/src/devtools/plugin.ts @@ -76,7 +76,7 @@ export function addDevtools(app: App, store: Store) { payload.instanceData.state.push({ type: '🍍 ' + store.$id, key: 'state', - editable: false, + editable: true, value: store.$state, }) @@ -161,6 +161,31 @@ export function addDevtools(app: App, store: Store) { } }) + api.on.editComponentState((payload) => { + if (payload.type.startsWith('🍍')) { + const storeId = payload.type.replace(/^🍍\s*/, '') + const store = registeredStores.get(storeId) + + if (!store) { + return toastMessage(`store "${storeId}" not found`, 'error') + } + + const { path } = payload + if (path[0] !== 'state') { + return toastMessage( + `Invalid path for store "${storeId}":\n${path}\nOnly state can be modified.` + ) + } + + // rewrite the first entry to be able to directly set the state as + // well as any other path + path[0] = '$state' + isTimelineActive = false + payload.set(store, path, payload.state.value) + isTimelineActive = true + } + }) + isAlreadyInstalled = true } else { api.sendInspectorTree(INSPECTOR_ID) -- 2.47.2