This repository has been archived by the owner on Jun 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
page.tsx
150 lines (125 loc) · 4.02 KB
/
page.tsx
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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { CommandRegistry } from '@phosphor/commands';
import { Widget } from '@phosphor/widgets';
import { ServiceManager } from '@jupyterlab/services';
import { PathExt, PageConfig } from '@jupyterlab/coreutils';
import {
NotebookPanel,
NotebookWidgetFactory,
NotebookModelFactory
} from '@jupyterlab/notebook';
import { Completer, CompletionHandler } from '@jupyterlab/completer';
import { editorServices } from '@jupyterlab/codemirror';
import { DocumentManager } from '@jupyterlab/docmanager';
import { DocumentRegistry } from '@jupyterlab/docregistry';
import { RenderMimeRegistry } from '@jupyterlab/rendermime';
import { Header } from '../components/header';
import { NotebookComponent } from './component';
import { CompleterComponent } from '../components/completer';
import * as React from 'react';
interface NotebookPageProps {
serviceManager: ServiceManager.IManager;
notebookPath: string;
rendermime: RenderMimeRegistry;
}
/**
* Notebook application component
*/
export class NotebookPage extends React.Component<NotebookPageProps> {
commands: CommandRegistry;
nbWidget: NotebookPanel;
completer: Completer;
completionHandler: CompletionHandler;
constructor(props: NotebookPageProps) {
super(props);
// Initialize the command registry with the bindings.
this.commands = new CommandRegistry();
let useCapture = true;
// Setup the keydown listener for the document.
document.addEventListener(
'keydown',
event => {
this.commands.processKeydownEvent(event);
},
useCapture
);
let opener = {
open: (widget: Widget) => {
// Do nothing for sibling widgets for now.
}
};
let docRegistry = new DocumentRegistry();
let docManager = new DocumentManager({
registry: docRegistry,
manager: this.props.serviceManager,
opener
});
let mFactory = new NotebookModelFactory({});
let editorFactory = editorServices.factoryService.newInlineEditor;
let contentFactory = new NotebookPanel.ContentFactory({ editorFactory });
let wFactory = new NotebookWidgetFactory({
name: 'Notebook',
modelName: 'notebook',
fileTypes: ['notebook'],
defaultFor: ['notebook'],
preferKernel: true,
canStartKernel: true,
rendermime: this.props.rendermime,
contentFactory,
mimeTypeService: editorServices.mimeTypeService
});
docRegistry.addModelFactory(mFactory);
docRegistry.addWidgetFactory(wFactory);
this.nbWidget = docManager.open(this.props.notebookPath) as NotebookPanel;
this.addCommands();
}
addCommands = () => {
let commands = this.commands;
commands.addCommand('switch:lab', {
label: 'Open in JupyterLab',
execute: () => {
const labUrl =
PageConfig.getBaseUrl() + 'lab/tree/' + this.props.notebookPath;
window.location.href = labUrl;
}
});
commands.addCommand('switch:classic', {
label: 'Open in Classic Jupyter',
execute: () => {
const classicUrl =
PageConfig.getBaseUrl() + 'tree/' + this.props.notebookPath;
window.location.href = classicUrl;
}
});
return commands;
};
commandOrder = ['notebook:download', '-', 'switch:lab', 'switch:classic'];
render() {
// FIXME: Better way of getting rid of extension?
const notebookName = PathExt.basename(this.props.notebookPath).replace(
'.ipynb',
''
);
return [
<Header
title={notebookName}
key="page-header"
/>,
<main key="main">
<NotebookComponent
id="main-container"
commands={this.commands}
notebookWidget={this.nbWidget}
notebookPath={this.props.notebookPath}
contentsManager={this.props.serviceManager.contents}
/>
</main>,
<CompleterComponent
key="completer"
commands={this.commands}
notebookPanel={this.nbWidget}
/>
];
}
}