8
0
Просмотр исходного кода

Added simplified invokation for other autoformats.

Maksymilian Barnaś 9 лет назад
Родитель
Сommit
de1c3d6e38

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

@@ -4,8 +4,10 @@
  */
 
 import AutoformatEngine from './autoformatengine.js';
-import HeadingEngine from '../heading/headingengine.js';
 import Feature from '../core/feature.js';
+import HeadingEngine from '../heading/headingengine.js';
+import ListEngine from '../list/listengine.js';
+
 /**
  * The autoformat feature. Looks for predefined regular expressions and converts inserted text accordingly.
  *
@@ -17,7 +19,7 @@ export default class Autoformat extends Feature {
 	 * @inheritDoc
 	 */
 	static get requires() {
-		return [ HeadingEngine ];
+		return [ HeadingEngine, ListEngine ];
 	}
 
 	/**
@@ -54,7 +56,8 @@ export default class Autoformat extends Feature {
 				batch.remove( range );
 
 				// This part needs slightly changed HeadingCommand.
-				// TODO Commit change to ckeditor5-heading
+				// TODO Commit change to ckeditor5-heading, don't forget to update tests.
+				// TODO Also commit changes to ckeditor5-engine/model/liveposition.js.
 				editor.execute( 'heading', {
 					batch: batch,
 					formatId: `heading${ headingLevel }`

+ 16 - 1
packages/ckeditor5-autoformat/src/autoformatengine.js

@@ -16,7 +16,22 @@ export default class AutoformatEngine {
 	 * @param {Regex} regex Regular expression to exec on just inserted text.
 	 * @param {Function} callback Callback to execute when text is matched.
 	 */
-	constructor ( editor, regex, callback ) {
+	constructor ( editor, regex, callbackOrCommand ) {
+		let callback;
+
+		if ( typeof callbackOrCommand === 'function' ) {
+			callback = callbackOrCommand;
+		} else {
+			const command = callbackOrCommand;
+
+			callback = ( batch, matched, range ) => {
+				batch.remove( range );
+				editor.execute( command, {
+					batch: batch
+				} );
+			};
+		}
+
 		editor.document.on( 'change', ( event, type, changes, batch ) => {
 			if ( type != 'insert' ) {
 				return;