imagealternatetext.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module image/image
  7. */
  8. import Plugin from 'ckeditor5-core/src/plugin';
  9. import ImageAlternateTextCommand from './imagealternatetextcommand';
  10. import ButtonView from 'ckeditor5-ui/src/button/buttonview';
  11. import clickOutsideHandler from 'ckeditor5-ui/src/bindings/clickoutsidehandler';
  12. import escPressHandler from 'ckeditor5-ui/src/bindings/escpresshandler';
  13. import ImageToolbar from '../imagetoolbar';
  14. import AlternateTextFormView from './ui/alternatetextformview';
  15. import ImageBalloonPanel from '../ui/imageballoonpanel';
  16. import alternateTextIcon from 'ckeditor5-core/theme/icons/source.svg';
  17. // TODO: move part of the theme to this sub-directory.
  18. /**
  19. * The image alternate text plugin.
  20. *
  21. * @extends module:core/plugin~Plugin
  22. */
  23. export default class ImageAlternateText extends Plugin {
  24. /**
  25. * @inheritDoc
  26. */
  27. init() {
  28. // TODO: Register ImageAlternateTextCommand in engine part.
  29. this.editor.commands.set( 'imageAlternateText', new ImageAlternateTextCommand( this.editor ) );
  30. // TODO: docs for this._panel and this._form.
  31. return Promise.all( [
  32. this._createButton(),
  33. this._createBalloonPanel()
  34. ] );
  35. }
  36. /**
  37. * Creates button showing alternate text change balloon panel and registers it in
  38. * editor's {@link module:ui/componentfactory~ComponentFactory ComponentFactory}.
  39. *
  40. * @private
  41. */
  42. _createButton() {
  43. const editor = this.editor;
  44. const command = editor.commands.get( 'imageAlternateText' );
  45. const t = editor.t;
  46. return editor.ui.componentFactory.add( 'imageAlternateText', ( locale ) => {
  47. const view = new ButtonView( locale );
  48. view.set( {
  49. label: t( 'Change alternate text' ),
  50. icon: alternateTextIcon
  51. } );
  52. view.bind( 'isEnabled' ).to( command, 'isEnabled' );
  53. this.listenTo( view, 'execute', () => this._showBalloonPanel() );
  54. return view;
  55. } );
  56. }
  57. _createBalloonPanel() {
  58. const editor = this.editor;
  59. const panel = new ImageBalloonPanel( editor );
  60. const form = this._form = new AlternateTextFormView( editor.locale );
  61. this.listenTo( form, 'submit', () => {
  62. editor.execute( 'imageAlternateText', { newValue: form.labeledTextarea.inputView.element.value } );
  63. this._hideBalloonPanel();
  64. } );
  65. this.listenTo( form, 'cancel', () => this._hideBalloonPanel() );
  66. // Close on `ESC` press.
  67. escPressHandler( {
  68. emitter: panel,
  69. activator: () => panel.isVisible,
  70. callback: () => this._hideBalloonPanel()
  71. } );
  72. // Close on click outside of balloon panel element.
  73. clickOutsideHandler( {
  74. emitter: panel,
  75. activator: () => panel.isVisible,
  76. contextElement: panel.element,
  77. callback: () => this._hideBalloonPanel()
  78. } );
  79. this._panel = panel;
  80. return Promise.all( [
  81. panel.content.add( this._form ),
  82. editor.ui.view.body.add( panel )
  83. ] );
  84. }
  85. _showBalloonPanel() {
  86. const editor = this.editor;
  87. const command = editor.commands.get( 'imageAlternateText' );
  88. const imageToolbar = editor.plugins.get( ImageToolbar );
  89. if ( imageToolbar ) {
  90. imageToolbar.hide();
  91. }
  92. this._form.labeledTextarea.value = command.value || '';
  93. this._form.labeledTextarea.select();
  94. this._panel.attach();
  95. }
  96. _hideBalloonPanel() {
  97. const editor = this.editor;
  98. this._panel.detach();
  99. editor.editing.view.focus();
  100. const imageToolbar = editor.plugins.get( ImageToolbar );
  101. if ( imageToolbar ) {
  102. imageToolbar.show();
  103. }
  104. }
  105. }