2 lines
16 KiB
JavaScript
2 lines
16 KiB
JavaScript
import{j as e,r as l,e as X,v as ce}from"./react-vendor-CgzNJrV9.js";import{c as V,u as me,a as $,B as R}from"./index-474mTIgd.js";import{B as H}from"./badge-CsEfIQQ2.js";import{S as ue}from"./spinner-DH1syOsQ.js";import{n as E}from"./notify-Zesh5czd.js";import{h as de,f as he}from"./content-CKr4Ur51.js";import{r as fe,X as xe,s as ge,t as Q,u as pe,v as ve,A as je,w as be,i as Ne,x as ke}from"./ui-vendor-CdH1UOKW.js";import{r as we}from"./postContent-CNPsgNvN.js";import"./markdown-vendor-DxR1h-Bq.js";function Se({content:t,onMentionClick:s}){return e.jsx("div",{className:"floor-body",children:e.jsx("span",{dangerouslySetInnerHTML:{__html:de(t)}})})}function D(t){var s;return(s=t.user)!=null&&s.nickname?t.user.nickname:t.guest_nick?t.guest_nick:"游客"}function ye(t){return D(t)[0]||"?"}function Ce(t){return!t.user_id||t.user_id===0}function Ee(t){const s=new Map,o=[];for(const n of t)s.set(n.id,{comment:n,children:[]});for(const n of t){const u=s.get(n.id);n.reply_to&&s.has(n.reply_to)?s.get(n.reply_to).children.push(u):o.push(u)}return o}function _e(t){const s=new Date(t),o=new Date,n=u=>String(u).padStart(2,"0");return s.getFullYear()===o.getFullYear()?`${n(s.getMonth()+1)}月${n(s.getDate())}日`:`${s.getFullYear()}年${s.getMonth()+1}月${s.getDate()}日`}function Z({node:t,nested:s,highlightFloor:o,replyToId:n,onReply:u,onCancelReply:r,renderReplyBox:d}){var m,g;const i=t.comment,f=D(i),j=Ce(i),x=o===i.floor,k=!!i.content_hidden,p=n===i.id;return e.jsxs("div",{id:`floor-${i.floor}`,className:`waline-comment ${s?"nested":""} ${x?"highlight":""}`,children:[e.jsx("div",{className:`waline-comment-avatar ${j&&!((m=i.user)!=null&&m.avatar)?"guest":""}`,children:(g=i.user)!=null&&g.avatar?e.jsx("img",{src:i.user.avatar,alt:""}):ye(i)}),e.jsxs("div",{className:"waline-comment-main",children:[e.jsx("div",{className:"waline-comment-head",children:i.guest_url?e.jsx("a",{href:i.guest_url,target:"_blank",rel:"noopener noreferrer",className:"waline-comment-author",children:f}):e.jsx("span",{className:"waline-comment-author",children:f})}),k?e.jsx("div",{className:"waline-comment-private-mask",children:"该评论为私密评论,仅文章作者与评论发起者可见!"}):e.jsxs("div",{className:"waline-comment-bubble",children:[i.reply_target&&e.jsxs("span",{className:"waline-reply-at",children:["@",D(i.reply_target)]}),e.jsx(Se,{content:i.content})]}),e.jsxs("div",{className:"waline-comment-meta",children:[e.jsxs("span",{className:"waline-comment-date",children:[e.jsx(fe,{size:14}),_e(i.created_at)]}),p?e.jsxs("button",{type:"button",className:"waline-comment-reply-btn cancel",onClick:r,children:[e.jsx(xe,{size:14}),"取消"]}):e.jsxs("button",{type:"button",className:"waline-comment-reply-btn",onClick:()=>u(i),children:[e.jsx(ge,{size:14}),"回复"]})]}),p&&d&&e.jsx("div",{id:`reply-box-${i.id}`,className:"comment-box-wrap inline",children:d(i)}),t.children.length>0&&e.jsx("div",{className:"waline-replies",children:t.children.map(w=>e.jsx(Z,{node:w,nested:!0,highlightFloor:o,replyToId:n,onReply:u,onCancelReply:r,renderReplyBox:d},w.comment.id))})]})]})}function Ie({comments:t,highlightFloor:s,replyToId:o,onReply:n,onCancelReply:u,renderReplyBox:r}){const d=Ee(t);return e.jsx("div",{className:"comment-thread-list",children:d.map(i=>e.jsx(Z,{node:i,highlightFloor:s,replyToId:o,onReply:n,onCancelReply:u,renderReplyBox:r},i.comment.id))})}const ee=l.forwardRef(({className:t,...s},o)=>e.jsx(Q,{className:V("peer inline-flex h-5 w-9 shrink-0 cursor-pointer items-center rounded-full border-2 border-transparent shadow-sm transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:bg-[var(--j13-green)] data-[state=unchecked]:bg-input",t),...s,ref:o,children:e.jsx(pe,{className:V("pointer-events-none block h-4 w-4 rounded-full bg-background shadow-lg ring-0 transition-transform data-[state=checked]:translate-x-4 data-[state=unchecked]:translate-x-0")})}));ee.displayName=Q.displayName;const Me=["😀","😁","😂","🤣","😃","😄","😅","😆","😉","😊","😋","😎","😍","😘","🥰","😗","😙","😚","🙂","🤗","🤩","🤔","🤨","😐","😑","😶","🙄","😏","😣","😥","😮","🤐","😯","😪","😫","🥱","😴","😌","😛","😜","😝","🤤","😒","😓","😔","😕","🙃","🤑","😲","🙁","😖","😞","😟","😤","😢","😭","😦","😧","😨","😩","🤯","😬","😰","😱","🥵","🥶","😳","🤪","😵","🥴","😠","😡","🤬","😷","🤒","🤕","🤢","🤮","🤧","😇","🥳","🥺","🤠","🤡","🤥","🤫","🤭","🧐","🤓","😈","👻","💀","☠️","👽","👾","🤖","🎃","😺","😸","😹","😻","😼","😽","🙀","😿","😾","👋","🤚","🖐","✋","🖖","👌","🤏","✌️","🤞","🤟","🤘","🤙","👈","👉","👆","👇","☝️","👍","👎","✊","👊","🤛","🤜","👏","🙌","👐","🤲","🤝","🙏","💪","🦾","🦿","🦵","🦶","❤️","🧡","💛","💚","💙","💜","🖤","🤍","🤎","💔","❣️","💕","💞","💓","💗","💖","💘","💝","💟","☮️","✨","⭐","🌟","💫","🔥","💥","💯","✅","❌","❓","❗","💢","💤","💦","🎉","🎊","🎁","🏆","🥇","🥈"];function Le({onSelect:t}){return e.jsx("div",{className:"emoji-picker",children:Me.map(s=>e.jsx("button",{type:"button",className:"emoji-picker-item",onClick:()=>t(s),children:s},s))})}const te="j13_guest_nick",se="j13_guest_email",ne="j13_guest_url",ae="j13_my_comment_ids";function $e(){return{nick:localStorage.getItem(te)||"",email:localStorage.getItem(se)||"",url:localStorage.getItem(ne)||""}}function Re(t){localStorage.setItem(te,t.nick),localStorage.setItem(se,t.email),localStorage.setItem(ne,t.url)}function oe(){try{const t=localStorage.getItem(ae);if(!t)return[];const s=JSON.parse(t);return Array.isArray(s)?s.filter(o=>typeof o=="number"):[]}catch{return[]}}function Pe(t){const s=oe();s.includes(t)||(s.push(t),localStorage.setItem(ae,JSON.stringify(s.slice(-200))))}function W({user:t,replyTo:s,inline:o,submitting:n,submitCount:u=0,onSubmit:r,onCancelReply:d}){var y;const i=$e(),[f,j]=l.useState(""),[x,k]=l.useState(i.nick),[p,m]=l.useState(i.email),[g,w]=l.useState(i.url),[b,_]=l.useState(!1),[N,I]=l.useState(!1),S=l.useRef(null),M=l.useRef(null);l.useEffect(()=>{var c;o&&s&&((c=S.current)==null||c.focus({preventScroll:!0}))},[s==null?void 0:s.id,o]),l.useEffect(()=>{j(""),I(!1),_(!1)},[u]),l.useEffect(()=>{if(!N)return;const c=v=>{M.current&&!M.current.contains(v.target)&&I(!1)};return document.addEventListener("mousedown",c),()=>document.removeEventListener("mousedown",c)},[N]);const T=c=>{const v=S.current;if(v){const C=v.selectionStart??f.length,B=v.selectionEnd??f.length,G=f.slice(0,C)+c+f.slice(B);j(G),requestAnimationFrame(()=>{v.focus();const P=C+c.length;v.setSelectionRange(P,P)})}else j(C=>C+c)},L=()=>{const c=f.trim();c&&(!t&&!x.trim()||(t||Re({nick:x.trim(),email:p.trim(),url:g.trim()}),r({content:c,guestNick:t?void 0:x.trim(),guestEmail:t?void 0:p.trim(),guestUrl:t?void 0:g.trim(),isPrivate:b})))},z=c=>{c.key==="Enter"&&(c.ctrlKey||c.metaKey)&&(c.preventDefault(),L())},Y=((y=t==null?void 0:t.nickname)==null?void 0:y[0])||(x==null?void 0:x[0])||"?";return e.jsxs("div",{className:"comment-box",ref:M,children:[e.jsx("div",{className:"comment-box-avatar",children:t!=null&&t.avatar?e.jsx("img",{src:t.avatar,alt:"",className:"comment-box-avatar-img"}):e.jsx("div",{className:`comment-box-avatar-placeholder ${t?"":"guest"}`,children:t?Y:e.jsx("svg",{viewBox:"0 0 24 24",width:"20",height:"20",fill:"currentColor",children:e.jsx("path",{d:"M12 12c2.7 0 4.8-2.1 4.8-4.8S14.7 2.4 12 2.4 7.2 4.5 7.2 7.2 9.3 12 12 12zm0 2.4c-3.2 0-9.6 1.6-9.6 4.8v2.4h19.2v-2.4c0-3.2-6.4-4.8-9.6-4.8z"})})})}),e.jsxs("div",{className:"comment-box-main",children:[s&&!o&&e.jsxs("div",{className:"comment-box-reply-hint",children:[e.jsxs("span",{children:["回复 #",s.floor," ",D(s)]}),d&&e.jsx("button",{type:"button",className:"comment-box-reply-cancel",onClick:d,children:"取消"})]}),e.jsxs("div",{className:`comment-box-input-wrap ${b?"private-mode":""}`,children:[e.jsx("textarea",{ref:S,className:"comment-box-textarea",placeholder:b?"正在隐私评论中...":"说点什么吧",value:f,onChange:c=>j(c.target.value),onKeyDown:z,rows:3}),e.jsx("button",{type:"button",className:"comment-box-send",disabled:n||!f.trim()||!t&&!x.trim(),onClick:L,title:"发送",children:e.jsx(ve,{size:16})})]}),!t&&e.jsxs("div",{className:"comment-box-guest-fields",children:[e.jsxs("label",{className:"comment-box-guest-field",children:[e.jsxs("span",{className:"comment-box-guest-label",children:["昵称",e.jsx("em",{className:"comment-box-guest-required",children:"必填"})]}),e.jsx("input",{className:"comment-box-guest-input",placeholder:"怎么称呼你",autoComplete:"nickname",value:x,onChange:c=>k(c.target.value)})]}),e.jsxs("label",{className:"comment-box-guest-field",children:[e.jsxs("span",{className:"comment-box-guest-label",children:["邮箱",e.jsx("em",{className:"comment-box-guest-optional",children:"选填"})]}),e.jsx("input",{className:"comment-box-guest-input",placeholder:"name@example.com",type:"email",autoComplete:"email",value:p,onChange:c=>m(c.target.value)})]}),e.jsxs("label",{className:"comment-box-guest-field",children:[e.jsxs("span",{className:"comment-box-guest-label",children:["网址",e.jsx("em",{className:"comment-box-guest-optional",children:"选填"})]}),e.jsx("input",{className:"comment-box-guest-input",placeholder:"https://example.com",type:"url",autoComplete:"url",value:g,onChange:c=>w(c.target.value)})]}),e.jsx("p",{className:"comment-box-guest-hint",children:"邮箱不会公开展示,仅用于站内记录。"})]}),e.jsxs("div",{className:"comment-box-toolbar",children:[e.jsx("button",{type:"button",className:`comment-box-owo ${N?"active":""}`,onClick:()=>I(c=>!c),children:"OwO"}),e.jsxs("label",{className:"comment-box-private",children:[e.jsx(ee,{checked:b,onCheckedChange:_}),e.jsx("span",{children:"隐私评论"})]})]}),N&&e.jsx(Le,{onSelect:T})]})]})}function De({html:t,isLoggedIn:s,className:o="post-detail-content"}){const n=X(),u=l.useMemo(()=>we(t,s),[t,s]),r=l.useCallback(d=>{const i=d.target;if(i.closest("[data-members-login]")){d.preventDefault(),n("/login");return}i.closest("[data-members-register]")&&(d.preventDefault(),n("/register"))},[n]);return e.jsx("div",{className:o,onClick:r,dangerouslySetInnerHTML:{__html:u}})}function Te(t,s){const{overflowY:o}=getComputedStyle(t);return o!=="auto"&&o!=="scroll"&&o!=="overlay"||t.scrollHeight<=t.clientHeight+1?!1:s<0?t.scrollTop>0:t.scrollTop+t.clientHeight<t.scrollHeight-1}function ze(t,s,o){let n=t;for(;n&&n!==o;){if(Te(n,s))return n;n=n.parentElement}return null}function Ye(t,s=!0){l.useEffect(()=>{if(!s)return;const o=t.current;if(!o)return;const n=o.closest(".app-shell");if(!n)return;const u=r=>{const d=r.target instanceof HTMLElement?r.target:null;if(!d)return;const i=ze(d,r.deltaY,n);if(i&&i!==o&&o.contains(i)||i===o)return;const f=o.scrollTop;o.scrollTop+=r.deltaY,o.scrollTop!==f&&r.preventDefault()};return n.addEventListener("wheel",u,{passive:!1}),()=>n.removeEventListener("wheel",u)},[t,s])}function Ve(){var K,F,O,q,J;const{id:t}=ce(),s=Number(t),o=X(),{user:n,refresh:u}=me(),[r,d]=l.useState(null),[i,f]=l.useState([]),[j,x]=l.useState(!1),[k,p]=l.useState(!1),[m,g]=l.useState(null),[w,b]=l.useState(!1),[_,N]=l.useState(!0),[I,S]=l.useState(null),[M,T]=l.useState(0),L=l.useRef(null),z=l.useRef(null),Y=l.useRef(null),y=l.useRef();Ye(L,!_&&!!r);const c=l.useCallback(async()=>{const a=n?[]:oe(),h=await $.comments(s,a);return Array.isArray(h.comments)?h.comments:[]},[s,n]),v=async()=>{if(s){N(!0);try{const[a,h]=await Promise.all([$.post(s),c()]);d(a.post),x(a.liked),p(a.favorited),f(h),await u()}catch(a){E.error(a instanceof Error?a.message:"加载失败")}finally{N(!1)}}};l.useEffect(()=>{g(null),v()},[s]);const C=l.useCallback(a=>{const h=document.getElementById(`floor-${a}`);h&&(h.scrollIntoView({behavior:"smooth",block:"center"}),S(a),clearTimeout(y.current),y.current=setTimeout(()=>S(null),2e3))},[]),B=a=>{if((m==null?void 0:m.id)===a.id){g(null);return}g(a)};l.useLayoutEffect(()=>{if(!m)return;const a=document.getElementById(`reply-box-${m.id}`);a==null||a.scrollIntoView({behavior:"smooth",block:"nearest"})},[m==null?void 0:m.id]),l.useEffect(()=>()=>clearTimeout(y.current),[]);const G=async()=>{if(!n){o("/login");return}try{const a=await $.like(s);x(a.liked),d(h=>h&&{...h,like_count:a.like_count})}catch(a){E.error(a instanceof Error?a.message:"操作失败")}},P=async()=>{if(!n){o("/login");return}try{const a=await $.favorite(s);p(a.favorited),E.success(a.favorited?"已收藏":"已取消收藏")}catch(a){E.error(a instanceof Error?a.message:"操作失败")}},U={user:n,submitting:w,submitCount:M,onSubmit:async a=>{b(!0);try{const h=await $.addComment(s,{content:a.content,replyTo:m==null?void 0:m.id,guestNick:a.guestNick,guestEmail:a.guestEmail,guestUrl:a.guestUrl,isPrivate:a.isPrivate});n||Pe(h.id),g(null),T(re=>re+1),E.success("评论成功"),f(await c()),setTimeout(()=>C(h.floor),100)}catch(h){E.error(h instanceof Error?h.message:"评论失败")}finally{b(!1)}},onCancelReply:()=>g(null)};if(_)return e.jsx("div",{className:"post-detail-loading flex justify-center py-16",children:e.jsx(ue,{size:"lg"})});if(!r)return e.jsxs("div",{className:"empty-state",children:[e.jsx("p",{children:"帖子不存在"}),e.jsx(R,{variant:"outline",onClick:()=>o("/"),children:"返回首页"})]});const ie=((F=(K=r.user)==null?void 0:K.nickname)==null?void 0:F[0])||"?",A=((O=r.tags)==null?void 0:O.split(/[,,]/).map(a=>a.trim()).filter(Boolean))??[],le=n&&(n.role==="admin"||n.id===r.user_id);return e.jsxs("div",{className:"page-wrap post-detail-page",ref:L,children:[e.jsxs("div",{className:"post-detail-header",children:[e.jsxs("div",{className:"post-detail-nav",children:[e.jsxs(R,{variant:"ghost",size:"sm",onClick:()=>o(-1),children:[e.jsx(je,{}),"返回"]}),r.board&&e.jsx(H,{variant:"green",className:"post-detail-board-tag",children:r.board.name})]}),e.jsxs("div",{className:"post-detail-head",children:[e.jsxs("h1",{className:"post-detail-title",children:[r.pinned&&e.jsx(H,{variant:"orange",className:"mr-2 align-middle",children:"置顶"}),r.title]}),e.jsxs("div",{className:"post-detail-author-row",children:[e.jsx("div",{className:"post-avatar post-avatar-lg",children:(q=r.user)!=null&&q.avatar?e.jsx("img",{src:r.user.avatar,alt:""}):ie}),e.jsxs("div",{className:"post-detail-author-info",children:[e.jsx("span",{className:"post-detail-author-name",children:(J=r.user)==null?void 0:J.nickname}),e.jsxs("span",{className:"post-detail-meta-line",children:[he(r.created_at)," · ",r.view_count," 次浏览"]})]})]})]}),A.length>0&&e.jsx("div",{className:"post-detail-tags",children:A.map(a=>e.jsx(H,{variant:"secondary",children:a},a))}),e.jsx(De,{html:r.content||"",isLoggedIn:!!n}),e.jsxs("div",{className:"post-detail-actions",children:[e.jsxs(R,{variant:j?"default":"outline",size:"sm",onClick:G,children:[e.jsx(be,{}),"点赞 ",r.like_count]}),e.jsxs(R,{variant:k?"default":"outline",size:"sm",onClick:P,children:[e.jsx(Ne,{}),k?"已收藏":"收藏"]}),le&&e.jsxs(R,{variant:"outline",size:"sm",onClick:()=>o(`/post/${s}/edit`),children:[e.jsx(ke,{}),"编辑"]})]})]}),e.jsxs("div",{className:"comment-section",ref:z,children:[e.jsxs("div",{className:"comment-section-bar",children:[e.jsx("span",{className:"comment-section-title",children:"评论区"}),e.jsxs("span",{className:"comment-section-count",children:[i.length," 条评论"]})]}),!m&&e.jsx("div",{className:"comment-box-wrap",ref:Y,children:e.jsx(W,{...U})}),e.jsx("div",{className:"comment-list-area",children:i.length===0&&!m?e.jsxs("div",{className:"comment-empty",children:[e.jsx("div",{className:"comment-empty-icon",children:"💬"}),e.jsx("p",{children:"暂无评论,来抢沙发吧"})]}):e.jsx(Ie,{comments:i,highlightFloor:I,replyToId:(m==null?void 0:m.id)??null,onReply:B,onCancelReply:()=>g(null),renderReplyBox:a=>e.jsx(W,{...U,replyTo:a,inline:!0},a.id)})})]})]})}export{Ve as default};
|