瀏覽代碼

Do not format if the plugin is disabled.

Implements 1. from https://github.com/ckeditor/ckeditor5/issues/1239#issuecomment-623380554.
Tomek Wytrębowicz 5 年之前
父節點
當前提交
710f7d8307

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

@@ -52,12 +52,12 @@ export default class Autoformat extends Plugin {
 
 		if ( commands.get( 'bulletedList' ) ) {
 			// eslint-disable-next-line no-new
-			new BlockAutoformatEditing( this.editor, /^[*-]\s$/, 'bulletedList' );
+			new BlockAutoformatEditing( this.editor, this, /^[*-]\s$/, 'bulletedList' );
 		}
 
 		if ( commands.get( 'numberedList' ) ) {
 			// eslint-disable-next-line no-new
-			new BlockAutoformatEditing( this.editor, /^1[.|)]\s$/, 'numberedList' );
+			new BlockAutoformatEditing( this.editor, this, /^1[.|)]\s$/, 'numberedList' );
 		}
 	}
 
@@ -83,8 +83,8 @@ export default class Autoformat extends Plugin {
 			/* eslint-disable no-new */
 			const boldCallback = getCallbackFunctionForInlineAutoformat( this.editor, 'bold' );
 
-			new InlineAutoformatEditing( this.editor, /(\*\*)([^*]+)(\*\*)$/g, boldCallback );
-			new InlineAutoformatEditing( this.editor, /(__)([^_]+)(__)$/g, boldCallback );
+			new InlineAutoformatEditing( this.editor, this, /(\*\*)([^*]+)(\*\*)$/g, boldCallback );
+			new InlineAutoformatEditing( this.editor, this, /(__)([^_]+)(__)$/g, boldCallback );
 			/* eslint-enable no-new */
 		}
 
@@ -94,8 +94,8 @@ export default class Autoformat extends Plugin {
 
 			// The italic autoformatter cannot be triggered by the bold markers, so we need to check the
 			// text before the pattern (e.g. `(?:^|[^\*])`).
-			new InlineAutoformatEditing( this.editor, /(?:^|[^*])(\*)([^*_]+)(\*)$/g, italicCallback );
-			new InlineAutoformatEditing( this.editor, /(?:^|[^_])(_)([^_]+)(_)$/g, italicCallback );
+			new InlineAutoformatEditing( this.editor, this, /(?:^|[^*])(\*)([^*_]+)(\*)$/g, italicCallback );
+			new InlineAutoformatEditing( this.editor, this, /(?:^|[^_])(_)([^_]+)(_)$/g, italicCallback );
 			/* eslint-enable no-new */
 		}
 
@@ -103,7 +103,7 @@ export default class Autoformat extends Plugin {
 			/* eslint-disable no-new */
 			const codeCallback = getCallbackFunctionForInlineAutoformat( this.editor, 'code' );
 
-			new InlineAutoformatEditing( this.editor, /(`)([^`]+)(`)$/g, codeCallback );
+			new InlineAutoformatEditing( this.editor, this, /(`)([^`]+)(`)$/g, codeCallback );
 			/* eslint-enable no-new */
 		}
 
@@ -111,7 +111,7 @@ export default class Autoformat extends Plugin {
 			/* eslint-disable no-new */
 			const strikethroughCallback = getCallbackFunctionForInlineAutoformat( this.editor, 'strikethrough' );
 
-			new InlineAutoformatEditing( this.editor, /(~~)([^~]+)(~~)$/g, strikethroughCallback );
+			new InlineAutoformatEditing( this.editor, this, /(~~)([^~]+)(~~)$/g, strikethroughCallback );
 			/* eslint-enable no-new */
 		}
 	}
@@ -138,7 +138,7 @@ export default class Autoformat extends Plugin {
 					const pattern = new RegExp( `^(#{${ level }})\\s$` );
 
 					// eslint-disable-next-line no-new
-					new BlockAutoformatEditing( this.editor, pattern, () => {
+					new BlockAutoformatEditing( this.editor, this, pattern, () => {
 						if ( !command.isEnabled ) {
 							return false;
 						}
@@ -160,7 +160,7 @@ export default class Autoformat extends Plugin {
 	_addBlockQuoteAutoformats() {
 		if ( this.editor.commands.get( 'blockQuote' ) ) {
 			// eslint-disable-next-line no-new
-			new BlockAutoformatEditing( this.editor, /^>\s$/, 'blockQuote' );
+			new BlockAutoformatEditing( this.editor, this, /^>\s$/, 'blockQuote' );
 		}
 	}
 
@@ -175,7 +175,7 @@ export default class Autoformat extends Plugin {
 	_addCodeBlockAutoformats() {
 		if ( this.editor.commands.get( 'codeBlock' ) ) {
 			// eslint-disable-next-line no-new
-			new BlockAutoformatEditing( this.editor, /^```$/, 'codeBlock' );
+			new BlockAutoformatEditing( this.editor, this, /^```$/, 'codeBlock' );
 		}
 	}
 }

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

@@ -30,11 +30,11 @@ export default class BlockAutoformatEditing {
 	 *
 	 * To convert a paragraph to heading 1 when `- ` is typed, using just the command name:
 	 *
-	 *		new BlockAutoformatEditing( editor, /^\- $/, 'heading1' );
+	 *		new BlockAutoformatEditing( editor, plugin, /^\- $/, 'heading1' );
 	 *
 	 * To convert a paragraph to heading 1 when `- ` is typed, using just the callback:
 	 *
-	 *		new BlockAutoformatEditing( editor, /^\- $/, ( context ) => {
+	 *		new BlockAutoformatEditing( editor, plugin, /^\- $/, ( context ) => {
 	 *			const { match } = context;
 	 *			const headingLevel = match[ 1 ].length;
 	 *
@@ -44,12 +44,13 @@ export default class BlockAutoformatEditing {
 	 * 		} );
 	 *
 	 * @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 {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.
 	 */
-	constructor( editor, pattern, callbackOrCommand ) {
+	constructor( editor, plugin, pattern, callbackOrCommand ) {
 		let callback;
 		let command = null;
 
@@ -65,7 +66,7 @@ export default class BlockAutoformatEditing {
 		}
 
 		editor.model.document.on( 'change', ( evt, batch ) => {
-			if ( command && !command.isEnabled ) {
+			if ( command && !command.isEnabled || !plugin.isEnabled ) {
 				return;
 			}
 

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

@@ -29,6 +29,7 @@ export default class InlineAutoformatEditing {
 	 * and executes the provided action if the text matches given criteria (regular expression or callback).
 	 *
 	 * @param {module:core/editor/editor~Editor} editor The editor instance.
+	 * @param {module:autoformat/autoformat~Autoformat} plugin The autoformat plugin instance.
 	 * @param {Function|RegExp} testRegexpOrCallback The regular expression or callback to execute on text.
 	 * Provided regular expression *must* have three capture groups. The first and the third capture group
 	 * should match opening and closing delimiters. The second capture group should match the text to format.
@@ -38,7 +39,7 @@ export default class InlineAutoformatEditing {
 	 *		// - The first to match the starting `**` delimiter.
 	 *		// - The second to match the text to format.
 	 *		// - The third to match the ending `**` delimiter.
-	 *		new InlineAutoformatEditing( editor, /(\*\*)([^\*]+?)(\*\*)$/g, 'bold' );
+	 *		new InlineAutoformatEditing( editor, plugin, /(\*\*)([^\*]+?)(\*\*)$/g, 'bold' );
 	 *
 	 * 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.
@@ -57,10 +58,10 @@ export default class InlineAutoformatEditing {
 	 * formatting. If callback is passed it should return `false` if changes should not be applied (e.g. if a command is disabled).
 	 *
 	 *		// Use attribute name:
-	 *		new InlineAutoformatEditing( editor, /(\*\*)([^\*]+?)(\*\*)$/g, 'bold' );
+	 *		new InlineAutoformatEditing( editor, plugin, /(\*\*)([^\*]+?)(\*\*)$/g, 'bold' );
 	 *
 	 *		// Use formatting callback:
-	 *		new InlineAutoformatEditing( editor, /(\*\*)([^\*]+?)(\*\*)$/g, ( writer, rangesToFormat ) => {
+	 *		new InlineAutoformatEditing( editor, plugin, /(\*\*)([^\*]+?)(\*\*)$/g, ( writer, rangesToFormat ) => {
 	 *			const command = editor.commands.get( 'bold' );
 	 *
 	 *			if ( !command.isEnabled ) {
@@ -74,7 +75,7 @@ export default class InlineAutoformatEditing {
 	 *			}
 	 *		} );
 	 */
-	constructor( editor, testRegexpOrCallback, attributeOrCallback ) {
+	constructor( editor, plugin, testRegexpOrCallback, attributeOrCallback ) {
 		let regExp;
 		let attributeKey;
 		let testCallback;
@@ -151,7 +152,7 @@ export default class InlineAutoformatEditing {
 		} );
 
 		editor.model.document.on( 'change', ( evt, batch ) => {
-			if ( batch.type == 'transparent' ) {
+			if ( batch.type == 'transparent' || !plugin.isEnabled ) {
 				return;
 			}
 

+ 12 - 0
packages/ckeditor5-autoformat/tests/autoformat.js

@@ -410,6 +410,18 @@ describe( 'Autoformat', () => {
 			expect( getData( model ) ).to.equal( '<paragraph>**foobar**[]</paragraph>' );
 		} );
 
+		it( 'should not format if the plugin is not enabled', () => {
+			editor.plugins.get( 'Autoformat' ).forceDisabled( 'Test' );
+
+			setData( model, '<paragraph>**foobar*[]</paragraph>' );
+
+			model.change( writer => {
+				writer.insertText( '*', doc.selection.getFirstPosition() );
+			} );
+
+			expect( getData( model ) ).to.equal( '<paragraph>**foobar**[]</paragraph>' );
+		} );
+
 		it( 'should work with <softBreak>s in paragraph', () => {
 			setData( model, '<paragraph>foo<softBreak></softBreak>**barbaz*[]</paragraph>' );
 			model.change( writer => {

+ 26 - 11
packages/ckeditor5-autoformat/tests/blockautoformatediting.js

@@ -13,7 +13,7 @@ import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
 import Command from '@ckeditor/ckeditor5-core/src/command';
 
 describe( 'BlockAutoformatEditing', () => {
-	let editor, model, doc;
+	let editor, model, doc, plugin;
 
 	testUtils.createSinonSandbox();
 
@@ -26,6 +26,7 @@ describe( 'BlockAutoformatEditing', () => {
 				editor = newEditor;
 				model = editor.model;
 				doc = model.document;
+				plugin = editor.plugins.get( 'Autoformat' );
 			} );
 	} );
 
@@ -36,7 +37,7 @@ describe( 'BlockAutoformatEditing', () => {
 
 			editor.commands.add( 'testCommand', testCommand );
 
-			new BlockAutoformatEditing( editor, /^[*]\s$/, 'testCommand' ); // eslint-disable-line no-new
+			new BlockAutoformatEditing( editor, plugin, /^[*]\s$/, 'testCommand' ); // eslint-disable-line no-new
 
 			setData( model, '<paragraph>*[]</paragraph>' );
 
@@ -53,7 +54,7 @@ describe( 'BlockAutoformatEditing', () => {
 
 			editor.commands.add( 'testCommand', testCommand );
 
-			new BlockAutoformatEditing( editor, /^[*]\s$/, 'testCommand' ); // eslint-disable-line no-new
+			new BlockAutoformatEditing( editor, plugin, /^[*]\s$/, 'testCommand' ); // eslint-disable-line no-new
 
 			setData( model, '<paragraph>*[]</paragraph>' );
 
@@ -75,7 +76,7 @@ describe( 'BlockAutoformatEditing', () => {
 
 			editor.commands.add( 'testCommand', testCommand );
 
-			new BlockAutoformatEditing( editor, /^[*]\s$/, 'testCommand' ); // eslint-disable-line no-new
+			new BlockAutoformatEditing( editor, plugin, /^[*]\s$/, 'testCommand' ); // eslint-disable-line no-new
 
 			setData( model, '<paragraph>*[]</paragraph>' );
 
@@ -90,7 +91,7 @@ describe( 'BlockAutoformatEditing', () => {
 	describe( 'callback', () => {
 		it( 'should run callback when the pattern is matched', () => {
 			const spy = testUtils.sinon.spy();
-			new BlockAutoformatEditing( editor, /^[*]\s$/, spy ); // eslint-disable-line no-new
+			new BlockAutoformatEditing( editor, plugin, /^[*]\s$/, spy ); // eslint-disable-line no-new
 
 			setData( model, '<paragraph>*[]</paragraph>' );
 			model.change( writer => {
@@ -100,9 +101,23 @@ describe( 'BlockAutoformatEditing', () => {
 			sinon.assert.calledOnce( spy );
 		} );
 
+		it( 'should not call callback when the pattern is matched and plugin is disabled', () => {
+			const callbackSpy = testUtils.sinon.spy().named( 'callback' );
+			new BlockAutoformatEditing( editor, plugin, /^[*]\s$/, callbackSpy ); // eslint-disable-line no-new
+
+			plugin.isEnabled = false;
+
+			setData( model, '<paragraph>*[]</paragraph>' );
+			model.change( writer => {
+				writer.insertText( ' ', doc.selection.getFirstPosition() );
+			} );
+
+			sinon.assert.notCalled( callbackSpy );
+		} );
+
 		it( 'should ignore other delta operations', () => {
 			const spy = testUtils.sinon.spy();
-			new BlockAutoformatEditing( editor, /^[*]\s/, spy ); // eslint-disable-line no-new
+			new BlockAutoformatEditing( editor, plugin, /^[*]\s/, spy ); // eslint-disable-line no-new
 
 			setData( model, '<paragraph>*[]</paragraph>' );
 			model.change( writer => {
@@ -114,7 +129,7 @@ describe( 'BlockAutoformatEditing', () => {
 
 		it( 'should stop if there is no text to run matching on', () => {
 			const spy = testUtils.sinon.spy();
-			new BlockAutoformatEditing( editor, /^[*]\s/, spy ); // eslint-disable-line no-new
+			new BlockAutoformatEditing( editor, plugin, /^[*]\s/, spy ); // eslint-disable-line no-new
 
 			setData( model, '<paragraph>[]</paragraph>' );
 			model.change( writer => {
@@ -142,7 +157,7 @@ describe( 'BlockAutoformatEditing', () => {
 				} );
 
 			const spy = testUtils.sinon.spy();
-			new BlockAutoformatEditing( editor, /^[*]\s/, spy ); // eslint-disable-line no-new
+			new BlockAutoformatEditing( editor, plugin, /^[*]\s/, spy ); // eslint-disable-line no-new
 
 			setData( model, '<paragraph>*<softBreak></softBreak>[]</paragraph>' );
 			model.change( writer => {
@@ -170,7 +185,7 @@ describe( 'BlockAutoformatEditing', () => {
 				} );
 
 			const spy = testUtils.sinon.spy();
-			new BlockAutoformatEditing( editor, /^[*]\s/, spy ); // eslint-disable-line no-new
+			new BlockAutoformatEditing( editor, plugin, /^[*]\s/, spy ); // eslint-disable-line no-new
 
 			setData( model, '<paragraph>* <softBreak></softBreak>[]</paragraph>' );
 
@@ -182,7 +197,7 @@ describe( 'BlockAutoformatEditing', () => {
 		} );
 
 		it( 'should stop if callback returned false', () => {
-			new BlockAutoformatEditing( editor, /^[*]\s$/, () => false ); // eslint-disable-line no-new
+			new BlockAutoformatEditing( editor, plugin, /^[*]\s$/, () => false ); // eslint-disable-line no-new
 
 			setData( model, '<paragraph>*[]</paragraph>' );
 			model.change( writer => {
@@ -195,7 +210,7 @@ describe( 'BlockAutoformatEditing', () => {
 
 	it( 'should ignore transparent batches', () => {
 		const spy = testUtils.sinon.spy();
-		new BlockAutoformatEditing( editor, /^[*]\s$/, spy ); // eslint-disable-line no-new
+		new BlockAutoformatEditing( editor, plugin, /^[*]\s$/, spy ); // eslint-disable-line no-new
 
 		setData( model, '<paragraph>*[]</paragraph>' );
 		model.enqueueChange( 'transparent', writer => {

+ 24 - 9
packages/ckeditor5-autoformat/tests/inlineautoformatediting.js

@@ -12,7 +12,7 @@ import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model
 import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
 
 describe( 'InlineAutoformatEditing', () => {
-	let editor, model, doc;
+	let editor, model, doc, plugin;
 
 	testUtils.createSinonSandbox();
 
@@ -25,6 +25,7 @@ describe( 'InlineAutoformatEditing', () => {
 				editor = newEditor;
 				model = editor.model;
 				doc = model.document;
+				plugin = editor.plugins.get( 'Autoformat' );
 
 				model.schema.extend( '$text', { allowAttributes: 'testAttribute' } );
 			} );
@@ -32,7 +33,7 @@ describe( 'InlineAutoformatEditing', () => {
 
 	describe( 'attribute', () => {
 		it( 'should stop early if there are less than 3 capture groups', () => {
-			new InlineAutoformatEditing( editor, /(\*)(.+?)\*/g, 'testAttribute' ); // eslint-disable-line no-new
+			new InlineAutoformatEditing( editor, plugin, /(\*)(.+?)\*/g, 'testAttribute' ); // eslint-disable-line no-new
 
 			setData( model, '<paragraph>*foobar[]</paragraph>' );
 			model.change( writer => {
@@ -43,7 +44,7 @@ describe( 'InlineAutoformatEditing', () => {
 		} );
 
 		it( 'should apply an attribute when the pattern is matched', () => {
-			new InlineAutoformatEditing( editor, /(\*)(.+?)(\*)/g, 'testAttribute' ); // eslint-disable-line no-new
+			new InlineAutoformatEditing( editor, plugin, /(\*)(.+?)(\*)/g, 'testAttribute' ); // eslint-disable-line no-new
 
 			setData( model, '<paragraph>*foobar[]</paragraph>' );
 			model.change( writer => {
@@ -54,7 +55,7 @@ describe( 'InlineAutoformatEditing', () => {
 		} );
 
 		it( 'should stop early if selection is not collapsed', () => {
-			new InlineAutoformatEditing( editor, /(\*)(.+?)\*/g, 'testAttribute' ); // eslint-disable-line no-new
+			new InlineAutoformatEditing( editor, plugin, /(\*)(.+?)\*/g, 'testAttribute' ); // eslint-disable-line no-new
 
 			setData( model, '<paragraph>*foob[ar]</paragraph>' );
 			model.change( writer => {
@@ -73,7 +74,7 @@ describe( 'InlineAutoformatEditing', () => {
 				remove: []
 			} );
 
-			new InlineAutoformatEditing( editor, testStub, formatSpy ); // eslint-disable-line no-new
+			new InlineAutoformatEditing( editor, plugin, testStub, formatSpy ); // eslint-disable-line no-new
 
 			setData( model, '<paragraph>*[]</paragraph>' );
 			model.change( writer => {
@@ -90,7 +91,7 @@ describe( 'InlineAutoformatEditing', () => {
 				remove: [ [] ]
 			} );
 
-			new InlineAutoformatEditing( editor, testStub, formatSpy ); // eslint-disable-line no-new
+			new InlineAutoformatEditing( editor, plugin, testStub, formatSpy ); // eslint-disable-line no-new
 
 			setData( model, '<paragraph>*[]</paragraph>' );
 			model.change( writer => {
@@ -107,7 +108,7 @@ describe( 'InlineAutoformatEditing', () => {
 				remove: [ [] ]
 			} );
 
-			new InlineAutoformatEditing( editor, testStub, formatSpy ); // eslint-disable-line no-new
+			new InlineAutoformatEditing( editor, plugin, testStub, formatSpy ); // eslint-disable-line no-new
 
 			setData( model, '<paragraph>[]</paragraph>' );
 			model.change( writer => {
@@ -117,6 +118,20 @@ describe( 'InlineAutoformatEditing', () => {
 			sinon.assert.notCalled( formatSpy );
 		} );
 
+		it( 'should not run callback when the pattern is matched and plugin is disabled', () => {
+			const callbackSpy = testUtils.sinon.spy().named( 'callback' );
+			new InlineAutoformatEditing( editor, plugin, /(\*)(.+?)(\*)/g, callbackSpy ); // eslint-disable-line no-new
+
+			plugin.isEnabled = false;
+
+			setData( model, '<paragraph>*foobar[]</paragraph>' );
+			model.change( writer => {
+				writer.insertText( '*', doc.selection.getFirstPosition() );
+			} );
+
+			sinon.assert.notCalled( callbackSpy );
+		} );
+
 		it( 'should not autoformat if callback returned false', () => {
 			setData( model, '<paragraph>Foobar[]</paragraph>' );
 
@@ -129,7 +144,7 @@ describe( 'InlineAutoformatEditing', () => {
 
 			const formatCallback = () => false;
 
-			new InlineAutoformatEditing( editor, testCallback, formatCallback ); // eslint-disable-line no-new
+			new InlineAutoformatEditing( editor, plugin, testCallback, formatCallback ); // eslint-disable-line no-new
 
 			model.change( writer => {
 				writer.insertText( ' ', doc.selection.getFirstPosition() );
@@ -140,7 +155,7 @@ describe( 'InlineAutoformatEditing', () => {
 	} );
 
 	it( 'should ignore transparent batches', () => {
-		new InlineAutoformatEditing( editor, /(\*)(.+?)(\*)/g, 'testAttribute' ); // eslint-disable-line no-new
+		new InlineAutoformatEditing( editor, plugin, /(\*)(.+?)(\*)/g, 'testAttribute' ); // eslint-disable-line no-new
 
 		setData( model, '<paragraph>*foobar[]</paragraph>' );
 		model.enqueueChange( 'transparent', writer => {