alignmentediting.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import AlignmentEditing from '../src/alignmentediting';
  6. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  7. import ImageCaptionEngine from '@ckeditor/ckeditor5-image/src/imagecaption/imagecaptionengine';
  8. import ListEngine from '@ckeditor/ckeditor5-list/src/listengine';
  9. import HeadingEngine from '@ckeditor/ckeditor5-heading/src/headingengine';
  10. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  11. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  12. import { eventNameToConsumableType } from '@ckeditor/ckeditor5-engine/src/conversion/model-to-view-converters';
  13. import AlignmentCommand from '../src/alignmentcommand';
  14. describe( 'AlignmentEditing', () => {
  15. let editor, model;
  16. beforeEach( () => {
  17. return VirtualTestEditor
  18. .create( {
  19. plugins: [ AlignmentEditing, Paragraph ]
  20. } )
  21. .then( newEditor => {
  22. editor = newEditor;
  23. model = editor.model;
  24. } );
  25. } );
  26. afterEach( () => {
  27. editor.destroy();
  28. } );
  29. it( 'adds alignment commands', () => {
  30. expect( editor.commands.get( 'alignLeft' ) ).to.be.instanceOf( AlignmentCommand );
  31. expect( editor.commands.get( 'alignRight' ) ).to.be.instanceOf( AlignmentCommand );
  32. expect( editor.commands.get( 'alignCenter' ) ).to.be.instanceOf( AlignmentCommand );
  33. expect( editor.commands.get( 'alignJustify' ) ).to.be.instanceOf( AlignmentCommand );
  34. } );
  35. it( 'allows for alignment in $blocks', () => {
  36. expect( model.schema.checkAttribute( [ '$root', '$block' ], 'alignment' ) ).to.be.true;
  37. } );
  38. describe( 'integration', () => {
  39. beforeEach( () => {
  40. return VirtualTestEditor
  41. .create( {
  42. plugins: [ AlignmentEditing, ImageCaptionEngine, Paragraph, ListEngine, HeadingEngine ]
  43. } )
  44. .then( newEditor => {
  45. editor = newEditor;
  46. model = editor.model;
  47. } );
  48. } );
  49. it( 'is allowed on paragraph', () => {
  50. expect( model.schema.checkAttribute( [ '$root', 'paragraph' ], 'alignment' ) ).to.be.true;
  51. } );
  52. it( 'is allowed on listItem', () => {
  53. expect( model.schema.checkAttribute( [ '$root', 'listItem' ], 'type' ) ).to.be.true;
  54. expect( model.schema.checkAttribute( [ '$root', 'listItem' ], 'indent' ) ).to.be.true;
  55. expect( model.schema.checkAttribute( [ '$root', 'listItem' ], 'alignment' ) ).to.be.true;
  56. } );
  57. it( 'is allowed on heading', () => {
  58. expect( model.schema.checkAttribute( [ '$root', 'heading1' ], 'alignment' ) ).to.be.true;
  59. } );
  60. it( 'is disallowed on caption', () => {
  61. expect( model.schema.checkAttribute( [ '$root', 'image', 'caption' ], 'alignment' ) ).to.be.false;
  62. } );
  63. } );
  64. describe( 'alignLeft', () => {
  65. it( 'adds converters to the data pipeline', () => {
  66. const data = '<p style="text-align:left;">x</p>';
  67. editor.setData( data );
  68. expect( getModelData( model ) ).to.equal( '<paragraph>[]x</paragraph>' );
  69. expect( editor.getData() ).to.equal( '<p>x</p>' );
  70. } );
  71. it( 'adds a converter to the view pipeline for removing attribute', () => {
  72. setModelData( model, '<paragraph alignment="center">[]x</paragraph>' );
  73. expect( editor.getData() ).to.equal( '<p style="text-align:center;">x</p>' );
  74. const command = editor.commands.get( 'alignLeft' );
  75. command.execute();
  76. expect( editor.getData() ).to.equal( '<p>x</p>' );
  77. } );
  78. } );
  79. describe( 'alignCenter', () => {
  80. it( 'adds converters to the data pipeline', () => {
  81. const data = '<p style="text-align:center;">x</p>';
  82. editor.setData( data );
  83. expect( getModelData( model ) ).to.equal( '<paragraph alignment="center">[]x</paragraph>' );
  84. expect( editor.getData() ).to.equal( data );
  85. } );
  86. it( 'adds a converter to the view pipeline', () => {
  87. setModelData( model, '<paragraph alignment="center">[]x</paragraph>' );
  88. expect( editor.getData() ).to.equal( '<p style="text-align:center;">x</p>' );
  89. } );
  90. it( 'adds a converter to the view pipeline for changing attribute', () => {
  91. setModelData( model, '<paragraph alignment="right">[]x</paragraph>' );
  92. expect( editor.getData() ).to.equal( '<p style="text-align:right;">x</p>' );
  93. const command = editor.commands.get( 'alignCenter' );
  94. command.execute();
  95. expect( editor.getData() ).to.equal( '<p style="text-align:center;">x</p>' );
  96. } );
  97. } );
  98. describe( 'alignRight', () => {
  99. it( 'adds converters to the data pipeline', () => {
  100. const data = '<p style="text-align:right;">x</p>';
  101. editor.setData( data );
  102. expect( getModelData( model ) ).to.equal( '<paragraph alignment="right">[]x</paragraph>' );
  103. expect( editor.getData() ).to.equal( data );
  104. } );
  105. it( 'adds a converter to the view pipeline', () => {
  106. setModelData( model, '<paragraph alignment="right">[]x</paragraph>' );
  107. expect( editor.getData() ).to.equal( '<p style="text-align:right;">x</p>' );
  108. } );
  109. } );
  110. describe( 'alignJustify', () => {
  111. it( 'adds converters to the data pipeline', () => {
  112. const data = '<p style="text-align:justify;">x</p>';
  113. editor.setData( data );
  114. expect( getModelData( model ) ).to.equal( '<paragraph alignment="justify">[]x</paragraph>' );
  115. expect( editor.getData() ).to.equal( data );
  116. } );
  117. it( 'adds a converter to the view pipeline', () => {
  118. setModelData( model, '<paragraph alignment="justify">[]x</paragraph>' );
  119. expect( editor.getData() ).to.equal( '<p style="text-align:justify;">x</p>' );
  120. } );
  121. } );
  122. describe( 'should be extensible', () => {
  123. it( 'converters in the data pipeline', () => {
  124. blockDefaultConversion( editor.data.modelToView );
  125. blockDefaultConversion( editor.editing.modelToView );
  126. const data = '<p style="text-align:justify;">x</p>';
  127. editor.setData( data );
  128. expect( getModelData( model ) ).to.equal( '<paragraph alignment="justify">[]x</paragraph>' );
  129. expect( editor.getData() ).to.equal( '<p>x</p>' );
  130. const command = editor.commands.get( 'alignLeft' );
  131. command.execute();
  132. expect( editor.getData() ).to.equal( '<p>x</p>' );
  133. } );
  134. } );
  135. describe( 'should work with broken styles', () => {
  136. it( 'should ignore empty style', () => {
  137. const data = '<p style="text-align:">x</p>';
  138. editor.setData( data );
  139. expect( getModelData( model ) ).to.equal( '<paragraph>[]x</paragraph>' );
  140. expect( editor.getData() ).to.equal( '<p>x</p>' );
  141. } );
  142. it( 'should ignore not known style', () => {
  143. const data = '<p style="text-align:unset;">x</p>';
  144. editor.setData( data );
  145. expect( getModelData( model ) ).to.equal( '<paragraph>[]x</paragraph>' );
  146. expect( editor.getData() ).to.equal( '<p>x</p>' );
  147. } );
  148. } );
  149. describe( 'config', () => {
  150. describe( 'styles', () => {
  151. describe( 'default value', () => {
  152. it( 'should be set', () => {
  153. expect( editor.config.get( 'alignment.styles' ) ).to.deep.equal( [ 'left', 'right', 'center', 'justify' ] );
  154. } );
  155. } );
  156. it( 'should customize commands', () => {
  157. return VirtualTestEditor
  158. .create( {
  159. alignment: { styles: [ 'left', 'right' ] },
  160. plugins: [ AlignmentEditing, Paragraph ]
  161. } )
  162. .then( editor => {
  163. expect( editor.commands.get( 'alignLeft' ), 'adds alignLeft' ).to.be.instanceof( AlignmentCommand );
  164. expect( editor.commands.get( 'alignRight' ), 'adds alignLeft' ).to.be.instanceof( AlignmentCommand );
  165. expect( editor.commands.get( 'alignCenter' ), 'does not add alignCenter' ).to.be.undefined;
  166. expect( editor.commands.get( 'alignJustify' ), 'does not add alignJustify' ).to.be.undefined;
  167. } );
  168. } );
  169. } );
  170. } );
  171. } );
  172. function blockDefaultConversion( dispatcher ) {
  173. dispatcher.on( 'attribute:alignment', ( evt, data, consumable ) => {
  174. consumable.consume( data.item, eventNameToConsumableType( evt.name ) );
  175. }, { 'priority': 'high' } );
  176. }