Skip to content

Commit

Permalink
feat(api): node actions for custom inspector (#1820)
Browse files Browse the repository at this point in the history
Co-authored-by: Guillaume Chau <guillaume.b.chau@gmail.com>
  • Loading branch information
DamianOsipiuk and Akryum committed Mar 29, 2022
1 parent e9398cd commit 22a1397
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 3 deletions.
5 changes: 5 additions & 0 deletions packages/api/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ export interface CustomInspectorOptions {
tooltip?: string
action: () => void | Promise<void>
}[]
nodeActions?: {
icon: string
tooltip?: string
action: (nodeId: string) => void | Promise<void>
}[]
}

export interface CustomInspectorNode {
Expand Down
6 changes: 3 additions & 3 deletions packages/app-backend-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -609,12 +609,12 @@ function connectBridge () {
}
})

ctx.bridge.on(BridgeEvents.TO_BACK_CUSTOM_INSPECTOR_ACTION, async ({ inspectorId, appId, actionIndex }) => {
ctx.bridge.on(BridgeEvents.TO_BACK_CUSTOM_INSPECTOR_ACTION, async ({ inspectorId, appId, actionIndex, actionType, args }) => {
const inspector = await getInspectorWithAppId(inspectorId, appId, ctx)
if (inspector) {
const action = inspector.actions[actionIndex]
const action = inspector[actionType ?? 'actions'][actionIndex]
try {
await action.action()
await action.action(...(args ?? []))
} catch (e) {
if (SharedData.debugInfo) {
console.error(e)
Expand Down
4 changes: 4 additions & 0 deletions packages/app-backend-core/src/inspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ export async function sendCustomInspectors (ctx: BackendContext) {
icon: a.icon,
tooltip: a.tooltip,
})),
nodeActions: i.nodeActions?.map(a => ({
icon: a.icon,
tooltip: a.tooltip,
})),
})
}
ctx.bridge.send(BridgeEvents.TO_FRONT_CUSTOM_INSPECTOR_LIST, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import EmptyPane from '@front/features/layout/EmptyPane.vue'
import { watch, defineComponent, ref } from '@vue/composition-api'
import { BridgeEvents } from '@vue-devtools/shared-utils'
import { useBridge } from '@front/features/bridge'
import { useCurrentInspector } from './composable'
import StateInspector from '../StateInspector.vue'
Expand Down Expand Up @@ -32,11 +34,29 @@ export default defineComponent({
}
})
// Custom actions
const {
bridge,
} = useBridge()
function executeCustomAction (index: number) {
bridge.send(BridgeEvents.TO_BACK_CUSTOM_INSPECTOR_ACTION, {
inspectorId: inspector.value.id,
appId: inspector.value.appId,
actionIndex: index,
actionType: 'nodeActions',
args: [
inspector.value.selectedNodeId,
],
})
}
return {
inspector,
filteredState,
editState,
stateInspector,
executeCustomAction,
}
},
})
Expand All @@ -58,6 +78,15 @@ export default defineComponent({
:placeholder="inspector.stateFilterPlaceholder || 'Filter state...'"
class="search flex-1 flat ml-2"
/>

<VueButton
v-for="(action, index) of inspector.nodeActions"
:key="index"
v-tooltip="action.tooltip"
class="icon-button flat"
:icon-left="action.icon"
@click="executeCustomAction(index)"
/>
</div>

<StateInspector
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export interface InspectorFromBackend {
icon: string
tooltip?: string
}[]
nodeActions?: {
icon: string
tooltip?: string
}[]
}

export interface Inspector extends InspectorFromBackend {
Expand Down
1 change: 1 addition & 0 deletions packages/shared-utils/src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export enum BridgeEvents {
TO_FRONT_CUSTOM_INSPECTOR_STATE = 'f:custom-inspector:state',
TO_BACK_CUSTOM_INSPECTOR_EDIT_STATE = 'b:custom-inspector:edit-state',
TO_BACK_CUSTOM_INSPECTOR_ACTION = 'b:custom-inspector:action',
TO_BACK_CUSTOM_INSPECTOR_NODE_ACTION = 'b:custom-inspector:node-action',
TO_FRONT_CUSTOM_INSPECTOR_SELECT_NODE = 'f:custom-inspector:select-node',

// Custom state
Expand Down
10 changes: 10 additions & 0 deletions packages/shell-dev-vue3/src/devtools-plugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,16 @@ export default {
},
},
],
nodeActions: [
{
icon: 'help',
tooltip: 'Test custom node action',
action: (arg1) => {
console.log('Node action', arg1)
api.selectInspectorNode('test-inspector', 'child')
},
},
],
})

api.addInspector({
Expand Down

0 comments on commit 22a1397

Please sign in to comment.