listui.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. import ListEditing from '../src/listediting';
  7. import ListUI from '../src/listui';
  8. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  9. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  10. import BlockQuote from '@ckeditor/ckeditor5-block-quote/src/blockquote';
  11. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  12. import { getCode } from '@ckeditor/ckeditor5-utils/src/keyboard';
  13. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  14. describe( 'ListUI', () => {
  15. let editor, model, bulletedListButton, numberedListButton;
  16. beforeEach( () => {
  17. const editorElement = document.createElement( 'div' );
  18. document.body.appendChild( editorElement );
  19. return ClassicTestEditor.create( editorElement, { plugins: [ Paragraph, BlockQuote, ListEditing, ListUI ] } )
  20. .then( newEditor => {
  21. editor = newEditor;
  22. model = editor.model;
  23. bulletedListButton = editor.ui.componentFactory.create( 'bulletedList' );
  24. numberedListButton = editor.ui.componentFactory.create( 'numberedList' );
  25. } );
  26. } );
  27. afterEach( () => {
  28. return editor.destroy();
  29. } );
  30. it( 'should be loaded', () => {
  31. expect( editor.plugins.get( ListUI ) ).to.be.instanceOf( ListUI );
  32. } );
  33. it( 'should set up keys for bulleted list and numbered list', () => {
  34. expect( bulletedListButton ).to.be.instanceOf( ButtonView );
  35. expect( numberedListButton ).to.be.instanceOf( ButtonView );
  36. } );
  37. it( 'should execute proper commands when buttons are used', () => {
  38. sinon.spy( editor, 'execute' );
  39. bulletedListButton.fire( 'execute' );
  40. expect( editor.execute.calledWithExactly( 'bulletedList' ) );
  41. numberedListButton.fire( 'execute' );
  42. expect( editor.execute.calledWithExactly( 'numberedList' ) );
  43. } );
  44. it( 'should bind bulleted list button model to bulledList command', () => {
  45. setData( model, '<listItem type="bulleted" indent="0">[]foo</listItem>' );
  46. const command = editor.commands.get( 'bulletedList' );
  47. expect( bulletedListButton.isOn ).to.be.true;
  48. expect( bulletedListButton.isEnabled ).to.be.true;
  49. command.value = false;
  50. expect( bulletedListButton.isOn ).to.be.false;
  51. command.isEnabled = false;
  52. expect( bulletedListButton.isEnabled ).to.be.false;
  53. } );
  54. it( 'should bind numbered list button model to numberedList command', () => {
  55. setData( model, '<listItem type="bulleted" indent="0">[]foo</listItem>' );
  56. const command = editor.commands.get( 'numberedList' );
  57. // We are in UL, so numbered list is off.
  58. expect( numberedListButton.isOn ).to.be.false;
  59. expect( numberedListButton.isEnabled ).to.be.true;
  60. command.value = true;
  61. expect( numberedListButton.isOn ).to.be.true;
  62. command.isEnabled = false;
  63. expect( numberedListButton.isEnabled ).to.be.false;
  64. } );
  65. describe( 'enter key handling callback', () => {
  66. it( 'should execute outdentList command on enter key in empty list', () => {
  67. const domEvtDataStub = { preventDefault() {} };
  68. sinon.spy( editor, 'execute' );
  69. setData( model, '<listItem type="bulleted" indent="0">[]</listItem>' );
  70. editor.editing.view.fire( 'enter', domEvtDataStub );
  71. expect( editor.execute.calledOnce ).to.be.true;
  72. expect( editor.execute.calledWithExactly( 'outdentList' ) );
  73. } );
  74. it( 'should not execute outdentList command on enter key in non-empty list', () => {
  75. const domEvtDataStub = { preventDefault() {} };
  76. sinon.spy( editor, 'execute' );
  77. setData( model, '<listItem type="bulleted" indent="0">foo[]</listItem>' );
  78. editor.editing.view.fire( 'enter', domEvtDataStub );
  79. expect( editor.execute.called ).to.be.false;
  80. } );
  81. } );
  82. describe( 'delete key handling callback', () => {
  83. it( 'should execute outdentList command on backspace key in first item of list', () => {
  84. const domEvtDataStub = { preventDefault() {}, direction: 'backward' };
  85. sinon.spy( editor, 'execute' );
  86. setData( model, '<listItem type="bulleted" indent="0">[]foo</listItem>' );
  87. editor.editing.view.fire( 'delete', domEvtDataStub );
  88. expect( editor.execute.calledWithExactly( 'outdentList' ) );
  89. } );
  90. it( 'should execute outdentList command on backspace key in first item of list', () => {
  91. const domEvtDataStub = { preventDefault() {}, direction: 'backward' };
  92. sinon.spy( editor, 'execute' );
  93. setData( model, '<paragraph>foo</paragraph><listItem type="bulleted" indent="0">[]foo</listItem>' );
  94. editor.editing.view.fire( 'delete', domEvtDataStub );
  95. expect( editor.execute.calledWithExactly( 'outdentList' ) );
  96. } );
  97. it( 'should not execute outdentList command on delete key in first item of list', () => {
  98. const domEvtDataStub = { preventDefault() {}, direction: 'forward' };
  99. sinon.spy( editor, 'execute' );
  100. setData( model, '<listItem type="bulleted" indent="0">[]foo</listItem>' );
  101. editor.editing.view.fire( 'delete', domEvtDataStub );
  102. sinon.assert.notCalled( editor.execute );
  103. } );
  104. it( 'should not execute outdentList command when selection is not collapsed', () => {
  105. const domEvtDataStub = { preventDefault() {}, direction: 'backward' };
  106. sinon.spy( editor, 'execute' );
  107. setData( model, '<listItem type="bulleted" indent="0">[fo]o</listItem>' );
  108. editor.editing.view.fire( 'delete', domEvtDataStub );
  109. sinon.assert.notCalled( editor.execute );
  110. } );
  111. it( 'should not execute outdentList command if not in list item', () => {
  112. const domEvtDataStub = { preventDefault() {}, direction: 'backward' };
  113. sinon.spy( editor, 'execute' );
  114. setData( model, '<paragraph>[]foo</paragraph>' );
  115. editor.editing.view.fire( 'delete', domEvtDataStub );
  116. sinon.assert.notCalled( editor.execute );
  117. } );
  118. it( 'should not execute outdentList command if not in first list item', () => {
  119. const domEvtDataStub = { preventDefault() {}, direction: 'backward' };
  120. sinon.spy( editor, 'execute' );
  121. setData( model, '<listItem type="bulleted" indent="0">foo</listItem><listItem type="bulleted" indent="0">[]foo</listItem>' );
  122. editor.editing.view.fire( 'delete', domEvtDataStub );
  123. sinon.assert.notCalled( editor.execute );
  124. } );
  125. it( 'should not execute outdentList command when selection is not on first position', () => {
  126. const domEvtDataStub = { preventDefault() {}, direction: 'backward' };
  127. sinon.spy( editor, 'execute' );
  128. setData( model, '<listItem type="bulleted" indent="0">fo[]o</listItem>' );
  129. editor.editing.view.fire( 'delete', domEvtDataStub );
  130. sinon.assert.notCalled( editor.execute );
  131. } );
  132. it( 'should not execute outdentList command when selection is not on first position', () => {
  133. const domEvtDataStub = { preventDefault() {}, direction: 'backward' };
  134. sinon.spy( editor, 'execute' );
  135. setData( model, '<listItem type="bulleted" indent="0">fo[]o</listItem>' );
  136. editor.editing.view.fire( 'delete', domEvtDataStub );
  137. sinon.assert.notCalled( editor.execute );
  138. } );
  139. it( 'should outdent list when previous element is nested in block quote', () => {
  140. const domEvtDataStub = { preventDefault() {}, direction: 'backward' };
  141. sinon.spy( editor, 'execute' );
  142. setData( model, '<blockQuote><paragraph>x</paragraph></blockQuote><listItem type="bulleted" indent="0">[]foo</listItem>' );
  143. editor.editing.view.fire( 'delete', domEvtDataStub );
  144. expect( editor.execute.calledWithExactly( 'outdentList' ) );
  145. } );
  146. it( 'should outdent list when list is nested in block quote', () => {
  147. const domEvtDataStub = { preventDefault() {}, direction: 'backward' };
  148. sinon.spy( editor, 'execute' );
  149. setData( model, '<paragraph>x</paragraph><blockQuote><listItem type="bulleted" indent="0">[]foo</listItem></blockQuote>' );
  150. editor.editing.view.fire( 'delete', domEvtDataStub );
  151. expect( editor.execute.calledWithExactly( 'outdentList' ) );
  152. } );
  153. } );
  154. describe( 'tab key handling callback', () => {
  155. let domEvtDataStub;
  156. beforeEach( () => {
  157. domEvtDataStub = {
  158. keyCode: getCode( 'Tab' ),
  159. preventDefault: sinon.spy(),
  160. stopPropagation: sinon.spy()
  161. };
  162. sinon.spy( editor, 'execute' );
  163. } );
  164. afterEach( () => {
  165. editor.execute.restore();
  166. } );
  167. it( 'should execute indentList command on tab key', () => {
  168. setData(
  169. model,
  170. '<listItem type="bulleted" indent="0">foo</listItem>' +
  171. '<listItem type="bulleted" indent="0">[]bar</listItem>'
  172. );
  173. editor.editing.view.fire( 'keydown', domEvtDataStub );
  174. expect( editor.execute.calledOnce ).to.be.true;
  175. expect( editor.execute.calledWithExactly( 'indentList' ) ).to.be.true;
  176. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  177. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  178. } );
  179. it( 'should execute outdentList command on Shift+Tab keystroke', () => {
  180. domEvtDataStub.keyCode += getCode( 'Shift' );
  181. setData(
  182. model,
  183. '<listItem type="bulleted" indent="0">foo</listItem>' +
  184. '<listItem type="bulleted" indent="1">[]bar</listItem>'
  185. );
  186. editor.editing.view.fire( 'keydown', domEvtDataStub );
  187. expect( editor.execute.calledOnce ).to.be.true;
  188. expect( editor.execute.calledWithExactly( 'outdentList' ) ).to.be.true;
  189. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  190. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  191. } );
  192. it( 'should not indent if command is disabled', () => {
  193. setData( model, '<listItem type="bulleted" indent="0">[]foo</listItem>' );
  194. editor.editing.view.fire( 'keydown', domEvtDataStub );
  195. expect( editor.execute.called ).to.be.false;
  196. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  197. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  198. } );
  199. it( 'should not indent or outdent if alt+tab is pressed', () => {
  200. domEvtDataStub.keyCode += getCode( 'alt' );
  201. setData(
  202. model,
  203. '<listItem type="bulleted" indent="0">foo</listItem>' +
  204. '<listItem type="bulleted" indent="0">[]bar</listItem>'
  205. );
  206. editor.editing.view.fire( 'keydown', domEvtDataStub );
  207. expect( editor.execute.called ).to.be.false;
  208. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  209. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  210. } );
  211. } );
  212. } );