ė ėēī ė―ëë githubė ėŽëĶž(wetube_v2)
ðĄ ėĪėę° ëęļ ęĩŽí (javascriptëĄ ëęļ ėķę°)
ð data attribute / JSON.parse()
ėēėėë íėí ę° ė ëķëĨž data ėėąėžëĄ ë§ëĪėīė pug íėžė ęļļęē ëėīíī íëĄ íļėëëĄ ę°ė ļėëĪ.
div.comment-container__item(data-comment-owner-id=comment.owner._id, data-comment-owner-avatarurl=comment.owner.avatarUrl, data-comment-owner-name=comment.owner.name)
ėīęąī ėë ęą° ę°ėė data-comment-ownerë§ ëĻęēžëĪ.
div.comment-container__item(data-comment-owner=comment.owner)
ownerNameLink.textContent = commentMixinItem.dataset.commentOwner.name;
ę·ļë°ë° ėīë ęē íëęđ comment.js íėžėė data ėėąė íĪ ę°ė ę°ė ļėŽ ėę° ėėëĪ.
ę°ė ę°ė ļėŽ ė ėëëĄJSON.parse()
ëĨž ėīėĐíī JSON ëŽļėėīė ėĪëļė íļëĄ ë°ęŋėĢžėëĪ.ownerNameLink.textContent = JSON.parse(commentMixinItem.dataset.commentOwner).name;
ð ėķíė ėė í ęē
comment.pugėė ęļ°ëģļ ėīëŊļė§ë ë°°íŽ í ėė í ęēėīëĪ.
comment.jsėėë ėžëĻ github avatarë§ ęģ ë ĪíëĪ. ëĄėŧŽ íīëė ėŽëĶ° íëĄí ėīëŊļė§ë ęģ ë Īíė§ ėėëĪ.(ė ëëĄ ëė°ë ĪëĐī ęē―ëĄė "/"ëĨž ėķę°íīėž íė§ë§ ėŽęļ°ėë ęĩģėī ęē―ė°ëĨž ëë ë§ëĪė§ ėėëĪ.) ėī ëí ë°°íŽ í ėė í ęēėīëĪ.//- comment.pug mixin comment(comment) div.comment-container__item(data-comment-owner=comment.owner) div.show-comment__profile-img a(href=`/users/${comment.owner._id}`) //- ë°°íŽ í ėė í ęē (ęļ°ëģļ ėīëŊļė§ í ëđ) if !comment.owner.avatarUrl div.profile-img ð else if comment.owner.socialOnly div.profile-img(style=`background-image: url("${comment.owner.avatarUrl}");`) else div.profile-img(style=`background-image: url("/${comment.owner.avatarUrl}");`) div.show-comment__data a.show-comment__owner-name(href=`/users/${comment.owner._id}`) #{comment.owner.name} p.show-comment__text #{comment.text}
// comment.js const addComment = (commentText) => { const itemDiv = document.createElement("div"); itemDiv.classList.add("comment-container__item"); // íëĄí ėīëŊļė§ const profileImgDiv = document.createElement("div"); profileImgDiv.classList.add("show-comment__profile-img"); const profileImgLink = document.createElement("a"); profileImgLink.setAttribute( "href", `/users/${JSON.parse(commentMixinItem.dataset.commentOwner)._id}` ); const profileImg = document.createElement("div"); profileImg.classList.add("profile-img"); profileImg.style.backgroundImage = `url("${ JSON.parse(commentMixinItem.dataset.commentOwner).avatarUrl }")`; // ėžëĻė Github Avatarë§ ęģ ë ĪíĻ (ë°°íŽ í ėė í ęē) // ėīëĶ & ëęļ ëīėĐ const dataDiv = document.createElement("div"); dataDiv.classList.add("show-comment__data"); const ownerNameLink = document.createElement("a"); ownerNameLink.classList.add("show-comment__owner-name"); ownerNameLink.setAttribute( "href", `/users/${JSON.parse(commentMixinItem.dataset.commentOwner)._id}` ); const text = document.createElement("div"); text.classList.add("show-comment__text"); // ėĄ°íĐ ë° ë°°ėđ profileImgLink.appendChild(profileImg); profileImgDiv.appendChild(profileImgLink); ownerNameLink.textContent = JSON.parse( commentMixinItem.dataset.commentOwner ).name; text.textContent = commentText; dataDiv.appendChild(ownerNameLink); dataDiv.appendChild(text); itemDiv.appendChild(profileImgDiv); itemDiv.appendChild(dataDiv); commentContainerShow.prepend(itemDiv); };
ðĄ textarea ëėī ėë ėĄ°ė
element.scrollHeight
ėīėĐ// comment.js const textarea = form.querySelector(".write-comment__textarea"); const textareaResize = (event) => { event.target.style.height = "1.5rem"; event.target.style.height = event.target.scrollHeight + "px"; }; textarea.addEventListener("keyup", textareaResize);
ėīë ęē ėėąíëë° ėēėė ėīėíęēë keyup ėīëēĪíļę° ėžėīë ëë§ëĪ textareaė ëėīę° 4pxėĐ ėĶę°íëĪ. ėėļė ėë ė―ëė ėėëĪ.
.write-comment__textarea:focus { border-bottom: 2px solid $color-white; transition: all 200ms ease-in-out; }
íīëđ ė―ëëĨž ėė íęģ comment.jsė ëĪė ė―ëëĨž ėķę°íëĪ.
ðĄ ëęļ ėėąë íīëĶ ė ėĢžëģ ėėëĪ ėĪíėž ëģęē―
// comment.js const textarea = form.querySelector(".write-comment__textarea"); const submitBtn = document.querySelector(".submit-button"); const TEXTAREA_FOCUS_BG_COLOR = "rgb(168, 218, 220)"; // $color-light-blue const TEXTAREA_BLUR_BG_COLOR = "rgba(241, 250, 238, 0.4)"; // $color-light-white const TEXTAREA_FOCUS_COLOR = "rgb(29, 53, 87)"; // $bg const TEXTAREA_BLUR_COLOR = "rgb(241, 250, 238)"; // $color-dark-white const handleTextareaValueInput = () => { if (textarea.value) { submitBtn.style.backgroundColor = TEXTAREA_FOCUS_BG_COLOR; submitBtn.style.color = TEXTAREA_FOCUS_COLOR; } else { submitBtn.style.backgroundColor = TEXTAREA_BLUR_BG_COLOR; submitBtn.style.color = TEXTAREA_BLUR_COLOR; } }; const handleTextareaFocus = () => { textarea.style.borderBottom = `2px solid ${TEXTAREA_FOCUS_BG_COLOR}`; }; const handleTextareaBlur = () => { textarea.style.borderBottom = `1px solid ${TEXTAREA_BLUR_BG_COLOR}`; }; textarea.addEventListener("input", handleTextareaValueInput); textarea.addEventListener("focus", handleTextareaFocus); textarea.addEventListener("blur", handleTextareaBlur);
ðĄ ė·Ļė ëēíž & ëęļ ëēíž ęĩŽí
ė íëļ ëęļ ėėąë ė°ļęģ íīė ëđė·íęē ęĩŽííęģ ė íëĪ.
ęļ°ëģļė ėžëĄ ëēížė ëģīėīė§ ėė§ë§ textareaę° focus ëëĐī ëēížėī ëģīėīëĐīė textareaė ėĪíėžėī ë°ëëĪ.
textarea íĪëģīë ė ë Ĩ ėŽëķė ë°ëž 'ëęļ ëēíž'ė ėĪíėžėī ë°ëëĪ.
ëęļ ëēížė ëëĨīëĐī ëęļ ėėąëė ėë ęļė ėŽëžė§ęģ ëŠĻë ęēė ęļ°ëģļ ėĪíėžëĄ ë°ëëĐ° ëēížëĪėī ėė ëëĪ.
ė·Ļė ëēížė ëëĨīëĐī ëęļ ėėąëė ėë ęļė ėŽëžė§ęģ ëŠĻë ęēė ęļ°ëģļ ėĪíėžëĄ ë°ëëĐ° ëēížëĪėī ėė ëëĪ.
ëĪė textareaę° focus ëëĐī ëēížëĪėī ëĪė ėķę°ëęģ textareaė ėĪíėžėī ë°ëëĪ.// comment.js let buttonsDiv = document.querySelector(".write-comment__buttons"); let submitBtn = document.querySelector(".submit-button"); let cancelBtn = document.querySelector(".cancel-button"); const TEXTAREA_FOCUS_BG_COLOR = "rgb(168, 218, 220)"; const TEXTAREA_BLUR_BG_COLOR = "rgba(241, 250, 238, 0.4)"; const TEXTAREA_FOCUS_COLOR = "rgb(29, 53, 87)"; const TEXTAREA_BLUR_COLOR = "rgb(241, 250, 238)"; const handleSubmitBtn = async (event) => { // ėĪëĩ if (status === 201) { addComment(text); handleCancelBtnClick(); // ėķę° } }; const handleTextareaValueInput = () => { if (textarea.value) { submitBtn.style.backgroundColor = TEXTAREA_FOCUS_BG_COLOR; submitBtn.style.color = TEXTAREA_FOCUS_COLOR; } else { submitBtn.style.backgroundColor = TEXTAREA_BLUR_BG_COLOR; submitBtn.style.color = TEXTAREA_BLUR_COLOR; } }; const handleCancelBtnClick = () => { textarea.value = ""; buttonsDiv.remove(); }; const handleTextareaFocus = () => { textarea.style.borderBottom = `2px solid ${TEXTAREA_FOCUS_BG_COLOR}`; if (buttonsDiv) { form.appendChild(buttonsDiv); handleTextareaValueInput(); // ëēę·ļ ėė } else { buttonsDiv = document.createElement("div"); buttonsDiv.classList.add("write-comment__buttons"); cancelBtn = document.createElement("button"); cancelBtn.classList.add("cancel-button"); cancelBtn.textContent = "ė·Ļė"; submitBtn = document.createElement("button"); submitBtn.classList.add("submit-button"); submitBtn.textContent = "ëęļ"; buttonsDiv.appendChild(cancelBtn); buttonsDiv.appendChild(submitBtn); form.appendChild(buttonsDiv); } textarea.addEventListener("input", handleTextareaValueInput); cancelBtn.addEventListener("click", handleCancelBtnClick); }; const handleTextareaBlur = () => { textarea.style.borderBottom = `1px solid ${TEXTAREA_BLUR_BG_COLOR}`; }; form.addEventListener("submit", handleSubmitBtn); textarea.addEventListener("keyup", textareaResize); textarea.addEventListener("focus", handleTextareaFocus); textarea.addEventListener("blur", handleTextareaBlur);
âŧ ëēę·ļ ėė
textareaė ęļė ė°ëĪę° formė ė ėķíė§ ėęģ 'ė·Ļė ëēíž'ė ëëŽ ëŠĻë ęļėī ė§ėė§ í ëĪė textareaę° focus ëëĐī textareaė ęļėī ėėīë 'ëęļ ëēíž'ęđė§ ėĪíėžėī ė ėĐëėī ėėëĪ.
â handleTextareaFocus íĻėėė buttonsDivę° ėė ë ėė ë§ëĪėë buttonsDivëĨž formė ėķę°í í
handleTextareaValueInput íĻė
ė ėíī value ę°ė ėĄīėŽ ė ëŽīė ë°ëž 'ëęļ ëēíž' ėĪíėžėī ė ėĐëëëĄ ėė íëĪ.
ðĄ ëęļ ėī ę°ė ėĪėę° ė ë°ėīíļ
//- watch.pug block content div.video-container(data-video=video)
// comment.js const commentContainer = document.querySelector(".comment-container"); const commentContainerCount = commentContainer.querySelector(".comment-container__count"); const addComment = (commentText) => { // ėĪëĩ commentContainerCount.textContent = `ëęļ ${ JSON.parse(videoContainer.dataset.video).comments.length + 1 // íëŽėĪ 1 íīėĪėž íĻ }ę°`; };