|
|
@@ -40,17 +40,7 @@ export default class HeadingCommand extends Command {
|
|
|
this.set( 'value', this.defaultFormat );
|
|
|
|
|
|
// Listen on selection change and set current command's format to format in the current selection.
|
|
|
- this.listenTo( editor.document.selection, 'change', () => {
|
|
|
- const position = editor.document.selection.getFirstPosition();
|
|
|
- const block = findTopmostBlock( position );
|
|
|
-
|
|
|
- if ( block ) {
|
|
|
- const format = this._getFormatById( block.name );
|
|
|
-
|
|
|
- // TODO: What should happen if format is not found?
|
|
|
- this.value = format;
|
|
|
- }
|
|
|
- } );
|
|
|
+ this.listenTo( editor.document.selection, 'change', () => this._updateValue() );
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -128,6 +118,9 @@ export default class HeadingCommand extends Command {
|
|
|
// If range's selection start/end is placed directly in renamed block - we need to restore it's position
|
|
|
// after renaming, because renaming puts new element there.
|
|
|
doc.selection.setRanges( ranges, isSelectionBackward );
|
|
|
+
|
|
|
+ // Update command's value after change.
|
|
|
+ this._updateValue();
|
|
|
} );
|
|
|
}
|
|
|
|
|
|
@@ -141,6 +134,23 @@ export default class HeadingCommand extends Command {
|
|
|
_getFormatById( id ) {
|
|
|
return this.formats.find( item => item.id === id ) || this.defaultFormat;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Updates command's {@link heading.HeadingCommand#value value} based on current selection.
|
|
|
+ *
|
|
|
+ * @private
|
|
|
+ */
|
|
|
+ _updateValue() {
|
|
|
+ const position = this.editor.document.selection.getFirstPosition();
|
|
|
+ const block = findTopmostBlock( position );
|
|
|
+
|
|
|
+ if ( block ) {
|
|
|
+ const format = this._getFormatById( block.name );
|
|
|
+
|
|
|
+ // TODO: What should happen if format is not found?
|
|
|
+ this.value = format;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// Looks for the topmost element in the position's ancestor (up to an element in the root).
|