public onPluginEvent()

in packages/roosterjs-editor-plugins/lib/plugins/HyperLink/HyperLink.ts [83:149]


    public onPluginEvent(event: PluginEvent): void {
        if (
            event.eventType == PluginEventType.MouseUp ||
            (event.eventType == PluginEventType.KeyUp &&
                (!this.isContentEditValue(event.rawEvent) || event.rawEvent.which == Keys.SPACE)) ||
            event.eventType == PluginEventType.ContentChanged
        ) {
            const anchor = this.editor.getElementAtCursor(
                'A[href]',
                null /*startFrom*/,
                event
            ) as HTMLAnchorElement;

            const shouldCheckUpdateLink =
                anchor !== this.trackedLink ||
                event.eventType == PluginEventType.KeyUp ||
                event.eventType == PluginEventType.ContentChanged;

            if (
                this.trackedLink &&
                (shouldCheckUpdateLink || this.tryGetHref(this.trackedLink) !== this.originalHref)
            ) {
                // If cursor has moved out of previously tracked link
                // update link href if display text doesn't match href anymore.
                if (shouldCheckUpdateLink) {
                    this.updateLinkHrefIfShouldUpdate();
                }

                // If the link's href value was edited, or the cursor has moved out of the
                // previously tracked link, stop tracking the link.
                this.resetLinkTracking();
            }

            // Cache link and href value if its href attribute currently matches its display text
            if (!this.trackedLink && this.doesLinkDisplayMatchHref(anchor)) {
                this.trackedLink = anchor;
                this.originalHref = this.tryGetHref(anchor);
            }
        }

        if (event.eventType == PluginEventType.MouseUp) {
            const anchor = this.editor.getElementAtCursor(
                'A',
                <Node>event.rawEvent.srcElement
            ) as HTMLAnchorElement;

            if (anchor) {
                if (this.onLinkClick && this.onLinkClick(anchor, event.rawEvent) !== false) {
                    return;
                }

                let href: string;
                if (
                    !Browser.isFirefox &&
                    (href = this.tryGetHref(anchor)) &&
                    isCtrlOrMetaPressed(event.rawEvent) &&
                    event.rawEvent.button === 0
                ) {
                    try {
                        const target = this.target || '_blank';
                        const window = this.editor.getDocument().defaultView;
                        window.open(href, target);
                    } catch {}
                }
            }
        }
    }