paragraph.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 paragraph/paragraph
  7. */
  8. import ParagraphCommand from './paragraphcommand';
  9. import InsertParagraphCommand from './insertparagraphcommand';
  10. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  11. /**
  12. * The paragraph feature for the editor.
  13. *
  14. * It introduces the `<paragraph>` element in the model which renders as a `<p>` element in the DOM and data.
  15. *
  16. * It also brings two editors commands:
  17. *
  18. * * The {@link module:paragraph/paragraphcommand~ParagraphCommand `'paragraph'`} command that converts all
  19. * blocks in the model selection into paragraphs.
  20. * * The {@link module:paragraph/insertparagraphcommand~InsertParagraphCommand `'insertParagraph'`} command
  21. * that inserts a new paragraph at a specified location in the model.
  22. *
  23. * @extends module:core/plugin~Plugin
  24. */
  25. export default class Paragraph extends Plugin {
  26. /**
  27. * @inheritDoc
  28. */
  29. static get pluginName() {
  30. return 'Paragraph';
  31. }
  32. /**
  33. * @inheritDoc
  34. */
  35. init() {
  36. const editor = this.editor;
  37. const model = editor.model;
  38. editor.commands.add( 'paragraph', new ParagraphCommand( editor ) );
  39. editor.commands.add( 'insertParagraph', new InsertParagraphCommand( editor ) );
  40. // Schema.
  41. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  42. editor.conversion.elementToElement( { model: 'paragraph', view: 'p' } );
  43. // Conversion for paragraph-like elements which has not been converted by any plugin.
  44. editor.conversion.for( 'upcast' ).elementToElement( {
  45. model: ( viewElement, modelWriter ) => {
  46. if ( !Paragraph.paragraphLikeElements.has( viewElement.name ) ) {
  47. return null;
  48. }
  49. // Do not auto-paragraph empty elements.
  50. if ( viewElement.isEmpty ) {
  51. return null;
  52. }
  53. return modelWriter.createElement( 'paragraph' );
  54. },
  55. view: /.+/,
  56. converterPriority: 'low'
  57. } );
  58. // Empty roots autoparagraphing. -----------------------------------------------
  59. // Post-fixer which takes care of adding empty paragraph elements to empty roots.
  60. // Besides fixing content on #changesDone we also need to handle editor.data#ready event because
  61. // if initial data is empty or setData() wasn't even called there will be no #change fired.
  62. model.document.registerPostFixer( writer => this._autoparagraphEmptyRoots( writer ) );
  63. editor.data.on( 'ready', () => {
  64. model.enqueueChange( 'transparent', writer => this._autoparagraphEmptyRoots( writer ) );
  65. }, { priority: 'lowest' } );
  66. }
  67. /**
  68. * Fixes all empty roots.
  69. *
  70. * @private
  71. * @returns {Boolean} `true` if any change has been applied, `false` otherwise.
  72. */
  73. _autoparagraphEmptyRoots( writer ) {
  74. const model = this.editor.model;
  75. for ( const rootName of model.document.getRootNames() ) {
  76. const root = model.document.getRoot( rootName );
  77. if ( root.isEmpty && root.rootName != '$graveyard' ) {
  78. // If paragraph element is allowed in the root, create paragraph element.
  79. if ( model.schema.checkChild( root, 'paragraph' ) ) {
  80. writer.insertElement( 'paragraph', root );
  81. return true;
  82. }
  83. }
  84. }
  85. }
  86. }
  87. /**
  88. * A list of element names which should be treated by the autoparagraphing algorithms as
  89. * paragraph-like. This means that e.g. the following content:
  90. *
  91. * <h1>Foo</h1>
  92. * <table>
  93. * <tr>
  94. * <td>X</td>
  95. * <td>
  96. * <ul>
  97. * <li>Y</li>
  98. * <li>Z</li>
  99. * </ul>
  100. * </td>
  101. * </tr>
  102. * </table>
  103. *
  104. * contains five paragraph-like elements: `<h1>`, two `<td>`s and two `<li>`s.
  105. * Hence, if none of the features is going to convert those elements the above content will be automatically handled
  106. * by the paragraph feature and converted to:
  107. *
  108. * <p>Foo</p>
  109. * <p>X</p>
  110. * <p>Y</p>
  111. * <p>Z</p>
  112. *
  113. * Note: The `<td>` containing two `<li>` elements was ignored as the innermost paragraph-like elements
  114. * have a priority upon conversion.
  115. *
  116. * @member {Set.<String>} module:paragraph/paragraph~Paragraph.paragraphLikeElements
  117. */
  118. Paragraph.paragraphLikeElements = new Set( [
  119. 'blockquote',
  120. 'dd',
  121. 'div',
  122. 'dt',
  123. 'h1',
  124. 'h2',
  125. 'h3',
  126. 'h4',
  127. 'h5',
  128. 'h6',
  129. 'li',
  130. 'p',
  131. 'td',
  132. 'th'
  133. ] );