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

Commit 2c0044c

Browse files
committed
Other: Aligned the implementation to the new Command API (see https://github.com/ckeditor/ckeditor5-core/issues/88).
BREAKING CHANGES: The command API has been changed.
2 parents 9b105ed + e5cd232 commit 2c0044c

File tree

7 files changed

+38
-92
lines changed

7 files changed

+38
-92
lines changed

src/imagestyle/imagestylecommand.js

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
* @module image/imagestyle/imagestylecommand
88
*/
99

10-
import Command from '@ckeditor/ckeditor5-core/src/command/command';
10+
import Command from '@ckeditor/ckeditor5-core/src/command';
1111
import { isImage } from '../image/utils';
1212

1313
/**
1414
* The image style command. It is used to apply different image styles.
1515
*
16-
* @extends module:core/command/command~Command
16+
* @extends module:core/command~Command
1717
*/
1818
export default class ImageStyleCommand extends Command {
1919
/**
@@ -26,14 +26,13 @@ export default class ImageStyleCommand extends Command {
2626
super( editor );
2727

2828
/**
29-
* The current command value - `true` if style handled by the command is applied on currently selected image,
29+
* The value of the command - `true` if style handled by the command is applied on currently selected image,
3030
* `false` otherwise.
3131
*
3232
* @readonly
3333
* @observable
3434
* @member {Boolean} #value
3535
*/
36-
this.set( 'value', false );
3736

3837
/**
3938
* Style handled by this command.
@@ -42,63 +41,40 @@ export default class ImageStyleCommand extends Command {
4241
* @member {module:image/imagestyle/imagestyleengine~ImageStyleFormat} #style
4342
*/
4443
this.style = style;
45-
46-
// Update current value and refresh state each time something change in model document.
47-
this.listenTo( editor.document, 'changesDone', () => {
48-
this._updateValue();
49-
this.refreshState();
50-
} );
5144
}
5245

5346
/**
54-
* Updates command's value.
55-
*
56-
* @private
47+
* @inheritDoc
5748
*/
58-
_updateValue() {
59-
const doc = this.editor.document;
60-
const element = doc.selection.getSelectedElement();
49+
refresh() {
50+
const element = this.editor.document.selection.getSelectedElement();
51+
52+
this.isEnabled = isImage( element );
6153

6254
if ( !element ) {
6355
this.value = false;
64-
65-
return;
66-
}
67-
68-
if ( this.style.value === null ) {
56+
} else if ( this.style.value === null ) {
6957
this.value = !element.hasAttribute( 'imageStyle' );
7058
} else {
7159
this.value = ( element.getAttribute( 'imageStyle' ) == this.style.value );
7260
}
7361
}
7462

75-
/**
76-
* @inheritDoc
77-
*/
78-
_checkEnabled() {
79-
const element = this.editor.document.selection.getSelectedElement();
80-
81-
return isImage( element );
82-
}
83-
8463
/**
8564
* Executes command.
8665
*
87-
* @protected
66+
* @fires execute
8867
* @param {Object} options
8968
* @param {module:engine/model/batch~Batch} [options.batch] Batch to collect all the change steps. New batch will be
9069
* created if this option is not set.
9170
*/
92-
_doExecute( options = {} ) {
93-
// Stop if style is already applied.
71+
execute( options = {} ) {
9472
if ( this.value ) {
9573
return;
9674
}
9775

98-
const editor = this.editor;
99-
const doc = editor.document;
100-
const selection = doc.selection;
101-
const imageElement = selection.getSelectedElement();
76+
const doc = this.editor.document;
77+
const imageElement = doc.selection.getSelectedElement();
10278

10379
doc.enqueueChanges( () => {
10480
const batch = options.batch || doc.batch();

src/imagestyle/imagestyleengine.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export default class ImageStyleEngine extends Plugin {
6969

7070
// Register separate command for each style.
7171
for ( const style of styles ) {
72-
editor.commands.set( style.name, new ImageStyleCommand( editor, style ) );
72+
editor.commands.add( style.name, new ImageStyleCommand( editor, style ) );
7373
}
7474
}
7575
}
@@ -89,7 +89,7 @@ export default class ImageStyleEngine extends Plugin {
8989
*
9090
* @typedef {Object} module:image/imagestyle/imagestyleengine~ImageStyleFormat
9191
* @property {String} name Name of the style. It will be used to:
92-
* * register {@link module:core/command/command~Command command} which will apply this style,
92+
* * register {@link module:core/command~Command command} which will apply this style,
9393
* * store style's button in editor's {@link module:ui/componentfactory~ComponentFactory ComponentFactory}.
9494
* @property {String} value Value used to store this style in model attribute.
9595
* When value is `null` style will be used as default one. Default style does not apply any CSS class to the view element.

src/imagetextalternative/imagetextalternativecommand.js

Lines changed: 16 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,30 @@
77
* @module image/imagelaternatetext/imagetextalternativecommand
88
*/
99

10-
import Command from '@ckeditor/ckeditor5-core/src/command/command';
10+
import Command from '@ckeditor/ckeditor5-core/src/command';
1111
import { isImage } from '../image/utils';
1212

1313
/**
1414
* The image text alternative command. It is used to change `alt` attribute on `image` elements.
1515
*
16-
* @extends module:core/command/command~Command
16+
* @extends module:core/command~Command
1717
*/
1818
export default class ImageTextAlternativeCommand extends Command {
1919
/**
20-
* @inheritDoc
20+
* The command value - `false` if there is no `alt` attribute, otherwise the value of the `alt` attribute.
21+
*
22+
* @readonly
23+
* @observable
24+
* @member {String|Boolean} #value
2125
*/
22-
constructor( editor ) {
23-
super( editor );
24-
25-
/**
26-
* The current command value - `false` if there is no `alt` attribute, otherwise contains string with `alt`
27-
* attribute value.
28-
*
29-
* @readonly
30-
* @observable
31-
* @member {String|Boolean} #value
32-
*/
33-
this.set( 'value', false );
34-
35-
// Update current value and refresh state each time something change in model document.
36-
this.listenTo( editor.document, 'changesDone', () => {
37-
this._updateValue();
38-
this.refreshState();
39-
} );
40-
}
4126

4227
/**
43-
* Updates command's value.
44-
*
45-
* @private
28+
* @inheritDoc
4629
*/
47-
_updateValue() {
48-
const doc = this.editor.document;
49-
const element = doc.selection.getSelectedElement();
30+
refresh() {
31+
const element = this.editor.document.selection.getSelectedElement();
32+
33+
this.isEnabled = isImage( element );
5034

5135
if ( isImage( element ) && element.hasAttribute( 'alt' ) ) {
5236
this.value = element.getAttribute( 'alt' );
@@ -56,26 +40,16 @@ export default class ImageTextAlternativeCommand extends Command {
5640
}
5741

5842
/**
59-
* @inheritDoc
60-
*/
61-
_checkEnabled() {
62-
const element = this.editor.document.selection.getSelectedElement();
63-
64-
return isImage( element );
65-
}
66-
67-
/**
68-
* Executes command.
43+
* Executes the command.
6944
*
70-
* @protected
45+
* @fires execute
7146
* @param {Object} options
7247
* @param {String} options.newValue New value of `alt` attribute to set.
7348
* @param {module:engine/model/batch~Batch} [options.batch] Batch to collect all the change steps. New batch will be
7449
* created if this option is not set.
7550
*/
76-
_doExecute( options ) {
77-
const editor = this.editor;
78-
const doc = editor.document;
51+
execute( options ) {
52+
const doc = this.editor.document;
7953
const imageElement = doc.selection.getSelectedElement();
8054

8155
doc.enqueueChanges( () => {

src/imagetextalternative/imagetextalternativeengine.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ export default class ImageTextAlternativeEngine extends Plugin {
2121
* @inheritDoc
2222
*/
2323
init() {
24-
this.editor.commands.set( 'imageTextAlternative', new ImageTextAlternativeCommand( this.editor ) );
24+
this.editor.commands.add( 'imageTextAlternative', new ImageTextAlternativeCommand( this.editor ) );
2525
}
2626
}

tests/imagestyle/imagestylecommand.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ describe( 'ImageStyleCommand', () => {
6060
it( 'should set proper value when executed', () => {
6161
setData( document, '[<image></image>]' );
6262

63-
otherStyleCommand._doExecute();
63+
otherStyleCommand.execute();
6464

6565
expect( getData( document ) ).to.equal( '[<image imageStyle="other"></image>]' );
6666
} );
6767

6868
it( 'should do nothing when attribute already present', () => {
6969
setData( document, '[<image imageStyle="other"></image>]' );
7070

71-
otherStyleCommand._doExecute();
71+
otherStyleCommand.execute();
7272

7373
expect( getData( document ) ).to.equal( '[<image imageStyle="other"></image>]' );
7474
} );
@@ -79,7 +79,7 @@ describe( 'ImageStyleCommand', () => {
7979

8080
setData( document, '[<image></image>]' );
8181

82-
otherStyleCommand._doExecute( { batch } );
82+
otherStyleCommand.execute( { batch } );
8383

8484
expect( getData( document ) ).to.equal( '[<image imageStyle="other"></image>]' );
8585
sinon.assert.calledOnce( spy );

tests/imagestyle/imagestyleengine.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ describe( 'ImageStyleEngine', () => {
4646
} );
4747

4848
it( 'should register separate command for each style', () => {
49-
expect( editor.commands.has( 'fullStyle' ) ).to.be.true;
50-
expect( editor.commands.has( 'sideStyle' ) ).to.be.true;
51-
expect( editor.commands.has( 'dummyStyle' ) ).to.be.true;
52-
5349
expect( editor.commands.get( 'fullStyle' ) ).to.be.instanceOf( ImageStyleCommand );
5450
expect( editor.commands.get( 'sideStyle' ) ).to.be.instanceOf( ImageStyleCommand );
5551
expect( editor.commands.get( 'dummyStyle' ) ).to.be.instanceOf( ImageStyleCommand );

tests/imagetextalternative/imagetextalternativecommand.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ describe( 'ImageTextAlternativeCommand', () => {
5858
it( 'should set proper alt if executed on image without alt attribute', () => {
5959
setData( document, '[<image src="image.png"></image>]' );
6060

61-
command._doExecute( { newValue: 'fiz buz' } );
61+
command.execute( { newValue: 'fiz buz' } );
6262

6363
expect( getData( document ) ).to.equal( '[<image alt="fiz buz" src="image.png"></image>]' );
6464
} );
6565

6666
it( 'should change alt if executed on image with alt attribute', () => {
6767
setData( document, '[<image alt="foo bar" src="image.png"></image>]' );
6868

69-
command._doExecute( { newValue: 'fiz buz' } );
69+
command.execute( { newValue: 'fiz buz' } );
7070

7171
expect( getData( document ) ).to.equal( '[<image alt="fiz buz" src="image.png"></image>]' );
7272
} );
@@ -77,7 +77,7 @@ describe( 'ImageTextAlternativeCommand', () => {
7777

7878
setData( document, '[<image src="image.png"></image>]' );
7979

80-
command._doExecute( { newValue: 'foo bar', batch } );
80+
command.execute( { newValue: 'foo bar', batch } );
8181

8282
expect( getData( document ) ).to.equal( '[<image alt="foo bar" src="image.png"></image>]' );
8383
sinon.assert.calledOnce( spy );

0 commit comments

Comments
 (0)