Skip to content

Commit

Permalink
Devtools renable copy attr context menu for firefox (#17740)
Browse files Browse the repository at this point in the history
* Use exportFunction() to share clipboard copy with JS running in document/page context.

* Remove no-longer-used option to disable copy operation.
  • Loading branch information
Brian Vaughn authored Dec 29, 2019
1 parent 2b903da commit 22ef96a
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 23 deletions.
2 changes: 1 addition & 1 deletion packages/react-devtools-extensions/firefox/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"scripts": ["build/background.js"]
},

"permissions": ["file:///*", "http://*/*", "https://*/*"],
"permissions": ["file:///*", "http://*/*", "https://*/*", "clipboardWrite"],

"content_scripts": [
{
Expand Down
16 changes: 16 additions & 0 deletions packages/react-devtools-extensions/src/injectGlobalHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,19 @@ if (document.contentType === 'text/html') {
detectReact,
);
}

if (typeof exportFunction === 'function') {
// eslint-disable-next-line no-undef
exportFunction(
text => {
// Call clipboard.writeText from the extension content script
// (as it has the clipboardWrite permission) and return a Promise
// accessible to the webpage js code.
return new window.Promise((resolve, reject) =>
window.navigator.clipboard.writeText(text).then(resolve, reject),
);
},
window.wrappedJSObject.__REACT_DEVTOOLS_GLOBAL_HOOK__,
{defineAs: 'clipboardCopyText'},
);
}
1 change: 0 additions & 1 deletion packages/react-devtools-extensions/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ function createPanelIfReactLoaded() {
browserTheme: getBrowserTheme(),
componentsPortalContainer,
enabledInspectedElementContextMenu: true,
enabledInspectedElementContextMenuCopy: isChrome,
overrideTab,
profilerPortalContainer,
showTabBar: false,
Expand Down
14 changes: 13 additions & 1 deletion packages/react-devtools-shared/src/backend/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,19 @@ export function cleanForBridge(

export function copyToClipboard(value: any): void {
const safeToCopy = serializeToString(value);
copy(safeToCopy === undefined ? 'undefined' : safeToCopy);
const text = safeToCopy === undefined ? 'undefined' : safeToCopy;
const {clipboardCopyText} = window.__REACT_DEVTOOLS_GLOBAL_HOOK__;

// On Firefox navigator.clipboard.writeText has to be called from
// the content script js code (because it requires the clipboardWrite
// permission to be allowed out of a "user handling" callback),
// clipboardCopyText is an helper injected into the page from.
// injectGlobalHook.
if (typeof clipboardCopyText === 'function') {
clipboardCopyText(text).catch(err => {});
} else {
copy(text);
}
}

export function copyWithSet(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,6 @@ function InspectedElementView({

const {
isEnabledForInspectedElement,
supportsCopyOperation,
viewAttributeSourceFunction,
} = useContext(ContextMenuContext);

Expand Down Expand Up @@ -445,14 +444,12 @@ function InspectedElementView({
<ContextMenu id="SelectedElement">
{data => (
<Fragment>
{supportsCopyOperation && (
<ContextMenuItem
onClick={() => copyInspectedElementPath(id, data.path)}
title="Copy value to clipboard">
<Icon className={styles.ContextMenuIcon} type="copy" /> Copy
value to clipboard
</ContextMenuItem>
)}
<ContextMenuItem
onClick={() => copyInspectedElementPath(id, data.path)}
title="Copy value to clipboard">
<Icon className={styles.ContextMenuIcon} type="copy" /> Copy
value to clipboard
</ContextMenuItem>
<ContextMenuItem
onClick={() => storeAsGlobal(id, data.path)}
title="Store as global variable">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ export type Props = {|
canViewElementSourceFunction?: ?CanViewElementSource,
defaultTab?: TabID,
enabledInspectedElementContextMenu?: boolean,
enabledInspectedElementContextMenuCopy?: boolean,
showTabBar?: boolean,
store: Store,
warnIfLegacyBackendDetected?: boolean,
Expand Down Expand Up @@ -97,7 +96,6 @@ export default function DevTools({
componentsPortalContainer,
defaultTab = 'components',
enabledInspectedElementContextMenu = false,
enabledInspectedElementContextMenuCopy = false,
overrideTab,
profilerPortalContainer,
showTabBar = false,
Expand All @@ -123,14 +121,9 @@ export default function DevTools({
const contextMenu = useMemo(
() => ({
isEnabledForInspectedElement: enabledInspectedElementContextMenu,
supportsCopyOperation: enabledInspectedElementContextMenuCopy,
viewAttributeSourceFunction: viewAttributeSourceFunction || null,
}),
[
enabledInspectedElementContextMenu,
enabledInspectedElementContextMenuCopy,
viewAttributeSourceFunction,
],
[enabledInspectedElementContextMenu, viewAttributeSourceFunction],
);

useEffect(
Expand Down
2 changes: 0 additions & 2 deletions packages/react-devtools-shared/src/devtools/views/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@ StoreContext.displayName = 'StoreContext';

export type ContextMenuContextType = {|
isEnabledForInspectedElement: boolean,
supportsCopyOperation: boolean,
viewAttributeSourceFunction?: ?ViewAttributeSource,
|};

export const ContextMenuContext = createContext<ContextMenuContextType>({
isEnabledForInspectedElement: false,
supportsCopyOperation: false,
viewAttributeSourceFunction: null,
});
ContextMenuContext.displayName = 'ContextMenuContext';
1 change: 0 additions & 1 deletion packages/react-devtools-shell/src/devtools.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ inject('dist/app.js', () => {
createElement(DevTools, {
browserTheme: 'light',
enabledInspectedElementContextMenu: true,
enabledInspectedElementContextMenuCopy: true,
showTabBar: true,
warnIfLegacyBackendDetected: true,
warnIfUnsupportedVersionDetected: true,
Expand Down

0 comments on commit 22ef96a

Please sign in to comment.