Repository navigation
Expand file tree
/
Copy pathglobal.ts
More file actions
88 lines (75 loc) · 3.41 KB
/
Copy pathglobal.ts
File metadata and controls
88 lines (75 loc) · 3.41 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
/**
* @file GitHub1s Ref Related Commands
* @author netcon
*/
import * as vscode from 'vscode';
import router from '@/router';
import { relativeTimeTo } from '@/helpers/date';
import { getRecentRepositories, removeRecentRepository } from '@/helpers/context';
import { adapterManager } from '@/adapters';
export const commandOpenOnOfficialPage = async () => {
const location = (await router.getHistory()).location;
const routerParser = await router.resolveParser();
const fullPath = `${location.pathname}${location.search}${location.hash}`;
const externalLink = await routerParser.buildExternalLink(fullPath);
return vscode.commands.executeCommand('vscode.open', vscode.Uri.parse(externalLink));
};
const repoPickItemButtons = [{ iconPath: new vscode.ThemeIcon('close') }];
const getRecentRepoPickItems = () =>
getRecentRepositories().map((record) => ({
label: record.name,
description: relativeTimeTo(record.timestamp),
buttons: repoPickItemButtons,
}));
export const commandOpenRepository = async () => {
const quickPick = vscode.window.createQuickPick();
const manualInputItem = { label: '' };
let recentRepoPickItems = getRecentRepoPickItems();
const updatePickerItems = () => {
if (manualInputItem.label) {
return (quickPick.items = [...recentRepoPickItems, manualInputItem]);
}
return (quickPick.items = recentRepoPickItems);
};
quickPick.placeholder = 'Select to open...';
updatePickerItems();
quickPick.show();
quickPick.onDidTriggerItemButton(async (event) => {
if (event.button === repoPickItemButtons[0]) {
await removeRecentRepository(event.item.label);
recentRepoPickItems = getRecentRepoPickItems();
updatePickerItems();
}
});
quickPick.onDidChangeValue((value) => {
manualInputItem.label = value ? `Open ${value}...` : '';
updatePickerItems();
});
quickPick.onDidAccept(async () => {
const choice = quickPick.activeItems[0];
const repository = choice === manualInputItem ? quickPick.value : choice.label;
const targetLink = vscode.Uri.parse((await router.href()) || '').with({
path: await (await router.resolveParser()).buildTreePath(repository),
});
vscode.commands.executeCommand('vscode.open', targetLink);
quickPick.hide();
});
};
const commandOpenOnlineEditor = async () => {
const currentScheme = adapterManager.getCurrentScheme();
const onlineEditorPath = ['github1s', 'ossinsight'].includes(currentScheme) ? '/editor' : '/';
const targetLink = vscode.Uri.parse((await router.href()) || '').with({ path: onlineEditorPath });
return vscode.commands.executeCommand('vscode.open', targetLink);
};
export const registerGlobalCommands = (context: vscode.ExtensionContext) => {
return context.subscriptions.push(
vscode.commands.registerCommand('github1s.commands.openOnGitHub', commandOpenOnOfficialPage),
vscode.commands.registerCommand('github1s.commands.openOnGitLab', commandOpenOnOfficialPage),
vscode.commands.registerCommand('github1s.commands.openOnBitbucket', commandOpenOnOfficialPage),
vscode.commands.registerCommand('github1s.commands.openOnNpm', commandOpenOnOfficialPage),
vscode.commands.registerCommand('github1s.commands.openOnOfficialPage', commandOpenOnOfficialPage),
vscode.commands.registerCommand('github1s.commands.openRepository', commandOpenRepository),
vscode.commands.registerCommand('github1s.commands.openOnlineEditor', commandOpenOnlineEditor),
vscode.commands.registerCommand('remoteHub.openRepository', commandOpenRepository)
);
};