mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-20 17:11:17 -08:00
feat: support gif slash command searches
This commit is contained in:
5 files changed
+38
-16
No files matched your search
@@ -0,0 +1,5 @@
|
||||
---
|
||||
sable: minor
|
||||
---
|
||||
|
||||
Add a `/gif` composer command that opens the GIF picker.
|
||||
@@ -455,6 +455,7 @@ type EmojiBoardProps = {
|
||||
onCustomEmojiSelect?: (mxc: string, shortcode: string) => void;
|
||||
onStickerSelect?: (mxc: string, shortcode: string, label: string) => void;
|
||||
onGifSelect?: (gif: GifData, spoiler?: boolean) => void;
|
||||
initialGifSearch?: string;
|
||||
allowTextCustomEmoji?: boolean;
|
||||
addToRecentEmoji?: boolean;
|
||||
isFullWidth?: boolean;
|
||||
@@ -474,6 +475,7 @@ export function EmojiBoard({
|
||||
onCustomEmojiSelect,
|
||||
onStickerSelect,
|
||||
onGifSelect,
|
||||
initialGifSearch,
|
||||
allowTextCustomEmoji,
|
||||
addToRecentEmoji = true,
|
||||
isFullWidth,
|
||||
@@ -576,6 +578,13 @@ export function EmojiBoard({
|
||||
{ wait: 200 }
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (!gifTab || initialGifSearch === undefined) return;
|
||||
setShowFavoritesOnly(initialGifSearch.length === 0);
|
||||
if (initialGifSearch) searchGifs(initialGifSearch);
|
||||
else resetGifSearch();
|
||||
}, [gifTab, initialGifSearch, searchGifs, resetGifSearch]);
|
||||
|
||||
const contentScrollRef = useRef<HTMLDivElement>(null);
|
||||
const virtualBaseRef = useRef<HTMLDivElement>(null);
|
||||
const virtualizer = useVirtualizer({
|
||||
@@ -677,10 +686,11 @@ export function EmojiBoard({
|
||||
<Box direction="Column" gap="200">
|
||||
{onTabChange && <EmojiBoardTabs tab={tab} onTabChange={onTabChange} />}
|
||||
<SearchInput
|
||||
key={tab}
|
||||
key={gifTab ? `${tab}-${initialGifSearch ?? ''}` : tab}
|
||||
query={emojiResult?.query}
|
||||
onChange={handleOnChange}
|
||||
tab={tab}
|
||||
defaultValue={gifTab ? initialGifSearch : undefined}
|
||||
allowTextCustomEmoji={allowTextCustomEmoji}
|
||||
onTextCustomEmojiSelect={handleTextCustomEmojiSelect}
|
||||
/>
|
||||
|
||||
@@ -7,6 +7,7 @@ import { EmojiBoardTab } from '../types';
|
||||
|
||||
type SearchInputProps = {
|
||||
query?: string;
|
||||
defaultValue?: string;
|
||||
onChange: ChangeEventHandler<HTMLInputElement>;
|
||||
allowTextCustomEmoji?: boolean;
|
||||
onTextCustomEmojiSelect?: (text: string) => void;
|
||||
@@ -14,6 +15,7 @@ type SearchInputProps = {
|
||||
};
|
||||
export function SearchInput({
|
||||
query,
|
||||
defaultValue,
|
||||
onChange,
|
||||
allowTextCustomEmoji,
|
||||
onTextCustomEmojiSelect,
|
||||
@@ -40,6 +42,7 @@ export function SearchInput({
|
||||
: 'Search'
|
||||
}
|
||||
maxLength={50}
|
||||
defaultValue={defaultValue}
|
||||
after={
|
||||
allowTextCustomEmoji && query && tab !== EmojiBoardTab.Gif ? (
|
||||
<Chip
|
||||
|
||||
@@ -26,7 +26,6 @@ type CommandAutocompleteProps = {
|
||||
editor: Editor;
|
||||
query: AutocompleteQuery<string>;
|
||||
requestClose: () => void;
|
||||
onGifSelect: () => void;
|
||||
};
|
||||
|
||||
const SEARCH_OPTIONS: UseAsyncSearchOptions = {
|
||||
@@ -40,7 +39,6 @@ export function CommandAutocomplete({
|
||||
editor,
|
||||
query,
|
||||
requestClose,
|
||||
onGifSelect,
|
||||
}: CommandAutocompleteProps) {
|
||||
const mx = useMatrixClient();
|
||||
const commands = useCommands(mx, room);
|
||||
@@ -63,11 +61,6 @@ export function CommandAutocomplete({
|
||||
}, [query.text, search, resetSearch]);
|
||||
|
||||
const handleAutocomplete: CommandAutoCompleteHandler = (commandName) => {
|
||||
if (commandName === GIF_COMMAND) {
|
||||
onGifSelect();
|
||||
requestClose();
|
||||
return;
|
||||
}
|
||||
const cmdEl = createCommandElement(commandName);
|
||||
replaceWithElement(editor, query.range, cmdEl);
|
||||
moveCursor(editor, true);
|
||||
@@ -118,7 +111,7 @@ export function CommandAutocomplete({
|
||||
</Text>
|
||||
<Text truncate priority="300" size="T200">
|
||||
{commandName === GIF_COMMAND
|
||||
? 'Search and send a GIF'
|
||||
? 'Search and send a GIF: /gif <search>'
|
||||
: commands[commandName as Command].description}
|
||||
</Text>
|
||||
</Box>
|
||||
|
||||
@@ -521,11 +521,13 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
|
||||
const [sendError, setSendError] = useState<string | undefined>();
|
||||
const isEncrypted = room.hasEncryptionStateEvent();
|
||||
const [emojiBoardTab, setEmojiBoardTab] = useState<EmojiBoardTab | undefined>(undefined);
|
||||
const [initialGifSearch, setInitialGifSearch] = useState<string>();
|
||||
const closeEmojiBoard = useCallback(() => {
|
||||
if (isMobileOrTablet()) {
|
||||
const activeElement = document.activeElement;
|
||||
if (activeElement instanceof HTMLElement) activeElement.blur();
|
||||
}
|
||||
setInitialGifSearch(undefined);
|
||||
setEmojiBoardTab(undefined);
|
||||
}, []);
|
||||
const toggleEmojiBoardTab = useCallback((tab: EmojiBoardTab) => {
|
||||
@@ -1212,6 +1214,8 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
|
||||
room,
|
||||
})
|
||||
);
|
||||
const rawGifCommand =
|
||||
commandName === undefined ? plainText.match(/^\/gif(?:\s+(.*))?$/i) : undefined;
|
||||
|
||||
let msgType = MsgType.Text;
|
||||
|
||||
@@ -1228,6 +1232,15 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
|
||||
return; // don't do anything besides handling the command
|
||||
}
|
||||
|
||||
if (rawGifCommand) {
|
||||
setInitialGifSearch(rawGifCommand[1] ?? '');
|
||||
setEmojiBoardTab(EmojiBoardTab.Gif);
|
||||
resetEditor(editor);
|
||||
resetEditorHistory(editor);
|
||||
sendTypingStatus(false);
|
||||
return;
|
||||
}
|
||||
|
||||
if (commandName) {
|
||||
plainText = trimCommand(commandName, plainText);
|
||||
customHtml = trimCommand(commandName, customHtml);
|
||||
@@ -1249,7 +1262,10 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
|
||||
if ((commandName as Command) === Command.Poll) setShowPollPicker(true);
|
||||
else if ((commandName as Command) === Command.Location && plainText.trim().length === 0)
|
||||
setShowLocationPicker(true);
|
||||
else {
|
||||
else if (commandName === 'gif') {
|
||||
setInitialGifSearch(plainText);
|
||||
setEmojiBoardTab(EmojiBoardTab.Gif);
|
||||
} else {
|
||||
const commandContent = commands[commandName as Command];
|
||||
if (commandContent) {
|
||||
commandContent.exe(plainText, customHtml);
|
||||
@@ -1831,12 +1847,6 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
|
||||
editor={editor}
|
||||
query={autocompleteQuery}
|
||||
requestClose={handleCloseAutocomplete}
|
||||
onGifSelect={() => {
|
||||
resetEditor(editor);
|
||||
resetEditorHistory(editor);
|
||||
sendTypingStatus(false);
|
||||
setEmojiBoardTab(EmojiBoardTab.Gif);
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
{isQuickTextReact &&
|
||||
@@ -2227,6 +2237,7 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
|
||||
onCustomEmojiSelect={handleEmoticonSelect}
|
||||
onStickerSelect={handleStickerSelect}
|
||||
onGifSelect={handleGifSelect}
|
||||
initialGifSearch={initialGifSearch}
|
||||
requestClose={closeEmojiBoard}
|
||||
/>
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user