blocktoolbar.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /**
  2. * Copyright (c) 2016 - 2017, CKSource - Frederico Knabben. All rights reserved.
  3. */
  4. /* global document, window, Event */
  5. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  6. import ClickObserver from '@ckeditor/ckeditor5-engine/src/view/observer/clickobserver';
  7. import BlockToolbar from '../../../src/toolbar/block/blocktoolbar';
  8. import ToolbarView from '../../../src/toolbar/toolbarview';
  9. import BalloonPanelView from '../../../src/panel/balloon/balloonpanelview';
  10. import BlockButtonView from './../../../src/toolbar/block/view/blockbuttonview';
  11. import Heading from '@ckeditor/ckeditor5-heading/src/heading';
  12. import HeadingButtonsUI from '@ckeditor/ckeditor5-heading/src/headingbuttonsui';
  13. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  14. import ParagraphButtonUI from '@ckeditor/ckeditor5-paragraph/src/paragraphbuttonui';
  15. import BlockQuote from '@ckeditor/ckeditor5-block-quote/src/blockquote';
  16. import Image from '@ckeditor/ckeditor5-image/src/image';
  17. import ImageCaption from '@ckeditor/ckeditor5-image/src/imagecaption';
  18. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  19. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  20. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  21. testUtils.createSinonSandbox();
  22. describe( 'BlockToolbar', () => {
  23. let editor, element, blockToolbar;
  24. beforeEach( () => {
  25. element = document.createElement( 'div' );
  26. document.body.appendChild( element );
  27. return ClassicTestEditor.create( element, {
  28. plugins: [ BlockToolbar, Heading, HeadingButtonsUI, Paragraph, ParagraphButtonUI, BlockQuote, Image, ImageCaption ],
  29. blockToolbar: [ 'paragraph', 'heading1', 'heading2', 'blockQuote' ]
  30. } ).then( newEditor => {
  31. editor = newEditor;
  32. blockToolbar = editor.plugins.get( BlockToolbar );
  33. } );
  34. } );
  35. afterEach( () => {
  36. element.remove();
  37. return editor.destroy();
  38. } );
  39. it( 'should have pluginName property', () => {
  40. expect( BlockToolbar.pluginName ).to.equal( 'BlockToolbar' );
  41. } );
  42. it( 'should register click observer', () => {
  43. expect( editor.editing.view.getObserver( ClickObserver ) ).to.be.instanceOf( ClickObserver );
  44. } );
  45. describe( 'child views', () => {
  46. describe( 'panelView', () => {
  47. it( 'should create view instance', () => {
  48. expect( blockToolbar.panelView ).to.instanceof( BalloonPanelView );
  49. } );
  50. it( 'should have additional class name', () => {
  51. expect( blockToolbar.panelView.className ).to.equal( 'ck-balloon-panel-block-toolbar' );
  52. } );
  53. it( 'should be added to ui.view.body collection', () => {
  54. expect( Array.from( editor.ui.view.body ) ).to.include( blockToolbar.panelView );
  55. } );
  56. it( 'should add panelView to ui.focusTracker', () => {
  57. expect( editor.ui.focusTracker.isFocused ).to.false;
  58. blockToolbar.panelView.element.dispatchEvent( new Event( 'focus' ) );
  59. expect( editor.ui.focusTracker.isFocused ).to.true;
  60. } );
  61. it( 'should close panelView after `Esc` press and focus view document', () => {
  62. const spy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  63. blockToolbar.panelView.isVisible = true;
  64. blockToolbar.toolbarView.keystrokes.press( {
  65. keyCode: keyCodes.esc,
  66. preventDefault: () => {},
  67. stopPropagation: () => {}
  68. } );
  69. expect( blockToolbar.panelView.isVisible ).to.false;
  70. sinon.assert.calledOnce( spy );
  71. } );
  72. it( 'should close panelView on click outside the panel and not focus view document', () => {
  73. const spy = testUtils.sinon.spy();
  74. editor.editing.view.on( 'focus', spy );
  75. blockToolbar.panelView.isVisible = true;
  76. document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  77. expect( blockToolbar.panelView.isVisible ).to.false;
  78. sinon.assert.notCalled( spy );
  79. } );
  80. it( 'should not close panelView on click on panel element', () => {
  81. blockToolbar.panelView.isVisible = true;
  82. blockToolbar.panelView.element.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  83. expect( blockToolbar.panelView.isVisible ).to.true;
  84. } );
  85. } );
  86. describe( 'toolbarView', () => {
  87. it( 'should create view instance', () => {
  88. expect( blockToolbar.toolbarView ).to.instanceof( ToolbarView );
  89. } );
  90. it( 'should be added to panelView#content collection', () => {
  91. expect( Array.from( blockToolbar.panelView.content ) ).to.include( blockToolbar.toolbarView );
  92. } );
  93. it( 'should initialize toolbar items based on Editor#blockToolbar config', () => {
  94. expect( Array.from( blockToolbar.toolbarView.items ) ).to.length( 4 );
  95. } );
  96. it( 'should hide panel after clicking on the button from toolbar', () => {
  97. blockToolbar.buttonView.fire( 'execute' );
  98. expect( blockToolbar.panelView.isVisible ).to.true;
  99. blockToolbar.toolbarView.items.get( 0 ).fire( 'execute' );
  100. expect( blockToolbar.panelView.isVisible ).to.false;
  101. } );
  102. } );
  103. describe( 'buttonView', () => {
  104. it( 'should create view instance', () => {
  105. expect( blockToolbar.buttonView ).to.instanceof( BlockButtonView );
  106. } );
  107. it( 'should be added to editor ui.view.body collection', () => {
  108. expect( Array.from( editor.ui.view.body ) ).to.include( blockToolbar.buttonView );
  109. } );
  110. it( 'should add buttonView to ui.focusTracker', () => {
  111. expect( editor.ui.focusTracker.isFocused ).to.false;
  112. blockToolbar.buttonView.element.dispatchEvent( new Event( 'focus' ) );
  113. expect( editor.ui.focusTracker.isFocused ).to.true;
  114. } );
  115. it( 'should pin panelView to the button and focus first item in toolbar on #execute event', () => {
  116. expect( blockToolbar.panelView.isVisible ).to.false;
  117. const pinSpy = testUtils.sinon.spy( blockToolbar.panelView, 'pin' );
  118. const focusSpy = testUtils.sinon.spy( blockToolbar.toolbarView.items.get( 0 ), 'focus' );
  119. blockToolbar.buttonView.fire( 'execute' );
  120. expect( blockToolbar.panelView.isVisible ).to.true;
  121. sinon.assert.calledWith( pinSpy, {
  122. target: blockToolbar.buttonView.element,
  123. limiter: editor.ui.view.editableElement
  124. } );
  125. sinon.assert.calledOnce( focusSpy );
  126. } );
  127. it( 'should hide panelView and focus editable on #execute event when panel was visible', () => {
  128. blockToolbar.panelView.isVisible = true;
  129. const spy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  130. blockToolbar.buttonView.fire( 'execute' );
  131. expect( blockToolbar.panelView.isVisible ).to.false;
  132. sinon.assert.calledOnce( spy );
  133. } );
  134. it( 'should bind #isOn to panelView#isVisible', () => {
  135. blockToolbar.panelView.isVisible = false;
  136. expect( blockToolbar.buttonView.isOn ).to.false;
  137. blockToolbar.panelView.isVisible = true;
  138. expect( blockToolbar.buttonView.isOn ).to.true;
  139. } );
  140. it( 'should hide button tooltip when panelView is opened', () => {
  141. blockToolbar.panelView.isVisible = false;
  142. expect( blockToolbar.buttonView.tooltip ).to.true;
  143. blockToolbar.panelView.isVisible = true;
  144. expect( blockToolbar.buttonView.tooltip ).to.false;
  145. } );
  146. } );
  147. } );
  148. describe( 'allowed elements', () => {
  149. it( 'should display button when the first selected block is a block element', () => {
  150. editor.model.schema.register( 'foo', { inheritAllFrom: '$block' } );
  151. editor.conversion.elementToElement( { model: 'foo', view: 'foo' } );
  152. setData( editor.model, '<foo>foo[]bar</foo>' );
  153. expect( blockToolbar.buttonView.isVisible ).to.true;
  154. } );
  155. it( 'should display button when the first selected block is an object', () => {
  156. setData( editor.model, '[<image src="foo.jpg"><caption>foo</caption></image>]' );
  157. expect( blockToolbar.buttonView.isVisible ).to.true;
  158. } );
  159. it( 'should display button when the selection is inside the object', () => {
  160. setData( editor.model, '<image src="foo.jpg"><caption>f[]oo</caption></image>' );
  161. expect( blockToolbar.buttonView.isVisible ).to.true;
  162. } );
  163. it( 'should not display button when the selection is placed in a root element', () => {
  164. setData( editor.model, '<paragraph>foo</paragraph>[]<paragraph>bar</paragraph>' );
  165. expect( blockToolbar.buttonView.isVisible ).to.false;
  166. } );
  167. it( 'should not display button when all toolbar items are disabled for the selected element', () => {
  168. const element = document.createElement( 'div' );
  169. document.body.appendChild( element );
  170. return ClassicTestEditor.create( element, {
  171. plugins: [ BlockToolbar, Heading, HeadingButtonsUI, Paragraph, ParagraphButtonUI, Image ],
  172. blockToolbar: [ 'paragraph', 'heading1', 'heading2' ]
  173. } ).then( editor => {
  174. const blockToolbar = editor.plugins.get( BlockToolbar );
  175. setData( editor.model, '[<image src="foo.jpg"></image>]' );
  176. expect( blockToolbar.buttonView.isVisible ).to.false;
  177. element.remove();
  178. return editor.destroy();
  179. } );
  180. } );
  181. } );
  182. describe( 'attaching button to the content', () => {
  183. it( 'should attach right side of the button to the left side of the editable and center with the first line ' +
  184. 'of selected block #1', () => {
  185. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  186. const target = editor.ui.view.editableElement.querySelector( 'p' );
  187. const styleMock = testUtils.sinon.stub( window, 'getComputedStyle' );
  188. styleMock.withArgs( target ).returns( {
  189. lineHeight: '20px',
  190. paddingTop: '10px'
  191. } );
  192. styleMock.callThrough();
  193. testUtils.sinon.stub( editor.ui.view.editableElement, 'getBoundingClientRect' ).returns( {
  194. left: 200
  195. } );
  196. testUtils.sinon.stub( target, 'getBoundingClientRect' ).returns( {
  197. top: 500,
  198. left: 300
  199. } );
  200. testUtils.sinon.stub( blockToolbar.buttonView.element, 'getBoundingClientRect' ).returns( {
  201. width: 100,
  202. height: 100
  203. } );
  204. editor.editing.view.fire( 'render' );
  205. expect( blockToolbar.buttonView.top ).to.equal( 470 );
  206. expect( blockToolbar.buttonView.left ).to.equal( 100 );
  207. } );
  208. it( 'should attach right side of the button to the left side of the editable and center with the first line ' +
  209. 'of selected block #2', () => {
  210. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  211. const target = editor.ui.view.editableElement.querySelector( 'p' );
  212. const styleMock = testUtils.sinon.stub( window, 'getComputedStyle' );
  213. styleMock.withArgs( target ).returns( {
  214. lineHeight: 'normal',
  215. fontSize: '20px',
  216. paddingTop: '10px'
  217. } );
  218. styleMock.callThrough();
  219. testUtils.sinon.stub( editor.ui.view.editableElement, 'getBoundingClientRect' ).returns( {
  220. left: 200
  221. } );
  222. testUtils.sinon.stub( target, 'getBoundingClientRect' ).returns( {
  223. top: 500,
  224. left: 300
  225. } );
  226. testUtils.sinon.stub( blockToolbar.buttonView.element, 'getBoundingClientRect' ).returns( {
  227. width: 100,
  228. height: 100
  229. } );
  230. editor.editing.view.fire( 'render' );
  231. expect( blockToolbar.buttonView.top ).to.equal( 472 );
  232. expect( blockToolbar.buttonView.left ).to.equal( 100 );
  233. } );
  234. it( 'should reposition panelView when is opened on view#render', () => {
  235. blockToolbar.panelView.isVisible = false;
  236. const spy = testUtils.sinon.spy( blockToolbar.panelView, 'pin' );
  237. editor.editing.view.fire( 'render' );
  238. sinon.assert.notCalled( spy );
  239. blockToolbar.panelView.isVisible = true;
  240. editor.editing.view.fire( 'render' );
  241. sinon.assert.calledWith( spy, {
  242. target: blockToolbar.buttonView.element,
  243. limiter: editor.ui.view.editableElement
  244. } );
  245. } );
  246. it( 'should not reset toolbar focus on view#render', () => {
  247. blockToolbar.panelView.isVisible = true;
  248. const spy = testUtils.sinon.spy( blockToolbar.toolbarView, 'focus' );
  249. editor.editing.view.fire( 'render' );
  250. sinon.assert.notCalled( spy );
  251. } );
  252. it( 'should hide opened panel on a selection direct change', () => {
  253. blockToolbar.panelView.isVisible = true;
  254. editor.model.document.selection.fire( 'change:range', { directChange: true } );
  255. expect( blockToolbar.panelView.isVisible ).to.false;
  256. } );
  257. it( 'should not hide opened panel on a selection not direct change', () => {
  258. blockToolbar.panelView.isVisible = true;
  259. editor.model.document.selection.fire( 'change:range', { directChange: false } );
  260. expect( blockToolbar.panelView.isVisible ).to.true;
  261. } );
  262. it( 'should hide button and stop attaching it when editor switch to readonly', () => {
  263. setData( editor.model, '<paragraph>foo[]bar</paragraph>' );
  264. blockToolbar.panelView.isVisible = true;
  265. expect( blockToolbar.buttonView.isVisible ).to.true;
  266. expect( blockToolbar.panelView.isVisible ).to.true;
  267. editor.isReadOnly = true;
  268. expect( blockToolbar.buttonView.isVisible ).to.false;
  269. expect( blockToolbar.panelView.isVisible ).to.false;
  270. editor.editing.view.fire( 'render' );
  271. expect( blockToolbar.buttonView.isVisible ).to.false;
  272. expect( blockToolbar.panelView.isVisible ).to.false;
  273. editor.isReadOnly = false;
  274. editor.editing.view.fire( 'render' );
  275. expect( blockToolbar.buttonView.isVisible ).to.true;
  276. expect( blockToolbar.panelView.isVisible ).to.false;
  277. } );
  278. it( 'should update button position on browser resize only when button is visible', () => {
  279. const spy = testUtils.sinon.spy( blockToolbar, '_attachButtonToElement' );
  280. setData( editor.model, '[]<paragraph>bar</paragraph>' );
  281. window.dispatchEvent( new Event( 'resize' ) );
  282. sinon.assert.notCalled( spy );
  283. setData( editor.model, '<paragraph>ba[]r</paragraph>' );
  284. spy.resetHistory();
  285. window.dispatchEvent( new Event( 'resize' ) );
  286. sinon.assert.called( spy );
  287. setData( editor.model, '[]<paragraph>bar</paragraph>' );
  288. spy.resetHistory();
  289. window.dispatchEvent( new Event( 'resize' ) );
  290. sinon.assert.notCalled( spy );
  291. } );
  292. } );
  293. } );