8
0

blocktoolbar.js 14 KB

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