Skip to content

Build your personal knowledge base with Trilium Notes

text editor/link previews/toolbar: add an unlink button

Adorian Dorancd086c8d4428
diff --git a/packages/ckeditor5/src/plugins/link_embed_toolbar.ts b/packages/ckeditor5/src/plugins/link_embed_toolbar.ts
index 0e0014fc06..529738d3e8 100644
--- a/packages/ckeditor5/src/plugins/link_embed_toolbar.ts
+++ b/packages/ckeditor5/src/plugins/link_embed_toolbar.ts
@@ -9,6 +9,7 @@ import {
addListToDropdown,
ButtonView,
DropdownButtonView,
+ IconUnlink,
type ListDropdownButtonDefinition,
type Locale,
type Command
@@ -16,6 +17,7 @@ import {
import LinkEmbed, {
CHANGE_LINK_DISPLAY_COMMAND,
LINK_DISPLAY_MODES,
+ REMOVE_LINK_EMBED_COMMAND,
type LinkDisplayMode
} from "./linkembed.js";
import { createCopyUrlButton } from "./copy_link_url.js";
@@ -23,14 +25,14 @@ import { createCopyUrlButton } from "./copy_link_url.js";
export default class LinkEmbedToolbar extends Plugin {
static get requires() {
- return [WidgetToolbarRepository, LinkEmbed, LinkEmbedLinkButton, LinkEmbedCopyUrlButton, LinkEmbedDisplayDropdown] as const;
+ return [WidgetToolbarRepository, LinkEmbed, LinkEmbedLinkButton, LinkEmbedCopyUrlButton, LinkEmbedUnlinkButton, LinkEmbedDisplayDropdown] as const;
}
afterInit() {
const widgetToolbarRepository = this.editor.plugins.get(WidgetToolbarRepository);
widgetToolbarRepository.register("linkEmbed", {
- items: ["linkEmbedLink", "linkEmbedCopyUrl", "|", "linkEmbedDisplayDropdown"],
+ items: ["linkEmbedLink", "linkEmbedCopyUrl", "linkEmbedUnlink", "|", "linkEmbedDisplayDropdown"],
balloonClassName: "ck-toolbar-container link-embed-toolbar",
getRelatedElement(selection) {
const selectedElement = selection.getSelectedElement();
@@ -179,6 +181,43 @@ class LinkEmbedCopyUrlButton extends Plugin {
}
}
+/**
+ * Registers the `linkEmbedUnlink` toolbar item: removes the link preview, leaving
+ * the bare URL as plain text — mirroring the default link toolbar's unlink button
+ * (same icon, same "remove the link" semantics).
+ */
+class LinkEmbedUnlinkButton extends Plugin {
+
+ static get requires() {
+ return [LinkEmbed] as const;
+ }
+
+ public init() {
+ const editor = this.editor;
+ const command = editor.commands.get(REMOVE_LINK_EMBED_COMMAND);
+
+ editor.ui.componentFactory.add("linkEmbedUnlink", locale => {
+ const button = new ButtonView(locale);
+ button.set({
+ label: locale.t("Unlink"),
+ icon: IconUnlink,
+ tooltip: true
+ });
+
+ if (command) {
+ button.bind("isEnabled").to(command, "isEnabled");
+ }
+
+ button.on("execute", () => {
+ editor.execute(REMOVE_LINK_EMBED_COMMAND);
+ editor.editing.view.focus();
+ });
+
+ 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
diff --git a/packages/ckeditor5/src/plugins/linkembed.ts b/packages/ckeditor5/src/plugins/linkembed.ts
index e973fb36af..cb2f6e2e46 100644
--- a/packages/ckeditor5/src/plugins/linkembed.ts
+++ b/packages/ckeditor5/src/plugins/linkembed.ts
@@ -5,6 +5,7 @@ import { preventCKEditorHandling } from './widget_utils.js';
export const LINK_EMBED_COMMAND = 'insertLinkEmbed';
export const CHANGE_LINK_DISPLAY_COMMAND = 'changeLinkDisplay';
+export const REMOVE_LINK_EMBED_COMMAND = 'removeLinkEmbed';
export const LINK_DISPLAY_MODES = [
{ value: 'inline', label: 'Inline' },
@@ -63,6 +64,7 @@ class LinkEmbedEditing extends Plugin {
this._defineConverters();
this.editor.commands.add(LINK_EMBED_COMMAND, new InsertLinkEmbedCommand(this.editor));
this.editor.commands.add(CHANGE_LINK_DISPLAY_COMMAND, new ChangeLinkDisplayCommand(this.editor));
+ this.editor.commands.add(REMOVE_LINK_EMBED_COMMAND, new RemoveLinkEmbedCommand(this.editor));
}
_defineSchema() {
@@ -243,6 +245,15 @@ class InsertLinkEmbedCommand extends Command {
const META_KEYS = ['url', 'embedType', 'title', 'description', 'favicon', 'siteName', 'image'] as const;
+/** Returns the currently selected linkEmbed/linkMention element, or null. */
+function getSelectedLinkWidget(editor: any) {
+ const selected = editor.model.document.selection.getSelectedElement();
+ if (selected && (selected.name === 'linkMention' || selected.name === 'linkEmbed')) {
+ return selected;
+ }
+ return null;
+}
+
class ChangeLinkDisplayCommand extends Command {
declare value: LinkDisplayMode | null;
/** Whether the selected link supports embed mode (e.g. YouTube). */
@@ -259,7 +270,7 @@ class ChangeLinkDisplayCommand extends Command {
override execute(options: { value: LinkDisplayMode }) {
const model = this.editor.model;
- const selected = this._getSelectedLinkWidget();
+ const selected = getSelectedLinkWidget(this.editor);
if (!selected) return;
const targetMode = options.value;
@@ -291,7 +302,7 @@ class ChangeLinkDisplayCommand extends Command {
}
override refresh() {
- const selected = this._getSelectedLinkWidget();
+ const selected = getSelectedLinkWidget(this.editor);
this.isEnabled = !!selected;
this.value = selected ? this._getMode(selected) : null;
@@ -317,14 +328,25 @@ class ChangeLinkDisplayCommand extends Command {
const component = glob.getComponentByEl<EditorComponent>(editorEl);
return component.detectEmbedType(url);
}
+}
- private _getSelectedLinkWidget() {
- const selection = this.editor.model.document.selection;
- const selected = selection.getSelectedElement();
- if (selected && (selected.name === 'linkMention' || selected.name === 'linkEmbed')) {
- return selected;
- }
- return null;
+class RemoveLinkEmbedCommand extends Command {
+ override execute() {
+ const model = this.editor.model;
+ const selected = getSelectedLinkWidget(this.editor);
+ if (!selected) return;
+
+ const url = selected.getAttribute('url') as string;
+
+ // Mirror the default link's unlink: drop the link entirely, leaving the
+ // bare URL as plain (non-linked) text.
+ model.change(writer => {
+ model.insertContent(writer.createText(url), writer.createRangeOn(selected));
+ });
+ }
+
+ override refresh() {
+ this.isEnabled = !!getSelectedLinkWidget(this.editor);
}
}