-
Notifications
You must be signed in to change notification settings - Fork 393
/
Copy pathfactory.ts
101 lines (87 loc) · 3.38 KB
/
factory.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
import {
IToolbarWidgetRegistry,
createToolbarFactory,
} from '@jupyterlab/apputils';
import {
INotebookTracker,
NotebookPanel,
NotebookWidgetFactory,
} from '@jupyterlab/notebook';
import { ISettingRegistry } from '@jupyterlab/settingregistry';
import { DocumentRegistry } from '@jupyterlab/docregistry';
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
import { IEditorServices } from '@jupyterlab/codeeditor';
import { ITranslator, TranslationBundle } from '@jupyterlab/translation';
import { IRisePreviewFactory } from 'jupyterlab-rise';
import { FACTORY, FILE_TYPES } from './tokens';
export function createFactory(
kernelFileTypeNames: string[],
toolbarRegistry: IToolbarWidgetRegistry,
settingRegistry: ISettingRegistry,
docRegistry: DocumentRegistry,
notebookTracker: INotebookTracker,
notebookFactory: NotebookWidgetFactory.IFactory,
contentFactory: NotebookPanel.IContentFactory,
editorServices: IEditorServices,
rendermime: IRenderMimeRegistry,
translator: ITranslator,
trans: TranslationBundle,
riseFactory: IRisePreviewFactory | null
) {
const allFileTypes = FILE_TYPES.concat(kernelFileTypeNames);
// primarily this block is copied/pasted from jlab4 code and specifically
// jupyterlab/packages/notebook-extension/src/index.ts
// inside the function `activateWidgetFactory` at line 1150 as of this writing
//
const toolbarFactory = createToolbarFactory(
toolbarRegistry,
settingRegistry,
'Notebook',
'@jupyterlab/notebook-extension:panel',
translator
);
// Duplicate notebook factory to apply it on Jupytext notebooks
// Mirror: https://github.com/jupyterlab/jupyterlab/blob/8a8c3752564f37493d4eb6b4c59008027fa83880/packages/notebook-extension/src/index.ts#L860
const factory = new NotebookWidgetFactory({
name: FACTORY,
label: trans.__(FACTORY),
fileTypes: allFileTypes,
modelName: notebookFactory.modelName ?? 'notebook',
preferKernel: notebookFactory.preferKernel ?? true,
canStartKernel: notebookFactory.canStartKernel ?? true,
rendermime,
contentFactory,
editorConfig: notebookFactory.editorConfig,
notebookConfig: notebookFactory.notebookConfig,
mimeTypeService: editorServices.mimeTypeService,
toolbarFactory: toolbarFactory,
translator,
});
docRegistry.addWidgetFactory(factory);
// Register widget created with the new factory in the notebook tracker
// This is required to activate notebook commands (and therefore shortcuts)
let id = 0; // The ID counter for notebook panels.
const ft = docRegistry.getFileType('notebook');
factory.widgetCreated.connect((sender, widget) => {
// If the notebook panel does not have an ID, assign it one.
widget.id = widget.id || `notebook-jupytext-${++id}`;
// Set up the title icon
widget.title.icon = ft?.icon;
widget.title.iconClass = ft?.iconClass ?? '';
widget.title.iconLabel = ft?.iconLabel ?? '';
// Notify the widget tracker if restore data needs to update.
widget.context.pathChanged.connect(() => {
// @ts-expect-error Trick using private API
void notebookTracker.save(widget);
});
// Add the notebook panel to the tracker.
// @ts-expect-error Trick using private API
void notebookTracker.add(widget);
});
// Add support for RISE slides
if (riseFactory) {
for (const fileType of allFileTypes) {
riseFactory.addFileType(fileType);
}
}
}