Skip to content

Build your personal knowledge base with Trilium Notes

fix(client): add the attribute on the first Alt+L or Alt+R

Elian Doran860d23404f53
diff --git a/apps/client/src/widgets/attribute_widgets/AttributeEditor.tsx b/apps/client/src/widgets/attribute_widgets/AttributeEditor.tsx
index eb7dd2aff0..c808a5df41 100644
--- a/apps/client/src/widgets/attribute_widgets/AttributeEditor.tsx
+++ b/apps/client/src/widgets/attribute_widgets/AttributeEditor.tsx
@@ -40,6 +40,8 @@ interface AttributeEditorProps {
notePath?: string | null;
ntxId?: string | null;
hidden?: boolean;
+ /** Runs once the editor is ready, for the shortcut that caused it to be mounted. */
+ initialCommand?: "addNewLabel" | "addNewRelation";
}
export interface AttributeEditorImperativeHandlers {
@@ -49,7 +51,7 @@ export interface AttributeEditorImperativeHandlers {
renderOwnedAttributes(ownedAttributes: FAttribute[]): Promise<void>;
}
-export default function AttributeEditor({ api, note, componentId, notePath, ntxId, hidden }: AttributeEditorProps) {
+export default function AttributeEditor({ api, note, componentId, notePath, ntxId, hidden, initialCommand }: AttributeEditorProps) {
const [ currentValue, setCurrentValue ] = useState("");
const [ error, setError ] = useState<unknown>();
const [ needsSaving, setNeedsSaving ] = useState(false);
@@ -309,7 +311,12 @@ export default function AttributeEditor({ api, note, componentId, notePath, ntxI
}}
onKeyDown={() => attributeDetailWidget.hide()}
onBlur={() => save()}
- onInitialized={() => editorRef.current?.focus()}
+ onInitialized={() => {
+ editorRef.current?.focus();
+ if (initialCommand) {
+ void handleAddNewAttributeCommand(initialCommand);
+ }
+ }}
disableNewlines disableSpellcheck
/>}
diff --git a/apps/client/src/widgets/layout/StatusBar.spec.tsx b/apps/client/src/widgets/layout/StatusBar.spec.tsx
index f49afef82a..e335075457 100644
--- a/apps/client/src/widgets/layout/StatusBar.spec.tsx
+++ b/apps/client/src/widgets/layout/StatusBar.spec.tsx
@@ -18,10 +18,16 @@ vi.mock("../react/hooks", async (importOriginal) => ({
// The attributes panel builds its editor on CKEditor, which is a legacy widget wanting a real parent
// component to be a child of. What the panel is here is a panel that is up or is not.
+const editorProps = vi.hoisted(() => ({ current: null as Record<string, unknown> | null }));
vi.mock("../attribute_widgets/AttributeEditor", async () => {
const { h } = await import("preact");
- return { default: () => h("div", { class: "attribute-editor-stub" }) };
+ return {
+ default: (props: Record<string, unknown>) => {
+ editorProps.current = props;
+ return h("div", { class: "attribute-editor-stub" });
+ }
+ };
});
import appContext from "../../components/app_context";
@@ -75,6 +81,14 @@ describe("StatusBar panels", () => {
expect(isShown(".attribute-list")).toBe(false);
});
+ it("hands the shortcut that opens the attributes panel to the editor it mounts", () => {
+ renderBar();
+ fire("addNewRelation");
+
+ expect(isShown(".attribute-list")).toBe(true);
+ expect(editorProps.current?.initialCommand).toBe("addNewRelation");
+ });
+
it("offers the attributes panel a way to the sidebar's own, and steps aside once taken", () => {
const triggerEvent = vi.spyOn(appContext, "triggerEvent").mockResolvedValue(undefined);
renderBar();
diff --git a/apps/client/src/widgets/layout/StatusBar.tsx b/apps/client/src/widgets/layout/StatusBar.tsx
index 97cc02cab6..1bdbee944a 100644
--- a/apps/client/src/widgets/layout/StatusBar.tsx
+++ b/apps/client/src/widgets/layout/StatusBar.tsx
@@ -470,8 +470,14 @@ function AttributesPane({ note, noteContext, attributesShown, setAttributesShown
hidden: !note
};
- // Show on keyboard shortcuts.
- useTriliumEvents([ "addNewLabel", "addNewRelation" ], () => setAttributesShown(true));
+ // Show on keyboard shortcuts. The editor is not listening until it mounts, so it is handed the first one.
+ const [ initialCommand, setInitialCommand ] = useState<"addNewLabel" | "addNewRelation">();
+ useTriliumEvents([ "addNewLabel", "addNewRelation" ], (_, eventName) => {
+ if (!editorMounted) {
+ setInitialCommand(eventName);
+ }
+ setAttributesShown(true);
+ });
useTriliumEvents([ "toggleRibbonTabOwnedAttributes", "toggleRibbonTabInheritedAttributes" ], () => setAttributesShown(!attributesShown));
// Auto-focus the owned attributes.
@@ -505,6 +511,7 @@ function AttributesPane({ note, noteContext, attributesShown, setAttributesShown
api={api}
notePath={noteContext.notePath}
ntxId={noteContext.ntxId}
+ initialCommand={initialCommand}
/>}
</BottomPanel>
);