GeezerVison is showing github.com — Re-enable "view source" button for standalone shell · react/react@1e8aa81
File treeExpand file treeCollapse file tree Open diff view settings packages/react-devtools-core/src
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-devtools-core/src/editor.js +21-8Lines changed: 21 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -102,31 +102,44 @@ function guessEditor(): Array<string> { | 102 | 102 |
| 103 | 103 | let childProcess = null; | 104 | 104 |
| 105 | | -export default function launchEditor( | | 105 | +export function getValidFilePath( | 106 | 106 | maybeRelativePath: string, | 107 | | - lineNumber: number, | 108 | 107 | absoluteProjectRoots: Array<string> | 109 | | -) { | | 108 | +): string | null { | 110 | 109 | // We use relative paths at Facebook with deterministic builds. | 111 | 110 | // This is why our internal tooling calls React DevTools with absoluteProjectRoots. | 112 | 111 | // If the filename is absolute then we don't need to care about this. | 113 | | - let filePath; | 114 | 112 | if (isAbsolute(maybeRelativePath)) { | 115 | 113 | if (existsSync(maybeRelativePath)) { | 116 | | - filePath = maybeRelativePath; | | 114 | + return maybeRelativePath; | 117 | 115 | } | 118 | 116 | } else { | 119 | 117 | for (let i = 0; i < absoluteProjectRoots.length; i++) { | 120 | 118 | const projectRoot = absoluteProjectRoots[i]; | 121 | 119 | const joinedPath = join(projectRoot, maybeRelativePath); | 122 | 120 | if (existsSync(joinedPath)) { | 123 | | - filePath = joinedPath; | 124 | | - break; | | 121 | + return joinedPath; | 125 | 122 | } | 126 | 123 | } | 127 | 124 | } | 128 | 125 |
| 129 | | - if (!filePath) { | | 126 | + return null; | | 127 | +} | | 128 | + | | 129 | +export function doesFilePathExist( | | 130 | + maybeRelativePath: string, | | 131 | + absoluteProjectRoots: Array<string> | | 132 | +): boolean { | | 133 | + return getValidFilePath(maybeRelativePath, absoluteProjectRoots) !== null; | | 134 | +} | | 135 | + | | 136 | +export function launchEditor( | | 137 | + maybeRelativePath: string, | | 138 | + lineNumber: number, | | 139 | + absoluteProjectRoots: Array<string> | | 140 | +) { | | 141 | + const filePath = getValidFilePath(maybeRelativePath, absoluteProjectRoots); | | 142 | + if (filePath === null) { | 130 | 143 | return; | 131 | 144 | } | 132 | 145 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-devtools-core/src/standalone.js +18-6Lines changed: 18 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -14,7 +14,7 @@ import { Server } from 'ws'; | 14 | 14 | import { existsSync, readFileSync } from 'fs'; | 15 | 15 | import { installHook } from 'src/hook'; | 16 | 16 | import DevTools from 'src/devtools/views/DevTools'; | 17 | | -import launchEditor from './launchEditor'; | | 17 | +import { doesFilePathExist, launchEditor } from './editor'; | 18 | 18 | import { __DEBUG__ } from 'src/constants'; | 19 | 19 |
| 20 | 20 | import type { FrontendBridge } from 'src/bridge'; | @@ -85,16 +85,31 @@ function reload() { | 85 | 85 | root.render( | 86 | 86 | createElement(DevTools, { | 87 | 87 | bridge: ((bridge: any): FrontendBridge), | | 88 | + canViewElementSourceFunction, | 88 | 89 | showTabBar: true, | 89 | 90 | store: ((store: any): Store), | 90 | 91 | warnIfLegacyBackendDetected: true, | 91 | 92 | viewElementSourceFunction, | 92 | | - viewElementSourceRequiresFileLocation: true, | 93 | 93 | }) | 94 | 94 | ); | 95 | 95 | }, 100); | 96 | 96 | } | 97 | 97 |
| | 98 | +function canViewElementSourceFunction( | | 99 | + inspectedElement: InspectedElement | | 100 | +): boolean { | | 101 | + if ( | | 102 | + inspectedElement.canViewSource === false || | | 103 | + inspectedElement.source === null | | 104 | + ) { | | 105 | + return false; | | 106 | + } | | 107 | + | | 108 | + const { source } = inspectedElement; | | 109 | + | | 110 | + return doesFilePathExist(source.fileName, projectRoots); | | 111 | +} | | 112 | + | 98 | 113 | function viewElementSourceFunction( | 99 | 114 | id: number, | 100 | 115 | inspectedElement: InspectedElement | @@ -171,10 +186,7 @@ function initialize(socket: WebSocket) { | 171 | 186 | socket.close(); | 172 | 187 | }); | 173 | 188 |
| 174 | | - store = new Store(bridge, { | 175 | | - supportsNativeInspection: false, | 176 | | - supportsViewSource: projectRoots.length > 0, | 177 | | - }); | | 189 | + store = new Store(bridge, { supportsNativeInspection: false }); | 178 | 190 |
| 179 | 191 | log('Connected'); | 180 | 192 | reload(); | |
Collapse file Copy file name to clipboardExpand all lines: src/devtools/store.js -8Lines changed: 0 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -49,7 +49,6 @@ type Config = {| | 49 | 49 | supportsNativeInspection?: boolean, | 50 | 50 | supportsReloadAndProfile?: boolean, | 51 | 51 | supportsProfiling?: boolean, | 52 | | - supportsViewSource?: boolean, | 53 | 52 | |}; | 54 | 53 |
| 55 | 54 | export type Capabilities = {| | @@ -125,7 +124,6 @@ export default class Store extends EventEmitter<{| | 125 | 124 | _supportsNativeInspection: boolean = false; | 126 | 125 | _supportsProfiling: boolean = false; | 127 | 126 | _supportsReloadAndProfile: boolean = false; | 128 | | - _supportsViewSource: boolean = true; | 129 | 127 |
| 130 | 128 | // Total number of visible elements (within all roots). | 131 | 129 | // Used for windowing purposes. | @@ -157,15 +155,13 @@ export default class Store extends EventEmitter<{| | 157 | 155 | supportsNativeInspection, | 158 | 156 | supportsProfiling, | 159 | 157 | supportsReloadAndProfile, | 160 | | - supportsViewSource, | 161 | 158 | } = config; | 162 | 159 | if (supportsCaptureScreenshots) { | 163 | 160 | this._supportsCaptureScreenshots = true; | 164 | 161 | this._captureScreenshots = | 165 | 162 | localStorageGetItem(LOCAL_STORAGE_CAPTURE_SCREENSHOTS_KEY) === 'true'; | 166 | 163 | } | 167 | 164 | this._supportsNativeInspection = supportsNativeInspection !== false; | 168 | | - this._supportsViewSource = supportsViewSource !== false; | 169 | 165 | if (supportsProfiling) { | 170 | 166 | this._supportsProfiling = true; | 171 | 167 | } | @@ -365,10 +361,6 @@ export default class Store extends EventEmitter<{| | 365 | 361 | return this._supportsReloadAndProfile && this._isBackendStorageAPISupported; | 366 | 362 | } | 367 | 363 |
| 368 | | - get supportsViewSource(): boolean { | 369 | | - return this._supportsViewSource; | 370 | | - } | 371 | | - | 372 | 364 | containsElement(id: number): boolean { | 373 | 365 | return this._idToElement.get(id) != null; | 374 | 366 | } | |
Collapse file Copy file name to clipboardExpand all lines: src/devtools/views/Components/SelectedElement.js +17-15Lines changed: 17 additions & 15 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -34,9 +34,10 @@ export type Props = {||}; | 34 | 34 | export default function SelectedElement(_: Props) { | 35 | 35 | const { inspectedElementID } = useContext(TreeStateContext); | 36 | 36 | const dispatch = useContext(TreeDispatcherContext); | 37 | | - const { isFileLocationRequired, viewElementSourceFunction } = useContext( | 38 | | - ViewElementSourceContext | 39 | | - ); | | 37 | + const { | | 38 | + canViewElementSourceFunction, | | 39 | + viewElementSourceFunction, | | 40 | + } = useContext(ViewElementSourceContext); | 40 | 41 | const bridge = useContext(BridgeContext); | 41 | 42 | const store = useContext(StoreContext); | 42 | 43 | const { dispatch: modalDialogDispatch } = useContext(ModalDialogContext); | @@ -90,11 +91,14 @@ export default function SelectedElement(_: Props) { | 90 | 91 | } | 91 | 92 | }, [inspectedElement, viewElementSourceFunction]); | 92 | 93 |
| | 94 | + // In some cases (e.g. FB internal usage) the standalone shell might not be able to view the source. | | 95 | + // To detect this case, we defer to an injected helper function (if present). | 93 | 96 | const canViewSource = | 94 | | - inspectedElement && | | 97 | + inspectedElement !== null && | 95 | 98 | inspectedElement.canViewSource && | 96 | 99 | viewElementSourceFunction !== null && | 97 | | - (!isFileLocationRequired || inspectedElement.source !== null); | | 100 | + (canViewElementSourceFunction === null || | | 101 | + canViewElementSourceFunction(inspectedElement)); | 98 | 102 |
| 99 | 103 | const isSuspended = | 100 | 104 | element !== null && | @@ -201,16 +205,14 @@ export default function SelectedElement(_: Props) { | 201 | 205 | > | 202 | 206 | <ButtonIcon type="log-data" /> | 203 | 207 | </Button> | 204 | | - {store.supportsViewSource && ( | 205 | | - <Button | 206 | | - className={styles.IconButton} | 207 | | - disabled={!canViewSource} | 208 | | - onClick={viewSource} | 209 | | - title="View source for this element" | 210 | | - > | 211 | | - <ButtonIcon type="view-source" /> | 212 | | - </Button> | 213 | | - )} | | 208 | + <Button | | 209 | + className={styles.IconButton} | | 210 | + disabled={!canViewSource} | | 211 | + onClick={viewSource} | | 212 | + title="View source for this element" | | 213 | + > | | 214 | + <ButtonIcon type="view-source" /> | | 215 | + </Button> | 214 | 216 | </div> | 215 | 217 |
| 216 | 218 | {inspectedElement === null && ( | |
Collapse file Copy file name to clipboardExpand all lines: src/devtools/views/Components/ViewElementSourceContext.js +5-2Lines changed: 5 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 |
| 3 | 3 | import { createContext } from 'react'; | 4 | 4 |
| 5 | | -import type { ViewElementSource } from 'src/devtools/views/DevTools'; | | 5 | +import type { | | 6 | + CanViewElementSource, | | 7 | + ViewElementSource, | | 8 | +} from 'src/devtools/views/DevTools'; | 6 | 9 |
| 7 | 10 | export type Context = {| | 8 | | - isFileLocationRequired: boolean, | | 11 | + canViewElementSourceFunction: CanViewElementSource | null, | 9 | 12 | viewElementSourceFunction: ViewElementSource | null, | 10 | 13 | |}; | 11 | 14 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/devtools/views/DevTools.js +9-6Lines changed: 9 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,16 +32,19 @@ export type ViewElementSource = ( | 32 | 32 | id: number, | 33 | 33 | inspectedElement: InspectedElement | 34 | 34 | ) => void; | | 35 | +export type CanViewElementSource = ( | | 36 | + inspectedElement: InspectedElement | | 37 | +) => boolean; | 35 | 38 |
| 36 | 39 | export type Props = {| | 37 | 40 | bridge: FrontendBridge, | 38 | 41 | browserTheme?: BrowserTheme, | | 42 | + canViewElementSourceFunction?: ?CanViewElementSource, | 39 | 43 | defaultTab?: TabID, | 40 | 44 | showTabBar?: boolean, | 41 | 45 | store: Store, | 42 | 46 | warnIfLegacyBackendDetected?: boolean, | 43 | 47 | viewElementSourceFunction?: ?ViewElementSource, | 44 | | - viewElementSourceRequiresFileLocation?: boolean, | 45 | 48 |
| 46 | 49 | // This property is used only by the web extension target. | 47 | 50 | // The built-in tab UI is hidden in that case, in favor of the browser's own panel tabs. | @@ -75,6 +78,7 @@ const tabs = [componentsTab, profilerTab]; | 75 | 78 | export default function DevTools({ | 76 | 79 | bridge, | 77 | 80 | browserTheme = 'light', | | 81 | + canViewElementSourceFunction = null, | 78 | 82 | defaultTab = 'components', | 79 | 83 | componentsPortalContainer, | 80 | 84 | overrideTab, | @@ -83,8 +87,7 @@ export default function DevTools({ | 83 | 87 | showTabBar = false, | 84 | 88 | store, | 85 | 89 | warnIfLegacyBackendDetected = false, | 86 | | - viewElementSourceFunction, | 87 | | - viewElementSourceRequiresFileLocation = false, | | 90 | + viewElementSourceFunction = null, | 88 | 91 | }: Props) { | 89 | 92 | const [tab, setTab] = useState(defaultTab); | 90 | 93 | if (overrideTab != null && overrideTab !== tab) { | @@ -93,10 +96,10 @@ export default function DevTools({ | 93 | 96 |
| 94 | 97 | const viewElementSource = useMemo( | 95 | 98 | () => ({ | 96 | | - isFileLocationRequired: viewElementSourceRequiresFileLocation, | 97 | | - viewElementSourceFunction: viewElementSourceFunction || null, | | 99 | + canViewElementSourceFunction, | | 100 | + viewElementSourceFunction, | 98 | 101 | }), | 99 | | - [viewElementSourceFunction, viewElementSourceRequiresFileLocation] | | 102 | + [canViewElementSourceFunction, viewElementSourceFunction] | 100 | 103 | ); | 101 | 104 |
| 102 | 105 | return ( | |
|