blocktoolbar.js 14 KB

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