ReverseImageSearch: add support for image modal

This commit is contained in:
Vendicated 2024-01-05 23:51:49 +01:00
parent 4d198e46bc
commit 13961a4ba5
No known key found for this signature in database
GPG key ID: D66986BAF75ECF18

View file

@ -36,17 +36,8 @@ function search(src: string, engine: string) {
open(engine + encodeURIComponent(src), "_blank");
}
const imageContextMenuPatch: NavContextMenuPatchCallback = (children, props) => () => {
if (!props) return;
const { reverseImageSearchType, itemHref, itemSrc } = props;
if (!reverseImageSearchType || reverseImageSearchType !== "img") return;
const src = itemHref ?? itemSrc;
const group = findGroupChildrenByChildId("copy-link", children);
if (group) {
group.push((
function makeSearchItem(src: string) {
return (
<Menu.MenuItem
label="Search Image"
key="search-image"
@ -90,8 +81,23 @@ const imageContextMenuPatch: NavContextMenuPatchCallback = (children, props) =>
action={() => Object.values(Engines).forEach(e => search(src, e))}
/>
</Menu.MenuItem>
));
}
);
}
const messageContextMenuPatch: NavContextMenuPatchCallback = (children, props) => () => {
if (props?.reverseImageSearchType !== "img") return;
const src = props.itemHref ?? props.itemSrc;
const group = findGroupChildrenByChildId("copy-link", children);
group?.push(makeSearchItem(src));
};
const imageContextMenuPatch: NavContextMenuPatchCallback = (children, props) => () => {
if (!props?.src) return;
const group = findGroupChildrenByChildId("copy-native-link", children) ?? children;
group.push(makeSearchItem(props.src));
};
export default definePlugin({
@ -111,10 +117,12 @@ export default definePlugin({
],
start() {
addContextMenuPatch("message", imageContextMenuPatch);
addContextMenuPatch("message", messageContextMenuPatch);
addContextMenuPatch("image-context", imageContextMenuPatch);
},
stop() {
removeContextMenuPatch("message", imageContextMenuPatch);
removeContextMenuPatch("message", messageContextMenuPatch);
removeContextMenuPatch("image-context", imageContextMenuPatch);
}
});