Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor/link: fix an issue

Adorian Doran9f9fa63d4ed7
diff --git a/packages/ckeditor5/src/hosted_balloon.ts b/packages/ckeditor5/src/hosted_balloon.ts
index bc6713d8ff..30b934d626 100644
--- a/packages/ckeditor5/src/hosted_balloon.ts
+++ b/packages/ckeditor5/src/hosted_balloon.ts
@@ -1,6 +1,5 @@
import {
- clickOutsideHandler, ContextualBalloon, type Editor, KeystrokeHandler, type Locale, View,
- type ViewRange
+ ContextualBalloon, type Editor, KeystrokeHandler, type Locale, View, type ViewRange
} from "ckeditor5";
/**
@@ -57,16 +56,17 @@ export default class HostedBalloon {
});
// Wired per view rather than once: the handler listens through the view, so destroying
- // the view on the way out stops it.
- clickOutsideHandler({
- emitter: view,
- activator: () => this.view === view,
+ // the view on the way out stops it. It listens in the capture phase because link previews
+ // and content embeds stop presses from bubbling, to keep them from CKEditor.
+ view.listenTo(document, 'mousedown', (_evt, domEvent) => {
// The balloon renders its panel as the editor starts up.
- contextElements: () => [
+ const keepOpen = [
this.balloon.view.element as HTMLElement, ...getHostLayers(editor, editorEl)
- ],
- callback: () => this.hide()
- });
+ ];
+ if (!keepOpen.some((element) => element.contains(domEvent.target as Node))) {
+ this.hide();
+ }
+ }, { useCapture: true });
this.balloon.add({ view, position: getBalloonPosition(editor, shownAt) });
this.view = view;
diff --git a/packages/ckeditor5/src/plugins/internallink.spec.ts b/packages/ckeditor5/src/plugins/internallink.spec.ts
index ba953db94a..37fe0a17e3 100644
--- a/packages/ckeditor5/src/plugins/internallink.spec.ts
+++ b/packages/ckeditor5/src/plugins/internallink.spec.ts
@@ -6,6 +6,7 @@ import {
import { beforeEach, describe, expect, it, vi } from "vitest";
import { createTestEditor } from "../../test/editor-kit.js";
+import { createLinkEmbedHost } from "../../test/link-embed-kit.js";
import InternalLinkPlugin, {
COMMAND_NAME, getAnchorNames, getLinkAtSelection, type LinkDialogRequest,
replaceLinkAtSelection
@@ -90,6 +91,30 @@ describe("InternalLinkPlugin", () => {
}
});
+ it("closes for a press on another link, a link preview included", async () => {
+ const linkEditor = await createTestEditor(
+ [ Essentials, Paragraph, LinkEditing, LinkEmbedEditing, InternalLinkPlugin ],
+ { internalLink: { showDialog }, linkEmbed: createLinkEmbedHost() }
+ );
+ setModelData(linkEditor.model, "<paragraph>[]"
+ + "<$text linkHref=\"https://a.example\">a</$text>"
+ + "<linkMention url=\"https://b.example\"></linkMention></paragraph>");
+ const { release } = renderIntoBalloon();
+ const balloon = linkEditor.plugins.get(ContextualBalloon);
+ const root = linkEditor.editing.view.getDomRoot();
+
+ const links = [ root?.querySelector("a"), root?.querySelector(".link-mention-inner") ];
+ for (const link of links) {
+ if (!link) throw new Error("Expected the link and the preview to render.");
+ linkEditor.execute(COMMAND_NAME);
+ expect(balloon.visibleView).not.toBeNull();
+
+ link.dispatchEvent(new MouseEvent("mousedown", { bubbles: true }));
+ expect(balloon.visibleView).toBeNull();
+ }
+ expect(release).toHaveBeenCalledTimes(2);
+ });
+
it("closes the balloon for a host that shows the dialog its own way, or has none", async () => {
const plainEditor = await createTestEditor([ Essentials, Paragraph, InternalLinkPlugin ]);