From 62d53aa01243a85c3193ea3ee0cce62c19549467 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotrek=20Koszuli=C5=84ski?= Date: Thu, 8 Jun 2017 12:08:20 +0200 Subject: [PATCH] Loosened integration with the heading feature. --- src/autoformat.js | 31 ++++++++++------------- tests/autoformat.js | 60 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 71 insertions(+), 20 deletions(-) diff --git a/src/autoformat.js b/src/autoformat.js index 4b8e41c..b061adc 100644 --- a/src/autoformat.js +++ b/src/autoformat.js @@ -127,34 +127,29 @@ export default class Autoformat extends Plugin { * Adds autoformatting related to {@link module:heading/heading~Heading}. * * It is using a number at the end of the command name to associate it with the proper trigger: + * * * `heading1` will be executed when typing `#`, * * `heading2` will be executed when typing `##`, - * * `heading3` will be executed when typing `###`. + * * ... up to `heading6` and `######`. * * @private */ _addHeadingAutoformats() { const commands = this.editor.commands; - const options = this.editor.config.get( 'heading.options' ); - - if ( options ) { - for ( const option of options ) { - const commandName = option.modelElement; - let match; - if ( commands.has( commandName ) && ( match = commandName.match( /\d+$/ ) ) ) { - const level = match[ 0 ]; - const regExp = new RegExp( `^(#{${ level }})\\s$` ); + Array.from( commands.keys() ) + .filter( name => name.match( /^heading[1-6]$/ ) ) + .forEach( commandName => { + const level = commandName[ 7 ]; + const pattern = new RegExp( `^(#{${ level }})\\s$` ); - // eslint-disable-next-line no-new - new BlockAutoformatEngine( this.editor, regExp, context => { - const { batch } = context; + // eslint-disable-next-line no-new + new BlockAutoformatEngine( this.editor, pattern, context => { + const { batch } = context; - this.editor.execute( commandName, { batch } ); - } ); - } - } - } + this.editor.execute( commandName, { batch } ); + } ); + } ); } /** diff --git a/tests/autoformat.js b/tests/autoformat.js index fb68e4f..f4fc428 100644 --- a/tests/autoformat.js +++ b/tests/autoformat.js @@ -4,17 +4,22 @@ */ import Autoformat from '../src/autoformat'; + import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph'; import ListEngine from '@ckeditor/ckeditor5-list/src/listengine'; import HeadingEngine from '@ckeditor/ckeditor5-heading/src/headingengine'; import BoldEngine from '@ckeditor/ckeditor5-basic-styles/src/boldengine'; import ItalicEngine from '@ckeditor/ckeditor5-basic-styles/src/italicengine'; import BlockQuoteEngine from '@ckeditor/ckeditor5-block-quote/src/blockquoteengine'; -import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor'; import Enter from '@ckeditor/ckeditor5-enter/src/enter'; + +import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor'; + import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model'; import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils'; +import Command from '@ckeditor/ckeditor5-core/src/command/command'; + testUtils.createSinonSandbox(); describe( 'Autoformat', () => { @@ -31,6 +36,10 @@ describe( 'Autoformat', () => { } ); } ); + afterEach( () => { + return editor.destroy(); + } ); + describe( 'Bulleted list', () => { it( 'should replace asterisk with bulleted list item', () => { setData( doc, '*[]' ); @@ -99,7 +108,7 @@ describe( 'Autoformat', () => { expect( getData( doc ) ).to.equal( '[]' ); } ); - it( 'should not replace minus character when inside heading', () => { + it( 'should not replace hash character when inside heading', () => { setData( doc, '#[]' ); doc.enqueueChanges( () => { batch.insert( doc.selection.getFirstPosition(), ' ' ); @@ -107,6 +116,53 @@ describe( 'Autoformat', () => { expect( getData( doc ) ).to.equal( '# []' ); } ); + + it( 'should work with heading1-heading6 commands regardless of the config of the heading feature', () => { + const spy1 = sinon.spy(); + const spy6 = sinon.spy(); + + class Heading6 extends Command { + _doExecute() { + spy6(); + } + } + class Heading1 extends Command { + _doExecute() { + spy1(); + } + } + + function HeadingPlugin( editor ) { + editor.commands.set( 'heading1', new Heading1() ); + editor.commands.set( 'heading6', new Heading6() ); + } + + return VirtualTestEditor + .create( { + plugins: [ + Paragraph, Autoformat, HeadingPlugin + ] + } ) + .then( editor => { + const doc = editor.document; + + setData( doc, '#[]' ); + doc.enqueueChanges( () => { + doc.batch().insert( doc.selection.getFirstPosition(), ' ' ); + } ); + + expect( spy1.calledOnce ).to.be.true; + + setData( doc, '######[]' ); + doc.enqueueChanges( () => { + doc.batch().insert( doc.selection.getFirstPosition(), ' ' ); + } ); + + expect( spy6.calledOnce ).to.be.true; + + return editor.destroy(); + } ); + } ); } ); describe( 'Block quote', () => {