plugin.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. Application
  5. } from 'phosphide/lib/core/application';
  6. import {
  7. MenuItem, Menu
  8. } from 'phosphor-menus';
  9. import {
  10. Widget
  11. } from 'phosphor-widget';
  12. import {
  13. IFrame
  14. } from '../iframe';
  15. import {
  16. MainMenu
  17. } from '../mainmenu/plugin';
  18. /**
  19. * The class name added to the help widget.
  20. */
  21. const HELP_CLASS = 'jp-Help';
  22. /**
  23. * A list of commands to add to the help widget.
  24. */
  25. const COMMANDS = [
  26. {
  27. text: 'Scipy Lecture Notes',
  28. id: 'help-doc:scipy-lecture-notes',
  29. url: 'http://www.scipy-lectures.org/'
  30. },
  31. {
  32. text: 'Numpy Reference',
  33. id: 'help-doc:numpy-reference',
  34. url: '//docs.scipy.org/doc/numpy/reference/'
  35. },
  36. {
  37. text: 'Scipy Reference',
  38. id: 'help-doc:scipy-reference',
  39. url: '//docs.scipy.org/doc/scipy/reference/'
  40. },
  41. {
  42. text: 'Notebook Tutorial',
  43. id: 'help-doc:notebook-tutorial',
  44. url: '//nbviewer.jupyter.org/github/jupyter/notebook/' +
  45. 'blob/master/docs/source/examples/Notebook/Notebook Basics.ipynb'
  46. },
  47. {
  48. text: 'Python Reference',
  49. id: 'help-doc:python-reference',
  50. url: '//docs.python.org/3.5/'
  51. },
  52. {
  53. text: 'IPython Reference',
  54. id: 'help-doc:ipython-reference',
  55. url: '//ipython.org/documentation.html?v=20160707164940'
  56. },
  57. {
  58. text: 'Matplotlib Reference',
  59. id: 'help-doc:mathplotlib-reference',
  60. url: 'http://matplotlib.org/contents.html?v=20160707164940'
  61. },
  62. {
  63. text: 'SymPy Reference',
  64. id: 'help-doc:sympy-reference',
  65. url: 'http://docs.sympy.org/latest/index.html?v=20160707164940'
  66. },
  67. {
  68. text: 'Pandas Reference',
  69. id: 'help-doc:pandas-reference',
  70. url: 'http://pandas.pydata.org/pandas-docs/stable/?v=20160707164940'
  71. },
  72. {
  73. text: 'Markdown Reference',
  74. id: 'help-doc:markdown-reference',
  75. url: '//help.github.com/articles/getting-started-with-writing-and-formatting-on-github/'
  76. }
  77. ];
  78. /**
  79. * The help handler extension.
  80. */
  81. export
  82. const helpHandlerExtension = {
  83. id: 'jupyter.extensions.help-handler',
  84. requires: [MainMenu],
  85. activate: activateHelpHandler
  86. };
  87. /**
  88. * Activate the help handler extension.
  89. *
  90. * @param app - The phosphide application object.
  91. *
  92. * returns A promise that resolves when the extension is activated.
  93. */
  94. function activateHelpHandler(app: Application, mainMenu: MainMenu): Promise<void> {
  95. let iframe = new IFrame();
  96. iframe.addClass(HELP_CLASS);
  97. iframe.title.text = 'Help';
  98. iframe.id = 'help-doc';
  99. let helpCommandItems = COMMANDS.map(command => {
  100. return {
  101. id: command.id,
  102. handler: () => {
  103. Private.attachHelp(app, iframe);
  104. Private.showHelp(app, iframe);
  105. iframe.loadURL(command.url);
  106. }
  107. };
  108. });
  109. app.commands.add(helpCommandItems);
  110. app.commands.add([
  111. {
  112. id: 'help-doc:activate',
  113. handler: () => { Private.showHelp(app, iframe); }
  114. },
  115. {
  116. id: 'help-doc:hide',
  117. handler: () => { Private.hideHelp(app, iframe); }
  118. },
  119. {
  120. id: 'help-doc:toggle',
  121. handler: () => { Private.toggleHelp(app, iframe); }
  122. }
  123. ]);
  124. let helpPaletteItems = COMMANDS.map(command => {
  125. return {
  126. command: command.id,
  127. text: command.text,
  128. caption: `Open ${command.text}`,
  129. category: 'Help'
  130. };
  131. });
  132. app.palette.add(helpPaletteItems);
  133. let menu = new Menu([
  134. new MenuItem({
  135. text: 'About JupyterLab',
  136. handler: () => {
  137. app.commands.execute('about-jupyterlab:show');
  138. }
  139. }),
  140. new MenuItem({
  141. text: 'Frequently Asked Questions',
  142. handler: () => {
  143. app.commands.execute('faq-jupyterlab:show');
  144. }
  145. }),
  146. new MenuItem({
  147. text: 'Notebook Tutorial',
  148. handler: () => {
  149. app.commands.execute('help-doc:notebook-tutorial');
  150. }
  151. }),
  152. new MenuItem({
  153. type: MenuItem.Separator
  154. }),
  155. new MenuItem({
  156. text: 'IPython Reference',
  157. handler: () => {
  158. app.commands.execute('help-doc:ipython-reference');
  159. }
  160. }),
  161. new MenuItem({
  162. text: 'Markdown Reference',
  163. handler: () => {
  164. app.commands.execute('help-doc:markdown-reference');
  165. }
  166. }),
  167. new MenuItem({
  168. text: 'Matplotlib Reference',
  169. handler: () => {
  170. app.commands.execute('help-doc:mathplotlib-reference');
  171. }
  172. }),
  173. new MenuItem({
  174. text: 'Numpy Reference',
  175. handler: () => {
  176. app.commands.execute('help-doc:numpy-reference');
  177. }
  178. }),
  179. new MenuItem({
  180. text: 'Pandas Reference',
  181. handler: () => {
  182. app.commands.execute('help-doc:pandas-reference');
  183. }
  184. }),
  185. new MenuItem({
  186. text: 'Python Reference',
  187. handler: () => {
  188. app.commands.execute('help-doc:python-reference');
  189. }
  190. }),
  191. new MenuItem({
  192. text: 'Scipy Lecture Notes',
  193. handler: () => {
  194. app.commands.execute('help-doc:scipy-lecture-notes');
  195. }
  196. }),
  197. new MenuItem({
  198. text: 'Scipy Reference',
  199. handler: () => {
  200. app.commands.execute('help-doc:scipy-reference');
  201. }
  202. }),
  203. new MenuItem({
  204. text: 'SymPy Reference',
  205. handler: () => {
  206. app.commands.execute('help-doc:sympy-reference');
  207. }
  208. })
  209. ]);
  210. let helpMenu = new MenuItem({ text: 'Help', submenu: menu });
  211. mainMenu.addItem(helpMenu);
  212. return Promise.resolve(void 0);
  213. }
  214. /**
  215. * A namespace for help plugin private functions.
  216. */
  217. namespace Private {
  218. /**
  219. * Attach the help iframe widget to the application shell.
  220. */
  221. export
  222. function attachHelp(app: Application, iframe: Widget): void {
  223. if (!iframe.isAttached) {
  224. app.shell.addToRightArea(iframe);
  225. }
  226. }
  227. /**
  228. * Show the help widget.
  229. */
  230. export
  231. function showHelp(app: Application, iframe: Widget): void {
  232. app.shell.activateRight(iframe.id);
  233. }
  234. /**
  235. * Hide the help widget.
  236. */
  237. export
  238. function hideHelp(app: Application, iframe: Widget): void {
  239. if (!iframe.isHidden) {
  240. app.shell.collapseRight();
  241. }
  242. }
  243. /**
  244. * Toggle whether the help widget is shown or hidden.
  245. */
  246. export
  247. function toggleHelp(app: Application, iframe: Widget): void {
  248. if (iframe.isHidden) {
  249. showHelp(app, iframe);
  250. } else {
  251. hideHelp(app, iframe);
  252. }
  253. }
  254. }