@jupyterlab/apputils 4.0.0-alpha.9 → 4.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/clipboard.d.ts +1 -1
- package/lib/dialog.d.ts +66 -4
- package/lib/dialog.js +126 -29
- package/lib/dialog.js.map +1 -1
- package/lib/domutils.js.map +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/inputdialog.d.ts +35 -3
- package/lib/inputdialog.js +122 -12
- package/lib/inputdialog.js.map +1 -1
- package/lib/kernelstatuses.js +26 -25
- package/lib/kernelstatuses.js.map +1 -1
- package/lib/mainareawidget.js +1 -1
- package/lib/mainareawidget.js.map +1 -1
- package/lib/menufactory.js +14 -15
- package/lib/menufactory.js.map +1 -1
- package/lib/notification.d.ts +336 -0
- package/lib/notification.js +310 -0
- package/lib/notification.js.map +1 -0
- package/lib/runningSessions.js +2 -2
- package/lib/runningSessions.js.map +1 -1
- package/lib/sanitizer.d.ts +20 -7
- package/lib/sanitizer.js +34 -7
- package/lib/sanitizer.js.map +1 -1
- package/lib/semanticCommand.js +11 -2
- package/lib/semanticCommand.js.map +1 -1
- package/lib/sessioncontext.d.ts +53 -6
- package/lib/sessioncontext.js +100 -68
- package/lib/sessioncontext.js.map +1 -1
- package/lib/testutils.d.ts +5 -0
- package/lib/testutils.js +46 -0
- package/lib/testutils.js.map +1 -0
- package/lib/thememanager.js +20 -5
- package/lib/thememanager.js.map +1 -1
- package/lib/tokens.d.ts +21 -42
- package/lib/tokens.js.map +1 -1
- package/lib/toolbar/factory.d.ts +2 -1
- package/lib/toolbar/factory.js +135 -105
- package/lib/toolbar/factory.js.map +1 -1
- package/lib/toolbar/index.js +4 -0
- package/lib/toolbar/index.js.map +1 -1
- package/lib/toolbar/registry.d.ts +11 -0
- package/lib/toolbar/registry.js +20 -3
- package/lib/toolbar/registry.js.map +1 -1
- package/lib/toolbar/widget.d.ts +3 -0
- package/lib/toolbar/widget.js +8 -5
- package/lib/toolbar/widget.js.map +1 -1
- package/lib/widgettracker.d.ts +13 -1
- package/lib/widgettracker.js +29 -2
- package/lib/widgettracker.js.map +1 -1
- package/package.json +31 -30
- package/src/clipboard.ts +107 -0
- package/src/commandlinker.ts +209 -0
- package/src/commandpalette.ts +175 -0
- package/src/dialog.tsx +1133 -0
- package/src/domutils.ts +53 -0
- package/src/index.ts +78 -0
- package/src/inputdialog.ts +551 -0
- package/src/kernelstatuses.tsx +279 -0
- package/src/mainareawidget.ts +362 -0
- package/src/menufactory.ts +201 -0
- package/src/notification.ts +559 -0
- package/src/printing.ts +180 -0
- package/src/runningSessions.tsx +233 -0
- package/src/sanitizer.ts +986 -0
- package/src/semanticCommand.ts +77 -0
- package/src/sessioncontext.tsx +1774 -0
- package/src/testutils.ts +49 -0
- package/src/thememanager.ts +530 -0
- package/src/tokens.ts +351 -0
- package/src/toolbar/factory.ts +426 -0
- package/src/toolbar/index.ts +8 -0
- package/src/toolbar/registry.ts +158 -0
- package/src/toolbar/widget.tsx +242 -0
- package/src/widgettracker.ts +391 -0
- package/src/windowresolver.ts +218 -0
- package/style/dialog.css +16 -1
- package/style/inputdialog.css +5 -0
- package/style/materialcolors.css +5 -0
package/src/printing.ts
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
// Copyright (c) Jupyter Development Team.
|
|
2
|
+
// Distributed under the terms of the Modified BSD License.
|
|
3
|
+
|
|
4
|
+
import { ServerConnection } from '@jupyterlab/services';
|
|
5
|
+
import { Widget } from '@lumino/widgets';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Any object is "printable" if it implements the `IPrintable` interface.
|
|
9
|
+
*
|
|
10
|
+
* To do this it, it must have a method called `Printing.symbol` which returns either a function
|
|
11
|
+
* to print the object or null if it cannot be printed.
|
|
12
|
+
*
|
|
13
|
+
* One way of printing is to use the `printWidget` function, which creates a hidden iframe
|
|
14
|
+
* and copies the DOM nodes from your widget to that iframe and printing just that iframe.
|
|
15
|
+
*
|
|
16
|
+
* Another way to print is to use the `printURL` function, which takes a URL and prints that page.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
export namespace Printing {
|
|
20
|
+
/**
|
|
21
|
+
* Function that takes no arguments and when invoked prints out some object or null if printing is not defined.
|
|
22
|
+
*/
|
|
23
|
+
export type OptionalAsyncThunk = (() => Promise<void>) | null;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Symbol to use for a method that returns a function to print an object.
|
|
27
|
+
*/
|
|
28
|
+
export const symbol = Symbol('printable');
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Objects who provide a custom way of printing themselves
|
|
32
|
+
* should implement this interface.
|
|
33
|
+
*/
|
|
34
|
+
export interface IPrintable {
|
|
35
|
+
/**
|
|
36
|
+
* Returns a function to print this object or null if it cannot be printed.
|
|
37
|
+
*/
|
|
38
|
+
[symbol]: () => OptionalAsyncThunk;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Returns whether an object implements a print method.
|
|
43
|
+
*/
|
|
44
|
+
export function isPrintable(a: unknown): a is IPrintable {
|
|
45
|
+
if (typeof a !== 'object' || !a) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
return symbol in a;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Returns the print function for an object, or null if it does not provide a handler.
|
|
53
|
+
*/
|
|
54
|
+
export function getPrintFunction(val: unknown): OptionalAsyncThunk {
|
|
55
|
+
if (isPrintable(val)) {
|
|
56
|
+
return val[symbol]();
|
|
57
|
+
}
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Prints a widget by copying it's DOM node
|
|
63
|
+
* to a hidden iframe and printing that iframe.
|
|
64
|
+
*/
|
|
65
|
+
export function printWidget(widget: Widget): Promise<void> {
|
|
66
|
+
return printContent(widget.node);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Prints a URL by loading it into an iframe.
|
|
71
|
+
*
|
|
72
|
+
* @param url URL to load into an iframe.
|
|
73
|
+
*/
|
|
74
|
+
export async function printURL(url: string): Promise<void> {
|
|
75
|
+
const settings = ServerConnection.makeSettings();
|
|
76
|
+
const text = await (
|
|
77
|
+
await ServerConnection.makeRequest(url, {}, settings)
|
|
78
|
+
).text();
|
|
79
|
+
return printContent(text);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Prints a URL or an element in an iframe and then removes the iframe after printing.
|
|
84
|
+
*/
|
|
85
|
+
async function printContent(textOrEl: string | HTMLElement): Promise<void> {
|
|
86
|
+
const isText = typeof textOrEl === 'string';
|
|
87
|
+
const iframe = createIFrame();
|
|
88
|
+
|
|
89
|
+
const parent = window.document.body;
|
|
90
|
+
parent.appendChild(iframe);
|
|
91
|
+
if (isText) {
|
|
92
|
+
iframe.srcdoc = textOrEl as string;
|
|
93
|
+
await resolveWhenLoaded(iframe);
|
|
94
|
+
} else {
|
|
95
|
+
iframe.src = 'about:blank';
|
|
96
|
+
await resolveWhenLoaded(iframe);
|
|
97
|
+
setIFrameNode(iframe, textOrEl as HTMLElement);
|
|
98
|
+
}
|
|
99
|
+
const printed = resolveAfterEvent();
|
|
100
|
+
launchPrint(iframe.contentWindow!);
|
|
101
|
+
// Once the print dialog has been dismissed, we regain event handling,
|
|
102
|
+
// and it should be safe to discard the hidden iframe.
|
|
103
|
+
await printed;
|
|
104
|
+
parent.removeChild(iframe);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Creates a new hidden iframe and appends it to the document
|
|
109
|
+
*
|
|
110
|
+
* Modified from
|
|
111
|
+
* https://github.com/joseluisq/printd/blob/eb7948d602583c055ab6dee3ee294b6a421da4b6/src/index.ts#L24
|
|
112
|
+
*/
|
|
113
|
+
function createIFrame(): HTMLIFrameElement {
|
|
114
|
+
const el = window.document.createElement('iframe');
|
|
115
|
+
|
|
116
|
+
// We need both allow-modals and allow-same-origin to be able to
|
|
117
|
+
// call print in the iframe.
|
|
118
|
+
// We intentionally do not allow scripts:
|
|
119
|
+
// https://github.com/jupyterlab/jupyterlab/pull/5850#pullrequestreview-230899790
|
|
120
|
+
el.setAttribute('sandbox', 'allow-modals allow-same-origin');
|
|
121
|
+
const css =
|
|
122
|
+
'visibility:hidden;width:0;height:0;position:absolute;z-index:-9999;bottom:0;';
|
|
123
|
+
el.setAttribute('style', css);
|
|
124
|
+
el.setAttribute('width', '0');
|
|
125
|
+
el.setAttribute('height', '0');
|
|
126
|
+
|
|
127
|
+
return el;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Copies a node from the base document to the iframe.
|
|
132
|
+
*/
|
|
133
|
+
function setIFrameNode(iframe: HTMLIFrameElement, node: HTMLElement) {
|
|
134
|
+
iframe.contentDocument!.body.appendChild(node.cloneNode(true));
|
|
135
|
+
iframe.contentDocument!.close();
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Promise that resolves when all resources are loaded in the window.
|
|
140
|
+
*/
|
|
141
|
+
function resolveWhenLoaded(iframe: HTMLIFrameElement): Promise<void> {
|
|
142
|
+
return new Promise(resolve => {
|
|
143
|
+
iframe.onload = () => resolve();
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* A promise that resolves after the next mousedown, mousemove, or
|
|
149
|
+
* keydown event. We use this as a proxy for determining when the
|
|
150
|
+
* main window has regained control after the print dialog is removed.
|
|
151
|
+
*
|
|
152
|
+
* We can't use the usual window.onafterprint handler because we
|
|
153
|
+
* disallow Javascript execution in the print iframe.
|
|
154
|
+
*/
|
|
155
|
+
function resolveAfterEvent(): Promise<void> {
|
|
156
|
+
return new Promise(resolve => {
|
|
157
|
+
const onEvent = () => {
|
|
158
|
+
document.removeEventListener('mousemove', onEvent, true);
|
|
159
|
+
document.removeEventListener('mousedown', onEvent, true);
|
|
160
|
+
document.removeEventListener('keydown', onEvent, true);
|
|
161
|
+
resolve();
|
|
162
|
+
};
|
|
163
|
+
document.addEventListener('mousemove', onEvent, true);
|
|
164
|
+
document.addEventListener('mousedown', onEvent, true);
|
|
165
|
+
document.addEventListener('keydown', onEvent, true);
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Prints a content window.
|
|
171
|
+
*/
|
|
172
|
+
function launchPrint(contentWindow: Window) {
|
|
173
|
+
const result = contentWindow.document.execCommand('print', false);
|
|
174
|
+
// execCommand won't work in firefox so we call the `print` method instead if it fails
|
|
175
|
+
// https://github.com/joseluisq/printd/blob/eb7948d602583c055ab6dee3ee294b6a421da4b6/src/index.ts#L148
|
|
176
|
+
if (!result) {
|
|
177
|
+
contentWindow.print();
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
// Copyright (c) Jupyter Development Team.
|
|
2
|
+
// Distributed under the terms of the Modified BSD License.
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
ServiceManager,
|
|
6
|
+
Session,
|
|
7
|
+
SessionManager,
|
|
8
|
+
Terminal,
|
|
9
|
+
TerminalManager
|
|
10
|
+
} from '@jupyterlab/services';
|
|
11
|
+
import {
|
|
12
|
+
ITranslator,
|
|
13
|
+
nullTranslator,
|
|
14
|
+
TranslationBundle
|
|
15
|
+
} from '@jupyterlab/translation';
|
|
16
|
+
import {
|
|
17
|
+
kernelIcon,
|
|
18
|
+
terminalIcon,
|
|
19
|
+
VDomModel,
|
|
20
|
+
VDomRenderer
|
|
21
|
+
} from '@jupyterlab/ui-components';
|
|
22
|
+
import React from 'react';
|
|
23
|
+
import { GroupItem, TextItem } from '@jupyterlab/statusbar';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Half spacing between subitems in a status item.
|
|
27
|
+
*/
|
|
28
|
+
const HALF_SPACING = 4;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* A pure functional component for rendering kernel and terminal sessions.
|
|
32
|
+
*
|
|
33
|
+
* @param props: the props for the component.
|
|
34
|
+
*
|
|
35
|
+
* @returns a tsx component for the running sessions.
|
|
36
|
+
*/
|
|
37
|
+
function RunningSessionsComponent(
|
|
38
|
+
props: RunningSessionsComponent.IProps
|
|
39
|
+
): React.ReactElement<RunningSessionsComponent.IProps> {
|
|
40
|
+
return (
|
|
41
|
+
<GroupItem spacing={HALF_SPACING} onClick={props.handleClick}>
|
|
42
|
+
<GroupItem spacing={HALF_SPACING}>
|
|
43
|
+
<TextItem source={props.terminals} />
|
|
44
|
+
<terminalIcon.react left={'1px'} top={'3px'} stylesheet={'statusBar'} />
|
|
45
|
+
</GroupItem>
|
|
46
|
+
<GroupItem spacing={HALF_SPACING}>
|
|
47
|
+
<TextItem source={props.sessions} />
|
|
48
|
+
<kernelIcon.react top={'2px'} stylesheet={'statusBar'} />
|
|
49
|
+
</GroupItem>
|
|
50
|
+
</GroupItem>
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* A namespace for RunningSessionsComponents statics.
|
|
56
|
+
*/
|
|
57
|
+
namespace RunningSessionsComponent {
|
|
58
|
+
/**
|
|
59
|
+
* The props for rendering the RunningSessionsComponent.
|
|
60
|
+
*/
|
|
61
|
+
export interface IProps {
|
|
62
|
+
/**
|
|
63
|
+
* A click handler for the component. By default this is used
|
|
64
|
+
* to activate the running sessions side panel.
|
|
65
|
+
*/
|
|
66
|
+
handleClick: () => void;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The number of running kernel sessions.
|
|
70
|
+
*/
|
|
71
|
+
sessions: number;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* The number of active terminal sessions.
|
|
75
|
+
*/
|
|
76
|
+
terminals: number;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* A VDomRenderer for a RunningSessions status item.
|
|
82
|
+
*/
|
|
83
|
+
export class RunningSessions extends VDomRenderer<RunningSessions.Model> {
|
|
84
|
+
/**
|
|
85
|
+
* Create a new RunningSessions widget.
|
|
86
|
+
*/
|
|
87
|
+
constructor(opts: RunningSessions.IOptions) {
|
|
88
|
+
super(new RunningSessions.Model());
|
|
89
|
+
this._serviceManager = opts.serviceManager;
|
|
90
|
+
this._handleClick = opts.onClick;
|
|
91
|
+
this.translator = opts.translator || nullTranslator;
|
|
92
|
+
this._trans = this.translator.load('jupyterload');
|
|
93
|
+
|
|
94
|
+
this._serviceManager.sessions.runningChanged.connect(
|
|
95
|
+
this._onSessionsRunningChanged,
|
|
96
|
+
this
|
|
97
|
+
);
|
|
98
|
+
this._serviceManager.terminals.runningChanged.connect(
|
|
99
|
+
this._onTerminalsRunningChanged,
|
|
100
|
+
this
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
this.addClass('jp-mod-highlighted');
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Render the running sessions widget.
|
|
108
|
+
*/
|
|
109
|
+
render(): JSX.Element | null {
|
|
110
|
+
if (!this.model) {
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
// TODO-TRANS: Should probably be handled differently.
|
|
114
|
+
// This is more localizable friendly: "Terminals: %1 | Kernels: %2"
|
|
115
|
+
this.title.caption = this._trans.__(
|
|
116
|
+
'%1 Terminals, %2 Kernel sessions',
|
|
117
|
+
this.model.terminals,
|
|
118
|
+
this.model!.sessions
|
|
119
|
+
);
|
|
120
|
+
return (
|
|
121
|
+
<RunningSessionsComponent
|
|
122
|
+
sessions={this.model.sessions}
|
|
123
|
+
terminals={this.model.terminals}
|
|
124
|
+
handleClick={this._handleClick}
|
|
125
|
+
/>
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Dispose of the status item.
|
|
131
|
+
*/
|
|
132
|
+
dispose(): void {
|
|
133
|
+
super.dispose();
|
|
134
|
+
|
|
135
|
+
this._serviceManager.sessions.runningChanged.disconnect(
|
|
136
|
+
this._onSessionsRunningChanged,
|
|
137
|
+
this
|
|
138
|
+
);
|
|
139
|
+
this._serviceManager.terminals.runningChanged.disconnect(
|
|
140
|
+
this._onTerminalsRunningChanged,
|
|
141
|
+
this
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Set the number of kernel sessions when the list changes.
|
|
147
|
+
*/
|
|
148
|
+
private _onSessionsRunningChanged(
|
|
149
|
+
manager: SessionManager,
|
|
150
|
+
sessions: Session.IModel[]
|
|
151
|
+
): void {
|
|
152
|
+
this.model!.sessions = sessions.length;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Set the number of terminal sessions when the list changes.
|
|
157
|
+
*/
|
|
158
|
+
private _onTerminalsRunningChanged(
|
|
159
|
+
manager: TerminalManager,
|
|
160
|
+
terminals: Terminal.IModel[]
|
|
161
|
+
): void {
|
|
162
|
+
this.model!.terminals = terminals.length;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
protected translator: ITranslator;
|
|
166
|
+
private _trans: TranslationBundle;
|
|
167
|
+
private _handleClick: () => void;
|
|
168
|
+
private _serviceManager: ServiceManager.IManager;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* A namespace for RunningSessions statics.
|
|
173
|
+
*/
|
|
174
|
+
export namespace RunningSessions {
|
|
175
|
+
/**
|
|
176
|
+
* A VDomModel for the RunningSessions status item.
|
|
177
|
+
*/
|
|
178
|
+
export class Model extends VDomModel {
|
|
179
|
+
/**
|
|
180
|
+
* The number of active kernel sessions.
|
|
181
|
+
*/
|
|
182
|
+
get sessions(): number {
|
|
183
|
+
return this._sessions;
|
|
184
|
+
}
|
|
185
|
+
set sessions(sessions: number) {
|
|
186
|
+
const oldSessions = this._sessions;
|
|
187
|
+
this._sessions = sessions;
|
|
188
|
+
|
|
189
|
+
if (oldSessions !== this._sessions) {
|
|
190
|
+
this.stateChanged.emit(void 0);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* The number of active terminal sessions.
|
|
196
|
+
*/
|
|
197
|
+
get terminals(): number {
|
|
198
|
+
return this._terminals;
|
|
199
|
+
}
|
|
200
|
+
set terminals(terminals: number) {
|
|
201
|
+
const oldTerminals = this._terminals;
|
|
202
|
+
this._terminals = terminals;
|
|
203
|
+
|
|
204
|
+
if (oldTerminals !== this._terminals) {
|
|
205
|
+
this.stateChanged.emit(void 0);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
private _terminals: number = 0;
|
|
210
|
+
private _sessions: number = 0;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Options for creating a RunningSessions item.
|
|
215
|
+
*/
|
|
216
|
+
export interface IOptions {
|
|
217
|
+
/**
|
|
218
|
+
* The application service manager.
|
|
219
|
+
*/
|
|
220
|
+
serviceManager: ServiceManager.IManager;
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* A click handler for the item. By default this is used
|
|
224
|
+
* to activate the running sessions side panel.
|
|
225
|
+
*/
|
|
226
|
+
onClick: () => void;
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* The application language translator.
|
|
230
|
+
*/
|
|
231
|
+
translator?: ITranslator;
|
|
232
|
+
}
|
|
233
|
+
}
|