8
0

imagetextalternative.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 'image/imagetextalternative';
  35. }
  36. /**
  37. * @inheritDoc
  38. */
  39. init() {
  40. this._createButton();
  41. // Push button to default image toolbar if the image toolbar was loaded.
  42. const defaultImageToolbarConfig = this.editor.config.get( 'image.defaultToolbar' );
  43. if ( defaultImageToolbarConfig ) {
  44. if ( defaultImageToolbarConfig.length ) {
  45. defaultImageToolbarConfig.push( '|' );
  46. }
  47. defaultImageToolbarConfig.push( 'imageTextAlternative' );
  48. }
  49. return this._createBalloonPanel().then( panel => {
  50. /**
  51. * Balloon panel containing text alternative change form.
  52. *
  53. * @member {module:image/ui/imageballoonpanel~ImageBalloonPanelView} #baloonPanel
  54. */
  55. this.balloonPanel = panel;
  56. /**
  57. * Form containing textarea and buttons, used to change `alt` text value.
  58. *
  59. * @member {module:image/imagetextalternative/ui/textalternativeformview~TextAlternativeFormView} #form
  60. */
  61. this.form = panel.content.get( 0 );
  62. } );
  63. }
  64. /**
  65. * Creates button showing text alternative change balloon panel and registers it in
  66. * editor's {@link module:ui/componentfactory~ComponentFactory ComponentFactory}.
  67. *
  68. * @private
  69. */
  70. _createButton() {
  71. const editor = this.editor;
  72. const command = editor.commands.get( 'imageTextAlternative' );
  73. const t = editor.t;
  74. editor.ui.componentFactory.add( 'imageTextAlternative', ( locale ) => {
  75. const view = new ButtonView( locale );
  76. view.set( {
  77. label: t( 'Change image text alternative' ),
  78. icon: textAlternativeIcon,
  79. tooltip: true
  80. } );
  81. view.bind( 'isEnabled' ).to( command, 'isEnabled' );
  82. this.listenTo( view, 'execute', () => this._showBalloonPanel() );
  83. return view;
  84. } );
  85. }
  86. /**
  87. * Creates balloon panel.
  88. *
  89. * @private
  90. * @return {Promise.<module:image/image/ui/imageballoonpanel~ImageBalloonPanelView>}
  91. */
  92. _createBalloonPanel() {
  93. const editor = this.editor;
  94. const panel = new ImageBalloonPanel( editor );
  95. const form = new TextAlternativeFormView( editor.locale );
  96. this.listenTo( form, 'submit', () => {
  97. editor.execute( 'imageTextAlternative', { newValue: form.lebeledInput.inputView.element.value } );
  98. this._hideBalloonPanel();
  99. } );
  100. // If image toolbar is present - hide it when text alternative balloon is visible.
  101. const imageToolbar = editor.plugins.get( ImageToolbar );
  102. if ( imageToolbar ) {
  103. this.listenTo( panel, 'change:isVisible', () => {
  104. if ( panel.isVisible ) {
  105. imageToolbar.hide();
  106. imageToolbar.isEnabled = false;
  107. } else {
  108. imageToolbar.show();
  109. imageToolbar.isEnabled = true;
  110. }
  111. } );
  112. }
  113. this.listenTo( form, 'cancel', () => this._hideBalloonPanel() );
  114. // Close on `ESC` press.
  115. escPressHandler( {
  116. emitter: panel,
  117. activator: () => panel.isVisible,
  118. callback: () => this._hideBalloonPanel()
  119. } );
  120. // Close on click outside of balloon panel element.
  121. clickOutsideHandler( {
  122. emitter: panel,
  123. activator: () => panel.isVisible,
  124. contextElement: panel.element,
  125. callback: () => this._hideBalloonPanel()
  126. } );
  127. return Promise.all( [
  128. panel.content.add( form ),
  129. editor.ui.view.body.add( panel )
  130. ] ).then( () => panel ) ;
  131. }
  132. /**
  133. * Shows the balloon panel.
  134. *
  135. * @private
  136. */
  137. _showBalloonPanel() {
  138. const editor = this.editor;
  139. const command = editor.commands.get( 'imageTextAlternative' );
  140. this.form.lebeledInput.value = command.value || '';
  141. this.balloonPanel.attach();
  142. this.form.lebeledInput.select();
  143. }
  144. /**
  145. * Hides the balloon panel.
  146. *
  147. * @private
  148. */
  149. _hideBalloonPanel() {
  150. const editor = this.editor;
  151. this.balloonPanel.detach();
  152. editor.editing.view.focus();
  153. }
  154. }