Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Merge pull request #30 from ckeditor/t/29
Browse files Browse the repository at this point in the history
Other: The autoformat feature will not depend on the configuration of the heading feature but it will use the available `heading*` commands. Closes #29.
  • Loading branch information
szymonkups committed Jun 8, 2017
2 parents 7d9a5b1 + 62d53aa commit d0cee1f
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 20 deletions.
31 changes: 13 additions & 18 deletions src/autoformat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 } );
} );
} );
}

/**
Expand Down
60 changes: 58 additions & 2 deletions tests/autoformat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -31,6 +36,10 @@ describe( 'Autoformat', () => {
} );
} );

afterEach( () => {
return editor.destroy();
} );

describe( 'Bulleted list', () => {
it( 'should replace asterisk with bulleted list item', () => {
setData( doc, '<paragraph>*[]</paragraph>' );
Expand Down Expand Up @@ -99,14 +108,61 @@ describe( 'Autoformat', () => {
expect( getData( doc ) ).to.equal( '<heading2>[]</heading2>' );
} );

it( 'should not replace minus character when inside heading', () => {
it( 'should not replace hash character when inside heading', () => {
setData( doc, '<heading1>#[]</heading1>' );
doc.enqueueChanges( () => {
batch.insert( doc.selection.getFirstPosition(), ' ' );
} );

expect( getData( doc ) ).to.equal( '<heading1># []</heading1>' );
} );

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, '<paragraph>#[]</paragraph>' );
doc.enqueueChanges( () => {
doc.batch().insert( doc.selection.getFirstPosition(), ' ' );
} );

expect( spy1.calledOnce ).to.be.true;

setData( doc, '<paragraph>######[]</paragraph>' );
doc.enqueueChanges( () => {
doc.batch().insert( doc.selection.getFirstPosition(), ' ' );
} );

expect( spy6.calledOnce ).to.be.true;

return editor.destroy();
} );
} );
} );

describe( 'Block quote', () => {
Expand Down

0 comments on commit d0cee1f

Please sign in to comment.