widgetresize.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. /* globals document, Event */
  6. import WidgetResize from '../src/widgetresize';
  7. // ClassicTestEditor can't be used, as it doesn't handle the focus, which is needed to test resizer visual cues.
  8. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  9. import ArticlePluginSet from '@ckeditor/ckeditor5-core/tests/_utils/articlepluginset';
  10. import { toWidget } from '../src/utils';
  11. import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  12. import Rect from '@ckeditor/ckeditor5-utils/src/dom/rect';
  13. import { mouseMock, Point } from './widgetresize/_utils/utils';
  14. describe( 'WidgetResize', () => {
  15. let editor, editorElement, widget, mouseListenerSpies;
  16. const commitStub = sinon.stub();
  17. before( () => {
  18. mouseListenerSpies = {
  19. down: sinon.spy( WidgetResize.prototype, '_mouseDownListener' ),
  20. move: sinon.spy( WidgetResize.prototype, '_mouseMoveListener' ),
  21. up: sinon.spy( WidgetResize.prototype, '_mouseUpListener' )
  22. };
  23. } );
  24. after( () => {
  25. for ( const stub of Object.values( mouseListenerSpies ) ) {
  26. stub.restore();
  27. }
  28. } );
  29. beforeEach( async () => {
  30. editorElement = createEditorElement();
  31. editor = await createEditor( editorElement );
  32. setModelData( editor.model, '[<widget></widget>]' );
  33. focusEditor( editor );
  34. widget = editor.editing.view.document.getRoot().getChild( 0 );
  35. for ( const stub of Object.values( mouseListenerSpies ) ) {
  36. stub.resetHistory();
  37. }
  38. commitStub.resetHistory();
  39. // It's crucial to have a precisely defined editor size for this test suite.
  40. editor.editing.view.change( writer => {
  41. const viewEditableRoot = editor.editing.view.document.getRoot();
  42. writer.setAttribute( 'style', 'width: 400px; padding: 0px; overflow: hidden', viewEditableRoot );
  43. } );
  44. } );
  45. afterEach( () => {
  46. editorElement.remove();
  47. if ( editor ) {
  48. return editor.destroy();
  49. }
  50. } );
  51. describe( 'plugin', () => {
  52. it( 'is loaded', () => {
  53. expect( editor.plugins.get( WidgetResize ) ).to.be.instanceOf( WidgetResize );
  54. } );
  55. } );
  56. describe( 'mouse listeners', () => {
  57. beforeEach( () => {
  58. createResizer();
  59. } );
  60. it( 'don\'t break when called with unexpected element', () => {
  61. const unrelatedElement = document.createElement( 'div' );
  62. editor.plugins.get( WidgetResize )._mouseDownListener( {}, {
  63. target: unrelatedElement
  64. } );
  65. } );
  66. it( 'passes new width to the options.onCommit()', () => {
  67. const usedResizer = 'top-right';
  68. const domParts = getWidgetDomParts( widget, usedResizer );
  69. const initialPointerPosition = getHandleCenterPoint( domParts.widget, usedResizer );
  70. const finalPointerPosition = initialPointerPosition.clone().moveBy( 20, 0 );
  71. mouseMock.dragTo( editor, domParts.resizeHandle, finalPointerPosition );
  72. expect( commitStub.callCount ).to.be.equal( 1 );
  73. sinon.assert.calledWithExactly( commitStub, '120px' );
  74. } );
  75. } );
  76. it( 'are detached when plugin is destroyed', async () => {
  77. await editor.destroy();
  78. const plugin = editor.plugins.get( WidgetResize );
  79. editor = null;
  80. const event = new Event( 'mousedown', { bubbles: true } );
  81. document.body.dispatchEvent( event );
  82. // Ensure nothing got called.
  83. expect( plugin._mouseDownListener.callCount ).to.be.equal( 0 );
  84. } );
  85. it( 'nothing bad happens if activeResizer got unset', () => {
  86. createResizer( {
  87. isCentered: () => true
  88. } );
  89. const usedResizer = 'top-right';
  90. const domParts = getWidgetDomParts( widget, usedResizer );
  91. const initialPointerPosition = getHandleCenterPoint( domParts.widget, usedResizer );
  92. editor.plugins.get( WidgetResize )._getResizerByHandle = sinon.stub().returns( null );
  93. mouseMock.dragTo( editor, domParts.resizeHandle, initialPointerPosition );
  94. // No exception should be thrown.
  95. } );
  96. describe( 'Integration (pixels)', () => {
  97. let resizer;
  98. beforeEach( () => {
  99. resizer = createResizer();
  100. } );
  101. it( 'properly sets the state for subsequent resizes', () => {
  102. const usedResizer = 'top-right';
  103. const domParts = getWidgetDomParts( widget, usedResizer );
  104. const initialPointerPosition = getHandleCenterPoint( domParts.widget, usedResizer );
  105. const intermediatePointerPosition = initialPointerPosition.clone().moveBy( 50, 0 );
  106. mouseMock.dragTo( editor, domParts.resizeHandle, intermediatePointerPosition );
  107. sinon.assert.calledWithExactly( commitStub.firstCall, '150px' );
  108. const finalPointerPosition = intermediatePointerPosition.clone().moveBy( 50, 0 );
  109. mouseMock.dragTo( editor, domParts.resizeHandle, finalPointerPosition );
  110. sinon.assert.calledWithExactly( commitStub.secondCall, '200px' );
  111. expect( commitStub.callCount ).to.be.equal( 2 );
  112. } );
  113. it( 'hides the resize wrapper when resizer gets disabled', () => {
  114. const resizerWrapper = editor.ui.getEditableElement().querySelector( '.ck-widget__resizer' );
  115. expect( resizerWrapper.style.display ).to.equal( '' );
  116. resizer.isEnabled = false;
  117. expect( resizerWrapper.style.display ).to.equal( 'none' );
  118. } );
  119. } );
  120. describe( 'Integration (percents)', () => {
  121. beforeEach( () => {
  122. createResizer( {
  123. unit: undefined
  124. } );
  125. } );
  126. it( 'properly sets the state for subsequent resizes', () => {
  127. const usedResizer = 'top-right';
  128. const domParts = getWidgetDomParts( widget, usedResizer );
  129. const initialPointerPosition = getHandleCenterPoint( domParts.widget, usedResizer );
  130. const intermediatePointerPosition = initialPointerPosition.clone().moveBy( 100, 0 );
  131. mouseMock.dragTo( editor, domParts.resizeHandle, intermediatePointerPosition );
  132. sinon.assert.calledWithExactly( commitStub.firstCall, '50%' );
  133. const finalPointerPosition = intermediatePointerPosition.clone().moveBy( 100, 0 );
  134. mouseMock.dragTo( editor, domParts.resizeHandle, finalPointerPosition );
  135. sinon.assert.calledWithExactly( commitStub.secondCall, '75%' );
  136. expect( commitStub.callCount ).to.be.equal( 2 );
  137. } );
  138. } );
  139. function createEditor( element ) {
  140. return ClassicEditor
  141. .create( element, {
  142. plugins: [
  143. ArticlePluginSet, WidgetResize, simpleWidgetPlugin
  144. ]
  145. } );
  146. function simpleWidgetPlugin( editor ) {
  147. editor.model.schema.register( 'widget', {
  148. inheritAllFrom: '$block',
  149. isObject: true
  150. } );
  151. editor.conversion.for( 'downcast' )
  152. .elementToElement( {
  153. model: 'widget',
  154. view: ( modelItem, viewWriter ) => {
  155. const div = viewWriter.createContainerElement( 'div' );
  156. viewWriter.setStyle( 'height', '100px', div );
  157. viewWriter.setStyle( 'width', '25%', div ); // It evaluates to 100px.
  158. return toWidget( div, viewWriter, {
  159. label: 'element label'
  160. } );
  161. }
  162. } );
  163. }
  164. }
  165. function createEditorElement() {
  166. const element = document.createElement( 'div' );
  167. document.body.appendChild( element );
  168. return element;
  169. }
  170. function createResizer( resizerOptions ) {
  171. const widgetModel = editor.model.document.getRoot().getChild( 0 );
  172. const defaultOptions = {
  173. unit: 'px',
  174. modelElement: widgetModel,
  175. viewElement: widget,
  176. editor,
  177. isCentered: () => false,
  178. getHandleHost( domWidgetElement ) {
  179. return domWidgetElement;
  180. },
  181. getResizeHost( domWidgetElement ) {
  182. return domWidgetElement;
  183. },
  184. onCommit: commitStub
  185. };
  186. return editor.plugins.get( WidgetResize ).attachTo( Object.assign( defaultOptions, resizerOptions ) );
  187. }
  188. function getWidgetDomParts( widget, resizerPosition ) {
  189. const view = editor.editing.view;
  190. const resizeWrapper = view.domConverter.mapViewToDom( widget.getChild( 0 ) );
  191. return {
  192. resizeWrapper,
  193. resizeHandle: resizeWrapper.querySelector( `.ck-widget__resizer__handle-${ resizerPosition }` ),
  194. widget: view.domConverter.mapViewToDom( widget )
  195. };
  196. }
  197. /**
  198. * Returns a center point for a given handle.
  199. *
  200. * @param {HTMLElement} domWrapper Wrapper of an element that contains the resizer.
  201. * @param {String} [handlePosition='top-left']
  202. * @returns {Point}
  203. */
  204. function getHandleCenterPoint( domWrapper, handlePosition ) {
  205. const wrapperRect = new Rect( domWrapper );
  206. const returnValue = new Point( wrapperRect.left, wrapperRect.top );
  207. const cornerPositionParts = handlePosition.split( '-' );
  208. if ( cornerPositionParts.includes( 'right' ) ) {
  209. returnValue.x = wrapperRect.right;
  210. }
  211. if ( cornerPositionParts.includes( 'bottom' ) ) {
  212. returnValue.y = wrapperRect.bottom;
  213. }
  214. return returnValue;
  215. }
  216. function focusEditor( editor ) {
  217. editor.editing.view.focus();
  218. editor.ui.focusTracker.isFocused = true;
  219. }
  220. } );