link.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 '@ckeditor/ckeditor5-core/src/plugin';
  9. import ClickObserver from '@ckeditor/ckeditor5-engine/src/view/observer/clickobserver';
  10. import LinkEngine from './linkengine';
  11. import LinkElement from './linkelement';
  12. import clickOutsideHandler from '@ckeditor/ckeditor5-ui/src/bindings/clickoutsidehandler';
  13. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  14. import LinkFormView from './ui/linkformview';
  15. import linkIcon from '../theme/icons/link.svg';
  16. import unlinkIcon from '../theme/icons/unlink.svg';
  17. import '../theme/theme.scss';
  18. /**
  19. * The link feature. It introduces the Link and Unlink buttons and the <kbd>Ctrl+K</kbd> keystroke.
  20. * Link UI is displayed using {@link module:core/editor/editorui~EditorUI#balloon}.
  21. *
  22. * It uses the {@link module:link/linkengine~LinkEngine link engine feature}.
  23. *
  24. * @extends module:core/plugin~Plugin
  25. */
  26. export default class Link extends Plugin {
  27. /**
  28. * @inheritDoc
  29. */
  30. static get requires() {
  31. return [ LinkEngine ];
  32. }
  33. /**
  34. * @inheritDoc
  35. */
  36. static get pluginName() {
  37. return 'link/link';
  38. }
  39. /**
  40. * @inheritDoc
  41. */
  42. init() {
  43. this.editor.editing.view.addObserver( ClickObserver );
  44. /**
  45. * The form view inside {@link #balloonPanelView}.
  46. *
  47. * @member {module:link/ui/linkformview~LinkFormView}
  48. */
  49. this.formView = this._createForm();
  50. // Create toolbar buttons.
  51. this._createToolbarLinkButton();
  52. this._createToolbarUnlinkButton();
  53. // Attach lifecycle actions to the link balloon.
  54. this._attachActions();
  55. }
  56. /**
  57. * Returns `true` when link panel is added to the {@link module:core/editor/editorui~EditorUI#balloon} stack.
  58. *
  59. * @private
  60. * @returns {Boolean}
  61. */
  62. get _isInStack() {
  63. return this.editor.ui.balloon.isPanelInStack( this.formView );
  64. }
  65. /**
  66. * Returns `true` when link panel is currently visible in {@link module:core/editor/editorui~EditorUI#balloon}.
  67. *
  68. * @private
  69. * @returns {Boolean}
  70. */
  71. get _isVisible() {
  72. const balloon = this.editor.ui.balloon;
  73. return balloon.visible && balloon.visible.view === this.formView;
  74. }
  75. /**
  76. * Creates the {@link module:link/ui/linkformview~LinkFormView} instance.
  77. *
  78. * @private
  79. * @returns {module:link/ui/linkformview~LinkFormView} Link form instance.
  80. */
  81. _createForm() {
  82. const editor = this.editor;
  83. const formView = new LinkFormView( editor.locale );
  84. formView.urlInputView.bind( 'value' ).to( editor.commands.get( 'link' ), 'value' );
  85. // Execute link command after clicking on formView `Save` button.
  86. this.listenTo( formView, 'submit', () => {
  87. editor.execute( 'link', formView.urlInputView.inputView.element.value );
  88. this._hidePanel( true );
  89. } );
  90. // Execute unlink command after clicking on formView `Unlink` button.
  91. this.listenTo( formView, 'unlink', () => {
  92. editor.execute( 'unlink' );
  93. this._hidePanel( true );
  94. } );
  95. // Hide balloon panel after clicking on formView `Cancel` button.
  96. this.listenTo( formView, 'cancel', () => this._hidePanel( true ) );
  97. // Close the panel on esc key press when the form has focus.
  98. formView.keystrokes.set( 'Esc', ( data, cancel ) => {
  99. this._hidePanel( true );
  100. cancel();
  101. } );
  102. return formView;
  103. }
  104. /**
  105. * Creates a toolbar link button. Clicking this button will show
  106. * {@link #balloonPanelView} attached to the selection.
  107. *
  108. * @private
  109. */
  110. _createToolbarLinkButton() {
  111. const editor = this.editor;
  112. const linkCommand = editor.commands.get( 'link' );
  113. const t = editor.t;
  114. // Handle `Ctrl+K` keystroke and show panel.
  115. editor.keystrokes.set( 'CTRL+K', () => this._showPanel( true ) );
  116. editor.ui.componentFactory.add( 'link', ( locale ) => {
  117. const button = new ButtonView( locale );
  118. button.isEnabled = true;
  119. button.label = t( 'Link' );
  120. button.icon = linkIcon;
  121. button.keystroke = 'CTRL+K';
  122. button.tooltip = true;
  123. // Bind button to the command.
  124. button.bind( 'isEnabled' ).to( linkCommand, 'isEnabled' );
  125. // Show the panel on button click.
  126. this.listenTo( button, 'execute', () => this._showPanel( true ) );
  127. return button;
  128. } );
  129. }
  130. /**
  131. * Creates a toolbar unlink button. Clicking this button will unlink
  132. * the selected link.
  133. *
  134. * @private
  135. */
  136. _createToolbarUnlinkButton() {
  137. const editor = this.editor;
  138. const t = editor.t;
  139. const unlinkCommand = editor.commands.get( 'unlink' );
  140. editor.ui.componentFactory.add( 'unlink', ( locale ) => {
  141. const button = new ButtonView( locale );
  142. button.isEnabled = false;
  143. button.label = t( 'Unlink' );
  144. button.icon = unlinkIcon;
  145. button.tooltip = true;
  146. // Bind button to the command.
  147. button.bind( 'isEnabled' ).to( unlinkCommand, 'isEnabled' );
  148. // Execute unlink command and hide panel, if open on button click.
  149. this.listenTo( button, 'execute', () => editor.execute( 'unlink' ) );
  150. return button;
  151. } );
  152. }
  153. /**
  154. * Attaches actions which defines when panel should be open or close.
  155. *
  156. * @private
  157. */
  158. _attachActions() {
  159. const viewDocument = this.editor.editing.view;
  160. const balloon = this.editor.ui.balloon;
  161. // Handle click on view document and show panel when selection is placed inside the link element.
  162. // Keep panel open until selection will be inside the same link element.
  163. this.listenTo( viewDocument, 'click', () => {
  164. const viewSelection = viewDocument.selection;
  165. const parentLink = getPositionParentLink( viewSelection.getFirstPosition() );
  166. // When collapsed selection is inside link element (link element is clicked).
  167. if ( viewSelection.isCollapsed && parentLink ) {
  168. // Then show panel but keep focus inside editor editable.
  169. this._showPanel();
  170. // Start listen to view document changes and close the panel when selection will be moved
  171. // out of the actual link element.
  172. this.listenTo( viewDocument, 'render', () => {
  173. const currentParentLink = getPositionParentLink( viewSelection.getFirstPosition() );
  174. if ( !viewSelection.isCollapsed || parentLink !== currentParentLink ) {
  175. this._hidePanel();
  176. } else {
  177. balloon.updatePosition();
  178. }
  179. } );
  180. }
  181. } );
  182. // Focus the form if link panel is visible and tab key has been pressed.
  183. this.editor.keystrokes.set( 'Tab', ( data, cancel ) => {
  184. if ( this._isVisible && !this.formView.focusTracker.isFocused ) {
  185. this.formView.focus();
  186. cancel();
  187. }
  188. } );
  189. // Close the panel on esc key press when editable has focus and link balloon is currently visible.
  190. this.editor.keystrokes.set( 'Esc', ( data, cancel ) => {
  191. if ( this._isVisible ) {
  192. this._hidePanel();
  193. cancel();
  194. }
  195. } );
  196. // Close on click outside of balloon panel element.
  197. clickOutsideHandler( {
  198. emitter: this.formView,
  199. activator: () => this._isInStack,
  200. contextElement: balloon.view.element,
  201. callback: () => this._hidePanel()
  202. } );
  203. }
  204. /**
  205. * Adds panel to {@link: core/editor/editorui~EditorUI#balloon}.
  206. *
  207. * @private
  208. * @param {Boolean} [focusInput=false] When `true` then link form will be focused on panel show.
  209. */
  210. _showPanel( focusInput ) {
  211. if ( this._isInStack ) {
  212. return;
  213. }
  214. this.editor.ui.balloon.add( {
  215. view: this.formView,
  216. position: this._getBalloonPositionData()
  217. } );
  218. if ( focusInput ) {
  219. this.formView.urlInputView.select();
  220. }
  221. }
  222. /**
  223. * Removes panel from {@link: core/editor/editorui~EditorUI#balloon}.
  224. *
  225. * @private
  226. * @param {Boolean} [focusEditable=false] When `true` then editable focus will be restored on panel hide.
  227. */
  228. _hidePanel( focusEditable ) {
  229. if ( !this._isInStack ) {
  230. return;
  231. }
  232. this.editor.ui.balloon.remove( this.formView );
  233. this.stopListening( this.editor.editing.view, 'render' );
  234. if ( focusEditable ) {
  235. this.editor.editing.view.focus();
  236. }
  237. }
  238. /**
  239. * Returns position configuration for the balloon panel. According to this data balloon will be attached
  240. * to the target element. If selection is collapsed and is placed inside link element, then panel
  241. * will be attached to whole link element, otherwise will be attached to the selection.
  242. *
  243. * @private
  244. * @returns {module:utils/dom/position~Options}
  245. */
  246. _getBalloonPositionData() {
  247. const viewDocument = this.editor.editing.view;
  248. const targetLink = getPositionParentLink( viewDocument.selection.getFirstPosition() );
  249. const target = targetLink ?
  250. // When selection is inside link element, then attach panel to this element.
  251. viewDocument.domConverter.getCorrespondingDomElement( targetLink )
  252. :
  253. // Otherwise attach panel to the selection.
  254. viewDocument.domConverter.viewRangeToDom( viewDocument.selection.getFirstRange() );
  255. return {
  256. target,
  257. limiter: viewDocument.domConverter.getCorrespondingDomElement( viewDocument.selection.editableElement )
  258. };
  259. }
  260. }
  261. // Try to find if one of the position parent ancestors is a LinkElement,
  262. // if yes return this element.
  263. //
  264. // @private
  265. // @param {engine.view.Position} position
  266. // @returns {module:link/linkelement~LinkElement|null}
  267. function getPositionParentLink( position ) {
  268. return position.parent.getAncestors().find( ( ancestor ) => ancestor instanceof LinkElement );
  269. }