Skip to content

Build your personal knowledge base with Trilium Notes

text editor/link previews/toolbar: add a copy button

Adorian Doranc4777d923e11
diff --git a/packages/ckeditor5/src/plugins/copy_link_url.ts b/packages/ckeditor5/src/plugins/copy_link_url.ts
index da57764369..4148c35130 100644
--- a/packages/ckeditor5/src/plugins/copy_link_url.ts
+++ b/packages/ckeditor5/src/plugins/copy_link_url.ts
@@ -1,6 +1,34 @@
-import { ButtonView, Plugin } from "ckeditor5";
+import { ButtonView, type Editor, type Locale, Plugin } from "ckeditor5";
import copyIcon from "../icons/copy.svg?raw";
+/**
+ * Builds a "Copy URL" toolbar button that copies `getUrl()` to the clipboard via
+ * the host-provided `clipboard.copy` callback (cross-browser fallback + toast).
+ * Shared by the default link toolbar and the link-embed widget toolbar.
+ */
+export function createCopyUrlButton(editor: Editor, locale: Locale, getUrl: () => string | null | undefined): ButtonView {
+ const button = new ButtonView(locale);
+ button.set({
+ label: translate(editor, "link.copy_url"),
+ icon: copyIcon,
+ tooltip: true
+ });
+
+ button.on("execute", () => {
+ const url = getUrl();
+ if (typeof url === "string" && url) {
+ editor.config.get("clipboard")?.copy?.(url);
+ }
+ });
+
+ return button;
+}
+
+function translate(editor: Editor, key: string) {
+ const translateFn = editor.config.get("translate") as ((key: string) => string) | undefined;
+ return translateFn ? translateFn(key) : key;
+}
+
/**
* Adds a "Copy URL" button to the link balloon toolbar, placed right after the URL preview.
* Copies the selected link's href to the clipboard via the host-provided `clipboard.copy`
@@ -11,28 +39,9 @@ export default class CopyLinkUrlButton extends Plugin {
init() {
const editor = this.editor;
- editor.ui.componentFactory.add("copyLinkUrl", (locale) => {
- const button = new ButtonView(locale);
- button.set({
- label: this._translate("link.copy_url"),
- icon: copyIcon,
- tooltip: true
- });
-
- this.listenTo(button, "execute", () => {
- const href = editor.commands.get("link")?.value;
- if (typeof href === "string" && href) {
- editor.config.get("clipboard")?.copy?.(href);
- }
- });
-
- return button;
- });
- }
-
- private _translate(key: string) {
- const translate = this.editor.config.get("translate") as ((key: string) => string) | undefined;
- return translate ? translate(key) : key;
+ editor.ui.componentFactory.add("copyLinkUrl", (locale) =>
+ createCopyUrlButton(editor, locale, () => editor.commands.get("link")?.value as string | null | undefined)
+ );
}
}
diff --git a/packages/ckeditor5/src/plugins/link_embed_toolbar.ts b/packages/ckeditor5/src/plugins/link_embed_toolbar.ts
index 8decf1d5e0..0e0014fc06 100644
--- a/packages/ckeditor5/src/plugins/link_embed_toolbar.ts
+++ b/packages/ckeditor5/src/plugins/link_embed_toolbar.ts
@@ -18,18 +18,19 @@ import LinkEmbed, {
LINK_DISPLAY_MODES,
type LinkDisplayMode
} from "./linkembed.js";
+import { createCopyUrlButton } from "./copy_link_url.js";
export default class LinkEmbedToolbar extends Plugin {
static get requires() {
- return [WidgetToolbarRepository, LinkEmbed, LinkEmbedLinkButton, LinkEmbedDisplayDropdown] as const;
+ return [WidgetToolbarRepository, LinkEmbed, LinkEmbedLinkButton, LinkEmbedCopyUrlButton, LinkEmbedDisplayDropdown] as const;
}
afterInit() {
const widgetToolbarRepository = this.editor.plugins.get(WidgetToolbarRepository);
widgetToolbarRepository.register("linkEmbed", {
- items: ["linkEmbedLink", "|", "linkEmbedDisplayDropdown"],
+ items: ["linkEmbedLink", "linkEmbedCopyUrl", "|", "linkEmbedDisplayDropdown"],
balloonClassName: "ck-toolbar-container link-embed-toolbar",
getRelatedElement(selection) {
const selectedElement = selection.getSelectedElement();
@@ -158,6 +159,26 @@ class LinkEmbedLinkButton extends Plugin {
}
}
+/**
+ * Registers the `linkEmbedCopyUrl` toolbar item: copies the selected widget's URL
+ * to the clipboard, mirroring the default link toolbar's copy-URL button.
+ */
+class LinkEmbedCopyUrlButton 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("linkEmbedCopyUrl", locale =>
+ createCopyUrlButton(editor, locale, () => command.url)
+ );
+ }
+}
+
/**
* 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