Skip to content

Build your personal knowledge base with Trilium Notes

feat(text): separate the Editable toggle, display modes and tools of an embed

Elian Doraneb853d556376
diff --git a/apps/client/src/widgets/type_widgets/mermaid/MermaidDiagram.spec.tsx b/apps/client/src/widgets/type_widgets/mermaid/MermaidDiagram.spec.tsx
index faf6e1dc8a..3481656a90 100644
--- a/apps/client/src/widgets/type_widgets/mermaid/MermaidDiagram.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/mermaid/MermaidDiagram.spec.tsx
@@ -90,6 +90,17 @@ describe("MermaidDiagram", () => {
.not.toContain("syntaxHelp");
});
+ it("groups the fullscreen with the zoom, and the help apart", async () => {
+ const { figure } = await mount({ editor: buildEditor(), displayMode: "split" });
+ figure.dataset.hideTitle = "true";
+ figure.dataset.boxSize = "medium";
+ const tools = getContentEmbedTools(figure)?.getTools() ?? [];
+ expect(tools.map(({ id, group }) => [ id, group ])).toEqual([
+ [ "zoomOut", "zoom" ], [ "resetZoom", "zoom" ], [ "zoomIn", "zoom" ],
+ [ "fullscreen", "zoom" ], [ "syntaxHelp", "help" ]
+ ]);
+ });
+
it("makes the source editable once Editable turns on", async () => {
const { figure } = await mount({ editor: buildEditor(), displayMode: "split" });
expect(await isSourceEditable(figure)).toBe(false);
diff --git a/packages/ckeditor5/src/plugins/content_embed/content_embed.spec.ts b/packages/ckeditor5/src/plugins/content_embed/content_embed.spec.ts
index 7d4433d7ba..2d64942f52 100644
--- a/packages/ckeditor5/src/plugins/content_embed/content_embed.spec.ts
+++ b/packages/ckeditor5/src/plugins/content_embed/content_embed.spec.ts
@@ -29,6 +29,7 @@ import ContentEmbed, {
CONVERT_EMBED_TO_LINK_COMMAND,
CONVERT_LINK_TO_EMBED_COMMAND,
CONTENT_EMBED_DISPLAY_MODE_COMMAND,
+ CONTENT_EMBED_DISPLAY_MODE_SEPARATOR,
CONTENT_EMBED_MENU,
DISPLAY_MODE_BUTTONS,
DISPLAY_MODES,
@@ -1644,6 +1645,23 @@ describe("ContentEmbed display mode", () => {
.toMatchObject({ displayMode: "preview" });
});
+ it("separates the display modes from the Editable toggle, while both show", () => {
+ const separator = editor.ui.componentFactory.create(CONTENT_EMBED_DISPLAY_MODE_SEPARATOR);
+ separator.render();
+ insertContentEmbed(editor, "n1", "medium");
+ const isShown = () => !separator.element?.classList.contains("ck-hidden");
+ expect(separator.element?.classList.contains("ck-toolbar__separator")).toBe(true);
+ expect(isShown()).toBe(false);
+
+ getContentEmbedTools.mockReturnValue({ hasEditableFlag: true, hasDisplayModes: true });
+ editor.ui.update();
+ expect(isShown()).toBe(true);
+
+ getContentEmbedTools.mockReturnValue({ hasEditableFlag: true });
+ editor.ui.update();
+ expect(isShown()).toBe(false);
+ });
+
it("offers a button per mode, on while it is the mode and shown only while enabled", () => {
const buttons = DISPLAY_MODES.map((mode) => {
const button = editor.ui.componentFactory.create(DISPLAY_MODE_BUTTONS[mode]);
diff --git a/packages/ckeditor5/src/plugins/content_embed/content_embed.ts b/packages/ckeditor5/src/plugins/content_embed/content_embed.ts
index 15f73bdfc4..bdc6d61dd6 100644
--- a/packages/ckeditor5/src/plugins/content_embed/content_embed.ts
+++ b/packages/ckeditor5/src/plugins/content_embed/content_embed.ts
@@ -7,6 +7,7 @@ import {
IconCaption,
IconLink,
IconThreeVerticalDots,
+ type Locale,
type MapperModelToViewPositionEvent,
ModelElement,
type ModelNode,
@@ -16,6 +17,7 @@ import {
Plugin,
toWidget,
toWidgetEditable,
+ View,
type ViewEditableElement,
type ViewElement,
Widget,
@@ -41,6 +43,8 @@ export const TOGGLE_CAPTION_COMMAND_NAME = 'toggleContentEmbedCaption';
export const TOGGLE_TITLE_COMMAND_NAME = 'toggleContentEmbedTitle';
export const TOGGLE_EDITABLE_COMMAND_NAME = 'toggleContentEmbedEditable';
export const CONTENT_EMBED_DISPLAY_MODE_COMMAND = 'contentEmbedDisplayMode';
+/** The toolbar separator between the Editable toggle and the display modes. */
+export const CONTENT_EMBED_DISPLAY_MODE_SEPARATOR = 'contentEmbedDisplayModeSeparator';
/** The toolbar button that opens the context menu of an embed whose title is hidden. */
export const CONTENT_EMBED_MENU = 'contentEmbedMenu';
@@ -341,6 +345,14 @@ class ContentEmbedUI extends Plugin {
return buttonView;
} );
+ editor.ui.componentFactory.add( CONTENT_EMBED_DISPLAY_MODE_SEPARATOR, locale => {
+ const separator = new ToolbarItemSeparatorView( locale );
+ separator.bind( 'isVisible' ).to(
+ editor.plugins.get( ContentEmbed ), 'selectedEmbedTools',
+ tools => !!tools?.hasEditableFlag && !!tools.hasDisplayModes );
+ return separator;
+ } );
+
for ( const mode of DISPLAY_MODES ) {
editor.ui.componentFactory.add( DISPLAY_MODE_BUTTONS[ mode ], locale => {
const command = editor.commands.get( CONTENT_EMBED_DISPLAY_MODE_COMMAND );
@@ -1547,3 +1559,24 @@ declare module 'ckeditor5' {
contentEmbedDisplayMode: ContentEmbedDisplayModeCommand;
}
}
+
+/** A toolbar separator that shows only while `isVisible`, as the items on both sides do. */
+class ToolbarItemSeparatorView extends View {
+ declare public isVisible: boolean;
+
+ constructor( locale?: Locale ) {
+ super( locale );
+ this.set( 'isVisible', false );
+
+ this.setTemplate( {
+ tag: 'span',
+ attributes: {
+ class: [
+ 'ck',
+ 'ck-toolbar__separator',
+ this.bindTemplate.if( 'isVisible', 'ck-hidden', isVisible => !isVisible )
+ ]
+ }
+ } );
+ }
+}
diff --git a/packages/ckeditor5/src/plugins/content_embed/content_embed_toolbar.spec.ts b/packages/ckeditor5/src/plugins/content_embed/content_embed_toolbar.spec.ts
index a8aaed2544..47a07e44ff 100644
--- a/packages/ckeditor5/src/plugins/content_embed/content_embed_toolbar.spec.ts
+++ b/packages/ckeditor5/src/plugins/content_embed/content_embed_toolbar.spec.ts
@@ -109,6 +109,7 @@ describe("ContentEmbedToolbar", () => {
expect(repository._toolbarDefinitions.get("contentEmbed")?.itemsConfig).toEqual([
TOGGLE_EDITABLE_COMMAND_NAME,
+ "contentEmbedDisplayModeSeparator",
"contentEmbedDisplayMode:source",
"contentEmbedDisplayMode:split",
"contentEmbedDisplayMode:preview",
diff --git a/packages/ckeditor5/src/plugins/content_embed/content_embed_toolbar.ts b/packages/ckeditor5/src/plugins/content_embed/content_embed_toolbar.ts
index d609c6a984..e9f8272659 100644
--- a/packages/ckeditor5/src/plugins/content_embed/content_embed_toolbar.ts
+++ b/packages/ckeditor5/src/plugins/content_embed/content_embed_toolbar.ts
@@ -2,6 +2,7 @@ import { Plugin, WidgetToolbarRepository, isWidget, type ViewElement } from "cke
import ContentEmbed, {
CONVERT_EMBED_TO_LINK_COMMAND,
CONTENT_EMBED_MENU,
+ CONTENT_EMBED_DISPLAY_MODE_SEPARATOR,
DISPLAY_MODE_BUTTONS,
DISPLAY_MODES,
TOGGLE_CAPTION_COMMAND_NAME,
@@ -28,6 +29,7 @@ export default class ContentEmbedToolbar extends Plugin {
widgetToolbarRepository.register("contentEmbed", {
items: [
TOGGLE_EDITABLE_COMMAND_NAME,
+ CONTENT_EMBED_DISPLAY_MODE_SEPARATOR,
...DISPLAY_MODES.map((mode) => DISPLAY_MODE_BUTTONS[mode]),
CONTENT_EMBED_TOOLS,
CONTENT_EMBED_BOX_SIZE_DROPDOWN,
diff --git a/packages/ckeditor5/src/plugins/content_embed/content_embed_tools.spec.ts b/packages/ckeditor5/src/plugins/content_embed/content_embed_tools.spec.ts
index e411494e15..60a662c9d2 100644
--- a/packages/ckeditor5/src/plugins/content_embed/content_embed_tools.spec.ts
+++ b/packages/ckeditor5/src/plugins/content_embed/content_embed_tools.spec.ts
@@ -153,6 +153,21 @@ describe("ContentEmbedTools", () => {
expect(getButtons(tools)[2]?.element?.classList.contains("undo")).toBe(true);
});
+ it("separates the tools from the Editable toggle and the display modes before them", () => {
+ const tools = createView();
+ provider = { ...provider, hasEditableFlag: true };
+ selectEmbed();
+ expect(getItems(tools)).toEqual([ "|", "1", "2" ]);
+
+ provider = { ...provider, hasEditableFlag: false, hasDisplayModes: true };
+ editor.ui.update();
+ expect(getItems(tools)).toEqual([ "|", "1", "2" ]);
+
+ provider = { ...provider, hasDisplayModes: false };
+ editor.ui.update();
+ expect(getItems(tools)).toEqual([ "1", "2" ]);
+ });
+
it("shows the children of a tool in a menu, without taking the focus", () => {
const tools = createView();
selectEmbed();
diff --git a/packages/ckeditor5/src/plugins/content_embed/content_embed_tools.ts b/packages/ckeditor5/src/plugins/content_embed/content_embed_tools.ts
index 2ab2d1d63a..cefe48928b 100644
--- a/packages/ckeditor5/src/plugins/content_embed/content_embed_tools.ts
+++ b/packages/ckeditor5/src/plugins/content_embed/content_embed_tools.ts
@@ -116,17 +116,20 @@ export class ContentEmbedToolsView extends View {
/** Shows the tools of the provider. Returns whether the views were created again. */
private showTools() {
const tools = this.provider?.getTools() ?? [];
+ // The Editable toggle and the display modes come before the tools.
+ const isAfterItems = !!(this.provider?.hasEditableFlag || this.provider?.hasDisplayModes);
const keys = tools.map(tool => JSON.stringify([
tool.group,
tool.id,
tool.children?.map(child => [ child.group, child.id, child.keystroke ])
]));
+ keys.push(String(isAfterItems));
const isNewSet = keys.length !== this.toolKeys.length
|| keys.some((key, index) => key !== this.toolKeys[index]);
if (isNewSet) {
this.toolKeys = keys;
- this.createViews(tools);
+ this.createViews(tools, isAfterItems);
}
for (const [ index, tool ] of tools.entries()) {
@@ -137,11 +140,17 @@ export class ContentEmbedToolsView extends View {
return isNewSet;
}
- /** Creates a view for each tool, with a separator between groups. */
- private createViews(tools: ContentEmbedTool[]) {
+ /**
+ * Creates a view for each tool, with a separator between groups, and before the first one
+ * when `isAfterItems`.
+ */
+ private createViews(tools: ContentEmbedTool[], isAfterItems: boolean) {
this.items.clear();
this.views = [];
this.updaters = [];
+ if (isAfterItems && tools.length) {
+ this.items.add(new ToolbarSeparatorView(this.locale));
+ }
let group: string | undefined;
for (const tool of tools) {
diff --git a/packages/ckeditor5/src/theme/content_embed_tools.css b/packages/ckeditor5/src/theme/content_embed_tools.css
index aa5ceef483..b025dd024e 100644
--- a/packages/ckeditor5/src/theme/content_embed_tools.css
+++ b/packages/ckeditor5/src/theme/content_embed_tools.css
@@ -8,3 +8,9 @@
border-inline-end: 1px solid var(--ck-content-embed-tools-divider-color);
padding-inline-end: var(--ck-spacing-small, var(--ck-spacing-sm));
}
+
+/* CKEditor hides a toolbar separator that is the first child of its container, which the one
+ between the embed's own items and these tools is. */
+.ck.ck-toolbar .ck.ck-content-embed-tools > .ck.ck-toolbar__separator:first-child {
+ display: inline-block;
+}