Debian Codegraph @

packages/help-extension/src/index.tsx

raw
1 // Copyright (c) Jupyter Development Team.
2 // Distributed under the terms of the Modified BSD License.
3
4 import {
5 JupyterFrontEnd,
6 JupyterFrontEndPlugin,
7 } from '@jupyterlab/application';
8
9 import { Dialog, ICommandPalette } from '@jupyterlab/apputils';
10
11 import { IMainMenu } from '@jupyterlab/mainmenu';
12
13 import { ITranslator } from '@jupyterlab/translation';
14
15 import { jupyterIcon } from '@jupyter-notebook/ui-components';
16
17 import * as React from 'react';
18
19 /**
20 * A list of resources to show in the help menu.
21 */
22 const RESOURCES = [
23 {
24 text: 'About Jupyter',
25 url: 'https://jupyter.org',
26 },
27 {
28 text: 'Markdown Reference',
29 url: 'https://commonmark.org/help/',
30 },
31 ];
32
33 /**
34 * The command IDs used by the help plugin.
35 */
36 namespace CommandIDs {
37 export const open = 'help:open';
38
39 export const about = 'help:about';
40 }
41
42 /**
43 * A plugin to open the about section with resources.
44 */
45 const open: JupyterFrontEndPlugin<void> = {
46 id: '@jupyter-notebook/help-extension:open',
47 autoStart: true,
48 activate: (app: JupyterFrontEnd): void => {
49 const { commands } = app;
50
51 commands.addCommand(CommandIDs.open, {
52 label: (args) => args['text'] as string,
53 execute: (args) => {
54 const url = args['url'] as string;
55 window.open(url);
56 },
57 });
58 },
59 };
60
61 /**
62 * Plugin to add a command to show an About Jupyter Notebook and Markdown Reference.
63 */
64 const about: JupyterFrontEndPlugin<void> = {
65 id: '@jupyter-notebook/help-extension:about',
66 autoStart: true,
67 requires: [ITranslator],
68 optional: [IMainMenu, ICommandPalette],
69 activate: (
70 app: JupyterFrontEnd,
71 translator: ITranslator,
72 menu: IMainMenu | null,
73 palette: ICommandPalette | null
74 ): void => {
75 const { commands } = app;
76 const trans = translator.load('notebook');
77 const category = trans.__('Help');
78
79 commands.addCommand(CommandIDs.about, {
80 label: trans.__('About %1', app.name),
81 execute: () => {
82 const title = (
83 <>
84 <span className="jp-AboutNotebook-header">
85 <jupyterIcon.react width="196px" height="auto" />
86 </span>
87 </>
88 );
89
90 const notebookURL = 'https://github.com/jupyter/notebook';
91 const contributorURL = 'https://github.com/jupyter/notebook/pulse';
92 const aboutJupyter = trans.__('JUPYTER NOTEBOOK ON GITHUB');
93 const contributorList = trans.__('CONTRIBUTOR LIST');
94 const externalLinks = (
95 <span>
96 <a
97 href={notebookURL}
98 target="_blank"
99 rel="noopener noreferrer"
100 className="jp-Button-flat jp-AboutNotebook-about-externalLinks"
101 >
102 {aboutJupyter}
103 </a>
104 <a
105 href={contributorURL}
106 target="_blank"
107 rel="noopener noreferrer"
108 className="jp-Button-flat jp-AboutNotebook-about-externalLinks"
109 >
110 {contributorList}
111 </a>
112 </span>
113 );
114 const version = trans.__('Version: %1', app.version);
115 const copyright = trans.__('© 2021-2023 Jupyter Notebook Contributors');
116 const body = (
117 <>
118 <span className="jp-AboutNotebook-version">{version}</span>
119 <div>{externalLinks}</div>
120 <span className="jp-AboutNotebook-about-copyright">
121 {copyright}
122 </span>
123 </>
124 );
125
126 const dialog = new Dialog({
127 title,
128 body,
129 buttons: [
130 Dialog.createButton({
131 label: trans.__('Dismiss'),
132 className:
133 'jp-AboutNotebook-about-button jp-mod-reject jp-mod-styled',
134 }),
135 ],
136 });
137 dialog.addClass('jp-AboutNotebook');
138 void dialog.launch();
139 },
140 });
141
142 if (palette) {
143 palette.addItem({ command: CommandIDs.about, category });
144 }
145
146 const resourcesGroup = RESOURCES.map((args) => ({
147 args,
148 command: CommandIDs.open,
149 }));
150
151 if (menu) {
152 menu.helpMenu.addGroup(resourcesGroup, 30);
153 }
154 },
155 };
156
157 const plugins: JupyterFrontEndPlugin<any>[] = [open, about];
158
159 export default plugins;