utils.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. this.x = x;
  48. this.y = y;
  49. }
  50. /**
  51. * Moves the point by a given `changeX` and `changeY`.
  52. *
  53. * @param {Number} changeX
  54. * @param {Number} changeY
  55. * @returns {Point} Returns current instance.
  56. */
  57. moveBy( changeX, changeY ) {
  58. this.x += changeX;
  59. this.y += changeY;
  60. return this;
  61. }
  62. /**
  63. * @returns {Point}
  64. */
  65. clone() {
  66. return new Point( this.x, this.y );
  67. }
  68. }