8
0

imagetextalternative.js 5.6 KB

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