Skip to content

[DevTools] Linkify Source View #33954

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

Merged
merged 1 commit into from
Jul 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,18 @@
max-width: 100%;
margin-left: 1rem;
}

.Link {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copied from KeyValue.css

color: var(--color-link);
white-space: pre;
overflow: hidden;
text-overflow: ellipsis;
flex: 1;
cursor: pointer;
border-radius: 0.125rem;
padding: 0px 2px;
}

.Link:hover {
background-color: var(--color-background-hover);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import * as React from 'react';
import {useCallback, useContext} from 'react';
import {copy} from 'clipboard-js';
import {toNormalUrl} from 'jsc-safe-url';

Expand All @@ -16,6 +17,8 @@ import ButtonIcon from '../ButtonIcon';
import Skeleton from './Skeleton';
import {withPermissionsCheck} from 'react-devtools-shared/src/frontend/utils/withPermissionsCheck';

import ViewElementSourceContext from './ViewElementSourceContext';

import type {Source as InspectedElementSource} from 'react-devtools-shared/src/shared/types';
import styles from './InspectedElementSourcePanel.css';

Expand Down Expand Up @@ -87,25 +90,45 @@ function CopySourceButton({source, symbolicatedSourcePromise}: Props) {

function FormattedSourceString({source, symbolicatedSourcePromise}: Props) {
const symbolicatedSource = React.use(symbolicatedSourcePromise);
if (symbolicatedSource == null) {
const {sourceURL, line} = source;

return (
<div
className={styles.SourceOneLiner}
data-testname="InspectedElementView-FormattedSourceString">
{formatSourceForDisplay(sourceURL, line)}
</div>
);
}
const {canViewElementSourceFunction, viewElementSourceFunction} = useContext(
ViewElementSourceContext,
);

// In some cases (e.g. FB internal usage) the standalone shell might not be able to view the source.
// To detect this case, we defer to an injected helper function (if present).
const linkIsEnabled =
viewElementSourceFunction != null &&
source != null &&
(canViewElementSourceFunction == null ||
canViewElementSourceFunction(source, symbolicatedSource));

const viewSource = useCallback(() => {
if (viewElementSourceFunction != null && source != null) {
viewElementSourceFunction(source, symbolicatedSource);
}
}, [source, symbolicatedSource]);

const {sourceURL, line} = symbolicatedSource;
let sourceURL, line;
if (symbolicatedSource == null) {
sourceURL = source.sourceURL;
line = source.line;
} else {
sourceURL = symbolicatedSource.sourceURL;
line = symbolicatedSource.line;
}

return (
<div
className={styles.SourceOneLiner}
data-testname="InspectedElementView-FormattedSourceString">
{formatSourceForDisplay(sourceURL, line)}
{linkIsEnabled ? (
<span className={styles.Link} onClick={viewSource}>
{formatSourceForDisplay(sourceURL, line)}
</span>
) : (
formatSourceForDisplay(sourceURL, line)
)}
</div>
);
}
Expand Down
Loading