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

Commit

Permalink
Merge a2f484d into cc7f454
Browse files Browse the repository at this point in the history
  • Loading branch information
scofalik committed Feb 6, 2019
2 parents cc7f454 + a2f484d commit f6edd7d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/autoformat.js
Expand Up @@ -128,6 +128,10 @@ export default class Autoformat extends Plugin {

// eslint-disable-next-line no-new
new BlockAutoformatEditing( this.editor, pattern, () => {
if ( !command.isEnabled ) {
return false;
}

this.editor.execute( 'heading', { value: commandValue } );
} );
} );
Expand Down
13 changes: 10 additions & 3 deletions src/blockautoformatediting.js
Expand Up @@ -7,6 +7,8 @@
* @module autoformat/blockautoformatediting
*/

import LiveRange from '@ckeditor/ckeditor5-engine/src/model/liverange';

/**
* The block autoformatting engine. It allows to format various block patterns. For example,
* it can be configured to turn a paragraph starting with `*` and followed by a space into a list item.
Expand Down Expand Up @@ -78,6 +80,7 @@ export default class BlockAutoformatEditing {
if ( changes.length != 1 || entry.type !== 'insert' || entry.name != '$text' || entry.length != 1 ) {
return;
}

const item = entry.position.textNode || entry.position.nodeAfter;

if ( !item.parent.is( 'paragraph' ) ) {
Expand All @@ -95,12 +98,16 @@ export default class BlockAutoformatEditing {
// Matched range.
const start = writer.createPositionAt( item.parent, 0 );
const end = writer.createPositionAt( item.parent, match[ 0 ].length );
const range = writer.createRange( start, end );
const range = new LiveRange( start, end );

const wasChanged = callback( { match } );

// Remove matched text.
writer.remove( range );
if ( wasChanged !== false ) {
writer.remove( range );
}

callback( { match } );
range.detach();
} );
} );
}
Expand Down
14 changes: 14 additions & 0 deletions tests/autoformat.js
Expand Up @@ -154,6 +154,7 @@ describe( 'Autoformat', () => {

function HeadingPlugin( editor ) {
editor.commands.add( 'heading', command );
command.refresh();
}

return VirtualTestEditor
Expand Down Expand Up @@ -187,6 +188,19 @@ describe( 'Autoformat', () => {
return editor.destroy();
} );
} );

it( 'should not replace if heading command is disabled', () => {
setData( model, '<paragraph>#[]</paragraph>' );

model.change( writer => {
editor.commands.get( 'heading' ).refresh = () => {};
editor.commands.get( 'heading' ).isEnabled = false;

writer.insertText( ' ', doc.selection.getFirstPosition() );
} );

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

describe( 'Block quote', () => {
Expand Down
11 changes: 11 additions & 0 deletions tests/blockautoformatediting.js
Expand Up @@ -123,6 +123,17 @@ describe( 'BlockAutoformatEditing', () => {

sinon.assert.notCalled( spy );
} );

it( 'should stop if callback returned false', () => {
new BlockAutoformatEditing( editor, /^[*]\s$/, () => false ); // eslint-disable-line no-new

setData( model, '<paragraph>*[]</paragraph>' );
model.change( writer => {
writer.insertText( ' ', doc.selection.getFirstPosition() );
} );

expect( getData( model ) ).to.equal( '<paragraph>* []</paragraph>' );
} );
} );

it( 'should ignore transparent batches', () => {
Expand Down

0 comments on commit f6edd7d

Please sign in to comment.