link.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module link/link
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import LinkEditing from './linkediting';
  10. import LinkUI from './linkui';
  11. /**
  12. * The link plugin.
  13. *
  14. * This is a "glue" plugin that loads the {@link module:link/linkediting~LinkEditing link editing feature}
  15. * and {@link module:link/linkui~LinkUI link UI feature}.
  16. *
  17. * @extends module:core/plugin~Plugin
  18. */
  19. export default class Link extends Plugin {
  20. /**
  21. * @inheritDoc
  22. */
  23. static get requires() {
  24. return [ LinkEditing, LinkUI ];
  25. }
  26. /**
  27. * @inheritDoc
  28. */
  29. static get pluginName() {
  30. return 'Link';
  31. }
  32. }
  33. /**
  34. * The configuration of the {@link module:link/link~Link} feature.
  35. *
  36. * Read more in {@link module:link/link~LinkConfig}.
  37. *
  38. * @member {module:link/link~LinkConfig} module:core/editor/editorconfig~EditorConfig#link
  39. */
  40. /**
  41. * The configuration of the {@link module:link/link~Link link feature}.
  42. *
  43. * ClassicEditor
  44. * .create( editorElement, {
  45. * link: ... // Link feature configuration.
  46. * } )
  47. * .then( ... )
  48. * .catch( ... );
  49. *
  50. * See {@link module:core/editor/editorconfig~EditorConfig all editor options}.
  51. * @interface LinkConfig
  52. */
  53. /**
  54. * When set to `true`, the `target="blank"` and `rel="noopener noreferrer"` attributes are automatically added to all external links
  55. * in the editor. "External links" are all links in the editor content starting with `http`, `https`, or `//`.
  56. *
  57. * ClassicEditor
  58. * .create( editorElement, {
  59. * link: {
  60. * addTargetToExternalLinks: true
  61. * }
  62. * } )
  63. * .then( ... )
  64. * .catch( ... );
  65. *
  66. * Internally, this option activates a predefined {@link module:link/link~LinkConfig#decorators automatic link decorator}
  67. * that extends all external links with the `target` and `rel` attributes.
  68. *
  69. * **Note**: To control the `target` and `rel` attributes of specific links in the edited content, a dedicated
  70. * {@link module:link/link~LinkDecoratorManualDefinition manual} decorator must be defined in the
  71. * {@link module:link/link~LinkConfig#decorators `config.link.decorators`} array. In such scenario,
  72. * the `config.link.addTargetToExternalLinks` option should remain `undefined` or `false` to not interfere with the manual decorator.
  73. *
  74. * It is possible to add other {@link module:link/link~LinkDecoratorAutomaticDefinition automatic}
  75. * or {@link module:link/link~LinkDecoratorManualDefinition manual} link decorators when this option is active.
  76. *
  77. * More information about decorators can be found in the {@link module:link/link~LinkConfig#decorators decorators configuration}
  78. * reference.
  79. *
  80. * @default false
  81. * @member {Boolean} module:link/link~LinkConfig#addTargetToExternalLinks
  82. */
  83. /**
  84. * Decorators provide an easy way to configure and manage additional link attributes in the editor content. There are
  85. * two types of link decorators:
  86. *
  87. * * {@link module:link/link~LinkDecoratorAutomaticDefinition Automatic} – They match links against pre–defined rules and
  88. * manage their attributes based on the results.
  89. * * {@link module:link/link~LinkDecoratorManualDefinition Manual} – They allow users to control link attributes individually,
  90. * using the editor UI.
  91. *
  92. * Link decorators are defined as objects with key-value pairs, where the key is the name provided for a given decorator and the
  93. * value is the decorator definition.
  94. *
  95. * The name of the decorator also corresponds to the {@glink framework/guides/architecture/editing-engine#text-attributes text attribute}
  96. * in the model. For instance, the `isExternal` decorator below is represented as a `linkIsExternal` attribute in the model.
  97. *
  98. * ClassicEditor
  99. * .create( editorElement, {
  100. * link: {
  101. * decorators: {
  102. * isExternal: {
  103. * mode: 'automatic',
  104. * callback: url => url.startsWith( 'http://' ),
  105. * attributes: {
  106. * target: '_blank',
  107. * rel: 'noopener noreferrer'
  108. * }
  109. * },
  110. * isDownloadable: {
  111. * mode: 'manual',
  112. * label: 'Downloadable',
  113. * attributes: {
  114. * download: 'file.png',
  115. * }
  116. * },
  117. * // ...
  118. * }
  119. * }
  120. * } )
  121. * .then( ... )
  122. * .catch( ... );
  123. *
  124. * To learn more about the configuration syntax, check out the {@link module:link/link~LinkDecoratorAutomaticDefinition automatic}
  125. * and {@link module:link/link~LinkDecoratorManualDefinition manual} decorator option reference.
  126. *
  127. * **Warning:** Currently, link decorators work independently of one another and no conflict resolution mechanism exists.
  128. * For example, configuring the `target` attribute using both an automatic and a manual decorator at the same time could end up with
  129. * quirky results. The same applies if multiple manual or automatic decorators were defined for the same attribute.
  130. *
  131. * **Note**: Since the `target` attribute management for external links is a common use case, there is a predefined automatic decorator
  132. * dedicated for that purpose which can be enabled by turning a single option on. Check out the
  133. * {@link module:link/link~LinkConfig#addTargetToExternalLinks `config.link.addTargetToExternalLinks`}
  134. * configuration description to learn more.
  135. *
  136. * See also the {@glink features/link#custom-link-attributes-decorators link feature guide} for more information.
  137. *
  138. * @member {Object.<String, module:link/link~LinkDecoratorDefinition>} module:link/link~LinkConfig#decorators
  139. */
  140. /**
  141. * Represents a link decorator definition ({@link module:link/link~LinkDecoratorManualDefinition `'manual'`}
  142. * or {@link module:link/link~LinkDecoratorAutomaticDefinition `'automatic'`}).
  143. *
  144. * @interface LinkDecoratorDefinition
  145. */
  146. /**
  147. * Link decorator type.
  148. *
  149. * Check out the {@glink features/link#custom-link-attributes-decorators link feature guide} for more information.
  150. *
  151. * @member {'manual'|'automatic'} module:link/link~LinkDecoratorDefinition#mode
  152. */
  153. /**
  154. * Describes an automatic {@link module:link/link~LinkConfig#decorators link decorator}. This decorator type matches
  155. * all links in the editor content against a function that decides whether the link should receive a pre–defined set of attributes.
  156. *
  157. * It takes an object with key-value pairs of attributes and a callback function that must return a Boolean value based on the link's
  158. * `href` (URL). When the callback returns `true`, attributes are applied to the link.
  159. *
  160. * For example, to add the `target="_blank"` attribute to all links in the editor starting with `http://`, the
  161. * configuration could look like this:
  162. *
  163. * {
  164. * mode: 'automatic',
  165. * callback: url => url.startsWith( 'http://' ),
  166. * attributes: {
  167. * target: '_blank'
  168. * }
  169. * }
  170. *
  171. * **Note**: Since the `target` attribute management for external links is a common use case, there is a predefined automatic decorator
  172. * dedicated for that purpose that can be enabled by turning a single option on. Check out the
  173. * {@link module:link/link~LinkConfig#addTargetToExternalLinks `config.link.addTargetToExternalLinks`}
  174. * configuration description to learn more.
  175. *
  176. * @typedef {Object} module:link/link~LinkDecoratorAutomaticDefinition
  177. * @property {'automatic'} mode Link decorator type. It is `'automatic'` for all automatic decorators.
  178. * @property {Function} callback Takes a `url` as a parameter and returns `true` if the `attributes` should be applied to the link.
  179. * @property {Object} attributes Key-value pairs used as link attributes added to the output during the
  180. * {@glink framework/guides/architecture/editing-engine#conversion downcasting}.
  181. * Attributes should follow the {@link module:engine/view/elementdefinition~ElementDefinition} syntax.
  182. */
  183. /**
  184. * Describes a manual {@link module:link/link~LinkConfig#decorators link decorator}. This decorator type is represented in
  185. * the link feature's {@link module:link/linkui user interface} as a switch that the user can use to control the presence
  186. * of a predefined set of attributes.
  187. *
  188. * For instance, to allow the users to manually control the presence of the `target="_blank"` and
  189. * `rel="noopener noreferrer"` attributes on specific links, the decorator could look as follows:
  190. *
  191. * {
  192. * mode: 'manual',
  193. * label: 'Open in a new tab',
  194. * attributes: {
  195. * target: '_blank',
  196. * rel: 'noopener noreferrer'
  197. * }
  198. * }
  199. *
  200. * @typedef {Object} module:link/link~LinkDecoratorManualDefinition
  201. * @property {'manual'} mode Link decorator type. It is `'manual'` for all manual decorators.
  202. * @property {String} label The label of the UI button that the user can use to control the presence of link attributes.
  203. * @property {Object} attributes Key-value pairs used as link attributes added to the output during the
  204. * {@glink framework/guides/architecture/editing-engine#conversion downcasting}.
  205. * Attributes should follow the {@link module:engine/view/elementdefinition~ElementDefinition} syntax.
  206. * @property {Boolean} [defaultValue=false] Controls whether the decorator is "on" by default.
  207. */