imagetextalternativeui.js 5.2 KB

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