8
0

alignmentediting.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. /**
  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. static get pluginName() {
  21. return 'AlignmentEditing';
  22. }
  23. /**
  24. * @inheritDoc
  25. */
  26. constructor( editor ) {
  27. super( editor );
  28. editor.config.define( 'alignment', {
  29. options: [ ...supportedOptions ]
  30. } );
  31. }
  32. /**
  33. * @inheritDoc
  34. */
  35. init() {
  36. const editor = this.editor;
  37. const locale = editor.locale;
  38. const schema = editor.model.schema;
  39. // Filter out unsupported options.
  40. const enabledOptions = editor.config.get( 'alignment.options' ).filter( isSupported );
  41. // Allow alignment attribute on all blocks.
  42. schema.extend( '$block', { allowAttributes: 'alignment' } );
  43. editor.model.schema.setAttributeProperties( 'alignment', { isFormatting: true } );
  44. const definition = _buildDefinition( enabledOptions.filter( option => !isDefault( option, locale ) ) );
  45. editor.conversion.attributeToAttribute( definition );
  46. editor.commands.add( 'alignment', new AlignmentCommand( editor ) );
  47. }
  48. }
  49. // Utility function responsible for building converter definition.
  50. // @private
  51. function _buildDefinition( options ) {
  52. const definition = {
  53. model: {
  54. key: 'alignment',
  55. values: options.slice()
  56. },
  57. view: {}
  58. };
  59. for ( const option of options ) {
  60. definition.view[ option ] = {
  61. key: 'style',
  62. value: {
  63. 'text-align': option
  64. }
  65. };
  66. }
  67. return definition;
  68. }