inlineeditoruiview.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. return view.init()
  56. .then( () => view.destroy() )
  57. .then( () => {
  58. sinon.assert.calledOnce( spy );
  59. } );
  60. } );
  61. } );
  62. } );
  63. describe( 'init()', () => {
  64. it( 'appends #toolbar to panel#content', () => {
  65. expect( view.panel.content ).to.have.length( 0 );
  66. return view.init()
  67. .then( () => {
  68. expect( view.panel.content.get( 0 ) ).to.equal( view.toolbar );
  69. } )
  70. .then( () => view.destroy() );
  71. } );
  72. } );
  73. describe( 'editableElement', () => {
  74. it( 'returns editable\'s view element', () => {
  75. return view.init()
  76. .then( () => {
  77. expect( view.editableElement.getAttribute( 'contentEditable' ) ).to.equal( 'true' );
  78. } )
  79. .then( () => 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. }
  199. } );
  200. } );