8
0

link.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module link/link
  7. */
  8. import Plugin from 'ckeditor5-core/src/plugin';
  9. import ClickObserver from 'ckeditor5-engine/src/view/observer/clickobserver';
  10. import LinkEngine from './linkengine';
  11. import LinkElement from './linkelement';
  12. import clickOutsideHandler from 'ckeditor5-ui/src/bindings/clickoutsidehandler';
  13. import escPressHandler from 'ckeditor5-ui/src/bindings/escpresshandler';
  14. import ButtonView from 'ckeditor5-ui/src/button/buttonview';
  15. import BalloonPanelView from 'ckeditor5-ui/src/balloonpanel/balloonpanelview';
  16. import LinkFormView from './ui/linkformview';
  17. import linkIcon from '../theme/icons/link.svg';
  18. import unlinkIcon from '../theme/icons/unlink.svg';
  19. import '../theme/theme.scss';
  20. /**
  21. * The link feature. It introduces the Link and Unlink buttons and the <kbd>Ctrl+K</kbd> keystroke.
  22. *
  23. * It uses the {@link module:link/linkengine~LinkEngine link engine feature}.
  24. *
  25. * @extends module:core/plugin~Plugin
  26. */
  27. export default class Link extends Plugin {
  28. /**
  29. * @inheritDoc
  30. */
  31. static get requires() {
  32. return [ LinkEngine ];
  33. }
  34. /**
  35. * @inheritDoc
  36. */
  37. init() {
  38. this.editor.editing.view.addObserver( ClickObserver );
  39. /**
  40. * Balloon panel view to display the main UI.
  41. *
  42. * @member {module:link/ui/balloonpanel~BalloonPanelView}
  43. */
  44. this.balloonPanelView = this._createBalloonPanel();
  45. /**
  46. * The form view inside {@link #balloonPanelView}.
  47. *
  48. * @member {module:link/ui/linkformview~LinkFormView}
  49. */
  50. this.formView = this._createForm();
  51. // Create toolbar buttons.
  52. this._createToolbarLinkButton();
  53. this._createToolbarUnlinkButton();
  54. }
  55. /**
  56. * Creates a toolbar link button. Clicking this button will show
  57. * {@link #balloonPanelView} attached to the selection.
  58. *
  59. * @private
  60. */
  61. _createToolbarLinkButton() {
  62. const editor = this.editor;
  63. const linkCommand = editor.commands.get( 'link' );
  64. const t = editor.t;
  65. // Handle `Ctrl+K` keystroke and show panel.
  66. editor.keystrokes.set( 'CTRL+K', () => this._showPanel() );
  67. editor.ui.componentFactory.add( 'link', ( locale ) => {
  68. const button = new ButtonView( locale );
  69. button.isEnabled = true;
  70. button.label = t( 'Link' );
  71. button.icon = linkIcon;
  72. button.keystroke = 'CTRL+K';
  73. button.tooltip = true;
  74. // Bind button to the command.
  75. button.bind( 'isEnabled' ).to( linkCommand, 'isEnabled' );
  76. // Show the panel on button click.
  77. this.listenTo( button, 'execute', () => this._showPanel() );
  78. return button;
  79. } );
  80. }
  81. /**
  82. * Creates a toolbar unlink button. Clicking this button will unlink
  83. * the selected link.
  84. *
  85. * @private
  86. */
  87. _createToolbarUnlinkButton() {
  88. const editor = this.editor;
  89. const t = editor.t;
  90. const unlinkCommand = editor.commands.get( 'unlink' );
  91. editor.ui.componentFactory.add( 'unlink', ( locale ) => {
  92. const button = new ButtonView( locale );
  93. button.isEnabled = false;
  94. button.label = t( 'Unlink' );
  95. button.icon = unlinkIcon;
  96. button.tooltip = true;
  97. // Bind button to the command.
  98. button.bind( 'isEnabled' ).to( unlinkCommand, 'isEnabled' );
  99. // Execute unlink command and hide panel, if open on button click.
  100. this.listenTo( button, 'execute', () => editor.execute( 'unlink' ) );
  101. return button;
  102. } );
  103. }
  104. /**
  105. * Creates the {@link module:ui/balloonpanel/balloonpanelview~BalloonPanelView} instance.
  106. *
  107. * @private
  108. * @returns {module:ui/balloonpanel/balloonpanelview~BalloonPanelView} Link balloon panel instance.
  109. */
  110. _createBalloonPanel() {
  111. const editor = this.editor;
  112. const viewDocument = editor.editing.view;
  113. // Create the balloon panel instance.
  114. const balloonPanelView = new BalloonPanelView( editor.locale );
  115. balloonPanelView.maxWidth = 300;
  116. // Add balloonPanel.view#element to FocusTracker.
  117. // @TODO: Do it automatically ckeditor5-core#23
  118. editor.ui.focusTracker.add( balloonPanelView.element );
  119. // Handle click on view document and show panel when selection is placed inside the link element.
  120. // Keep panel open until selection will be inside the same link element.
  121. this.listenTo( viewDocument, 'click', () => {
  122. const viewSelection = viewDocument.selection;
  123. const parentLink = getPositionParentLink( viewSelection.getFirstPosition() );
  124. if ( viewSelection.isCollapsed && parentLink ) {
  125. this._attachPanelToElement();
  126. this.listenTo( viewDocument, 'render', () => {
  127. const currentParentLink = getPositionParentLink( viewSelection.getFirstPosition() );
  128. if ( !viewSelection.isCollapsed || parentLink !== currentParentLink ) {
  129. this._hidePanel();
  130. } else {
  131. this._attachPanelToElement( parentLink );
  132. }
  133. } );
  134. this.listenTo( balloonPanelView, 'change:isVisible', () => this.stopListening( viewDocument, 'render' ) );
  135. }
  136. } );
  137. // Close on `ESC` press.
  138. escPressHandler( {
  139. emitter: balloonPanelView,
  140. activator: () => balloonPanelView.isVisible,
  141. callback: () => this._hidePanel( true )
  142. } );
  143. // Close on click outside of balloon panel element.
  144. clickOutsideHandler( {
  145. emitter: balloonPanelView,
  146. activator: () => balloonPanelView.isVisible,
  147. contextElement: balloonPanelView.element,
  148. callback: () => this._hidePanel()
  149. } );
  150. editor.ui.view.body.add( balloonPanelView );
  151. return balloonPanelView;
  152. }
  153. /**
  154. * Creates the {@link module:link/ui/linkformview~LinkFormView} instance.
  155. *
  156. * @private
  157. * @returns {module:link/ui/linkformview~LinkFormView} Link form instance.
  158. */
  159. _createForm() {
  160. const editor = this.editor;
  161. const formView = new LinkFormView( editor.locale );
  162. formView.urlInputView.bind( 'value' ).to( editor.commands.get( 'link' ), 'value' );
  163. // Execute link command after clicking on formView `Save` button.
  164. this.listenTo( formView, 'submit', () => {
  165. editor.execute( 'link', formView.urlInputView.inputView.element.value );
  166. this._hidePanel( true );
  167. } );
  168. // Execute unlink command after clicking on formView `Unlink` button.
  169. this.listenTo( formView, 'unlink', () => {
  170. editor.execute( 'unlink' );
  171. this._hidePanel( true );
  172. } );
  173. // Hide balloon panel after clicking on formView `Cancel` button.
  174. this.listenTo( formView, 'cancel', () => this._hidePanel( true ) );
  175. this.balloonPanelView.content.add( formView );
  176. return formView;
  177. }
  178. /**
  179. * Shows {@link #balloonPanelView link balloon panel} and attach to target element.
  180. * If selection is collapsed and is placed inside link element, then panel will be attached
  181. * to whole link element, otherwise will be attached to the selection.
  182. *
  183. * @private
  184. * @param {module:link/linkelement~LinkElement} [parentLink] Target element.
  185. */
  186. _attachPanelToElement( parentLink ) {
  187. const viewDocument = this.editor.editing.view;
  188. const targetLink = parentLink || getPositionParentLink( viewDocument.selection.getFirstPosition() );
  189. const target = targetLink ?
  190. // When selection is inside link element, then attach panel to this element.
  191. viewDocument.domConverter.getCorrespondingDomElement( targetLink )
  192. :
  193. // Otherwise attach panel to the selection.
  194. viewDocument.domConverter.viewRangeToDom( viewDocument.selection.getFirstRange() );
  195. this.balloonPanelView.attachTo( {
  196. target,
  197. limiter: viewDocument.domConverter.getCorrespondingDomElement( viewDocument.selection.editableElement )
  198. } );
  199. }
  200. /**
  201. * Hides {@link #balloonPanelView balloon panel view}.
  202. *
  203. * @private
  204. * @param {Boolean} [focusEditable=false] When `true` then editable focus will be restored on panel hide.
  205. */
  206. _hidePanel( focusEditable ) {
  207. this.balloonPanelView.hide();
  208. if ( focusEditable ) {
  209. this.editor.editing.view.focus();
  210. }
  211. }
  212. /**
  213. * Shows {@link #balloonPanelView balloon panel view}.
  214. *
  215. * @private
  216. */
  217. _showPanel() {
  218. this._attachPanelToElement();
  219. this.formView.urlInputView.select();
  220. }
  221. }
  222. // Try to find if one of the position parent ancestors is a LinkElement,
  223. // if yes return this element.
  224. //
  225. // @private
  226. // @param {engine.view.Position} position
  227. // @returns {module:link/linkelement~LinkElement|null}
  228. function getPositionParentLink( position ) {
  229. return position.parent.getAncestors().find( ( ancestor ) => ancestor instanceof LinkElement );
  230. }