Files
kiss-translator/src/views/Action/index.js

196 lines
4.5 KiB
JavaScript
Raw Normal View History

2023-08-06 21:12:01 +08:00
import Paper from "@mui/material/Paper";
2023-08-05 23:56:16 +08:00
import Fab from "@mui/material/Fab";
2023-08-08 14:58:36 +08:00
import TranslateIcon from "@mui/icons-material/Translate";
2023-08-05 23:56:16 +08:00
import ThemeProvider from "../../hooks/Theme";
2023-08-06 21:12:01 +08:00
import Draggable from "./Draggable";
import { useEffect, useState, useMemo, useCallback } from "react";
2023-08-30 18:05:37 +08:00
import { SettingProvider } from "../../hooks/Setting";
2023-08-06 21:12:01 +08:00
import Popup from "../Popup";
2023-08-16 22:13:07 +08:00
import { debounce } from "../../libs/utils";
2023-09-02 13:14:27 +08:00
import * as shortcut from "@violentmonkey/shortcut";
import { isGm } from "../../libs/client";
2023-09-04 22:03:17 +08:00
import Header from "../Popup/Header";
2023-08-05 23:56:16 +08:00
2023-08-16 22:38:58 +08:00
export default function Action({ translator, fab }) {
2023-08-16 17:36:22 +08:00
const fabWidth = 40;
2023-08-06 21:12:01 +08:00
const [showPopup, setShowPopup] = useState(false);
const [windowSize, setWindowSize] = useState({
w: window.innerWidth,
h: window.innerHeight,
2023-08-06 21:12:01 +08:00
});
const [moved, setMoved] = useState(false);
2023-08-16 22:13:07 +08:00
const handleWindowResize = useMemo(
() =>
debounce(() => {
setWindowSize({
w: window.innerWidth,
h: window.innerHeight,
2023-08-16 22:13:07 +08:00
});
}),
[]
);
2023-08-06 21:12:01 +08:00
const handleWindowClick = (e) => {
setShowPopup(false);
};
const handleStart = useCallback(() => {
setMoved(false);
}, []);
const handleMove = useCallback(() => {
setMoved(true);
}, []);
2023-09-02 13:14:27 +08:00
useEffect(() => {
// 注册快捷键
shortcut.register("a-q", () => {
translator.toggle();
setShowPopup(false);
});
shortcut.register("a-c", () => {
translator.toggleStyle();
setShowPopup(false);
});
shortcut.register("a-k", () => {
setShowPopup((pre) => !pre);
});
return () => {
shortcut.disable();
};
}, [translator]);
useEffect(() => {
// 注册菜单
const menuCommandIds = [];
if (isGm) {
try {
menuCommandIds.push(
GM.registerMenuCommand(
"Toggle Translate",
(event) => {
translator.toggle();
setShowPopup(false);
},
"Q"
),
GM.registerMenuCommand(
"Toggle Style",
(event) => {
translator.toggleStyle();
setShowPopup(false);
},
"C"
),
GM.registerMenuCommand(
"Open Menu",
(event) => {
setShowPopup((pre) => !pre);
},
"K"
)
);
} catch (err) {
console.log("[registerMenuCommand]", err);
}
}
return () => {
if (isGm) {
try {
menuCommandIds.forEach((id) => {
GM.unregisterMenuCommand(id);
});
} catch (err) {
//
}
}
};
}, [translator]);
2023-08-06 21:12:01 +08:00
useEffect(() => {
window.addEventListener("resize", handleWindowResize);
return () => {
window.removeEventListener("resize", handleWindowResize);
2023-08-16 22:13:07 +08:00
};
}, [handleWindowResize]);
useEffect(() => {
window.addEventListener("click", handleWindowClick);
2023-09-02 13:14:27 +08:00
2023-08-16 22:13:07 +08:00
return () => {
2023-08-06 21:12:01 +08:00
window.removeEventListener("click", handleWindowClick);
};
}, []);
const popProps = useMemo(() => {
const width = Math.min(windowSize.w, 300);
2023-08-09 22:58:03 +08:00
const height = Math.min(windowSize.h, 442);
2023-08-06 21:12:01 +08:00
const left = (windowSize.w - width) / 2;
const top = (windowSize.h - height) / 2;
return {
windowSize,
width,
height,
left,
top,
};
}, [windowSize]);
const fabProps = {
windowSize,
width: fabWidth,
height: fabWidth,
2023-08-17 16:22:04 +08:00
left: fab.x ?? 0,
2023-08-16 22:38:58 +08:00
top: fab.y ?? windowSize.h / 2,
2023-08-06 21:12:01 +08:00
};
2023-08-05 23:56:16 +08:00
return (
2023-08-30 18:05:37 +08:00
<SettingProvider>
2023-08-06 21:12:01 +08:00
<ThemeProvider>
2023-08-08 14:58:36 +08:00
<Draggable
key="pop"
{...popProps}
show={showPopup}
onStart={handleStart}
onMove={handleMove}
handler={
<Paper style={{ cursor: "move" }} elevation={3}>
2023-09-04 22:03:17 +08:00
<Header setShowPopup={setShowPopup} />
2023-08-06 21:12:01 +08:00
</Paper>
2023-08-08 14:58:36 +08:00
}
>
<Paper>
2023-08-21 14:03:39 +08:00
{showPopup && (
<Popup setShowPopup={setShowPopup} translator={translator} />
)}
2023-08-08 14:58:36 +08:00
</Paper>
</Draggable>
<Draggable
key="fab"
2023-08-16 17:36:22 +08:00
snapEdge
2023-08-08 14:58:36 +08:00
{...fabProps}
show={!showPopup}
onStart={handleStart}
onMove={handleMove}
handler={
<Fab
2023-08-16 17:36:22 +08:00
size="small"
2023-08-08 14:58:36 +08:00
color="primary"
onClick={(e) => {
if (!moved) {
setShowPopup((pre) => !pre);
}
}}
>
<TranslateIcon />
</Fab>
}
/>
2023-08-06 21:12:01 +08:00
</ThemeProvider>
2023-08-30 18:05:37 +08:00
</SettingProvider>
2023-08-05 23:56:16 +08:00
);
}