LinkCommand has a special handling when inserting links with a collapsed (not expanded) selection (aka: no selection). We need to take care of this behavior, which is, that LinkCommand inserts the linkHref attribute as text within the editor and afterwards places the cursor after the link.

For content-links this is no valid approach. We don't want content:123 to be written into the CKEditor text field. We want the content's name to be written, and, perhaps, if not available because of concurrent changes, fallback to the content:123 form.

Approach: While several approaches exist, the given approach ensures that only one Undo-step is created when (instead of one for the raw content-ID and the content-name afterwards) and that it does not require deep integration into the private API of CKEditor's link feature.

Thus, this hook is provided as post-fixer registered at the document. To replace content names, it requires to be informed by the FormView of a content name to replace. This is, because content names can only be retrieved asynchronously, while a post-fixer needs to evaluate synchronously.

The suggested approach is, that the Save button is disabled until the FormView has a valid name to hand over to this plugin, which will then use this name for replacement.

Usage Example:

const plugin = <ContentLinkCommandHook | undefined>editor.plugins.get(ContentLinkCommandHook.pluginName);
plugin?.registerContentName("content/123", "myName");

If registerContentName is called multiple times, only the last registered replacement will be considered.

Important: The name must be registered, before LinkCommand is executed.

No need to check if replacement required on registration: It is this plugin, which decides, if a replacement is required or not. More specifically, it will only respond to content insertion events (via document.differ) and not to attribute changes. I.e., with the current implementation of LinkCommand (as of CKEditor 29.x) it will be triggered if the LinkCommands calls insertContent for a collapsed selection outside an existing link — which again triggers the linkHref attribute to be written to the text, which is the scenario, we want to prevent for content-links.

Hierarchy (view full)

Constructors

  • Parameters

    • editor: default

    Returns ContentLinkCommandHook

    Inherit Doc

Properties

pluginName: "ContentLinkCommandHook" = ...
requires: typeof default[] = ...

Methods

  • Hook for FormView to register a content-name, which is about to be replaced as soon as CKEditor's Link Features tries to insert the "raw link" into CKEditor text.

    For any unregistered URI path, the name generated by CKEditor's Link Feature won't be vetoed.

    Subsequent calls to this method will override a previous replacement, i.e., only one replacement is remembered at a time.

    The registration must be done prior to the execution of LinkCommand.

    The replacement cache is cleared as soon as LinkCommand finished execution.

    Parameters

    • uriOrPath: string

      URI path of the content (content/123) or content URI as stored in the model (content:123).

    • name: string

      resolved name

    Returns void