forked from jupyterlab/jupyterlab-git
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
146 lines (115 loc) · 3.77 KB
/
index.ts
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
import { addCommands, CommandIDs } from './gitMenuCommands';
import { PathExt } from '@jupyterlab/coreutils';
import { GitWidget } from './components/GitWidget';
import {
ILayoutRestorer,
JupyterLab,
JupyterLabPlugin
} from '@jupyterlab/application';
import {
IFileBrowserFactory
} from '@jupyterlab/filebrowser'
import {IMainMenu } from '@jupyterlab/mainmenu';
import { Menu } from '@phosphor/widgets';
import { Token } from '@phosphor/coreutils';
import { gitTabStyle } from './componentsStyle/GitWidgetStyle';
import { IDiffCallback } from './git';
export { IDiffCallback } from './git';
import '../style/variables.css';
import {GitClone} from "./gitClone";
export const EXTENSION_ID = 'jupyter.extensions.git_plugin';
export const IGitExtension = new Token<IGitExtension>(EXTENSION_ID);
/** Interface for extension class */
export interface IGitExtension {
registerDiffProvider(filetypes: string[], callback: IDiffCallback): void;
}
/**
* The default running sessions extension.
*/
const plugin: JupyterLabPlugin<IGitExtension> = {
id: 'jupyter.extensions.running-sessions-git',
requires: [IMainMenu, ILayoutRestorer, IFileBrowserFactory],
provides: IGitExtension,
activate,
autoStart: true
};
/**
* Export the plugin as default.
*/
export default plugin;
/** Main extension class */
export class GitExtension implements IGitExtension {
git_plugin: GitWidget;
git_clone_widget: GitClone;
constructor(app: JupyterLab, restorer: ILayoutRestorer, factory: IFileBrowserFactory) {
this.app = app;
this.git_plugin = new GitWidget(
app,
{ manager: app.serviceManager },
this.performDiff.bind(this)
);
this.git_plugin.id = 'jp-git-sessions';
this.git_plugin.title.iconClass = `jp-SideBar-tabIcon ${gitTabStyle}`;
this.git_plugin.title.caption = 'Git';
// Let the application restorer track the running panel for restoration of
// application state (e.g. setting the running panel as the current side bar
// widget).
restorer.add(this.git_plugin, 'git-sessions');
app.shell.addToLeftArea(this.git_plugin, { rank: 200 });
this.git_clone_widget = new GitClone(factory);
}
registerDiffProvider(filetypes: string[], callback: IDiffCallback): void {
filetypes.forEach(fileType => {
this.diffProviders[fileType] = callback;
});
}
performDiff(
filename: string,
revisionA: string,
revisionB: string
) {
let extension = PathExt.extname(filename).toLocaleLowerCase();
if (this.diffProviders[extension] !== undefined) {
this.diffProviders[extension](filename, revisionA, revisionB);
} else {
this.app.commands.execute('git:terminal-cmd', {
cmd: 'git diff ' + revisionA + ' ' + revisionB
});
}
}
private app: JupyterLab;
private diffProviders: { [key: string]: IDiffCallback } = {};
}
/**
* Activate the running plugin.
*/
function activate(
app: JupyterLab,
mainMenu: IMainMenu,
restorer: ILayoutRestorer,
factory: IFileBrowserFactory
): IGitExtension {
const { commands } = app;
let git_extension = new GitExtension(app, restorer, factory);
const category = 'Git';
// Rank has been chosen somewhat arbitrarily to give priority to the running
// sessions widget in the sidebar.
addCommands(app, app.serviceManager);
let menu = new Menu({ commands });
let tutorial = new Menu({ commands });
tutorial.title.label = ' Tutorial ';
menu.title.label = category;
[
CommandIDs.gitUI,
CommandIDs.gitTerminal,
CommandIDs.gitInit
].forEach(command => {
menu.addItem({ command });
});
[CommandIDs.setupRemotes, CommandIDs.googleLink].forEach(command => {
tutorial.addItem({ command });
});
menu.addItem({ type: 'submenu', submenu: tutorial });
mainMenu.addMenu(menu, { rank: 60 });
return git_extension;
}