alignmentediting.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /**
  2. * @license Copyright (c) 2003-2017, 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.check( { name: '$block', inside: '$root', attributes: '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.check( { name: 'paragraph', attributes: 'alignment' } ) ).to.be.true;
  51. } );
  52. it( 'is allowed on listItem', () => {
  53. expect( model.schema.check( { name: 'listItem', attributes: [ 'type', 'indent', 'alignment' ] } ) ).to.be.true;
  54. } );
  55. it( 'is allowed on heading', () => {
  56. expect( model.schema.check( { name: 'heading1', attributes: 'alignment' } ) ).to.be.true;
  57. } );
  58. it( 'is disallowed on caption', () => {
  59. expect( model.schema.check( { name: 'caption', attributes: 'alignment' } ) ).to.be.false;
  60. } );
  61. } );
  62. describe( 'alignLeft', () => {
  63. it( 'adds converters to the data pipeline', () => {
  64. const data = '<p style="text-align:left;">x</p>';
  65. editor.setData( data );
  66. expect( getModelData( model ) ).to.equal( '<paragraph>[]x</paragraph>' );
  67. expect( editor.getData() ).to.equal( '<p>x</p>' );
  68. } );
  69. it( 'adds a converter to the view pipeline for removing attribute', () => {
  70. setModelData( model, '<paragraph alignment="center">[]x</paragraph>' );
  71. expect( editor.getData() ).to.equal( '<p style="text-align:center;">x</p>' );
  72. const command = editor.commands.get( 'alignLeft' );
  73. command.execute();
  74. expect( editor.getData() ).to.equal( '<p>x</p>' );
  75. } );
  76. } );
  77. describe( 'alignCenter', () => {
  78. it( 'adds converters to the data pipeline', () => {
  79. const data = '<p style="text-align:center;">x</p>';
  80. editor.setData( data );
  81. expect( getModelData( model ) ).to.equal( '<paragraph alignment="center">[]x</paragraph>' );
  82. expect( editor.getData() ).to.equal( data );
  83. } );
  84. it( 'adds a converter to the view pipeline', () => {
  85. setModelData( model, '<paragraph alignment="center">[]x</paragraph>' );
  86. expect( editor.getData() ).to.equal( '<p style="text-align:center;">x</p>' );
  87. } );
  88. it( 'adds a converter to the view pipeline for changing attribute', () => {
  89. setModelData( model, '<paragraph alignment="right">[]x</paragraph>' );
  90. expect( editor.getData() ).to.equal( '<p style="text-align:right;">x</p>' );
  91. const command = editor.commands.get( 'alignCenter' );
  92. command.execute();
  93. expect( editor.getData() ).to.equal( '<p style="text-align:center;">x</p>' );
  94. } );
  95. } );
  96. describe( 'alignRight', () => {
  97. it( 'adds converters to the data pipeline', () => {
  98. const data = '<p style="text-align:right;">x</p>';
  99. editor.setData( data );
  100. expect( getModelData( model ) ).to.equal( '<paragraph alignment="right">[]x</paragraph>' );
  101. expect( editor.getData() ).to.equal( data );
  102. } );
  103. it( 'adds a converter to the view pipeline', () => {
  104. setModelData( model, '<paragraph alignment="right">[]x</paragraph>' );
  105. expect( editor.getData() ).to.equal( '<p style="text-align:right;">x</p>' );
  106. } );
  107. } );
  108. describe( 'alignJustify', () => {
  109. it( 'adds converters to the data pipeline', () => {
  110. const data = '<p style="text-align:justify;">x</p>';
  111. editor.setData( data );
  112. expect( getModelData( model ) ).to.equal( '<paragraph alignment="justify">[]x</paragraph>' );
  113. expect( editor.getData() ).to.equal( data );
  114. } );
  115. it( 'adds a converter to the view pipeline', () => {
  116. setModelData( model, '<paragraph alignment="justify">[]x</paragraph>' );
  117. expect( editor.getData() ).to.equal( '<p style="text-align:justify;">x</p>' );
  118. } );
  119. } );
  120. describe( 'should be extensible', () => {
  121. it( 'converters in the data pipeline', () => {
  122. blockDefaultConversion( editor.data.modelToView );
  123. blockDefaultConversion( editor.editing.modelToView );
  124. const data = '<p style="text-align:justify;">x</p>';
  125. editor.setData( data );
  126. expect( getModelData( model ) ).to.equal( '<paragraph alignment="justify">[]x</paragraph>' );
  127. expect( editor.getData() ).to.equal( '<p>x</p>' );
  128. const command = editor.commands.get( 'alignLeft' );
  129. command.execute();
  130. expect( editor.getData() ).to.equal( '<p>x</p>' );
  131. } );
  132. } );
  133. describe( 'should work with broken styles', () => {
  134. it( 'should ignore empty style', () => {
  135. const data = '<p style="text-align:">x</p>';
  136. editor.setData( data );
  137. expect( getModelData( model ) ).to.equal( '<paragraph>[]x</paragraph>' );
  138. expect( editor.getData() ).to.equal( '<p>x</p>' );
  139. } );
  140. it( 'should ignore not known style', () => {
  141. const data = '<p style="text-align:unset;">x</p>';
  142. editor.setData( data );
  143. expect( getModelData( model ) ).to.equal( '<paragraph>[]x</paragraph>' );
  144. expect( editor.getData() ).to.equal( '<p>x</p>' );
  145. } );
  146. } );
  147. describe( 'config', () => {
  148. describe( 'styles', () => {
  149. describe( 'default value', () => {
  150. it( 'should be set', () => {
  151. expect( editor.config.get( 'alignment.styles' ) ).to.deep.equal( [ 'left', 'right', 'center', 'justify' ] );
  152. } );
  153. } );
  154. it( 'should customize commands', () => {
  155. return VirtualTestEditor
  156. .create( {
  157. alignment: { styles: [ 'left', 'right' ] },
  158. plugins: [ AlignmentEditing, Paragraph ]
  159. } )
  160. .then( editor => {
  161. expect( editor.commands.get( 'alignLeft' ), 'adds alignLeft' ).to.be.instanceof( AlignmentCommand );
  162. expect( editor.commands.get( 'alignRight' ), 'adds alignLeft' ).to.be.instanceof( AlignmentCommand );
  163. expect( editor.commands.get( 'alignCenter' ), 'does not add alignCenter' ).to.be.undefined;
  164. expect( editor.commands.get( 'alignJustify' ), 'does not add alignJustify' ).to.be.undefined;
  165. } );
  166. } );
  167. } );
  168. } );
  169. } );
  170. function blockDefaultConversion( dispatcher ) {
  171. dispatcher.on(
  172. 'addAttribute:alignment',
  173. ( evt, data, consumable ) => consumable.consume( data.item, eventNameToConsumableType( evt.name ) ),
  174. { 'priority': 'high' }
  175. );
  176. dispatcher.on(
  177. 'changeAttribute:alignment',
  178. ( evt, data, consumable ) => consumable.consume( data.item, eventNameToConsumableType( evt.name ) ),
  179. { 'priority': 'high' }
  180. );
  181. dispatcher.on(
  182. 'removeAttribute:alignment',
  183. ( evt, data, consumable ) => consumable.consume( data.item, eventNameToConsumableType( evt.name ) ),
  184. { 'priority': 'high' }
  185. );
  186. }