Skip to content

Commit

Permalink
feat(web): fix hotKey bug & hidden some buttons (#602)
Browse files Browse the repository at this point in the history
  • Loading branch information
kongwy229 authored Jan 4, 2023
1 parent a75a61f commit f0bcbe6
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 72 deletions.
36 changes: 31 additions & 5 deletions web/src/hooks/useHotKey.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
import { useCallback, useEffect } from "react";
import { useCallback, useEffect, useRef } from "react";

import { formatHotKeyModifier } from "@/utils/format";
function useHotKey(
keyMap: string,
keyMap: string[],
trigger: () => void,
config: {
enabled?: boolean;
} = {
enabled: true,
},
) {
const pressKey = useRef<any>(null);
const timeout = useRef<any>(null);

const handleKeyDown = useCallback(
(event: any) => {
if (event.key === keyMap && (event.ctrlKey || event.metaKey)) {
if (event.repeat) {
return;
}
if (keyMap.indexOf(event.key) > -1 && (event.ctrlKey || event.metaKey)) {
event.preventDefault();
trigger();
pressKey.current = event.key;
if (timeout.current === null) {
timeout.current = setTimeout(() => {
// trigger the event if there is no change within 100ms
if (pressKey.current === event.key) {
trigger();
}
clearTimeout(timeout.current);
timeout.current = null;
pressKey.current = null;
}, 100);
}
}
},
[keyMap, trigger],
Expand All @@ -31,7 +49,15 @@ function useHotKey(
};
}, [config?.enabled, handleKeyDown]);

return "⌘" + keyMap;
// return shortcut key text ,if keyMap has more than two items will format [../..]
const res = `${formatHotKeyModifier()} +
${
keyMap.length > 1
? "[" + keyMap.map((item) => item.toUpperCase()).join("/") + "]"
: keyMap[0].toUpperCase()
}`;

return res;
}

export default useHotKey;
26 changes: 13 additions & 13 deletions web/src/pages/app/functions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { t } from "i18next";
import FunctionEditor from "@/components/Editor/FunctionEditor";
import FileTypeIcon, { FileType } from "@/components/FileTypeIcon";
import PanelHeader from "@/components/Panel/Header";
import { Pages } from "@/constants";

// import { Pages } from "@/constants";
import LeftPanel from "../mods/LeftPanel";
import RightPanel from "../mods/RightPanel";

Expand All @@ -22,25 +22,25 @@ import FunctionPanel from "./mods/FunctionPanel";
import useFunctionStore from "./store";

import useFunctionCache from "@/hooks/useFuncitonCache";
import useHotKey from "@/hooks/useHotKey";
import useGlobalStore from "@/pages/globalStore";
// import useHotKey from "@/hooks/useHotKey";
// import useGlobalStore from "@/pages/globalStore";

function FunctionPage() {
const globalStore = useGlobalStore((state) => state);
// const globalStore = useGlobalStore((state) => state);
const store = useFunctionStore((store) => store);
const { currentFunction, updateFunctionCode } = store;

const functionCache = useFunctionCache();

useHotKey(
"s",
async () => {
// showInfo("已开启自动保存");
},
{
enabled: globalStore.currentPageId === Pages.function,
},
);
// useHotKey(
// "s",
// async () => {
// // showInfo("已开启自动保存");
// },
// {
// enabled: globalStore.currentPageId === Pages.function,
// },
// );

return (
<>
Expand Down
28 changes: 7 additions & 21 deletions web/src/pages/app/functions/mods/DebugPannel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import {
Button,
Center,
Input,
InputGroup,
InputRightElement,
Select,
Spinner,
Tab,
Expand All @@ -21,6 +19,7 @@ import CopyText from "@/components/CopyText";
import JsonEditor from "@/components/Editor/JsonEditor";
import PanelHeader from "@/components/Panel/Header";
import { Pages } from "@/constants";
import { formatHotKeyModifier } from "@/utils/format";

import { useCompileMutation } from "../../service";
import useFunctionStore from "../../store";
Expand All @@ -47,17 +46,7 @@ export default function DebugPanel() {
const [params, setParams] = useState(JSON.stringify({ name: "test" }, null, 2));

useHotKey(
"r",
() => {
runningCode();
},
{
enabled: globalStore.currentPageId === Pages.function,
},
);

useHotKey(
"s",
["r", "s"],
() => {
runningCode();
},
Expand Down Expand Up @@ -108,7 +97,7 @@ export default function DebugPanel() {
<Tabs width="100%">
<TabList>
<Tab>接口调试</Tab>
<Tab>历史请求</Tab>
{/* <Tab>历史请求</Tab> */}
</TabList>

<TabPanels h="full">
Expand All @@ -133,12 +122,9 @@ export default function DebugPanel() {
);
})}
</Select>
<InputGroup className="ml-2">
<CopyText className="ml-2" text={getFunctionDebugUrl()}>
<Input size="sm" readOnly rounded={4} value={getFunctionDebugUrl()} />
<InputRightElement>
<CopyText text={getFunctionDebugUrl()} className="mb-2" />
</InputRightElement>
</InputGroup>
</CopyText>
<Button
style={{ borderRadius: 2 }}
size="sm"
Expand All @@ -149,7 +135,7 @@ export default function DebugPanel() {
colorScheme="green"
isLoading={isLoading}
>
{t("FunctionPanel.Debug")} (⌘ + R)
{t("FunctionPanel.Debug")} ({formatHotKeyModifier()}+R)
</Button>
</div>
<div className="mx-2 pb-2 mb-2">调用参数:</div>
Expand Down Expand Up @@ -179,7 +165,7 @@ export default function DebugPanel() {
</div>
</div>
</TabPanel>
<TabPanel padding={0}>to be continued...</TabPanel>
{/* <TabPanel padding={0}>to be continued...</TabPanel> */}
</TabPanels>
</Tabs>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,17 @@ const AddDepenceModal = () => {
const [packageList, setPackageList] = useState<TDependenceItem[]>([]);
const { isOpen, onOpen, onClose } = useDisclosure();
usePackageQuery((data) => {
const newList = (data || []).map((item: any) => {
return {
package: {
name: item.name,
version: item.spec,
},
versions: [],
};
});
const newList = (data || [])
.filter((item: any) => !item.builtin)
.map((item: any) => {
return {
package: {
name: item.name,
version: item.spec,
},
versions: [],
};
});
setPackageList(newList);
});

Expand Down Expand Up @@ -91,7 +93,7 @@ const AddDepenceModal = () => {
setIsShowChecked(false);
setName(val);
}, 1000),
[],
[setIsShowChecked, setName],
);

const initModal = () => {
Expand Down
33 changes: 20 additions & 13 deletions web/src/pages/app/functions/mods/DependecePanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,31 @@ export default function DependecyList() {
>
<div>
<FileTypeIcon type={FileType.npm} />
<span className="ml-2 w-40 inline-block whitespace-nowrap overflow-hidden overflow-ellipsis">
{packageItem?.name}
</span>
<Tooltip
label={packageItem?.builtin ? "内置依赖,不可更改" : null}
placement="top"
>
<span className="ml-2 w-40 inline-block whitespace-nowrap overflow-hidden overflow-ellipsis">
{packageItem?.name}
</span>
</Tooltip>
</div>
<div className="text-slate-500 ">
<span className="w-20 inline-block whitespace-nowrap overflow-hidden overflow-ellipsis">
{packageItem?.spec}
</span>
<span className="ml-5 hidden group-hover:inline-block">
<Tooltip label={t("Delete").toString()} placement="top">
<CloseIcon
fontSize={10}
onClick={() => {
delPackageMutation.mutate({ name: packageItem?.name });
}}
/>
</Tooltip>
</span>
{!packageItem?.builtin ? (
<span className="ml-5 hidden group-hover:inline-block">
<Tooltip label={t("Delete").toString()} placement="top">
<CloseIcon
fontSize={10}
onClick={() => {
delPackageMutation.mutate({ name: packageItem?.name });
}}
/>
</Tooltip>
</span>
) : null}
</div>
</SectionList.Item>
);
Expand Down
1 change: 1 addition & 0 deletions web/src/pages/app/functions/mods/DependecePanel/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export type TDependenceItem = {
export type TPackage = {
name: string;
spec: string;
builtin?: boolean;
};

const queryKeys = {
Expand Down
6 changes: 3 additions & 3 deletions web/src/pages/app/functions/mods/DeployButton/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export default function DeployButton() {

const updateFunctionMutation = useUpdateFunctionMutation();

useHotKey(
"p",
const hotKeyP = useHotKey(
["p"],
async () => {
onOpen();
},
Expand Down Expand Up @@ -70,7 +70,7 @@ export default function DeployButton() {
onOpen();
}}
>
{t("FunctionPanel.Deploy")} (⌘ + P)
{t("FunctionPanel.Deploy")} ({hotKeyP})
</Button>

<Modal isOpen={isOpen} onClose={onClose} size="6xl" isCentered>
Expand Down
21 changes: 14 additions & 7 deletions web/src/pages/app/storages/mods/FileList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default function FileList() {
const { getList, getFileUrl, deleteFile } = useAwsS3();
const { currentStorage, prefix, setPrefix } = useStorageStore();
const bucketName = currentStorage?.metadata.name;
// const bucketType = currentStorage?.spec.policy;

const query = useQuery(
["fileList", bucketName],
Expand All @@ -32,15 +33,16 @@ export default function FileList() {

useEffect(() => {
query.refetch();
}, [bucketName, prefix]);
}, [bucketName, prefix, query]);

const viewAppFile = (file: TFile) => {
if (file.Prefix) {
changeDirectory(file);
return;
}

window.open(getFileUrl(bucketName!, file.Key), "_blank");
// const fileUrl = bucketType === 'private' ? getFileUrl(bucketName!, file.Key) : `http://${bucketName}.oss.dev.laf.run/${file.Key}`;
const fileUrl = getFileUrl(bucketName!, file.Key);
window.open(fileUrl, "_blank");
};

const changeDirectory = (file: TFile) => {
Expand All @@ -59,7 +61,7 @@ export default function FileList() {
<HStack spacing={12}>
<span>容量: {currentStorage?.spec.storage} </span>
<span>已用: {currentStorage?.status?.capacity?.storage} </span>
<span>文件数: {currentStorage?.status?.capacity?.objectCount} </span>
{/* <span>文件数: {currentStorage?.status?.capacity?.objectCount} </span> */}
</HStack>
</PanelHeader>
<div className="px-2 pb-20 h-full overflow-auto">
Expand Down Expand Up @@ -98,12 +100,12 @@ export default function FileList() {
}}
>
{file.Prefix ? (
<a
<span
className="cursor-pointer text-blue-700 underline"
onClick={() => changeDirectory(file)}
>
{dirName[dirName.length - 2]}
</a>
</span>
) : (
fileName[fileName.length - 1]
)}
Expand All @@ -114,7 +116,12 @@ export default function FileList() {
{file.LastModified ? formatDate(file.LastModified) : "--"}
</Td>
<Td isNumeric className="flex justify-end">
<IconWrap onClick={() => viewAppFile(file)}>
<IconWrap
placement="left"
// tooltip={bucketType === 'private' && file.Key ? '临时链接,有效期15分钟' : undefined}
tooltip="临时链接,有效期15分钟"
onClick={() => viewAppFile(file)}
>
<ViewIcon fontSize={12} />
</IconWrap>
{!file.Prefix ? (
Expand Down
5 changes: 5 additions & 0 deletions web/src/utils/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ export function formatCapacity(capacity: string) {
const num = capacity.split("Gi")[0];
return parseInt(num, 10);
}

export function formatHotKeyModifier() {
const isWin = /windows/i.test(navigator.userAgent.toLowerCase());
return isWin ? "Ctrl" : "⌘";
}

0 comments on commit f0bcbe6

Please sign in to comment.