|
|
@@ -40,7 +40,6 @@ export default class Paragraph extends Plugin {
|
|
|
init() {
|
|
|
const editor = this.editor;
|
|
|
const model = editor.model;
|
|
|
- const data = editor.data;
|
|
|
|
|
|
editor.commands.add( 'paragraph', new ParagraphCommand( editor ) );
|
|
|
editor.commands.add( 'insertParagraph', new InsertParagraphCommand( editor ) );
|
|
|
@@ -50,10 +49,7 @@ export default class Paragraph extends Plugin {
|
|
|
|
|
|
editor.conversion.elementToElement( { model: 'paragraph', view: 'p' } );
|
|
|
|
|
|
- // Content autoparagraphing. --------------------------------------------------
|
|
|
-
|
|
|
- // Handles element which has not been converted by any plugin and checks if it would be converted if
|
|
|
- // we wrap it in a paragraph or change it to a paragraph.
|
|
|
+ // Conversion for paragraph-like elements which has not been converted by any plugin.
|
|
|
editor.conversion.for( 'upcast' ).elementToElement( {
|
|
|
model: ( viewElement, { writer } ) => {
|
|
|
if ( !Paragraph.paragraphLikeElements.has( viewElement.name ) ) {
|
|
|
@@ -70,64 +66,6 @@ export default class Paragraph extends Plugin {
|
|
|
view: /.+/,
|
|
|
converterPriority: 'low'
|
|
|
} );
|
|
|
-
|
|
|
- data.upcastDispatcher.on( 'element', ( evt, data, conversionApi ) => {
|
|
|
- // Do not try auto-paragraphing if the element was already converted.
|
|
|
- if ( !conversionApi.consumable.test( data.viewItem, { name: data.viewItem.name } ) ) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- // If the element is not paragraph-like try wrapping it in a paragraph.
|
|
|
- if ( isParagraphable( data.viewItem, data.modelCursor, conversionApi.schema ) ) {
|
|
|
- Object.assign( data, wrapInParagraph( data.viewItem, data.modelCursor, conversionApi ) );
|
|
|
- }
|
|
|
- }, { priority: 'low' } );
|
|
|
-
|
|
|
- // Handles not converted text nodes and checks if would be converted if we wraps then by a paragraph.
|
|
|
- data.upcastDispatcher.on( 'text', ( evt, data, conversionApi ) => {
|
|
|
- // When node is already converted then do nothing.
|
|
|
- if ( data.modelRange ) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- if ( isParagraphable( data.viewItem, data.modelCursor, conversionApi.schema ) ) {
|
|
|
- Object.assign( data, wrapInParagraph( data.viewItem, data.modelCursor, conversionApi ) );
|
|
|
- }
|
|
|
- }, { priority: 'lowest' } );
|
|
|
-
|
|
|
- // Empty roots autoparagraphing. -----------------------------------------------
|
|
|
-
|
|
|
- // Post-fixer which takes care of adding empty paragraph elements to empty roots.
|
|
|
- // Besides fixing content on #changesDone we also need to handle editor.data#ready event because
|
|
|
- // if initial data is empty or setData() wasn't even called there will be no #change fired.
|
|
|
- model.document.registerPostFixer( writer => this._autoparagraphEmptyRoots( writer ) );
|
|
|
-
|
|
|
- editor.data.on( 'ready', () => {
|
|
|
- model.enqueueChange( 'transparent', writer => this._autoparagraphEmptyRoots( writer ) );
|
|
|
- }, { priority: 'lowest' } );
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Fixes all empty roots.
|
|
|
- *
|
|
|
- * @private
|
|
|
- * @returns {Boolean} `true` if any change has been applied, `false` otherwise.
|
|
|
- */
|
|
|
- _autoparagraphEmptyRoots( writer ) {
|
|
|
- const model = this.editor.model;
|
|
|
-
|
|
|
- for ( const rootName of model.document.getRootNames() ) {
|
|
|
- const root = model.document.getRoot( rootName );
|
|
|
-
|
|
|
- if ( root.isEmpty && root.rootName != '$graveyard' ) {
|
|
|
- // If paragraph element is allowed in the root, create paragraph element.
|
|
|
- if ( model.schema.checkChild( root, 'paragraph' ) ) {
|
|
|
- writer.insertElement( 'paragraph', root );
|
|
|
-
|
|
|
- return true;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -175,28 +113,6 @@ Paragraph.paragraphLikeElements = new Set( [
|
|
|
'h6',
|
|
|
'li',
|
|
|
'p',
|
|
|
- 'td'
|
|
|
+ 'td',
|
|
|
+ 'th'
|
|
|
] );
|
|
|
-
|
|
|
-function wrapInParagraph( input, position, conversionApi ) {
|
|
|
- const paragraph = conversionApi.writer.createElement( 'paragraph' );
|
|
|
-
|
|
|
- conversionApi.writer.insert( paragraph, position );
|
|
|
- return conversionApi.convertItem( input, conversionApi.writer.createPositionAt( paragraph, 0 ) );
|
|
|
-}
|
|
|
-
|
|
|
-function isParagraphable( node, position, schema ) {
|
|
|
- const context = schema.createContext( position );
|
|
|
-
|
|
|
- // When paragraph is allowed in this context...
|
|
|
- if ( !schema.checkChild( context, 'paragraph' ) ) {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- // And a node would be allowed in this paragraph...
|
|
|
- if ( !schema.checkChild( context.push( 'paragraph' ), node ) ) {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- return true;
|
|
|
-}
|