index.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. ILayoutRestorer, JupyterLab, JupyterLabPlugin
  5. } from '@jupyterlab/application';
  6. import {
  7. ICommandPalette, InstanceTracker
  8. } from '@jupyterlab/apputils';
  9. import {
  10. ImageViewer, ImageViewerFactory, IImageTracker
  11. } from '@jupyterlab/imageviewer';
  12. /**
  13. * The command IDs used by the image widget plugin.
  14. */
  15. namespace CommandIDs {
  16. export
  17. const resetZoom = 'imageviewer:reset-zoom';
  18. export
  19. const zoomIn = 'imageviewer:zoom-in';
  20. export
  21. const zoomOut = 'imageviewer:zoom-out';
  22. export
  23. const rot90 = 'imageviewer:rot90';
  24. export
  25. const rot270 = 'imageviewer:rot270';
  26. }
  27. /**
  28. * The list of file types for images.
  29. */
  30. const FILE_TYPES = [
  31. 'png', 'gif', 'jpeg', 'svg', 'bmp', 'ico', 'xbm', 'tiff'
  32. ];
  33. /**
  34. * The name of the factory that creates image widgets.
  35. */
  36. const FACTORY = 'Image';
  37. /**
  38. * The image file handler extension.
  39. */
  40. const plugin: JupyterLabPlugin<IImageTracker> = {
  41. activate,
  42. id: '@jupyterlab/imageviewer-extension:plugin',
  43. provides: IImageTracker,
  44. requires: [ICommandPalette, ILayoutRestorer],
  45. autoStart: true
  46. };
  47. /**
  48. * Export the plugin as default.
  49. */
  50. export default plugin;
  51. /**
  52. * Activate the image widget extension.
  53. */
  54. function activate(app: JupyterLab, palette: ICommandPalette, restorer: ILayoutRestorer): IImageTracker {
  55. const namespace = 'image-widget';
  56. const factory = new ImageViewerFactory({
  57. name: FACTORY,
  58. modelName: 'base64',
  59. fileTypes: FILE_TYPES,
  60. defaultFor: FILE_TYPES,
  61. readOnly: true
  62. });
  63. const tracker = new InstanceTracker<ImageViewer>({ namespace });
  64. // Handle state restoration.
  65. restorer.restore(tracker, {
  66. command: 'docmanager:open',
  67. args: widget => ({ path: widget.context.path, factory: FACTORY }),
  68. name: widget => widget.context.path
  69. });
  70. app.docRegistry.addWidgetFactory(factory);
  71. factory.widgetCreated.connect((sender, widget) => {
  72. // Notify the instance tracker if restore data needs to update.
  73. widget.context.pathChanged.connect(() => { tracker.save(widget); });
  74. tracker.add(widget);
  75. const types = app.docRegistry.getFileTypesForPath(widget.context.path);
  76. if (types.length > 0) {
  77. widget.title.iconClass = types[0].iconClass;
  78. widget.title.iconLabel = types[0].iconLabel;
  79. }
  80. });
  81. addCommands(app, tracker);
  82. const category = 'Image Viewer';
  83. [CommandIDs.zoomIn, CommandIDs.zoomOut, CommandIDs.resetZoom, CommandIDs.rot90, CommandIDs.rot270]
  84. .forEach(command => { palette.addItem({ command, category }); });
  85. return tracker;
  86. }
  87. /**
  88. * Add the commands for the image widget.
  89. */
  90. export
  91. function addCommands(app: JupyterLab, tracker: IImageTracker) {
  92. const { commands } = app;
  93. /**
  94. * Whether there is an active image viewer.
  95. */
  96. function isEnabled(): boolean {
  97. return tracker.currentWidget !== null &&
  98. tracker.currentWidget === app.shell.currentWidget;
  99. }
  100. commands.addCommand('imageviewer:zoom-in', {
  101. execute: zoomIn,
  102. label: 'Zoom In',
  103. isEnabled
  104. });
  105. commands.addCommand('imageviewer:zoom-out', {
  106. execute: zoomOut,
  107. label: 'Zoom Out',
  108. isEnabled
  109. });
  110. commands.addCommand('imageviewer:reset-zoom', {
  111. execute: resetZoom,
  112. label: 'Reset Zoom',
  113. isEnabled
  114. });
  115. commands.addCommand('imageviewer:rot90', {
  116. execute: rot90,
  117. label: 'Rotate clockwise',
  118. isEnabled
  119. });
  120. commands.addCommand('imageviewer:rot270', {
  121. execute: rot270,
  122. label: 'Rotate counterclockwise',
  123. isEnabled
  124. });
  125. function zoomIn(): void {
  126. const widget = tracker.currentWidget;
  127. if (widget) {
  128. widget.scale = widget.scale > 1 ? widget.scale + 0.5 : widget.scale * 2;
  129. }
  130. }
  131. function zoomOut(): void {
  132. const widget = tracker.currentWidget;
  133. if (widget) {
  134. widget.scale = widget.scale > 1 ? widget.scale - 0.5 : widget.scale / 2;
  135. }
  136. }
  137. function resetZoom(): void {
  138. const widget = tracker.currentWidget;
  139. if (widget) {
  140. widget.scale = 1;
  141. }
  142. }
  143. function rot90(): void {
  144. const widget = tracker.currentWidget;
  145. if (widget) {
  146. widget.rotation += 90;
  147. }
  148. }
  149. function rot270(): void {
  150. const widget = tracker.currentWidget;
  151. if (widget) {
  152. widget.rotation += 270;
  153. }
  154. }
  155. }