widgetresize.js 21 KB

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