link.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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, the editor will add the given protocol to the link when the user creates a link without one.
  55. * For example, when the user is creating a link and types `ckeditor.com` in the link form input, during link submission
  56. * the editor will automatically add the `http://` protocol, so the link will look as follows: `http://ckeditor.com`.
  57. *
  58. * The feature also provides email address auto-detection. When you submit `hello@example.com`,
  59. * the plugin will automatically change it to `mailto:hello@example.com`.
  60. *
  61. * ClassicEditor
  62. * .create( editorElement, {
  63. * link: {
  64. * defaultProtocol: 'http://'
  65. * }
  66. * } )
  67. * .then( ... )
  68. * .catch( ... );
  69. *
  70. * **NOTE:** If no configuration is provided, the editor will not auto-fix the links.
  71. *
  72. * @member {String} module:link/link~LinkConfig#defaultProtocol
  73. */
  74. /**
  75. * When set to `true`, the `target="blank"` and `rel="noopener noreferrer"` attributes are automatically added to all external links
  76. * in the editor. "External links" are all links in the editor content starting with `http`, `https`, or `//`.
  77. *
  78. * ClassicEditor
  79. * .create( editorElement, {
  80. * link: {
  81. * addTargetToExternalLinks: true
  82. * }
  83. * } )
  84. * .then( ... )
  85. * .catch( ... );
  86. *
  87. * Internally, this option activates a predefined {@link module:link/link~LinkConfig#decorators automatic link decorator}
  88. * that extends all external links with the `target` and `rel` attributes.
  89. *
  90. * **Note**: To control the `target` and `rel` attributes of specific links in the edited content, a dedicated
  91. * {@link module:link/link~LinkDecoratorManualDefinition manual} decorator must be defined in the
  92. * {@link module:link/link~LinkConfig#decorators `config.link.decorators`} array. In such scenario,
  93. * the `config.link.addTargetToExternalLinks` option should remain `undefined` or `false` to not interfere with the manual decorator.
  94. *
  95. * It is possible to add other {@link module:link/link~LinkDecoratorAutomaticDefinition automatic}
  96. * or {@link module:link/link~LinkDecoratorManualDefinition manual} link decorators when this option is active.
  97. *
  98. * More information about decorators can be found in the {@link module:link/link~LinkConfig#decorators decorators configuration}
  99. * reference.
  100. *
  101. * @default false
  102. * @member {Boolean} module:link/link~LinkConfig#addTargetToExternalLinks
  103. */
  104. /**
  105. * Decorators provide an easy way to configure and manage additional link attributes in the editor content. There are
  106. * two types of link decorators:
  107. *
  108. * * {@link module:link/link~LinkDecoratorAutomaticDefinition Automatic} – They match links against pre–defined rules and
  109. * manage their attributes based on the results.
  110. * * {@link module:link/link~LinkDecoratorManualDefinition Manual} – They allow users to control link attributes individually,
  111. * using the editor UI.
  112. *
  113. * Link decorators are defined as objects with key-value pairs, where the key is the name provided for a given decorator and the
  114. * value is the decorator definition.
  115. *
  116. * The name of the decorator also corresponds to the {@glink framework/guides/architecture/editing-engine#text-attributes text attribute}
  117. * in the model. For instance, the `isExternal` decorator below is represented as a `linkIsExternal` attribute in the model.
  118. *
  119. * ClassicEditor
  120. * .create( editorElement, {
  121. * link: {
  122. * decorators: {
  123. * isExternal: {
  124. * mode: 'automatic',
  125. * callback: url => url.startsWith( 'http://' ),
  126. * attributes: {
  127. * target: '_blank',
  128. * rel: 'noopener noreferrer'
  129. * }
  130. * },
  131. * isDownloadable: {
  132. * mode: 'manual',
  133. * label: 'Downloadable',
  134. * attributes: {
  135. * download: 'file.png',
  136. * }
  137. * },
  138. * // ...
  139. * }
  140. * }
  141. * } )
  142. * .then( ... )
  143. * .catch( ... );
  144. *
  145. * To learn more about the configuration syntax, check out the {@link module:link/link~LinkDecoratorAutomaticDefinition automatic}
  146. * and {@link module:link/link~LinkDecoratorManualDefinition manual} decorator option reference.
  147. *
  148. * **Warning:** Currently, link decorators work independently of one another and no conflict resolution mechanism exists.
  149. * For example, configuring the `target` attribute using both an automatic and a manual decorator at the same time could end up with
  150. * quirky results. The same applies if multiple manual or automatic decorators were defined for the same attribute.
  151. *
  152. * **Note**: Since the `target` attribute management for external links is a common use case, there is a predefined automatic decorator
  153. * dedicated for that purpose which can be enabled by turning a single option on. Check out the
  154. * {@link module:link/link~LinkConfig#addTargetToExternalLinks `config.link.addTargetToExternalLinks`}
  155. * configuration description to learn more.
  156. *
  157. * See also the {@glink features/link#custom-link-attributes-decorators link feature guide} for more information.
  158. *
  159. * @member {Object.<String, module:link/link~LinkDecoratorDefinition>} module:link/link~LinkConfig#decorators
  160. */
  161. /**
  162. * A link decorator definition. Two types implement this defition:
  163. *
  164. * * {@link module:link/link~LinkDecoratorManualDefinition}
  165. * * {@link module:link/link~LinkDecoratorAutomaticDefinition}
  166. *
  167. * Refer to their document for more information about available options or to the
  168. * {@glink features/link#custom-link-attributes-decorators link feature guide} for general information.
  169. *
  170. * @interface LinkDecoratorDefinition
  171. */
  172. /**
  173. * Link decorator type.
  174. *
  175. * Check out the {@glink features/link#custom-link-attributes-decorators link feature guide} for more information.
  176. *
  177. * @member {'manual'|'automatic'} module:link/link~LinkDecoratorDefinition#mode
  178. */
  179. /**
  180. * Describes an automatic {@link module:link/link~LinkConfig#decorators link decorator}. This decorator type matches
  181. * all links in the editor content against a function that decides whether the link should receive a pre–defined set of attributes.
  182. *
  183. * 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
  184. * `href` (URL). When the callback returns `true`, attributes are applied to the link.
  185. *
  186. * For example, to add the `target="_blank"` attribute to all links in the editor starting with `http://`, the
  187. * configuration could look like this:
  188. *
  189. * {
  190. * mode: 'automatic',
  191. * callback: url => url.startsWith( 'http://' ),
  192. * attributes: {
  193. * target: '_blank'
  194. * }
  195. * }
  196. *
  197. * **Note**: Since the `target` attribute management for external links is a common use case, there is a predefined automatic decorator
  198. * dedicated for that purpose that can be enabled by turning a single option on. Check out the
  199. * {@link module:link/link~LinkConfig#addTargetToExternalLinks `config.link.addTargetToExternalLinks`}
  200. * configuration description to learn more.
  201. *
  202. * @typedef {Object} module:link/link~LinkDecoratorAutomaticDefinition
  203. * @property {'automatic'} mode Link decorator type. It is `'automatic'` for all automatic decorators.
  204. * @property {Function} callback Takes a `url` as a parameter and returns `true` if the `attributes` should be applied to the link.
  205. * @property {Object} attributes Key-value pairs used as link attributes added to the output during the
  206. * {@glink framework/guides/architecture/editing-engine#conversion downcasting}.
  207. * Attributes should follow the {@link module:engine/view/elementdefinition~ElementDefinition} syntax.
  208. */
  209. /**
  210. * Describes a manual {@link module:link/link~LinkConfig#decorators link decorator}. This decorator type is represented in
  211. * the link feature's {@link module:link/linkui user interface} as a switch that the user can use to control the presence
  212. * of a predefined set of attributes.
  213. *
  214. * For instance, to allow the users to manually control the presence of the `target="_blank"` and
  215. * `rel="noopener noreferrer"` attributes on specific links, the decorator could look as follows:
  216. *
  217. * {
  218. * mode: 'manual',
  219. * label: 'Open in a new tab',
  220. * defaultValue: true,
  221. * attributes: {
  222. * target: '_blank',
  223. * rel: 'noopener noreferrer'
  224. * }
  225. * }
  226. *
  227. * @typedef {Object} module:link/link~LinkDecoratorManualDefinition
  228. * @property {'manual'} mode Link decorator type. It is `'manual'` for all manual decorators.
  229. * @property {String} label The label of the UI button that the user can use to control the presence of link attributes.
  230. * @property {Object} attributes Key-value pairs used as link attributes added to the output during the
  231. * {@glink framework/guides/architecture/editing-engine#conversion downcasting}.
  232. * Attributes should follow the {@link module:engine/view/elementdefinition~ElementDefinition} syntax.
  233. * @property {Boolean} [defaultValue] Controls whether the decorator is "on" by default.
  234. */