Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Commit

Permalink
Share querystring logic with tabs & breakpoints sidebar (#7206)
Browse files Browse the repository at this point in the history
  • Loading branch information
WenInCode authored and darkwing committed Nov 13, 2018
1 parent 6fbc4e4 commit a485600
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 46 deletions.
20 changes: 13 additions & 7 deletions src/components/Editor/Tab.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ import {
getRawSourceURL,
getTruncatedFileName,
getDisplayPath,
isPretty
isPretty,
getSourceQueryString
} from "../../utils/source";
import { copyToTheClipboard } from "../../utils/clipboard";
import { getTabMenuItems } from "../../utils/tabs";

import {
getSelectedSource,
getActiveSearch,
getSourcesForTabs
getSourcesForTabs,
getHasSiblingOfSameName
} from "../../selectors";

import classnames from "classnames";
Expand All @@ -46,7 +48,8 @@ type Props = {
closeTab: Source => void,
closeTabs: (List<string>) => void,
togglePrettyPrint: string => void,
showSource: string => void
showSource: string => void,
hasSiblingOfSameName: boolean
};

class Tab extends PureComponent<Props> {
Expand Down Expand Up @@ -155,7 +158,8 @@ class Tab extends PureComponent<Props> {
selectSource,
closeTab,
source,
tabSources
tabSources,
hasSiblingOfSameName
} = this.props;
const sourceId = source.id;
const active =
Expand All @@ -181,6 +185,7 @@ class Tab extends PureComponent<Props> {
});

const path = getDisplayPath(source, tabSources);
const query = hasSiblingOfSameName ? getSourceQueryString(source) : "";

return (
<div
Expand All @@ -190,14 +195,14 @@ class Tab extends PureComponent<Props> {
// Accommodate middle click to close tab
onMouseUp={e => e.button === 1 && closeTab(source)}
onContextMenu={e => this.onTabContextMenu(e, sourceId)}
title={getFileURL(source)}
title={getFileURL(source, false)}
>
<SourceIcon
source={source}
shouldHide={icon => ["file", "javascript"].includes(icon)}
/>
<div className="filename">
{getTruncatedFileName(source)}
{getTruncatedFileName(source, query)}
{path && <span>{`../${path}/..`}</span>}
</div>
<CloseButton
Expand All @@ -215,7 +220,8 @@ const mapStateToProps = (state, { source }) => {
return {
tabSources: getSourcesForTabs(state),
selectedSource: selectedSource,
activeSearch: getActiveSearch(state)
activeSearch: getActiveSearch(state),
hasSiblingOfSameName: getHasSiblingOfSameName(state, source)
};
};

Expand Down
18 changes: 6 additions & 12 deletions src/components/PrimaryPanes/SourcesTreeItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ import Svg from "../shared/Svg";

import {
getGeneratedSourceByURL,
getSourcesUrlsInSources
getHasSiblingOfSameName
} from "../../selectors";
import actions from "../../actions";

import { isOriginal as isOriginalSource } from "../../utils/source";
import {
isOriginal as isOriginalSource,
getSourceQueryString
} from "../../utils/source";
import { isDirectory } from "../../utils/sources-tree";
import { copyToTheClipboard } from "../../utils/clipboard";
import { features } from "../../utils/prefs";
import { parse } from "../../utils/url";

import type { TreeNode } from "../../utils/sources-tree/types";
import type { Source } from "../../types";
Expand Down Expand Up @@ -181,7 +183,7 @@ class SourceTreeItem extends Component<Props, State> {
<span className="suffix">{L10N.getStr("sourceFooter.mappedSuffix")}</span>
) : null;

const querystring = source ? parse(source.url).search : null;
const querystring = getSourceQueryString(source);
const query =
hasSiblingOfSameName && querystring ? (
<span className="query">{querystring}</span>
Expand Down Expand Up @@ -214,14 +216,6 @@ function getHasMatchingGeneratedSource(state, source: ?Source) {
return !!getGeneratedSourceByURL(state, source.url);
}

function getHasSiblingOfSameName(state, source: ?Source) {
if (!source) {
return false;
}

return getSourcesUrlsInSources(state, source.url).length > 1;
}

const mapStateToProps = (state, props) => {
const { source } = props;
return {
Expand Down
61 changes: 61 additions & 0 deletions src/components/SecondaryPanes/Breakpoints/BreakpointHeading.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */

// @flow
import React, { PureComponent } from "react";
import { connect } from "react-redux";
import actions from "../../../actions";
import {
getTruncatedFileName,
getDisplayPath,
getSourceQueryString,
getFileURL
} from "../../../utils/source";
import { getHasSiblingOfSameName } from "../../../selectors";

import SourceIcon from "../../shared/SourceIcon";

import type { Source } from "../../../types";

type Props = {
sources: Source[],
source: Source,
hasSiblingOfSameName: boolean,
selectSource: string => void
};

class BreakpointHeading extends PureComponent<Props> {
render() {
const { sources, source, hasSiblingOfSameName, selectSource } = this.props;

const path = getDisplayPath(source, sources);
const query = hasSiblingOfSameName ? getSourceQueryString(source) : "";

return (
<div
className="breakpoint-heading"
title={getFileURL(source, false)}
onClick={() => selectSource(source.id)}
>
<SourceIcon
source={source}
shouldHide={icon => ["file", "javascript"].includes(icon)}
/>
<div className="filename">
{getTruncatedFileName(source, query)}
{path && <span>{`../${path}/..`}</span>}
</div>
</div>
);
}
}

const mapStateToProps = (state, { source }) => ({
hasSiblingOfSameName: getHasSiblingOfSameName(state, source)
});

export default connect(
mapStateToProps,
{ selectSource: actions.selectSource }
)(BreakpointHeading);
5 changes: 5 additions & 0 deletions src/components/SecondaryPanes/Breakpoints/Breakpoints.css
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
align-items: center;
}

.breakpoints-list .breakpoint-heading .filename {
overflow: hidden;
text-overflow: ellipsis;
}

.breakpoints-list .breakpoint-heading .filename span {
opacity: 0.7;
padding-left: 4px;
Expand Down
33 changes: 9 additions & 24 deletions src/components/SecondaryPanes/Breakpoints/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@ import { connect } from "react-redux";
import ExceptionOption from "./ExceptionOption";

import Breakpoint from "./Breakpoint";
import SourceIcon from "../../shared/SourceIcon";
import BreakpointHeading from "./BreakpointHeading";

import actions from "../../../actions";
import {
getTruncatedFileName,
getDisplayPath,
getRawSourceURL
} from "../../../utils/source";
import { getDisplayPath } from "../../../utils/source";
import { makeLocationId } from "../../../utils/breakpoint";

import { getSelectedSource, getBreakpointSources } from "../../../selectors";
Expand All @@ -33,8 +29,7 @@ type Props = {
selectedSource: Source,
shouldPauseOnExceptions: boolean,
shouldPauseOnCaughtExceptions: boolean,
pauseOnExceptions: Function,
selectSource: string => void
pauseOnExceptions: Function
};

class Breakpoints extends Component<Props> {
Expand Down Expand Up @@ -85,21 +80,12 @@ class Breakpoints extends Component<Props> {
...breakpointSources.map(({ source, breakpoints, i }) => {
const path = getDisplayPath(source, sources);
return [
<div
className="breakpoint-heading"
title={getRawSourceURL(source.url)}
<BreakpointHeading
source={source}
sources={sources}
path={path}
key={source.url}
onClick={() => this.props.selectSource(source.id)}
>
<SourceIcon
source={source}
shouldHide={icon => ["file", "javascript"].includes(icon)}
/>
<div className="filename">
{getTruncatedFileName(source)}
{path && <span>{`../${path}/..`}</span>}
</div>
</div>,
/>,
...breakpoints.map(breakpoint => (
<Breakpoint
breakpoint={breakpoint}
Expand Down Expand Up @@ -130,7 +116,6 @@ const mapStateToProps = state => ({
export default connect(
mapStateToProps,
{
pauseOnExceptions: actions.pauseOnExceptions,
selectSource: actions.selectSource
pauseOnExceptions: actions.pauseOnExceptions
}
)(Breakpoints);
8 changes: 8 additions & 0 deletions src/reducers/sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,14 @@ export function getSourcesUrlsInSources(state: OuterState, url: string) {
return [...new Set(Object.keys(urls).filter(Boolean))];
}

export function getHasSiblingOfSameName(state: OuterState, source: ?Source) {
if (!source) {
return false;
}

return getSourcesUrlsInSources(state, source.url).length > 1;
}

export function getSourceInSources(sources: SourcesMap, id: string): ?Source {
return sources[id];
}
Expand Down
14 changes: 13 additions & 1 deletion src/test/mochitest/browser_dbg-sources-querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,17 @@ add_task(async function() {
labels.includes("simple1.js?x=1") && labels.includes("simple1.js?x=2"),
true,
"simple1.js?x=1 and simple2.jsx=2 exist"
)
);

const source = findSource(dbg, "simple1.js?x=1");
await selectSource(dbg, source);
const tab = findElement(dbg, "activeTab");
is(tab.innerText, "simple1.js?x=1", "Tab label is simple1.js?x=1");
await addBreakpoint(dbg, "simple1.js?x=1", 6);
const breakpointHeading = findElement(dbg, "breakpointItem", 2).innerText;
is(
breakpointHeading,
"simple1.js?x=1",
"Breakpoint heading is simple1.js?x=1"
);
});
12 changes: 10 additions & 2 deletions src/utils/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,12 @@ export function getFilename(source: Source) {
* @memberof utils/source
* @static
*/
export function getTruncatedFileName(source: Source, length: number = 30) {
return truncateMiddleText(getFilename(source), length);
export function getTruncatedFileName(
source: Source,
querystring: string = "",
length: number = 30
) {
return truncateMiddleText(`${getFilename(source)}${querystring}`, length);
}

/* Gets path for files with same filename for editor tabs, breakpoints, etc.
Expand Down Expand Up @@ -456,3 +460,7 @@ export function isOriginal(source: Source) {
export function isGenerated(source: Source) {
return isGeneratedId(source.id);
}

export function getSourceQueryString(source: ?Source) {
return source ? parseURL(source.url).search : "";
}
2 changes: 2 additions & 0 deletions src/utils/tests/source.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ describe("sources", () => {
url: "really-really-really-really-really-really-long-name.html",
id: ""
},
"",
30
)
).toBe("really-really…long-name.html");
Expand All @@ -77,6 +78,7 @@ describe("sources", () => {
url: `${encodedUnicode.repeat(30)}.html`,
id: ""
},
"",
30
)
).toBe("測測測測測測測測測測測測測…測測測測測測測測測.html");
Expand Down

0 comments on commit a485600

Please sign in to comment.