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

React Devtools Component: Add hotkeys to start inspecting node #17473

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/react-devtools-extensions/chrome/manifest.json
Expand Up @@ -48,5 +48,15 @@
"js": ["build/injectGlobalHook.js"],
"run_at": "document_start"
}
]
],

"commands": {
"inspect_node": {
"suggested_key": {
"default": "Ctrl+Shift+X",
"mac": "Command+Shift+X"
},
"description": "Toggle on element/node inspector on React Devtools Component"
}
}
}
12 changes: 11 additions & 1 deletion packages/react-devtools-extensions/firefox/manifest.json
Expand Up @@ -52,5 +52,15 @@
"js": ["build/injectGlobalHook.js"],
"run_at": "document_start"
}
]
],

"commands": {
"inspect_node": {
"suggested_key": {
"default": "Ctrl+Shift+X",
"mac": "Command+Shift+X"
},
"description": "Toggle on element/node inspector on React Devtools Component"
}
}
}
20 changes: 20 additions & 0 deletions packages/react-devtools-extensions/src/backend.js
Expand Up @@ -80,4 +80,24 @@ function setup(hook) {
hook.nativeStyleEditorValidAttributes,
);
}

// listen message coming from content script
window.addEventListener('message', event => {
if (
!event.data ||
event.data.source !== 'react-devtools-content-script' ||
!event.data.payload ||
event.data.payload.type !== 'command' ||
!event.data.payload.command
) {
return;
}
switch (event.data.payload.command) {
case 'inspect_node':
agent.startInspectingNative();
return;
default:
return;
}
});
}
16 changes: 16 additions & 0 deletions packages/react-devtools-extensions/src/background.js
Expand Up @@ -113,3 +113,19 @@ chrome.runtime.onMessage.addListener((request, sender) => {
}
}
});

// Add hotkeys/commands here
chrome.commands.onCommand.addListener(command => {
switch (command) {
case 'inspect_node': {
// this specific hotkey need to activate a function within the page scripts
// we need signal the action to content script first and then passed it on to page script
chrome.tabs.query({active: true, currentWindow: true}, tabs => {
chrome.tabs.sendMessage(tabs[0].id, {command: 'inspect_node'});
});
return;
}
default:
return;
}
});
18 changes: 18 additions & 0 deletions packages/react-devtools-extensions/src/contentScript.js
Expand Up @@ -77,3 +77,21 @@ if (!backendInitialized) {
}
}, 500);
}

// listen message coming from background script
chrome.runtime.onMessage.addListener(request => {
// If the message is related to hotkeys / command being triggered
if (request.command) {
// Pass hotkeys / command to page scripts since hotkeys is usually have something to do with the devtools itself
window.postMessage(
{
source: 'react-devtools-content-script',
payload: {
type: 'command',
command: request.command,
},
},
'*',
);
}
});
2 changes: 2 additions & 0 deletions packages/react-devtools-shared/src/backend/agent.js
Expand Up @@ -100,6 +100,8 @@ export default class Agent extends EventEmitter<{|
_persistedSelection: PersistedSelection | null = null;
_persistedSelectionMatch: PathMatch | null = null;
_traceUpdatesEnabled: boolean = false;
startInspectingNative: () => void;
stopInspectingNative: () => void;

constructor(bridge: BackendBridge) {
super();
Expand Down
Expand Up @@ -34,6 +34,10 @@ export default function setupHighlighter(
bridge.addListener('startInspectingNative', startInspectingNative);
bridge.addListener('stopInspectingNative', stopInspectingNative);

// Agent not only listen but should also be able to carry out these actions directly
agent.startInspectingNative = startInspectingNative;
agent.stopInspectingNative = stopInspectingNative;

function startInspectingNative() {
registerListenersOnWindow(window);
}
Expand Down
Expand Up @@ -43,7 +43,7 @@ export default function InspectHostNodesToggle() {
<Toggle
onChange={handleChange}
isChecked={isInspecting}
title="Select an element in the page to inspect it">
title="Select an element in the page to inspect it (Cmd/Ctrl Shift X)">
<ButtonIcon type="search" />
</Toggle>
);
Expand Down