added basic plaintext image description adjacent to the text input
This commit is contained in:
1 parent
a2d61da254
commit
6c90547ce5
4 files changed
+52
-4
No files matched your search
@@ -98,6 +98,7 @@ type UploadCardRendererProps = {
|
||||
isEncrypted?: boolean;
|
||||
fileItem: TUploadItem;
|
||||
setMetadata: (fileItem: TUploadItem, metadata: TUploadMetadata) => void;
|
||||
setDescription: (fileContent: TUploadContent) => void;
|
||||
onRemove: (file: TUploadContent) => void;
|
||||
onComplete?: (upload: UploadSuccess) => void;
|
||||
};
|
||||
@@ -105,6 +106,7 @@ export function UploadCardRenderer({
|
||||
isEncrypted,
|
||||
fileItem,
|
||||
setMetadata,
|
||||
setDescription,
|
||||
onRemove,
|
||||
onComplete,
|
||||
}: UploadCardRendererProps) {
|
||||
@@ -130,13 +132,15 @@ export function UploadCardRenderer({
|
||||
cancelUpload();
|
||||
onRemove(file);
|
||||
};
|
||||
const handleDescription = () => {
|
||||
setDescription(file);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (upload.status === UploadStatus.Success) {
|
||||
onComplete?.(upload);
|
||||
}
|
||||
}, [upload, onComplete]);
|
||||
|
||||
return (
|
||||
<UploadCard
|
||||
radii="300"
|
||||
@@ -155,6 +159,16 @@ export function UploadCardRenderer({
|
||||
<Text size="B300">Retry</Text>
|
||||
</Chip>
|
||||
)}
|
||||
<IconButton
|
||||
onClick={handleDescription}
|
||||
aria-label="Add Upload Description"
|
||||
variant="SurfaceVariant"
|
||||
radii="Pill"
|
||||
size="300"
|
||||
>
|
||||
<Icon src={Icons.Pencil} size="50" />
|
||||
</IconButton>
|
||||
|
||||
<IconButton
|
||||
onClick={removeUpload}
|
||||
aria-label="Cancel Upload"
|
||||
|
||||
@@ -182,6 +182,7 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
|
||||
|
||||
const [uploadBoard, setUploadBoard] = useState(true);
|
||||
const [selectedFiles, setSelectedFiles] = useAtom(roomIdToUploadItemsAtomFamily(roomId));
|
||||
const [describedFile, setDescribedFile] = useState<TUploadContent | undefined>(undefined);
|
||||
const uploadFamilyObserverAtom = createUploadFamilyObserverAtom(
|
||||
roomUploadAtomFamily,
|
||||
selectedFiles.map((f) => f.file)
|
||||
@@ -214,6 +215,7 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
|
||||
metadata: {
|
||||
markedAsSpoiler: false,
|
||||
},
|
||||
body: 'aaa',
|
||||
})
|
||||
);
|
||||
} else {
|
||||
@@ -225,6 +227,7 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
|
||||
metadata: {
|
||||
markedAsSpoiler: false,
|
||||
},
|
||||
body: 'aaa',
|
||||
})
|
||||
);
|
||||
}
|
||||
@@ -303,7 +306,12 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
|
||||
},
|
||||
[setSelectedFiles]
|
||||
);
|
||||
|
||||
const handleDescription = useCallback(
|
||||
(fileItem: TUploadContent) => {
|
||||
setDescribedFile(fileItem);
|
||||
},
|
||||
[setDescribedFile]
|
||||
);
|
||||
const handleRemoveUpload = useCallback(
|
||||
(upload: TUploadContent | TUploadContent[]) => {
|
||||
const uploads = Array.isArray(upload) ? upload : [upload];
|
||||
@@ -331,6 +339,14 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
|
||||
const fileItem = selectedFiles.find((f) => f.file === upload.file);
|
||||
if (!fileItem) throw new Error('Broken upload');
|
||||
|
||||
if (fileItem.file === describedFile) {
|
||||
fileItem.body = plaintext;
|
||||
resetEditor(editor);
|
||||
resetEditorHistory(editor);
|
||||
setInputKey((prev) => prev + 1);
|
||||
sendTypingStatus(false);
|
||||
setDescribedFile(undefined);
|
||||
}
|
||||
if (fileItem.file.type.startsWith('image')) {
|
||||
return getImageMsgContent(mx, fileItem, upload.mxc);
|
||||
}
|
||||
@@ -364,6 +380,7 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
|
||||
const submit = useCallback(async () => {
|
||||
uploadBoardHandlers.current?.handleSend();
|
||||
|
||||
if (describedFile !== undefined) return;
|
||||
const commandName = getBeginCommand(editor);
|
||||
let plainText = toPlainText(editor.children, isMarkdown).trim();
|
||||
let customHtml = trimCustomHtml(
|
||||
@@ -436,13 +453,24 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
|
||||
resetEditorHistory(editor);
|
||||
setInputKey((prev) => prev + 1);
|
||||
sendTypingStatus(false);
|
||||
setDescribedFile(undefined);
|
||||
|
||||
try {
|
||||
await mx.sendMessage(roomId, content as any);
|
||||
} catch (error) {
|
||||
log.error('failed to send message', { roomId }, error);
|
||||
}
|
||||
}, [mx, roomId, editor, replyDraft, sendTypingStatus, setReplyDraft, isMarkdown, commands]);
|
||||
}, [
|
||||
mx,
|
||||
roomId,
|
||||
editor,
|
||||
replyDraft,
|
||||
describedFile,
|
||||
sendTypingStatus,
|
||||
setReplyDraft,
|
||||
isMarkdown,
|
||||
commands,
|
||||
]);
|
||||
|
||||
const handleKeyDown: KeyboardEventHandler = useCallback(
|
||||
(evt) => {
|
||||
@@ -546,6 +574,7 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
|
||||
fileItem={fileItem}
|
||||
setMetadata={handleFileMetadata}
|
||||
onRemove={handleRemoveUpload}
|
||||
setDescription={handleDescription}
|
||||
/>
|
||||
))}
|
||||
</UploadBoardContent>
|
||||
|
||||
@@ -77,6 +77,7 @@ export const getImageMsgContent = async (
|
||||
} else {
|
||||
content.url = mxc;
|
||||
}
|
||||
if (item.body?.length && item.body.length > 0) content.body = item.body;
|
||||
return content;
|
||||
};
|
||||
|
||||
@@ -126,6 +127,7 @@ export const getVideoMsgContent = async (
|
||||
} else {
|
||||
content.url = mxc;
|
||||
}
|
||||
if (item.body?.length && item.body.length > 0) content.body = item.body;
|
||||
return content;
|
||||
};
|
||||
|
||||
@@ -148,6 +150,7 @@ export const getAudioMsgContent = (item: TUploadItem, mxc: string): IContent =>
|
||||
} else {
|
||||
content.url = mxc;
|
||||
}
|
||||
if (item.body?.length && item.body.length > 0) content.body = item.body;
|
||||
return content;
|
||||
};
|
||||
|
||||
@@ -155,8 +158,8 @@ export const getFileMsgContent = (item: TUploadItem, mxc: string): IContent => {
|
||||
const { file, encInfo } = item;
|
||||
const content: IContent = {
|
||||
msgtype: MsgType.File,
|
||||
body: file.name,
|
||||
filename: file.name,
|
||||
body: file.name,
|
||||
info: {
|
||||
mimetype: file.type,
|
||||
size: file.size,
|
||||
@@ -170,5 +173,6 @@ export const getFileMsgContent = (item: TUploadItem, mxc: string): IContent => {
|
||||
} else {
|
||||
content.url = mxc;
|
||||
}
|
||||
if (item.body?.length && item.body.length > 0) content.body = item.body;
|
||||
return content;
|
||||
};
|
||||
@@ -16,6 +16,7 @@ export type TUploadItem = {
|
||||
originalFile: TUploadContent;
|
||||
metadata: TUploadMetadata;
|
||||
encInfo: EncryptedAttachmentInfo | undefined;
|
||||
body?: string;
|
||||
};
|
||||
|
||||
export type TUploadListAtom = ReturnType<typeof createListAtom<TUploadItem>>;
|
||||
|
||||
Reference in new issue
Block a user