settingseditor.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import { ILabStatus } from '@jupyterlab/application';
  2. import { ReactWidget, showDialog } from '@jupyterlab/apputils';
  3. import { ISettingRegistry, Settings } from '@jupyterlab/settingregistry';
  4. import { IStateDB } from '@jupyterlab/statedb';
  5. import { ITranslator, nullTranslator } from '@jupyterlab/translation';
  6. import { IFormComponentRegistry } from '@jupyterlab/ui-components';
  7. import { CommandRegistry } from '@lumino/commands';
  8. import { IDisposable } from '@lumino/disposable';
  9. import { Message } from '@lumino/messaging';
  10. import { ISignal, Signal } from '@lumino/signaling';
  11. import { SplitPanel } from '@lumino/widgets';
  12. import React from 'react';
  13. import { PluginList } from './pluginlist';
  14. import { SettingsPanel } from './settingspanel';
  15. /**
  16. * Form based interface for editing settings.
  17. */
  18. export class SettingsEditor extends SplitPanel {
  19. constructor(options: SettingsEditor.IOptions) {
  20. super({
  21. orientation: 'horizontal',
  22. renderer: SplitPanel.defaultRenderer,
  23. spacing: 1
  24. });
  25. this.translator = options.translator || nullTranslator;
  26. this._status = options.status;
  27. const list = (this._list = new PluginList({
  28. registry: options.registry,
  29. toSkip: options.toSkip,
  30. translator: this.translator,
  31. query: options.query
  32. }));
  33. this.addWidget(list);
  34. this.setDirtyState = this.setDirtyState.bind(this);
  35. /**
  36. * Initializes the settings panel after loading the schema for all plugins.
  37. */
  38. void Promise.all(
  39. PluginList.sortPlugins(options.registry)
  40. .filter(plugin => {
  41. const { schema } = plugin;
  42. const deprecated = schema['jupyter.lab.setting-deprecated'] === true;
  43. const editable = Object.keys(schema.properties || {}).length > 0;
  44. const extensible = schema.additionalProperties !== false;
  45. return !deprecated && (editable || extensible);
  46. })
  47. .map(async plugin => await options.registry.load(plugin.id))
  48. )
  49. .then(settings => {
  50. const settingsPanel = ReactWidget.create(
  51. <SettingsPanel
  52. settings={
  53. settings.filter(
  54. pluginSettings =>
  55. !(options.toSkip ?? []).includes(pluginSettings.id)
  56. ) as Settings[]
  57. }
  58. editorRegistry={options.editorRegistry}
  59. handleSelectSignal={this._list.handleSelectSignal}
  60. onSelect={(id: string) => (this._list.selection = id)}
  61. hasError={this._list.setError}
  62. updateFilterSignal={this._list.updateFilterSignal}
  63. updateDirtyState={this.setDirtyState}
  64. translator={this.translator}
  65. initialFilter={this._list.filter}
  66. />
  67. );
  68. this.addWidget(settingsPanel);
  69. })
  70. .catch(reason => {
  71. console.error(`Fail to load the setting plugins:\n${reason}`);
  72. });
  73. }
  74. /**
  75. * A signal emitted on the start and end of a saving operation.
  76. */
  77. get saveStateChanged(): ISignal<this, SettingsEditor.SaveState> {
  78. return this._saveStateChange;
  79. }
  80. /**
  81. * Set the dirty state status
  82. *
  83. * @param dirty New status
  84. */
  85. setDirtyState(dirty: boolean): void {
  86. this._dirty = dirty;
  87. if (this._dirty && !this._clearDirty) {
  88. this._clearDirty = this._status.setDirty();
  89. } else if (!this._dirty && this._clearDirty) {
  90. this._clearDirty.dispose();
  91. this._clearDirty = null;
  92. }
  93. if (dirty) {
  94. if (!this.title.className.includes('jp-mod-dirty')) {
  95. this.title.className += ' jp-mod-dirty';
  96. }
  97. } else {
  98. this.title.className = this.title.className.replace('jp-mod-dirty', '');
  99. }
  100. this._saveStateChange.emit(dirty ? 'started' : 'completed');
  101. }
  102. /**
  103. * A message handler invoked on a `'close-request'` message.
  104. *
  105. * @param msg Widget message
  106. */
  107. protected onCloseRequest(msg: Message): void {
  108. const trans = this.translator.load('jupyterlab');
  109. if (this._list.hasErrors) {
  110. void showDialog({
  111. title: trans.__('Warning'),
  112. body: trans.__(
  113. 'Unsaved changes due to validation error. Continue without saving?'
  114. )
  115. }).then(value => {
  116. if (value.button.accept) {
  117. this.dispose();
  118. super.onCloseRequest(msg);
  119. }
  120. });
  121. } else if (this._dirty) {
  122. void showDialog({
  123. title: trans.__('Warning'),
  124. body: trans.__(
  125. 'Some changes have not been saved. Continue without saving?'
  126. )
  127. }).then(value => {
  128. if (value.button.accept) {
  129. this.dispose();
  130. super.onCloseRequest(msg);
  131. }
  132. });
  133. } else {
  134. this.dispose();
  135. super.onCloseRequest(msg);
  136. }
  137. }
  138. protected translator: ITranslator;
  139. private _clearDirty: IDisposable | null = null;
  140. private _status: ILabStatus;
  141. private _dirty: boolean = false;
  142. private _list: PluginList;
  143. private _saveStateChange = new Signal<this, SettingsEditor.SaveState>(this);
  144. }
  145. export namespace SettingsEditor {
  146. /**
  147. * Settings editor save state
  148. */
  149. export type SaveState = 'started' | 'failed' | 'completed';
  150. /**
  151. * Settings editor options
  152. */
  153. export interface IOptions {
  154. /**
  155. * Form component registry
  156. */
  157. editorRegistry: IFormComponentRegistry;
  158. /**
  159. * The state database key for the editor's state management.
  160. */
  161. key: string;
  162. /**
  163. * The setting registry the editor modifies.
  164. */
  165. registry: ISettingRegistry;
  166. /**
  167. * The state database used to store layout.
  168. */
  169. state: IStateDB;
  170. /**
  171. * Command registry used to open the JSON settings editor.
  172. */
  173. commands: CommandRegistry;
  174. /**
  175. * Application status
  176. */
  177. status: ILabStatus;
  178. /**
  179. * List of plugins to skip
  180. */
  181. toSkip?: string[];
  182. /**
  183. * The application language translator.
  184. */
  185. translator?: ITranslator;
  186. query?: string;
  187. }
  188. }