From 11d70e47a1bd9e0e37e1caf147988f818209b506 Mon Sep 17 00:00:00 2001 From: sunmin <81913106+leesunmin1231@users.noreply.github.com> Date: Thu, 8 Dec 2022 20:16:28 +0900 Subject: [PATCH] Feature/fe/210 past action (#211) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(fe): 에디터 클릭 안하고 업로드 되는 부분 수정 #210 * fix(fe): paste action 수정 #210 * fix(fe): 이미지 업로드 재수정 #210 --- client/components/Write/Editor/index.tsx | 48 +++++++++++++++--------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/client/components/Write/Editor/index.tsx b/client/components/Write/Editor/index.tsx index 0e69153f..0081c4b9 100644 --- a/client/components/Write/Editor/index.tsx +++ b/client/components/Write/Editor/index.tsx @@ -51,6 +51,11 @@ export default function Editor({ parentPostData, modifyPostData }: Props) { const previewRef = useRef(null); const [tabIndex, setTabIndex] = useState(0); // 0 Editor, 1 Preview const [content, setContent] = useState(''); + const [contentHTML, setContentHTML] = useState('

'); // 탭 전환용 + const authedUserInfo = useRecoilValue(authedUser); + const [imageOver, setImageOver] = useState(false); + + // 멘션 기능 관련 상태. const [dropDownDisplay, setDropDownDisplay] = useState('none'); const [dropDownPosition, setDropDownPosition] = useState<{ x: string; y: string }>({ x: '0px', @@ -60,10 +65,7 @@ export default function Editor({ parentPostData, modifyPostData }: Props) { const [mentionList, setMentionList] = useState([]); const [followList, setFollowList] = useState([]); const [inputUserId, setInputUserId] = useState(''); - const [contentHTML, setContentHTML] = useState('

'); // 탭 전환용 const [selectUser, setSelectUser] = useState(0); - const [imageOver, setImageOver] = useState(false); - const authedUserInfo = useRecoilValue(authedUser); const submitHandler = async () => { const removeDup = new Set(mentionList); @@ -198,31 +200,41 @@ export default function Editor({ parentPostData, modifyPostData }: Props) { // 아래부터 에디터 제스쳐 관련 코드 const pasteAction = (data: string) => { - // console.log(JSON.stringify(data)); const cursor = window.getSelection(); - if (!cursor) return; - if (!contentRef.current) return; + if (!cursor) return false; + if (!contentRef.current) return false; const collapseNode = cursor.anchorNode; if (cursor.type === 'Caret') { - if (!cursor.anchorNode) return; - const position = cursor.anchorNode.nodeType === 3 ? cursor.anchorOffset + data.length : 1; - cursor.anchorNode.textContent = `${cursor.anchorNode?.textContent?.slice( - 0, - cursor.anchorOffset - )}${data}${cursor.anchorNode?.textContent?.slice(cursor.anchorOffset)}`; - + if (!cursor.anchorNode) return false; + const position = cursor.anchorNode.nodeName === '#text' ? cursor.anchorOffset + data.length : 1; + switch (cursor.anchorNode.nodeName) { + case 'DIV': + cursor.anchorNode.textContent = `${cursor.anchorNode?.textContent}${data}`; + break; + case '#text': + cursor.anchorNode.textContent = `${cursor.anchorNode?.textContent?.slice( + 0, + cursor.anchorOffset + )}${data}${cursor.anchorNode?.textContent?.slice(cursor.anchorOffset)}`; + break; + default: + break; + } window.getSelection()?.collapse(collapseNode, position); + return true; } if (cursor.type === 'Range') { - if (!cursor.anchorNode || !cursor.focusNode) return; + if (!cursor.anchorNode || !cursor.focusNode) return false; cursor.deleteFromDocument(); - const position = cursor.anchorNode.nodeType === 3 ? cursor.anchorOffset + data.length : 1; + const position = cursor.anchorNode.nodeName === '#text' ? cursor.anchorOffset + data.length : 1; cursor.anchorNode.textContent = `${cursor.anchorNode?.textContent?.slice( 0, cursor.anchorOffset )}${data}${cursor.anchorNode?.textContent?.slice(cursor.anchorOffset)}`; window.getSelection()?.collapse(collapseNode, position); + return true; } + return false; }; const handlePaste = (e: ClipboardEvent) => { @@ -383,8 +395,10 @@ export default function Editor({ parentPostData, modifyPostData }: Props) { fetchImage() .then((imageData) => { const data = `![${files[0].name as string}](${imageData.data.imageLink})`; - pasteAction(`${data}`); - setContent(data); // setContent를 안하면 프리뷰에 반영이 안됩니다.. + const success = pasteAction(`${data}`); + if (success) { + setContent(data); // setContent를 안하면 프리뷰에 반영이 안됩니다.. + } }) .catch((e) => alert(`이미지 업로드에 실패하였습니다. Error Message: ${e}`)); } else {