utils.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import WidgetResize from '../../../src/widgetresize';
  6. export const mouseMock = {
  7. down( editor, domTarget ) {
  8. this._getPlugin( editor )._mouseDownListener( {}, {
  9. target: domTarget
  10. } );
  11. },
  12. move( editor, domTarget, eventData ) {
  13. const combinedEventData = Object.assign( {}, eventData, {
  14. target: domTarget
  15. } );
  16. this._getPlugin( editor )._mouseMoveListener( {}, combinedEventData );
  17. },
  18. up( editor ) {
  19. this._getPlugin( editor )._mouseUpListener();
  20. },
  21. /**
  22. * Emulates mouse drag gesture by triggering:
  23. *
  24. * * the `mousedown` event on the `domTarget`,
  25. * * the `mousemove` event on `domTarget`, with the pointer coordinates at `finalPosition`,
  26. * * the `mouseup` event.
  27. *
  28. * @param {module:core/editor/editor~Editor} editor
  29. * @param {HTMLElement} domTarget
  30. * @param {Point} finalPosition
  31. */
  32. dragTo( editor, domTarget, finalPosition ) {
  33. const moveEventData = {
  34. pageX: finalPosition.x,
  35. pageY: finalPosition.y
  36. };
  37. this.down( editor, domTarget );
  38. this.move( editor, domTarget, moveEventData );
  39. this.up( editor );
  40. },
  41. _getPlugin( editor ) {
  42. return editor.plugins.get( WidgetResize );
  43. }
  44. };
  45. export class Point {
  46. constructor( x, y ) {
  47. /**
  48. * @readonly
  49. */
  50. this.x = x;
  51. /**
  52. * @readonly
  53. */
  54. this.y = y;
  55. }
  56. /**
  57. * Moves the point by a given `changeX` and `changeY` and returns it as a **new instance**.
  58. *
  59. * @param {Number} changeX
  60. * @param {Number} changeY
  61. * @returns {Point}
  62. */
  63. moveBy( changeX, changeY ) {
  64. return new Point( this.x + changeX, this.y + changeY );
  65. }
  66. /**
  67. * Moves the point to a given position in x axis and returns it as a **new instance**.
  68. *
  69. * @param {Number} x
  70. * @returns {Point}
  71. */
  72. moveToX( x ) {
  73. return new Point( x, this.y );
  74. }
  75. /**
  76. * Moves the point to a given position in y axis and returns it as a **new instance**.
  77. *
  78. * @param {Number} y
  79. * @returns {Point}
  80. */
  81. moveToY( y ) {
  82. return new Point( this.x, y );
  83. }
  84. }