Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

선택지에서 이미지 붙혀넣기로 업로드 가능하도록 구현 #698

Merged
merged 2 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const IsDeletable = () => {
handleDeleteOptionClick={() => {}}
handleRemoveImageClick={() => {}}
handleUploadImage={() => {}}
handlePasteImage={() => {}}
optionId={Math.floor(Math.random() * 100000)}
text="방학 때 강릉으로 강아지와 기차여행을 하려했지
만 장마가 와서 취소했어요. 여행을 별로 좋"
Expand All @@ -42,6 +43,7 @@ export const IsNotDeletable = () => {
handleDeleteOptionClick={() => {}}
handleRemoveImageClick={() => {}}
handleUploadImage={() => {}}
handlePasteImage={() => {}}
optionId={Math.floor(Math.random() * 100000)}
text="방학 때 강릉으로 강아지와 기차여행을 하려했지
만 장마가 와서 취소했어요. 여행을 별로 좋"
Expand All @@ -62,6 +64,7 @@ export const ShowImage = () => {
handleDeleteOptionClick={() => {}}
handleRemoveImageClick={() => {}}
handleUploadImage={() => {}}
handlePasteImage={() => {}}
optionId={Math.floor(Math.random() * 100000)}
text="방학 때 강릉으로 강아지와 기차여행을 하려했지
만 장마가 와서 취소했어요. 여행을 별로 좋"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ChangeEvent, MutableRefObject } from 'react';
import { ChangeEvent, ClipboardEvent, MutableRefObject } from 'react';

import { POST_WRITING_OPTION } from '@constants/policy';
import { POST_OPTION_POLICY } from '@constants/policyMessage';

import OptionCancelButton from './OptionCancelButton';
Expand All @@ -11,17 +12,16 @@ interface WritingVoteOptionProps {
text: string;
isDeletable: boolean;
ariaLabel: string;
handleUpdateOptionChange: (event: ChangeEvent<HTMLTextAreaElement | HTMLInputElement>) => void;
handleUpdateOptionChange: (event: ChangeEvent<HTMLTextAreaElement>) => void;
handleDeleteOptionClick: () => void;
handleRemoveImageClick: () => void;
handleUploadImage: (event: ChangeEvent<HTMLInputElement>) => void;
handlePasteImage: (event: ClipboardEvent<HTMLTextAreaElement>) => void;
imageUrl: string;
contentInputRefList: MutableRefObject<HTMLInputElement[]>;
index: number;
}

const MAX_WRITING_LENGTH = 50;

export default function WritingVoteOption({
optionId,
text,
Expand All @@ -31,6 +31,7 @@ export default function WritingVoteOption({
handleDeleteOptionClick,
handleRemoveImageClick,
handleUploadImage,
handlePasteImage,
imageUrl,
contentInputRefList,
index,
Expand All @@ -47,9 +48,10 @@ export default function WritingVoteOption({
<S.ContentTextArea
name="optionText"
defaultValue={text}
onChange={(e: ChangeEvent<HTMLTextAreaElement>) => handleUpdateOptionChange(e)}
onChange={handleUpdateOptionChange}
onPaste={handlePasteImage}
placeholder={POST_OPTION_POLICY.DEFAULT}
maxLength={MAX_WRITING_LENGTH}
maxLength={POST_WRITING_OPTION.MAX_LENGTH}
/>

<OptionUploadImageButton
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChangeEvent, MutableRefObject } from 'react';
import { ChangeEvent, ClipboardEvent, MutableRefObject } from 'react';

import { WritingVoteOptionType } from '@hooks/useWritingOption';

Expand All @@ -21,6 +21,7 @@ interface WritingVoteOptionListProps {
removeImage: (optionId: number) => void;
handleUploadImage: (event: ChangeEvent<HTMLInputElement>, optionId: number) => void;
contentInputRefList: MutableRefObject<HTMLInputElement[]>;
handlePasteImage: (event: ClipboardEvent<HTMLTextAreaElement>, optionId: number) => void;
};
}

Expand All @@ -33,6 +34,7 @@ export default function WritingVoteOptionList({ writingOptionHook }: WritingVote
removeImage,
handleUploadImage,
contentInputRefList,
handlePasteImage,
} = writingOptionHook;
const isDeletable = optionList.length > MINIMUM_COUNT;

Expand All @@ -53,6 +55,9 @@ export default function WritingVoteOptionList({ writingOptionHook }: WritingVote
}
imageUrl={optionItem.imageUrl}
contentInputRefList={contentInputRefList}
handlePasteImage={(event: ClipboardEvent<HTMLTextAreaElement>) =>
handlePasteImage(event, optionItem.id)
}
index={index}
/>
))}
Expand Down
21 changes: 20 additions & 1 deletion frontend/src/hooks/useWritingOption.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { ChangeEvent, useRef, useState } from 'react';
import React, { ChangeEvent, ClipboardEvent, useRef, useState } from 'react';

import { POST_WRITING_OPTION } from '@constants/policy';

Expand Down Expand Up @@ -68,6 +68,24 @@ export const useWritingOption = (initialOptionList?: WritingVoteOptionType[]) =>
setOptionList(removedOptionList);
};

const handlePasteImage = (event: ClipboardEvent<HTMLTextAreaElement>, optionId: number) => {
const file = event.clipboardData.files[0];

if (!file) return;

if (file.type.slice(0, 5) === 'image') {
event.preventDefault();

const optionIndex = optionList.findIndex(option => option.id === optionId);

uploadImage({
imageFile: file,
inputElement: contentInputRefList.current[optionIndex],
setPreviewImageUrl: setPreviewImageUrl(optionId),
});
}
};

const removeImage = (optionId: number) => {
const updatedOptionList = optionList.map(optionItem => {
if (optionItem.id === optionId) {
Expand Down Expand Up @@ -121,5 +139,6 @@ export const useWritingOption = (initialOptionList?: WritingVoteOptionType[]) =>
removeImage,
handleUploadImage,
contentInputRefList,
handlePasteImage,
};
};
Loading