8
0

imagetextalternative.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module image/imagetextalternative
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  10. import ImageTextAlternativeEngine from './imagetextalternative/imagetextalternativeengine';
  11. import escPressHandler from '@ckeditor/ckeditor5-ui/src/bindings/escpresshandler';
  12. import clickOutsideHandler from '@ckeditor/ckeditor5-ui/src/bindings/clickoutsidehandler';
  13. import ImageToolbar from './imagetoolbar';
  14. import TextAlternativeFormView from './imagetextalternative/ui/textalternativeformview';
  15. import ImageBalloonPanel from './image/ui/imageballoonpanelview';
  16. import textAlternativeIcon from '@ckeditor/ckeditor5-core/theme/icons/low-vision.svg';
  17. import '../theme/imagetextalternative/theme.scss';
  18. /**
  19. * The image text alternative plugin.
  20. *
  21. * @extends module:core/plugin~Plugin
  22. */
  23. export default class ImageTextAlternative extends Plugin {
  24. /**
  25. * @inheritDoc
  26. */
  27. static get requires() {
  28. return [ ImageTextAlternativeEngine ];
  29. }
  30. /**
  31. * @inheritDoc
  32. */
  33. static get pluginName() {
  34. return 'ImageTextAlternative';
  35. }
  36. /**
  37. * @inheritDoc
  38. */
  39. init() {
  40. this._createButton();
  41. return this._createBalloonPanel().then( panel => {
  42. /**
  43. * Balloon panel containing text alternative change form.
  44. *
  45. * @member {module:image/image/ui/imageballoonpanel~ImageBalloonPanelView} #baloonPanel
  46. */
  47. this.balloonPanel = panel;
  48. /**
  49. * Form containing textarea and buttons, used to change `alt` text value.
  50. *
  51. * @member {module:image/imagetextalternative/ui/textalternativeformview~TextAlternativeFormView} #form
  52. */
  53. this.form = panel.content.get( 0 );
  54. } );
  55. }
  56. /**
  57. * Creates button showing text alternative change balloon panel and registers it in
  58. * editor's {@link module:ui/componentfactory~ComponentFactory ComponentFactory}.
  59. *
  60. * @private
  61. */
  62. _createButton() {
  63. const editor = this.editor;
  64. const command = editor.commands.get( 'imageTextAlternative' );
  65. const t = editor.t;
  66. editor.ui.componentFactory.add( 'imageTextAlternative', locale => {
  67. const view = new ButtonView( locale );
  68. view.set( {
  69. label: t( 'Change image text alternative' ),
  70. icon: textAlternativeIcon,
  71. tooltip: true
  72. } );
  73. view.bind( 'isEnabled' ).to( command, 'isEnabled' );
  74. this.listenTo( view, 'execute', () => this._showBalloonPanel() );
  75. return view;
  76. } );
  77. }
  78. /**
  79. * Creates balloon panel.
  80. *
  81. * @private
  82. * @return {Promise.<module:image/image/ui/imageballoonpanel~ImageBalloonPanelView>}
  83. */
  84. _createBalloonPanel() {
  85. const editor = this.editor;
  86. const panel = new ImageBalloonPanel( editor );
  87. const form = new TextAlternativeFormView( editor.locale );
  88. this.listenTo( form, 'submit', () => {
  89. editor.execute( 'imageTextAlternative', { newValue: form.labeledInput.inputView.element.value } );
  90. this._hideBalloonPanel();
  91. } );
  92. // If image toolbar is present - hide it when text alternative balloon is visible.
  93. const imageToolbar = editor.plugins.get( ImageToolbar );
  94. if ( imageToolbar ) {
  95. this.listenTo( panel, 'change:isVisible', () => {
  96. if ( panel.isVisible ) {
  97. imageToolbar.hide();
  98. imageToolbar.isEnabled = false;
  99. } else {
  100. imageToolbar.show();
  101. imageToolbar.isEnabled = true;
  102. }
  103. } );
  104. }
  105. this.listenTo( form, 'cancel', () => this._hideBalloonPanel() );
  106. // Close on `ESC` press.
  107. escPressHandler( {
  108. emitter: panel,
  109. activator: () => panel.isVisible,
  110. callback: () => this._hideBalloonPanel()
  111. } );
  112. // Close on click outside of balloon panel element.
  113. clickOutsideHandler( {
  114. emitter: panel,
  115. activator: () => panel.isVisible,
  116. contextElement: panel.element,
  117. callback: () => this._hideBalloonPanel()
  118. } );
  119. return Promise.all( [
  120. panel.content.add( form ),
  121. editor.ui.view.body.add( panel )
  122. ] ).then( () => panel );
  123. }
  124. /**
  125. * Shows the balloon panel.
  126. *
  127. * @private
  128. */
  129. _showBalloonPanel() {
  130. const editor = this.editor;
  131. const command = editor.commands.get( 'imageTextAlternative' );
  132. const labeledInput = this.form.labeledInput;
  133. this.balloonPanel.attach();
  134. // Make sure that each time the panel shows up, the field remains in sync with the value of
  135. // the command. If the user typed in the input, then canceled the balloon (`labeledInput#value`
  136. // stays unaltered) and re-opened it without changing the value of the command, they would see the
  137. // old value instead of the actual value of the command.
  138. // https://github.com/ckeditor/ckeditor5-image/issues/114
  139. labeledInput.value = labeledInput.inputView.element.value = command.value || '';
  140. this.form.labeledInput.select();
  141. }
  142. /**
  143. * Hides the balloon panel.
  144. *
  145. * @private
  146. */
  147. _hideBalloonPanel() {
  148. const editor = this.editor;
  149. this.balloonPanel.detach();
  150. editor.editing.view.focus();
  151. }
  152. }