redux-devtools/packages/redux-devtools-inspector-monitor-trace-tab/src/openFile.ts

142 lines
4.6 KiB
TypeScript
Raw Normal View History

import StackFrame from './react-error-overlay/utils/stack-frame';
2018-12-18 02:03:08 +03:00
const isFF = navigator.userAgent.indexOf('Firefox') !== -1;
function openResource(
fileName: string,
lineNumber: number,
stackFrame: StackFrame
) {
2018-12-15 04:04:34 +03:00
const adjustedLineNumber = Math.max(lineNumber - 1, 0);
chrome.devtools.panels.openResource(fileName, adjustedLineNumber, ((result: {
isError?: boolean;
}) => {
//console.log("openResource callback args: ", callbackArgs);
if (result.isError) {
const { fileName: finalFileName, lineNumber: finalLineNumber } =
stackFrame;
const adjustedLineNumber = Math.max(finalLineNumber! - 1, 0);
chrome.devtools.panels.openResource(
finalFileName!,
adjustedLineNumber,
(/* result */) => {
// console.log("openResource result: ", result);
}
);
2018-12-15 04:04:34 +03:00
}
}) as () => void);
2018-12-15 04:04:34 +03:00
}
function openAndCloseTab(url: string) {
chrome.tabs.create({ url }, (tab) => {
2018-12-18 02:03:08 +03:00
const removeTab = () => {
chrome.windows.onFocusChanged.removeListener(removeTab);
if (tab && tab.id) {
chrome.tabs.remove(tab.id, async () => {
2019-01-10 21:51:14 +03:00
// eslint-disable-next-line no-console
if (chrome.runtime.lastError) console.log(chrome.runtime.lastError);
2018-12-18 02:03:08 +03:00
else if (chrome.devtools && chrome.devtools.inspectedWindow) {
await chrome.tabs.update(chrome.devtools.inspectedWindow.tabId, {
active: true,
2019-01-10 21:51:14 +03:00
});
}
2018-12-18 02:03:08 +03:00
});
}
};
if (chrome.windows) chrome.windows.onFocusChanged.addListener(removeTab);
});
}
function openInIframe(url: string) {
2018-12-18 02:03:08 +03:00
const iframe = document.createElement('iframe');
iframe.src = url;
iframe.style.display = 'none';
2018-12-18 02:03:08 +03:00
document.body.appendChild(iframe);
setTimeout(() => iframe.parentNode!.removeChild(iframe), 3000);
2018-12-18 02:03:08 +03:00
}
function openInEditor(editor: string, path: string, stackFrame: StackFrame) {
2018-12-15 04:04:34 +03:00
const projectPath = path.replace(/\/$/, '');
2019-01-10 21:51:14 +03:00
const file =
stackFrame._originalFileName ||
(stackFrame as unknown as { finalFileName: string }).finalFileName ||
2019-01-10 21:51:14 +03:00
stackFrame.fileName ||
'';
let filePath = /^https?:\/\//.test(file)
? file.replace(/^https?:\/\/[^/]*/, '')
: file.replace(/^\w+:\/\//, '');
2018-12-16 01:02:41 +03:00
filePath = filePath.replace(/^\/~\//, '/node_modules/');
2018-12-15 04:04:34 +03:00
const line = stackFrame._originalLineNumber || stackFrame.lineNumber || '0';
2019-01-10 21:51:14 +03:00
const column =
stackFrame._originalColumnNumber || stackFrame.columnNumber || '0';
2018-12-15 04:04:34 +03:00
let url;
switch (editor) {
2019-01-10 21:51:14 +03:00
case 'vscode':
case 'code':
url = `vscode://file/${projectPath}${filePath}:${line}:${column}`;
break;
2018-12-15 04:04:34 +03:00
case 'atom':
2019-01-10 21:51:14 +03:00
url = `atom://core/open/file?filename=${projectPath}${filePath}&line=${line}&column=${column}`;
break;
case 'webstorm':
case 'phpstorm':
case 'idea':
url = `${editor}://open?file=${projectPath}${filePath}&line=${line}&column=${column}`;
break;
default:
// sublime, emacs, macvim, textmate + custom like https://github.com/eclemens/atom-url-handler
2018-12-15 04:04:34 +03:00
url = `${editor}://open/?url=file://${projectPath}${filePath}&line=${line}&column=${column}`;
}
2018-12-18 02:03:08 +03:00
if (chrome.devtools && !isFF) {
if (chrome.tabs) openAndCloseTab(url);
else window.open(url);
} else {
openInIframe(url);
}
2018-12-15 04:04:34 +03:00
}
export default function openFile(
fileName: string,
lineNumber: number,
stackFrame: StackFrame
) {
2018-12-16 00:50:11 +03:00
if (!chrome || !chrome.storage) return; // TODO: Pass editor settings for using outside of browser extension
2019-01-10 21:51:14 +03:00
const storage = isFF
? chrome.storage.local
: chrome.storage.sync || chrome.storage.local;
storage.get(
['useEditor', 'editor', 'projectPath'],
function ({ useEditor, editor, projectPath }) {
2019-01-10 21:51:14 +03:00
if (
useEditor &&
projectPath &&
typeof editor === 'string' &&
/^\w{1,30}$/.test(editor)
2019-01-10 21:51:14 +03:00
) {
openInEditor(editor.toLowerCase(), projectPath as string, stackFrame);
} else {
if (
chrome.devtools &&
chrome.devtools.panels &&
!!chrome.devtools.panels.openResource
) {
openResource(fileName, lineNumber, stackFrame);
} else if (chrome.runtime && (chrome.runtime.openOptionsPage || isFF)) {
if (chrome.devtools && isFF) {
chrome.devtools.inspectedWindow.eval(
'confirm("Set the editor to open the file in?")',
(result) => {
if (!result) return;
chrome.runtime.sendMessage({ type: 'OPEN_OPTIONS' });
}
);
} else if (confirm('Set the editor to open the file in?')) {
chrome.runtime.openOptionsPage();
}
2018-12-15 04:04:34 +03:00
}
}
2018-12-16 00:50:11 +03:00
}
);
2018-12-15 04:04:34 +03:00
}