edit.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { Menu, Widget } from '@phosphor/widgets';
  4. import { IJupyterLabMenu, IMenuExtender, JupyterLabMenu } from './labmenu';
  5. /**
  6. * An interface for an Edit menu.
  7. */
  8. export interface IEditMenu extends IJupyterLabMenu {
  9. /**
  10. * A set storing IUndoers for the Edit menu.
  11. */
  12. readonly undoers: Set<IEditMenu.IUndoer<Widget>>;
  13. /**
  14. * A set storing IClearers for the Edit menu.
  15. */
  16. readonly clearers: Set<IEditMenu.IClearer<Widget>>;
  17. /**
  18. * A set storing IFindReplacers for the Edit menu.
  19. */
  20. readonly findReplacers: Set<IEditMenu.IFindReplacer<Widget>>;
  21. /**
  22. * A set storing IGoToLiners for the Edit menu.
  23. */
  24. readonly goToLiners: Set<IEditMenu.IGoToLiner<Widget>>;
  25. }
  26. /**
  27. * An extensible Edit menu for the application.
  28. */
  29. export class EditMenu extends JupyterLabMenu implements IEditMenu {
  30. /**
  31. * Construct the edit menu.
  32. */
  33. constructor(options: Menu.IOptions) {
  34. super(options);
  35. this.menu.title.label = 'Edit';
  36. this.undoers = new Set<IEditMenu.IUndoer<Widget>>();
  37. this.clearers = new Set<IEditMenu.IClearer<Widget>>();
  38. this.findReplacers = new Set<IEditMenu.IFindReplacer<Widget>>();
  39. this.goToLiners = new Set<IEditMenu.IGoToLiner<Widget>>();
  40. }
  41. /**
  42. * A set storing IUndoers for the Edit menu.
  43. */
  44. readonly undoers: Set<IEditMenu.IUndoer<Widget>>;
  45. /**
  46. * A set storing IClearers for the Edit menu.
  47. */
  48. readonly clearers: Set<IEditMenu.IClearer<Widget>>;
  49. /**
  50. * A set storing IFindReplacers for the Edit menu.
  51. */
  52. readonly findReplacers: Set<IEditMenu.IFindReplacer<Widget>>;
  53. /**
  54. * A set storing IGoToLiners for the Edit menu.
  55. */
  56. readonly goToLiners: Set<IEditMenu.IGoToLiner<Widget>>;
  57. /**
  58. * Dispose of the resources held by the edit menu.
  59. */
  60. dispose(): void {
  61. this.undoers.clear();
  62. this.clearers.clear();
  63. this.findReplacers.clear();
  64. super.dispose();
  65. }
  66. }
  67. /**
  68. * Namespace for IEditMenu
  69. */
  70. export namespace IEditMenu {
  71. /**
  72. * Interface for an activity that uses Undo/Redo.
  73. */
  74. export interface IUndoer<T extends Widget> extends IMenuExtender<T> {
  75. /**
  76. * Execute an undo command for the activity.
  77. */
  78. undo?: (widget: T) => void;
  79. /**
  80. * Execute a redo command for the activity.
  81. */
  82. redo?: (widget: T) => void;
  83. }
  84. /**
  85. * Interface for an activity that wants to register a 'Clear...' menu item
  86. */
  87. export interface IClearer<T extends Widget> extends IMenuExtender<T> {
  88. /**
  89. * A name for the thing to be cleared, used for labeling `clearCurrent`.
  90. */
  91. noun?: string;
  92. /**
  93. * A plural name for the thing to be cleared, used for labeling `clearAll`.
  94. */
  95. pluralNoun?: string;
  96. /**
  97. * A function to clear the currently portion of activity.
  98. */
  99. clearCurrent?: (widget: T) => void;
  100. /**
  101. * A function to clear all of an activity.
  102. */
  103. clearAll?: (widget: T) => void;
  104. }
  105. /**
  106. * Interface for an activity that uses Find/Find+Replace.
  107. */
  108. export interface IFindReplacer<T extends Widget> extends IMenuExtender<T> {
  109. /**
  110. * Execute a find command for the activity.
  111. */
  112. find?: (widget: T) => void;
  113. /**
  114. * Execute a find/replace command for the activity.
  115. */
  116. findAndReplace?: (widget: T) => void;
  117. }
  118. /**
  119. * Interface for an activity that uses Go to Line.
  120. */
  121. export interface IGoToLiner<T extends Widget> extends IMenuExtender<T> {
  122. /**
  123. * Execute a go to line command for the activity.
  124. */
  125. goToLine?: (widget: T) => void;
  126. }
  127. }