8
0

inlineeditoruiview.js 5.9 KB

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