utils.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 function getWidgetDomParts( editor, widget, resizerPosition ) {
  46. const view = editor.editing.view;
  47. const domWidget = view.domConverter.mapViewToDom( widget );
  48. return {
  49. resizeWrapper: domWidget.querySelector( '.ck-widget__resizer' ),
  50. resizeHandle: domWidget.querySelector( `.ck-widget__resizer__handle-${ resizerPosition }` ),
  51. widget: domWidget
  52. };
  53. }
  54. export class Point {
  55. constructor( x, y ) {
  56. this.x = x;
  57. this.y = y;
  58. }
  59. /**
  60. * Moves the point by a given `changeX` and `changeY`.
  61. *
  62. * @param {Number} changeX
  63. * @param {Number} changeY
  64. * @returns {Point} Returns current instance.
  65. */
  66. moveBy( changeX, changeY ) {
  67. this.x += changeX;
  68. this.y += changeY;
  69. return this;
  70. }
  71. /**
  72. * @returns {Point}
  73. */
  74. clone() {
  75. return new Point( this.x, this.y );
  76. }
  77. }