imagetextalternative.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 clickOutsideHandler from '@ckeditor/ckeditor5-ui/src/bindings/clickoutsidehandler';
  12. import TextAlternativeFormView from './imagetextalternative/ui/textalternativeformview';
  13. import ContextualBalloon from '@ckeditor/ckeditor5-ui/src/panel/balloon/contextualballoon';
  14. import textAlternativeIcon from '@ckeditor/ckeditor5-core/theme/icons/low-vision.svg';
  15. import { repositionContextualBalloon, getBalloonPositionData } from './image/ui/utils';
  16. import { isImageWidgetSelected } from './image/utils';
  17. /**
  18. * The image text alternative plugin.
  19. *
  20. * The plugin uses the {@link module:ui/panel/balloon/contextualballoon~ContextualBalloon}.
  21. *
  22. * @extends module:core/plugin~Plugin
  23. */
  24. export default class ImageTextAlternative extends Plugin {
  25. /**
  26. * @inheritDoc
  27. */
  28. static get requires() {
  29. return [ ImageTextAlternativeEngine, ContextualBalloon ];
  30. }
  31. /**
  32. * @inheritDoc
  33. */
  34. static get pluginName() {
  35. return 'ImageTextAlternative';
  36. }
  37. /**
  38. * @inheritDoc
  39. */
  40. init() {
  41. this._createButton();
  42. this._createForm();
  43. }
  44. /**
  45. * Creates a button showing the balloon panel for changing the image text alternative and
  46. * registers it in the editor {@link module:ui/componentfactory~ComponentFactory ComponentFactory}.
  47. *
  48. * @private
  49. */
  50. _createButton() {
  51. const editor = this.editor;
  52. const command = editor.commands.get( 'imageTextAlternative' );
  53. const t = editor.t;
  54. editor.ui.componentFactory.add( 'imageTextAlternative', locale => {
  55. const view = new ButtonView( locale );
  56. view.set( {
  57. label: t( 'Change image text alternative' ),
  58. icon: textAlternativeIcon,
  59. tooltip: true
  60. } );
  61. view.bind( 'isEnabled' ).to( command, 'isEnabled' );
  62. this.listenTo( view, 'execute', () => this._showForm() );
  63. return view;
  64. } );
  65. }
  66. /**
  67. * Creates the {@link module:image/imagetextalternative/ui/textalternativeformview~TextAlternativeFormView}
  68. * form.
  69. *
  70. * @private
  71. */
  72. _createForm() {
  73. const editor = this.editor;
  74. const editingView = editor.editing.view;
  75. /**
  76. * The contextual balloon plugin instance.
  77. *
  78. * @private
  79. * @member {module:ui/panel/balloon/contextualballoon~ContextualBalloon}
  80. */
  81. this._balloon = this.editor.plugins.get( 'ContextualBalloon' );
  82. /**
  83. * A form containing a textarea and buttons, used to change the `alt` text value.
  84. *
  85. * @member {module:image/imagetextalternative/ui/textalternativeformview~TextAlternativeFormView} #form
  86. */
  87. this._form = new TextAlternativeFormView( editor.locale );
  88. // Render the form so its #element is available for clickOutsideHandler.
  89. this._form.render();
  90. this.listenTo( this._form, 'submit', () => {
  91. editor.execute( 'imageTextAlternative', {
  92. newValue: this._form.labeledInput.inputView.element.value
  93. } );
  94. this._hideForm( true );
  95. } );
  96. this.listenTo( this._form, 'cancel', () => {
  97. this._hideForm( true );
  98. } );
  99. // Close the form on Esc key press.
  100. this._form.keystrokes.set( 'Esc', ( data, cancel ) => {
  101. this._hideForm( true );
  102. cancel();
  103. } );
  104. // Reposition the balloon or hide the form if an image widget is no longer selected.
  105. this.listenTo( editingView, 'render', () => {
  106. if ( !isImageWidgetSelected( editingView.selection ) ) {
  107. this._hideForm( true );
  108. } else if ( this._isVisible ) {
  109. repositionContextualBalloon( editor );
  110. }
  111. }, { priority: 'low' } );
  112. // Close on click outside of balloon panel element.
  113. clickOutsideHandler( {
  114. emitter: this._form,
  115. activator: () => this._isVisible,
  116. contextElements: [ this._form.element ],
  117. callback: () => this._hideForm()
  118. } );
  119. }
  120. /**
  121. * Shows the {@link #_form} in the {@link #_balloon}.
  122. *
  123. * @private
  124. */
  125. _showForm() {
  126. if ( this._isVisible ) {
  127. return;
  128. }
  129. const editor = this.editor;
  130. const command = editor.commands.get( 'imageTextAlternative' );
  131. const labeledInput = this._form.labeledInput;
  132. if ( !this._balloon.hasView( this._form ) ) {
  133. this._balloon.add( {
  134. view: this._form,
  135. position: getBalloonPositionData( editor )
  136. } );
  137. }
  138. // Make sure that each time the panel shows up, the field remains in sync with the value of
  139. // the command. If the user typed in the input, then canceled the balloon (`labeledInput#value`
  140. // stays unaltered) and re-opened it without changing the value of the command, they would see the
  141. // old value instead of the actual value of the command.
  142. // https://github.com/ckeditor/ckeditor5-image/issues/114
  143. labeledInput.value = labeledInput.inputView.element.value = command.value || '';
  144. this._form.labeledInput.select();
  145. }
  146. /**
  147. * Removes the {@link #_form} from the {@link #_balloon}.
  148. *
  149. * @param {Boolean} [focusEditable=false] Controls whether the editing view is focused afterwards.
  150. * @private
  151. */
  152. _hideForm( focusEditable ) {
  153. if ( !this._isVisible ) {
  154. return;
  155. }
  156. this._balloon.remove( this._form );
  157. if ( focusEditable ) {
  158. this.editor.editing.view.focus();
  159. }
  160. }
  161. /**
  162. * Returns `true` when the {@link #_form} is the visible view
  163. * in the {@link #_balloon}.
  164. *
  165. * @private
  166. * @type {Boolean}
  167. */
  168. get _isVisible() {
  169. return this._balloon.visibleView == this._form;
  170. }
  171. }