imagestylecommand.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module image/imagestyle/imagestylecommand
  7. */
  8. import Command from '../../core/command/command.js';
  9. import ModelElement from '../../engine/model/element.js';
  10. export default class ImageStyleCommand extends Command {
  11. constructor( editor ) {
  12. super( editor );
  13. this.set( 'value', false );
  14. const document = this.editor.document;
  15. this.listenTo( document.selection, 'change', () => {
  16. const element = getSelectedElement( document.selection );
  17. if ( element && element.name === 'image' && element.hasAttribute( 'style' ) ) {
  18. this.value = element.getAttribute( 'style' );
  19. } else {
  20. this.value = false;
  21. }
  22. } );
  23. this.listenTo( document, 'changesDone', () => {
  24. this.refreshState();
  25. } );
  26. }
  27. _checkEnabled() {
  28. const document = this.editor.document;
  29. const element = getSelectedElement( document.selection );
  30. // Todo: add schema checking.
  31. return element && element.name === 'image';
  32. }
  33. }
  34. // TODO: duplicated code - move to model selection.
  35. function getSelectedElement( modelSelection ) {
  36. if ( modelSelection.rangeCount !== 1 ) {
  37. return null;
  38. }
  39. const range = modelSelection.getFirstRange();
  40. const nodeAfterStart = range.start.nodeAfter;
  41. const nodeBeforeEnd = range.end.nodeBefore;
  42. return ( nodeAfterStart instanceof ModelElement && nodeAfterStart == nodeBeforeEnd ) ? nodeAfterStart : null;
  43. }