inlineeditoruiview.js 7.2 KB

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