fontsizeediting.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module font/fontsize/fontsizeediting
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import {
  10. modelAttributeToViewAttributeElement,
  11. viewToModelAttribute
  12. } from '@ckeditor/ckeditor5-engine/src/conversion/definition-based-converters';
  13. import FontSizeCommand from './fontsizecommand';
  14. import { normalizeOptions } from './utils';
  15. /**
  16. * The Font Size Editing feature.
  17. *
  18. * @extends module:core/plugin~Plugin
  19. */
  20. export default class FontSizeEditing extends Plugin {
  21. /**
  22. * @inheritDoc
  23. */
  24. constructor( editor ) {
  25. super( editor );
  26. // Define default configuration using named presets.
  27. editor.config.define( 'fontSize', {
  28. options: [
  29. 'tiny',
  30. 'small',
  31. 'normal',
  32. 'big',
  33. 'huge'
  34. ]
  35. } );
  36. // Get configuration
  37. const data = editor.data;
  38. const editing = editor.editing;
  39. // Define view to model conversion.
  40. const options = normalizeOptions( this.editor.config.get( 'fontSize.options' ) ).filter( item => item.model );
  41. for ( const option of options ) {
  42. // Covert view to model.
  43. viewToModelAttribute( 'fontSize', option, [ data.viewToModel ] );
  44. }
  45. // Define model to view conversion.
  46. modelAttributeToViewAttributeElement( 'fontSize', options, [ data.modelToView, editing.modelToView ] );
  47. // Add FontSize command.
  48. editor.commands.add( 'fontSize', new FontSizeCommand( editor ) );
  49. }
  50. /**
  51. * @inheritDoc
  52. */
  53. init() {
  54. const editor = this.editor;
  55. // Allow fontSize attribute on text nodes.
  56. editor.model.schema.extend( '$text', { allowAttributes: 'fontSize' } );
  57. }
  58. }