Procházet zdrojové kódy

Internal (autoformat): Restored initial block autoformat regexp format.

Marek Lewandowski před 5 roky
rodič
revize
a63f559f1c

+ 5 - 5
packages/ckeditor5-autoformat/src/autoformat.js

@@ -51,11 +51,11 @@ export default class Autoformat extends Plugin {
 		const commands = this.editor.commands;
 
 		if ( commands.get( 'bulletedList' ) ) {
-			blockAutoformatEditing( this.editor, this, /^[*-]\s/, 'bulletedList' );
+			blockAutoformatEditing( this.editor, this, /^[*-]\s$/, 'bulletedList' );
 		}
 
 		if ( commands.get( 'numberedList' ) ) {
-			blockAutoformatEditing( this.editor, this, /^1[.|)]\s/, 'numberedList' );
+			blockAutoformatEditing( this.editor, this, /^1[.|)]\s$/, 'numberedList' );
 		}
 	}
 
@@ -125,7 +125,7 @@ export default class Autoformat extends Plugin {
 				.filter( name => name.match( /^heading[1-6]$/ ) )
 				.forEach( modelName => {
 					const level = modelName[ 7 ];
-					const pattern = new RegExp( `^(#{${ level }})\\s` );
+					const pattern = new RegExp( `^(#{${ level }})\\s$` );
 
 					blockAutoformatEditing( this.editor, this, pattern, () => {
 						// Should only be active if command is enabled and heading style associated with pattern is inactive.
@@ -149,7 +149,7 @@ export default class Autoformat extends Plugin {
 	 */
 	_addBlockQuoteAutoformats() {
 		if ( this.editor.commands.get( 'blockQuote' ) ) {
-			blockAutoformatEditing( this.editor, this, /^>\s/, 'blockQuote' );
+			blockAutoformatEditing( this.editor, this, /^>\s$/, 'blockQuote' );
 		}
 	}
 
@@ -163,7 +163,7 @@ export default class Autoformat extends Plugin {
 	 */
 	_addCodeBlockAutoformats() {
 		if ( this.editor.commands.get( 'codeBlock' ) ) {
-			blockAutoformatEditing( this.editor, this, /^```/, 'codeBlock' );
+			blockAutoformatEditing( this.editor, this, /^```$/, 'codeBlock' );
 		}
 	}
 }

+ 14 - 10
packages/ckeditor5-autoformat/src/blockautoformatediting.js

@@ -5,7 +5,6 @@
 
 import LiveRange from '@ckeditor/ckeditor5-engine/src/model/liverange';
 import first from '@ckeditor/ckeditor5-utils/src/first';
-import Position from '@ckeditor/ckeditor5-engine/src/model/position';
 
 /**
  * The block autoformatting engine. It allows to format various block patterns. For example,
@@ -46,7 +45,8 @@ import Position from '@ckeditor/ckeditor5-engine/src/model/position';
  *
  * @param {module:core/editor/editor~Editor} editor The editor instance.
  * @param {module:autoformat/autoformat~Autoformat} plugin The autoformat plugin instance.
- * @param {RegExp} pattern The regular expression to execute on just inserted text.
+ * @param {RegExp} pattern The regular expression to execute on just inserted text. The regular expression is tested against the text
+ * from the beginning until the caret position.
  * @param {Function|String} callbackOrCommand The callback to execute or the command to run when the text is matched.
  * In case of providing the callback, it receives the following parameter:
  * * {Object} match RegExp.exec() result of matching the pattern to inserted text.
@@ -71,6 +71,12 @@ export default function blockAutoformatEditing( editor, plugin, pattern, callbac
 			return;
 		}
 
+		const range = first( editor.model.document.selection.getRanges() );
+
+		if ( !range.isCollapsed ) {
+			return;
+		}
+
 		if ( batch.type == 'transparent' ) {
 			return;
 		}
@@ -97,19 +103,17 @@ export default function blockAutoformatEditing( editor, plugin, pattern, callbac
 		}
 
 		const firstNode = blockToFormat.getChild( 0 );
-		const match = pattern.exec( firstNode.data );
+		const firstNodeRange = editor.model.createRangeOn( firstNode );
 
-		// ...and this text node's data match the pattern.
-		if ( !match ) {
+		// Range is only expected to be within or at the very end of the first text node.
+		if ( !firstNodeRange.containsRange( range ) && !range.end.isEqual( firstNodeRange.end ) ) {
 			return;
 		}
 
-		const range = first( editor.model.document.selection.getRanges() );
-
-		// We're only handling a collapsed selection that is right after the matched text (#5671).
-		const expectedPosition = ( new Position( range.root, firstNode.getPath() ) ).getShiftedBy( match[ 0 ].length );
+		const match = pattern.exec( firstNode.data.substr( 0, range.end.offset ) );
 
-		if ( !( range.isCollapsed && range.end.isEqual( expectedPosition ) ) ) {
+		// ...and this text node's data match the pattern.
+		if ( !match ) {
 			return;
 		}
 

+ 5 - 5
packages/ckeditor5-autoformat/tests/blockautoformatediting.js

@@ -117,7 +117,7 @@ describe( 'blockAutoformatEditing', () => {
 
 		it( 'should ignore other delta operations', () => {
 			const spy = testUtils.sinon.spy();
-			blockAutoformatEditing( editor, plugin, /^[*]\s/, spy );
+			blockAutoformatEditing( editor, plugin, /^[*]\s$/, spy );
 
 			setData( model, '<paragraph>*[]</paragraph>' );
 			model.change( writer => {
@@ -129,7 +129,7 @@ describe( 'blockAutoformatEditing', () => {
 
 		it( 'should stop if there is no text to run matching on', () => {
 			const spy = testUtils.sinon.spy();
-			blockAutoformatEditing( editor, plugin, /^[*]\s/, spy );
+			blockAutoformatEditing( editor, plugin, /^[*]\s$/, spy );
 
 			setData( model, '<paragraph>[]</paragraph>' );
 			model.change( writer => {
@@ -157,7 +157,7 @@ describe( 'blockAutoformatEditing', () => {
 				} );
 
 			const spy = testUtils.sinon.spy();
-			blockAutoformatEditing( editor, plugin, /^[*]\s/, spy );
+			blockAutoformatEditing( editor, plugin, /^[*]\s$/, spy );
 
 			setData( model, '<paragraph>*<softBreak></softBreak>[]</paragraph>' );
 			model.change( writer => {
@@ -169,7 +169,7 @@ describe( 'blockAutoformatEditing', () => {
 
 		it( 'should not call callback when typing in the middle of block text', () => {
 			const spy = testUtils.sinon.spy();
-			blockAutoformatEditing( editor, plugin, /^[*]\s/, spy );
+			blockAutoformatEditing( editor, plugin, /^[*]\s$/, spy );
 
 			setData( model, '<paragraph>* foo[]bar</paragraph>' );
 			model.change( writer => {
@@ -197,7 +197,7 @@ describe( 'blockAutoformatEditing', () => {
 				} );
 
 			const spy = testUtils.sinon.spy();
-			blockAutoformatEditing( editor, plugin, /^[*]\s/, spy );
+			blockAutoformatEditing( editor, plugin, /^[*]\s$/, spy );
 
 			setData( model, '<paragraph>* <softBreak></softBreak>[]</paragraph>' );