mirror of
https://github.com/zadam/trilium.git
synced 2026-03-22 16:23:50 +01:00
test: add test for attribute_autocomplete
This commit is contained in:
parent
334c8cbea3
commit
59ebd0f122
47
apps/client/src/services/attribute_autocomplete.spec.ts
Normal file
47
apps/client/src/services/attribute_autocomplete.spec.ts
Normal file
@ -0,0 +1,47 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { shouldAutocompleteHandleEnterKey } from "./attribute_autocomplete.js";
|
||||
|
||||
describe("attribute autocomplete enter handling", () => {
|
||||
it("delegates plain Enter when the panel is open and an item is active", () => {
|
||||
expect(shouldAutocompleteHandleEnterKey(
|
||||
{ key: "Enter", ctrlKey: false, metaKey: false },
|
||||
{ isPanelOpen: true, hasActiveItem: true }
|
||||
)).toBe(true);
|
||||
});
|
||||
|
||||
it("does not delegate plain Enter when there is no active suggestion", () => {
|
||||
expect(shouldAutocompleteHandleEnterKey(
|
||||
{ key: "Enter", ctrlKey: false, metaKey: false },
|
||||
{ isPanelOpen: true, hasActiveItem: false }
|
||||
)).toBe(false);
|
||||
});
|
||||
|
||||
it("does not delegate plain Enter when the panel is closed", () => {
|
||||
expect(shouldAutocompleteHandleEnterKey(
|
||||
{ key: "Enter", ctrlKey: false, metaKey: false },
|
||||
{ isPanelOpen: false, hasActiveItem: false }
|
||||
)).toBe(false);
|
||||
});
|
||||
|
||||
it("does not delegate Ctrl+Enter even when an item is active", () => {
|
||||
expect(shouldAutocompleteHandleEnterKey(
|
||||
{ key: "Enter", ctrlKey: true, metaKey: false },
|
||||
{ isPanelOpen: true, hasActiveItem: true }
|
||||
)).toBe(false);
|
||||
});
|
||||
|
||||
it("does not delegate Cmd+Enter even when an item is active", () => {
|
||||
expect(shouldAutocompleteHandleEnterKey(
|
||||
{ key: "Enter", ctrlKey: false, metaKey: true },
|
||||
{ isPanelOpen: true, hasActiveItem: true }
|
||||
)).toBe(false);
|
||||
});
|
||||
|
||||
it("ignores non-Enter keys", () => {
|
||||
expect(shouldAutocompleteHandleEnterKey(
|
||||
{ key: "ArrowDown", ctrlKey: false, metaKey: false },
|
||||
{ isPanelOpen: false, hasActiveItem: false }
|
||||
)).toBe(true);
|
||||
});
|
||||
});
|
||||
@ -13,6 +13,21 @@ interface NameItem extends BaseItem {
|
||||
name: string;
|
||||
}
|
||||
|
||||
export function shouldAutocompleteHandleEnterKey(
|
||||
event: Pick<KeyboardEvent, "key" | "ctrlKey" | "metaKey">,
|
||||
{ isPanelOpen, hasActiveItem }: { isPanelOpen: boolean; hasActiveItem: boolean }
|
||||
) {
|
||||
if (event.key !== "Enter") {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (event.ctrlKey || event.metaKey) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return isPanelOpen && hasActiveItem;
|
||||
}
|
||||
|
||||
interface InitAttributeNameOptions {
|
||||
/** The <input> element where the user types */
|
||||
$el: JQuery<HTMLElement>;
|
||||
@ -200,22 +215,16 @@ function initAttributeNameAutocomplete({ $el, attributeType, open, onValueChange
|
||||
}, 50);
|
||||
},
|
||||
onKeyDown(e, handlers) {
|
||||
if (e.key === "Enter") {
|
||||
if (e.ctrlKey || e.metaKey) {
|
||||
// Let the outer widget handle save shortcuts such as Ctrl+Enter.
|
||||
return;
|
||||
}
|
||||
|
||||
if (isPanelOpen && hasActiveItem) {
|
||||
// Prevent the enter key from propagating to parent dialogs
|
||||
// (which might interpret it as "submit" or "save and close")
|
||||
e.stopPropagation();
|
||||
} else {
|
||||
// No active suggestion means the user is keeping their typed value.
|
||||
// Let outer shortcuts continue to bubble.
|
||||
return;
|
||||
}
|
||||
if (!shouldAutocompleteHandleEnterKey(e, { isPanelOpen, hasActiveItem })) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (e.key === "Enter") {
|
||||
// Prevent the enter key from propagating to parent dialogs
|
||||
// (which might interpret it as "submit" or "save and close")
|
||||
e.stopPropagation();
|
||||
}
|
||||
|
||||
handlers.onKeyDown(e as any);
|
||||
}
|
||||
});
|
||||
@ -397,20 +406,14 @@ function initLabelValueAutocomplete({ $el, open, nameCallback, onValueChange }:
|
||||
}, 50);
|
||||
},
|
||||
onKeyDown(e, handlers) {
|
||||
if (e.key === "Enter") {
|
||||
if (e.ctrlKey || e.metaKey) {
|
||||
// Let the outer widget handle save shortcuts such as Ctrl+Enter.
|
||||
return;
|
||||
}
|
||||
|
||||
if (isPanelOpen && hasActiveItem) {
|
||||
e.stopPropagation();
|
||||
} else {
|
||||
// No active suggestion means the user is keeping their typed value.
|
||||
// Let outer shortcuts such as Ctrl+Enter continue to bubble.
|
||||
return;
|
||||
}
|
||||
if (!shouldAutocompleteHandleEnterKey(e, { isPanelOpen, hasActiveItem })) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (e.key === "Enter") {
|
||||
e.stopPropagation();
|
||||
}
|
||||
|
||||
handlers.onKeyDown(e as any);
|
||||
}
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user