viewUserInputEvents.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. import { MouseTarget } from '../controller/mouseTarget.js';
  6. export class ViewUserInputEvents {
  7. constructor(coordinatesConverter) {
  8. this.onKeyDown = null;
  9. this.onKeyUp = null;
  10. this.onContextMenu = null;
  11. this.onMouseMove = null;
  12. this.onMouseLeave = null;
  13. this.onMouseDown = null;
  14. this.onMouseUp = null;
  15. this.onMouseDrag = null;
  16. this.onMouseDrop = null;
  17. this.onMouseDropCanceled = null;
  18. this.onMouseWheel = null;
  19. this._coordinatesConverter = coordinatesConverter;
  20. }
  21. emitKeyDown(e) {
  22. if (this.onKeyDown) {
  23. this.onKeyDown(e);
  24. }
  25. }
  26. emitKeyUp(e) {
  27. if (this.onKeyUp) {
  28. this.onKeyUp(e);
  29. }
  30. }
  31. emitContextMenu(e) {
  32. if (this.onContextMenu) {
  33. this.onContextMenu(this._convertViewToModelMouseEvent(e));
  34. }
  35. }
  36. emitMouseMove(e) {
  37. if (this.onMouseMove) {
  38. this.onMouseMove(this._convertViewToModelMouseEvent(e));
  39. }
  40. }
  41. emitMouseLeave(e) {
  42. if (this.onMouseLeave) {
  43. this.onMouseLeave(this._convertViewToModelMouseEvent(e));
  44. }
  45. }
  46. emitMouseDown(e) {
  47. if (this.onMouseDown) {
  48. this.onMouseDown(this._convertViewToModelMouseEvent(e));
  49. }
  50. }
  51. emitMouseUp(e) {
  52. if (this.onMouseUp) {
  53. this.onMouseUp(this._convertViewToModelMouseEvent(e));
  54. }
  55. }
  56. emitMouseDrag(e) {
  57. if (this.onMouseDrag) {
  58. this.onMouseDrag(this._convertViewToModelMouseEvent(e));
  59. }
  60. }
  61. emitMouseDrop(e) {
  62. if (this.onMouseDrop) {
  63. this.onMouseDrop(this._convertViewToModelMouseEvent(e));
  64. }
  65. }
  66. emitMouseDropCanceled() {
  67. if (this.onMouseDropCanceled) {
  68. this.onMouseDropCanceled();
  69. }
  70. }
  71. emitMouseWheel(e) {
  72. if (this.onMouseWheel) {
  73. this.onMouseWheel(e);
  74. }
  75. }
  76. _convertViewToModelMouseEvent(e) {
  77. if (e.target) {
  78. return {
  79. event: e.event,
  80. target: this._convertViewToModelMouseTarget(e.target)
  81. };
  82. }
  83. return e;
  84. }
  85. _convertViewToModelMouseTarget(target) {
  86. return ViewUserInputEvents.convertViewToModelMouseTarget(target, this._coordinatesConverter);
  87. }
  88. static convertViewToModelMouseTarget(target, coordinatesConverter) {
  89. return new ExternalMouseTarget(target.element, target.type, target.mouseColumn, target.position ? coordinatesConverter.convertViewPositionToModelPosition(target.position) : null, target.range ? coordinatesConverter.convertViewRangeToModelRange(target.range) : null, target.detail);
  90. }
  91. }
  92. class ExternalMouseTarget {
  93. constructor(element, type, mouseColumn, position, range, detail) {
  94. this.element = element;
  95. this.type = type;
  96. this.mouseColumn = mouseColumn;
  97. this.position = position;
  98. this.range = range;
  99. this.detail = detail;
  100. }
  101. toString() {
  102. return MouseTarget.toString(this);
  103. }
  104. }