index.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*-----------------------------------------------------------------------------
  2. | Copyright (c) Jupyter Development Team.
  3. | Distributed under the terms of the Modified BSD License.
  4. |----------------------------------------------------------------------------*/
  5. import {
  6. ILayoutRestorer,
  7. JupyterFrontEnd,
  8. JupyterFrontEndPlugin
  9. } from '@jupyterlab/application';
  10. import {
  11. ICommandPalette,
  12. MainAreaWidget,
  13. WidgetTracker
  14. } from '@jupyterlab/apputils';
  15. import { IEditorServices } from '@jupyterlab/codeeditor';
  16. import { IStateDB } from '@jupyterlab/statedb';
  17. import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
  18. import {
  19. ISettingEditorTracker,
  20. SettingEditor
  21. } from '@jupyterlab/settingeditor';
  22. import { ISettingRegistry } from '@jupyterlab/settingregistry';
  23. /**
  24. * The command IDs used by the setting editor.
  25. */
  26. namespace CommandIDs {
  27. export const open = 'settingeditor:open';
  28. export const revert = 'settingeditor:revert';
  29. export const save = 'settingeditor:save';
  30. }
  31. /**
  32. * The default setting editor extension.
  33. */
  34. const plugin: JupyterFrontEndPlugin<ISettingEditorTracker> = {
  35. id: '@jupyterlab/settingeditor-extension:plugin',
  36. requires: [
  37. ILayoutRestorer,
  38. ISettingRegistry,
  39. IEditorServices,
  40. IStateDB,
  41. IRenderMimeRegistry,
  42. ICommandPalette
  43. ],
  44. autoStart: true,
  45. provides: ISettingEditorTracker,
  46. activate
  47. };
  48. /**
  49. * Activate the setting editor extension.
  50. */
  51. function activate(
  52. app: JupyterFrontEnd,
  53. restorer: ILayoutRestorer,
  54. registry: ISettingRegistry,
  55. editorServices: IEditorServices,
  56. state: IStateDB,
  57. rendermime: IRenderMimeRegistry,
  58. palette: ICommandPalette
  59. ): ISettingEditorTracker {
  60. const { commands, shell } = app;
  61. const namespace = 'setting-editor';
  62. const factoryService = editorServices.factoryService;
  63. const editorFactory = factoryService.newInlineEditor;
  64. const tracker = new WidgetTracker<MainAreaWidget<SettingEditor>>({
  65. namespace
  66. });
  67. let editor: SettingEditor;
  68. // Handle state restoration.
  69. void restorer.restore(tracker, {
  70. command: CommandIDs.open,
  71. args: widget => ({}),
  72. name: widget => namespace
  73. });
  74. commands.addCommand(CommandIDs.open, {
  75. execute: () => {
  76. if (tracker.currentWidget) {
  77. shell.activateById(tracker.currentWidget.id);
  78. return;
  79. }
  80. const key = plugin.id;
  81. const when = app.restored;
  82. editor = new SettingEditor({
  83. commands: {
  84. registry: commands,
  85. revert: CommandIDs.revert,
  86. save: CommandIDs.save
  87. },
  88. editorFactory,
  89. key,
  90. registry,
  91. rendermime,
  92. state,
  93. when
  94. });
  95. // Notify the command registry when the visibility status of the setting
  96. // editor's commands change. The setting editor toolbar listens for this
  97. // signal from the command registry.
  98. editor.commandsChanged.connect((sender: any, args: string[]) => {
  99. args.forEach(id => {
  100. commands.notifyCommandChanged(id);
  101. });
  102. });
  103. editor.id = namespace;
  104. editor.title.label = 'Settings';
  105. editor.title.iconClass = 'jp-SettingsIcon';
  106. let main = new MainAreaWidget({ content: editor });
  107. void tracker.add(main);
  108. shell.add(main);
  109. },
  110. label: 'Advanced Settings Editor'
  111. });
  112. palette.addItem({ category: 'Settings', command: CommandIDs.open });
  113. commands.addCommand(CommandIDs.revert, {
  114. execute: () => {
  115. tracker.currentWidget?.content.revert();
  116. },
  117. iconClass: 'jp-MaterialIcon jp-UndoIcon',
  118. label: 'Revert User Settings',
  119. isEnabled: () => tracker.currentWidget?.content.canRevertRaw ?? false
  120. });
  121. commands.addCommand(CommandIDs.save, {
  122. execute: () => tracker.currentWidget?.content.save(),
  123. iconClass: 'jp-MaterialIcon jp-SaveIcon',
  124. label: 'Save User Settings',
  125. isEnabled: () => tracker.currentWidget?.content.canSaveRaw ?? false
  126. });
  127. return tracker;
  128. }
  129. export default plugin;