8
0

indentcodeblockcommand.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 CodeBlockEditing from '../src/codeblockediting';
  6. import IndentCodeBlockCommand from '../src/indentcodeblockcommand';
  7. import AlignmentEditing from '@ckeditor/ckeditor5-alignment/src/alignmentediting';
  8. import BoldEditing from '@ckeditor/ckeditor5-basic-styles/src/bold/boldediting';
  9. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  10. import BlockQuoteEditing from '@ckeditor/ckeditor5-block-quote/src/blockquoteediting';
  11. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  12. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  13. describe( 'IndentCodeBlockCommand', () => {
  14. let editor, model, indentCommand;
  15. beforeEach( () => {
  16. return ModelTestEditor
  17. .create( {
  18. plugins: [ CodeBlockEditing, Paragraph, BlockQuoteEditing, AlignmentEditing, BoldEditing ]
  19. } )
  20. .then( newEditor => {
  21. editor = newEditor;
  22. model = editor.model;
  23. indentCommand = new IndentCodeBlockCommand( editor, 'forward' );
  24. } );
  25. } );
  26. afterEach( () => {
  27. return editor.destroy();
  28. } );
  29. describe( '#isEnabled', () => {
  30. it( 'should be true when the first selected block is a codeBlock #1', () => {
  31. setModelData( model, '<codeBlock language="foo">f[]oo</codeBlock>' );
  32. expect( indentCommand.isEnabled ).to.be.true;
  33. } );
  34. it( 'should be true when the first selected block is a codeBlock #2', () => {
  35. setModelData( model, '<codeBlock language="foo">f[oo</codeBlock><paragraph>ba]r</paragraph>' );
  36. expect( indentCommand.isEnabled ).to.be.true;
  37. } );
  38. it( 'should be false when there is no code block in the selection', () => {
  39. setModelData( model, '<paragraph>foo[]</paragraph>' );
  40. expect( indentCommand.isEnabled ).to.be.false;
  41. } );
  42. it( 'should be false when the selection is not anchored in the code block', () => {
  43. setModelData( model, '<paragraph>f[oo</paragraph><codeBlock language="foo">bar</codeBlock><paragraph>ba]z</paragraph>' );
  44. expect( indentCommand.isEnabled ).to.be.false;
  45. } );
  46. describe( 'config.codeBlock.indentSequence', () => {
  47. it( 'should disable the command when the config is not set', () => {
  48. return ModelTestEditor
  49. .create( {
  50. plugins: [ CodeBlockEditing, Paragraph, BlockQuoteEditing, AlignmentEditing, BoldEditing ],
  51. codeBlock: {
  52. indentSequence: false
  53. }
  54. } )
  55. .then( newEditor => {
  56. const editor = newEditor;
  57. const model = editor.model;
  58. const indentCommand = new IndentCodeBlockCommand( editor, 'forward' );
  59. setModelData( model, '<codeBlock language="foo">[]foo</codeBlock>' );
  60. expect( indentCommand.isEnabled ).to.be.false;
  61. return editor.destroy();
  62. } );
  63. } );
  64. } );
  65. } );
  66. describe( 'execute()', () => {
  67. it( 'should indent when a selection is collapsed in an empty code block', () => {
  68. setModelData( model, '<codeBlock language="foo">[]</codeBlock>' );
  69. indentCommand.execute();
  70. expect( getModelData( model ) ).to.equal( '<codeBlock language="foo"> []</codeBlock>' );
  71. } );
  72. it( 'should indent when a selection is collapsed', () => {
  73. setModelData( model, '<codeBlock language="foo">f[]oo</codeBlock>' );
  74. indentCommand.execute();
  75. expect( getModelData( model ) ).to.equal( '<codeBlock language="foo">f []oo</codeBlock>' );
  76. } );
  77. it( 'should indent a whole line when a selection is expanded', () => {
  78. setModelData( model, '<codeBlock language="foo">f[o]o</codeBlock>' );
  79. indentCommand.execute();
  80. expect( getModelData( model ) ).to.equal( '<codeBlock language="foo"> f[o]o</codeBlock>' );
  81. } );
  82. it( 'should indent multiple lines when a selection is expanded', () => {
  83. setModelData( model, '<codeBlock language="foo">f[oo<softBreak></softBreak>b]ar</codeBlock>' );
  84. indentCommand.execute();
  85. expect( getModelData( model ) ).to.equal( '<codeBlock language="foo"> f[oo<softBreak></softBreak> b]ar</codeBlock>' );
  86. } );
  87. it( 'should append the indentation to the line\'s leading white spaces (#1)', () => {
  88. setModelData( model, '<codeBlock language="foo">[]foo</codeBlock>' );
  89. // <codeBlock language="foo"> []foo</codeBlock>
  90. model.change( writer => {
  91. writer.insertText( ' ', model.document.getRoot().getChild( 0 ) );
  92. } );
  93. indentCommand.execute();
  94. expect( getModelData( model ) ).to.equal( '<codeBlock language="foo"> []foo</codeBlock>' );
  95. } );
  96. it( 'should append the indentation to the line\'s leading white spaces (#2)', () => {
  97. setModelData( model, '<codeBlock language="foo">f[oo<softBreak></softBreak>b]ar</codeBlock>' );
  98. // <codeBlock language="foo"> f[oo<softBreak></softBreak> b]ar</codeBlock>
  99. model.change( writer => {
  100. writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 4 );
  101. writer.insertText( ' ', model.document.getRoot().getChild( 0 ), 0 );
  102. } );
  103. indentCommand.execute();
  104. expect( getModelData( model ) ).to.equal(
  105. '<codeBlock language="foo"> f[oo<softBreak></softBreak> b]ar</codeBlock>' );
  106. } );
  107. describe( 'config.codeBlock.indentSequence', () => {
  108. it( 'should be used when indenting', () => {
  109. return ModelTestEditor
  110. .create( {
  111. plugins: [ CodeBlockEditing, Paragraph, BlockQuoteEditing, AlignmentEditing, BoldEditing ],
  112. codeBlock: {
  113. indentSequence: ' '
  114. }
  115. } )
  116. .then( newEditor => {
  117. const editor = newEditor;
  118. const model = editor.model;
  119. const indentCommand = new IndentCodeBlockCommand( editor, 'forward' );
  120. setModelData( model, '<codeBlock language="foo">f[o]o</codeBlock>' );
  121. indentCommand.execute();
  122. expect( getModelData( model ) ).to.equal( '<codeBlock language="foo"> f[o]o</codeBlock>' );
  123. return editor.destroy();
  124. } );
  125. } );
  126. } );
  127. } );
  128. } );