浏览代码

Remove unused code for applying an attribute through default inlineAutoformatEditing's formatCallback.

It was used only in tests, and was not checking whether the command `.isEnabled`.
Autoformat uses its own `getCallbackFunctionForInlineAutoformat`.
The change was proposed at https://github.com/ckeditor/ckeditor5/issues/7290#issuecomment-633898710.
Tomek Wytrębowicz 5 年之前
父节点
当前提交
3aa51bc3a3

+ 4 - 29
packages/ckeditor5-autoformat/src/inlineautoformatediting.js

@@ -34,7 +34,7 @@
  *		// - The first to match the starting `**` delimiter.
  *		// - The second to match the text to format.
  *		// - The third to match the ending `**` delimiter.
- *		inlineAutoformatEditing( editor, plugin, /(\*\*)([^\*]+?)(\*\*)$/g, 'bold' );
+ *		inlineAutoformatEditing( editor, plugin, /(\*\*)([^\*]+?)(\*\*)$/g, formatCallback );
  *
  * When a function is provided instead of the regular expression, it will be executed with the text to match as a parameter.
  * The function should return proper "ranges" to delete and format.
@@ -49,13 +49,9 @@
  *			]
  *		}
  *
- * @param {Function|String} attributeOrCallback The name of attribute to apply on matching text or a callback for manual
- * formatting. If callback is passed it should return `false` if changes should not be applied (e.g. if a command is disabled).
+ * @param {Function} formatCallback A callback to apply actual formatting.
+ * It should return `false` if changes should not be applied (e.g. if a command is disabled).
  *
- *		// Use attribute name:
- *		inlineAutoformatEditing( editor, plugin, /(\*\*)([^\*]+?)(\*\*)$/g, 'bold' );
- *
- *		// Use formatting callback:
  *		inlineAutoformatEditing( editor, plugin, /(\*\*)([^\*]+?)(\*\*)$/g, ( writer, rangesToFormat ) => {
  *			const command = editor.commands.get( 'bold' );
  *
@@ -70,11 +66,9 @@
  *			}
  *		} );
  */
-export default function inlineAutoformatEditing( editor, plugin, testRegexpOrCallback, attributeOrCallback ) {
+export default function inlineAutoformatEditing( editor, plugin, testRegexpOrCallback, formatCallback ) {
 	let regExp;
-	let attributeKey;
 	let testCallback;
-	let formatCallback;
 
 	if ( testRegexpOrCallback instanceof RegExp ) {
 		regExp = testRegexpOrCallback;
@@ -82,12 +76,6 @@ export default function inlineAutoformatEditing( editor, plugin, testRegexpOrCal
 		testCallback = testRegexpOrCallback;
 	}
 
-	if ( typeof attributeOrCallback == 'string' ) {
-		attributeKey = attributeOrCallback;
-	} else {
-		formatCallback = attributeOrCallback;
-	}
-
 	// A test callback run on changed text.
 	testCallback = testCallback || ( text => {
 		let result;
@@ -133,19 +121,6 @@ export default function inlineAutoformatEditing( editor, plugin, testRegexpOrCal
 		};
 	} );
 
-	// A format callback run on matched text.
-	formatCallback = formatCallback || ( ( writer, rangesToFormat ) => {
-		const validRanges = editor.model.schema.getValidRanges( rangesToFormat, attributeKey );
-
-		for ( const range of validRanges ) {
-			writer.setAttribute( attributeKey, true, range );
-		}
-
-		// After applying attribute to the text, remove given attribute from the selection.
-		// This way user is able to type a text without attribute used by auto formatter.
-		writer.removeSelectionAttribute( attributeKey );
-	} );
-
 	editor.model.document.on( 'change:data', ( evt, batch ) => {
 		if ( batch.type == 'transparent' || !plugin.isEnabled ) {
 			return;

+ 12 - 11
packages/ckeditor5-autoformat/tests/inlineautoformatediting.js

@@ -33,38 +33,38 @@ describe( 'inlineAutoformatEditing', () => {
 			} );
 	} );
 
-	describe( 'attribute', () => {
-		it( 'should stop early if there are less than 3 capture groups', () => {
-			inlineAutoformatEditing( editor, plugin, /(\*)(.+?)\*/g, 'testAttribute' );
+	describe( 'regExp', () => {
+		it( 'should not call formatCallback if there are less than 3 capture groups', () => {
+			inlineAutoformatEditing( editor, plugin, /(\*)(.+?)\*/g, formatSpy );
 
 			setData( model, '<paragraph>*foobar[]</paragraph>' );
 			model.change( writer => {
 				writer.insertText( '*', doc.selection.getFirstPosition() );
 			} );
 
-			expect( getData( model ) ).to.equal( '<paragraph>*foobar*[]</paragraph>' );
+			sinon.assert.notCalled( formatSpy );
 		} );
 
-		it( 'should apply an attribute when the pattern is matched', () => {
-			inlineAutoformatEditing( editor, plugin, /(\*)(.+?)(\*)/g, 'testAttribute' );
+		it( 'should call formatCallback when the pattern is matched', () => {
+			inlineAutoformatEditing( editor, plugin, /(\*)(.+?)(\*)/g, formatSpy );
 
 			setData( model, '<paragraph>*foobar[]</paragraph>' );
 			model.change( writer => {
 				writer.insertText( '*', doc.selection.getFirstPosition() );
 			} );
 
-			expect( getData( model ) ).to.equal( '<paragraph><$text testAttribute="true">foobar</$text>[]</paragraph>' );
+			sinon.assert.calledOnce( formatSpy );
 		} );
 
-		it( 'should stop early if selection is not collapsed', () => {
-			inlineAutoformatEditing( editor, plugin, /(\*)(.+?)\*/g, 'testAttribute' );
+		it( 'should not call formatCallback if selection is not collapsed', () => {
+			inlineAutoformatEditing( editor, plugin, /(\*)(.+?)\*/g, formatSpy );
 
 			setData( model, '<paragraph>*foob[ar]</paragraph>' );
 			model.change( writer => {
 				writer.insertText( '*', doc.selection.getFirstPosition() );
 			} );
 
-			expect( getData( model ) ).to.equal( '<paragraph>*foob*[ar]</paragraph>' );
+			sinon.assert.notCalled( formatSpy );
 		} );
 	} );
 
@@ -153,13 +153,14 @@ describe( 'inlineAutoformatEditing', () => {
 	} );
 
 	it( 'should ignore transparent batches', () => {
-		inlineAutoformatEditing( editor, plugin, /(\*)(.+?)(\*)/g, 'testAttribute' );
+		inlineAutoformatEditing( editor, plugin, /(\*)(.+?)(\*)/g, formatSpy );
 
 		setData( model, '<paragraph>*foobar[]</paragraph>' );
 		model.enqueueChange( 'transparent', writer => {
 			writer.insertText( '*', doc.selection.getFirstPosition() );
 		} );
 
+		sinon.assert.notCalled( formatSpy );
 		expect( getData( model ) ).to.equal( '<paragraph>*foobar*[]</paragraph>' );
 	} );
 } );