forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtabs.js
More file actions
227 lines (190 loc) · 5.62 KB
/
tabs.js
File metadata and controls
227 lines (190 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* 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
/**
* Tabs reducer
* @module reducers/tabs
*/
import { createSelector } from "reselect";
import { isOriginalId } from "devtools-source-map";
import move from "lodash-move";
import { asyncStore } from "../utils/prefs";
import {
getSource,
getSources,
getSourceInSources,
getUrls,
getSpecificSourceByURL,
getSpecificSourceByURLInSources
} from "./sources";
import type { Action } from "../actions/types";
import type { SourcesState } from "./sources";
import type { Source } from "../types";
import type { Selector } from "./types";
export type Tab = {
url: string,
framework?: string | null,
isOriginal: boolean,
sourceId?: string
};
export type TabList = Tab[];
function isSimilarTab(tab: Tab, url: string, isOriginal: boolean) {
return tab.url === url && tab.isOriginal === isOriginal;
}
function update(state: TabList = [], action: Action): TabList {
switch (action.type) {
case "ADD_TAB":
case "UPDATE_TAB":
return updateTabList(state, action);
case "MOVE_TAB":
return moveTabInList(state, action);
case "CLOSE_TAB":
case "CLOSE_TABS":
asyncStore.tabs = action.tabs;
return action.tabs;
default:
return state;
}
}
export function removeSourceFromTabList(
tabs: TabList,
source: Source
): TabList {
return tabs.filter(
tab => tab.url !== source.url || tab.isOriginal != isOriginalId(source.id)
);
}
export function removeSourcesFromTabList(tabs: TabList, sources: Source[]) {
return sources.reduce(
(t, source) => removeSourceFromTabList(t, source),
tabs
);
}
/**
* Adds the new source to the tab list if it is not already there
* @memberof reducers/tabs
* @static
*/
function updateTabList(
tabs: TabList,
{ url, framework = null, sourceId, isOriginal = false }
) {
// Set currentIndex to -1 for URL-less tabs so that they aren't
// filtered by isSimilarTab
const currentIndex = url
? tabs.findIndex(tab => isSimilarTab(tab, url, isOriginal))
: -1;
if (currentIndex === -1) {
tabs = [{ url, framework, sourceId, isOriginal }, ...tabs];
} else if (framework) {
tabs[currentIndex].framework = framework;
}
asyncStore.tabs = persistTabs(tabs);
return tabs;
}
function persistTabs(tabs) {
return tabs.filter(tab => tab.url).map(tab => {
const newTab = { ...tab };
delete newTab.sourceId;
return newTab;
});
}
function moveTabInList(tabs: TabList, { url, tabIndex: newIndex }) {
const currentIndex = tabs.findIndex(tab => tab.url == url);
tabs = move(tabs, currentIndex, newIndex);
asyncStore.tabs = tabs;
return tabs;
}
/**
* Gets the next tab to select when a tab closes. Heuristics:
* 1. if the selected tab is available, it remains selected
* 2. if it is gone, the next available tab to the left should be active
* 3. if the first tab is active and closed, select the second tab
*
* @memberof reducers/tabs
* @static
*/
export function getNewSelectedSourceId(
state: OuterState,
availableTabs: TabList
): string {
const selectedLocation = state.sources.selectedLocation;
if (!selectedLocation) {
return "";
}
const selectedTab = getSource(state, selectedLocation.sourceId);
if (!selectedTab) {
return "";
}
const matchingTab = availableTabs.find(tab =>
isSimilarTab(tab, selectedTab.url, isOriginalId(selectedLocation.sourceId))
);
if (matchingTab) {
const sources = state.sources.sources;
if (!sources) {
return "";
}
const selectedSource = getSpecificSourceByURL(
state,
selectedTab.url,
isOriginalId(selectedTab.id)
);
if (selectedSource) {
return selectedSource.id;
}
return "";
}
const tabUrls = state.tabs.map(t => t.url);
const leftNeighborIndex = Math.max(tabUrls.indexOf(selectedTab.url) - 1, 0);
const lastAvailbleTabIndex = availableTabs.length - 1;
const newSelectedTabIndex = Math.min(leftNeighborIndex, lastAvailbleTabIndex);
const availableTab = availableTabs[newSelectedTabIndex];
if (availableTab) {
const tabSource = getSpecificSourceByURL(
state,
availableTab.url,
availableTab.isOriginal
);
if (tabSource) {
return tabSource.id;
}
}
return "";
}
// Selectors
// Unfortunately, it's really hard to make these functions accept just
// the state that we care about and still type it with Flow. The
// problem is that we want to re-export all selectors from a single
// module for the UI, and all of those selectors should take the
// top-level app state, so we'd have to "wrap" them to automatically
// pick off the piece of state we're interested in. It's impossible
// (right now) to type those wrapped functions.
type OuterState = { tabs: TabList, sources: SourcesState };
export const getTabs = (state: OuterState): TabList => state.tabs;
export const getSourceTabs: Selector<Tab[]> = createSelector(
getTabs,
getSources,
getUrls,
(tabs, sources, urls) =>
tabs.filter(tab => getTabWithOrWithoutUrl(tab, sources, urls))
);
export const getSourcesForTabs: Selector<Source[]> = createSelector(
getSourceTabs,
getSources,
getUrls,
(tabs, sources, urls) =>
tabs.map(tab => getTabWithOrWithoutUrl(tab, sources, urls)).filter(Boolean)
);
function getTabWithOrWithoutUrl(tab, sources, urls) {
if (tab.url) {
return getSpecificSourceByURLInSources(
sources,
urls,
tab.url,
tab.isOriginal
);
}
return tab.sourceId ? getSourceInSources(sources, tab.sourceId) : null;
}
export default update;