fictioneer/js/comments.min.js

3 lines
14 KiB
JavaScript
Raw Permalink Normal View History

2024-12-06 17:38:32 +01:00
function fcn_wrapInTag(e,t,i={}){const n=i.href?' href="'+i.href+'" target="_blank" rel="nofollow noreferrer noopener"':"",a=i.shortcode?["[","]"]:["<",">"],s=e.selectionStart,o=e.selectionEnd,r=a[0]+t+n+a[1],l=a[0]+"/"+t+a[1],c=r+e.value.substring(s,o)+l;e.value=e.value.substring(0,s)+c+e.value.substring(o,e.value.length),e.setSelectionRange(s+r.length,o+r.length),e.focus()}document.addEventListener("fcnUserDataReady",(()=>{fcn().userData().fingerprint==document.documentElement.dataset.authorFingerprint&&document.body.classList.add("is-post-author")}));const fcn_ajaxCommentForm=_$$$("ajax-comment-form-target");function fcn_setupCommentFormObserver(){const e=new IntersectionObserver((([t])=>{t.isIntersecting&&(fcn_getCommentForm(),e.disconnect())}),{rootMargin:"450px",threshold:1});e.observe(fcn_ajaxCommentForm)}function fcn_getCommentForm(){let e;FcnUtils.aGet({action:"fictioneer_ajax_get_comment_form",post_id:_$$$("comments").dataset.postId,fcn_fast_comment_ajax:1}).then((t=>{if(t.success){const e=document.createElement("div");e.innerHTML=t.data.html;const i=e.querySelector("#comment_post_ID"),n=e.querySelector("#cancel-comment-reply-link"),a=e.querySelector(".logout-link");i&&(i.value=t.data.postId),n&&(n.href="#respond"),a&&(a.href=_$$$("comments").dataset.logoutUrl),fcn_ajaxCommentForm.innerHTML=e.innerHTML,e.remove(),fcn_applyCommentStack(),_$$$("fictioneer-ajax-nonce")?.remove(),document.body.appendChild(FcnUtils.html`${t.data.nonceHtml}`)}else e=FcnUtils.buildErrorNotice(t.data.error)})).catch((t=>{e=FcnUtils.buildErrorNotice(t)})).then((()=>{fcn_ajaxCommentForm.classList.remove("comments-skeleton"),e&&(fcn_ajaxCommentForm.innerHTML="",fcn_ajaxCommentForm.appendChild(e))}))}function fcn_applyCommentStack(e=null){(e=e??_$(FcnGlobals.commentFormSelector))&&("TEXTAREA"==e.tagName?(FcnGlobals.commentStack.forEach((t=>{e.value+=t})),FcnUtils.adjustTextarea(e)):"DIV"==e.tagName&&FcnGlobals.commentStack.forEach((t=>{e.innerHTML+=t})),FcnGlobals.commentStack=[])}fcn_ajaxCommentForm&&document.addEventListener("fcnUserDataReady",(()=>{fcn_setupCommentFormObserver()})),application.register("fictioneer-comment-form",class extends Stimulus.Controller{static get targets(){return["submit","cancel","textarea","privateToggle","emailNotificationToggle","author","email","cookies","privacyPolicy"]}initialize(){this.respond=this.element.closest("#respond"),this.order=this.respond.closest(".fictioneer-comments")?.dataset.order??"desc",this.parent=this.element.querySelector("#comment_parent"),this.addJSTrap()}adjustTextarea(){FcnUtils.adjustTextarea(this.textareaTarget)}toolbarButtons(e){const t=e.target.closest("[data-bbcode]");t&&fcn_wrapInTag(this.textareaTarget,t.dataset.bbcode,{shortcode:!0})}keyComboCodes(e){if(_$(".fictioneer-comment-toolbar")&&"TEXTAREA"===document.activeElement.tagName&&(e.ctrlKey||e.metaKey)){const t=e.key.toLowerCase();if(["b","i","s","q","h","l"].includes(t)){e.preventDefault();const i={q:"quote",h:"spoiler",l:"link"};fcn_wrapInTag(document.activeElement,i[t]||t,{shortcode:!0})}}}addJSTrap(){this.element.appendChild(FcnUtils.html`
<input type="hidden" name="fictioneer_comment_validator" value="299792458">
2024-12-06 17:38:32 +01:00
`)}revealFormInputs(){this.respond.querySelectorAll(".fictioneer-respond__form-actions, .fictioneer-respond__form-bottom").forEach((e=>{e.classList.remove("hidden")}))}ajaxSubmit(e){if(Date.now()<FcnGlobals.pageLoadTimestamp+3e3)return;if(this.element.classList.remove("_invalid"),!document.documentElement.dataset.ajaxSubmit)return this.element.reportValidity()?(this.submitTarget.value=this.submitTarget.dataset.disabled,this.submitTarget.classList.add("_disabled"),void setTimeout((()=>{this.submitTarget.value=this.submitTarget.dataset.enabled,this.submitTarget.classList.remove("_disabled")}),2e3)):void this.element.classList.add("_invalid");if(e.preventDefault(),!this.element.reportValidity())return void this.element.classList.add("_invalid");const t=new FormData(this.element),i=Object.fromEntries(t.entries()),n=i.comment_parent??0,a=_$$$(`comment-${n}`),s=this.textareaTarget.value.length>1;let o=!0,r=!0;if(this.textareaTarget.classList.toggle("_error",!s),this.hasPrivacyPolicyTarget&&(r=this.privacyPolicyTarget.checked,this.privacyPolicyTarget.classList.toggle("_error",!r)),this.hasEmailTarget&&this.emailTarget?.value.length>0&&(o=/\S+@\S+\.\S+/.test(this.emailTarget.value),this.emailTarget.classList.toggle("_error",!o)),!s||!r||!o)return;this.element.classList.add("ajax-in-progress"),this.submitTarget.value=this.submitTarget.dataset.disabled,this.submitTarget.disabled=!0;const l={action:"fictioneer_ajax_submit_comment",content:this.textareaTarget.value,depth:a?parseInt(a.dataset.depth)+1:1,fcn_fast_comment_ajax:1,...i};this.hasEmailTarget&&this.emailTarget?.value&&(l.email=this.emailTarget.value),this.hasAuthorTarget&&this.authorTarget?.value&&(l.author=this.authorTarget.value),_$$$("comment-submit-error-notice")?.remove(),FcnUtils.aPost(l).then((e=>{if(e.success&&e.data?.comment){let t=_$(".commentlist"),i="insertBefore";if(t&&!a&&t.firstElementChild){let e=null;if(t.firstElementChild.classList.contains("_sticky"))for(e=t.firstElementChild,t=e,i="insertAfter";e.nextElementSibling&&e.nextElementSibling.classList.contains("_sticky");)e=t.nextElementSibling,t=e}if(t||(t=document.createElement("ol"),t.classList="fictioneer-comments__list commentlist",_$$$("comments").appendChild(t),i="append"),a&&(t=a.querySelector(".children"),i="append",!t)){const e=document.createElement("ol");a.appendChild(e),t=e}let n=document.createElement("div");switch(n.innerHTML=e.data.comment,n=n.firstChild,i){case"append":t.appendChild(n);break;case"insertBefore":t.insertBefore(n,t.firstChild);break;case"insertAfter":t.nextSibling?t.parentNode.insertBefore(n,t.nextSibling):t.parentNode.appendChild(n)}this.textareaTarget.value="",this.textareaTarget.style.height="","0"!=this.parent.value&&this.cancelTarget.click();const s=window.location.protocol+"//"+window.location.host+window.location.pathname;("desc"!=this.order||FcnGlobals.urlParams.corder)&&(FcnGlobals.urlParams.corder=this.order),e.data.commentcode&&(FcnGlobals.urlParams.commentcode=e.data.commentcode);let o=Object.entries(FcnGlobals.urlParams).map((([e,t])=>`${e}=${t}`)).join("&");""!==o&&(o=`?${o}`),history.replaceState({path:s},"",s+o+`#comment-${e.data.comment_id}`),n.scrollIntoView({behavior:"smooth"})}else this.element.insertBefore(FcnUtils.buildErrorNotice(e.data.failure??e.data.error??fictioneer_tl.notification.error,"comment-submit-error-notice",!1),this.element.firstChild),console.error("Error:",e.data.error??e.data.failure??"Unknown")})).catch((e=>{this.element.insertBefore(FcnUtils.buildErrorNotice(`${e.statusText} (${e.status})`,"comment-submit-error-notice"),this.element.firstChild)})).then((()=>{this.element.classList.remove("ajax-in-progress"),this.submitTarget.disabled=!1,this.submitTarget.value=this.submitTarget.dataset.enabled}))}togglePrivate(){this.respond.classList.toggle("_private",this.privateToggleTarget.checked)}}),application.register("fictioneer-comment",class extends Stimulus.Controller{static get targets(){return["modMenuToggle","modMenu","modIcon","editLink","flagButton","deleteButton","editButton","content","inlineEditWrapper","inlineEditTextarea","inli