imagetextalternative.js 4.6 KB

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