8
0

inlineeditoruiview.js 7.6 KB

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