-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-29: Improve Performance and fix some inconsistencies
- Loading branch information
Showing
7 changed files
with
244 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { ContentCopy, ContentPaste, Gif } from '@mui/icons-material'; | ||
import { Box, Divider, ListItemIcon, ListItemText, MenuItem, MenuList, Paper, Typography } from '@mui/material'; | ||
import React, { RefObject, useEffect, useRef, useState } from 'react'; | ||
import ContextMenuOptions from './ContextMenuOptions'; | ||
|
||
interface ContextMenuProps { | ||
options: ContextMenuOptions[]; | ||
element: RefObject<HTMLElement>; | ||
} | ||
|
||
const ContextMenu: React.FC<ContextMenuProps> = React.memo(({ options, element }) => { | ||
const constexMenuMaxLength = 300; | ||
|
||
const contextRef = useRef<HTMLDivElement>(null); | ||
const [contextMenuInfo, setContextMenuInfo] = useState({ | ||
posX: 0, | ||
posY: 0, | ||
selectedText: "", | ||
selectedElement: null as HTMLElement | null, | ||
show: false | ||
}); | ||
|
||
const handleElementClick = (event: React.MouseEvent<HTMLElement>, handler: (event: React.MouseEvent<HTMLElement>) => void) => { | ||
event.preventDefault(); | ||
handler(event); | ||
setContextMenuInfo({ ...contextMenuInfo, show: false }); | ||
}; | ||
|
||
|
||
useEffect(() => { | ||
const contextMenuEventHandler = (e: MouseEvent) => { | ||
e.preventDefault(); | ||
const selectedText = window.getSelection()?.toString(); | ||
const selectedElement = e.target as HTMLElement; | ||
setContextMenuInfo({ posX: e.pageX, posY: e.pageY, selectedText: selectedText ?? "", selectedElement, show: true }); | ||
} | ||
|
||
const offClickHandler = (event: MouseEvent) => { | ||
if (contextRef.current && !contextRef.current.contains(event.target as Node)) { | ||
setContextMenuInfo({ ...contextMenuInfo, show: false }) | ||
} | ||
} | ||
|
||
element.current?.addEventListener('contextmenu', contextMenuEventHandler); | ||
document.addEventListener('click', offClickHandler); | ||
return () => { | ||
element.current?.removeEventListener('contextmenu', contextMenuEventHandler); | ||
document.removeEventListener('click', offClickHandler); | ||
} | ||
}, [contextRef]); | ||
|
||
let { posX: left, posY: top, selectedText, selectedElement, show } = contextMenuInfo; | ||
|
||
return ( | ||
<Box | ||
ref={contextRef} | ||
style={{ | ||
position: 'absolute', | ||
top: `${top > window.innerHeight - window.innerHeight * 0.1 ? top - window.innerHeight * 0.1 : top}px`, | ||
left: `${left > window.innerWidth - constexMenuMaxLength ? left - constexMenuMaxLength : left}px`, | ||
display: show ? 'block' : 'none', | ||
}}> | ||
<Paper sx={{ width: constexMenuMaxLength }}> | ||
<MenuList> | ||
{options.map((option, index) => ( | ||
<MenuItem key={index} onClick={(e) => handleElementClick(e, option.handler)}> | ||
<ListItemIcon> | ||
{option.icon} | ||
</ListItemIcon> | ||
<ListItemText sx={{ marginRight: 2 }}>{option.label}</ListItemText> | ||
<Typography variant="body2" color="text.secondary"> | ||
{option.shortcut} | ||
</Typography> | ||
</MenuItem> | ||
))} | ||
</MenuList> | ||
</Paper> | ||
</Box> | ||
); | ||
}); | ||
|
||
export default ContextMenu; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { FileCopyOutlined } from "@mui/icons-material"; | ||
import { invoke } from "@tauri-apps/api/core"; | ||
|
||
export default interface ContextMenuOptions { | ||
icon: React.ReactNode; | ||
label: string; | ||
shortcut: string; | ||
handler: (event: React.MouseEvent<HTMLElement>) => void; | ||
} | ||
|
||
export const paste = { | ||
icon: <FileCopyOutlined />, | ||
label: "Paste", | ||
shortcut: "Ctrl+V", | ||
handler: (event: React.MouseEvent<HTMLElement>) => { | ||
event.preventDefault(); | ||
navigator.clipboard.readText().then(clipText => { | ||
console.log(clipText); | ||
}); | ||
} | ||
}; | ||
|
||
export const copy = { | ||
icon: <FileCopyOutlined />, | ||
label: "Copy", | ||
shortcut: "Ctrl+C", | ||
handler: (event: React.MouseEvent<HTMLElement>) => { | ||
event.preventDefault(); | ||
const selectedElement = event.target as HTMLElement; | ||
copyElement(selectedElement); | ||
} | ||
}; | ||
|
||
export const showDeveloperTools = { | ||
icon: <FileCopyOutlined />, | ||
label: "Developer Tools", | ||
shortcut: "Ctrl+Shift+I", | ||
handler: (event: React.MouseEvent<HTMLElement>) => { | ||
event.preventDefault(); | ||
invoke("dev_tools"); | ||
} | ||
}; | ||
|
||
|
||
async function copyElement(element: HTMLElement) { | ||
let clipboardItemData = {}; | ||
|
||
if (element instanceof HTMLImageElement) { | ||
const response = await fetch(element.src); | ||
const blob = await response.blob(); | ||
clipboardItemData = { | ||
'image/png': blob | ||
}; | ||
} else if (element instanceof HTMLVideoElement) { | ||
const response = await fetch(element.src); | ||
const blob = await response.blob(); | ||
clipboardItemData = { | ||
'video/mp4': blob | ||
}; | ||
} else if (element instanceof HTMLParagraphElement || element instanceof HTMLSpanElement) { | ||
clipboardItemData = { | ||
'text/plain': new Blob([element.innerText], { type: 'text/plain' }) | ||
}; | ||
} | ||
|
||
const clipboardItem = new ClipboardItem(clipboardItemData); | ||
await navigator.clipboard.write([clipboardItem]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Box, CircularProgress, Container, Divider, Typography, } from "@mui/material"; | ||
import { useDispatch } from "react-redux"; | ||
import './styles/Profile.css' | ||
import { useTranslation } from "react-i18next"; | ||
import UploadBox from "../UploadBox"; | ||
import { useState } from "react"; | ||
|
||
|
||
function AppearanceSettings() { | ||
const dispatch = useDispatch(); | ||
const [t] = useTranslation(); | ||
|
||
let [loading, setLoading] = useState(false); | ||
|
||
function displayLoadingText(text: string) { | ||
if (loading) { | ||
return (<CircularProgress />) | ||
} else { | ||
return (<Typography>{text}</Typography>) | ||
} | ||
} | ||
|
||
return ( | ||
<Box> | ||
<Container className="settingsContainer"> | ||
<Typography variant="h4">{t("Appearance", { ns: "appearance" })}</Typography> | ||
<Divider sx={{ marginBottom: 5 }} /> | ||
<Box sx={{ | ||
display: 'flex', | ||
flexDirection: 'row', | ||
alignContent: 'center', | ||
maxWidth: '100%' | ||
}}> | ||
<UploadBox onUpload={(path) => showUploadBox(path, ImageType.Background, 3 / 1, "rect")}>{displayLoadingText(t("Background Image", { ns: "appearance" }))}</UploadBox> | ||
</Box> | ||
</Container> | ||
</Box > | ||
) | ||
} | ||
|
||
export default AppearanceSettings; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters