-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
index.tsx
187 lines (172 loc) · 4.89 KB
/
index.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
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
/* -----------------------------------------------------------------------------
| Copyright (c) Jupyter Development Team.
| Distributed under the terms of the Modified BSD License.
|----------------------------------------------------------------------------*/
/**
* @packageDocumentation
* @module htmlviewer-extension
*/
import {
ILayoutRestorer,
JupyterFrontEnd,
JupyterFrontEndPlugin
} from '@jupyterlab/application';
import {
createToolbarFactory,
ICommandPalette,
IToolbarWidgetRegistry,
WidgetTracker
} from '@jupyterlab/apputils';
import { DocumentRegistry } from '@jupyterlab/docregistry';
import {
HTMLViewer,
HTMLViewerFactory,
IHTMLViewerTracker,
ToolbarItems
} from '@jupyterlab/htmlviewer';
import { ISettingRegistry } from '@jupyterlab/settingregistry';
import { ITranslator } from '@jupyterlab/translation';
import { html5Icon } from '@jupyterlab/ui-components';
/**
* Factory name
*/
const FACTORY = 'HTML Viewer';
/**
* Command IDs used by the plugin.
*/
namespace CommandIDs {
export const trustHTML = 'htmlviewer:trust-html';
}
/**
* The HTML file handler extension.
*/
const htmlPlugin: JupyterFrontEndPlugin<IHTMLViewerTracker> = {
activate: activateHTMLViewer,
id: '@jupyterlab/htmlviewer-extension:plugin',
provides: IHTMLViewerTracker,
requires: [ITranslator],
optional: [
ICommandPalette,
ILayoutRestorer,
ISettingRegistry,
IToolbarWidgetRegistry
],
autoStart: true
};
/**
* Activate the HTMLViewer extension.
*/
function activateHTMLViewer(
app: JupyterFrontEnd,
translator: ITranslator,
palette: ICommandPalette | null,
restorer: ILayoutRestorer | null,
settingRegistry: ISettingRegistry | null,
toolbarRegistry: IToolbarWidgetRegistry | null
): IHTMLViewerTracker {
let toolbarFactory:
| ((widget: HTMLViewer) => DocumentRegistry.IToolbarItem[])
| undefined;
const trans = translator.load('jupyterlab');
if (toolbarRegistry) {
toolbarRegistry.registerFactory<HTMLViewer>(FACTORY, 'refresh', widget =>
ToolbarItems.createRefreshButton(widget, translator)
);
toolbarRegistry.registerFactory<HTMLViewer>(FACTORY, 'trust', widget =>
ToolbarItems.createTrustButton(widget, translator)
);
if (settingRegistry) {
toolbarFactory = createToolbarFactory(
toolbarRegistry,
settingRegistry,
FACTORY,
htmlPlugin.id,
translator
);
}
}
// Add an HTML file type to the docregistry.
const ft: DocumentRegistry.IFileType = {
name: 'html',
contentType: 'file',
fileFormat: 'text',
displayName: trans.__('HTML File'),
extensions: ['.html'],
mimeTypes: ['text/html'],
icon: html5Icon
};
app.docRegistry.addFileType(ft);
// Create a new viewer factory.
const factory = new HTMLViewerFactory({
name: FACTORY,
fileTypes: ['html'],
defaultFor: ['html'],
readOnly: true,
toolbarFactory,
translator
});
// Create a widget tracker for HTML documents.
const tracker = new WidgetTracker<HTMLViewer>({
namespace: 'htmlviewer'
});
// Handle state restoration.
if (restorer) {
void restorer.restore(tracker, {
command: 'docmanager:open',
args: widget => ({ path: widget.context.path, factory: 'HTML Viewer' }),
name: widget => widget.context.path
});
}
app.docRegistry.addWidgetFactory(factory);
factory.widgetCreated.connect((sender, widget) => {
// Track the widget.
void tracker.add(widget);
// Notify the widget tracker if restore data needs to update.
widget.context.pathChanged.connect(() => {
void tracker.save(widget);
});
// Notify the application when the trust state changes so it
// can update any renderings of the trust command.
widget.trustedChanged.connect(() => {
app.commands.notifyCommandChanged(CommandIDs.trustHTML);
});
widget.title.icon = ft.icon!;
widget.title.iconClass = ft.iconClass ?? '';
widget.title.iconLabel = ft.iconLabel ?? '';
});
// Add a command to trust the active HTML document,
// allowing script executions in its context.
app.commands.addCommand(CommandIDs.trustHTML, {
label: trans.__('Trust HTML File'),
caption: trans.__(`Whether the HTML file is trusted.
Trusting the file allows scripts to run in it,
which may result in security risks.
Only enable for files you trust.`),
isEnabled: () => !!tracker.currentWidget,
isToggled: () => {
const current = tracker.currentWidget;
if (!current) {
return false;
}
return current.trusted;
},
execute: () => {
const current = tracker.currentWidget;
if (!current) {
return;
}
current.trusted = !current.trusted;
}
});
if (palette) {
palette.addItem({
command: CommandIDs.trustHTML,
category: trans.__('File Operations')
});
}
return tracker;
}
/**
* Export the plugins as default.
*/
export default htmlPlugin;