Skip to content

Build your personal knowledge base with Trilium Notes

fix(mobile/text): return from a widget row to the main toolbar

Elian Doran6f0c504dd225
diff --git a/packages/ckeditor5/src/plugins/context_toolbar.spec.ts b/packages/ckeditor5/src/plugins/context_toolbar.spec.ts
index bfc90eea0f..229fc12ea9 100644
--- a/packages/ckeditor5/src/plugins/context_toolbar.spec.ts
+++ b/packages/ckeditor5/src/plugins/context_toolbar.spec.ts
@@ -7,8 +7,10 @@ import {
import { describe, expect, it, vi } from "vitest";
import { createTestEditor } from "../../test/editor-kit.js";
+import { installGlobMock } from "../../test/globals-test-kit.js";
import ContextToolbar from "./context_toolbar.js";
import ImagePosition from "./image_position.js";
+import LinkEmbedToolbar from "./link_embed/link_embed_toolbar.js";
import ToolbarGroupMenu, { type ToolbarGroupMenuRequest } from "./toolbar_group_menu.js";
const TABLE = "<table><tableRow><tableCell><paragraph>a[]</paragraph></tableCell></tableRow></table><paragraph>b</paragraph>";
@@ -212,6 +214,19 @@ describe("ContextToolbar", () => {
});
});
+ it("names the toolbar of a link preview after the link it is", async () => {
+ installGlobMock({ getComponentByEl: () => ({ renderLinkMention: vi.fn(), detectEmbedType: () => "opengraph" }) });
+ const editor = await createTestEditor([Essentials, Paragraph, LinkEmbedToolbar, ContextToolbar], {
+ toolbar: ["contextToolbar"]
+ });
+ editor.ui.focusTracker.isFocused = true;
+ setModelData(editor.model, "<paragraph>a[<linkMention url=\"https://a.b\"></linkMention>]b</paragraph>");
+ editor.ui.update();
+
+ expect(getButton(editor)).toMatchObject({ isVisible: true, label: "Link", icon: IconLink });
+ expect([...editor.plugins.get(ContextToolbar).toolbarView.items][0]).toMatchObject({ label: "Link" });
+ });
+
it("leaves the balloons alone in an editor without its button", async () => {
const { editor } = await createEditor(["undo"]);
expect(editor.plugins.get(WidgetToolbarRepository).isEnabled).toBe(true);
diff --git a/packages/ckeditor5/src/plugins/context_toolbar.ts b/packages/ckeditor5/src/plugins/context_toolbar.ts
index 96202aa340..8a677a9967 100644
--- a/packages/ckeditor5/src/plugins/context_toolbar.ts
+++ b/packages/ckeditor5/src/plugins/context_toolbar.ts
@@ -17,7 +17,8 @@ const TOOLBAR_ICONS: Record<string, string> = {
image: IconImage,
codeblock: IconCodeBlock,
admonition: admonitionIcon,
- link: IconLink
+ link: IconLink,
+ linkEmbed: IconLink
};
/** What `WidgetToolbarRepository` keeps of a registered toolbar. */
@@ -228,9 +229,10 @@ function getToolbarName(editor: Editor, source: ContextToolbarSource | null) {
case "admonition": return t("Admonition");
case "tabs": return t("Tabs");
case "multicolumnLayout": return t("Columns");
- case "contentEmbed":
- case "linkEmbed": return t("Embed");
- case "link": return t("Link");
+ case "contentEmbed": return t("Embed");
+ // A link preview, which is a link whatever its shape.
+ case "link":
+ case "linkEmbed": return t("Link");
default: return undefined;
}
}
diff --git a/packages/ckeditor5/src/plugins/text_formatting_toolbar.spec.ts b/packages/ckeditor5/src/plugins/text_formatting_toolbar.spec.ts
index aa9163849f..a03c9b28b7 100644
--- a/packages/ckeditor5/src/plugins/text_formatting_toolbar.spec.ts
+++ b/packages/ckeditor5/src/plugins/text_formatting_toolbar.spec.ts
@@ -1,6 +1,6 @@
import {
_setModelData as setModelData,
- Bold, ButtonView, type ClassicEditor, Essentials, IconPreviousArrow, IconText, Italic, Paragraph,
+ Bold, ButtonView, type ClassicEditor, Essentials, HorizontalLine, IconPreviousArrow, IconText, Italic, Paragraph,
ToolbarSeparatorView
} from "ckeditor5";
import { describe, expect, it, vi } from "vitest";
@@ -119,6 +119,22 @@ describe("TextFormattingToolbar", () => {
expect(plugin.isActive).toBe(true);
});
+ it("does not swap in for a selected object, which holds no text to format", async () => {
+ const editor = await createTestEditor([Essentials, Paragraph, Bold, HorizontalLine, TextFormattingToolbar], {
+ toolbar: ["textFormatting"],
+ textFormattingToolbar: ["bold"]
+ });
+ const plugin = editor.plugins.get(TextFormattingToolbar);
+ setModelData(editor.model, "<paragraph>fo[]obar</paragraph><horizontalLine></horizontalLine>");
+
+ editor.model.change((writer) => writer.setSelection(editor.model.document.getRoot()?.getChild(1) ?? null, "on"));
+ expect(plugin.isActive).toBe(false);
+
+ // Text selected from there still swaps it in.
+ select(editor, 0, 3);
+ expect(plugin.isActive).toBe(true);
+ });
+
it("does not follow the selection when no items are configured", async () => {
const editor = await createTestEditor([Essentials, Paragraph, TextFormattingToolbar], {
toolbar: ["textFormatting"]
diff --git a/packages/ckeditor5/src/plugins/text_formatting_toolbar.ts b/packages/ckeditor5/src/plugins/text_formatting_toolbar.ts
index 3cc7058a9e..8661321c3c 100644
--- a/packages/ckeditor5/src/plugins/text_formatting_toolbar.ts
+++ b/packages/ckeditor5/src/plugins/text_formatting_toolbar.ts
@@ -82,23 +82,29 @@ export default class TextFormattingToolbar extends Plugin {
/**
* Swaps the toolbar as the selection turns from a caret into a range of text and back. A range
- * that only grows or shrinks, as a selection handle is dragged, swaps nothing.
+ * that only grows or shrinks, as a selection handle is dragged, swaps nothing, and neither does
+ * a selected object, such as an image, which holds no text to format.
*/
private followSelection() {
- const selection = this.editor.model.document.selection;
- let wasCollapsed = selection.isCollapsed;
+ const { model } = this.editor;
+ const selection = model.document.selection;
+ const holdsText = () => {
+ const selected = selection.getSelectedElement();
+ return !selection.isCollapsed && !(selected && model.schema.isObject(selected));
+ };
+ let heldText = holdsText();
this.listenTo(selection, "change:range", () => {
- const isCollapsed = selection.isCollapsed;
- if (isCollapsed === wasCollapsed) {
+ const isHoldingText = holdsText();
+ if (isHoldingText === heldText) {
return;
}
- wasCollapsed = isCollapsed;
+ heldText = isHoldingText;
- if (!isCollapsed && !this.isActive) {
+ if (isHoldingText && !this.isActive) {
this.isActive = true;
this.shownForSelection = true;
- } else if (isCollapsed && this.shownForSelection) {
+ } else if (!isHoldingText && this.shownForSelection) {
this.isActive = false;
this.shownForSelection = false;
}