From ef67a98c20d3852e56f6890efe71eab90d7537c9 Mon Sep 17 00:00:00 2001 From: pelya Date: Mon, 18 Dec 2023 15:13:16 +0200 Subject: [PATCH 1/2] Save current image from the gallery view by pressing Ctrl-S --- ts/components/Lightbox.tsx | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/ts/components/Lightbox.tsx b/ts/components/Lightbox.tsx index f6c63c38d86..465f8665681 100644 --- a/ts/components/Lightbox.tsx +++ b/ts/components/Lightbox.tsx @@ -161,7 +161,7 @@ export function Lightbox({ }, [setVideoTime, videoElement]); const handleSave = ( - event: React.MouseEvent + event: KeyboardEvent | React.MouseEvent ) => { if (isViewOnce) { return; @@ -193,6 +193,8 @@ export function Lightbox({ const onKeyDown = useCallback( (event: KeyboardEvent) => { + const isMacOS = (window.platform === 'darwin'); + switch (event.key) { case 'Escape': { closeLightbox(); @@ -211,10 +213,16 @@ export function Lightbox({ onNext(event); break; + case 's': + if (isMacOS ? event.metaKey : event.ctrlKey) { + handleSave(event); + } + break; + default: } }, - [closeLightbox, onNext, onPrevious] + [closeLightbox, onNext, onPrevious, handleSave] ); const onClose = (event: React.MouseEvent) => { From 2ff0d87309fc7f02a385831a5c7701ede629caa5 Mon Sep 17 00:00:00 2001 From: trevor-signal Date: Fri, 22 Dec 2023 15:51:27 -0500 Subject: [PATCH 2/2] Use utility & fixup dependency array --- ts/components/Lightbox.tsx | 34 ++++++++++++++++--------------- ts/hooks/useKeyboardShortcuts.tsx | 2 +- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/ts/components/Lightbox.tsx b/ts/components/Lightbox.tsx index 465f8665681..9f22ae2d019 100644 --- a/ts/components/Lightbox.tsx +++ b/ts/components/Lightbox.tsx @@ -26,6 +26,7 @@ import { isGIF } from '../types/Attachment'; import { useRestoreFocus } from '../hooks/useRestoreFocus'; import { usePrevious } from '../hooks/usePrevious'; import { arrow } from '../util/keyboard'; +import { isCmdOrCtrl } from '../hooks/useKeyboardShortcuts'; export type PropsType = { children?: ReactNode; @@ -160,21 +161,24 @@ export function Lightbox({ setVideoTime(videoElement.currentTime); }, [setVideoTime, videoElement]); - const handleSave = ( - event: KeyboardEvent | React.MouseEvent - ) => { - if (isViewOnce) { - return; - } + const handleSave = useCallback( + ( + event: KeyboardEvent | React.MouseEvent + ) => { + if (isViewOnce) { + return; + } - event.stopPropagation(); - event.preventDefault(); + event.stopPropagation(); + event.preventDefault(); - const mediaItem = media[selectedIndex]; - const { attachment, message, index } = mediaItem; + const mediaItem = media[selectedIndex]; + const { attachment, message, index } = mediaItem; - saveAttachment(attachment, message.sent_at, index + 1); - }; + saveAttachment(attachment, message.sent_at, index + 1); + }, + [isViewOnce, media, saveAttachment, selectedIndex] + ); const handleForward = ( event: React.MouseEvent @@ -193,8 +197,6 @@ export function Lightbox({ const onKeyDown = useCallback( (event: KeyboardEvent) => { - const isMacOS = (window.platform === 'darwin'); - switch (event.key) { case 'Escape': { closeLightbox(); @@ -214,8 +216,8 @@ export function Lightbox({ break; case 's': - if (isMacOS ? event.metaKey : event.ctrlKey) { - handleSave(event); + if (isCmdOrCtrl(event)) { + handleSave(event); } break; diff --git a/ts/hooks/useKeyboardShortcuts.tsx b/ts/hooks/useKeyboardShortcuts.tsx index f32fb69421d..5c81e26cd65 100644 --- a/ts/hooks/useKeyboardShortcuts.tsx +++ b/ts/hooks/useKeyboardShortcuts.tsx @@ -14,7 +14,7 @@ import type { ContextMenuTriggerType } from '../components/conversation/MessageC type KeyboardShortcutHandlerType = (ev: KeyboardEvent) => boolean; -function isCmdOrCtrl(ev: KeyboardEvent): boolean { +export function isCmdOrCtrl(ev: KeyboardEvent): boolean { const { ctrlKey, metaKey } = ev; const commandKey = get(window, 'platform') === 'darwin' && metaKey; const controlKey = get(window, 'platform') !== 'darwin' && ctrlKey;