Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor/text note transclusion: add a save status indicator

Adorian Doran28f061e61c5e
diff --git a/apps/client/src/stylesheets/style.css b/apps/client/src/stylesheets/style.css
index c180f8e1b3..13518ef45f 100644
--- a/apps/client/src/stylesheets/style.css
+++ b/apps/client/src/stylesheets/style.css
@@ -1512,6 +1512,10 @@ body.mobile .modal-content-with-sidebar > .modal-main > .modal-header > .modal-t
flex-shrink: 0;
}
+.include-note-title-row > .include-note-badges:empty {
+ display: none;
+}
+
.include-note-toggle {
transition: transform 0.2s ease;
}
diff --git a/apps/client/src/widgets/type_widgets/text/ContentEmbed.spec.tsx b/apps/client/src/widgets/type_widgets/text/ContentEmbed.spec.tsx
index 1b3bc251fb..b917842615 100644
--- a/apps/client/src/widgets/type_widgets/text/ContentEmbed.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/text/ContentEmbed.spec.tsx
@@ -168,14 +168,14 @@ function click(target: HTMLElement) {
describe("ContentEmbed", () => {
it("lays out the title row for each box size", () => {
- const plain = [ "title", "open", "menu" ];
- const withFullscreen = [ "title", "open", "fullscreen", "menu" ];
+ const plain = [ "title", "badges", "open", "menu" ];
+ const withFullscreen = [ "title", "badges", "open", "fullscreen", "menu" ];
const layouts: [ string | undefined, string[] ][] = [
[ undefined, plain ],
[ "small", plain ],
[ "medium", withFullscreen ],
[ "full", withFullscreen ],
- [ "expandable", [ "toggle", "title", "open", "menu" ] ]
+ [ "expandable", [ "toggle", "title", "badges", "open", "menu" ] ]
];
for (const [ boxSize, expected ] of layouts) {
diff --git a/apps/client/src/widgets/type_widgets/text/ContentEmbed.tsx b/apps/client/src/widgets/type_widgets/text/ContentEmbed.tsx
index 587e7ef38c..0f0284a09c 100644
--- a/apps/client/src/widgets/type_widgets/text/ContentEmbed.tsx
+++ b/apps/client/src/widgets/type_widgets/text/ContentEmbed.tsx
@@ -98,6 +98,8 @@ export default function ContentEmbed({
/>
)}
<ContentEmbedTitle title={title} />
+ {/* The content of the embed puts badges here, such as its save status. */}
+ <div className="note-badges include-note-badges" />
<ContentEmbedActionButton
className="include-note-open"
action={getOpenInNewTabAction(notePath, viewScope)}
diff --git a/apps/client/src/widgets/type_widgets/text/TextEmbed.spec.tsx b/apps/client/src/widgets/type_widgets/text/TextEmbed.spec.tsx
index c303795e4a..0397aa11f1 100644
--- a/apps/client/src/widgets/type_widgets/text/TextEmbed.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/text/TextEmbed.spec.tsx
@@ -82,11 +82,19 @@ describe("TextEmbed", () => {
expect(stub && appContext.getComponentByEl(stub)).toBe(props.parentComponent);
expect(figure.querySelector(".editable-embed-preview")).toBeNull();
+ // The title row of the embed shows how the saving of the note goes.
+ act(() => props.noteContext?.setContextData("saveState", { state: "error" }));
+ await vi.waitFor(() => {
+ expect(figure.querySelector(".include-note-badges > .save-status-badge.error"))
+ .not.toBeNull();
+ });
+
await act(async () => {
delete figure.dataset.editable;
await Promise.resolve();
});
expect(editorAskedToSave).toHaveBeenCalledWith({ ntxIds: [ props.ntxId ] });
+ expect(figure.querySelector(".save-status-badge")).toBeNull();
expect(figure.querySelector(".editable-text-stub")).toBeNull();
expect(figure.querySelector(".editable-embed-preview")).not.toBeNull();
});
@@ -104,9 +112,15 @@ async function mount(note: FNote, editor: ContentEditor | undefined, options: Mo
if (options.isEditable) {
figure.dataset.editable = "true";
}
- const box = document.createElement("div");
- box.className = "include-note-content";
- figure.append(box);
+ // The markup of `ContentEmbed`, with the slot for the badges in its title row.
+ const wrapper = document.createElement("div");
+ wrapper.className = "include-note-wrapper";
+ wrapper.innerHTML = `<div class="include-note-title-row">`
+ + `<div class="note-badges include-note-badges"></div></div>`
+ + `<div class="include-note-body"><div class="include-note-content"></div></div>`;
+ const box = wrapper.querySelector(".include-note-content");
+ if (!box) throw new Error("Expected the content box.");
+ figure.append(wrapper);
document.body.append(figure);
figures.push(figure);
diff --git a/apps/client/src/widgets/type_widgets/text/TextEmbedEditor.tsx b/apps/client/src/widgets/type_widgets/text/TextEmbedEditor.tsx
index f6f7400a65..4e43be4794 100644
--- a/apps/client/src/widgets/type_widgets/text/TextEmbedEditor.tsx
+++ b/apps/client/src/widgets/type_widgets/text/TextEmbedEditor.tsx
@@ -1,5 +1,6 @@
import "./TextEmbedEditor.css";
+import { createPortal } from "preact";
import { useLayoutEffect, useRef, useState } from "preact/hooks";
import type FNote from "../../../entities/fnote";
@@ -7,9 +8,10 @@ import { randomString } from "../../../services/utils";
import {
announceEmbeddedNoteClosing, EmbeddedNoteScope, useEmbeddedNoteContext
} from "../../EmbeddedNotePane";
+import { SaveStatusBadge } from "../../layout/NoteBadges";
import { useLegacyComponentElement, useNoteContext } from "../../react/hooks";
import EditableText from "./EditableText";
-import { NestedEmbedContext } from "./editable_embed";
+import { NestedEmbedContext, useEmbedBadgeSlot } from "./editable_embed";
/**
* The text editor of a text note in an embed. It has a note context and a component of its own,
@@ -41,6 +43,7 @@ function ScopedEditor({ note }: { note: FNote }) {
const { note: contextNote, noteContext, ntxId, parentComponent } = useNoteContext();
// The plugins of the editor find their host by the element of its component.
useLegacyComponentElement(rootRef);
+ const badgeSlot = useEmbedBadgeSlot(rootRef);
return (
<div ref={rootRef} className="text-embed-editor">
@@ -53,6 +56,7 @@ function ScopedEditor({ note }: { note: FNote }) {
viewScope={noteContext.viewScope}
/>
)}
+ {badgeSlot && createPortal(<SaveStatusBadge />, badgeSlot)}
</div>
);
}
diff --git a/apps/client/src/widgets/type_widgets/text/editable_embed.tsx b/apps/client/src/widgets/type_widgets/text/editable_embed.tsx
index c23488f8b4..ffc2689f07 100644
--- a/apps/client/src/widgets/type_widgets/text/editable_embed.tsx
+++ b/apps/client/src/widgets/type_widgets/text/editable_embed.tsx
@@ -163,6 +163,23 @@ export function useNestedEditor(ntxId: string | null | undefined) {
return editor;
}
+/**
+ * The element in the title row of the embed around `rootRef` that holds the badges of its content,
+ * such as the save status of an included note, or `null`.
+ */
+export function useEmbedBadgeSlot(rootRef: RefObject<HTMLElement | null>) {
+ const [ slot, setSlot ] = useState<HTMLElement | null>(null);
+
+ useEffect(() => {
+ const wrapper = rootRef.current?.closest(".include-note-wrapper");
+ setSlot(wrapper?.querySelector<HTMLElement>(
+ ":scope > .include-note-title-row > .include-note-badges"
+ ) ?? null);
+ }, [ rootRef ]);
+
+ return slot;
+}
+
/** The editable root of a text editor, which CKEditor gives a reference to the editor. */
type EditorRootElement = HTMLElement & { ckeditorInstance?: CKTextEditor };