index.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import 'es6-promise/auto'; // polyfill Promise on IE
  4. import '@jupyterlab/theme-light-extension/static/embed.css';
  5. import '../index.css';
  6. import { CommandRegistry } from '@phosphor/commands';
  7. import { CommandPalette, SplitPanel, Widget } from '@phosphor/widgets';
  8. import { ServiceManager } from '@jupyterlab/services';
  9. import { editorServices } from '@jupyterlab/codemirror';
  10. import { ConsolePanel } from '@jupyterlab/console';
  11. import {
  12. RenderMimeRegistry,
  13. standardRendererFactories as initialFactories
  14. } from '@jupyterlab/rendermime';
  15. let TITLE = 'Console';
  16. function main(): void {
  17. let path = '';
  18. let query: { [key: string]: string } = Object.create(null);
  19. window.location.search
  20. .substr(1)
  21. .split('&')
  22. .forEach(item => {
  23. let pair = item.split('=');
  24. if (pair[0]) {
  25. query[pair[0]] = pair[1];
  26. }
  27. });
  28. if (query['path']) {
  29. path = query['path'];
  30. }
  31. let manager = new ServiceManager();
  32. manager.ready.then(() => {
  33. startApp(path, manager);
  34. });
  35. }
  36. /**
  37. * Start the application.
  38. */
  39. function startApp(path: string, manager: ServiceManager.IManager) {
  40. // Initialize the command registry with the key bindings.
  41. let commands = new CommandRegistry();
  42. // Setup the keydown listener for the document.
  43. document.addEventListener('keydown', event => {
  44. commands.processKeydownEvent(event);
  45. });
  46. let rendermime = new RenderMimeRegistry({ initialFactories });
  47. let editorFactory = editorServices.factoryService.newInlineEditor;
  48. let contentFactory = new ConsolePanel.ContentFactory({ editorFactory });
  49. let consolePanel = new ConsolePanel({
  50. rendermime,
  51. manager,
  52. path,
  53. contentFactory,
  54. mimeTypeService: editorServices.mimeTypeService
  55. });
  56. consolePanel.title.label = TITLE;
  57. let palette = new CommandPalette({ commands });
  58. let panel = new SplitPanel();
  59. panel.id = 'main';
  60. panel.orientation = 'horizontal';
  61. panel.spacing = 0;
  62. SplitPanel.setStretch(palette, 0);
  63. SplitPanel.setStretch(consolePanel, 1);
  64. panel.addWidget(palette);
  65. panel.addWidget(consolePanel);
  66. // Attach the panel to the DOM.
  67. Widget.attach(panel, document.body);
  68. // Handle resize events.
  69. window.addEventListener('resize', () => {
  70. panel.update();
  71. });
  72. let selector = '.jp-ConsolePanel';
  73. let category = 'Console';
  74. let command: string;
  75. // Add the commands.
  76. command = 'console:clear';
  77. commands.addCommand(command, {
  78. label: 'Clear',
  79. execute: () => {
  80. consolePanel.console.clear();
  81. }
  82. });
  83. palette.addItem({ command, category });
  84. command = 'console:execute';
  85. commands.addCommand(command, {
  86. label: 'Execute Prompt',
  87. execute: () => {
  88. consolePanel.console.execute();
  89. }
  90. });
  91. palette.addItem({ command, category });
  92. commands.addKeyBinding({ command, selector, keys: ['Enter'] });
  93. command = 'console:execute-forced';
  94. commands.addCommand(command, {
  95. label: 'Execute Cell (forced)',
  96. execute: () => {
  97. consolePanel.console.execute(true);
  98. }
  99. });
  100. palette.addItem({ command, category });
  101. commands.addKeyBinding({ command, selector, keys: ['Shift Enter'] });
  102. command = 'console:linebreak';
  103. commands.addCommand(command, {
  104. label: 'Insert Line Break',
  105. execute: () => {
  106. consolePanel.console.insertLinebreak();
  107. }
  108. });
  109. palette.addItem({ command, category });
  110. commands.addKeyBinding({ command, selector, keys: ['Ctrl Enter'] });
  111. }
  112. window.addEventListener('load', main);