inlineeditoruiview.js 8.1 KB

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