2023-07-31 04:19:04 +02:00
|
|
|
import { Signal, useSignal } from "@preact/signals";
|
2023-08-09 23:51:40 +02:00
|
|
|
import { useEffect, useRef } from "preact/hooks";
|
2023-07-31 04:19:04 +02:00
|
|
|
import { useEventListener } from "@lib/hooks/useEventListener.ts";
|
|
|
|
import { menus } from "@islands/KMenu/commands.ts";
|
|
|
|
import { MenuEntry } from "@islands/KMenu/types.ts";
|
2023-08-02 13:11:17 +02:00
|
|
|
import * as icons from "@components/icons.tsx";
|
2023-08-08 11:18:54 +02:00
|
|
|
import { IS_BROWSER } from "$fresh/runtime.ts";
|
2023-07-31 04:19:04 +02:00
|
|
|
const KMenuEntry = (
|
|
|
|
{ entry, activeIndex, index }: {
|
|
|
|
entry: MenuEntry;
|
|
|
|
activeIndex: Signal<number>;
|
|
|
|
index: number;
|
|
|
|
},
|
|
|
|
) => {
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
onClick={() => activeIndex.value = index}
|
2023-08-02 13:11:17 +02:00
|
|
|
class={`px-4 py-2 flex items-center gap-1 ${
|
2023-07-31 04:19:04 +02:00
|
|
|
activeIndex.value === index
|
|
|
|
? "bg-gray-100 text-gray-900"
|
|
|
|
: "text-gray-400"
|
|
|
|
}`}
|
|
|
|
>
|
2023-08-02 13:11:17 +02:00
|
|
|
{entry?.icon && icons[entry.icon]({ class: "w-4 h-4 mr-1" })}
|
2023-07-31 04:19:04 +02:00
|
|
|
{entry.title}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const KMenu = (
|
|
|
|
{ type = "main", context }: { context: unknown; type: keyof typeof menus },
|
|
|
|
) => {
|
|
|
|
const activeMenuType = useSignal(type);
|
|
|
|
const activeMenu = menus[activeMenuType.value || "main"];
|
2023-08-02 01:58:03 +02:00
|
|
|
const activeState = useSignal<"normal" | "loading" | "error" | "input">(
|
|
|
|
"normal",
|
|
|
|
);
|
2023-08-02 15:05:35 +02:00
|
|
|
const loadingText = useSignal("");
|
2023-07-31 04:19:04 +02:00
|
|
|
const activeIndex = useSignal(-1);
|
|
|
|
|
2023-08-04 13:48:12 +02:00
|
|
|
const containerRef = useRef<HTMLDivElement>(null);
|
2023-07-31 04:19:04 +02:00
|
|
|
const input = useRef<HTMLInputElement>(null);
|
|
|
|
const commandInput = useSignal("");
|
|
|
|
|
2023-08-02 13:25:19 +02:00
|
|
|
const visible = useSignal(false);
|
2023-08-01 21:35:21 +02:00
|
|
|
if (visible.value === false) {
|
|
|
|
setTimeout(() => {
|
|
|
|
activeMenuType.value = "main";
|
|
|
|
activeState.value = "normal";
|
|
|
|
if (input.current) {
|
|
|
|
input.current.value = "";
|
|
|
|
}
|
|
|
|
commandInput.value = "";
|
|
|
|
}, 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
const entries = activeMenu?.entries?.filter((entry) => {
|
|
|
|
const entryVisible = typeof entry["visible"] === "boolean"
|
|
|
|
? entry.visible
|
|
|
|
: typeof entry["visible"] === "function"
|
|
|
|
? entry.visible?.(context)
|
|
|
|
: true;
|
|
|
|
|
|
|
|
const search = commandInput?.value?.toLowerCase();
|
|
|
|
const searchMatches = entry.title.toLowerCase().includes(search) ||
|
|
|
|
entry.meta?.toLowerCase()?.includes(search);
|
|
|
|
|
|
|
|
return entryVisible && searchMatches;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (entries.length === 1) {
|
|
|
|
activeIndex.value = 0;
|
|
|
|
}
|
|
|
|
|
2023-07-31 04:19:04 +02:00
|
|
|
function activateEntry(menuEntry?: MenuEntry) {
|
|
|
|
if (!menuEntry) return;
|
|
|
|
|
|
|
|
menuEntry.cb({
|
|
|
|
activeMenu: activeMenuType,
|
2023-08-02 15:05:35 +02:00
|
|
|
loadingText,
|
2023-07-31 04:19:04 +02:00
|
|
|
menus,
|
|
|
|
activeState,
|
2023-08-01 21:35:21 +02:00
|
|
|
commandInput,
|
2023-07-31 04:19:04 +02:00
|
|
|
visible,
|
|
|
|
}, context);
|
|
|
|
}
|
|
|
|
|
2023-08-04 13:48:12 +02:00
|
|
|
const container = containerRef.current;
|
|
|
|
if (container) {
|
|
|
|
const selectedItem = container.children[activeIndex.value];
|
|
|
|
if (selectedItem) {
|
|
|
|
const itemPosition = selectedItem.getBoundingClientRect();
|
|
|
|
const containerPosition = container.getBoundingClientRect();
|
|
|
|
|
|
|
|
// Check if the selected item is above the visible area
|
|
|
|
if (itemPosition.top < containerPosition.top) {
|
|
|
|
container.scrollTop -= containerPosition.top - itemPosition.top;
|
|
|
|
} // Check if the selected item is below the visible area
|
|
|
|
else if (itemPosition.bottom > containerPosition.bottom) {
|
|
|
|
container.scrollTop += itemPosition.bottom - containerPosition.bottom;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-31 04:19:04 +02:00
|
|
|
useEventListener("keydown", (ev: KeyboardEvent) => {
|
2023-08-06 00:33:06 +02:00
|
|
|
if (ev.key === "k") {
|
|
|
|
if (ev?.target?.nodeName == "INPUT") {
|
|
|
|
return;
|
|
|
|
}
|
2023-08-06 18:06:09 +02:00
|
|
|
|
2023-08-06 00:33:06 +02:00
|
|
|
if (!visible.value) {
|
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
|
|
|
}
|
|
|
|
|
2023-07-31 04:19:04 +02:00
|
|
|
visible.value = !visible.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ev.key === "ArrowDown") {
|
|
|
|
const index = activeIndex.value;
|
|
|
|
if (index + 1 >= entries.length) {
|
|
|
|
activeIndex.value = 0;
|
|
|
|
} else {
|
|
|
|
activeIndex.value += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ev.key === "Enter") {
|
|
|
|
activateEntry(entries[activeIndex.value]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ev.key === "ArrowUp") {
|
|
|
|
const index = activeIndex.value;
|
|
|
|
if (index - 1 < 0) {
|
|
|
|
activeIndex.value = entries.length - 1;
|
|
|
|
} else {
|
|
|
|
activeIndex.value -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ev.key === "Escape") {
|
|
|
|
visible.value = false;
|
|
|
|
if (input.current) {
|
|
|
|
input.current.value = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (visible.value) {
|
|
|
|
input.current?.focus();
|
|
|
|
} else {
|
|
|
|
input.current?.blur();
|
|
|
|
}
|
2023-08-08 11:18:54 +02:00
|
|
|
}, IS_BROWSER ? document?.body : undefined);
|
2023-07-31 04:19:04 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
2023-12-14 13:49:38 +01:00
|
|
|
class={`${visible.value ? "opacity-100" : "opacity-0"} pointer-events-${
|
2023-07-31 04:19:04 +02:00
|
|
|
visible.value ? "auto" : "none"
|
|
|
|
} transition grid place-items-center w-full h-full fixed top-0 left-0 z-50`}
|
2023-08-02 13:11:17 +02:00
|
|
|
style={{ background: "#141217ee" }}
|
2023-07-31 04:19:04 +02:00
|
|
|
>
|
|
|
|
<div
|
2023-08-02 13:11:17 +02:00
|
|
|
class={`relative w-1/2 max-h-64 max-w-[400px] rounded-2xl shadow-2xl nnoisy-gradient overflow-hidden after:opacity-10 bg-gray-700`}
|
2023-08-06 18:22:40 +02:00
|
|
|
style={{ background: "#2B2930", color: "#818181" }}
|
2023-07-31 04:19:04 +02:00
|
|
|
>
|
|
|
|
<div
|
2023-08-02 15:05:35 +02:00
|
|
|
class={`grid h-12 text-gray-400 ${
|
|
|
|
activeState.value !== "loading" && "border-b"
|
|
|
|
} border-gray-500 `}
|
|
|
|
style={{
|
|
|
|
gridTemplateColumns: activeState.value !== "loading"
|
2023-08-04 13:48:12 +02:00
|
|
|
? "auto 1fr"
|
2023-08-02 15:05:35 +02:00
|
|
|
: "1fr",
|
|
|
|
}}
|
2023-07-31 04:19:04 +02:00
|
|
|
>
|
2023-08-02 01:58:03 +02:00
|
|
|
{(activeState.value === "normal" || activeState.value === "input") &&
|
2023-07-31 04:19:04 +02:00
|
|
|
(
|
|
|
|
<>
|
|
|
|
<div class="grid place-items-center border-r border-gray-500">
|
2023-08-04 13:48:12 +02:00
|
|
|
<span class="text-white mx-4">
|
2023-07-31 04:19:04 +02:00
|
|
|
{activeMenu.title}
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
<input
|
|
|
|
ref={input}
|
|
|
|
onInput={() => {
|
|
|
|
commandInput.value = input.current?.value || "";
|
|
|
|
}}
|
2023-08-02 13:11:17 +02:00
|
|
|
style={{ outline: "none !important" }}
|
2023-07-31 04:19:04 +02:00
|
|
|
placeholder="Command"
|
2023-08-02 13:11:17 +02:00
|
|
|
class="bg-transparent color pl-4 outline outline outline-2 outline-offset-2"
|
2023-07-31 04:19:04 +02:00
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
{activeState.value === "loading" && (
|
2023-08-02 15:05:35 +02:00
|
|
|
<div class="py-3 px-4 flex items-center gap-2">
|
|
|
|
<icons.IconLoader2 class="animate-spin w-4 h-4" />
|
|
|
|
{loadingText.value || "Loading..."}
|
2023-07-31 04:19:04 +02:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
{activeState.value === "normal" &&
|
|
|
|
(
|
2023-08-04 13:48:12 +02:00
|
|
|
<div
|
|
|
|
class=""
|
|
|
|
style={{ maxHeight: "12rem", overflowY: "auto" }}
|
|
|
|
ref={containerRef}
|
|
|
|
>
|
2023-08-02 01:58:03 +02:00
|
|
|
{entries?.length === 0 && (
|
|
|
|
<div class="text-gray-400 px-4 py-2">
|
|
|
|
No Entries
|
|
|
|
</div>
|
|
|
|
)}
|
2023-08-01 21:35:21 +02:00
|
|
|
{entries.map(
|
2023-07-31 04:19:04 +02:00
|
|
|
(k, index) => {
|
|
|
|
return (
|
|
|
|
<KMenuEntry
|
|
|
|
entry={k}
|
|
|
|
activeIndex={activeIndex}
|
|
|
|
index={index}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|