utils.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  6. import Table from '../../src/table';
  7. import TableCellProperties from '../../src/tablecellproperties';
  8. import { findAncestor } from '../../src/commands/utils';
  9. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  10. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  11. import View from '@ckeditor/ckeditor5-ui/src/view';
  12. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  13. import BalloonPanelView from '@ckeditor/ckeditor5-ui/src/panel/balloon/balloonpanelview';
  14. import ToolbarView from '@ckeditor/ckeditor5-ui/src/toolbar/toolbarview';
  15. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  16. import {
  17. repositionContextualBalloon,
  18. getBalloonCellPositionData,
  19. getBorderStyleDefinitions,
  20. getBorderStyleLabels,
  21. fillToolbar
  22. } from '../../src/ui/utils';
  23. import Collection from '@ckeditor/ckeditor5-utils/src/collection';
  24. describe( 'UI Utils', () => {
  25. let editor, editingView, balloon, editorElement;
  26. beforeEach( () => {
  27. editorElement = global.document.createElement( 'div' );
  28. global.document.body.appendChild( editorElement );
  29. return ClassicEditor
  30. .create( editorElement, {
  31. plugins: [ Table, TableCellProperties, Paragraph ]
  32. } )
  33. .then( newEditor => {
  34. editor = newEditor;
  35. editingView = editor.editing.view;
  36. balloon = editor.plugins.get( 'ContextualBalloon' );
  37. } );
  38. } );
  39. afterEach( () => {
  40. editorElement.remove();
  41. return editor.destroy();
  42. } );
  43. describe( 'repositionContextualBalloon()', () => {
  44. describe( 'with respect to the table cell', () => {
  45. it( 'should re-position the ContextualBalloon when the table cell is selected', () => {
  46. const spy = sinon.spy( balloon, 'updatePosition' );
  47. const defaultPositions = BalloonPanelView.defaultPositions;
  48. const view = new View();
  49. view.element = global.document.createElement( 'div' );
  50. balloon.add( {
  51. view,
  52. position: {
  53. target: global.document.body
  54. }
  55. } );
  56. setData( editor.model,
  57. '<table><tableRow>' +
  58. '<tableCell><paragraph>foo</paragraph></tableCell>' +
  59. '<tableCell><paragraph>[bar]</paragraph></tableCell>' +
  60. '</tableRow></table>' );
  61. repositionContextualBalloon( editor, 'cell' );
  62. const modelCell = findAncestor( 'tableCell', editor.model.document.selection.getFirstPosition() );
  63. const viewCell = editor.editing.mapper.toViewElement( modelCell );
  64. sinon.assert.calledWithExactly( spy, {
  65. target: editingView.domConverter.viewToDom( viewCell ),
  66. positions: [
  67. defaultPositions.northArrowSouth,
  68. defaultPositions.northArrowSouthWest,
  69. defaultPositions.northArrowSouthEast,
  70. defaultPositions.southArrowNorth,
  71. defaultPositions.southArrowNorthWest,
  72. defaultPositions.southArrowNorthEast
  73. ]
  74. } );
  75. } );
  76. it( 'should not engage with no table is selected', () => {
  77. const spy = sinon.spy( balloon, 'updatePosition' );
  78. setData( editor.model, '<paragraph>foo</paragraph>' );
  79. repositionContextualBalloon( editor, 'cell' );
  80. sinon.assert.notCalled( spy );
  81. } );
  82. } );
  83. describe( 'with respect to the entire table', () => {
  84. it( 'should re-position the ContextualBalloon when the table is selected', () => {
  85. const spy = sinon.spy( balloon, 'updatePosition' );
  86. const defaultPositions = BalloonPanelView.defaultPositions;
  87. const view = new View();
  88. view.element = global.document.createElement( 'div' );
  89. balloon.add( {
  90. view,
  91. position: {
  92. target: global.document.body
  93. }
  94. } );
  95. setData( editor.model,
  96. '<table><tableRow>' +
  97. '<tableCell><paragraph>foo</paragraph></tableCell>' +
  98. '<tableCell><paragraph>[bar]</paragraph></tableCell>' +
  99. '</tableRow></table>' );
  100. repositionContextualBalloon( editor, 'table' );
  101. const modelTable = findAncestor( 'table', editor.model.document.selection.getFirstPosition() );
  102. const viewTable = editor.editing.mapper.toViewElement( modelTable );
  103. sinon.assert.calledWithExactly( spy, {
  104. target: editingView.domConverter.viewToDom( viewTable ),
  105. positions: [
  106. defaultPositions.northArrowSouth,
  107. defaultPositions.northArrowSouthWest,
  108. defaultPositions.northArrowSouthEast,
  109. defaultPositions.southArrowNorth,
  110. defaultPositions.southArrowNorthWest,
  111. defaultPositions.southArrowNorthEast
  112. ]
  113. } );
  114. } );
  115. it( 'should not engage with no table is selected', () => {
  116. const spy = sinon.spy( balloon, 'updatePosition' );
  117. setData( editor.model, '<paragraph>foo</paragraph>' );
  118. repositionContextualBalloon( editor, 'table' );
  119. sinon.assert.notCalled( spy );
  120. } );
  121. } );
  122. } );
  123. describe( 'getBalloonCellPositionData', () => {
  124. it( 'returns the position data', () => {
  125. const defaultPositions = BalloonPanelView.defaultPositions;
  126. setData( editor.model, '<table><tableRow>' +
  127. '<tableCell><paragraph>foo</paragraph></tableCell>' +
  128. '<tableCell><paragraph>[bar]</paragraph></tableCell>' +
  129. '</tableRow></table>' );
  130. const data = getBalloonCellPositionData( editor );
  131. const modelCell = findAncestor( 'tableCell', editor.model.document.selection.getFirstPosition() );
  132. const viewCell = editor.editing.mapper.toViewElement( modelCell );
  133. expect( data ).to.deep.equal( {
  134. target: editingView.domConverter.viewToDom( viewCell ),
  135. positions: [
  136. defaultPositions.northArrowSouth,
  137. defaultPositions.northArrowSouthWest,
  138. defaultPositions.northArrowSouthEast,
  139. defaultPositions.southArrowNorth,
  140. defaultPositions.southArrowNorthWest,
  141. defaultPositions.southArrowNorthEast
  142. ]
  143. } );
  144. } );
  145. } );
  146. describe( 'getBorderStyleLabels()', () => {
  147. it( 'should return labels for different border styles', () => {
  148. const t = string => string;
  149. expect( getBorderStyleLabels( t ) ).to.deep.equal( {
  150. none: 'None',
  151. solid: 'Solid',
  152. dotted: 'Dotted',
  153. dashed: 'Dashed',
  154. double: 'Double',
  155. groove: 'Groove',
  156. ridge: 'Ridge',
  157. inset: 'Inset',
  158. outset: 'Outset',
  159. } );
  160. } );
  161. } );
  162. describe( 'getBorderStyleDefinitions()', () => {
  163. let view, locale, definitions;
  164. beforeEach( () => {
  165. locale = { t: val => val };
  166. view = new View( locale );
  167. view.set( 'borderStyle', 'none' );
  168. definitions = getBorderStyleDefinitions( view );
  169. } );
  170. it( 'should return a collection', () => {
  171. expect( definitions ).to.be.instanceOf( Collection );
  172. } );
  173. it( 'should create a button definition for each style', () => {
  174. expect( definitions.map( ( { type } ) => type ).every( item => item === 'button' ) ).to.be.true;
  175. } );
  176. it( 'should set label of a button for each style', () => {
  177. expect( definitions.map( ( { model: { label } } ) => label ) ).to.have.ordered.members( [
  178. 'None',
  179. 'Solid',
  180. 'Dotted',
  181. 'Dashed',
  182. 'Double',
  183. 'Groove',
  184. 'Ridge',
  185. 'Inset',
  186. 'Outset',
  187. ] );
  188. } );
  189. it( 'should set type of a button for each style', () => {
  190. expect( definitions.map( ( { model: { withText } } ) => withText ).every( item => item === true ) ).to.be.true;
  191. } );
  192. it( 'should bind button\'s #isOn to the view #borderStyle property', () => {
  193. view.borderStyle = 'dotted';
  194. expect( definitions.map( ( { model: { isOn } } ) => isOn ) ).to.have.ordered.members( [
  195. false,
  196. false,
  197. true,
  198. false,
  199. false,
  200. false,
  201. false,
  202. false,
  203. false,
  204. ] );
  205. view.borderStyle = 'inset';
  206. expect( definitions.map( ( { model: { isOn } } ) => isOn ) ).to.have.ordered.members( [
  207. false,
  208. false,
  209. false,
  210. false,
  211. false,
  212. false,
  213. false,
  214. true,
  215. false,
  216. ] );
  217. } );
  218. } );
  219. describe( 'fillToolbar()', () => {
  220. let view, locale, toolbar;
  221. const labels = {
  222. first: 'Do something',
  223. second: 'Do something else'
  224. };
  225. const icons = {
  226. first: '<svg viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path /></svg>',
  227. second: '<svg viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path /></svg>'
  228. };
  229. beforeEach( () => {
  230. locale = { t: val => val };
  231. view = new View( locale );
  232. view.set( 'someProperty', 'foo' );
  233. toolbar = new ToolbarView( locale );
  234. fillToolbar( {
  235. view, toolbar, icons, labels,
  236. propertyName: 'someProperty'
  237. } );
  238. } );
  239. afterEach( () => {
  240. view.destroy();
  241. } );
  242. it( 'should create buttons', () => {
  243. expect( toolbar.items ).to.have.length( 2 );
  244. expect( toolbar.items.first ).to.be.instanceOf( ButtonView );
  245. expect( toolbar.items.last ).to.be.instanceOf( ButtonView );
  246. } );
  247. it( 'should set button labels', () => {
  248. expect( toolbar.items.first.label ).to.equal( 'Do something' );
  249. expect( toolbar.items.last.label ).to.equal( 'Do something else' );
  250. } );
  251. it( 'should set button icons', () => {
  252. expect( toolbar.items.first.icon ).to.equal( icons.first );
  253. expect( toolbar.items.last.icon ).to.equal( icons.second );
  254. } );
  255. it( 'should bind button #isOn to an observable property', () => {
  256. expect( toolbar.items.first.isOn ).to.be.false;
  257. expect( toolbar.items.last.isOn ).to.be.false;
  258. view.someProperty = 'first';
  259. expect( toolbar.items.first.isOn ).to.be.true;
  260. expect( toolbar.items.last.isOn ).to.be.false;
  261. view.someProperty = 'second';
  262. expect( toolbar.items.first.isOn ).to.be.false;
  263. expect( toolbar.items.last.isOn ).to.be.true;
  264. } );
  265. it( 'should make the buttons change the property value upon execution', () => {
  266. toolbar.items.first.fire( 'execute' );
  267. expect( view.someProperty ).to.equal( 'first' );
  268. toolbar.items.last.fire( 'execute' );
  269. expect( view.someProperty ).to.equal( 'second' );
  270. } );
  271. } );
  272. } );