paragraph.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. const data = editor.data;
  39. editor.commands.add( 'paragraph', new ParagraphCommand( editor ) );
  40. editor.commands.add( 'insertParagraph', new InsertParagraphCommand( editor ) );
  41. // Schema.
  42. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  43. editor.conversion.elementToElement( { model: 'paragraph', view: 'p' } );
  44. // Content autoparagraphing. --------------------------------------------------
  45. // Handles element which has not been converted by any plugin and checks if it would be converted if
  46. // we wrap it in a paragraph or change it to a paragraph.
  47. editor.conversion.for( 'upcast' ).elementToElement( {
  48. model: ( viewElement, modelWriter ) => {
  49. if ( !Paragraph.paragraphLikeElements.has( viewElement.name ) ) {
  50. return null;
  51. }
  52. // Do not auto-paragraph empty elements.
  53. if ( viewElement.isEmpty ) {
  54. return null;
  55. }
  56. return modelWriter.createElement( 'paragraph' );
  57. },
  58. converterPriority: 'low'
  59. } );
  60. data.upcastDispatcher.on( 'element', ( evt, data, conversionApi ) => {
  61. // Do not try auto-paragraphing if the element was already converted.
  62. if ( !conversionApi.consumable.test( data.viewItem, { name: data.viewItem.name } ) ) {
  63. return;
  64. }
  65. // If the element is not paragraph-like try wrapping it in a paragraph.
  66. if ( isParagraphable( data.viewItem, data.modelCursor, conversionApi.schema ) ) {
  67. Object.assign( data, wrapInParagraph( data.viewItem, data.modelCursor, conversionApi ) );
  68. }
  69. }, { priority: 'low' } );
  70. // Handles not converted text nodes and checks if would be converted if we wraps then by a paragraph.
  71. data.upcastDispatcher.on( 'text', ( evt, data, conversionApi ) => {
  72. // When node is already converted then do nothing.
  73. if ( data.modelRange ) {
  74. return;
  75. }
  76. if ( isParagraphable( data.viewItem, data.modelCursor, conversionApi.schema ) ) {
  77. Object.assign( data, wrapInParagraph( data.viewItem, data.modelCursor, conversionApi ) );
  78. }
  79. }, { priority: 'lowest' } );
  80. // Empty roots autoparagraphing. -----------------------------------------------
  81. // Post-fixer which takes care of adding empty paragraph elements to empty roots.
  82. // Besides fixing content on #changesDone we also need to handle editor.data#ready event because
  83. // if initial data is empty or setData() wasn't even called there will be no #change fired.
  84. model.document.registerPostFixer( writer => this._autoparagraphEmptyRoots( writer ) );
  85. editor.data.on( 'ready', () => {
  86. model.enqueueChange( 'transparent', writer => this._autoparagraphEmptyRoots( writer ) );
  87. }, { priority: 'lowest' } );
  88. }
  89. /**
  90. * Fixes all empty roots.
  91. *
  92. * @private
  93. * @returns {Boolean} `true` if any change has been applied, `false` otherwise.
  94. */
  95. _autoparagraphEmptyRoots( writer ) {
  96. const model = this.editor.model;
  97. for ( const rootName of model.document.getRootNames() ) {
  98. const root = model.document.getRoot( rootName );
  99. if ( root.isEmpty && root.rootName != '$graveyard' ) {
  100. // If paragraph element is allowed in the root, create paragraph element.
  101. if ( model.schema.checkChild( root, 'paragraph' ) ) {
  102. writer.insertElement( 'paragraph', root );
  103. return true;
  104. }
  105. }
  106. }
  107. }
  108. }
  109. /**
  110. * A list of element names which should be treated by the autoparagraphing algorithms as
  111. * paragraph-like. This means that e.g. the following content:
  112. *
  113. * <h1>Foo</h1>
  114. * <table>
  115. * <tr>
  116. * <td>X</td>
  117. * <td>
  118. * <ul>
  119. * <li>Y</li>
  120. * <li>Z</li>
  121. * </ul>
  122. * </td>
  123. * </tr>
  124. * </table>
  125. *
  126. * contains five paragraph-like elements: `<h1>`, two `<td>`s and two `<li>`s.
  127. * Hence, if none of the features is going to convert those elements the above content will be automatically handled
  128. * by the paragraph feature and converted to:
  129. *
  130. * <p>Foo</p>
  131. * <p>X</p>
  132. * <p>Y</p>
  133. * <p>Z</p>
  134. *
  135. * Note: The `<td>` containing two `<li>` elements was ignored as the innermost paragraph-like elements
  136. * have a priority upon conversion.
  137. *
  138. * @member {Set.<String>} module:paragraph/paragraph~Paragraph.paragraphLikeElements
  139. */
  140. Paragraph.paragraphLikeElements = new Set( [
  141. 'blockquote',
  142. 'dd',
  143. 'div',
  144. 'dt',
  145. 'h1',
  146. 'h2',
  147. 'h3',
  148. 'h4',
  149. 'h5',
  150. 'h6',
  151. 'li',
  152. 'p',
  153. 'td'
  154. ] );
  155. function wrapInParagraph( input, position, conversionApi ) {
  156. const paragraph = conversionApi.writer.createElement( 'paragraph' );
  157. conversionApi.writer.insert( paragraph, position );
  158. return conversionApi.convertItem( input, conversionApi.writer.createPositionAt( paragraph, 0 ) );
  159. }
  160. function isParagraphable( node, position, schema ) {
  161. const context = schema.createContext( position );
  162. // When paragraph is allowed in this context...
  163. if ( !schema.checkChild( context, 'paragraph' ) ) {
  164. return false;
  165. }
  166. // And a node would be allowed in this paragraph...
  167. if ( !schema.checkChild( context.push( 'paragraph' ), node ) ) {
  168. return false;
  169. }
  170. return true;
  171. }