Skip to content

armgit / memos

Public Imported from GitHub
0

Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.

chore: tweak memo actions

boojackd2560e08773f
diff --git a/web/src/components/MemoEditor/Toolbar/EditorToolbar.tsx b/web/src/components/MemoEditor/Toolbar/EditorToolbar.tsx
index bb6ac87f6..c2e0d36d1 100644
--- a/web/src/components/MemoEditor/Toolbar/EditorToolbar.tsx
+++ b/web/src/components/MemoEditor/Toolbar/EditorToolbar.tsx
@@ -13,20 +13,13 @@ import AudienceMenu from "./AudienceMenu";
import InsertMenu from "./InsertMenu";
/**
- * Shortcut chip inside the commit button. While saving, a spinner takes the
- * chip's place; both layers share one grid cell so the button keeps its width
- * across the swap. Hidden on coarse pointers, where there is no keyboard to hint.
+ * Save shortcut as key caps, shown in the commit button's tooltip rather than on
+ * the button itself so the primary action reads as a plain verb.
*/
-const ShortcutChip: FC<{ busy: boolean }> = ({ busy }) => (
- <kbd
- aria-hidden
- className="grid place-items-center rounded-[4px] bg-primary-foreground/20 px-1 py-0.5 font-sans text-2xs leading-none font-medium pointer-coarse:hidden"
- >
- <span className={cn("col-start-1 row-start-1 inline-flex items-center gap-px", busy && "invisible")}>
- {primaryModifierGlyph()}
- <CornerDownLeftIcon className="size-2.5" strokeWidth={2.5} />
- </span>
- <LoaderIcon className={cn("col-start-1 row-start-1 size-2.5 animate-spin", !busy && "invisible")} strokeWidth={3} />
+const ShortcutKeys: FC = () => (
+ <kbd className="inline-flex items-center gap-px rounded-[4px] bg-primary-foreground/20 px-1 py-0.5 font-sans text-2xs leading-none font-medium">
+ {primaryModifierGlyph()}
+ <CornerDownLeftIcon className="size-2.5" strokeWidth={2.5} />
</kbd>
);
@@ -85,11 +78,35 @@ export const EditorToolbar: FC<EditorToolbarProps> = ({
{t("editor.saved")}
<CheckIcon className="size-3.5" strokeWidth={2.5} />
</Button>
+ ) : blockedMessage ? (
+ // A disabled button fires no pointer events, so a focusable wrapper hosts the tooltip.
+ <Tooltip>
+ <TooltipTrigger render={<span className="inline-flex" tabIndex={0} aria-label={blockedMessage} />}>
+ <Button size="sm" disabled>
+ {commitLabel}
+ </Button>
+ </TooltipTrigger>
+ <TooltipContent side="top">{blockedMessage}</TooltipContent>
+ </Tooltip>
) : (
- <Button size="sm" onClick={onSave} disabled={isSaving || !valid}>
- {commitLabel}
- <ShortcutChip busy={isSaving} />
- </Button>
+ // The same trigger stays mounted through the save so the button keeps focus;
+ // only its tooltip is switched off. Both modifiers save (see
+ // buildEditorExtensions), so both are announced.
+ <Tooltip disabled={isSaving}>
+ <TooltipTrigger render={<Button size="sm" onClick={onSave} disabled={isSaving} aria-keyshortcuts="Meta+Enter Control+Enter" />}>
+ {/* While saving, a spinner covers the label in the same grid cell so the button keeps its width. */}
+ <span className="grid place-items-center">
+ <span className={cn("col-start-1 row-start-1", isSaving && "invisible")}>{commitLabel}</span>
+ <LoaderIcon className={cn("col-start-1 row-start-1 size-3.5 animate-spin", !isSaving && "invisible")} strokeWidth={2.5} />
+ </span>
+ </TooltipTrigger>
+ <TooltipContent side="top">
+ <span className="flex items-center gap-1.5">
+ {commitLabel}
+ <ShortcutKeys />
+ </span>
+ </TooltipContent>
+ </Tooltip>
);
return (
@@ -122,16 +139,7 @@ export const EditorToolbar: FC<EditorToolbarProps> = ({
</Button>
)}
- {blockedMessage ? (
- <Tooltip>
- <TooltipTrigger render={<span className="inline-flex" tabIndex={0} aria-label={blockedMessage} />}>
- {commitButton}
- </TooltipTrigger>
- <TooltipContent side="top">{blockedMessage}</TooltipContent>
- </Tooltip>
- ) : (
- commitButton
- )}
+ {commitButton}
</div>
</div>
);
diff --git a/web/tests/editor-commit-button.test.tsx b/web/tests/editor-commit-button.test.tsx
new file mode 100644
index 000000000..38a15ec9d
--- /dev/null
+++ b/web/tests/editor-commit-button.test.tsx
@@ -0,0 +1,59 @@
+import { act, fireEvent, render, screen, waitFor } from "@testing-library/react";
+import { describe, expect, it, vi } from "vitest";
+import { createInitialState, EditorProvider, useEditorContext } from "@/components/MemoEditor/state";
+import { EditorToolbar } from "@/components/MemoEditor/Toolbar/EditorToolbar";
+
+vi.mock("@/components/MemoEditor/Toolbar/InsertMenu", () => ({ default: () => null }));
+vi.mock("@/components/MemoEditor/Toolbar/AudienceMenu", () => ({ default: () => null }));
+vi.mock("@/utils/i18n", () => ({ useTranslate: () => (key: string) => key }));
+
+let dispatchSaving: ((saving: boolean) => void) | undefined;
+
+const Toolbar = () => {
+ const { actions, dispatch } = useEditorContext();
+ dispatchSaving = (saving) => dispatch(actions.setLoading("saving", saving));
+ return <EditorToolbar onSave={vi.fn()} onAudioRecorderClick={vi.fn()} onInsertImages={vi.fn()} />;
+};
+
+const renderToolbar = (content: string) => {
+ const initial = createInitialState();
+ initial.content = content;
+ render(
+ <EditorProvider initialEditorState={initial}>
+ <Toolbar />
+ </EditorProvider>,
+ );
+};
+
+describe("EditorToolbar commit button", () => {
+ it("names only the verb and moves the shortcut into its tooltip", async () => {
+ renderToolbar("A thought");
+ const button = screen.getByRole("button", { name: "editor.save" });
+
+ expect(button).toHaveAttribute("aria-keyshortcuts", "Meta+Enter Control+Enter");
+ expect(button.querySelector("kbd")).toBeNull();
+
+ fireEvent.keyDown(document.body, { key: "Tab" });
+ act(() => button.focus());
+ const tooltip = await waitFor(
+ () => {
+ const popup = document.querySelector('[data-slot="tooltip-content"]');
+ expect(popup).not.toBeNull();
+ return popup!;
+ },
+ { timeout: 2000 },
+ );
+ expect(tooltip).toHaveTextContent("editor.save");
+ expect(tooltip.querySelector("kbd")).not.toBeNull();
+ });
+
+ it("keeps the same button mounted while saving", () => {
+ renderToolbar("A thought");
+ const button = screen.getByRole("button", { name: "editor.save" });
+
+ act(() => dispatchSaving?.(true));
+
+ expect(screen.getByRole("button")).toBe(button);
+ expect(button).toBeDisabled();
+ });
+});