Browse Source

Added the Code Block feature support using 3 grave accent characters (```) in a row.

Aleksander Nowodzinski 6 years ago
parent
commit
2726358672

+ 16 - 0
packages/ckeditor5-autoformat/src/autoformat.js

@@ -35,6 +35,7 @@ export default class Autoformat extends Plugin {
 		this._addBasicStylesAutoformats();
 		this._addHeadingAutoformats();
 		this._addBlockQuoteAutoformats();
+		this._addCodeBlockAutoformats();
 	}
 
 	/**
@@ -152,6 +153,21 @@ export default class Autoformat extends Plugin {
 			new BlockAutoformatEditing( this.editor, /^>\s$/, 'blockQuote' );
 		}
 	}
+
+	/**
+	 * Adds autoformatting related to {@link module:code-block/code-block~CodeBlock}.
+	 *
+	 * When typed:
+	 * - `\`\`\`` – A paragraph will be changed to a code block.
+	 *
+	 * @private
+	 */
+	_addCodeBlockAutoformats() {
+		if ( this.editor.commands.get( 'codeBlock' ) ) {
+			// eslint-disable-next-line no-new
+			new BlockAutoformatEditing( this.editor, /^```$/, 'codeBlock' );
+		}
+	}
 }
 
 // Helper function for getting `InlineAutoformatEditing` callbacks that checks if command is enabled.

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

@@ -12,6 +12,7 @@ import BoldEditing from '@ckeditor/ckeditor5-basic-styles/src/bold/boldediting';
 import CodeEditing from '@ckeditor/ckeditor5-basic-styles/src/code/codeediting';
 import ItalicEditing from '@ckeditor/ckeditor5-basic-styles/src/italic/italicediting';
 import BlockQuoteEditing from '@ckeditor/ckeditor5-block-quote/src/blockquoteediting';
+import CodeBlockEditing from '@ckeditor/ckeditor5-code-block/src/codeblockediting';
 import Enter from '@ckeditor/ckeditor5-enter/src/enter';
 import ShiftEnter from '@ckeditor/ckeditor5-enter/src/shiftenter';
 
@@ -40,6 +41,7 @@ describe( 'Autoformat', () => {
 					ItalicEditing,
 					CodeEditing,
 					BlockQuoteEditing,
+					CodeBlockEditing,
 					ShiftEnter
 				]
 			} )
@@ -252,6 +254,53 @@ describe( 'Autoformat', () => {
 		} );
 	} );
 
+	describe( 'Code block', () => {
+		it( 'should replace triple grave accents with a code block', () => {
+			setData( model, '<paragraph>``[]</paragraph>' );
+			model.change( writer => {
+				writer.insertText( '`', doc.selection.getFirstPosition() );
+			} );
+
+			expect( getData( model ) ).to.equal( '<codeBlock language="plaintext">[]</codeBlock>' );
+		} );
+
+		it( 'should not replace triple grave accents when already in a code block', () => {
+			setData( model, '<codeBlock language="plaintext">``[]</codeBlock>' );
+			model.change( writer => {
+				writer.insertText( '`', doc.selection.getFirstPosition() );
+			} );
+
+			expect( getData( model ) ).to.equal( '<codeBlock language="plaintext">```[]</codeBlock>' );
+		} );
+
+		it( 'should not replace triple grave accents when inside heading', () => {
+			setData( model, '<heading1>``[]</heading1>' );
+			model.change( writer => {
+				writer.insertText( '`', doc.selection.getFirstPosition() );
+			} );
+
+			expect( getData( model ) ).to.equal( '<heading1>```[]</heading1>' );
+		} );
+
+		it( 'should not replace triple grave accents when inside numbered list', () => {
+			setData( model, '<listItem listIndent="0" listType="numbered">1. ``[]</listItem>' );
+			model.change( writer => {
+				writer.insertText( '`', doc.selection.getFirstPosition() );
+			} );
+
+			expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="numbered">1. ```[]</listItem>' );
+		} );
+
+		it( 'should not replace triple grave accents when inside buletted list', () => {
+			setData( model, '<listItem listIndent="0" listType="bulleted">1. ``[]</listItem>' );
+			model.change( writer => {
+				writer.insertText( '`', doc.selection.getFirstPosition() );
+			} );
+
+			expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="bulleted">1. ```[]</listItem>' );
+		} );
+	} );
+
 	describe( 'Inline autoformat', () => {
 		it( 'should replace both "**" with bold', () => {
 			setData( model, '<paragraph>**foobar*[]</paragraph>' );
@@ -418,6 +467,15 @@ describe( 'Autoformat', () => {
 			expect( getData( model ) ).to.equal( '<paragraph>> []</paragraph>' );
 		} );
 
+		it( 'should not replace "```" with code block', () => {
+			setData( model, '<paragraph>``[]</paragraph>' );
+			model.change( writer => {
+				writer.insertText( '`', doc.selection.getFirstPosition() );
+			} );
+
+			expect( getData( model ) ).to.equal( '<paragraph>```[]</paragraph>' );
+		} );
+
 		it( 'should use only configured headings', () => {
 			return VirtualTestEditor
 				.create( {