paragraph.js 6.0 KB

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