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

Make basic styles autoformat implementations execute commands #71

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/autoformat.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import BlockAutoformatEditing from './blockautoformatediting';
import InlineAutoformatEditing from './inlineautoformatediting';
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import LiveRange from '@ckeditor/ckeditor5-engine/src/model/liverange';

/**
* Enables a set of predefined autoformatting actions.
Expand Down Expand Up @@ -167,12 +168,17 @@ function getCallbackFunctionForInlineAutoformat( editor, attributeKey ) {
return false;
}

const validRanges = editor.model.schema.getValidRanges( rangesToFormat, attributeKey );
const oldSelectionRange = LiveRange.fromRange( editor.model.document.selection.getFirstRange() );

for ( const range of validRanges ) {
writer.setAttribute( attributeKey, true, range );
for ( const range of rangesToFormat ) {
writer.setSelection( range );

command.execute( { forceValue: true } );
}

writer.setSelection( oldSelectionRange );
oldSelectionRange.detach();

// After applying attribute to the text, remove given attribute from the selection.
// This way user is able to type a text without attribute used by auto formatter.
writer.removeSelectionAttribute( attributeKey );
Expand Down
14 changes: 13 additions & 1 deletion tests/autoformat.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,33 +252,45 @@ describe( 'Autoformat', () => {

describe( 'Inline autoformat', () => {
it( 'should replace both "**" with bold', () => {
const command = editor.commands.get( 'bold' );
sinon.spy( command, 'execute' );

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

expect( command.execute.calledOnce ).to.be.true;
expect( getData( model ) ).to.equal( '<paragraph><$text bold="true">foobar</$text>[]</paragraph>' );
} );

it( 'should replace both "*" with italic', () => {
const command = editor.commands.get( 'italic' );
sinon.spy( command, 'execute' );

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

expect( command.execute.calledOnce ).to.be.true;
expect( getData( model ) ).to.equal( '<paragraph><$text italic="true">foobar</$text>[]</paragraph>' );
} );

it( 'should replace both "`" with code', () => {
const command = editor.commands.get( 'code' );
sinon.spy( command, 'execute' );

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

expect( command.execute.calledOnce ).to.be.true;
expect( getData( model ) ).to.equal( '<paragraph><$text code="true">foobar</$text>[]</paragraph>' );
} );

it( 'nothing should be replaces when typing "*"', () => {
it( 'nothing should be replaced when typing "*"', () => {
setData( model, '<paragraph>foobar[]</paragraph>' );
model.change( writer => {
writer.insertText( '*', doc.selection.getFirstPosition() );
Expand Down
10 changes: 5 additions & 5 deletions tests/undointegration.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe( 'Autoformat undo integration', () => {

expect( getData( model ) ).to.equal( '<paragraph><$text bold="true">foobar</$text>[]</paragraph>' );
editor.execute( 'undo' );
expect( getData( model ) ).to.equal( '<paragraph>**foobar**[]</paragraph>' );
expect( getData( model ) ).to.equal( '<paragraph>**[foobar]**</paragraph>' );
} );

it( 'should undo replacing "__" with bold', () => {
Expand All @@ -72,7 +72,7 @@ describe( 'Autoformat undo integration', () => {

expect( getData( model ) ).to.equal( '<paragraph><$text bold="true">foobar</$text>[]</paragraph>' );
editor.execute( 'undo' );
expect( getData( model ) ).to.equal( '<paragraph>__foobar__[]</paragraph>' );
expect( getData( model ) ).to.equal( '<paragraph>__[foobar]__</paragraph>' );
} );

it( 'should undo replacing "*" with italic', () => {
Expand All @@ -83,7 +83,7 @@ describe( 'Autoformat undo integration', () => {

expect( getData( model ) ).to.equal( '<paragraph><$text italic="true">foobar</$text>[]</paragraph>' );
editor.execute( 'undo' );
expect( getData( model ) ).to.equal( '<paragraph>*foobar*[]</paragraph>' );
expect( getData( model ) ).to.equal( '<paragraph>*[foobar]*</paragraph>' );
} );

it( 'should undo replacing "_" with italic', () => {
Expand All @@ -94,7 +94,7 @@ describe( 'Autoformat undo integration', () => {

expect( getData( model ) ).to.equal( '<paragraph><$text italic="true">foobar</$text>[]</paragraph>' );
editor.execute( 'undo' );
expect( getData( model ) ).to.equal( '<paragraph>_foobar_[]</paragraph>' );
expect( getData( model ) ).to.equal( '<paragraph>_[foobar]_</paragraph>' );
} );

it( 'should undo replacing "`" with code', () => {
Expand All @@ -105,7 +105,7 @@ describe( 'Autoformat undo integration', () => {

expect( getData( model ) ).to.equal( '<paragraph><$text code="true">foobar</$text>[]</paragraph>' );
editor.execute( 'undo' );
expect( getData( model ) ).to.equal( '<paragraph>`foobar`[]</paragraph>' );
expect( getData( model ) ).to.equal( '<paragraph>`[foobar]`</paragraph>' );
} );
} );

Expand Down