forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
144 lines (117 loc) · 3.16 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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import {
Menu
} from '@phosphor/widgets';
import {
JupyterLab, JupyterLabPlugin
} from '@jupyterlab/application';
import {
IEditorServices
} from '@jupyterlab/codeeditor';
import {
ICommandPalette, IMainMenu
} from '@jupyterlab/apputils';
import {
IEditorTracker
} from '@jupyterlab/editorwidget';
import {
MonacoCodeEditorFactory, MonacoMimeTypeService, MonacoCodeEditor
} from '@jupyterlab/monaco';
/**
* The editor services.
*/
export
const servicesPlugin: JupyterLabPlugin<IEditorServices> = {
id: IEditorServices.name,
provides: IEditorServices,
activate: (): IEditorServices => {
const factoryService = new MonacoCodeEditorFactory();
const mimeTypeService = new MonacoMimeTypeService();
return { factoryService, mimeTypeService };
}
};
/**
* The editor commands.
*/
export
const commandsPlugin: JupyterLabPlugin<void> = {
id: 'jupyter.services.editor-commands',
requires: [IEditorTracker, IMainMenu, ICommandPalette],
activate: activateEditorCommands,
autoStart: true
};
/**
* Export the plugins as default.
*/
const plugins: JupyterLabPlugin<any>[] = [commandsPlugin, servicesPlugin];
export default plugins;
/**
* The default theme.
*/
const DEFAULT_THEME = 'default-theme';
/**
* Themes.
*/
const THEMES = [
'vs', 'vs-dark', 'hc-black'
];
/**
* The map of command ids used by the editor.
*/
const cmdIds = {
lineNumbers: 'editor:line-numbers',
lineWrap: 'editor:line-wrap',
createConsole: 'editor:create-console',
runCode: 'editor:run-code',
changeTheme: 'editor:change-theme'
};
/**
* Set up the editor widget menu and commands.
*/
function activateEditorCommands(app: JupyterLab, tracker: IEditorTracker, mainMenu: IMainMenu, palette: ICommandPalette): void {
let { commands } = app;
/**
* Create a menu for the editor.
*/
function createMenu(): Menu {
let settings = new Menu({ commands });
let themeMenu = new Menu({ commands });
let menu = new Menu({ commands });
menu.title.label = 'Editor';
settings.title.label = 'Settings';
themeMenu.title.label = 'Theme';
settings.addItem({ command: cmdIds.lineNumbers });
commands.addCommand(cmdIds.changeTheme, {
label: args => args['theme'] as string,
execute: args => {
let name: string = args['theme'] as string || DEFAULT_THEME;
tracker.forEach(widget => {
const editor = widget.editor;
if (editor instanceof MonacoCodeEditor) {
editor.editor.updateOptions({
theme: name
});
}
});
}
});
for (const theme of THEMES) {
themeMenu.addItem({
command: cmdIds.changeTheme,
args: { theme }
});
}
menu.addItem({ type: 'separator' });
menu.addItem({ type: 'submenu', submenu: settings });
// menu.addItem({ type: 'submenu', menu: themeMenu });
return menu;
}
mainMenu.addMenu(createMenu(), { rank: 30 });
[
cmdIds.lineNumbers,
cmdIds.lineWrap,
cmdIds.createConsole,
cmdIds.runCode,
].forEach(command => palette.addItem({ command, category: 'Editor' }));
}