@jupyterlite/repl-extension 0.2.2 → 0.2.3

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.
Files changed (2) hide show
  1. package/package.json +11 -10
  2. package/src/index.ts +238 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jupyterlite/repl-extension",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "description": "JupyterLite - Console Extension",
5
5
  "homepage": "https://github.com/jupyterlite/jupyterlite",
6
6
  "bugs": {
@@ -27,7 +27,8 @@
27
27
  "lib/*.js.map",
28
28
  "lib/*.js",
29
29
  "style/*.css",
30
- "style/index.js"
30
+ "style/index.js",
31
+ "src/**/*.{ts,tsx}"
31
32
  ],
32
33
  "scripts": {
33
34
  "build": "tsc -b",
@@ -37,14 +38,14 @@
37
38
  "watch": "tsc -b --watch"
38
39
  },
39
40
  "dependencies": {
40
- "@jupyterlab/application": "~4.0.9",
41
- "@jupyterlab/apputils": "~4.1.9",
42
- "@jupyterlab/console": "~4.0.9",
43
- "@jupyterlab/translation": "~4.0.9",
44
- "@jupyterlab/ui-components": "~4.0.9",
45
- "@jupyterlite/application": "^0.2.2",
46
- "@jupyterlite/ui-components": "^0.2.2",
47
- "@lumino/widgets": "^2.3.0"
41
+ "@jupyterlab/application": "~4.0.11",
42
+ "@jupyterlab/apputils": "~4.1.11",
43
+ "@jupyterlab/console": "~4.0.11",
44
+ "@jupyterlab/translation": "~4.0.11",
45
+ "@jupyterlab/ui-components": "~4.0.11",
46
+ "@jupyterlite/application": "^0.2.3",
47
+ "@jupyterlite/ui-components": "^0.2.3",
48
+ "@lumino/widgets": "^2.3.1"
48
49
  },
49
50
  "devDependencies": {
50
51
  "@babel/core": "^7.11.6",
package/src/index.ts ADDED
@@ -0,0 +1,238 @@
1
+ // Copyright (c) Jupyter Development Team.
2
+ // Distributed under the terms of the Modified BSD License.
3
+
4
+ import {
5
+ ILabStatus,
6
+ IRouter,
7
+ JupyterFrontEndPlugin,
8
+ JupyterFrontEnd,
9
+ Router,
10
+ } from '@jupyterlab/application';
11
+
12
+ import { CommandToolbarButton, IThemeManager, Toolbar } from '@jupyterlab/apputils';
13
+
14
+ import { IConsoleTracker } from '@jupyterlab/console';
15
+
16
+ import { ITranslator } from '@jupyterlab/translation';
17
+
18
+ import { clearIcon, refreshIcon, runIcon } from '@jupyterlab/ui-components';
19
+
20
+ import { SingleWidgetApp } from '@jupyterlite/application';
21
+
22
+ import { liteIcon } from '@jupyterlite/ui-components';
23
+
24
+ import { Widget } from '@lumino/widgets';
25
+
26
+ /**
27
+ * The name of the translation bundle for internationalized strings.
28
+ */
29
+ const I18N_BUNDLE = 'jupyterlite';
30
+
31
+ /**
32
+ * A plugin to add buttons to the console toolbar.
33
+ */
34
+ const buttons: JupyterFrontEndPlugin<void> = {
35
+ id: '@jupyterlite/console-application:buttons',
36
+ autoStart: true,
37
+ requires: [ITranslator],
38
+ optional: [IConsoleTracker],
39
+ activate: (
40
+ app: JupyterFrontEnd,
41
+ translator: ITranslator,
42
+ tracker: IConsoleTracker | null,
43
+ ) => {
44
+ if (!tracker) {
45
+ return;
46
+ }
47
+
48
+ const { commands } = app;
49
+ const trans = translator.load(I18N_BUNDLE);
50
+
51
+ // wrapper commands to be able to override the icon
52
+ const runCommand = 'repl:run';
53
+ commands.addCommand(runCommand, {
54
+ caption: trans.__('Run'),
55
+ icon: runIcon,
56
+ execute: () => {
57
+ return commands.execute('console:run-forced');
58
+ },
59
+ });
60
+
61
+ const runButton = new CommandToolbarButton({
62
+ commands,
63
+ id: runCommand,
64
+ });
65
+
66
+ const restartCommand = 'repl:restart';
67
+ commands.addCommand(restartCommand, {
68
+ caption: trans.__('Restart'),
69
+ icon: refreshIcon,
70
+ execute: () => {
71
+ return commands.execute('console:restart-kernel');
72
+ },
73
+ });
74
+
75
+ const restartButton = new CommandToolbarButton({
76
+ commands,
77
+ id: restartCommand,
78
+ });
79
+
80
+ const clearCommand = 'repl:clear';
81
+ commands.addCommand(clearCommand, {
82
+ caption: trans.__('Clear'),
83
+ icon: clearIcon,
84
+ execute: () => {
85
+ return commands.execute('console:clear');
86
+ },
87
+ });
88
+
89
+ const clearButton = new CommandToolbarButton({
90
+ commands,
91
+ id: clearCommand,
92
+ });
93
+
94
+ tracker.widgetAdded.connect((_, console) => {
95
+ const { toolbar } = console;
96
+
97
+ console.toolbar.addItem('run', runButton);
98
+ console.toolbar.addItem('restart', restartButton);
99
+ console.toolbar.addItem('clear', clearButton);
100
+
101
+ toolbar.addItem('spacer', Toolbar.createSpacerItem());
102
+
103
+ const node = document.createElement('a');
104
+ node.title = trans.__('Powered by JupyterLite');
105
+ node.href = 'https://github.com/jupyterlite/jupyterlite';
106
+ node.target = '_blank';
107
+ node.rel = 'noopener noreferrer';
108
+ const poweredBy = new Widget({ node });
109
+ liteIcon.element({
110
+ container: node,
111
+ elementPosition: 'center',
112
+ margin: '2px 2px 2px 8px',
113
+ height: 'auto',
114
+ width: '16px',
115
+ });
116
+
117
+ poweredBy.addClass('jp-PoweredBy');
118
+ toolbar.insertAfter('spacer', 'powered-by', poweredBy);
119
+ });
120
+ },
121
+ };
122
+
123
+ /**
124
+ * A plugin to open a code console and
125
+ * parse custom parameters from the query string arguments.
126
+ */
127
+ const consolePlugin: JupyterFrontEndPlugin<void> = {
128
+ id: '@jupyterlite/repl-extension:console',
129
+ autoStart: true,
130
+ optional: [IConsoleTracker, IThemeManager],
131
+ activate: (
132
+ app: JupyterFrontEnd,
133
+ tracker: IConsoleTracker | null,
134
+ themeManager: IThemeManager | null,
135
+ ) => {
136
+ if (!tracker) {
137
+ return;
138
+ }
139
+ const { commands, serviceManager, started } = app;
140
+
141
+ const search = window.location.search;
142
+ const urlParams = new URLSearchParams(search);
143
+ const code = urlParams.getAll('code');
144
+ const kernel = urlParams.get('kernel') || undefined;
145
+ const theme = urlParams.get('theme')?.trim();
146
+ const toolbar = urlParams.get('toolbar');
147
+
148
+ Promise.all([started, serviceManager.ready]).then(async () => {
149
+ commands.execute('console:create', { kernelPreference: { name: kernel } });
150
+ });
151
+
152
+ if (theme && themeManager) {
153
+ const themeName = decodeURIComponent(theme);
154
+ themeManager.setTheme(themeName);
155
+ }
156
+
157
+ tracker.widgetAdded.connect(async (_, widget) => {
158
+ const { console } = widget;
159
+
160
+ if (!toolbar) {
161
+ // hide the toolbar by default if not specified
162
+ widget.toolbar.dispose();
163
+ }
164
+
165
+ if (code) {
166
+ await console.sessionContext.ready;
167
+ code.forEach((line) => console.inject(line));
168
+ }
169
+ });
170
+ },
171
+ };
172
+
173
+ /**
174
+ * The default JupyterLab application status provider.
175
+ */
176
+ const status: JupyterFrontEndPlugin<ILabStatus> = {
177
+ id: '@jupyterlite/repl-extension:status',
178
+ autoStart: true,
179
+ provides: ILabStatus,
180
+ requires: [ITranslator],
181
+ activate: (app: JupyterFrontEnd, translator: ITranslator) => {
182
+ if (!(app instanceof SingleWidgetApp)) {
183
+ const trans = translator.load(I18N_BUNDLE);
184
+ throw new Error(trans.__('%1 must be activated in SingleWidgetApp.', status.id));
185
+ }
186
+ return app.status;
187
+ },
188
+ };
189
+
190
+ /**
191
+ * The default paths for a single widget app.
192
+ */
193
+ const paths: JupyterFrontEndPlugin<JupyterFrontEnd.IPaths> = {
194
+ id: '@jupyterlite/repl-extension:paths',
195
+ autoStart: true,
196
+ provides: JupyterFrontEnd.IPaths,
197
+ activate: (app: JupyterFrontEnd): JupyterFrontEnd.IPaths => {
198
+ if (!(app instanceof SingleWidgetApp)) {
199
+ throw new Error(`${paths.id} must be activated in SingleWidgetApp.`);
200
+ }
201
+ return app.paths;
202
+ },
203
+ };
204
+
205
+ /**
206
+ * The default URL router provider.
207
+ */
208
+ const router: JupyterFrontEndPlugin<IRouter> = {
209
+ id: '@jupyterlite/repl-extension:router',
210
+ autoStart: true,
211
+ provides: IRouter,
212
+ requires: [JupyterFrontEnd.IPaths],
213
+ activate: (app: JupyterFrontEnd, paths: JupyterFrontEnd.IPaths) => {
214
+ const { commands } = app;
215
+ const base = paths.urls.base;
216
+ const router = new Router({ base, commands });
217
+ void app.started.then(() => {
218
+ // Route the very first request on load.
219
+ void router.route();
220
+
221
+ // Route all pop state events.
222
+ window.addEventListener('popstate', () => {
223
+ void router.route();
224
+ });
225
+ });
226
+ return router;
227
+ },
228
+ };
229
+
230
+ const plugins: JupyterFrontEndPlugin<any>[] = [
231
+ buttons,
232
+ consolePlugin,
233
+ paths,
234
+ router,
235
+ status,
236
+ ];
237
+
238
+ export default plugins;