subscriptediting.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 basic-styles/subscript/subscriptediting
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import AttributeCommand from '../attributecommand';
  10. const SUBSCRIPT = 'subscript';
  11. /**
  12. * The subscript editing feature.
  13. *
  14. * It registers the `sub` command and introduces the `sub` attribute in the model which renders to the view
  15. * as a `<sub>` element.
  16. *
  17. * @extends module:core/plugin~Plugin
  18. */
  19. export default class SubscriptEditing extends Plugin {
  20. /**
  21. * @inheritDoc
  22. */
  23. static get pluginName() {
  24. return 'SubscriptEditing';
  25. }
  26. /**
  27. * @inheritDoc
  28. */
  29. init() {
  30. const editor = this.editor;
  31. // Allow sub attribute on text nodes.
  32. editor.model.schema.extend( '$text', { allowAttributes: SUBSCRIPT } );
  33. editor.model.schema.setAttributeProperties( SUBSCRIPT, {
  34. isFormatting: true,
  35. copyOnEnter: true
  36. } );
  37. // Build converter from model to view for data and editing pipelines.
  38. editor.conversion.attributeToElement( {
  39. model: SUBSCRIPT,
  40. view: 'sub',
  41. upcastAlso: [
  42. {
  43. styles: {
  44. 'vertical-align': 'sub'
  45. }
  46. }
  47. ]
  48. } );
  49. // Create sub command.
  50. editor.commands.add( SUBSCRIPT, new AttributeCommand( editor, SUBSCRIPT ) );
  51. }
  52. }