tab.js (4170B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */ 4 5 import { showMenu, buildMenu } from "../../context-menu/menu"; 6 import { getTabMenuItems } from "../../utils/tabs"; 7 8 import { 9 getSelectedLocation, 10 getOpenedSources, 11 isSourceBlackBoxed, 12 isSourceMapIgnoreListEnabled, 13 isSourceOnSourceMapIgnoreList, 14 isPrettyPrinted, 15 } from "../../selectors/index"; 16 17 import { toggleBlackBox } from "../sources/blackbox"; 18 import { prettyPrintAndSelectSource } from "../sources/prettyPrint"; 19 import { copyToClipboard, showSource } from "../ui"; 20 import { closeTabForSource, closeTabsForSources } from "../tabs"; 21 22 import { getRawSourceURL, shouldBlackbox } from "../../utils/source"; 23 import { copyToTheClipboard } from "../../utils/clipboard"; 24 25 /** 26 * Show the context menu of Tab. 27 * 28 * @param {object} event 29 * The context-menu DOM event. 30 * @param {object} source 31 * Source object of the related Tab. 32 */ 33 export function showTabContextMenu(event, source) { 34 return async ({ dispatch, getState }) => { 35 const state = getState(); 36 const selectedLocation = getSelectedLocation(state); 37 38 const isBlackBoxed = isSourceBlackBoxed(state, source); 39 const isSourceOnIgnoreList = 40 isSourceMapIgnoreListEnabled(state) && 41 isSourceOnSourceMapIgnoreList(state, source); 42 const isSourcePrettyPrinted = isPrettyPrinted(state, source); 43 44 const openedSources = getOpenedSources(state); 45 const otherSources = openedSources.filter(s => s != source); 46 const sourceIndex = openedSources.indexOf(source); 47 const sourcesForTabsAfter = openedSources.slice(sourceIndex + 1); 48 49 const tabMenuItems = getTabMenuItems(); 50 const items = [ 51 { 52 item: { 53 ...tabMenuItems.closeTab, 54 click: () => dispatch(closeTabForSource(source)), 55 }, 56 }, 57 { 58 item: { 59 ...tabMenuItems.closeOtherTabs, 60 disabled: otherSources.length === 0, 61 click: () => dispatch(closeTabsForSources(otherSources)), 62 }, 63 }, 64 { 65 item: { 66 ...tabMenuItems.closeTabsToEnd, 67 disabled: sourcesForTabsAfter.length === 0, 68 click: () => { 69 dispatch(closeTabsForSources(sourcesForTabsAfter)); 70 }, 71 }, 72 }, 73 { 74 item: { 75 ...tabMenuItems.closeAllTabs, 76 click: () => dispatch(closeTabsForSources(openedSources)), 77 }, 78 }, 79 { item: { type: "separator" } }, 80 { 81 item: { 82 ...tabMenuItems.copySource, 83 // Only enable when this is the selected source as this requires the source to be loaded, 84 // which may not be the case if the tab wasn't ever selected. 85 // 86 // Note that when opening the debugger, you may have tabs opened from a previous session, 87 // but no selected location. 88 disabled: selectedLocation?.source.id !== source.id, 89 click: () => { 90 dispatch(copyToClipboard(selectedLocation)); 91 }, 92 }, 93 }, 94 { 95 item: { 96 ...tabMenuItems.copySourceUri2, 97 disabled: !source.url, 98 click: () => copyToTheClipboard(getRawSourceURL(source.url)), 99 }, 100 }, 101 { 102 item: { 103 ...tabMenuItems.showSource, 104 // Source Tree only shows sources with URL 105 disabled: !source.url, 106 click: () => dispatch(showSource(source.id)), 107 }, 108 }, 109 { 110 item: { 111 ...tabMenuItems.toggleBlackBox, 112 label: isBlackBoxed 113 ? L10N.getStr("ignoreContextItem.unignore") 114 : L10N.getStr("ignoreContextItem.ignore"), 115 disabled: isSourceOnIgnoreList || !shouldBlackbox(source), 116 click: () => dispatch(toggleBlackBox(source)), 117 }, 118 }, 119 { 120 item: { 121 ...tabMenuItems.prettyPrint, 122 disabled: isSourcePrettyPrinted, 123 click: () => dispatch(prettyPrintAndSelectSource(source)), 124 }, 125 }, 126 ]; 127 128 showMenu(event, buildMenu(items)); 129 }; 130 }