8
0

inlineeditoruiview.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 InlineEditorUIView from '../src/inlineeditoruiview';
  6. import EditingView from '@ckeditor/ckeditor5-engine/src/view/view';
  7. import ToolbarView from '@ckeditor/ckeditor5-ui/src/toolbar/toolbarview';
  8. import BalloonPanelView from '@ckeditor/ckeditor5-ui/src/panel/balloon/balloonpanelview';
  9. import InlineEditableUIView from '@ckeditor/ckeditor5-ui/src/editableui/inline/inlineeditableuiview';
  10. import Locale from '@ckeditor/ckeditor5-utils/src/locale';
  11. import createRoot from '@ckeditor/ckeditor5-engine/tests/view/_utils/createroot.js';
  12. import Rect from '@ckeditor/ckeditor5-utils/src/dom/rect';
  13. import toUnit from '@ckeditor/ckeditor5-utils/src/dom/tounit';
  14. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  15. const toPx = toUnit( 'px' );
  16. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  17. /* globals setTimeout */
  18. describe( 'InlineEditorUIView', () => {
  19. let locale, view, editingView, editingViewRoot;
  20. testUtils.createSinonSandbox();
  21. beforeEach( () => {
  22. locale = new Locale();
  23. editingView = new EditingView();
  24. editingViewRoot = createRoot( editingView.document );
  25. view = new InlineEditorUIView( locale, editingView );
  26. view.editable.name = editingViewRoot.rootName;
  27. } );
  28. describe( 'constructor()', () => {
  29. describe( '#toolbar', () => {
  30. it( 'is created', () => {
  31. expect( view.toolbar ).to.be.instanceof( ToolbarView );
  32. } );
  33. it( 'is given a locale object', () => {
  34. expect( view.toolbar.locale ).to.equal( locale );
  35. } );
  36. it( 'is not rendered', () => {
  37. expect( view.toolbar.isRendered ).to.be.false;
  38. } );
  39. } );
  40. describe( '#panel', () => {
  41. it( 'is created', () => {
  42. expect( view.panel ).to.be.instanceof( BalloonPanelView );
  43. } );
  44. it( 'is given a locale object', () => {
  45. expect( view.panel.locale ).to.equal( locale );
  46. } );
  47. it( 'gets view.panel#withArrow set', () => {
  48. expect( view.panel.withArrow ).to.be.false;
  49. } );
  50. it( 'is not rendered', () => {
  51. expect( view.panel.isRendered ).to.be.false;
  52. } );
  53. } );
  54. describe( '#editable', () => {
  55. it( 'is created', () => {
  56. expect( view.editable ).to.be.instanceof( InlineEditableUIView );
  57. } );
  58. it( 'is given a locale object', () => {
  59. expect( view.editable.locale ).to.equal( locale );
  60. } );
  61. it( 'is not rendered', () => {
  62. expect( view.editable.isRendered ).to.be.false;
  63. } );
  64. } );
  65. } );
  66. describe( 'render()', () => {
  67. beforeEach( () => {
  68. // We need to set this option explicitly before render phase,
  69. // otherwise it is `undefined` and toolbar items grouping will be skipped in tests.
  70. view.toolbar.options.shouldGroupWhenFull = true;
  71. view.render();
  72. } );
  73. afterEach( () => {
  74. view.destroy();
  75. } );
  76. describe( '#toolbar', () => {
  77. it( 'is given the right CSS classes', () => {
  78. expect( view.toolbar.element.classList.contains( 'ck-toolbar_floating' ) ).to.be.true;
  79. } );
  80. it( 'sets the default value of the #viewportTopOffset attribute', () => {
  81. expect( view.viewportTopOffset ).to.equal( 0 );
  82. } );
  83. // Sometimes this test can fail, due to the fact that it have to wait for async ResizeObserver execution.
  84. it( 'max-width should be set to the width of the editable element', done => {
  85. const viewElement = view.editable.element;
  86. global.document.body.appendChild( viewElement );
  87. expect( global.document.body.contains( viewElement ) ).to.be.true;
  88. viewElement.style.width = '400px';
  89. // Unfortunately we have to wait for async ResizeObserver execution.
  90. // ResizeObserver which has been called after changing width of viewElement,
  91. // needs 2x requestAnimationFrame or timeout to update a layout.
  92. // See more: https://twitter.com/paul_irish/status/912693347315150849/photo/1
  93. setTimeout( () => {
  94. const editableWidthWithPadding = toPx( new Rect( viewElement ).width );
  95. expect( view.toolbar.maxWidth ).to.be.equal( editableWidthWithPadding );
  96. global.document.body.removeChild( viewElement );
  97. done();
  98. }, 500 );
  99. } );
  100. } );
  101. describe( '#panel', () => {
  102. it( 'is given the right CSS class', () => {
  103. expect( view.panel.element.classList.contains( 'ck-toolbar-container' ) ).to.be.true;
  104. } );
  105. it( 'is put into the #body collection', () => {
  106. expect( view.body.get( 0 ) ).to.equal( view.panel );
  107. } );
  108. } );
  109. describe( '#editable', () => {
  110. it( 'is registered as a child', () => {
  111. const spy = sinon.spy( view.editable, 'destroy' );
  112. view.destroy();
  113. sinon.assert.calledOnce( spy );
  114. } );
  115. } );
  116. } );
  117. describe( 'init()', () => {
  118. it( 'appends #toolbar to panel#content', () => {
  119. locale = new Locale();
  120. const view = new InlineEditorUIView( locale, editingView );
  121. view.editable.name = editingViewRoot.rootName;
  122. expect( view.panel.content ).to.have.length( 0 );
  123. view.render();
  124. expect( view.panel.content.get( 0 ) ).to.equal( view.toolbar );
  125. view.destroy();
  126. } );
  127. } );
  128. describe( 'panelPositions', () => {
  129. it( 'returns the positions in the right order (uiLanguageDirection="ltr")', () => {
  130. locale.uiLanguageDirection = 'ltr';
  131. const uiView = new InlineEditorUIView( locale, editingView );
  132. const positions = uiView.panelPositions;
  133. const editableRect = {
  134. top: 100,
  135. bottom: 200,
  136. left: 100,
  137. right: 100,
  138. width: 100,
  139. height: 100
  140. };
  141. const panelRect = {
  142. width: 50,
  143. height: 50
  144. };
  145. expect( positions ).to.have.length( 2 );
  146. expect( positions[ 0 ]( editableRect, panelRect ).name ).to.equal( 'toolbar_west' );
  147. expect( positions[ 1 ]( editableRect, panelRect ).name ).to.equal( 'toolbar_east' );
  148. } );
  149. it( 'returns the positions in the right order (uiLanguageDirection="rtl")', () => {
  150. locale.uiLanguageDirection = 'rtl';
  151. const uiView = new InlineEditorUIView( locale, editingView );
  152. const positions = uiView.panelPositions;
  153. const editableRect = {
  154. top: 100,
  155. bottom: 200,
  156. left: 100,
  157. right: 100,
  158. width: 100,
  159. height: 100
  160. };
  161. const panelRect = {
  162. width: 50,
  163. height: 50
  164. };
  165. expect( positions ).to.have.length( 2 );
  166. expect( positions[ 0 ]( editableRect, panelRect ).name ).to.equal( 'toolbar_east' );
  167. expect( positions[ 1 ]( editableRect, panelRect ).name ).to.equal( 'toolbar_west' );
  168. } );
  169. describe( 'west', () => {
  170. testTopPositions( 0, 100 );
  171. } );
  172. describe( 'east', () => {
  173. testTopPositions( 1, 150 );
  174. } );
  175. function testTopPositions( positionIndex, expectedLeft ) {
  176. it( 'positions the panel above editable when there\'s enough space', () => {
  177. const position = view.panelPositions[ positionIndex ];
  178. const editableRect = {
  179. top: 101, // !
  180. bottom: 200,
  181. left: 100,
  182. right: 100,
  183. width: 100,
  184. height: 100
  185. };
  186. const panelRect = {
  187. width: 50,
  188. height: 100 // !
  189. };
  190. const { top, left } = position( editableRect, panelRect );
  191. expect( top ).to.equal( 1 );
  192. expect( left ).to.equal( expectedLeft );
  193. } );
  194. it( 'positions the panel over the editable when there\'s not enough space above (1)', () => {
  195. const position = view.panelPositions[ positionIndex ];
  196. const editableRect = {
  197. top: 100, // !
  198. bottom: 300,
  199. left: 100,
  200. right: 100,
  201. width: 100,
  202. height: 200
  203. };
  204. const panelRect = {
  205. width: 50,
  206. height: 100 // !
  207. };
  208. const { top, left } = position( editableRect, panelRect );
  209. expect( top ).to.equal( 0 );
  210. expect( left ).to.equal( expectedLeft );
  211. } );
  212. it( 'positions the panel over the editable when there\'s not enough space above (2)', () => {
  213. const position = view.panelPositions[ positionIndex ];
  214. const editableRect = {
  215. top: 99, // !
  216. bottom: 399,
  217. left: 100,
  218. right: 100,
  219. width: 100,
  220. height: 200
  221. };
  222. const panelRect = {
  223. width: 50,
  224. height: 100 // !
  225. };
  226. const { top, left } = position( editableRect, panelRect );
  227. expect( top ).to.equal( 0 );
  228. expect( left ).to.equal( expectedLeft );
  229. } );
  230. it( 'positions the panel over the editable when there\'s not enough space above (3)', () => {
  231. const position = view.panelPositions[ positionIndex ];
  232. const editableRect = {
  233. top: 51, // !
  234. bottom: 399,
  235. left: 100,
  236. right: 100,
  237. width: 100,
  238. height: 200
  239. };
  240. const panelRect = {
  241. width: 50,
  242. height: 100 // !
  243. };
  244. const { top, left } = position( editableRect, panelRect );
  245. expect( top ).to.equal( 0 );
  246. expect( left ).to.equal( expectedLeft );
  247. } );
  248. it( 'positions the panel below the editable when there\'s not enough space above/over', () => {
  249. const position = view.panelPositions[ positionIndex ];
  250. const editableRect = {
  251. top: 50,
  252. bottom: 150, // !
  253. left: 100,
  254. right: 100,
  255. width: 100,
  256. height: 100
  257. };
  258. const panelRect = {
  259. width: 50,
  260. height: 100 // !
  261. };
  262. const { top, left } = position( editableRect, panelRect );
  263. expect( top ).to.equal( 150 );
  264. expect( left ).to.equal( expectedLeft );
  265. } );
  266. describe( 'view#viewportTopOffset', () => {
  267. it( 'sticks the panel to the offset when there\'s not enough space above', () => {
  268. view.viewportTopOffset = 50;
  269. const position = view.panelPositions[ positionIndex ];
  270. const editableRect = {
  271. top: 0, // !
  272. bottom: 200,
  273. left: 100,
  274. right: 100,
  275. width: 100,
  276. height: 200
  277. };
  278. const panelRect = {
  279. width: 50,
  280. height: 50
  281. };
  282. const { top, left } = position( editableRect, panelRect );
  283. expect( top ).to.equal( 50 );
  284. expect( left ).to.equal( expectedLeft );
  285. } );
  286. it( 'positions the panel below the editable when there\'s not enough space above/over', () => {
  287. view.viewportTopOffset = 50;
  288. const position = view.panelPositions[ positionIndex ];
  289. const editableRect = {
  290. top: 100,
  291. bottom: 150,
  292. left: 100,
  293. right: 100,
  294. width: 100,
  295. height: 50
  296. };
  297. const panelRect = {
  298. width: 50,
  299. height: 80
  300. };
  301. const { top, left } = position( editableRect, panelRect );
  302. expect( top ).to.equal( 150 );
  303. expect( left ).to.equal( expectedLeft );
  304. } );
  305. } );
  306. }
  307. } );
  308. } );