widgetresize.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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. let localEditorElement, localEditor;
  369. beforeEach( async () => {
  370. localEditorElement = createEditorElement();
  371. localEditor = await ClassicEditor.create( localEditorElement, {
  372. plugins: [
  373. WidgetResize, simpleWidgetPlugin
  374. ]
  375. } );
  376. } );
  377. afterEach( () => {
  378. localEditorElement.remove();
  379. return localEditor.destroy();
  380. } );
  381. it( 'works without WidgetToolbarRepository plugin', async () => {
  382. setModelData( localEditor.model, '[<widget></widget>]' );
  383. localEditor.plugins.get( WidgetResize ).attachTo( gerResizerOptions( localEditor ) );
  384. // Nothing should be thrown.
  385. } );
  386. it( 'sets the visible resizer if associated widget is already focused', async () => {
  387. setModelData( localEditor.model, '[<widget></widget>]' );
  388. const widgetResizePlugin = localEditor.plugins.get( WidgetResize );
  389. const resizer = widgetResizePlugin.attachTo( gerResizerOptions( localEditor ) );
  390. expect( widgetResizePlugin.visibleResizer ).to.eql( resizer );
  391. } );
  392. function gerResizerOptions( editor ) {
  393. return {
  394. modelElement: editor.model.document.getRoot().getChild( 0 ),
  395. viewElement: editor.editing.view.document.getRoot().getChild( 0 ),
  396. editor,
  397. isCentered: () => false,
  398. getHandleHost( domWidgetElement ) {
  399. return domWidgetElement;
  400. },
  401. getResizeHost( domWidgetElement ) {
  402. return domWidgetElement;
  403. },
  404. onCommit: commitStub
  405. };
  406. }
  407. } );
  408. describe( 'init()', () => {
  409. it( 'adds listener to redraw resizer on visible resizer change', async () => {
  410. setModelData( editor.model, '<widget></widget><paragraph>[]</paragraph>' );
  411. widget = editor.editing.view.document.getRoot().getChild( 0 );
  412. const resizer = createResizer();
  413. const redrawSpy = sinon.spy( resizer, 'redraw' );
  414. focusEditor( editor );
  415. editor.model.change( writer => {
  416. const widgetModel = editor.model.document.getRoot().getChild( 0 );
  417. writer.setSelection( widgetModel, 'on' );
  418. } );
  419. expect( redrawSpy.callCount ).to.equal( 1 );
  420. } );
  421. } );
  422. describe( 'cancel()', () => {
  423. it( 'restores original view width', () => {
  424. const resizer = createResizer();
  425. const usedHandle = 'bottom-right';
  426. const domParts = getWidgetDomParts( editor, widget, usedHandle );
  427. const startingPosition = getHandleCenterPoint( domParts.widget, usedHandle ).moveBy( 100, 0 );
  428. const domTarget = domParts.resizeHandle;
  429. resizerMouseSimulator.down( editor, domTarget );
  430. resizerMouseSimulator.move( editor, domTarget, {
  431. pageX: startingPosition.x,
  432. pageY: startingPosition.y
  433. } );
  434. resizer.cancel();
  435. // Value should be restored to the initial value (#6060).
  436. expect( widget.getStyle( 'width' ) ).to.equal( INITIAL_WIDGET_WIDTH );
  437. } );
  438. } );
  439. describe( '_getResizerByHandle()', () => {
  440. it( 'returns properly in case of invalid handle element', () => {
  441. const randomElement = document.createElement( 'span' );
  442. const plugin = editor.plugins.get( WidgetResize );
  443. createResizer();
  444. expect( plugin._getResizerByHandle( randomElement ) ).to.be.undefined;
  445. } );
  446. } );
  447. /**
  448. * @param {Object} options
  449. * @param {String} options.usedHandle Handle that should be used for resize, e.g. 'bottom-right'.
  450. * @param {Object} options.movePointerBy How much should the pointer move during the drag compared to the initial position.
  451. * @param {String} options.expectedWidth
  452. */
  453. function generateResizeTest( options ) {
  454. return () => {
  455. options = options || {};
  456. const usedHandle = options.usedHandle;
  457. const domParts = getWidgetDomParts( editor, widget, usedHandle );
  458. const initialPointerPosition = getHandleCenterPoint( domParts.widget, usedHandle );
  459. const pointerDifference = options.movePointerBy;
  460. const finalPointerPosition = initialPointerPosition.moveBy( pointerDifference.x, pointerDifference.y );
  461. resizerMouseSimulator.dragTo( editor, domParts.resizeHandle, finalPointerPosition );
  462. expect( commitStub.callCount, 'call count' ).to.be.eql( 1 );
  463. expect( commitStub.args[ 0 ][ 0 ], 'width' ).to.equal( options.expectedWidth );
  464. sinon.assert.calledOnce( commitStub );
  465. };
  466. }
  467. function createEditor( element ) {
  468. return ClassicEditor
  469. .create( element, {
  470. plugins: [
  471. ArticlePluginSet, WidgetResize, simpleWidgetPlugin
  472. ],
  473. image: {
  474. toolbar: [ 'imageStyle:full', 'imageStyle:side' ]
  475. }
  476. } );
  477. }
  478. function simpleWidgetPlugin( editor ) {
  479. editor.model.schema.register( 'widget', {
  480. inheritAllFrom: '$block',
  481. isObject: true
  482. } );
  483. editor.conversion.for( 'downcast' )
  484. .elementToElement( {
  485. model: 'widget',
  486. view: ( modelItem, { writer } ) => {
  487. const parentDiv = writer.createContainerElement( 'div' );
  488. writer.setStyle( 'height', '50px', parentDiv );
  489. writer.setStyle( 'width', '25%', parentDiv ); // It evaluates to 100px.
  490. const subDiv = writer.createContainerElement( 'div' );
  491. writer.insert( writer.createPositionAt( subDiv, 'start' ), writer.createText( 'foo' ) );
  492. writer.addClass( 'sub-div', subDiv );
  493. writer.setStyle( 'height', '20px', subDiv );
  494. writer.setStyle( 'width', '50px', subDiv );
  495. writer.insert( writer.createPositionAt( parentDiv, 'start' ), subDiv );
  496. return toWidget( parentDiv, writer, {
  497. label: 'element label'
  498. } );
  499. }
  500. } );
  501. }
  502. function createEditorElement() {
  503. const element = document.createElement( 'div' );
  504. document.body.appendChild( element );
  505. return element;
  506. }
  507. function createResizer( resizerOptions ) {
  508. const widgetModel = editor.model.document.getRoot().getChild( 0 );
  509. const defaultOptions = {
  510. unit: 'px',
  511. modelElement: widgetModel,
  512. viewElement: widget,
  513. editor,
  514. isCentered: () => false,
  515. getHandleHost( domWidgetElement ) {
  516. return domWidgetElement;
  517. },
  518. getResizeHost( domWidgetElement ) {
  519. return domWidgetElement;
  520. },
  521. onCommit: commitStub
  522. };
  523. return editor.plugins.get( WidgetResize ).attachTo( Object.assign( defaultOptions, resizerOptions ) );
  524. }
  525. } );