Преглед на файлове

Renamed Autoformatter to AutoformatEngine.

Maksymilian Barnaś преди 9 години
родител
ревизия
0ec5ccf14a

+ 18 - 6
packages/ckeditor5-autoformat/src/autoformat.js

@@ -3,33 +3,45 @@
  * For licensing, see LICENSE.md.
  */
 
-import Autoformatter from './autoformatter.js';
+import AutoformatEngine from './autoformatengine.js';
+import HeadingEngine from '../heading/headingengine.js';
 import Feature from '../core/feature.js';
 
 export default class Autoformat extends Feature {
+	/**
+	 * @inheritDoc
+	 */
+	static get requires() {
+		return [ AutoformatEngine, HeadingEngine ];
+	}
+
 	init() {
 		const editor = this.editor;
 
+		// I've noticed the commands are set only when they are required.
+		// I guess this is expected behavior, but it makes things harder.
+		// For now I'll require needed commands explicitly.
+
 		if ( editor.commands.has( 'blockquote' ) ) {
-			new Autoformatter( /^> $/, 'blockquote' );
+			new AutoformatEngine( /^> $/, 'blockquote' );
 		}
 
 		if ( editor.commands.has( 'bulletedList' ) ) {
-			new Autoformatter( /^[\*\-] $/, 'bulletedList' );
+			new AutoformatEngine( /^[\*\-] $/, 'bulletedList' );
 		}
 
 		if ( editor.commands.has( 'numberedList' ) ) {
-			new Autoformatter( /^\d+[\.|)]? $/, 'numberedList' ); // "1 A", "1. A", "123 A"
+			new AutoformatEngine( /^\d+[\.|)]? $/, 'numberedList' ); // "1 A", "1. A", "123 A"
 		}
 
 		if ( editor.commands.has( 'heading' ) ) {
-			// The batch must come from the Autoformatter, because it should be the same batch which is later
+			// The batch must come from the AutoformatEngine, because it should be the same batch which is later
 			// used by the command. E.g.:
 			//
 			// <p>## ^</p> -> <heading2>^</heading2> (two steps: executing heading command + removing the text prefix)
 			//
 			// After ctrl+z: <p>## ^</p> (so undo two steps)
-			new Autoformatter( /^(#{1,3}) $/, ( batch, regexpMatch ) => {
+			new AutoformatEngine( /^(#{1,3}) $/, ( batch, regexpMatch ) => {
 				// TODO The heading command may be reconfigured in the future to support a different number
 				// of headings. That option must be exposed somehow, because we need to know here whether the replacement
 				// can be done or not.

+ 55 - 0
packages/ckeditor5-autoformat/src/autoformatengine.js

@@ -0,0 +1,55 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+import Feature from '../core/feature.js';
+
+export default class AutoformatEngine extends Feature {
+	init () {
+		// const editor = this.editor;
+		// const data = editor.data;
+		// const editing = editor.editing;
+
+		/**
+		for ( let format of formats ) {
+			// Skip paragraph - it is defined in required Paragraph feature.
+			if ( format.id !== 'paragraph' ) {
+				// Schema.
+				editor.document.schema.registerItem( format.id, '$block' );
+
+				// Build converter from model to view for data and editing pipelines.
+				buildModelConverter().for( data.modelToView, editing.modelToView )
+					.fromElement( format.id )
+					.toElement( format.viewElement );
+
+				// Build converter from view to model for data pipeline.
+				buildViewConverter().for( data.viewToModel )
+					.fromElement( format.viewElement )
+					.toElement( format.id );
+			}
+		}
+
+		// Register the heading command.
+		const command = new HeadingCommand( editor, formats );
+		editor.commands.set( 'heading', command );
+
+		// If the enter command is added to the editor, alter its behavior.
+		// Enter at the end of a heading element should create a paragraph.
+		const enterCommand = editor.commands.get( 'enter' );
+
+		if ( enterCommand ) {
+			this.listenTo( enterCommand, 'afterExecute', ( evt, data ) => {
+				const positionParent = editor.document.selection.getFirstPosition().parent;
+				const batch = data.batch;
+				const isHeading = formats.some( ( format ) => format.id == positionParent.name );
+
+				if ( isHeading && positionParent.name != command.defaultFormat.id && positionParent.childCount === 0 ) {
+					batch.rename( positionParent, command.defaultFormat.id );
+				}
+			} );
+		}
+
+		**/
+	}
+}

+ 0 - 7
packages/ckeditor5-autoformat/src/autoformatter.js

@@ -1,7 +0,0 @@
-/**
- * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
-
-export default class Autoformatter {
-}