|
| 1 | +/** |
| 2 | + * Disables the AutoLink plugin during the normal editing to prevent incorrect |
| 3 | + * transformations and to defer the link detection to the server side. Pasting |
| 4 | + * a link while having an active text selection is selectively enabled. |
| 5 | + * |
| 6 | + * @author Alexander Ebert |
| 7 | + * @copyright 2001-2024 WoltLab GmbH |
| 8 | + * @license LGPL-2.1-or-later |
| 9 | + * @since 6.0 |
| 10 | + */ |
| 11 | + |
| 12 | +import { Plugin } from "@ckeditor/ckeditor5-core"; |
| 13 | +import { AutoLink } from "@ckeditor/ckeditor5-link"; |
| 14 | + |
| 15 | +export class WoltlabAutoLink extends Plugin { |
| 16 | + readonly #key = "WoltlabAutoLink"; |
| 17 | + |
| 18 | + static get pluginName() { |
| 19 | + return "WoltlabAutoLink" as const; |
| 20 | + } |
| 21 | + |
| 22 | + static get requires() { |
| 23 | + return [AutoLink] as const; |
| 24 | + } |
| 25 | + |
| 26 | + init() { |
| 27 | + if (!this.editor.plugins.has(AutoLink)) { |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + // Unconditionally disable the `AutoLink` plugin which interferes with our |
| 32 | + // own link detection and all creates potentially invalid links. |
| 33 | + // See https://github.com/ckeditor/ckeditor5/issues/14497 |
| 34 | + const autoLink = this.editor.plugins.get(AutoLink); |
| 35 | + autoLink.forceDisabled(this.#key); |
| 36 | + |
| 37 | + // Replacing text with a link from clipboard is provided by the `AutoLink` |
| 38 | + // plugin, requiring us to enable the plugin for the duration of the paste. |
| 39 | + // |
| 40 | + // AutoLink’s event listener uses the "high" priority. |
| 41 | + const clipboardPipeline = this.editor.plugins.get("ClipboardPipeline"); |
| 42 | + clipboardPipeline.on( |
| 43 | + "inputTransformation", |
| 44 | + () => { |
| 45 | + autoLink.clearForceDisabled(this.#key); |
| 46 | + }, |
| 47 | + { priority: "highest" }, |
| 48 | + ); |
| 49 | + |
| 50 | + clipboardPipeline.on( |
| 51 | + "inputTransformation", |
| 52 | + () => { |
| 53 | + autoLink.forceDisabled(this.#key); |
| 54 | + }, |
| 55 | + { priority: "normal" }, |
| 56 | + ); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +export default WoltlabAutoLink; |
0 commit comments