8
0

inlineeditoruiview.js 7.3 KB

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