mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-20 00:51:17 -08:00
Add gif functionality using klipy
This commit is contained in:
1 parent
38b64b3ef0
commit
ab23f571e6
17 files changed
+695
-59
No files matched your search
@@ -0,0 +1,5 @@
|
||||
---
|
||||
default: minor
|
||||
---
|
||||
|
||||
# Added a GIF search functionality
|
||||
@@ -44,5 +44,10 @@
|
||||
"hashRouter": {
|
||||
"enabled": false,
|
||||
"basename": "/"
|
||||
},
|
||||
|
||||
"gifs": {
|
||||
"proxyUrl": "gifs.sable.moe",
|
||||
"klipyApiKey": "SET_YOUR_TOKEN_HERE"
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ import type {
|
||||
ReactNode,
|
||||
RefObject,
|
||||
} from 'react';
|
||||
import { useCallback, useEffect, useMemo, useRef } from 'react';
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
||||
import { Box, config, Scroll } from 'folds';
|
||||
import { ClockCounterClockwise } from '$components/icons/phosphor';
|
||||
import FocusTrap from 'focus-trap-react';
|
||||
@@ -44,10 +44,12 @@ import {
|
||||
SidebarDivider,
|
||||
Sidebar,
|
||||
NoStickerPacks,
|
||||
GifStatus,
|
||||
createPreviewDataAtom,
|
||||
Preview,
|
||||
EmojiItem,
|
||||
StickerItem,
|
||||
GifItem,
|
||||
CustomEmojiItem,
|
||||
ImageGroupIcon,
|
||||
GroupIcon,
|
||||
@@ -55,7 +57,10 @@ import {
|
||||
EmojiGroup,
|
||||
EmojiBoardLayout,
|
||||
} from './components';
|
||||
import { EmojiBoardTab, EmojiType } from './types';
|
||||
import { EmojiBoardTab, EmojiType, GifData } from './types';
|
||||
import { GitDiffIcon } from '@phosphor-icons/react';
|
||||
import { useClientConfig } from '$hooks/useClientConfig';
|
||||
import { useFavoriteGifs } from '$hooks/useFavoriteGifs';
|
||||
|
||||
const RECENT_GROUP_ID = 'recent_group';
|
||||
const SEARCH_GROUP_ID = 'search_group';
|
||||
@@ -70,11 +75,20 @@ type StickerGroupItem = {
|
||||
name: string;
|
||||
items: Array<PackImageReader>;
|
||||
};
|
||||
type GifGroupItem = {
|
||||
id: string;
|
||||
name: string;
|
||||
items: GifData[];
|
||||
};
|
||||
|
||||
const useGroups = (
|
||||
tab: EmojiBoardTab,
|
||||
imagePacks: ImagePack[]
|
||||
): [EmojiGroupItem[], StickerGroupItem[]] => {
|
||||
imagePacks: ImagePack[],
|
||||
data: {
|
||||
gifs: GifData[];
|
||||
favorites: GifData[];
|
||||
}
|
||||
): [EmojiGroupItem[], StickerGroupItem[], GifGroupItem[]] => {
|
||||
const mx = useMatrixClient();
|
||||
|
||||
const recentEmojis = useRecentEmoji(mx, 21);
|
||||
@@ -134,17 +148,64 @@ const useGroups = (
|
||||
return g;
|
||||
}, [mx, imagePacks, tab]);
|
||||
|
||||
return [emojiGroupItems, stickerGroupItems];
|
||||
const gifGroupItems = useMemo(() => {
|
||||
if (tab !== EmojiBoardTab.Gif) return [];
|
||||
return [
|
||||
{
|
||||
id: 'gif_group',
|
||||
name: 'GIFs',
|
||||
items: data.gifs,
|
||||
},
|
||||
];
|
||||
}, [tab, data]);
|
||||
|
||||
return [emojiGroupItems, stickerGroupItems, gifGroupItems];
|
||||
};
|
||||
|
||||
const useItemRenderer = (tab: EmojiBoardTab, saveStickerEmojiBandwidth: boolean) => {
|
||||
const mx = useMatrixClient();
|
||||
const useAuthentication = useMediaAuthentication();
|
||||
|
||||
const renderItem = (emoji: IEmoji | PackImageReader, index: number) => {
|
||||
if ('unicode' in emoji) {
|
||||
return <EmojiItem key={emoji.unicode + index} emoji={emoji} />;
|
||||
const renderItem = (item: IEmoji | PackImageReader | GifData, index: number) => {
|
||||
if (tab === EmojiBoardTab.Gif) {
|
||||
const gif = item as GifData;
|
||||
|
||||
let initialGifUrl = gif.preview_url ?? gif.url;
|
||||
let gifUrl = initialGifUrl.startsWith('mxc://')
|
||||
? (mxcUrlToHttp(mx, initialGifUrl, useAuthentication) ?? '')
|
||||
: initialGifUrl;
|
||||
const aspectRatio =
|
||||
gif.width && gif.height && gif.width > 0 && gif.height > 0
|
||||
? `${gif.width} / ${gif.height}`
|
||||
: '1 / 1';
|
||||
|
||||
return (
|
||||
<GifItem
|
||||
key={gif.id + index}
|
||||
label={gif.title}
|
||||
type={EmojiType.Gif}
|
||||
data={gif.url}
|
||||
shortcode={gif.title}
|
||||
gif={gif}
|
||||
style={{ aspectRatio }}
|
||||
>
|
||||
<img
|
||||
loading="lazy"
|
||||
alt=""
|
||||
aria-hidden
|
||||
src={gifUrl}
|
||||
style={{ display: 'block', width: '100%', height: '100%', objectFit: 'cover' }}
|
||||
/>
|
||||
</GifItem>
|
||||
);
|
||||
}
|
||||
|
||||
if ('unicode' in item) {
|
||||
return <EmojiItem key={item.unicode + index} emoji={item} />;
|
||||
}
|
||||
|
||||
const emoji = item as PackImageReader;
|
||||
|
||||
if (tab === EmojiBoardTab.Sticker) {
|
||||
return (
|
||||
<StickerItem
|
||||
@@ -384,11 +445,14 @@ type EmojiBoardProps = {
|
||||
onEmojiSelect?: (unicode: string, shortcode: string) => void;
|
||||
onCustomEmojiSelect?: (mxc: string, shortcode: string) => void;
|
||||
onStickerSelect?: (mxc: string, shortcode: string, label: string) => void;
|
||||
onGifSelect?: (gif: GifData) => void;
|
||||
allowTextCustomEmoji?: boolean;
|
||||
addToRecentEmoji?: boolean;
|
||||
isFullWidth?: boolean;
|
||||
};
|
||||
|
||||
const getGifName = (v: GifData) => v.title;
|
||||
|
||||
export function EmojiBoard({
|
||||
tab = EmojiBoardTab.Emoji,
|
||||
onTabChange,
|
||||
@@ -398,6 +462,7 @@ export function EmojiBoard({
|
||||
onEmojiSelect,
|
||||
onCustomEmojiSelect,
|
||||
onStickerSelect,
|
||||
onGifSelect,
|
||||
allowTextCustomEmoji,
|
||||
addToRecentEmoji = true,
|
||||
isFullWidth,
|
||||
@@ -406,18 +471,17 @@ export function EmojiBoard({
|
||||
const [saveStickerEmojiBandwidth] = useSetting(settingsAtom, 'saveStickerEmojiBandwidth');
|
||||
|
||||
const emojiTab = tab === EmojiBoardTab.Emoji;
|
||||
const gifTab = tab === EmojiBoardTab.Gif;
|
||||
const usage = emojiTab ? ImageUsage.Emoticon : ImageUsage.Sticker;
|
||||
|
||||
const previewAtom = useMemo(
|
||||
() => createPreviewDataAtom(emojiTab ? DefaultEmojiPreview : undefined),
|
||||
[emojiTab]
|
||||
() => createPreviewDataAtom(tab === EmojiBoardTab.Emoji ? DefaultEmojiPreview : undefined),
|
||||
[tab]
|
||||
);
|
||||
const activeGroupIdAtom = useMemo(() => atom<string | undefined>(undefined), []);
|
||||
const setActiveGroupId = useSetAtom(activeGroupIdAtom);
|
||||
const imagePacks = useRelevantImagePacks(usage, imagePackRooms);
|
||||
const [emojiGroupItems, stickerGroupItems] = useGroups(tab, imagePacks);
|
||||
const groups = emojiTab ? emojiGroupItems : stickerGroupItems;
|
||||
const renderItem = useItemRenderer(tab, saveStickerEmojiBandwidth);
|
||||
const favoriteGifs = useFavoriteGifs().gifs as GifData[];
|
||||
|
||||
const searchList = useMemo(() => {
|
||||
let list: Array<PackImageReader | IEmoji> = [];
|
||||
@@ -426,22 +490,165 @@ export function EmojiBoard({
|
||||
return list;
|
||||
}, [emojiTab, usage, imagePacks]);
|
||||
|
||||
const [result, search, resetSearch] = useAsyncSearch(
|
||||
const [emojiResult, emojiSearch, resetEmojiSearch] = useAsyncSearch(
|
||||
searchList,
|
||||
getEmoticonSearchStr,
|
||||
SEARCH_OPTIONS
|
||||
);
|
||||
|
||||
const searchedItems = result?.items.slice(0, 100);
|
||||
const [gifResult, gifSearch, resetGifSearch] = useAsyncSearch(
|
||||
favoriteGifs,
|
||||
getGifName,
|
||||
SEARCH_OPTIONS
|
||||
);
|
||||
|
||||
const searchedItems = emojiResult?.items.slice(0, 100);
|
||||
const searchedGifItems = gifResult?.items.slice(0, 100) ?? favoriteGifs;
|
||||
|
||||
function useGifSearch() {
|
||||
const [gifs, setGifs] = useState<{
|
||||
gifs: GifData[];
|
||||
favorites: GifData[];
|
||||
}>({
|
||||
gifs: [],
|
||||
favorites: favoriteGifs,
|
||||
});
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const clientConfig = useClientConfig();
|
||||
const klipyApiKey = clientConfig.gifs?.klipyApiKey ?? '';
|
||||
|
||||
const parseKlipyResult = useCallback((klipyResult: any): GifData => {
|
||||
const SIZE_LIMIT = 3 * 1024 * 1024; // 3MB
|
||||
|
||||
const formats = klipyResult.file || {};
|
||||
const preview = formats.xs.gif || formats.sm.gif || formats.md.gif;
|
||||
|
||||
// Start with full resolution GIF
|
||||
let fullRes = formats.hd.gif;
|
||||
// If full res is too large and medium exists, use medium instead
|
||||
if (fullRes && fullRes.size > SIZE_LIMIT && formats.md) {
|
||||
fullRes = formats.md.gif;
|
||||
}
|
||||
|
||||
// Fallback if no suitable format found
|
||||
if (!fullRes) {
|
||||
fullRes = formats.md || preview;
|
||||
}
|
||||
|
||||
// Get dimensions from the selected full resolution format
|
||||
const width = fullRes?.width || preview?.width || 0;
|
||||
const height = fullRes?.height || preview?.height || 0;
|
||||
|
||||
return {
|
||||
id: klipyResult.id,
|
||||
title: klipyResult.title || 'GIF',
|
||||
url: fullRes?.url || '',
|
||||
preview_url: preview?.url || fullRes?.url || '',
|
||||
width,
|
||||
height,
|
||||
};
|
||||
}, []);
|
||||
|
||||
const searchGifs = useCallback(
|
||||
async (query: string) => {
|
||||
const trimmedQuery = query.trim();
|
||||
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
|
||||
gifSearch(trimmedQuery);
|
||||
|
||||
try {
|
||||
const url = new URL('https://api.klipy.com');
|
||||
url.pathname = `/api/v1/${klipyApiKey}/gifs/search`;
|
||||
url.searchParams.set('q', trimmedQuery);
|
||||
url.searchParams.set('per_page', '50'); // TODO: infinite scroll?
|
||||
|
||||
const response = await fetch(url.toString());
|
||||
|
||||
if (response.status === 200) {
|
||||
const data = await response.json();
|
||||
const results = data.data.data as any[] | undefined;
|
||||
|
||||
if (results) {
|
||||
const gifData: GifData[] = results.map(parseKlipyResult);
|
||||
setGifs((old) => ({
|
||||
...old,
|
||||
gifs: gifData,
|
||||
}));
|
||||
} else {
|
||||
setGifs((old) => ({
|
||||
...old,
|
||||
gifs: [],
|
||||
}));
|
||||
}
|
||||
} else {
|
||||
throw new Error(`HTTP ${response.status}`);
|
||||
}
|
||||
} catch {
|
||||
setError('Failed to search GIFs');
|
||||
setGifs((old) => ({
|
||||
...old,
|
||||
gifs: [],
|
||||
}));
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
},
|
||||
[parseKlipyResult]
|
||||
);
|
||||
|
||||
return { gifs, loading, error, searchGifs };
|
||||
}
|
||||
|
||||
const { gifs, loading: gifsLoading, error: gifsError, searchGifs } = useGifSearch();
|
||||
const [emojiGroupItems, stickerGroupItems, gifGroupItems] = useGroups(tab, imagePacks, gifs);
|
||||
const [showFavoritesOnly, setShowFavoritesOnly] = useState(true);
|
||||
const groupsByTab = {
|
||||
[EmojiBoardTab.Emoji]: emojiGroupItems,
|
||||
[EmojiBoardTab.Sticker]: stickerGroupItems,
|
||||
[EmojiBoardTab.Gif]:
|
||||
showFavoritesOnly && gifs.favorites.length > 0
|
||||
? [
|
||||
{
|
||||
id: 'favorites_group',
|
||||
name: 'Favorites',
|
||||
items: searchedGifItems,
|
||||
},
|
||||
]
|
||||
: searchedGifItems.length > 0
|
||||
? [
|
||||
{
|
||||
id: 'favorites_group',
|
||||
name: 'Favorites',
|
||||
items: searchedGifItems,
|
||||
},
|
||||
].concat(gifGroupItems)
|
||||
: gifGroupItems,
|
||||
};
|
||||
const groups = groupsByTab[tab];
|
||||
const renderItem = useItemRenderer(tab, saveStickerEmojiBandwidth);
|
||||
|
||||
const handleOnChange: ChangeEventHandler<HTMLInputElement> = useDebounce(
|
||||
useCallback(
|
||||
(evt) => {
|
||||
const term = evt.target.value;
|
||||
if (term) search(term);
|
||||
else resetSearch();
|
||||
if (tab === EmojiBoardTab.Gif) {
|
||||
if (term) {
|
||||
setShowFavoritesOnly(false);
|
||||
searchGifs(term);
|
||||
} else {
|
||||
setShowFavoritesOnly(true);
|
||||
resetGifSearch();
|
||||
}
|
||||
} else if (term) {
|
||||
emojiSearch(term);
|
||||
} else {
|
||||
resetEmojiSearch();
|
||||
}
|
||||
},
|
||||
[search, resetSearch]
|
||||
[emojiSearch, resetEmojiSearch, searchGifs, resetGifSearch, tab]
|
||||
),
|
||||
{ wait: 200 }
|
||||
);
|
||||
@@ -494,6 +701,11 @@ export function EmojiBoard({
|
||||
if (emojiInfo.type === EmojiType.Sticker) {
|
||||
onStickerSelect?.(emojiInfo.data, emojiInfo.shortcode, emojiInfo.label);
|
||||
}
|
||||
if (emojiInfo.type === EmojiType.Gif) {
|
||||
const gifDataStr = targetEl.getAttribute('data-gif-data');
|
||||
const gifData = gifDataStr ? JSON.parse(gifDataStr) : null;
|
||||
onGifSelect?.(gifData);
|
||||
}
|
||||
if (!evt.altKey && !evt.shiftKey) requestClose();
|
||||
};
|
||||
|
||||
@@ -518,7 +730,7 @@ export function EmojiBoard({
|
||||
const group = inViewVItem ? groups[inViewVItem?.index] : undefined;
|
||||
setActiveGroupId(group?.id);
|
||||
}
|
||||
}, [vItems, groups, setActiveGroupId, result?.query]);
|
||||
}, [vItems, groups, setActiveGroupId, emojiResult?.query, gifResult?.query]);
|
||||
|
||||
// reset scroll position on search
|
||||
useEffect(() => {
|
||||
@@ -526,7 +738,7 @@ export function EmojiBoard({
|
||||
if (scrollElement) {
|
||||
scrollElement.scrollTo({ top: 0 });
|
||||
}
|
||||
}, [result?.query]);
|
||||
}, [emojiResult?.query, gifResult?.query]);
|
||||
|
||||
// reset scroll position on tab change
|
||||
useEffect(() => {
|
||||
@@ -560,7 +772,7 @@ export function EmojiBoard({
|
||||
{onTabChange && <EmojiBoardTabs tab={tab} onTabChange={onTabChange} />}
|
||||
<SearchInput
|
||||
key={tab}
|
||||
query={result?.query}
|
||||
query={emojiResult?.query}
|
||||
onChange={handleOnChange}
|
||||
allowTextCustomEmoji={allowTextCustomEmoji}
|
||||
onTextCustomEmojiSelect={handleTextCustomEmojiSelect}
|
||||
@@ -576,12 +788,14 @@ export function EmojiBoard({
|
||||
onScrollToGroup={handleScrollToGroup}
|
||||
/>
|
||||
) : (
|
||||
<StickerSidebar
|
||||
activeGroupAtom={activeGroupIdAtom}
|
||||
packs={imagePacks}
|
||||
saveStickerEmojiBandwidth={saveStickerEmojiBandwidth}
|
||||
onScrollToGroup={handleScrollToGroup}
|
||||
/>
|
||||
!gifTab && (
|
||||
<StickerSidebar
|
||||
activeGroupAtom={activeGroupIdAtom}
|
||||
packs={imagePacks}
|
||||
saveStickerEmojiBandwidth={saveStickerEmojiBandwidth}
|
||||
onScrollToGroup={handleScrollToGroup}
|
||||
/>
|
||||
)
|
||||
)
|
||||
}
|
||||
isFullWidth={isFullWidth}
|
||||
@@ -593,7 +807,7 @@ export function EmojiBoard({
|
||||
previewAtom={previewAtom}
|
||||
onGroupItemClick={handleGroupItemClick}
|
||||
>
|
||||
{searchedItems && (
|
||||
{tab !== EmojiBoardTab.Gif && searchedItems && (
|
||||
<EmojiGroup
|
||||
id={SEARCH_GROUP_ID}
|
||||
label={searchedItems.length ? 'Search Results' : 'No Results found'}
|
||||
@@ -618,7 +832,7 @@ export function EmojiBoard({
|
||||
ref={virtualizer.measureElement}
|
||||
key={vItem.index}
|
||||
>
|
||||
<EmojiGroup key={group.id} id={group.id} label={group.name}>
|
||||
<EmojiGroup key={group.id} id={group.id} label={group.name} isGifGroup={gifTab}>
|
||||
{group.items.map(renderItem)}
|
||||
</EmojiGroup>
|
||||
</VirtualTile>
|
||||
@@ -626,9 +840,16 @@ export function EmojiBoard({
|
||||
})}
|
||||
</div>
|
||||
{tab === EmojiBoardTab.Sticker && groups.length === 0 && <NoStickerPacks />}
|
||||
{gifTab && (
|
||||
<GifStatus
|
||||
loading={gifsLoading}
|
||||
error={gifsError}
|
||||
isEmpty={groups.flatMap((v) => v.items.map((i) => 'gif')).length === 0}
|
||||
/>
|
||||
)}
|
||||
</EmojiGroupHolder>
|
||||
</Box>
|
||||
<Preview previewAtom={previewAtom} />
|
||||
{!gifTab && <Preview previewAtom={previewAtom} />}
|
||||
</EmojiBoardLayout>
|
||||
</FocusTrap>
|
||||
);
|
||||
|
||||
@@ -10,9 +10,10 @@ export const EmojiGroup = as<
|
||||
{
|
||||
id: string;
|
||||
label: string;
|
||||
isGifGroup?: boolean;
|
||||
children: ReactNode;
|
||||
}
|
||||
>(({ className, id, label, children, ...props }, ref) => (
|
||||
>(({ className, id, label, isGifGroup, children, ...props }, ref) => (
|
||||
<Box
|
||||
id={getDOMGroupId(id)}
|
||||
data-group-id={id}
|
||||
@@ -25,10 +26,17 @@ export const EmojiGroup = as<
|
||||
<Text id={`EmojiGroup-${id}-label`} as="label" className={css.EmojiGroupLabel} size="O400">
|
||||
{label}
|
||||
</Text>
|
||||
<div aria-labelledby={`EmojiGroup-${id}-label`} className={css.EmojiGroupContent}>
|
||||
<Box wrap="Wrap" justifyContent="Center">
|
||||
{children}
|
||||
</Box>
|
||||
<div
|
||||
aria-labelledby={`EmojiGroup-${id}-label`}
|
||||
className={isGifGroup ? css.GifGroupContent : css.EmojiGroupContent}
|
||||
>
|
||||
{isGifGroup ? (
|
||||
children
|
||||
) : (
|
||||
<Box wrap="Wrap" justifyContent="Center">
|
||||
{children}
|
||||
</Box>
|
||||
)}
|
||||
</div>
|
||||
</Box>
|
||||
));
|
||||
@@ -1,11 +1,16 @@
|
||||
import { Box } from 'folds';
|
||||
import { Box, color, config, Menu, MenuItem } from 'folds';
|
||||
import type { MatrixClient } from '$types/matrix-sdk';
|
||||
import type { PackImageReader } from '$plugins/custom-emoji';
|
||||
import type { IEmoji } from '$plugins/emoji';
|
||||
import { mxcUrlToHttp } from '$utils/matrix';
|
||||
import type { EmojiItemInfo } from '$components/emoji-board/types';
|
||||
import { EmojiType } from '$components/emoji-board/types';
|
||||
import { EmojiItemInfo, EmojiType, GifData } from '$components/emoji-board/types';
|
||||
import { CSSProperties, ReactNode, useEffect, useState } from 'react';
|
||||
import * as css from './styles.css';
|
||||
import { useFavoriteGifs } from '$hooks/useFavoriteGifs';
|
||||
import { Star, menuIcon } from '$components/icons/phosphor';
|
||||
import { MATRIX_SABLE_UNSTABLE_FAVORITE_GIFS } from '$unstable/prefixes';
|
||||
import { useMatrixClient } from '$hooks/useMatrixClient';
|
||||
import classNames from 'classnames';
|
||||
|
||||
const ANIMATED_MIME_TYPES = new Set(['image/gif', 'image/apng']);
|
||||
|
||||
@@ -140,3 +145,92 @@ export function StickerItem({
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
|
||||
export function GifItem({
|
||||
label,
|
||||
type,
|
||||
data,
|
||||
shortcode,
|
||||
gif,
|
||||
style,
|
||||
children,
|
||||
}: {
|
||||
label: string;
|
||||
type: EmojiType;
|
||||
data: string;
|
||||
shortcode: string;
|
||||
gif: GifData;
|
||||
style?: CSSProperties;
|
||||
children: ReactNode;
|
||||
}) {
|
||||
const [isHovered, setIsHovered] = useState(false);
|
||||
const initialFavorited = useFavoriteGifs();
|
||||
const [favoritedContent, setFavoritedContent] = useState(initialFavorited);
|
||||
const [favorited, setFavorited] = useState(
|
||||
favoritedContent.gifs.find((v) => v.url == gif?.url) != undefined
|
||||
);
|
||||
const mx = useMatrixClient();
|
||||
|
||||
useEffect(() => {
|
||||
setFavoritedContent(initialFavorited);
|
||||
}, [initialFavorited]);
|
||||
|
||||
return (
|
||||
<Box
|
||||
as="button"
|
||||
className={css.GifItem}
|
||||
type="button"
|
||||
style={style}
|
||||
alignItems="Center"
|
||||
justifyContent="Center"
|
||||
title={label}
|
||||
aria-label={`${label} gif`}
|
||||
data-emoji-type={type}
|
||||
data-emoji-data={data}
|
||||
data-emoji-shortcode={shortcode}
|
||||
data-gif-data={gif ? JSON.stringify(gif) : undefined}
|
||||
onPointerEnter={() => setIsHovered(true)}
|
||||
onPointerLeave={() => setIsHovered(false)}
|
||||
>
|
||||
{children}
|
||||
{isHovered && (
|
||||
<Box style={{ padding: config.space.S200, right: 0, top: 0, position: 'absolute' }}>
|
||||
<Menu style={{ padding: config.space.S0 }}>
|
||||
<Box>
|
||||
<MenuItem
|
||||
size="300"
|
||||
radii="0"
|
||||
fill="Soft"
|
||||
variant="Secondary"
|
||||
title={favorited ? 'Unfavorite gif' : 'Favorite gif'}
|
||||
onClick={async (e) => {
|
||||
e.preventDefault();
|
||||
if (!favorited) {
|
||||
setFavorited(true);
|
||||
await mx
|
||||
.setAccountData(MATRIX_SABLE_UNSTABLE_FAVORITE_GIFS, {
|
||||
gifs: [...favoritedContent.gifs, gif],
|
||||
})
|
||||
.catch(() => setFavorited(false));
|
||||
} else {
|
||||
setFavorited(false);
|
||||
await mx
|
||||
.setAccountData(MATRIX_SABLE_UNSTABLE_FAVORITE_GIFS, {
|
||||
gifs: favoritedContent.gifs.filter((v) => v.url != gif.url),
|
||||
})
|
||||
.catch(() => setFavorited(true));
|
||||
}
|
||||
}}
|
||||
>
|
||||
{menuIcon(Star, {
|
||||
weight: favorited ? 'fill' : 'regular',
|
||||
color: favorited ? color.Warning.MainHover : color.Surface.OnContainer,
|
||||
})}
|
||||
</MenuItem>
|
||||
</Box>
|
||||
</Menu>
|
||||
</Box>
|
||||
)}
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
import { SmileySadIcon } from '@phosphor-icons/react';
|
||||
import { Box, toRem, config, Text } from 'folds';
|
||||
|
||||
export function GifSearching() {
|
||||
return (
|
||||
<Box
|
||||
style={{ padding: `${toRem(60)} ${config.space.S500}` }}
|
||||
alignItems="Center"
|
||||
justifyContent="Center"
|
||||
direction="Column"
|
||||
gap="300"
|
||||
>
|
||||
<Text>Loading GIFs...</Text>
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
|
||||
export function GifSearchError({ error }: { error: string }) {
|
||||
return (
|
||||
<Box
|
||||
style={{ padding: `${toRem(60)} ${config.space.S500}` }}
|
||||
alignItems="Center"
|
||||
justifyContent="Center"
|
||||
direction="Column"
|
||||
gap="300"
|
||||
>
|
||||
<Text>Error: {error}</Text>
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
|
||||
export function NoGifResults() {
|
||||
return (
|
||||
<Box
|
||||
style={{ padding: `${toRem(60)} ${config.space.S500}` }}
|
||||
alignItems="Center"
|
||||
justifyContent="Center"
|
||||
direction="Column"
|
||||
gap="300"
|
||||
>
|
||||
<SmileySadIcon size="48" />
|
||||
<Box direction="Inherit">
|
||||
<Text align="Center">No GIFs found!</Text>
|
||||
<Text priority="300" align="Center" size="T200">
|
||||
Try searching for something else or favoriting some gifs.
|
||||
</Text>
|
||||
</Box>
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
|
||||
type GifStatusProps = {
|
||||
loading: boolean;
|
||||
error: string | null;
|
||||
isEmpty: boolean;
|
||||
};
|
||||
|
||||
export function GifStatus({ loading, error, isEmpty }: Readonly<GifStatusProps>) {
|
||||
if (loading) return <GifSearching />;
|
||||
if (error) return <GifSearchError error={error} />;
|
||||
if (isEmpty) return <NoGifResults />;
|
||||
return null;
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import { useRef } from 'react';
|
||||
import { Input, Chip, Text } from 'folds';
|
||||
import { mobileOrTablet } from '$utils/user-agent';
|
||||
import { ArrowRight, sizedIcon, MagnifyingGlass } from '$components/icons/phosphor';
|
||||
import { EmojiBoardTab } from '../types';
|
||||
|
||||
type SearchInputProps = {
|
||||
query?: string;
|
||||
@@ -29,10 +30,12 @@ export function SearchInput({
|
||||
ref={inputRef}
|
||||
variant="SurfaceVariant"
|
||||
size="400"
|
||||
placeholder={allowTextCustomEmoji ? 'Search or Text Reaction ' : 'Search'}
|
||||
placeholder={
|
||||
allowTextCustomEmoji && !EmojiBoardTab.Gif ? 'Search or Text Reaction ' : 'Search'
|
||||
}
|
||||
maxLength={50}
|
||||
after={
|
||||
allowTextCustomEmoji && query ? (
|
||||
allowTextCustomEmoji && query && !EmojiBoardTab.Gif ? (
|
||||
<Chip
|
||||
variant="Primary"
|
||||
radii="Pill"
|
||||
|
||||
@@ -15,6 +15,18 @@ export function EmojiBoardTabs({
|
||||
}) {
|
||||
return (
|
||||
<Box gap="100">
|
||||
<Badge
|
||||
style={styles}
|
||||
as="button"
|
||||
variant="Secondary"
|
||||
fill={tab === EmojiBoardTab.Gif ? 'Solid' : 'None'}
|
||||
size="500"
|
||||
onClick={() => onTabChange(EmojiBoardTab.Gif)}
|
||||
>
|
||||
<Text as="span" size="L400">
|
||||
GIF
|
||||
</Text>
|
||||
</Badge>
|
||||
<Badge
|
||||
style={styles}
|
||||
as="button"
|
||||
|
||||
@@ -2,6 +2,7 @@ export * from './SearchInput';
|
||||
export * from './Tabs';
|
||||
export * from './Sidebar';
|
||||
export * from './NoStickerPacks';
|
||||
export * from './NoGifResults';
|
||||
export * from './Preview';
|
||||
export * from './Item';
|
||||
export * from './Group';
|
||||
|
||||
@@ -126,6 +126,21 @@ export const EmojiGroupContent = style([
|
||||
},
|
||||
]);
|
||||
|
||||
export const GifGroupContent = style([
|
||||
DefaultReset,
|
||||
{
|
||||
columnCount: 2,
|
||||
columnGap: config.space.S100,
|
||||
padding: `0 ${config.space.S200}`,
|
||||
|
||||
'@media': {
|
||||
'screen and (max-width: 480px)': {
|
||||
columnCount: 1,
|
||||
},
|
||||
},
|
||||
},
|
||||
]);
|
||||
|
||||
/**
|
||||
* Item
|
||||
*/
|
||||
@@ -172,3 +187,44 @@ export const StickerImg = style([
|
||||
objectFit: 'contain',
|
||||
},
|
||||
]);
|
||||
|
||||
export const GifContainer = style({
|
||||
columnCount: 3,
|
||||
columnGap: toRem(8),
|
||||
padding: toRem(16),
|
||||
|
||||
'@media': {
|
||||
'(max-width: 768px)': {
|
||||
columnCount: 2,
|
||||
},
|
||||
'(max-width: 480px)': {
|
||||
columnCount: 1,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export const GifItem = style([
|
||||
DefaultReset,
|
||||
FocusOutline,
|
||||
{
|
||||
width: '100%',
|
||||
marginBottom: toRem(8),
|
||||
breakInside: 'avoid',
|
||||
borderRadius: config.radii.R400,
|
||||
cursor: 'pointer',
|
||||
overflow: 'hidden',
|
||||
display: 'block',
|
||||
position: 'relative',
|
||||
|
||||
':hover': {
|
||||
backgroundColor: color.Surface.ContainerHover,
|
||||
},
|
||||
},
|
||||
]);
|
||||
|
||||
export const GifImg = style({
|
||||
width: '100%',
|
||||
height: '100%',
|
||||
objectFit: 'cover',
|
||||
borderRadius: config.radii.R400,
|
||||
});
|
||||
@@ -1,12 +1,14 @@
|
||||
export enum EmojiBoardTab {
|
||||
Emoji = 'Emoji',
|
||||
Sticker = 'Sticker',
|
||||
Gif = 'Gif',
|
||||
}
|
||||
|
||||
export enum EmojiType {
|
||||
Emoji = 'emoji',
|
||||
CustomEmoji = 'customEmoji',
|
||||
Sticker = 'sticker',
|
||||
Gif = 'gif',
|
||||
}
|
||||
|
||||
export type EmojiItemInfo = {
|
||||
@@ -15,3 +17,12 @@ export type EmojiItemInfo = {
|
||||
shortcode: string;
|
||||
label: string;
|
||||
};
|
||||
|
||||
export type GifData = {
|
||||
id: string;
|
||||
title: string;
|
||||
url: string;
|
||||
preview_url?: string;
|
||||
width?: number;
|
||||
height?: number;
|
||||
};
|
||||
@@ -16,10 +16,19 @@ import {
|
||||
Tooltip,
|
||||
TooltipProvider,
|
||||
as,
|
||||
color,
|
||||
config,
|
||||
toRem,
|
||||
} from 'folds';
|
||||
import { Eye, EyeSlash, menuIcon, sizedIcon, Image, Warning } from '$components/icons/phosphor';
|
||||
import {
|
||||
Eye,
|
||||
EyeSlash,
|
||||
menuIcon,
|
||||
sizedIcon,
|
||||
Image,
|
||||
Warning,
|
||||
Star,
|
||||
} from '$components/icons/phosphor';
|
||||
import classNames from 'classnames';
|
||||
import { BlurhashCanvas } from 'react-blurhash';
|
||||
import FocusTrap from 'focus-trap-react';
|
||||
@@ -35,7 +44,13 @@ import { useMediaAuthentication } from '$hooks/useMediaAuthentication';
|
||||
import { ModalWide } from '$styles/Modal.css';
|
||||
import { validBlurHash } from '$utils/blurHash';
|
||||
import * as css from './style.css';
|
||||
import { MATRIX_UNSTABLE_BLUR_HASH_PROPERTY_NAME } from '../../../../unstable/prefixes';
|
||||
import {
|
||||
MATRIX_SABLE_UNSTABLE_FAVORITE_GIFS,
|
||||
MATRIX_UNSTABLE_BLUR_HASH_PROPERTY_NAME,
|
||||
} from '../../../../unstable/prefixes';
|
||||
import { useAccountData } from '$hooks/useAccountData';
|
||||
import { AccountDataEvents, IContent } from 'matrix-js-sdk';
|
||||
import { useFavoriteGifs } from '$hooks/useFavoriteGifs';
|
||||
|
||||
function thumbnailDimsForMaxEdge(
|
||||
maxEdge: number,
|
||||
@@ -119,6 +134,11 @@ export const ImageContent = as<'div', ImageContentProps>(
|
||||
const [blurred, setBlurred] = useState(markedAsSpoiler ?? false);
|
||||
const [isHovered, setIsHovered] = useState(false);
|
||||
|
||||
const favoritedContent = useFavoriteGifs();
|
||||
const [favorited, setFavorited] = useState(
|
||||
favoritedContent.gifs.find((v) => v.url == url) != undefined
|
||||
);
|
||||
|
||||
const [srcState, loadSrc] = useAsyncCallback(
|
||||
useCallback(async () => {
|
||||
if (url.startsWith('http')) return url;
|
||||
@@ -374,21 +394,66 @@ export const ImageContent = as<'div', ImageContentProps>(
|
||||
{isHovered && (
|
||||
<Box style={{ padding: config.space.S200, right: 0, position: 'absolute' }}>
|
||||
<Menu style={{ padding: config.space.S0 }}>
|
||||
<MenuItem
|
||||
size="300"
|
||||
after={menuIcon(blurred ? Eye : EyeSlash)}
|
||||
radii="300"
|
||||
fill="Soft"
|
||||
variant="Secondary"
|
||||
title={blurred ? 'Reveal Image' : 'Hide Image'}
|
||||
onClick={(e) => {
|
||||
e.preventDefault();
|
||||
if (srcState.status === AsyncStatus.Idle) {
|
||||
loadSrc();
|
||||
setBlurred(false);
|
||||
} else setBlurred(!blurred);
|
||||
}}
|
||||
/>
|
||||
<Box>
|
||||
<MenuItem
|
||||
size="300"
|
||||
radii="0"
|
||||
fill="Soft"
|
||||
variant="Secondary"
|
||||
title={blurred ? 'Reveal Image' : 'Hide Image'}
|
||||
onClick={(e) => {
|
||||
e.preventDefault();
|
||||
if (srcState.status === AsyncStatus.Idle) {
|
||||
loadSrc();
|
||||
setBlurred(false);
|
||||
} else setBlurred(!blurred);
|
||||
}}
|
||||
>
|
||||
{menuIcon(blurred ? Eye : EyeSlash)}
|
||||
</MenuItem>
|
||||
{info?.mimetype == 'image/gif' && (
|
||||
<MenuItem
|
||||
size="300"
|
||||
radii="0"
|
||||
fill="Soft"
|
||||
variant="Secondary"
|
||||
title={favorited ? 'Unfavorite gif' : 'Favorite gif'}
|
||||
onClick={async (e) => {
|
||||
e.preventDefault();
|
||||
if (srcState.status === AsyncStatus.Success) {
|
||||
if (!favorited) {
|
||||
setFavorited(true);
|
||||
await mx
|
||||
.setAccountData(MATRIX_SABLE_UNSTABLE_FAVORITE_GIFS, {
|
||||
gifs: [
|
||||
...favoritedContent.gifs,
|
||||
{
|
||||
title: body,
|
||||
url: url,
|
||||
width: imageW,
|
||||
height: imageH,
|
||||
},
|
||||
],
|
||||
})
|
||||
.catch(() => setFavorited(false));
|
||||
} else {
|
||||
setFavorited(false);
|
||||
await mx
|
||||
.setAccountData(MATRIX_SABLE_UNSTABLE_FAVORITE_GIFS, {
|
||||
gifs: favoritedContent.gifs.filter((v) => v.url != url),
|
||||
})
|
||||
.catch(() => setFavorited(true));
|
||||
}
|
||||
}
|
||||
}}
|
||||
>
|
||||
{menuIcon(Star, {
|
||||
weight: favorited ? 'fill' : 'regular',
|
||||
color: favorited ? color.Warning.MainHover : color.Surface.OnContainer,
|
||||
})}
|
||||
</MenuItem>
|
||||
)}
|
||||
</Box>
|
||||
</Menu>
|
||||
</Box>
|
||||
)}
|
||||
|
||||
@@ -64,7 +64,7 @@ import {
|
||||
BlockType,
|
||||
} from '$components/editor';
|
||||
import { plainToEditorInput } from '$components/editor/input';
|
||||
import { EmojiBoard, EmojiBoardTab } from '$components/emoji-board';
|
||||
import { EmojiBoard, EmojiBoardTab, GifData } from '$components/emoji-board';
|
||||
import { UseStateProvider } from '$components/UseStateProvider';
|
||||
import type { TUploadContent } from '$utils/matrix';
|
||||
import { encryptFile, getImageInfo, mxcUrlToHttp, toggleReaction } from '$utils/matrix';
|
||||
@@ -179,6 +179,8 @@ import { AudioMessageRecorder } from './AudioMessageRecorder';
|
||||
import * as prefix from '$unstable/prefixes';
|
||||
import { PollDialog } from './poll-modals';
|
||||
import { LocationDialog } from './location-modal';
|
||||
import { useClientConfig } from '$hooks/useClientConfig';
|
||||
import { GifIcon } from '@phosphor-icons/react';
|
||||
|
||||
// Returns the event ID of the most recent non-reaction/non-edit event in a thread,
|
||||
// falling back to the thread root if no replies exist yet.
|
||||
@@ -277,6 +279,7 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
|
||||
// don't clobber the main room draft (and vice versa).
|
||||
const draftKey = threadRootId ?? roomId;
|
||||
const mx = useMatrixClient();
|
||||
const clientConfig = useClientConfig();
|
||||
const useAuthentication = useMediaAuthentication();
|
||||
const [enterForNewline] = useSetting(settingsAtom, 'enterForNewline');
|
||||
const [editorOldAddFile] = useSetting(settingsAtom, 'editorOldAddFile');
|
||||
@@ -1304,6 +1307,57 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
|
||||
mx.sendEvent(roomId, EventType.Sticker, content);
|
||||
};
|
||||
|
||||
const handleGifSelect = async (gif: GifData) => {
|
||||
function toBase64Url(value: string): string {
|
||||
const bytes = new TextEncoder().encode(value);
|
||||
let binary = '';
|
||||
|
||||
for (const byte of bytes) {
|
||||
binary += String.fromCodePoint(byte);
|
||||
}
|
||||
|
||||
return btoa(binary).replaceAll('+', '-').replaceAll('/', '_').replaceAll(/=+$/g, '');
|
||||
}
|
||||
|
||||
function toMatrixID(fname: string, prefix: string): string {
|
||||
const base64 = toBase64Url(fname);
|
||||
return prefix + base64;
|
||||
}
|
||||
|
||||
let url = gif.url.startsWith('mxc://')
|
||||
? gif.url
|
||||
: `mxc://${clientConfig.gifs?.proxyUrl ?? ''}/${toMatrixID(gif.url.slice('https://static.klipy.com/ii/'.length), 'klipy_')}`;
|
||||
|
||||
const content: RoomMessageEventContent & ReplyEventContent & IContent = {
|
||||
body: gif.title,
|
||||
url: url,
|
||||
msgtype: MsgType.Image,
|
||||
info: {
|
||||
w: gif.width,
|
||||
h: gif.height,
|
||||
mimetype: 'image/gif',
|
||||
},
|
||||
};
|
||||
|
||||
// Handle replies if there's a reply draft
|
||||
if (replyDraft) {
|
||||
content['m.relates_to'] = {
|
||||
'm.in_reply_to': {
|
||||
event_id: replyDraft.eventId,
|
||||
},
|
||||
};
|
||||
if (replyDraft.relation?.rel_type === RelationType.Thread) {
|
||||
content['m.relates_to'].event_id = replyDraft.relation.event_id;
|
||||
content['m.relates_to'].rel_type = RelationType.Thread;
|
||||
content['m.relates_to'].is_falling_back = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Send the gif as sticker event.
|
||||
await mx.sendEvent(roomId, EventType.RoomMessage, content);
|
||||
setReplyDraft(undefined);
|
||||
};
|
||||
|
||||
return (
|
||||
<div ref={ref}>
|
||||
{selectedFiles.length > 0 && (
|
||||
@@ -1702,6 +1756,7 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
|
||||
onEmojiSelect={handleEmoticonSelect}
|
||||
onCustomEmojiSelect={handleEmoticonSelect}
|
||||
onStickerSelect={handleStickerSelect}
|
||||
onGifSelect={handleGifSelect}
|
||||
requestClose={() => {
|
||||
setEmojiBoardTab((t) => {
|
||||
if (t) {
|
||||
@@ -1714,6 +1769,17 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
|
||||
/>
|
||||
}
|
||||
>
|
||||
<IconButton
|
||||
aria-pressed={emojiBoardTab === EmojiBoardTab.Gif}
|
||||
onClick={() => setEmojiBoardTab(EmojiBoardTab.Gif)}
|
||||
variant="SurfaceVariant"
|
||||
size="300"
|
||||
radii="300"
|
||||
>
|
||||
{composerIcon(GifIcon, {
|
||||
weight: emojiBoardTab === EmojiBoardTab.Gif ? 'fill' : 'regular',
|
||||
})}
|
||||
</IconButton>
|
||||
{!hideStickerBtn && (
|
||||
<IconButton
|
||||
aria-pressed={emojiBoardTab === EmojiBoardTab.Sticker}
|
||||
@@ -1732,7 +1798,10 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
|
||||
<IconButton
|
||||
ref={emojiBtnRef}
|
||||
aria-pressed={
|
||||
hideStickerBtn ? !!emojiBoardTab : emojiBoardTab === EmojiBoardTab.Emoji
|
||||
hideStickerBtn
|
||||
? emojiBoardTab === EmojiBoardTab.Emoji ||
|
||||
emojiBoardTab === EmojiBoardTab.Gif
|
||||
: emojiBoardTab === EmojiBoardTab.Emoji
|
||||
}
|
||||
onClick={() => setEmojiBoardTab(EmojiBoardTab.Emoji)}
|
||||
variant="SurfaceVariant"
|
||||
|
||||
@@ -7,6 +7,11 @@ export type HashRouterConfig = {
|
||||
basename?: string;
|
||||
};
|
||||
|
||||
export type GifsConfig = {
|
||||
klipyApiKey?: string;
|
||||
proxyUrl?: string;
|
||||
};
|
||||
|
||||
export type ClientConfig = {
|
||||
defaultHomeserver?: number;
|
||||
homeserverList?: string[];
|
||||
@@ -43,6 +48,8 @@ export type ClientConfig = {
|
||||
|
||||
hashRouter?: HashRouterConfig;
|
||||
|
||||
gifs?: GifsConfig;
|
||||
|
||||
matrixToBaseUrl?: string;
|
||||
|
||||
themeCatalogBaseUrl?: string;
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
import { AccountDataEvents } from 'matrix-js-sdk';
|
||||
import { MATRIX_SABLE_UNSTABLE_FAVORITE_GIFS } from '../../unstable/prefixes';
|
||||
import { useAccountData } from './useAccountData';
|
||||
|
||||
export const useFavoriteGifs =
|
||||
(): AccountDataEvents[typeof MATRIX_SABLE_UNSTABLE_FAVORITE_GIFS] => {
|
||||
const favoritedGifsData = useAccountData(MATRIX_SABLE_UNSTABLE_FAVORITE_GIFS);
|
||||
const favoritedContent = favoritedGifsData?.getContent<
|
||||
AccountDataEvents[typeof MATRIX_SABLE_UNSTABLE_FAVORITE_GIFS]
|
||||
>() ?? { gifs: [] };
|
||||
|
||||
return favoritedContent;
|
||||
};
|
||||
Vendored
+2
@@ -6,6 +6,7 @@ import type { MemberPowerTag } from '$types/matrix/room';
|
||||
import type { RoomAbbreviationsContent } from '$utils/abbreviations';
|
||||
import type { PronounSet } from '$utils/pronouns';
|
||||
import type * as prefix from '$unstable/prefixes';
|
||||
import { GifData } from '$components/emoji-board';
|
||||
|
||||
type PowerLevelTagsEventContent = Record<number, MemberPowerTag>;
|
||||
|
||||
@@ -57,5 +58,6 @@ declare module 'matrix-js-sdk/lib/@types/event' {
|
||||
[prefix.MATRIX_SABLE_UNSTABLE_ACCOUNT_SETTINGS_PROPERTY_NAME]: Record<string, unknown>;
|
||||
[prefix.MATRIX_SABLE_UNSTABLE_DISMISSED_INVITES]: { roomIds: string[] };
|
||||
[prefix.MATRIX_SABLE_UNSTABLE_ACCOUNT_ADDED_SERVERS_PROPERTY_NAME]: AddedServersContent;
|
||||
[prefix.MATRIX_SABLE_UNSTABLE_FAVORITE_GIFS]: { gifs: Omit<GifData, 'id'>[] };
|
||||
}
|
||||
}
|
||||
@@ -14,3 +14,4 @@ export const MATRIX_SABLE_UNSTABLE_ACCOUNT_PER_MESSAGE_PROFILES_PROPERTY_NAME =
|
||||
|
||||
export const MATRIX_SABLE_UNSTABLE_DISMISSED_INVITES = 'moe.sable.dismissed_invites';
|
||||
export const MATRIX_SABLE_UNSTABLE_ACCOUNT_ADDED_SERVERS_PROPERTY_NAME = 'moe.sable.added_servers';
|
||||
export const MATRIX_SABLE_UNSTABLE_FAVORITE_GIFS = 'moe.sable.favorite_gifs';
|
||||
Reference in new issue
Block a user