@jupyterlab/apputils 4.0.0-alpha.20 → 4.0.0-alpha.22

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.
@@ -15,7 +15,7 @@ import {
15
15
  TranslationBundle
16
16
  } from '@jupyterlab/translation';
17
17
  import { find } from '@lumino/algorithm';
18
- import { PromiseDelegate, UUID } from '@lumino/coreutils';
18
+ import { JSONExt, PromiseDelegate, UUID } from '@lumino/coreutils';
19
19
  import { IDisposable, IObservableDisposable } from '@lumino/disposable';
20
20
  import { ISignal, Signal } from '@lumino/signaling';
21
21
  import { Widget } from '@lumino/widgets';
@@ -96,6 +96,14 @@ export interface ISessionContext extends IObservableDisposable {
96
96
  >
97
97
  >;
98
98
 
99
+ /**
100
+ * Signal emitted if the kernel preference changes.
101
+ */
102
+ readonly kernelPreferenceChanged: ISignal<
103
+ this,
104
+ IChangedArgs<ISessionContext.IKernelPreference>
105
+ >;
106
+
99
107
  /**
100
108
  * A signal emitted when the kernel status changes, proxied from the session connection.
101
109
  */
@@ -198,6 +206,13 @@ export interface ISessionContext extends IObservableDisposable {
198
206
  */
199
207
  readonly specsManager: KernelSpec.IManager;
200
208
 
209
+ /**
210
+ * Starts new Kernel.
211
+ *
212
+ * @returns Whether to ask the user to pick a kernel.
213
+ */
214
+ startKernel(): Promise<boolean>;
215
+
201
216
  /**
202
217
  * Restart the current Kernel.
203
218
  *
@@ -269,7 +284,7 @@ export namespace ISessionContext {
269
284
 
270
285
  /**
271
286
  * Automatically start the default kernel if no other matching kernel is
272
- * found (default `true`).
287
+ * found (default `false`).
273
288
  */
274
289
  readonly autoStartDefault?: boolean;
275
290
  }
@@ -287,10 +302,7 @@ export namespace ISessionContext {
287
302
  /**
288
303
  * Select a kernel for the session.
289
304
  */
290
- selectKernel(
291
- session: ISessionContext,
292
- translator?: ITranslator
293
- ): Promise<void>;
305
+ selectKernel(session: ISessionContext): Promise<void>;
294
306
 
295
307
  /**
296
308
  * Restart the session context.
@@ -303,10 +315,17 @@ export namespace ISessionContext {
303
315
  * kernel name and resolves with `true`. If no kernel has been started,
304
316
  * this is a no-op, and resolves with `false`.
305
317
  */
306
- restart(
307
- session: ISessionContext,
308
- translator?: ITranslator
309
- ): Promise<boolean>;
318
+ restart(session: ISessionContext): Promise<boolean>;
319
+ }
320
+
321
+ /**
322
+ * Session context dialog options
323
+ */
324
+ export interface IDialogsOptions {
325
+ /**
326
+ * Application translator object
327
+ */
328
+ translator?: ITranslator;
310
329
  }
311
330
  }
312
331
 
@@ -445,7 +464,25 @@ export class SessionContext implements ISessionContext {
445
464
  return this._kernelPreference;
446
465
  }
447
466
  set kernelPreference(value: ISessionContext.IKernelPreference) {
448
- this._kernelPreference = value;
467
+ if (!JSONExt.deepEqual(value as any, this._kernelPreference as any)) {
468
+ const oldValue = this._kernelPreference;
469
+ this._kernelPreference = value;
470
+ this._preferenceChanged.emit({
471
+ name: 'kernelPreference',
472
+ oldValue,
473
+ newValue: JSONExt.deepCopy(value as any)
474
+ });
475
+ }
476
+ }
477
+
478
+ /**
479
+ * Signal emitted if the kernel preference changes.
480
+ */
481
+ get kernelPreferenceChanged(): ISignal<
482
+ this,
483
+ IChangedArgs<ISessionContext.IKernelPreference>
484
+ > {
485
+ return this._preferenceChanged;
449
486
  }
450
487
 
451
488
  /**
@@ -508,26 +545,7 @@ export class SessionContext implements ISessionContext {
508
545
  if (this._pendingKernelName === this.noKernelName) {
509
546
  return this.noKernelName;
510
547
  }
511
- if (
512
- !kernel &&
513
- !this.isReady &&
514
- this.kernelPreference.canStart !== false &&
515
- this.kernelPreference.shouldStart !== false
516
- ) {
517
- let name =
518
- this._pendingKernelName ||
519
- SessionContext.getDefaultKernel({
520
- specs: this.specsManager.specs,
521
- sessions: this.sessionManager.running(),
522
- preference: this.kernelPreference
523
- }) ||
524
- '';
525
- if (name) {
526
- name = this.specsManager.specs?.kernelspecs[name]?.display_name ?? name;
527
- return name;
528
- }
529
- return this.noKernelName;
530
- }
548
+
531
549
  if (this._pendingKernelName) {
532
550
  return (
533
551
  this.specsManager.specs?.kernelspecs[this._pendingKernelName]
@@ -645,6 +663,45 @@ export class SessionContext implements ISessionContext {
645
663
  Signal.clearData(this);
646
664
  }
647
665
 
666
+ /**
667
+ * Starts new Kernel.
668
+ *
669
+ * @returns Whether to ask the user to pick a kernel.
670
+ */
671
+ async startKernel(): Promise<boolean> {
672
+ const preference = this.kernelPreference;
673
+
674
+ if (!preference.autoStartDefault && preference.shouldStart === false) {
675
+ return true;
676
+ }
677
+
678
+ let options: Partial<Kernel.IModel> | undefined;
679
+ if (preference.id) {
680
+ options = { id: preference.id };
681
+ } else {
682
+ const name = Private.getDefaultKernel({
683
+ specs: this.specsManager.specs,
684
+ sessions: this.sessionManager.running(),
685
+ preference
686
+ });
687
+ if (name) {
688
+ options = { name };
689
+ }
690
+ }
691
+
692
+ if (options) {
693
+ try {
694
+ await this._changeKernel(options);
695
+ return false;
696
+ } catch (err) {
697
+ /* no-op */
698
+ }
699
+ }
700
+
701
+ // Always fall back to selecting a kernel
702
+ return true;
703
+ }
704
+
648
705
  /**
649
706
  * Restart the current Kernel.
650
707
  *
@@ -817,31 +874,7 @@ export class SessionContext implements ISessionContext {
817
874
  return false;
818
875
  }
819
876
 
820
- let options: Partial<Kernel.IModel> | undefined;
821
- if (preference.id) {
822
- options = { id: preference.id };
823
- } else {
824
- const name = SessionContext.getDefaultKernel({
825
- specs: this.specsManager.specs,
826
- sessions: this.sessionManager.running(),
827
- preference
828
- });
829
- if (name) {
830
- options = { name };
831
- }
832
- }
833
-
834
- if (options) {
835
- try {
836
- await this._changeKernel(options);
837
- return false;
838
- } catch (err) {
839
- /* no-op */
840
- }
841
- }
842
-
843
- // Always fall back to selecting a kernel
844
- return true;
877
+ return this.startKernel();
845
878
  }
846
879
 
847
880
  /**
@@ -1167,6 +1200,10 @@ export class SessionContext implements ISessionContext {
1167
1200
  this,
1168
1201
  Session.ISessionConnection.IKernelChangedArgs
1169
1202
  >(this);
1203
+ private _preferenceChanged = new Signal<
1204
+ this,
1205
+ IChangedArgs<ISessionContext.IKernelPreference>
1206
+ >(this);
1170
1207
  private _sessionChanged = new Signal<
1171
1208
  this,
1172
1209
  IChangedArgs<
@@ -1265,6 +1302,13 @@ export namespace SessionContext {
1265
1302
  * Get the default kernel name given select options.
1266
1303
  */
1267
1304
  export function getDefaultKernel(options: IKernelSearch): string | null {
1305
+ const { preference } = options;
1306
+ const { shouldStart } = preference;
1307
+
1308
+ if (shouldStart === false) {
1309
+ return null;
1310
+ }
1311
+
1268
1312
  return Private.getDefaultKernel(options);
1269
1313
  }
1270
1314
  }
@@ -1272,19 +1316,19 @@ export namespace SessionContext {
1272
1316
  /**
1273
1317
  * The default implementation of the client session dialog provider.
1274
1318
  */
1275
- export const sessionContextDialogs: ISessionContext.IDialogs = {
1319
+ export class SessionContextDialogs implements ISessionContext.IDialogs {
1320
+ constructor(options: ISessionContext.IDialogsOptions = {}) {
1321
+ this._translator = options.translator ?? nullTranslator;
1322
+ }
1323
+
1276
1324
  /**
1277
1325
  * Select a kernel for the session.
1278
1326
  */
1279
- async selectKernel(
1280
- sessionContext: ISessionContext,
1281
- translator?: ITranslator
1282
- ): Promise<void> {
1327
+ async selectKernel(sessionContext: ISessionContext): Promise<void> {
1283
1328
  if (sessionContext.isDisposed) {
1284
1329
  return Promise.resolve();
1285
1330
  }
1286
- translator = translator || nullTranslator;
1287
- const trans = translator.load('jupyterlab');
1331
+ const trans = this._translator.load('jupyterlab');
1288
1332
 
1289
1333
  // If there is no existing kernel, offer the option
1290
1334
  // to keep no kernel.
@@ -1297,16 +1341,37 @@ export const sessionContextDialogs: ISessionContext.IDialogs = {
1297
1341
  Dialog.okButton({ label: trans.__('Select') })
1298
1342
  ];
1299
1343
 
1344
+ const autoStartDefault = sessionContext.kernelPreference.autoStartDefault;
1345
+ const hasCheckbox = typeof autoStartDefault === 'boolean';
1346
+
1300
1347
  const dialog = new Dialog({
1301
1348
  title: trans.__('Select Kernel'),
1302
- body: new Private.KernelSelector(sessionContext, translator),
1303
- buttons
1349
+ body: new Private.KernelSelector(sessionContext, this._translator),
1350
+ buttons,
1351
+ checkbox: hasCheckbox
1352
+ ? {
1353
+ label: trans.__('Always start the preferred kernel'),
1354
+ caption: trans.__(
1355
+ 'Remember my choice and always start the preferred kernel'
1356
+ ),
1357
+ checked: autoStartDefault
1358
+ }
1359
+ : null
1304
1360
  });
1305
1361
 
1306
1362
  const result = await dialog.launch();
1363
+
1307
1364
  if (sessionContext.isDisposed || !result.button.accept) {
1308
1365
  return;
1309
1366
  }
1367
+
1368
+ if (hasCheckbox && result.isChecked !== null) {
1369
+ sessionContext.kernelPreference = {
1370
+ ...sessionContext.kernelPreference,
1371
+ autoStartDefault: result.isChecked
1372
+ };
1373
+ }
1374
+
1310
1375
  const model = result.value;
1311
1376
  if (model === null && !sessionContext.hasNoKernel) {
1312
1377
  return sessionContext.shutdown();
@@ -1314,7 +1379,7 @@ export const sessionContextDialogs: ISessionContext.IDialogs = {
1314
1379
  if (model) {
1315
1380
  await sessionContext.changeKernel(model);
1316
1381
  }
1317
- },
1382
+ }
1318
1383
 
1319
1384
  /**
1320
1385
  * Restart the session.
@@ -1326,12 +1391,8 @@ export const sessionContextDialogs: ISessionContext.IDialogs = {
1326
1391
  * If there is no kernel, we start a kernel with the last run
1327
1392
  * kernel name and resolves with `true`.
1328
1393
  */
1329
- async restart(
1330
- sessionContext: ISessionContext,
1331
- translator?: ITranslator
1332
- ): Promise<boolean> {
1333
- translator = translator || nullTranslator;
1334
- const trans = translator.load('jupyterlab');
1394
+ async restart(sessionContext: ISessionContext): Promise<boolean> {
1395
+ const trans = this._translator.load('jupyterlab');
1335
1396
 
1336
1397
  await sessionContext.initialize();
1337
1398
  if (sessionContext.isDisposed) {
@@ -1368,7 +1429,9 @@ export const sessionContextDialogs: ISessionContext.IDialogs = {
1368
1429
  }
1369
1430
  return false;
1370
1431
  }
1371
- };
1432
+
1433
+ private _translator: ITranslator;
1434
+ }
1372
1435
 
1373
1436
  /**
1374
1437
  * The namespace for module private data.
@@ -1431,10 +1494,9 @@ namespace Private {
1431
1494
  options: SessionContext.IKernelSearch
1432
1495
  ): string | null {
1433
1496
  const { specs, preference } = options;
1434
- const { name, language, shouldStart, canStart, autoStartDefault } =
1435
- preference;
1497
+ const { name, language, canStart, autoStartDefault } = preference;
1436
1498
 
1437
- if (!specs || shouldStart === false || canStart === false) {
1499
+ if (!specs || canStart === false) {
1438
1500
  return null;
1439
1501
  }
1440
1502
 
@@ -301,7 +301,18 @@ export class ThemeManager implements IThemeManager {
301
301
  Object.keys(overidesSchema).forEach(key => {
302
302
  // override validation is against the CSS property in the description
303
303
  // field. Example: for key ui-font-family, .description is font-family
304
- this._overrideProps[key] = overidesSchema[key].description;
304
+ let description;
305
+ switch (key) {
306
+ case 'code-font-size':
307
+ case 'content-font-size1':
308
+ case 'ui-font-size1':
309
+ description = 'font-size';
310
+ break;
311
+ default:
312
+ description = overidesSchema[key].description;
313
+ break;
314
+ }
315
+ this._overrideProps[key] = description;
305
316
  });
306
317
  }
307
318
 
package/src/tokens.ts CHANGED
@@ -191,22 +191,14 @@ export namespace IThemeManager {
191
191
  /**
192
192
  * The sanitizer token.
193
193
  */
194
- export const ISanitizer = new Token<ISanitizer>(
194
+ export const ISanitizer = new Token<IRenderMime.ISanitizer>(
195
195
  '@jupyterlab/apputils:ISanitizer'
196
196
  );
197
197
 
198
- export interface ISanitizer {
199
- /**
200
- * Sanitize an HTML string.
201
- *
202
- * @param dirty - The dirty text.
203
- *
204
- * @param options - The optional sanitization options.
205
- *
206
- * @returns The sanitized string.
207
- */
208
- sanitize(dirty: string, options?: ISanitizer.IOptions): string;
209
- }
198
+ /**
199
+ * @deprecated since v4 use {@link IRenderMime.ISanitizer}
200
+ */
201
+ export type ISanitizer = IRenderMime.ISanitizer;
210
202
 
211
203
  /**
212
204
  * The namespace for `ISanitizer` related interfaces.
@@ -214,23 +206,10 @@ export interface ISanitizer {
214
206
  export namespace ISanitizer {
215
207
  /**
216
208
  * The options used to sanitize.
209
+ *
210
+ * @deprecated in v4 use {@link IRenderMime.ISanitizerOptions}
217
211
  */
218
- export interface IOptions {
219
- /**
220
- * The allowed tags.
221
- */
222
- allowedTags?: string[];
223
-
224
- /**
225
- * The allowed attributes for a given tag.
226
- */
227
- allowedAttributes?: { [key: string]: string[] };
228
-
229
- /**
230
- * The allowed style values for a given tag.
231
- */
232
- allowedStyles?: { [key: string]: { [key: string]: RegExp[] } };
233
- }
212
+ export type IOptions = IRenderMime.ISanitizerOptions;
234
213
  }
235
214
 
236
215
  /**
@@ -20,7 +20,7 @@ import {
20
20
  } from '@jupyterlab/ui-components';
21
21
  import { Widget } from '@lumino/widgets';
22
22
  import * as React from 'react';
23
- import { ISessionContext, sessionContextDialogs } from '../sessioncontext';
23
+ import { ISessionContext, SessionContextDialogs } from '../sessioncontext';
24
24
  import { translateKernelStatuses } from '../kernelstatuses';
25
25
  /**
26
26
  * The class name added to toolbar kernel name text.
@@ -68,14 +68,13 @@ export namespace Toolbar {
68
68
  dialogs?: ISessionContext.IDialogs,
69
69
  translator?: ITranslator
70
70
  ): Widget {
71
- translator = translator || nullTranslator;
71
+ translator = translator ?? nullTranslator;
72
72
  const trans = translator.load('jupyterlab');
73
73
  return new ToolbarButton({
74
74
  icon: refreshIcon,
75
75
  onClick: () => {
76
- void (dialogs ?? sessionContextDialogs).restart(
77
- sessionContext,
78
- translator
76
+ void (dialogs ?? new SessionContextDialogs({ translator })).restart(
77
+ sessionContext
79
78
  );
80
79
  },
81
80
  tooltip: trans.__('Restart the kernel')
@@ -97,7 +96,7 @@ export namespace Toolbar {
97
96
  const el = ReactWidget.create(
98
97
  <Private.KernelNameComponent
99
98
  sessionContext={sessionContext}
100
- dialogs={dialogs ?? sessionContextDialogs}
99
+ dialogs={dialogs ?? new SessionContextDialogs({ translator })}
101
100
  translator={translator}
102
101
  />
103
102
  );
@@ -155,7 +154,7 @@ namespace Private {
155
154
  const translator = props.translator || nullTranslator;
156
155
  const trans = translator.load('jupyterlab');
157
156
  const callback = () => {
158
- void props.dialogs.selectKernel(props.sessionContext, translator);
157
+ void props.dialogs.selectKernel(props.sessionContext);
159
158
  };
160
159
  return (
161
160
  <UseSignal