inlineeditoruiview.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import InlineEditorUIView from '../src/inlineeditoruiview';
  6. import EditingView from '@ckeditor/ckeditor5-engine/src/view/view';
  7. import ToolbarView from '@ckeditor/ckeditor5-ui/src/toolbar/toolbarview';
  8. import BalloonPanelView from '@ckeditor/ckeditor5-ui/src/panel/balloon/balloonpanelview';
  9. import InlineEditableUIView from '@ckeditor/ckeditor5-ui/src/editableui/inline/inlineeditableuiview';
  10. import Locale from '@ckeditor/ckeditor5-utils/src/locale';
  11. import createRoot from '@ckeditor/ckeditor5-engine/tests/view/_utils/createroot.js';
  12. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  13. describe( 'InlineEditorUIView', () => {
  14. let locale, view, editingView, editingViewRoot;
  15. testUtils.createSinonSandbox();
  16. beforeEach( () => {
  17. locale = new Locale( 'en' );
  18. editingView = new EditingView();
  19. editingViewRoot = createRoot( editingView.document );
  20. view = new InlineEditorUIView( locale, editingView );
  21. view.editable.name = editingViewRoot.rootName;
  22. } );
  23. describe( 'constructor()', () => {
  24. describe( '#toolbar', () => {
  25. it( 'is created', () => {
  26. expect( view.toolbar ).to.be.instanceof( ToolbarView );
  27. } );
  28. it( 'is given a locale object', () => {
  29. expect( view.toolbar.locale ).to.equal( locale );
  30. } );
  31. it( 'is not rendered', () => {
  32. expect( view.toolbar.isRendered ).to.be.false;
  33. } );
  34. } );
  35. describe( '#panel', () => {
  36. it( 'is created', () => {
  37. expect( view.panel ).to.be.instanceof( BalloonPanelView );
  38. } );
  39. it( 'is given a locale object', () => {
  40. expect( view.panel.locale ).to.equal( locale );
  41. } );
  42. it( 'gets view.panel#withArrow set', () => {
  43. expect( view.panel.withArrow ).to.be.false;
  44. } );
  45. it( 'is not rendered', () => {
  46. expect( view.panel.isRendered ).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 locale object', () => {
  54. expect( view.editable.locale ).to.equal( locale );
  55. } );
  56. it( 'is not rendered', () => {
  57. expect( view.editable.isRendered ).to.be.false;
  58. } );
  59. } );
  60. } );
  61. describe( 'render()', () => {
  62. beforeEach( () => {
  63. view.render();
  64. } );
  65. afterEach( () => {
  66. view.destroy();
  67. } );
  68. describe( '#toolbar', () => {
  69. it( 'is given the right CSS classes', () => {
  70. expect( view.toolbar.element.classList.contains( 'ck-toolbar_floating' ) ).to.be.true;
  71. } );
  72. it( 'sets the default value of the #viewportTopOffset attribute', () => {
  73. expect( view.viewportTopOffset ).to.equal( 0 );
  74. } );
  75. } );
  76. describe( '#panel', () => {
  77. it( 'is given the right CSS class', () => {
  78. expect( view.panel.element.classList.contains( 'ck-toolbar-container' ) ).to.be.true;
  79. } );
  80. it( 'is put into the #body collection', () => {
  81. expect( view.body.get( 0 ) ).to.equal( view.panel );
  82. } );
  83. } );
  84. describe( '#editable', () => {
  85. it( 'is registered as a child', () => {
  86. const spy = sinon.spy( view.editable, 'destroy' );
  87. view.destroy();
  88. sinon.assert.calledOnce( spy );
  89. } );
  90. } );
  91. } );
  92. describe( 'init()', () => {
  93. it( 'appends #toolbar to panel#content', () => {
  94. locale = new Locale( 'en' );
  95. const view = new InlineEditorUIView( locale, editingView );
  96. view.editable.name = editingViewRoot.rootName;
  97. expect( view.panel.content ).to.have.length( 0 );
  98. view.render();
  99. expect( view.panel.content.get( 0 ) ).to.equal( view.toolbar );
  100. view.destroy();
  101. } );
  102. } );
  103. describe( 'panelPositions', () => {
  104. it( 'returns the positions in the right order (languageDirection="ltr")', () => {
  105. locale.languageDirection = 'ltr';
  106. const uiView = new InlineEditorUIView( locale, editingView );
  107. const positions = uiView.panelPositions;
  108. const editableRect = {
  109. top: 100,
  110. bottom: 200,
  111. left: 100,
  112. right: 100,
  113. width: 100,
  114. height: 100
  115. };
  116. const panelRect = {
  117. width: 50,
  118. height: 50
  119. };
  120. expect( positions ).to.have.length( 2 );
  121. expect( positions[ 0 ]( editableRect, panelRect ).name ).to.equal( 'toolbar_west' );
  122. expect( positions[ 1 ]( editableRect, panelRect ).name ).to.equal( 'toolbar_east' );
  123. } );
  124. it( 'returns the positions in the right order (languageDirection="rtl")', () => {
  125. locale.languageDirection = 'rtl';
  126. const uiView = new InlineEditorUIView( locale, editingView );
  127. const positions = uiView.panelPositions;
  128. const editableRect = {
  129. top: 100,
  130. bottom: 200,
  131. left: 100,
  132. right: 100,
  133. width: 100,
  134. height: 100
  135. };
  136. const panelRect = {
  137. width: 50,
  138. height: 50
  139. };
  140. expect( positions ).to.have.length( 2 );
  141. expect( positions[ 0 ]( editableRect, panelRect ).name ).to.equal( 'toolbar_east' );
  142. expect( positions[ 1 ]( editableRect, panelRect ).name ).to.equal( 'toolbar_west' );
  143. } );
  144. describe( 'west', () => {
  145. testTopPositions( 0, 100 );
  146. } );
  147. describe( 'east', () => {
  148. testTopPositions( 1, 150 );
  149. } );
  150. function testTopPositions( positionIndex, expectedLeft ) {
  151. it( 'positions the panel above editable when there\'s enough space', () => {
  152. const position = view.panelPositions[ positionIndex ];
  153. const editableRect = {
  154. top: 101, // !
  155. bottom: 200,
  156. left: 100,
  157. right: 100,
  158. width: 100,
  159. height: 100
  160. };
  161. const panelRect = {
  162. width: 50,
  163. height: 100 // !
  164. };
  165. const { top, left } = position( editableRect, panelRect );
  166. expect( top ).to.equal( 1 );
  167. expect( left ).to.equal( expectedLeft );
  168. } );
  169. it( 'positions the panel over the editable when there\'s not enough space above (1)', () => {
  170. const position = view.panelPositions[ positionIndex ];
  171. const editableRect = {
  172. top: 100, // !
  173. bottom: 300,
  174. left: 100,
  175. right: 100,
  176. width: 100,
  177. height: 200
  178. };
  179. const panelRect = {
  180. width: 50,
  181. height: 100 // !
  182. };
  183. const { top, left } = position( editableRect, panelRect );
  184. expect( top ).to.equal( 0 );
  185. expect( left ).to.equal( expectedLeft );
  186. } );
  187. it( 'positions the panel over the editable when there\'s not enough space above (2)', () => {
  188. const position = view.panelPositions[ positionIndex ];
  189. const editableRect = {
  190. top: 99, // !
  191. bottom: 399,
  192. left: 100,
  193. right: 100,
  194. width: 100,
  195. height: 200
  196. };
  197. const panelRect = {
  198. width: 50,
  199. height: 100 // !
  200. };
  201. const { top, left } = position( editableRect, panelRect );
  202. expect( top ).to.equal( 0 );
  203. expect( left ).to.equal( expectedLeft );
  204. } );
  205. it( 'positions the panel over the editable when there\'s not enough space above (3)', () => {
  206. const position = view.panelPositions[ positionIndex ];
  207. const editableRect = {
  208. top: 51, // !
  209. bottom: 399,
  210. left: 100,
  211. right: 100,
  212. width: 100,
  213. height: 200
  214. };
  215. const panelRect = {
  216. width: 50,
  217. height: 100 // !
  218. };
  219. const { top, left } = position( editableRect, panelRect );
  220. expect( top ).to.equal( 0 );
  221. expect( left ).to.equal( expectedLeft );
  222. } );
  223. it( 'positions the panel below the editable when there\'s not enough space above/over', () => {
  224. const position = view.panelPositions[ positionIndex ];
  225. const editableRect = {
  226. top: 50,
  227. bottom: 150, // !
  228. left: 100,
  229. right: 100,
  230. width: 100,
  231. height: 100
  232. };
  233. const panelRect = {
  234. width: 50,
  235. height: 100 // !
  236. };
  237. const { top, left } = position( editableRect, panelRect );
  238. expect( top ).to.equal( 150 );
  239. expect( left ).to.equal( expectedLeft );
  240. } );
  241. describe( 'view#viewportTopOffset', () => {
  242. it( 'sticks the panel to the offset when there\'s not enough space above', () => {
  243. view.viewportTopOffset = 50;
  244. const position = view.panelPositions[ positionIndex ];
  245. const editableRect = {
  246. top: 0, // !
  247. bottom: 200,
  248. left: 100,
  249. right: 100,
  250. width: 100,
  251. height: 200
  252. };
  253. const panelRect = {
  254. width: 50,
  255. height: 50
  256. };
  257. const { top, left } = position( editableRect, panelRect );
  258. expect( top ).to.equal( 50 );
  259. expect( left ).to.equal( expectedLeft );
  260. } );
  261. it( 'positions the panel below the editable when there\'s not enough space above/over', () => {
  262. view.viewportTopOffset = 50;
  263. const position = view.panelPositions[ positionIndex ];
  264. const editableRect = {
  265. top: 100,
  266. bottom: 150,
  267. left: 100,
  268. right: 100,
  269. width: 100,
  270. height: 50
  271. };
  272. const panelRect = {
  273. width: 50,
  274. height: 80
  275. };
  276. const { top, left } = position( editableRect, panelRect );
  277. expect( top ).to.equal( 150 );
  278. expect( left ).to.equal( expectedLeft );
  279. } );
  280. } );
  281. }
  282. } );
  283. } );