From ba54d70421ad257f0ba31cbc2d5d5a5a72030d02 Mon Sep 17 00:00:00 2001 From: "Tom (plebeius.eth)" Date: Thu, 31 Oct 2024 12:55:38 +0100 Subject: [PATCH] feat(interface settings): add 'Fit expanded images to screen' setting --- .../comment-media/comment-media.module.css | 11 ++++++++++ .../comment-media/comment-media.tsx | 8 +++++-- .../interface-settings/interface-settings.tsx | 7 +++---- src/stores/use-expanded-media-store.ts | 21 +++++++++++++++++++ 4 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 src/stores/use-expanded-media-store.ts diff --git a/src/components/comment-media/comment-media.module.css b/src/components/comment-media/comment-media.module.css index 67287ce1..eeb2f756 100644 --- a/src/components/comment-media/comment-media.module.css +++ b/src/components/comment-media/comment-media.module.css @@ -149,4 +149,15 @@ .content { line-height: 0; /* Ensure no extra space due to line-height */ } +} + +.fitToScreen { + max-width: 100vw; + max-height: 100vh; +} + +.fitToScreen img, +.fitToScreen video { + max-width: 100%; + max-height: 100vh; } \ No newline at end of file diff --git a/src/components/comment-media/comment-media.tsx b/src/components/comment-media/comment-media.tsx index 96464484..92db08a4 100644 --- a/src/components/comment-media/comment-media.tsx +++ b/src/components/comment-media/comment-media.tsx @@ -1,11 +1,12 @@ import React, { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { Comment } from '@plebbit/plebbit-react-hooks'; -import styles from './comment-media.module.css'; import { CommentMediaInfo, getDisplayMediaInfoType, getHasThumbnail, getMediaDimensions } from '../../lib/utils/media-utils'; import { getHostname } from '../../lib/utils/url-utils'; +import useExpandedMediaStore from '../../stores/use-expanded-media-store'; import useFetchGifFirstFrame from '../../hooks/use-fetch-gif-first-frame'; import useIsMobile from '../../hooks/use-is-mobile'; +import styles from './comment-media.module.css'; import Embed, { canEmbed } from '../embed'; interface MediaProps { @@ -108,7 +109,10 @@ const Media = ({ commentMediaInfo, isReply, setShowThumbnail }: MediaProps) => { const { t } = useTranslation(); const { thumbnail, type, url } = commentMediaInfo || {}; const isMobile = useIsMobile(); - const mediaClass = isMobile ? styles.mediaMobile : isReply ? styles.mediaDesktopReply : styles.mediaDesktopOp; + const { fitExpandedImagesToScreen } = useExpandedMediaStore(); + const mediaClass = `${isMobile ? styles.mediaMobile : isReply ? styles.mediaDesktopReply : styles.mediaDesktopOp} ${ + fitExpandedImagesToScreen ? styles.fitToScreen : '' + }`; const mediaDimensions = getMediaDimensions(commentMediaInfo); return ( diff --git a/src/components/settings-modal/interface-settings/interface-settings.tsx b/src/components/settings-modal/interface-settings/interface-settings.tsx index 30181ba5..43bc2d08 100644 --- a/src/components/settings-modal/interface-settings/interface-settings.tsx +++ b/src/components/settings-modal/interface-settings/interface-settings.tsx @@ -7,6 +7,7 @@ import styles from './interface-settings.module.css'; import _ from 'lodash'; import useInterfaceSettingsStore from '../../../stores/use-interface-settings-store'; import useCatalogFiltersStore from '../../../stores/use-catalog-filters-store'; +import useExpandedMediaStore from '../../../stores/use-expanded-media-store'; const commitRef = process.env.REACT_APP_COMMIT_REF; const isElectron = window.isElectron === true; @@ -114,6 +115,7 @@ const InterfaceSettings = () => { const { hideAvatars, setHideAvatars } = useAvatarVisibilityStore(); const { hideGoreBoards, setHideGoreBoards, hideAdultBoards, setHideAdultBoards, hideThreadsWithoutImages, setHideThreadsWithoutImages } = useInterfaceSettingsStore(); const { setShowTextOnlyThreads } = useCatalogFiltersStore(); + const { fitExpandedImagesToScreen, setFitExpandedImagesToScreen } = useExpandedMediaStore(); const handleHideAvatarsChange = (e: React.ChangeEvent) => { setHideAvatars(e.target.checked); @@ -169,10 +171,7 @@ const InterfaceSettings = () => {
{_.capitalize(t('fit_expanded_images_to_screen_tip'))}
diff --git a/src/stores/use-expanded-media-store.ts b/src/stores/use-expanded-media-store.ts new file mode 100644 index 00000000..95231e6a --- /dev/null +++ b/src/stores/use-expanded-media-store.ts @@ -0,0 +1,21 @@ +import { create } from 'zustand'; +import { persist } from 'zustand/middleware'; + +interface ExpandedMediaState { + fitExpandedImagesToScreen: boolean; + setFitExpandedImagesToScreen: (fit: boolean) => void; +} + +const useExpandedMediaStore = create()( + persist( + (set) => ({ + fitExpandedImagesToScreen: false, + setFitExpandedImagesToScreen: (fit) => set({ fitExpandedImagesToScreen: fit }), + }), + { + name: 'expanded-media-store', + }, + ), +); + +export default useExpandedMediaStore;