alignmentediting.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module alignment/alignmentediting
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import AlignmentCommand from './alignmentcommand';
  10. import { isDefault, isSupported, supportedOptions } from './utils';
  11. /**
  12. * The alignment editing feature. It introduces the {@link module:alignment/alignmentcommand~AlignmentCommand command} and adds
  13. * the `alignment` attribute for block elements in the {@link module:engine/model/model~Model model}.
  14. * @extends module:core/plugin~Plugin
  15. */
  16. export default class AlignmentEditing extends Plugin {
  17. /**
  18. * @inheritDoc
  19. */
  20. constructor( editor ) {
  21. super( editor );
  22. editor.config.define( 'alignment', {
  23. options: [ ...supportedOptions ]
  24. } );
  25. }
  26. /**
  27. * @inheritDoc
  28. */
  29. init() {
  30. const editor = this.editor;
  31. const schema = editor.model.schema;
  32. // Filter out unsupported options.
  33. const enabledOptions = editor.config.get( 'alignment.options' ).filter( isSupported );
  34. // Allow alignment attribute on all blocks.
  35. schema.extend( '$block', { allowAttributes: 'alignment' } );
  36. const definition = _buildDefinition( enabledOptions.filter( option => !isDefault( option ) ) );
  37. editor.conversion.attributeToAttribute( definition );
  38. editor.commands.add( 'alignment', new AlignmentCommand( editor ) );
  39. }
  40. }
  41. // Utility function responsible for building converter definition.
  42. // @private
  43. function _buildDefinition( options ) {
  44. const definition = {
  45. model: {
  46. key: 'alignment',
  47. values: options.slice()
  48. },
  49. view: {}
  50. };
  51. for ( const option of options ) {
  52. definition.view[ option ] = {
  53. key: 'style',
  54. value: {
  55. 'text-align': option
  56. }
  57. };
  58. }
  59. return definition;
  60. }