plugin.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. IWidgetOpener, FileBrowserWidget
  5. } from './browser';
  6. import {
  7. FileBrowserModel
  8. } from './model';
  9. import {
  10. DocumentManager
  11. } from '../docmanager';
  12. import {
  13. DocumentRegistry
  14. } from '../docregistry';
  15. import {
  16. Application
  17. } from 'phosphide/lib/core/application';
  18. import {
  19. Menu, MenuItem
  20. } from 'phosphor-menus';
  21. import {
  22. Widget
  23. } from 'phosphor-widget';
  24. import {
  25. JupyterServices
  26. } from '../services/plugin';
  27. import {
  28. WidgetTracker
  29. } from '../widgettracker';
  30. /**
  31. * The default file browser extension.
  32. */
  33. export
  34. const fileBrowserExtension = {
  35. id: 'jupyter.extensions.fileBrowser',
  36. requires: [JupyterServices, DocumentRegistry],
  37. activate: activateFileBrowser
  38. };
  39. /**
  40. * The class name for all main area portrait tab icons.
  41. */
  42. const PORTRAIT_ICON_CLASS = 'jp-MainAreaPortraitIcon';
  43. /**
  44. * The class name for the notebook icon from the default theme.
  45. */
  46. const NOTEBOOK_ICON_CLASS = 'jp-ImageNotebook';
  47. /**
  48. * The class name for the text editor icon from the default theme.
  49. */
  50. const TEXTEDITOR_ICON_CLASS = 'jp-ImageTextEditor';
  51. /**
  52. * Activate the file browser.
  53. */
  54. function activateFileBrowser(app: Application, provider: JupyterServices, registry: DocumentRegistry): Promise<void> {
  55. let contents = provider.contentsManager;
  56. let sessions = provider.sessionManager;
  57. let id = 0;
  58. let tracker = new WidgetTracker<Widget>();
  59. let activeWidget: Widget;
  60. tracker.activeWidgetChanged.connect((sender, widget) => {
  61. activeWidget = widget;
  62. });
  63. let opener: IWidgetOpener = {
  64. open: (widget) => {
  65. if (!widget.id) {
  66. widget.id = `document-manager-${++id}`;
  67. }
  68. if (!widget.isAttached) {
  69. app.shell.addToMainArea(widget);
  70. tracker.addWidget(widget);
  71. }
  72. }
  73. };
  74. let docManager = new DocumentManager({
  75. registry,
  76. contentsManager: contents,
  77. sessionManager: sessions,
  78. kernelspecs: provider.kernelspecs,
  79. opener
  80. });
  81. let fbModel = new FileBrowserModel({
  82. contentsManager: contents,
  83. sessionManager: sessions,
  84. kernelspecs: provider.kernelspecs
  85. });
  86. let fbWidget = new FileBrowserWidget({
  87. model: fbModel,
  88. manager: docManager,
  89. opener
  90. });
  91. let menu = createMenu(fbWidget);
  92. // Add a context menu to the dir listing.
  93. let node = fbWidget.node.getElementsByClassName('jp-DirListing-content')[0];
  94. node.addEventListener('contextmenu', (event: MouseEvent) => {
  95. event.preventDefault();
  96. let x = event.clientX;
  97. let y = event.clientY;
  98. menu.popup(x, y);
  99. });
  100. // Add the command for a new items.
  101. let newTextFileId = 'file-operations:new-text-file';
  102. app.commands.add([
  103. {
  104. id: newTextFileId,
  105. <<<<<<< HEAD
  106. handler: () => {
  107. let icon = `${PORTRAIT_ICON_CLASS} ${TEXTEDITOR_ICON_CLASS}`;
  108. fbWidget.createNew('file').then(widget => widget.title.icon = icon);
  109. }
  110. =======
  111. handler: () => fbWidget.createNew({ type: 'file' })
  112. >>>>>>> e8dce4a... Update jupyter-js-services and clean up interfaces
  113. }
  114. ]);
  115. let newNotebookId = 'file-operations:new-notebook';
  116. app.commands.add([
  117. {
  118. id: newNotebookId,
  119. handler: () => {
  120. let icon = `${PORTRAIT_ICON_CLASS} ${NOTEBOOK_ICON_CLASS}`;
  121. fbWidget.createNew({ type: 'notebook' }).then(widget => widget.title.icon = icon);
  122. }
  123. }]);
  124. // Add the command for saving a document.
  125. let saveDocumentId = 'file-operations:save';
  126. app.commands.add([
  127. {
  128. id: saveDocumentId,
  129. handler: () => {
  130. if (activeWidget) {
  131. let context = docManager.contextForWidget(activeWidget);
  132. context.save();
  133. }
  134. }
  135. }
  136. ]);
  137. app.palette.add([
  138. {
  139. command: saveDocumentId,
  140. category: 'File Operations',
  141. text: 'Save Document',
  142. caption: 'Save the current document'
  143. }
  144. ]);
  145. // Add the command for reverting a document.
  146. let revertDocumentId = 'file-operations:revert';
  147. app.commands.add([
  148. {
  149. id: revertDocumentId,
  150. handler: () => {
  151. if (activeWidget) {
  152. let context = docManager.contextForWidget(activeWidget);
  153. context.revert();
  154. }
  155. }
  156. }
  157. ]);
  158. app.palette.add([
  159. {
  160. command: revertDocumentId,
  161. category: 'File Operations',
  162. text: 'Revert Document',
  163. caption: 'Revert the current document'
  164. }
  165. ]);
  166. // Add the command for closing a document.
  167. let closeDocumentId = 'file-operations:close';
  168. app.commands.add([
  169. {
  170. id: closeDocumentId,
  171. handler: () => {
  172. if (activeWidget) {
  173. activeWidget.close();
  174. }
  175. }
  176. }
  177. ]);
  178. app.palette.add([
  179. {
  180. command: closeDocumentId,
  181. category: 'File Operations',
  182. text: 'Close Document',
  183. caption: 'Close the current document'
  184. }
  185. ]);
  186. // Add the command for closing all documents.
  187. let closeAllId = 'file-operations:close-all';
  188. app.commands.add([
  189. {
  190. id: closeAllId,
  191. handler: () => {
  192. docManager.closeAll();
  193. }
  194. }
  195. ]);
  196. app.palette.add([
  197. {
  198. command: closeAllId,
  199. category: 'File Operations',
  200. text: 'Close All',
  201. caption: 'Close all open documents'
  202. }
  203. ]);
  204. app.palette.add([
  205. {
  206. command: newTextFileId,
  207. category: 'File Operations',
  208. text: 'New Text File',
  209. caption: 'Create a new text file'
  210. },
  211. {
  212. command: newNotebookId,
  213. category: 'File Operations',
  214. text: 'New Notebook',
  215. caption: 'Create a new notebook'
  216. }
  217. ]);
  218. app.commands.add([
  219. {
  220. id: 'file-browser:activate',
  221. handler: showBrowser
  222. },
  223. {
  224. id: 'file-browser:hide',
  225. handler: hideBrowser
  226. },
  227. {
  228. id: 'file-browser:toggle',
  229. handler: toggleBrowser
  230. }
  231. ]);
  232. fbWidget.title.text = 'Files';
  233. fbWidget.id = 'file-browser';
  234. app.shell.addToLeftArea(fbWidget, { rank: 40 });
  235. showBrowser();
  236. return Promise.resolve(void 0);
  237. function showBrowser(): void {
  238. app.shell.activateLeft(fbWidget.id);
  239. }
  240. function hideBrowser(): void {
  241. if (!fbWidget.isHidden) {
  242. app.shell.collapseLeft();
  243. }
  244. }
  245. function toggleBrowser(): void {
  246. if (fbWidget.isHidden) {
  247. showBrowser();
  248. } else {
  249. hideBrowser();
  250. }
  251. }
  252. }
  253. /**
  254. * Create a context menu for the file browser listing.
  255. */
  256. function createMenu(fbWidget: FileBrowserWidget): Menu {
  257. return new Menu([
  258. new MenuItem({
  259. text: '&Open',
  260. icon: 'fa fa-folder-open-o',
  261. shortcut: 'Ctrl+O',
  262. handler: () => { fbWidget.open(); }
  263. }),
  264. new MenuItem({
  265. text: '&Rename',
  266. icon: 'fa fa-edit',
  267. shortcut: 'Ctrl+R',
  268. handler: () => { fbWidget.rename(); }
  269. }),
  270. new MenuItem({
  271. text: '&Delete',
  272. icon: 'fa fa-remove',
  273. shortcut: 'Ctrl+D',
  274. handler: () => { fbWidget.delete(); }
  275. }),
  276. new MenuItem({
  277. text: 'Duplicate',
  278. icon: 'fa fa-copy',
  279. handler: () => { fbWidget.duplicate(); }
  280. }),
  281. new MenuItem({
  282. text: 'Cut',
  283. icon: 'fa fa-cut',
  284. shortcut: 'Ctrl+X',
  285. handler: () => { fbWidget.cut(); }
  286. }),
  287. new MenuItem({
  288. text: '&Copy',
  289. icon: 'fa fa-copy',
  290. shortcut: 'Ctrl+C',
  291. handler: () => { fbWidget.copy(); }
  292. }),
  293. new MenuItem({
  294. text: '&Paste',
  295. icon: 'fa fa-paste',
  296. shortcut: 'Ctrl+V',
  297. handler: () => { fbWidget.paste(); }
  298. }),
  299. new MenuItem({
  300. text: 'Download',
  301. icon: 'fa fa-download',
  302. handler: () => { fbWidget.download(); }
  303. }),
  304. new MenuItem({
  305. text: 'Shutdown Kernel',
  306. icon: 'fa fa-stop-circle-o',
  307. handler: () => { fbWidget.shutdownKernels(); }
  308. })
  309. ]);
  310. }