pluginlist.tsx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*-----------------------------------------------------------------------------
  2. | Copyright (c) Jupyter Development Team.
  3. | Distributed under the terms of the Modified BSD License.
  4. |----------------------------------------------------------------------------*/
  5. import { ISettingRegistry } from '@jupyterlab/coreutils';
  6. import { IconX } from '@jupyterlab/ui-components';
  7. import { Message } from '@phosphor/messaging';
  8. import { ISignal, Signal } from '@phosphor/signaling';
  9. import { Widget } from '@phosphor/widgets';
  10. import * as React from 'react';
  11. import * as ReactDOM from 'react-dom';
  12. /**
  13. * A list of plugins with editable settings.
  14. */
  15. export class PluginList extends Widget {
  16. /**
  17. * Create a new plugin list.
  18. */
  19. constructor(options: PluginList.IOptions) {
  20. super();
  21. this.registry = options.registry;
  22. this.addClass('jp-PluginList');
  23. this._confirm = options.confirm;
  24. this.registry.pluginChanged.connect(() => {
  25. this.update();
  26. }, this);
  27. }
  28. /**
  29. * The setting registry.
  30. */
  31. readonly registry: ISettingRegistry;
  32. /**
  33. * A signal emitted when a list user interaction happens.
  34. */
  35. get changed(): ISignal<this, void> {
  36. return this._changed;
  37. }
  38. /**
  39. * The selection value of the plugin list.
  40. */
  41. get scrollTop(): number {
  42. return this.node.querySelector('ul').scrollTop;
  43. }
  44. /**
  45. * The selection value of the plugin list.
  46. */
  47. get selection(): string {
  48. return this._selection;
  49. }
  50. set selection(selection: string) {
  51. if (this._selection === selection) {
  52. return;
  53. }
  54. this._selection = selection;
  55. this.update();
  56. }
  57. /**
  58. * Handle the DOM events for the widget.
  59. *
  60. * @param event - The DOM event sent to the widget.
  61. *
  62. * #### Notes
  63. * This method implements the DOM `EventListener` interface and is
  64. * called in response to events on the plugin list's node. It should
  65. * not be called directly by user code.
  66. */
  67. handleEvent(event: Event): void {
  68. switch (event.type) {
  69. case 'mousedown':
  70. this._evtMousedown(event as MouseEvent);
  71. break;
  72. default:
  73. break;
  74. }
  75. }
  76. /**
  77. * Handle `'after-attach'` messages.
  78. */
  79. protected onAfterAttach(msg: Message): void {
  80. this.node.addEventListener('mousedown', this);
  81. this.update();
  82. }
  83. /**
  84. * Handle `before-detach` messages for the widget.
  85. */
  86. protected onBeforeDetach(msg: Message): void {
  87. this.node.removeEventListener('mousedown', this);
  88. }
  89. /**
  90. * Handle `'update-request'` messages.
  91. */
  92. protected onUpdateRequest(msg: Message): void {
  93. const { node, registry } = this;
  94. const selection = this._selection;
  95. Private.populateList(registry, selection, node);
  96. node.querySelector('ul').scrollTop = this._scrollTop;
  97. }
  98. /**
  99. * Handle the `'mousedown'` event for the plugin list.
  100. *
  101. * @param event - The DOM event sent to the widget
  102. */
  103. private _evtMousedown(event: MouseEvent): void {
  104. event.preventDefault();
  105. let target = event.target as HTMLElement;
  106. let id = target.getAttribute('data-id');
  107. if (id === this._selection) {
  108. return;
  109. }
  110. if (!id) {
  111. while (!id && target !== this.node) {
  112. target = target.parentElement as HTMLElement;
  113. id = target.getAttribute('data-id');
  114. }
  115. }
  116. if (!id) {
  117. return;
  118. }
  119. this._confirm()
  120. .then(() => {
  121. this._scrollTop = this.scrollTop;
  122. this._selection = id;
  123. this._changed.emit(undefined);
  124. this.update();
  125. })
  126. .catch(() => {
  127. /* no op */
  128. });
  129. }
  130. private _changed = new Signal<this, void>(this);
  131. private _confirm: () => Promise<void>;
  132. private _scrollTop = 0;
  133. private _selection = '';
  134. }
  135. /**
  136. * A namespace for `PluginList` statics.
  137. */
  138. export namespace PluginList {
  139. /**
  140. * The instantiation options for a plugin list.
  141. */
  142. export interface IOptions {
  143. /**
  144. * A function that allows for asynchronously confirming a selection.
  145. *
  146. * #### Notest
  147. * If the promise returned by the function resolves, then the selection will
  148. * succeed and emit an event. If the promise rejects, the selection is not
  149. * made.
  150. */
  151. confirm: () => Promise<void>;
  152. /**
  153. * The setting registry for the plugin list.
  154. */
  155. registry: ISettingRegistry;
  156. }
  157. }
  158. /**
  159. * A namespace for private module data.
  160. */
  161. namespace Private {
  162. /**
  163. * The JupyterLab plugin schema key for the setting editor icon of a plugin.
  164. */
  165. const ICON_CLASS_KEY = 'jupyter.lab.setting-icon-class';
  166. /**
  167. * The JupyterLab plugin schema key for the setting editor label of a plugin.
  168. */
  169. const ICON_LABEL_KEY = 'jupyter.lab.setting-icon-label';
  170. /**e
  171. * The JupyterLab plugin schema key for IconRegistry name
  172. * of the setting editor icon of a plugin
  173. */
  174. const ICON_NAME_KEY = 'jupyter.lab.setting-icon-name';
  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. function 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. /**
  209. * Populate the plugin list.
  210. */
  211. export function populateList(
  212. registry: ISettingRegistry,
  213. selection: string,
  214. node: HTMLElement
  215. ): void {
  216. const plugins = sortPlugins(registry).filter(plugin => {
  217. const { schema } = plugin;
  218. const deprecated = schema['jupyter.lab.setting-deprecated'] === true;
  219. const editable = Object.keys(schema.properties || {}).length > 0;
  220. const extensible = schema.additionalProperties !== false;
  221. return !deprecated && (editable || extensible);
  222. });
  223. const items = plugins.map(plugin => {
  224. const { id, schema, version } = plugin;
  225. const itemTitle = `${schema.description}\n${id}\n${version}`;
  226. const image = getHint(ICON_CLASS_KEY, registry, plugin);
  227. const iconClass = `jp-PluginList-icon${
  228. image ? ' ' + image : ''
  229. }`;
  230. const iconTitle = getHint(ICON_LABEL_KEY, registry, plugin);
  231. return (
  232. <li
  233. className={id === selection ? 'jp-mod-selected' : ''}
  234. data-id={id}
  235. key={id}
  236. title={itemTitle}
  237. >
  238. {iconName ? (
  239. <IconX
  240. tag={'span'}
  241. name={iconName}
  242. className={iconClass}
  243. title={iconTitle}
  244. />
  245. ) : (
  246. <span className={iconClass} title={iconTitle} />
  247. )}
  248. <span>{schema.title || id}</span>
  249. </li>
  250. );
  251. });
  252. ReactDOM.unmountComponentAtNode(node);
  253. ReactDOM.render(<ul>{items}</ul>, node);
  254. }
  255. /**
  256. * Sort a list of plugins by title and ID.
  257. */
  258. function sortPlugins(registry: ISettingRegistry): ISettingRegistry.IPlugin[] {
  259. return Object.keys(registry.plugins)
  260. .map(plugin => registry.plugins[plugin])
  261. .sort((a, b) => {
  262. return (a.schema.title || a.id).localeCompare(b.schema.title || b.id);
  263. });
  264. }
  265. }