Repository navigation
Expand file tree
/
Copy pathplugin.ts
More file actions
159 lines (151 loc) · 4.31 KB
/
Copy pathplugin.ts
File metadata and controls
159 lines (151 loc) · 4.31 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
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
import {
ILabShell,
JupyterFrontEnd,
JupyterFrontEndPlugin
} from '@jupyterlab/application';
import {
IThemeManager,
IWidgetTracker,
WidgetTracker
} from '@jupyterlab/apputils';
import { IEditorServices } from '@jupyterlab/codeeditor';
import { PathExt } from '@jupyterlab/coreutils';
import { IDocumentManager } from '@jupyterlab/docmanager';
import { IDefaultFileBrowser } from '@jupyterlab/filebrowser';
import { INotebookTracker, NotebookPanel } from '@jupyterlab/notebook';
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
import { IKernelSpecManager } from '@jupyterlab/services';
import { Widget } from '@lumino/widgets';
import {
ISpectaDocTracker,
ISpectaLayoutRegistry,
ISpectaShell
} from '../token';
import {
configLabLayout,
createFileBrowser,
hideAppLoadingIndicator,
isSpectaApp,
readSpectaConfig,
registerDocumentFactory
} from '../tool';
const activate = (
app: JupyterFrontEnd<ISpectaShell>,
rendermime: IRenderMimeRegistry,
tracker: INotebookTracker,
editorServices: IEditorServices,
contentFactory: NotebookPanel.IContentFactory,
spectaLayoutRegistry: ISpectaLayoutRegistry,
themeManager: IThemeManager
): IWidgetTracker => {
const namespace = 'specta';
const spectaTracker = new WidgetTracker<Widget>({ namespace });
registerDocumentFactory({
factoryName: 'specta',
app,
rendermime,
tracker,
editorServices,
contentFactory,
spectaTracker,
spectaLayoutRegistry,
themeManager
});
return spectaTracker;
};
export const spectaDocument: JupyterFrontEndPlugin<
IWidgetTracker,
ISpectaShell
> = {
id: 'specta:notebook-doc',
autoStart: true,
requires: [
IRenderMimeRegistry,
INotebookTracker,
IEditorServices,
NotebookPanel.IContentFactory,
ISpectaLayoutRegistry,
IThemeManager
],
activate,
provides: ISpectaDocTracker
};
export const spectaOpener: JupyterFrontEndPlugin<void, ILabShell> = {
id: 'specta/application-extension:opener',
autoStart: true,
requires: [
IDocumentManager,
IDefaultFileBrowser,
ISpectaDocTracker,
IKernelSpecManager
],
activate: async (
app: JupyterFrontEnd<ILabShell>,
docManager: IDocumentManager,
defaultBrowser: IDefaultFileBrowser
): Promise<void> => {
const urlParams = new URLSearchParams(window.location.search);
if (!isSpectaApp()) {
// Not a specta app
const path = urlParams.get('specta-path');
if (!path) {
return;
}
app.restored.then(async () => {
const labShell = app.shell;
if (PathExt.extname(path) === '.ipynb') {
const commands = app.commands;
const spectaConfig = readSpectaConfig({});
console.log('spectaConfig', spectaConfig);
await configLabLayout({
config: spectaConfig.labConfig,
labShell,
commands
});
const widget = docManager.openOrReveal(path, 'specta');
if (widget) {
app.shell.add(widget, 'main');
}
}
});
return;
} else {
// Specta app
const path = urlParams.get('path');
if (!path) {
const browser = createFileBrowser({ defaultBrowser });
app.shell.add(browser, 'main', { rank: 100 });
hideAppLoadingIndicator();
} else {
if (PathExt.extname(path) === '.ipynb') {
app.shell.addClass('specta-document-viewer');
const widget = docManager.openOrReveal(path, 'specta');
if (widget) {
app.shell.add(widget, 'main');
}
} else {
let count = 0;
const tryOpen = () => {
const widget = docManager.openOrReveal(path, 'default');
if (widget) {
app.shell.add(widget, 'main');
hideAppLoadingIndicator();
} else {
count++;
if (count > 10) {
console.error('Failed to open file', path);
const widget = new Widget();
widget.node.innerHTML = `<h2 style="text-align: center; margin-top: 200px;">Failed to open file ${path}</h2>`;
app.shell.add(widget, 'main');
hideAppLoadingIndicator();
return;
}
setTimeout(tryOpen, 100);
}
};
tryOpen();
}
}
}
}
};