/* 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 . */
/**
* Redux actions for the editor tabs
*/
import { removeDocument } from "../utils/editor/index";
import { selectSource } from "./sources/index";
import { getSelectedLocation, getSourcesForTabs } from "../selectors/index";
export function addTab(source, sourceActor) {
return {
type: "ADD_TAB",
source,
sourceActor,
};
}
export function moveTab(url, tabIndex) {
return {
type: "MOVE_TAB",
url,
tabIndex,
};
}
export function moveTabBySourceId(sourceId, tabIndex) {
return {
type: "MOVE_TAB_BY_SOURCE_ID",
sourceId,
tabIndex,
};
}
export function closeTab(source) {
return closeTabs([source]);
}
export function closeTabs(sources) {
return ({ dispatch, getState }) => {
if (!sources.length) {
return;
}
for (const source of sources) {
removeDocument(source.id);
}
// If we are removing the tabs for the selected location,
// we need to select another source
const newSourceToSelect = getNewSourceToSelect(getState(), sources);
dispatch({ type: "CLOSE_TABS", sources });
dispatch(selectSource(newSourceToSelect));
};
}
/**
* Compute the potential new source to select while closing tabs for a given set of sources.
*
* @param {Object} state
* Redux state object.
* @param {Array