diff --git a/src/autoformat.js b/src/autoformat.js index 31fec83..3af45ba 100644 --- a/src/autoformat.js +++ b/src/autoformat.js @@ -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. @@ -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 ); diff --git a/tests/autoformat.js b/tests/autoformat.js index 40e580f..0bfb797 100644 --- a/tests/autoformat.js +++ b/tests/autoformat.js @@ -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, '**foobar*[]' ); model.change( writer => { writer.insertText( '*', doc.selection.getFirstPosition() ); } ); + expect( command.execute.calledOnce ).to.be.true; expect( getData( model ) ).to.equal( '<$text bold="true">foobar[]' ); } ); it( 'should replace both "*" with italic', () => { + const command = editor.commands.get( 'italic' ); + sinon.spy( command, 'execute' ); + setData( model, '*foobar[]' ); model.change( writer => { writer.insertText( '*', doc.selection.getFirstPosition() ); } ); + expect( command.execute.calledOnce ).to.be.true; expect( getData( model ) ).to.equal( '<$text italic="true">foobar[]' ); } ); it( 'should replace both "`" with code', () => { + const command = editor.commands.get( 'code' ); + sinon.spy( command, 'execute' ); + setData( model, '`foobar[]' ); model.change( writer => { writer.insertText( '`', doc.selection.getFirstPosition() ); } ); + expect( command.execute.calledOnce ).to.be.true; expect( getData( model ) ).to.equal( '<$text code="true">foobar[]' ); } ); - it( 'nothing should be replaces when typing "*"', () => { + it( 'nothing should be replaced when typing "*"', () => { setData( model, 'foobar[]' ); model.change( writer => { writer.insertText( '*', doc.selection.getFirstPosition() ); diff --git a/tests/undointegration.js b/tests/undointegration.js index f03a67c..1bc3882 100644 --- a/tests/undointegration.js +++ b/tests/undointegration.js @@ -61,7 +61,7 @@ describe( 'Autoformat undo integration', () => { expect( getData( model ) ).to.equal( '<$text bold="true">foobar[]' ); editor.execute( 'undo' ); - expect( getData( model ) ).to.equal( '**foobar**[]' ); + expect( getData( model ) ).to.equal( '**[foobar]**' ); } ); it( 'should undo replacing "__" with bold', () => { @@ -72,7 +72,7 @@ describe( 'Autoformat undo integration', () => { expect( getData( model ) ).to.equal( '<$text bold="true">foobar[]' ); editor.execute( 'undo' ); - expect( getData( model ) ).to.equal( '__foobar__[]' ); + expect( getData( model ) ).to.equal( '__[foobar]__' ); } ); it( 'should undo replacing "*" with italic', () => { @@ -83,7 +83,7 @@ describe( 'Autoformat undo integration', () => { expect( getData( model ) ).to.equal( '<$text italic="true">foobar[]' ); editor.execute( 'undo' ); - expect( getData( model ) ).to.equal( '*foobar*[]' ); + expect( getData( model ) ).to.equal( '*[foobar]*' ); } ); it( 'should undo replacing "_" with italic', () => { @@ -94,7 +94,7 @@ describe( 'Autoformat undo integration', () => { expect( getData( model ) ).to.equal( '<$text italic="true">foobar[]' ); editor.execute( 'undo' ); - expect( getData( model ) ).to.equal( '_foobar_[]' ); + expect( getData( model ) ).to.equal( '_[foobar]_' ); } ); it( 'should undo replacing "`" with code', () => { @@ -105,7 +105,7 @@ describe( 'Autoformat undo integration', () => { expect( getData( model ) ).to.equal( '<$text code="true">foobar[]' ); editor.execute( 'undo' ); - expect( getData( model ) ).to.equal( '`foobar`[]' ); + expect( getData( model ) ).to.equal( '`[foobar]`' ); } ); } );