From 1252a0020d1fbb2c65f014349d3b852c17addeb7 Mon Sep 17 00:00:00 2001 From: Brian Vaughn Date: Tue, 10 Dec 2019 08:49:30 -0800 Subject: [PATCH 1/2] Show component location info for selected element in bottom/right of props panel --- .../views/Components/SelectedElement.css | 25 ++++++++++ .../views/Components/SelectedElement.js | 47 +++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/packages/react-devtools-shared/src/devtools/views/Components/SelectedElement.css b/packages/react-devtools-shared/src/devtools/views/Components/SelectedElement.css index c6fa68ddb782d..e73880f632012 100644 --- a/packages/react-devtools-shared/src/devtools/views/Components/SelectedElement.css +++ b/packages/react-devtools-shared/src/devtools/views/Components/SelectedElement.css @@ -31,6 +31,31 @@ font-family: var(--font-family-sans); } +.Source { + padding: 0.25rem; + border-top: 1px solid var(--color-border); +} + +.SourceHeaderRow { + display: flex; + align-items: center; +} + +.SourceHeader { + flex: 1 1; + font-family: var(--font-family-sans); +} + +.SourceOneLiner { + font-family: var(--font-family-monospace); + font-size: var(--font-size-monospace-normal); + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; + max-width: 100%; + margin-left: 1rem; +} + .Component, .Owner { color: var(--color-component-name); diff --git a/packages/react-devtools-shared/src/devtools/views/Components/SelectedElement.js b/packages/react-devtools-shared/src/devtools/views/Components/SelectedElement.js index 08d15104ec8e5..384e4d653d092 100644 --- a/packages/react-devtools-shared/src/devtools/views/Components/SelectedElement.js +++ b/packages/react-devtools-shared/src/devtools/views/Components/SelectedElement.js @@ -7,6 +7,7 @@ * @flow */ +import {copy} from 'clipboard-js'; import React, {useCallback, useContext} from 'react'; import {TreeDispatcherContext, TreeStateContext} from './TreeContext'; import {BridgeContext, StoreContext} from '../context'; @@ -272,6 +273,7 @@ function InspectedElementView({ hooks, owners, props, + source, state, } = inspectedElement; @@ -403,6 +405,51 @@ function InspectedElementView({ ))} )} + + {source !== null && ( + + )} + + ); +} + +// The function below (formatSourceForDisplay) is based on packages/shared/describeComponentFrame.js +const BEFORE_SLASH_RE = /^(.*)[\\\/]/; +function formatSourceForDisplay(fileName: string, lineNumber: string) { + let nameOnly = fileName.replace(BEFORE_SLASH_RE, ''); + // In DEV, include code for a common special case: + // prefer "folder/index.js" instead of just "index.js". + if (/^index\./.test(nameOnly)) { + const match = fileName.match(BEFORE_SLASH_RE); + if (match) { + const pathBeforeSlash = match[1]; + if (pathBeforeSlash) { + const folderName = pathBeforeSlash.replace(BEFORE_SLASH_RE, ''); + nameOnly = folderName + '/' + nameOnly; + } + } + } + return `${nameOnly}:${lineNumber}`; +} + +type SourceProps = {| + fileName: string, + lineNumber: string, +|}; + +function Source({fileName, lineNumber}: SourceProps) { + const handleCopy = () => copy(`${fileName}:${lineNumber}`); + return ( +
+
+
source
+ +
+
+ {formatSourceForDisplay(fileName, lineNumber)} +
); } From 040b28ce8b26869142b41efd1fa0d36c41390624 Mon Sep 17 00:00:00 2001 From: Brian Vaughn Date: Tue, 10 Dec 2019 09:11:58 -0800 Subject: [PATCH 2/2] Moved RegExp declaration into function basedon PR feedback --- .../src/devtools/views/Components/SelectedElement.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/react-devtools-shared/src/devtools/views/Components/SelectedElement.js b/packages/react-devtools-shared/src/devtools/views/Components/SelectedElement.js index 384e4d653d092..dcf3529a7140d 100644 --- a/packages/react-devtools-shared/src/devtools/views/Components/SelectedElement.js +++ b/packages/react-devtools-shared/src/devtools/views/Components/SelectedElement.js @@ -413,10 +413,12 @@ function InspectedElementView({ ); } -// The function below (formatSourceForDisplay) is based on packages/shared/describeComponentFrame.js -const BEFORE_SLASH_RE = /^(.*)[\\\/]/; +// This function is based on packages/shared/describeComponentFrame.js function formatSourceForDisplay(fileName: string, lineNumber: string) { + const BEFORE_SLASH_RE = /^(.*)[\\\/]/; + let nameOnly = fileName.replace(BEFORE_SLASH_RE, ''); + // In DEV, include code for a common special case: // prefer "folder/index.js" instead of just "index.js". if (/^index\./.test(nameOnly)) { @@ -429,6 +431,7 @@ function formatSourceForDisplay(fileName: string, lineNumber: string) { } } } + return `${nameOnly}:${lineNumber}`; }