8
0

inlineeditoruiview.js 7.2 KB

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