inlineeditoruiview.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import InlineEditorUIView from '../src/inlineeditoruiview';
  6. import ToolbarView from '@ckeditor/ckeditor5-ui/src/toolbar/toolbarview';
  7. import BalloonPanelView from '@ckeditor/ckeditor5-ui/src/panel/balloon/balloonpanelview';
  8. import InlineEditableUIView from '@ckeditor/ckeditor5-ui/src/editableui/inline/inlineeditableuiview';
  9. import Locale from '@ckeditor/ckeditor5-utils/src/locale';
  10. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  11. describe( 'InlineEditorUIView', () => {
  12. let locale, view;
  13. testUtils.createSinonSandbox();
  14. beforeEach( () => {
  15. locale = new Locale( 'en' );
  16. view = new InlineEditorUIView( locale );
  17. } );
  18. describe( 'constructor()', () => {
  19. describe( '#toolbar', () => {
  20. it( 'is created', () => {
  21. expect( view.toolbar ).to.be.instanceof( ToolbarView );
  22. } );
  23. it( 'is given a locale object', () => {
  24. expect( view.toolbar.locale ).to.equal( locale );
  25. } );
  26. it( 'is not rendered', () => {
  27. expect( view.toolbar.isRendered ).to.be.false;
  28. } );
  29. } );
  30. describe( '#panel', () => {
  31. it( 'is created', () => {
  32. expect( view.panel ).to.be.instanceof( BalloonPanelView );
  33. } );
  34. it( 'is given a locale object', () => {
  35. expect( view.panel.locale ).to.equal( locale );
  36. } );
  37. it( 'gets view.panel#withArrow set', () => {
  38. expect( view.panel.withArrow ).to.be.false;
  39. } );
  40. it( 'is not rendered', () => {
  41. expect( view.panel.isRendered ).to.be.false;
  42. } );
  43. } );
  44. describe( '#editable', () => {
  45. it( 'is created', () => {
  46. expect( view.editable ).to.be.instanceof( InlineEditableUIView );
  47. } );
  48. it( 'is given a locale object', () => {
  49. expect( view.editable.locale ).to.equal( locale );
  50. } );
  51. it( 'is not rendered', () => {
  52. expect( view.editable.isRendered ).to.be.false;
  53. } );
  54. } );
  55. } );
  56. describe( 'render()', () => {
  57. beforeEach( () => {
  58. view.render();
  59. } );
  60. afterEach( () => {
  61. view.destroy();
  62. } );
  63. describe( '#toolbar', () => {
  64. it( 'is given the right CSS classes', () => {
  65. expect( view.toolbar.element.classList.contains( 'ck-toolbar_floating' ) ).to.be.true;
  66. } );
  67. it( 'sets the default value of the #viewportTopOffset attribute', () => {
  68. expect( view.viewportTopOffset ).to.equal( 0 );
  69. } );
  70. } );
  71. describe( '#panel', () => {
  72. it( 'is given the right CSS class', () => {
  73. expect( view.panel.element.classList.contains( 'ck-toolbar-container' ) ).to.be.true;
  74. } );
  75. it( 'is put into the #body collection', () => {
  76. expect( view.body.get( 0 ) ).to.equal( view.panel );
  77. } );
  78. } );
  79. describe( '#editable', () => {
  80. it( 'is registered as a child', () => {
  81. const spy = sinon.spy( view.editable, 'destroy' );
  82. view.destroy();
  83. sinon.assert.calledOnce( spy );
  84. } );
  85. } );
  86. } );
  87. describe( 'init()', () => {
  88. it( 'appends #toolbar to panel#content', () => {
  89. const view = new InlineEditorUIView( locale );
  90. expect( view.panel.content ).to.have.length( 0 );
  91. view.render();
  92. expect( view.panel.content.get( 0 ) ).to.equal( view.toolbar );
  93. view.destroy();
  94. } );
  95. } );
  96. describe( 'panelPositions', () => {
  97. it( 'returns the positions in the right order', () => {
  98. const positions = view.panelPositions;
  99. const editableRect = {
  100. top: 100,
  101. bottom: 200,
  102. left: 100,
  103. right: 100,
  104. width: 100,
  105. height: 100
  106. };
  107. const panelRect = {
  108. width: 50,
  109. height: 50
  110. };
  111. expect( positions ).to.have.length( 2 );
  112. expect( positions[ 0 ]( editableRect, panelRect ).name ).to.equal( 'toolbar_west' );
  113. expect( positions[ 1 ]( editableRect, panelRect ).name ).to.equal( 'toolbar_east' );
  114. } );
  115. describe( 'west', () => {
  116. testTopPositions( 0, 100 );
  117. } );
  118. describe( 'east', () => {
  119. testTopPositions( 1, 150 );
  120. } );
  121. function testTopPositions( positionIndex, expectedLeft ) {
  122. it( 'positions the panel above editable when there\'s enough space', () => {
  123. const position = view.panelPositions[ positionIndex ];
  124. const editableRect = {
  125. top: 101, // !
  126. bottom: 200,
  127. left: 100,
  128. right: 100,
  129. width: 100,
  130. height: 100
  131. };
  132. const panelRect = {
  133. width: 50,
  134. height: 100 // !
  135. };
  136. const { top, left } = position( editableRect, panelRect );
  137. expect( top ).to.equal( 1 );
  138. expect( left ).to.equal( expectedLeft );
  139. } );
  140. it( 'positions the panel over the editable when there\'s not enough space above (1)', () => {
  141. const position = view.panelPositions[ positionIndex ];
  142. const editableRect = {
  143. top: 100, // !
  144. bottom: 300,
  145. left: 100,
  146. right: 100,
  147. width: 100,
  148. height: 200
  149. };
  150. const panelRect = {
  151. width: 50,
  152. height: 100 // !
  153. };
  154. const { top, left } = position( editableRect, panelRect );
  155. expect( top ).to.equal( 0 );
  156. expect( left ).to.equal( expectedLeft );
  157. } );
  158. it( 'positions the panel over the editable when there\'s not enough space above (2)', () => {
  159. const position = view.panelPositions[ positionIndex ];
  160. const editableRect = {
  161. top: 99, // !
  162. bottom: 399,
  163. left: 100,
  164. right: 100,
  165. width: 100,
  166. height: 200
  167. };
  168. const panelRect = {
  169. width: 50,
  170. height: 100 // !
  171. };
  172. const { top, left } = position( editableRect, panelRect );
  173. expect( top ).to.equal( 0 );
  174. expect( left ).to.equal( expectedLeft );
  175. } );
  176. it( 'positions the panel over the editable when there\'s not enough space above (3)', () => {
  177. const position = view.panelPositions[ positionIndex ];
  178. const editableRect = {
  179. top: 51, // !
  180. bottom: 399,
  181. left: 100,
  182. right: 100,
  183. width: 100,
  184. height: 200
  185. };
  186. const panelRect = {
  187. width: 50,
  188. height: 100 // !
  189. };
  190. const { top, left } = position( editableRect, panelRect );
  191. expect( top ).to.equal( 0 );
  192. expect( left ).to.equal( expectedLeft );
  193. } );
  194. it( 'positions the panel below the editable when there\'s not enough space above/over', () => {
  195. const position = view.panelPositions[ positionIndex ];
  196. const editableRect = {
  197. top: 50,
  198. bottom: 150, // !
  199. left: 100,
  200. right: 100,
  201. width: 100,
  202. height: 100
  203. };
  204. const panelRect = {
  205. width: 50,
  206. height: 100 // !
  207. };
  208. const { top, left } = position( editableRect, panelRect );
  209. expect( top ).to.equal( 150 );
  210. expect( left ).to.equal( expectedLeft );
  211. } );
  212. describe( 'view#viewportTopOffset', () => {
  213. it( 'sticks the panel to the offset when there\'s not enough space above', () => {
  214. view.viewportTopOffset = 50;
  215. const position = view.panelPositions[ positionIndex ];
  216. const editableRect = {
  217. top: 0, // !
  218. bottom: 200,
  219. left: 100,
  220. right: 100,
  221. width: 100,
  222. height: 200
  223. };
  224. const panelRect = {
  225. width: 50,
  226. height: 50
  227. };
  228. const { top, left } = position( editableRect, panelRect );
  229. expect( top ).to.equal( 50 );
  230. expect( left ).to.equal( expectedLeft );
  231. } );
  232. it( 'positions the panel below the editable when there\'s not enough space above/over', () => {
  233. view.viewportTopOffset = 50;
  234. const position = view.panelPositions[ positionIndex ];
  235. const editableRect = {
  236. top: 100,
  237. bottom: 150,
  238. left: 100,
  239. right: 100,
  240. width: 100,
  241. height: 50
  242. };
  243. const panelRect = {
  244. width: 50,
  245. height: 80
  246. };
  247. const { top, left } = position( editableRect, panelRect );
  248. expect( top ).to.equal( 150 );
  249. expect( left ).to.equal( expectedLeft );
  250. } );
  251. } );
  252. }
  253. } );
  254. } );