Skip to content

Build your personal knowledge base with Trilium Notes

text editor/link previews/toolbar: add a link with the URL

Adorian Doran7cdcdbb4f15c
diff --git a/packages/ckeditor5/src/plugins/link_embed_toolbar.ts b/packages/ckeditor5/src/plugins/link_embed_toolbar.ts
index 5c9de6e24e..8decf1d5e0 100644
--- a/packages/ckeditor5/src/plugins/link_embed_toolbar.ts
+++ b/packages/ckeditor5/src/plugins/link_embed_toolbar.ts
@@ -7,8 +7,10 @@ import {
ViewModel,
createDropdown,
addListToDropdown,
+ ButtonView,
DropdownButtonView,
type ListDropdownButtonDefinition,
+ type Locale,
type Command
} from "ckeditor5";
import LinkEmbed, {
@@ -20,14 +22,14 @@ import LinkEmbed, {
export default class LinkEmbedToolbar extends Plugin {
static get requires() {
- return [WidgetToolbarRepository, LinkEmbed, LinkEmbedDisplayDropdown] as const;
+ return [WidgetToolbarRepository, LinkEmbed, LinkEmbedLinkButton, LinkEmbedDisplayDropdown] as const;
}
afterInit() {
const widgetToolbarRepository = this.editor.plugins.get(WidgetToolbarRepository);
widgetToolbarRepository.register("linkEmbed", {
- items: ["linkEmbedDisplayDropdown"],
+ items: ["linkEmbedLink", "|", "linkEmbedDisplayDropdown"],
balloonClassName: "ck-toolbar-container link-embed-toolbar",
getRelatedElement(selection) {
const selectedElement = selection.getSelectedElement();
@@ -127,3 +129,61 @@ class LinkEmbedDisplayDropdown extends Plugin {
return items;
}
}
+
+/**
+ * Registers the `linkEmbedLink` toolbar item: the leading segment of the link
+ * embed balloon that shows the widget's URL and opens it in a new tab when
+ * clicked — mirroring CKEditor's own link toolbar preview button.
+ */
+class LinkEmbedLinkButton extends Plugin {
+
+ static get requires() {
+ return [LinkEmbed] as const;
+ }
+
+ public init() {
+ const editor = this.editor;
+ const command = editor.commands.get(CHANGE_LINK_DISPLAY_COMMAND) as Command & { url: string | null };
+
+ editor.ui.componentFactory.add("linkEmbedLink", locale => {
+ const button = new LinkEmbedPreviewButtonView(locale);
+
+ button.set("tooltip", locale.t("Open link in new tab"));
+ button.bind("isEnabled").to(command, "url", url => !!url);
+ button.bind("label").to(command, "url", url => url ?? undefined);
+ button.bind("href").to(command, "url", url => url ?? undefined);
+
+ return button;
+ });
+ }
+}
+
+/**
+ * A {@link ButtonView} rendered as an `<a target="_blank">` so a click opens the
+ * URL in a new tab. Reuses CKEditor's `ck-link-toolbar__preview` styling for
+ * visual parity with the built-in link toolbar.
+ */
+class LinkEmbedPreviewButtonView extends ButtonView {
+ declare href: string | undefined;
+
+ constructor(locale: Locale) {
+ super(locale);
+
+ const bind = this.bindTemplate;
+ this.set("href", undefined);
+ this.set("withText", true);
+
+ this.extendTemplate({
+ attributes: {
+ class: ["ck-link-toolbar__preview"],
+ href: bind.to("href"),
+ target: "_blank",
+ rel: "noopener noreferrer"
+ }
+ });
+
+ if (this.template) {
+ this.template.tag = "a";
+ }
+ }
+}
diff --git a/packages/ckeditor5/src/plugins/linkembed.ts b/packages/ckeditor5/src/plugins/linkembed.ts
index 1b323a0c68..e973fb36af 100644
--- a/packages/ckeditor5/src/plugins/linkembed.ts
+++ b/packages/ckeditor5/src/plugins/linkembed.ts
@@ -247,11 +247,14 @@ class ChangeLinkDisplayCommand extends Command {
declare value: LinkDisplayMode | null;
/** Whether the selected link supports embed mode (e.g. YouTube). */
declare embedAvailable: boolean;
+ /** URL of the selected link widget, exposed for the toolbar's open-link button. */
+ declare url: string | null;
constructor(editor: any) {
super(editor);
- // Register as observable so CKEditor's bind().to() works.
+ // Register as observables so CKEditor's bind().to() works.
this.set('embedAvailable', false);
+ this.set('url', null);
}
override execute(options: { value: LinkDisplayMode }) {
@@ -295,8 +298,10 @@ class ChangeLinkDisplayCommand extends Command {
if (selected) {
const url = selected.getAttribute('url') as string;
this.embedAvailable = this._detectEmbedType(url) !== 'opengraph';
+ this.url = url;
} else {
this.embedAvailable = false;
+ this.url = null;
}
}