pluginlist.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /* -----------------------------------------------------------------------------
  2. | Copyright (c) Jupyter Development Team.
  3. | Distributed under the terms of the Modified BSD License.
  4. |----------------------------------------------------------------------------*/
  5. import { FilterBox, ReactWidget } from '@jupyterlab/apputils';
  6. import { ISettingRegistry, Settings } from '@jupyterlab/settingregistry';
  7. import { ITranslator, nullTranslator } from '@jupyterlab/translation';
  8. import { classes, LabIcon, settingsIcon } from '@jupyterlab/ui-components';
  9. import { Message } from '@lumino/messaging';
  10. import { ISignal, Signal } from '@lumino/signaling';
  11. import React from 'react';
  12. /**
  13. * The JupyterLab plugin schema key for the setting editor
  14. * icon class of a plugin.
  15. */
  16. const ICON_KEY = 'jupyter.lab.setting-icon';
  17. /**
  18. * The JupyterLab plugin schema key for the setting editor
  19. * icon class of a plugin.
  20. */
  21. const ICON_CLASS_KEY = 'jupyter.lab.setting-icon-class';
  22. /**
  23. * The JupyterLab plugin schema key for the setting editor
  24. * icon label of a plugin.
  25. */
  26. const ICON_LABEL_KEY = 'jupyter.lab.setting-icon-label';
  27. /**
  28. * A list of plugins with editable settings.
  29. */
  30. export class PluginList extends ReactWidget {
  31. /**
  32. * Create a new plugin list.
  33. */
  34. constructor(options: PluginList.IOptions) {
  35. super();
  36. this.registry = options.registry;
  37. this.translator = options.translator || nullTranslator;
  38. this.addClass('jp-PluginList');
  39. this._confirm = options.confirm;
  40. this.registry.pluginChanged.connect(() => {
  41. this.update();
  42. }, this);
  43. this.mapPlugins = this.mapPlugins.bind(this);
  44. this._filter = (item: ISettingRegistry.IPlugin) => true;
  45. this.setFilter = this.setFilter.bind(this);
  46. this.setError = this.setError.bind(this);
  47. this._evtMousedown = this._evtMousedown.bind(this);
  48. this._allPlugins = PluginList.sortPlugins(this.registry).filter(plugin => {
  49. const { schema } = plugin;
  50. const deprecated = schema['jupyter.lab.setting-deprecated'] === true;
  51. const editable = Object.keys(schema.properties || {}).length > 0;
  52. const extensible = schema.additionalProperties !== false;
  53. // Filters out a couple of plugins that take too long to load in the new settings editor.
  54. const correctEditor =
  55. // If this is the json settings editor, anything is fine
  56. this._confirm ||
  57. // If this is the new settings editor, remove context menu / main menu settings.
  58. (!this._confirm && !(options.toSkip ?? []).includes(plugin.id));
  59. return (
  60. !deprecated &&
  61. correctEditor &&
  62. (editable || extensible) &&
  63. this._filter?.(plugin)
  64. );
  65. });
  66. this._errors = {};
  67. this.selection = this._allPlugins[0].id;
  68. }
  69. /**
  70. * The setting registry.
  71. */
  72. readonly registry: ISettingRegistry;
  73. /**
  74. * A signal emitted when a list user interaction happens.
  75. */
  76. get changed(): ISignal<this, void> {
  77. return this._changed;
  78. }
  79. /**
  80. * The selection value of the plugin list.
  81. */
  82. get scrollTop(): number | undefined {
  83. return this.node.querySelector('ul')?.scrollTop;
  84. }
  85. get hasErrors(): boolean {
  86. for (const id in this._errors) {
  87. if (this._errors[id]) {
  88. return true;
  89. }
  90. }
  91. return false;
  92. }
  93. /**
  94. * The selection value of the plugin list.
  95. */
  96. get selection(): string {
  97. return this._selection;
  98. }
  99. set selection(selection: string) {
  100. this._selection = selection;
  101. this.update();
  102. }
  103. get filter(): (item: ISettingRegistry.IPlugin) => boolean {
  104. return this._filter;
  105. }
  106. set filter(filter: (item: ISettingRegistry.IPlugin) => boolean) {
  107. this._filter = filter;
  108. }
  109. protected async updateModifiedPlugins(): Promise<void> {
  110. const modifiedPlugins = [];
  111. for (const plugin of this._allPlugins) {
  112. const settings: Settings = (await this.registry.load(
  113. plugin.id
  114. )) as Settings;
  115. if (settings.isModified) {
  116. modifiedPlugins.push(plugin);
  117. }
  118. }
  119. if (this._modifiedPlugins.length !== modifiedPlugins.length) {
  120. this._modifiedPlugins = modifiedPlugins;
  121. this.update();
  122. return;
  123. }
  124. for (const plugin of modifiedPlugins) {
  125. if (!this._modifiedPlugins.find(p => p.id === plugin.id)) {
  126. this._modifiedPlugins = modifiedPlugins;
  127. this.update();
  128. return;
  129. }
  130. }
  131. }
  132. get handleSelectSignal(): ISignal<this, string> {
  133. return this._handleSelectSignal;
  134. }
  135. /**
  136. * Handle `'update-request'` messages.
  137. */
  138. protected onUpdateRequest(msg: Message): void {
  139. void this.updateModifiedPlugins();
  140. const ul = this.node.querySelector('ul');
  141. if (ul && this._scrollTop !== undefined) {
  142. ul.scrollTop = this._scrollTop;
  143. }
  144. super.onUpdateRequest(msg);
  145. }
  146. /**
  147. * Handle the `'mousedown'` event for the plugin list.
  148. *
  149. * @param event - The DOM event sent to the widget
  150. */
  151. private _evtMousedown(event: React.MouseEvent<HTMLButtonElement>): void {
  152. const target = event.currentTarget;
  153. const id = target.getAttribute('data-id');
  154. if (!id) {
  155. return;
  156. }
  157. if (this._confirm) {
  158. this._confirm(id)
  159. .then(() => {
  160. this.selection = id!;
  161. this._changed.emit(undefined);
  162. this.update();
  163. })
  164. .catch(() => {
  165. /* no op */
  166. });
  167. } else {
  168. this._scrollTop = this.scrollTop;
  169. this._selection = id!;
  170. this._handleSelectSignal.emit(id!);
  171. this._changed.emit(undefined);
  172. this.update();
  173. }
  174. }
  175. /**
  176. * Check the plugin for a rendering hint's value.
  177. *
  178. * #### Notes
  179. * The order of priority for overridden hints is as follows, from most
  180. * important to least:
  181. * 1. Data set by the end user in a settings file.
  182. * 2. Data set by the plugin author as a schema default.
  183. * 3. Data set by the plugin author as a top-level key of the schema.
  184. */
  185. getHint(
  186. key: string,
  187. registry: ISettingRegistry,
  188. plugin: ISettingRegistry.IPlugin
  189. ): string {
  190. // First, give priority to checking if the hint exists in the user data.
  191. let hint = plugin.data.user[key];
  192. // Second, check to see if the hint exists in composite data, which folds
  193. // in default values from the schema.
  194. if (!hint) {
  195. hint = plugin.data.composite[key];
  196. }
  197. // Third, check to see if the plugin schema has defined the hint.
  198. if (!hint) {
  199. hint = plugin.schema[key];
  200. }
  201. // Finally, use the defaults from the registry schema.
  202. if (!hint) {
  203. const { properties } = registry.schema;
  204. hint = properties && properties[key] && properties[key].default;
  205. }
  206. return typeof hint === 'string' ? hint : '';
  207. }
  208. setFilter(filter: (item: string) => boolean): void {
  209. this._filter = (value: ISettingRegistry.IPlugin) => {
  210. return filter(value.schema.title?.toLowerCase() ?? '');
  211. };
  212. this.update();
  213. }
  214. setError(id: string, error: boolean): void {
  215. if (this._errors[id] !== error) {
  216. this._errors[id] = error;
  217. this.update();
  218. } else {
  219. this._errors[id] = error;
  220. }
  221. }
  222. mapPlugins(plugin: ISettingRegistry.IPlugin): JSX.Element {
  223. const { id, schema, version } = plugin;
  224. const trans = this.translator.load('jupyterlab');
  225. const title =
  226. typeof schema.title === 'string' ? trans._p('schema', schema.title) : id;
  227. const description =
  228. typeof schema.description === 'string'
  229. ? trans._p('schema', schema.description)
  230. : '';
  231. const itemTitle = `${description}\n${id}\n${version}`;
  232. const icon = this.getHint(ICON_KEY, this.registry, plugin);
  233. const iconClass = this.getHint(ICON_CLASS_KEY, this.registry, plugin);
  234. const iconTitle = this.getHint(ICON_LABEL_KEY, this.registry, plugin);
  235. return (
  236. <button
  237. onClick={this._evtMousedown}
  238. className={`${
  239. id === this.selection
  240. ? 'jp-mod-selected jp-PluginList-entry'
  241. : 'jp-PluginList-entry'
  242. } ${this._errors[id] ? 'jp-ErrorPlugin' : ''}`}
  243. data-id={id}
  244. key={id}
  245. title={itemTitle}
  246. >
  247. {id === this.selection || this._errors[id] ? (
  248. <div className="jp-SelectedIndicator" />
  249. ) : null}
  250. <LabIcon.resolveReact
  251. icon={icon || (iconClass ? undefined : settingsIcon)}
  252. iconClass={classes(iconClass, 'jp-Icon')}
  253. title={iconTitle}
  254. tag="span"
  255. stylesheet="settingsEditor"
  256. />
  257. <span>{title}</span>
  258. </button>
  259. );
  260. }
  261. render(): JSX.Element {
  262. const trans = this.translator.load('jupyterlab');
  263. const modifiedItems = this._modifiedPlugins
  264. .filter(this._filter)
  265. .map(this.mapPlugins);
  266. const otherItems = this._allPlugins
  267. .filter(
  268. plugin =>
  269. !this._modifiedPlugins.includes(plugin) && this._filter(plugin)
  270. )
  271. .map(this.mapPlugins);
  272. return (
  273. <div className="jp-PluginList-wrapper">
  274. <FilterBox
  275. updateFilter={this.setFilter}
  276. useFuzzyFilter={true}
  277. placeholder={trans.__('Search…')}
  278. forceRefresh={false}
  279. />
  280. {modifiedItems.length > 0 && (
  281. <div>
  282. <h1 className="jp-PluginList-header">{trans.__('Modified')}</h1>
  283. <ul>{modifiedItems}</ul>
  284. </div>
  285. )}
  286. <h1 className="jp-PluginList-header">{trans.__('Settings')}</h1>
  287. <ul>{otherItems}</ul>
  288. </div>
  289. );
  290. }
  291. protected translator: ITranslator;
  292. private _changed = new Signal<this, void>(this);
  293. private _errors: { [id: string]: boolean };
  294. private _filter: (item: ISettingRegistry.IPlugin) => boolean;
  295. private _handleSelectSignal = new Signal<this, string>(this);
  296. private _modifiedPlugins: ISettingRegistry.IPlugin[] = [];
  297. private _allPlugins: ISettingRegistry.IPlugin[] = [];
  298. private _confirm?: (id: string) => Promise<void>;
  299. private _scrollTop: number | undefined = 0;
  300. private _selection = '';
  301. }
  302. /**
  303. * A namespace for `PluginList` statics.
  304. */
  305. export namespace PluginList {
  306. /**
  307. * The instantiation options for a plugin list.
  308. */
  309. export interface IOptions {
  310. /**
  311. * A function that allows for asynchronously confirming a selection.
  312. *
  313. * #### Notes
  314. * If the promise returned by the function resolves, then the selection will
  315. * succeed and emit an event. If the promise rejects, the selection is not
  316. * made.
  317. */
  318. confirm?: (id: string) => Promise<void>;
  319. /**
  320. * The setting registry for the plugin list.
  321. */
  322. registry: ISettingRegistry;
  323. /**
  324. * List of plugins to skip
  325. */
  326. toSkip?: string[];
  327. /**
  328. * The setting registry for the plugin list.
  329. */
  330. translator?: ITranslator;
  331. }
  332. /**
  333. * Sort a list of plugins by title and ID.
  334. */
  335. export function sortPlugins(
  336. registry: ISettingRegistry
  337. ): ISettingRegistry.IPlugin[] {
  338. return Object.keys(registry.plugins)
  339. .map(plugin => registry.plugins[plugin]!)
  340. .sort((a, b) => {
  341. return (a.schema.title || a.id).localeCompare(b.schema.title || b.id);
  342. });
  343. }
  344. }