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

Commit 9b066f1

Browse files
author
Piotr Jasiun
authored
Merge pull request #67 from ckeditor/t/66
Feature: Cancel `BlockAutoformatEditing` autoformatting if given callback returned `false`. Closes #66.
2 parents cc7f454 + a2f484d commit 9b066f1

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

src/autoformat.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ export default class Autoformat extends Plugin {
128128

129129
// eslint-disable-next-line no-new
130130
new BlockAutoformatEditing( this.editor, pattern, () => {
131+
if ( !command.isEnabled ) {
132+
return false;
133+
}
134+
131135
this.editor.execute( 'heading', { value: commandValue } );
132136
} );
133137
} );

src/blockautoformatediting.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
* @module autoformat/blockautoformatediting
88
*/
99

10+
import LiveRange from '@ckeditor/ckeditor5-engine/src/model/liverange';
11+
1012
/**
1113
* The block autoformatting engine. It allows to format various block patterns. For example,
1214
* it can be configured to turn a paragraph starting with `*` and followed by a space into a list item.
@@ -78,6 +80,7 @@ export default class BlockAutoformatEditing {
7880
if ( changes.length != 1 || entry.type !== 'insert' || entry.name != '$text' || entry.length != 1 ) {
7981
return;
8082
}
83+
8184
const item = entry.position.textNode || entry.position.nodeAfter;
8285

8386
if ( !item.parent.is( 'paragraph' ) ) {
@@ -95,12 +98,16 @@ export default class BlockAutoformatEditing {
9598
// Matched range.
9699
const start = writer.createPositionAt( item.parent, 0 );
97100
const end = writer.createPositionAt( item.parent, match[ 0 ].length );
98-
const range = writer.createRange( start, end );
101+
const range = new LiveRange( start, end );
102+
103+
const wasChanged = callback( { match } );
99104

100105
// Remove matched text.
101-
writer.remove( range );
106+
if ( wasChanged !== false ) {
107+
writer.remove( range );
108+
}
102109

103-
callback( { match } );
110+
range.detach();
104111
} );
105112
} );
106113
}

tests/autoformat.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ describe( 'Autoformat', () => {
154154

155155
function HeadingPlugin( editor ) {
156156
editor.commands.add( 'heading', command );
157+
command.refresh();
157158
}
158159

159160
return VirtualTestEditor
@@ -187,6 +188,19 @@ describe( 'Autoformat', () => {
187188
return editor.destroy();
188189
} );
189190
} );
191+
192+
it( 'should not replace if heading command is disabled', () => {
193+
setData( model, '<paragraph>#[]</paragraph>' );
194+
195+
model.change( writer => {
196+
editor.commands.get( 'heading' ).refresh = () => {};
197+
editor.commands.get( 'heading' ).isEnabled = false;
198+
199+
writer.insertText( ' ', doc.selection.getFirstPosition() );
200+
} );
201+
202+
expect( getData( model ) ).to.equal( '<paragraph># []</paragraph>' );
203+
} );
190204
} );
191205

192206
describe( 'Block quote', () => {

tests/blockautoformatediting.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,17 @@ describe( 'BlockAutoformatEditing', () => {
123123

124124
sinon.assert.notCalled( spy );
125125
} );
126+
127+
it( 'should stop if callback returned false', () => {
128+
new BlockAutoformatEditing( editor, /^[*]\s$/, () => false ); // eslint-disable-line no-new
129+
130+
setData( model, '<paragraph>*[]</paragraph>' );
131+
model.change( writer => {
132+
writer.insertText( ' ', doc.selection.getFirstPosition() );
133+
} );
134+
135+
expect( getData( model ) ).to.equal( '<paragraph>* []</paragraph>' );
136+
} );
126137
} );
127138

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

0 commit comments

Comments
 (0)