widgetresize.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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 { resizerMouseSimulator, focusEditor, getHandleCenterPoint, getWidgetDomParts } from './widgetresize/_utils/utils';
  13. describe( 'WidgetResize', () => {
  14. let editor, editorElement, widget, mouseListenerSpies, commitStub;
  15. const INITIAL_WIDGET_WIDTH = '25%';
  16. beforeEach( async () => {
  17. editorElement = createEditorElement();
  18. editor = await createEditor( editorElement );
  19. setModelData( editor.model, '[<widget></widget>]' );
  20. focusEditor( editor );
  21. widget = editor.editing.view.document.getRoot().getChild( 0 );
  22. // It's crucial to have a precisely defined editor size for this test suite.
  23. editor.editing.view.change( writer => {
  24. const viewEditableRoot = editor.editing.view.document.getRoot();
  25. writer.setAttribute( 'style', 'width: 400px; padding: 0px; overflow: hidden', viewEditableRoot );
  26. } );
  27. commitStub = sinon.stub();
  28. mouseListenerSpies = {
  29. down: sinon.spy( WidgetResize.prototype, '_mouseDownListener' ),
  30. move: sinon.spy( WidgetResize.prototype, '_mouseMoveListener' ),
  31. up: sinon.spy( WidgetResize.prototype, '_mouseUpListener' )
  32. };
  33. } );
  34. afterEach( () => {
  35. editorElement.remove();
  36. for ( const stub of Object.values( mouseListenerSpies ) ) {
  37. stub.restore();
  38. }
  39. if ( editor ) {
  40. return editor.destroy();
  41. }
  42. } );
  43. describe( 'plugin', () => {
  44. it( 'is loaded', () => {
  45. expect( editor.plugins.get( WidgetResize ) ).to.be.instanceOf( WidgetResize );
  46. } );
  47. } );
  48. describe( 'mouse listeners', () => {
  49. beforeEach( () => {
  50. createResizer();
  51. } );
  52. it( 'don\'t break when called with unexpected element', () => {
  53. const unrelatedElement = document.createElement( 'div' );
  54. editor.plugins.get( WidgetResize )._mouseDownListener( {}, {
  55. domTarget: unrelatedElement
  56. } );
  57. } );
  58. it( 'passes new width to the options.onCommit()', () => {
  59. const usedResizer = 'top-right';
  60. const domParts = getWidgetDomParts( editor, widget, usedResizer );
  61. const initialPointerPosition = getHandleCenterPoint( domParts.widget, usedResizer );
  62. const finalPointerPosition = initialPointerPosition.clone().moveBy( 20, 0 );
  63. resizerMouseSimulator.dragTo( editor, domParts.resizeHandle, finalPointerPosition );
  64. expect( commitStub.callCount ).to.equal( 1 );
  65. sinon.assert.calledWithExactly( commitStub, '120px' );
  66. } );
  67. it( 'are detached when plugin is destroyed', async () => {
  68. await editor.destroy();
  69. const plugin = editor.plugins.get( WidgetResize );
  70. editor = null;
  71. const event = new Event( 'mousedown', { bubbles: true } );
  72. document.body.dispatchEvent( event );
  73. // Ensure nothing got called.
  74. expect( plugin._mouseDownListener.callCount ).to.equal( 0 );
  75. } );
  76. it( 'nothing bad happens if activeResizer got unset', () => {
  77. createResizer( {
  78. isCentered: () => true
  79. } );
  80. const usedResizer = 'top-right';
  81. const domParts = getWidgetDomParts( editor, widget, usedResizer );
  82. const initialPointerPosition = getHandleCenterPoint( domParts.widget, usedResizer );
  83. editor.plugins.get( WidgetResize )._getResizerByHandle = sinon.stub().returns( null );
  84. resizerMouseSimulator.dragTo( editor, domParts.resizeHandle, initialPointerPosition );
  85. // No exception should be thrown.
  86. } );
  87. it( 'stops the event after starting resizing', () => {
  88. const stopSpy = sinon.spy().named( 'stop' );
  89. const domParts = getWidgetDomParts( editor, widget, 'top-right' );
  90. resizerMouseSimulator.down( editor, domParts.resizeHandle, stopSpy );
  91. expect( stopSpy.called ).to.be.equal( true );
  92. } );
  93. } );
  94. describe( 'visibility', () => {
  95. beforeEach( () => {
  96. createResizer();
  97. } );
  98. it( 'it\'s hidden when no widget is focused', () => {
  99. // This particular test needs a paragraph, so that widget is no longer focused.
  100. setModelData( editor.model, '<widget></widget><paragraph>[]</paragraph>' );
  101. const allResizers = editor.ui.getEditableElement().querySelectorAll( '.ck-widget__resizer__handle' );
  102. for ( const resizer of allResizers ) {
  103. expect( isVisible( resizer ) ).to.be.false;
  104. }
  105. } );
  106. it( 'it\'s visible once the widget is focused', () => {
  107. // Widget is focused by default.
  108. const allResizers = editor.ui.getEditableElement().querySelectorAll( '.ck-widget__resizer__handle' );
  109. for ( const resizer of allResizers ) {
  110. expect( isVisible( resizer ) ).to.be.true;
  111. }
  112. } );
  113. function isVisible( element ) {
  114. // Checks if the DOM element is visible to the end user.
  115. return element.offsetParent !== null && !element.classList.contains( 'ck-hidden' );
  116. }
  117. } );
  118. describe( 'integration (pixels)', () => {
  119. describe( 'aligned widget', () => {
  120. beforeEach( () => {
  121. createResizer();
  122. } );
  123. it( 'properly sets the state for subsequent resizes', () => {
  124. const usedResizer = 'top-right';
  125. const domParts = getWidgetDomParts( editor, widget, usedResizer );
  126. const initialPointerPosition = getHandleCenterPoint( domParts.widget, usedResizer );
  127. const intermediatePointerPosition = initialPointerPosition.clone().moveBy( 50, 0 );
  128. resizerMouseSimulator.dragTo( editor, domParts.resizeHandle, intermediatePointerPosition );
  129. sinon.assert.calledWithExactly( commitStub.firstCall, '150px' );
  130. const finalPointerPosition = intermediatePointerPosition.clone().moveBy( 50, 0 );
  131. resizerMouseSimulator.dragTo( editor, domParts.resizeHandle, finalPointerPosition );
  132. sinon.assert.calledWithExactly( commitStub.secondCall, '200px' );
  133. expect( commitStub.callCount ).to.equal( 2 );
  134. } );
  135. it( 'shrinks correctly with left-bottom handler', generateResizeTest( {
  136. usedHandle: 'bottom-left',
  137. movePointerBy: { x: 20, y: -10 },
  138. expectedWidth: '80px'
  139. } ) );
  140. it( 'shrinks correctly with right-bottom handler', generateResizeTest( {
  141. usedHandle: 'bottom-right',
  142. movePointerBy: { x: -20, y: -10 },
  143. expectedWidth: '80px'
  144. } ) );
  145. it( 'shrinks correctly with left-top handler', generateResizeTest( {
  146. usedHandle: 'top-left',
  147. movePointerBy: { x: 20, y: 10 },
  148. expectedWidth: '80px'
  149. } ) );
  150. it( 'shrinks correctly with right-top handler', generateResizeTest( {
  151. usedHandle: 'top-right',
  152. movePointerBy: { x: -20, y: 10 },
  153. expectedWidth: '80px'
  154. } ) );
  155. it( 'enlarges correctly with left-bottom handler', generateResizeTest( {
  156. usedHandle: 'bottom-left',
  157. movePointerBy: { x: -10, y: 10 },
  158. expectedWidth: '120px'
  159. } ) );
  160. it( 'enlarges correctly with right-bottom handler', generateResizeTest( {
  161. usedHandle: 'bottom-right',
  162. movePointerBy: { x: 10, y: 10 },
  163. expectedWidth: '120px'
  164. } ) );
  165. it( 'enlarges correctly with right-bottom handler, y axis only', generateResizeTest( {
  166. usedHandle: 'bottom-right',
  167. movePointerBy: { x: 0, y: 20 },
  168. expectedWidth: '140px'
  169. } ) );
  170. it( 'enlarges correctly with right-bottom handler, x axis only', generateResizeTest( {
  171. usedHandle: 'bottom-right',
  172. movePointerBy: { x: 40, y: 0 },
  173. expectedWidth: '140px'
  174. } ) );
  175. it( 'enlarges correctly with left-top handler', generateResizeTest( {
  176. usedHandle: 'top-left',
  177. movePointerBy: { x: -20, y: -10 },
  178. expectedWidth: '120px'
  179. } ) );
  180. it( 'enlarges correctly with right-top handler', generateResizeTest( {
  181. usedHandle: 'top-right',
  182. movePointerBy: { x: 20, y: 10 },
  183. expectedWidth: '120px'
  184. } ) );
  185. } );
  186. describe( 'centered widget', () => {
  187. beforeEach( () => {
  188. createResizer( {
  189. isCentered: () => true
  190. } );
  191. } );
  192. it( 'shrinks correctly with left-bottom handler', generateResizeTest( {
  193. usedHandle: 'bottom-left',
  194. movePointerBy: { x: 10, y: -10 },
  195. expectedWidth: '80px'
  196. } ) );
  197. it( 'shrinks correctly with right-bottom handler', generateResizeTest( {
  198. usedHandle: 'bottom-right',
  199. movePointerBy: { x: -10, y: -10 },
  200. expectedWidth: '80px'
  201. } ) );
  202. it( 'enlarges correctly with right-bottom handler, x axis only', generateResizeTest( {
  203. usedHandle: 'bottom-right',
  204. movePointerBy: { x: 10, y: 0 },
  205. expectedWidth: '120px'
  206. } ) );
  207. it( 'enlarges correctly with right-bottom handler, y axis only', generateResizeTest( {
  208. usedHandle: 'bottom-right',
  209. movePointerBy: { x: 0, y: 10 },
  210. expectedWidth: '120px'
  211. } ) );
  212. it( 'enlarges correctly with left-bottom handler, x axis only', generateResizeTest( {
  213. usedHandle: 'bottom-left',
  214. movePointerBy: { x: -10, y: 0 },
  215. expectedWidth: '120px'
  216. } ) );
  217. it( 'enlarges correctly with left-bottom handler, y axis only', generateResizeTest( {
  218. usedHandle: 'bottom-left',
  219. movePointerBy: { x: 0, y: 10 },
  220. expectedWidth: '120px'
  221. } ) );
  222. // --- top handlers ---
  223. it( 'enlarges correctly with left-top handler', generateResizeTest( {
  224. usedHandle: 'top-left',
  225. movePointerBy: { x: -10, y: -10 },
  226. expectedWidth: '120px'
  227. } ) );
  228. it( 'enlarges correctly with left-top handler, y axis only', generateResizeTest( {
  229. usedHandle: 'top-left',
  230. movePointerBy: { x: 0, y: -10 },
  231. expectedWidth: '120px'
  232. } ) );
  233. it( 'enlarges correctly with right-top handler', generateResizeTest( {
  234. usedHandle: 'top-right',
  235. movePointerBy: { x: 10, y: -10 },
  236. expectedWidth: '120px'
  237. } ) );
  238. it( 'enlarges correctly with right-top handler, y axis only', generateResizeTest( {
  239. usedHandle: 'top-right',
  240. movePointerBy: { x: 0, y: -10 },
  241. expectedWidth: '120px'
  242. } ) );
  243. } );
  244. } );
  245. describe( 'integration (percents)', () => {
  246. describe( 'side aligned widget', () => {
  247. beforeEach( () => {
  248. createResizer( {
  249. unit: undefined
  250. } );
  251. } );
  252. it( 'properly sets the state for subsequent resizes', () => {
  253. const usedResizer = 'top-right';
  254. const domParts = getWidgetDomParts( editor, widget, usedResizer );
  255. const initialPointerPosition = getHandleCenterPoint( domParts.widget, usedResizer );
  256. const intermediatePointerPosition = initialPointerPosition.clone().moveBy( 100, 0 );
  257. resizerMouseSimulator.dragTo( editor, domParts.resizeHandle, intermediatePointerPosition );
  258. sinon.assert.calledWithExactly( commitStub.firstCall, '50%' );
  259. const finalPointerPosition = intermediatePointerPosition.clone().moveBy( 100, 0 );
  260. resizerMouseSimulator.dragTo( editor, domParts.resizeHandle, finalPointerPosition );
  261. sinon.assert.calledWithExactly( commitStub.secondCall, '75%' );
  262. expect( commitStub.callCount ).to.equal( 2 );
  263. } );
  264. it( 'shrinks correctly with bottom-left handler', generateResizeTest( {
  265. usedHandle: 'bottom-left',
  266. movePointerBy: { x: 10, y: -10 },
  267. expectedWidth: '22.5%'
  268. } ) );
  269. } );
  270. describe( 'centered widget', () => {
  271. beforeEach( () => {
  272. createResizer( {
  273. isCentered: () => true,
  274. unit: undefined
  275. } );
  276. } );
  277. it( 'shrinks correctly with bottom-left handler', generateResizeTest( {
  278. usedHandle: 'bottom-left',
  279. movePointerBy: { x: 10, y: -10 },
  280. expectedWidth: '20%'
  281. } ) );
  282. it( 'enlarges correctly with bottom-right handler', generateResizeTest( {
  283. usedHandle: 'bottom-right',
  284. movePointerBy: { x: 0, y: 5 },
  285. expectedWidth: '27.5%'
  286. } ) );
  287. it( 'enlarges correctly an image with unsupported width unit', () => {
  288. editor.editing.view.change( writer => {
  289. writer.setStyle( 'width', '100pt', widget );
  290. } );
  291. generateResizeTest( {
  292. usedHandle: 'bottom-right',
  293. movePointerBy: { x: 0, y: 5 },
  294. expectedWidth: '36.67%'
  295. } )();
  296. } );
  297. } );
  298. it( 'doesn\'t call options.onCommit() in case of no change', () => {
  299. const commitStub = sinon.stub();
  300. createResizer( {
  301. onCommit: commitStub
  302. } );
  303. const domParts = getWidgetDomParts( editor, widget, 'top-left' );
  304. resizerMouseSimulator.down( editor, domParts.resizeHandle );
  305. resizerMouseSimulator.up( editor );
  306. expect( commitStub.callCount, 'call count' ).to.be.eql( 0 );
  307. } );
  308. // Note that ultimately width should be changed, but through a model converter, not with direct view changes (#6060).
  309. it( 'restores the original view width after resize is done', () => {
  310. createResizer();
  311. const usedHandle = 'bottom-right';
  312. const domParts = getWidgetDomParts( editor, widget, usedHandle );
  313. const finalPointerPosition = getHandleCenterPoint( domParts.widget, usedHandle ).moveBy( 40, 40 );
  314. resizerMouseSimulator.dragTo( editor, domParts.resizeHandle, finalPointerPosition );
  315. expect( widget.getStyle( 'width' ) ).to.equal( INITIAL_WIDGET_WIDTH );
  316. } );
  317. // Verify render count https://github.com/ckeditor/ckeditor5-widget/pull/122#issuecomment-617012777.
  318. it( 'renders the final result in one step', () => {
  319. const renderListener = sinon.stub();
  320. const resizer = createResizer();
  321. const usedHandle = 'bottom-right';
  322. const domParts = getWidgetDomParts( editor, widget, usedHandle );
  323. const finalPointerPosition = getHandleCenterPoint( domParts.widget, usedHandle ).moveBy( 40, 40 );
  324. // Start listening on render at the last stage, to exclude rendering caused by moving the mouse.
  325. // Commit is triggered by mouse up and that's what interests us.
  326. resizer.on( 'commit', () => {
  327. editor.editing.view.on( 'render', renderListener );
  328. }, { priority: 'highest' } );
  329. resizerMouseSimulator.dragTo( editor, domParts.resizeHandle, finalPointerPosition );
  330. expect( renderListener.callCount ).to.equal( 1 );
  331. } );
  332. it( 'returns proper value when resize host is different from widget wrapper', () => {
  333. createResizer( {
  334. unit: undefined,
  335. getHandleHost( domWidgetElement ) {
  336. return domWidgetElement.querySelector( '.sub-div' );
  337. }
  338. } );
  339. const usedHandle = 'bottom-right';
  340. const pointerDifference = { x: -10, y: -10 };
  341. const expectedWidth = '20%';
  342. const domParts = getWidgetDomParts( editor, widget, usedHandle );
  343. const resizeHost = domParts.widget.querySelector( '.sub-div' );
  344. const initialPointerPosition = getHandleCenterPoint( resizeHost, usedHandle );
  345. const finalPointerPosition = initialPointerPosition.moveBy( pointerDifference.x, pointerDifference.y );
  346. resizerMouseSimulator.dragTo( editor, domParts.resizeHandle, finalPointerPosition );
  347. expect( commitStub.callCount, 'call count' ).to.be.eql( 1 );
  348. expect( commitStub.args[ 0 ][ 0 ], 'width' ).to.equal( expectedWidth );
  349. sinon.assert.calledOnce( commitStub );
  350. } );
  351. it( 'doesn\'t break if the widget wrapper was removed from DOM', () => {
  352. const resizer = createResizer( {
  353. unit: undefined
  354. } );
  355. const domParts = getWidgetDomParts( editor, widget, 'bottom-right' );
  356. domParts.resizeWrapper.remove();
  357. resizer.redraw();
  358. } );
  359. } );
  360. describe( 'attachTo()', () => {
  361. it( 'works without WidgetToolbarRepository plugin', async () => {
  362. const localEditorElement = createEditorElement();
  363. const localEditor = await ClassicEditor.create( localEditorElement, {
  364. plugins: [
  365. WidgetResize, simpleWidgetPlugin
  366. ]
  367. } );
  368. setModelData( localEditor.model, '[<widget></widget>]' );
  369. const resizerOptions = {
  370. modelElement: localEditor.model.document.getRoot().getChild( 0 ),
  371. viewElement: localEditor.editing.view.document.getRoot().getChild( 0 ),
  372. editor: localEditor,
  373. isCentered: () => false,
  374. getHandleHost( domWidgetElement ) {
  375. return domWidgetElement;
  376. },
  377. getResizeHost( domWidgetElement ) {
  378. return domWidgetElement;
  379. },
  380. onCommit: commitStub
  381. };
  382. localEditor.plugins.get( WidgetResize ).attachTo( resizerOptions );
  383. // Nothing should be thrown.
  384. // And clean up.
  385. localEditorElement.remove();
  386. return localEditor.destroy();
  387. } );
  388. } );
  389. describe( 'init()', () => {
  390. it( 'adds listener to redraw resizer on visible resizer change', async () => {
  391. setModelData( editor.model, '<widget></widget><paragraph>[]</paragraph>' );
  392. widget = editor.editing.view.document.getRoot().getChild( 0 );
  393. const resizer = createResizer();
  394. const redrawSpy = sinon.spy( resizer, 'redraw' );
  395. focusEditor( editor );
  396. editor.model.change( writer => {
  397. const widgetModel = editor.model.document.getRoot().getChild( 0 );
  398. writer.setSelection( widgetModel, 'on' );
  399. } );
  400. expect( redrawSpy.callCount ).to.equal( 1 );
  401. } );
  402. } );
  403. describe( 'cancel()', () => {
  404. it( 'restores original view width', () => {
  405. const resizer = createResizer();
  406. const usedHandle = 'bottom-right';
  407. const domParts = getWidgetDomParts( editor, widget, usedHandle );
  408. const startingPosition = getHandleCenterPoint( domParts.widget, usedHandle ).moveBy( 100, 0 );
  409. const domTarget = domParts.resizeHandle;
  410. resizerMouseSimulator.down( editor, domTarget );
  411. resizerMouseSimulator.move( editor, domTarget, {
  412. pageX: startingPosition.x,
  413. pageY: startingPosition.y
  414. } );
  415. resizer.cancel();
  416. // Value should be restored to the initial value (#6060).
  417. expect( widget.getStyle( 'width' ) ).to.equal( INITIAL_WIDGET_WIDTH );
  418. } );
  419. } );
  420. describe( '_getResizerByHandle()', () => {
  421. it( 'returns properly in case of invalid handle element', () => {
  422. const randomElement = document.createElement( 'span' );
  423. const plugin = editor.plugins.get( WidgetResize );
  424. createResizer();
  425. expect( plugin._getResizerByHandle( randomElement ) ).to.be.undefined;
  426. } );
  427. } );
  428. /**
  429. * @param {Object} options
  430. * @param {String} options.usedHandle Handle that should be used for resize, e.g. 'bottom-right'.
  431. * @param {Object} options.movePointerBy How much should the pointer move during the drag compared to the initial position.
  432. * @param {String} options.expectedWidth
  433. */
  434. function generateResizeTest( options ) {
  435. return () => {
  436. options = options || {};
  437. const usedHandle = options.usedHandle;
  438. const domParts = getWidgetDomParts( editor, widget, usedHandle );
  439. const initialPointerPosition = getHandleCenterPoint( domParts.widget, usedHandle );
  440. const pointerDifference = options.movePointerBy;
  441. const finalPointerPosition = initialPointerPosition.moveBy( pointerDifference.x, pointerDifference.y );
  442. resizerMouseSimulator.dragTo( editor, domParts.resizeHandle, finalPointerPosition );
  443. expect( commitStub.callCount, 'call count' ).to.be.eql( 1 );
  444. expect( commitStub.args[ 0 ][ 0 ], 'width' ).to.equal( options.expectedWidth );
  445. sinon.assert.calledOnce( commitStub );
  446. };
  447. }
  448. function createEditor( element ) {
  449. return ClassicEditor
  450. .create( element, {
  451. plugins: [
  452. ArticlePluginSet, WidgetResize, simpleWidgetPlugin
  453. ],
  454. image: {
  455. toolbar: [ 'imageStyle:full', 'imageStyle:side' ]
  456. }
  457. } );
  458. }
  459. function simpleWidgetPlugin( editor ) {
  460. editor.model.schema.register( 'widget', {
  461. inheritAllFrom: '$block',
  462. isObject: true
  463. } );
  464. editor.conversion.for( 'downcast' )
  465. .elementToElement( {
  466. model: 'widget',
  467. view: ( modelItem, viewWriter ) => {
  468. const parentDiv = viewWriter.createContainerElement( 'div' );
  469. viewWriter.setStyle( 'height', '50px', parentDiv );
  470. viewWriter.setStyle( 'width', '25%', parentDiv ); // It evaluates to 100px.
  471. const subDiv = viewWriter.createContainerElement( 'div' );
  472. viewWriter.insert( viewWriter.createPositionAt( subDiv, 'start' ), viewWriter.createText( 'foo' ) );
  473. viewWriter.addClass( 'sub-div', subDiv );
  474. viewWriter.setStyle( 'height', '20px', subDiv );
  475. viewWriter.setStyle( 'width', '50px', subDiv );
  476. viewWriter.insert( viewWriter.createPositionAt( parentDiv, 'start' ), subDiv );
  477. return toWidget( parentDiv, viewWriter, {
  478. label: 'element label'
  479. } );
  480. }
  481. } );
  482. }
  483. function createEditorElement() {
  484. const element = document.createElement( 'div' );
  485. document.body.appendChild( element );
  486. return element;
  487. }
  488. function createResizer( resizerOptions ) {
  489. const widgetModel = editor.model.document.getRoot().getChild( 0 );
  490. const defaultOptions = {
  491. unit: 'px',
  492. modelElement: widgetModel,
  493. viewElement: widget,
  494. editor,
  495. isCentered: () => false,
  496. getHandleHost( domWidgetElement ) {
  497. return domWidgetElement;
  498. },
  499. getResizeHost( domWidgetElement ) {
  500. return domWidgetElement;
  501. },
  502. onCommit: commitStub
  503. };
  504. return editor.plugins.get( WidgetResize ).attachTo( Object.assign( defaultOptions, resizerOptions ) );
  505. }
  506. } );