-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathindex.ts
275 lines (240 loc) · 8.63 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import {
JupyterFrontEnd,
JupyterFrontEndPlugin,
ILayoutRestorer
} from '@jupyterlab/application';
import { jsonIcon } from '@jupyterlab/ui-components';
import { WidgetTracker, ICommandPalette } from '@jupyterlab/apputils';
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
import { IEditorServices } from '@jupyterlab/codeeditor';
import { CodeCell } from '@jupyterlab/cells';
import { IFileBrowserFactory } from '@jupyterlab/filebrowser';
import { ILauncher } from '@jupyterlab/launcher';
import { ITranslator } from '@jupyterlab/translation';
import { ISettingRegistry } from '@jupyterlab/settingregistry';
import { IKernelMenu, IMainMenu } from '@jupyterlab/mainmenu';
import { IJupyterWidgetRegistry } from '@jupyter-widgets/base';
import {
WidgetRenderer,
registerWidgetManager
} from '@jupyter-widgets/jupyterlab-manager';
import { BlocklyEditorFactory } from 'jupyterlab-blockly';
import { IBlocklyRegistry } from 'jupyterlab-blockly';
import { BlocklyEditor } from 'jupyterlab-blockly';
import { blockly_icon } from './icons';
/**
* The name of the factory that creates the editor widgets.
*/
const FACTORY = 'Blockly editor';
const PALETTE_CATEGORY = 'Blockly editor';
namespace CommandIDs {
export const createNew = 'blockly:create-new-blockly-file';
}
/**
* The id of the translation plugin.
*/
const PLUGIN_ID = '@jupyterlab/translation-extension:plugin';
/**
* Initialization data for the jupyterlab-blocky extension.
*/
const plugin: JupyterFrontEndPlugin<IBlocklyRegistry> = {
id: 'jupyterlab-blocky:plugin',
autoStart: true,
requires: [
ILayoutRestorer,
IRenderMimeRegistry,
IEditorServices,
IFileBrowserFactory,
ISettingRegistry,
ITranslator
],
optional: [ILauncher, ICommandPalette, IMainMenu, IJupyterWidgetRegistry],
provides: IBlocklyRegistry,
activate: (
app: JupyterFrontEnd,
restorer: ILayoutRestorer,
rendermime: IRenderMimeRegistry,
editorServices: IEditorServices,
browserFactory: IFileBrowserFactory,
settings: ISettingRegistry,
translator: ITranslator,
launcher: ILauncher | null,
palette: ICommandPalette | null,
mainMenu: IMainMenu | null,
widgetRegistry: IJupyterWidgetRegistry | null
): IBlocklyRegistry => {
console.log('JupyterLab extension jupyterlab-blocky is activated!');
// Namespace for the tracker
const namespace = 'jupyterlab-blocky';
// Creating the tracker for the document
const tracker = new WidgetTracker<BlocklyEditor>({ namespace });
// Handle state restoration.
if (restorer) {
// When restoring the app, if the document was open, reopen it
restorer.restore(tracker, {
command: 'docmanager:open',
args: widget => ({ path: widget.context.path, factory: FACTORY }),
name: widget => widget.context.path
});
}
const { commands } = app;
// Creating the widget factory to register it so the document manager knows about
// our new DocumentWidget
const widgetFactory = new BlocklyEditorFactory({
name: FACTORY,
modelName: 'text',
fileTypes: ['blockly'],
defaultFor: ['blockly'],
// Kernel options, in this case we need to execute the code generated
// in the blockly editor. The best way would be to use kernels, for
// that reason, we tell the widget factory to start a kernel session
// when opening the editor, and close the session when closing the editor.
canStartKernel: true,
preferKernel: true,
shutdownOnClose: true,
// The rendermime instance, necessary to render the outputs
// after a code execution. And the mimeType service to get the
// mimeType from the kernel language
rendermime: rendermime,
mimetypeService: editorServices.mimeTypeService,
// The translator instance, used for the internalization of the plugin.
translator: translator
});
// Add the widget to the tracker when it's created
widgetFactory.widgetCreated.connect((sender, widget) => {
// Adding the Blockly icon for the widget so it appears next to the file name.
widget.title.icon = blockly_icon;
// Notify the instance tracker if restore data needs to update.
widget.context.pathChanged.connect(() => {
tracker.save(widget);
});
tracker.add(widget);
});
// Registering the file type
app.docRegistry.addFileType({
name: 'blockly',
displayName: 'Blockly',
contentType: 'file',
fileFormat: 'json',
extensions: ['.jpblockly'],
mimeTypes: ['application/json'],
icon: jsonIcon,
iconLabel: 'JupyterLab-Blockly'
});
// Registering the widget factory
app.docRegistry.addWidgetFactory(widgetFactory);
function getSetting(setting: ISettingRegistry.ISettings): string {
// Read the settings and convert to the correct type
const currentLocale: string = setting.get('locale').composite as string;
return currentLocale;
}
// Wait for the application to be restored and
// for the settings for this plugin to be loaded
settings.load(PLUGIN_ID).then(setting => {
// Read the settings
const currentLocale = getSetting(setting);
// Listen for our plugin setting changes using Signal
setting.changed.connect(getSetting);
// Get new language and call the function that modifies the language name accordingly.
// Also, make the transformation to have the name of the language package as in Blockly.
const language =
currentLocale[currentLocale.length - 2].toUpperCase() +
currentLocale[currentLocale.length - 1].toLowerCase();
console.log(`Current Language : '${language}'`);
// Transmitting the current language to the manager.
widgetFactory.registry.setlanguage(language);
});
commands.addCommand(CommandIDs.createNew, {
label: args =>
args['isPalette'] ? 'New Blockly Editor' : 'Blockly Editor',
caption: 'Create a new Blockly Editor',
icon: args => (args['isPalette'] ? null : blockly_icon),
execute: async args => {
// Get the directory in which the Blockly file must be created;
// otherwise take the current filebrowser directory
const cwd =
args['cwd'] || browserFactory.tracker.currentWidget.model.path;
// Create a new untitled Blockly file
const model = await commands.execute('docmanager:new-untitled', {
path: cwd,
type: 'file',
ext: '.jpblockly'
});
// Open the newly created file with the 'Editor'
return commands.execute('docmanager:open', {
path: model.path,
factory: FACTORY
});
}
});
// Add the command to the launcher
if (launcher) {
launcher.add({
command: CommandIDs.createNew,
category: 'Other',
rank: 1
});
}
// Add the command to the palette
if (palette) {
palette.addItem({
command: CommandIDs.createNew,
args: { isPalette: true },
category: PALETTE_CATEGORY
});
}
// Add the command to the main menu
if (mainMenu) {
mainMenu.kernelMenu.kernelUsers.add({
tracker,
interruptKernel: current => {
const kernel = current.context.sessionContext.session?.kernel;
if (kernel) {
return kernel.interrupt();
}
return Promise.resolve(void 0);
},
reconnectToKernel: current => {
const kernel = current.context.sessionContext.session?.kernel;
if (kernel) {
return kernel.reconnect();
}
return Promise.resolve(void 0);
},
restartKernel: current => {
const kernel = current.context.sessionContext.session?.kernel;
if (kernel) {
return kernel.restart();
}
return Promise.resolve(void 0);
},
shutdownKernel: current => current.context.sessionContext.shutdown()
} as IKernelMenu.IKernelUser<BlocklyEditor>);
}
if (widgetRegistry) {
tracker.forEach(panel => {
registerWidgetManager(
panel.context as any,
panel.content.rendermime,
widgetRenderers([panel.content.cell])
);
});
tracker.widgetAdded.connect((sender, panel) => {
registerWidgetManager(
panel.context as any,
panel.content.rendermime,
widgetRenderers([panel.content.cell])
);
});
}
return widgetFactory.registry;
}
};
function* widgetRenderers(cells: CodeCell[]): IterableIterator<WidgetRenderer> {
for (const w of cells) {
if (w instanceof WidgetRenderer) {
yield w;
}
}
}
export default plugin;