From df0601c79718cb74d3807219e6ab9af9e90a95b8 Mon Sep 17 00:00:00 2001 From: Tetrakern <26898880+Tetrakern@users.noreply.github.com> Date: Mon, 19 Jun 2023 03:42:36 +0200 Subject: [PATCH] Optimize commend inline edit Use template instead of putting the whole form into each comment. --- comments.php | 18 ++++++++++++++++++ .../functions/comments/_comments_threads.php | 10 ---------- js/comments.min.js | 2 +- src/js/comments.js | 7 +++++++ 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/comments.php b/comments.php index 8c059f01..6fa1ca2f 100644 --- a/comments.php +++ b/comments.php @@ -78,6 +78,24 @@ $logout_url = fictioneer_get_logout_url( get_permalink() ); ?> + + {if(e.success)switch(e.data.operation){case"sticky":n.classList.add("_sticky");break;case"unsticky":n.classList.remove("_sticky");break;case"approve":n.classList.remove("_unapproved");break;case"unapprove":n.classList.add("_unapproved");break;case"open":n.classList.remove("_closed");break;case"close":n.classList.add("_closed");break;case"trash":case"spam":n.style.overflow="hidden",n.style.height="0",n.style.margin="0",n.style.opacity="0"}else o.classList="fa-solid fa-triangle-exclamation mod-menu-toggle-icon",o.style.color="var(--warning)",n.querySelector(".popup-menu-toggle").style.opacity="1",e.data.error&&fcn_showNotification(e.data.error,5,"warning")})).catch((e=>{o.classList="fa-solid fa-triangle-exclamation mod-menu-toggle-icon",o.style.color="var(--warning)",n.querySelector(".popup-menu-toggle").style.opacity="1",e.status&&e.statusText?fcn_showNotification(`${e.status}: ${e.statusText}`,5,"warning"):e&&fcn_showNotification(e,5,"warning")})).then((()=>{n.classList.remove("ajax-in-progress"),fcn_lastClicked&&fcn_lastClicked.classList.remove("last-clicked"),fcn_lastClicked=null})))}function fcn_addModerationEvents(){_$$(".button-ajax-moderate-comment").forEach((e=>{e.addEventListener("click",(e=>{fcn_moderateComment(e.currentTarget.dataset.id,e.currentTarget.dataset.action)}))}))}function fcn_addCommentMouseleaveEvents(){_$$(".fictioneer-comment__container").forEach((e=>{e.addEventListener("mouseleave",(e=>{fcn_lastClicked&&fcn_lastClicked.classList.remove("last-clicked"),fcn_lastClicked=null,e.stopPropagation()}))}))}function fcn_flagComment(e){if(!fcn_isLoggedIn)return;const t=e.closest(".fictioneer-comment"),n=t.querySelector(".fictioneer-report-comment-button");t.classList.contains("ajax-in-progress")||(t.classList.add("ajax-in-progress"),fcn_ajaxPost({action:"fictioneer_ajax_report_comment",id:t.dataset.id,dubious:n.classList.contains("_dubious")}).then((e=>{e.success?(n.classList.toggle("on",e.data.flagged),n.classList.remove("_dubious"),e.data.resync&&fcn_showNotification(e.data.resync)):e.data?.error&&fcn_showNotification(e.data.error,5,"warning")})).catch((e=>{e.status&&e.statusText&&fcn_showNotification(`${e.status}: ${e.statusText}`,5,"warning")})).then((()=>{t.classList.remove("ajax-in-progress")})))}function fcn_revealCommentFormInputs(e){e.closest("form").querySelectorAll(".fictioneer-respond__form-actions, .fictioneer-respond__form-bottom").forEach((e=>{e.classList.remove("hidden")})),e.removeAttribute("onclick")}function fcn_addCommentFormEvents(){_$$$("comment")?.addEventListener("focus",(e=>{fcn_revealCommentFormInputs(e.currentTarget)}),{once:!0})}function fcn_textareaAdjust(e){e.style.height="auto",e.style.height=e.scrollHeight+"px"}function fcn_addTextareaEvents(){_$$(".adaptive-textarea").forEach((e=>{e.addEventListener("input",(e=>{fcn_textareaAdjust(e.currentTarget)}))}))}function fcn_addPrivateToggleEvents(){_$$$("fictioneer-private-comment-toggle")?.addEventListener("change",(e=>{_$$$("respond")?.classList.toggle("_private",e.currentTarget.checked)}))}function fcn_wrapInTag(e,t,n={}){const o=n.href?' href="'+n.href+'" target="_blank" rel="nofollow noreferrer noopener"':"",c=n.shortcode?["[","]"]:["<",">"],a=e.selectionStart,i=e.selectionEnd,s=c[0]+t+o+c[1],r=c[0]+"/"+t+c[1],m=s+e.value.substring(a,i)+r;e.value=e.value.substring(0,a)+m+e.value.substring(i,e.value.length),e.setSelectionRange(a+s.length,i+s.length),e.focus()}function fcn_bindAJAXCommentSubmit(){fcn_theRoot.dataset.ajaxSubmit&&"true"==fcn_theRoot.dataset.ajaxSubmit&&_$$$("commentform")?.addEventListener("submit",(e=>{if(e.preventDefault(),Date.now()1,o.classList.toggle("_error",!u),s&&(g=s.checked),s?.classList.toggle("_error",!g),a&&a.value.length>0&&(_=/\S+@\S+\.\S+/.test(a.value)),a?.classList.toggle("_error",!_),!u||!g||!_)return!1;t.classList.add("ajax-in-progress"),n.disabled=!0,n.value=n.dataset.disabled;const p={action:"fictioneer_ajax_submit_comment",post_id:_$$$("comment_post_ID").value,content:o.value,private_comment:l?.checked??0,notification:f?.checked??0,cookie_consent:i?.checked??0,privacy_consent:s?.checked??0,unfiltered_html:_$$$("_wp_unfiltered_html_comment_disabled")?.value??"",depth:d?parseInt(d.dataset.depth)+1:1,fictioneer_comment_validator:r?.value??0};m&&(p.parent_id=m),a?.value&&(p.email=a?.value),c?.value&&(p.author=c?.value),fcn_ajaxPost(p).then((e=>{if(_$$$("comment-submit-error-notice")?.remove(),e.success&&e.data?.comment){let t=_$(".commentlist"),n="insertBefore";if(t&&!d&&t.firstElementChild){let e=null;if(t.firstElementChild.classList.contains("_sticky"))for(e=t.firstElementChild,t=e,n="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),n="append"),d&&(t=d.querySelector(".children"),n="append",!t)){const e=document.createElement("ol");d.appendChild(e),t=e}let c=document.createElement("div");switch(c.innerHTML=e.data.comment,c=c.firstChild,n){case"append":t.appendChild(c);break;case"insertBefore":t.insertBefore(c,t.firstChild);break;case"insertAfter":t.nextSibling?t.parentNode.insertBefore(c,t.nextSibling):t.parentNode.appendChild(c)}fcn_addModerationEvents(),fcn_addCommentMouseleaveEvents(),"0"!=_$$$("comment_parent").value&&_$$$("cancel-comment-reply-link").click(),o.value="",o.style.height="";const a=window.location.protocol+"//"+window.location.host+window.location.pathname;let i="";e.data.commentcode&&(i+=`?commentcode=${e.data.commentcode}`),history.pushState({path:a},"",a+i+`#comment-${e.data.comment_id}`),c.scrollIntoView({behavior:"smooth"})}else t.insertBefore(fcn_buildErrorNotice(e.data?.error??__("Error","fictioneer"),"comment-submit-error-notice"),t.firstChild)})).catch((e=>{_$$$("comment-submit-error-notice")?.remove(),t.insertBefore(fcn_buildErrorNotice(`${e.status}: ${e.statusText}`,"comment-submit-error-notice"),t.firstChild)})).then((()=>{t.classList.remove("ajax-in-progress"),n.disabled=!1,n.value=n.dataset.enabled}))}))}fcn_addJSTrap(),fcn_addModerationEvents(),fcn_addCommentMouseleaveEvents(),fcn_addCommentFormEvents(),fcn_addTextareaEvents(),fcn_addPrivateToggleEvents(),_$(".comment-section")?.addEventListener("click",(e=>{const t=e.target.closest("[data-bbcode]");t&&fcn_wrapInTag(_$$$("comment"),t.dataset.bbcode,{shortcode:!0})})),fcn_bindAJAXCommentSubmit();var fcn_commentEditUndos={};function fcn_triggerInlineCommentEdit(e){const t=e.closest(".fictioneer-comment");if(t){const e=t.querySelector(".fictioneer-comment__content"),n=t.querySelector(".fictioneer-comment__edit"),o=t.querySelector(".comment-inline-edit-content");fcn_commentEditUndos[t.id]=o.value,t.classList.add("_editing"),e.hidden=!0,n.hidden=!1,o.style.height=o.scrollHeight+"px"}}function fcn_submitInlineCommentEdit(e){const t=e.closest(".fictioneer-comment"),n=t.querySelector(".fictioneer-comment__edit"),o=t.querySelector(".comment-inline-edit-content").value;let c=t.querySelector(".fictioneer-comment__edit-note");o!=fcn_commentEditUndos[t.id]?t&&(n.classList.add("ajax-in-progress"),e.innerHTML=e.dataset.disabled,e.disabled=!0,fcn_ajaxPost({action:"fictioneer_ajax_edit_comment",comment_id:t.id.replace("comment-",""),content:o}).then((e=>{if(e.success){const n=t.querySelector(".fictioneer-comment__content");n.innerHTML=e.data.content,fcn_restoreComment(t,!1,e.data.raw),c||(c=document.createElement("div")),c.classList.add("fictioneer-comment__edit-note"),c.innerHTML=e.data.edited,n.parentNode.appendChild(c)}else fcn_restoreComment(t,!0),e.data?.error&&fcn_showNotification(e.data.error,5,"warning")})).catch((e=>{fcn_restoreComment(t,!0),e.status&&e.statusText&&fcn_showNotification(`${e.status}: ${e.statusText}`,5,"warning")})).then((()=>{n.classList.remove("ajax-in-progress"),e.innerHTML=e.dataset.enabled,e.disabled=!1}))):fcn_restoreComment(t,!0)}function fcn_cancelInlineCommentEdit(e){const t=e.closest(".fictioneer-comment");t&&fcn_restoreComment(t,!0)}function fcn_restoreComment(e,t=!1,n=null){e.querySelector(".fictioneer-comment__content").hidden=!1,e.querySelector(".fictioneer-comment__edit").hidden=!0,e.classList.remove("_editing"),t&&fcn_commentEditUndos[e.id]?e.querySelector(".comment-inline-edit-content").value=fcn_commentEditUndos[e.id]:n&&(e.querySelector(".comment-inline-edit-content").value=n)}function fcn_revealEditButton(){let e=parseInt(fcn_theRoot.dataset.editTime);e&&(e=e>0?6e4*e:e,_$$(".fictioneer-comment[data-fingerprint]").forEach((t=>{if(fcn_matchFingerprint(t.dataset.fingerprint)){if(e>0&&parseInt(t.dataset.timestamp)+e{if(fcn_matchFingerprint(e.dataset.fingerprint)){const t=e.querySelector(".fictioneer-comment__delete");t&&(t.hidden=!1)}}))}function fcn_deleteMyComment(e){if(!fcn_isLoggedIn)return;const t=prompt(e.dataset.dialogMessage);if(!t||t.toLowerCase()!=e.dataset.dialogConfirm.toLowerCase())return;const n=e.closest(".fictioneer-comment");n.classList.contains("ajax-in-progress")||(n.classList.add("ajax-in-progress"),fcn_ajaxPost(payload={action:"fictioneer_ajax_delete_my_comment",comment_id:n.dataset.id}).then((e=>{e.success?(n.classList.add("_deleted"),n.querySelector(".fictioneer-comment__container").innerHTML=e.data.html):e.data.error&&fcn_showNotification(e.data.error,5,"warning")})).catch((e=>{e.status&&e.statusText&&fcn_showNotification(`${e.status}: ${e.statusText}`,5,"warning")})).then((()=>{n.classList.remove("ajax-in-progress")})))}fcn_isLoggedIn&&fcn_revealEditButton(),fcn_isLoggedIn&&fcn_revealDeleteButton();const fcn_ajaxCommentForm=_$$$("ajax-comment-form-target");function fcn_getCommentForm(){let e;fcn_ajaxGet({action:"fictioneer_ajax_get_comment_form",post_id:_$$$("comments").dataset.postId}).then((t=>{if(t.success){const e=document.createElement("div");e.innerHTML=t.data.html;const n=e.querySelector("#comment_post_ID"),o=e.querySelector("#cancel-comment-reply-link"),c=e.querySelector(".logout-link");n&&(n.value=t.data.postId),o&&(o.href="#respond"),c&&(c.href=_$$$("comments").dataset.logoutUrl),fcn_ajaxCommentForm.innerHTML=e.innerHTML,e.remove(),fcn_addTextareaEvents(),fcn_addCommentFormEvents(),fcn_addPrivateToggleEvents(),fcn_theRoot.dataset.ajaxSubmit&&fcn_bindAJAXCommentSubmit(),fcn_addJSTrap()}else e=fcn_buildErrorNotice(t.data.error)})).catch((t=>{e=fcn_buildErrorNotice(t)})).then((()=>{fcn_ajaxCommentForm.classList.remove("comments-skeleton"),e&&(fcn_ajaxCommentForm.innerHTML="",fcn_ajaxCommentForm.appendChild(e))}))}fcn_ajaxCommentForm&&(fcn_theRoot.dataset.ajaxNonce&&!_$$$("fictioneer-ajax-nonce")?fcn_theRoot.addEventListener("nonceReady",(()=>{fcn_getCommentForm()})):fcn_getCommentForm()),_$(".fictioneer-comments")?.addEventListener("click",(e=>{const t=e.target.closest("[data-click]"),n=t?.dataset.click;if(n)switch(n){case"submit-inline-comment-edit":fcn_submitInlineCommentEdit(t);break;case"cancel-inline-comment-edit":fcn_cancelInlineCommentEdit(t);break;case"trigger-inline-comment-edit":fcn_triggerInlineCommentEdit(t);break;case"delete-my-comment":fcn_deleteMyComment(t);break;case"flag-comment":fcn_flagComment(t)}})); \ No newline at end of file +function fcn_addJSTrap(){const e=document.querySelector(".comment-form");if(e){const t=document.createElement("input");t.setAttribute("type","hidden"),t.id="fictioneer-comment-validator",t.setAttribute("name","fictioneer_comment_validator"),t.setAttribute("value","299792458"),e.appendChild(t)}}function fcn_moderateComment(e,t){const n=_$$$(`comment-${e}`),o=n.querySelector(".mod-menu-toggle-icon");n.classList.contains("ajax-in-progress")||(n.classList.add("ajax-in-progress"),"trash"!=t&&"spam"!=t||(n.style.height=n.clientHeight+"px"),fcn_ajaxPost({action:"fictioneer_ajax_moderate_comment",operation:t,id:e}).then((e=>{if(e.success)switch(e.data.operation){case"sticky":n.classList.add("_sticky");break;case"unsticky":n.classList.remove("_sticky");break;case"approve":n.classList.remove("_unapproved");break;case"unapprove":n.classList.add("_unapproved");break;case"open":n.classList.remove("_closed");break;case"close":n.classList.add("_closed");break;case"trash":case"spam":n.style.overflow="hidden",n.style.height="0",n.style.margin="0",n.style.opacity="0"}else o.classList="fa-solid fa-triangle-exclamation mod-menu-toggle-icon",o.style.color="var(--warning)",n.querySelector(".popup-menu-toggle").style.opacity="1",e.data.error&&fcn_showNotification(e.data.error,5,"warning")})).catch((e=>{o.classList="fa-solid fa-triangle-exclamation mod-menu-toggle-icon",o.style.color="var(--warning)",n.querySelector(".popup-menu-toggle").style.opacity="1",e.status&&e.statusText?fcn_showNotification(`${e.status}: ${e.statusText}`,5,"warning"):e&&fcn_showNotification(e,5,"warning")})).then((()=>{n.classList.remove("ajax-in-progress"),fcn_lastClicked&&fcn_lastClicked.classList.remove("last-clicked"),fcn_lastClicked=null})))}function fcn_addModerationEvents(){_$$(".button-ajax-moderate-comment").forEach((e=>{e.addEventListener("click",(e=>{fcn_moderateComment(e.currentTarget.dataset.id,e.currentTarget.dataset.action)}))}))}function fcn_addCommentMouseleaveEvents(){_$$(".fictioneer-comment__container").forEach((e=>{e.addEventListener("mouseleave",(e=>{fcn_lastClicked&&fcn_lastClicked.classList.remove("last-clicked"),fcn_lastClicked=null,e.stopPropagation()}))}))}function fcn_flagComment(e){if(!fcn_isLoggedIn)return;const t=e.closest(".fictioneer-comment"),n=t.querySelector(".fictioneer-report-comment-button");t.classList.contains("ajax-in-progress")||(t.classList.add("ajax-in-progress"),fcn_ajaxPost({action:"fictioneer_ajax_report_comment",id:t.dataset.id,dubious:n.classList.contains("_dubious")}).then((e=>{e.success?(n.classList.toggle("on",e.data.flagged),n.classList.remove("_dubious"),e.data.resync&&fcn_showNotification(e.data.resync)):e.data?.error&&fcn_showNotification(e.data.error,5,"warning")})).catch((e=>{e.status&&e.statusText&&fcn_showNotification(`${e.status}: ${e.statusText}`,5,"warning")})).then((()=>{t.classList.remove("ajax-in-progress")})))}function fcn_revealCommentFormInputs(e){e.closest("form").querySelectorAll(".fictioneer-respond__form-actions, .fictioneer-respond__form-bottom").forEach((e=>{e.classList.remove("hidden")})),e.removeAttribute("onclick")}function fcn_addCommentFormEvents(){_$$$("comment")?.addEventListener("focus",(e=>{fcn_revealCommentFormInputs(e.currentTarget)}),{once:!0})}function fcn_textareaAdjust(e){e.style.height="auto",e.style.height=e.scrollHeight+"px"}function fcn_addTextareaEvents(){_$$(".adaptive-textarea").forEach((e=>{e.addEventListener("input",(e=>{fcn_textareaAdjust(e.currentTarget)}))}))}function fcn_addPrivateToggleEvents(){_$$$("fictioneer-private-comment-toggle")?.addEventListener("change",(e=>{_$$$("respond")?.classList.toggle("_private",e.currentTarget.checked)}))}function fcn_wrapInTag(e,t,n={}){const o=n.href?' href="'+n.href+'" target="_blank" rel="nofollow noreferrer noopener"':"",c=n.shortcode?["[","]"]:["<",">"],a=e.selectionStart,i=e.selectionEnd,s=c[0]+t+o+c[1],r=c[0]+"/"+t+c[1],m=s+e.value.substring(a,i)+r;e.value=e.value.substring(0,a)+m+e.value.substring(i,e.value.length),e.setSelectionRange(a+s.length,i+s.length),e.focus()}function fcn_bindAJAXCommentSubmit(){fcn_theRoot.dataset.ajaxSubmit&&"true"==fcn_theRoot.dataset.ajaxSubmit&&_$$$("commentform")?.addEventListener("submit",(e=>{if(e.preventDefault(),Date.now()1,o.classList.toggle("_error",!u),s&&(g=s.checked),s?.classList.toggle("_error",!g),a&&a.value.length>0&&(_=/\S+@\S+\.\S+/.test(a.value)),a?.classList.toggle("_error",!_),!u||!g||!_)return!1;t.classList.add("ajax-in-progress"),n.disabled=!0,n.value=n.dataset.disabled;const p={action:"fictioneer_ajax_submit_comment",post_id:_$$$("comment_post_ID").value,content:o.value,private_comment:l?.checked??0,notification:f?.checked??0,cookie_consent:i?.checked??0,privacy_consent:s?.checked??0,unfiltered_html:_$$$("_wp_unfiltered_html_comment_disabled")?.value??"",depth:d?parseInt(d.dataset.depth)+1:1,fictioneer_comment_validator:r?.value??0};m&&(p.parent_id=m),a?.value&&(p.email=a?.value),c?.value&&(p.author=c?.value),fcn_ajaxPost(p).then((e=>{if(_$$$("comment-submit-error-notice")?.remove(),e.success&&e.data?.comment){let t=_$(".commentlist"),n="insertBefore";if(t&&!d&&t.firstElementChild){let e=null;if(t.firstElementChild.classList.contains("_sticky"))for(e=t.firstElementChild,t=e,n="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),n="append"),d&&(t=d.querySelector(".children"),n="append",!t)){const e=document.createElement("ol");d.appendChild(e),t=e}let c=document.createElement("div");switch(c.innerHTML=e.data.comment,c=c.firstChild,n){case"append":t.appendChild(c);break;case"insertBefore":t.insertBefore(c,t.firstChild);break;case"insertAfter":t.nextSibling?t.parentNode.insertBefore(c,t.nextSibling):t.parentNode.appendChild(c)}fcn_addModerationEvents(),fcn_addCommentMouseleaveEvents(),"0"!=_$$$("comment_parent").value&&_$$$("cancel-comment-reply-link").click(),o.value="",o.style.height="";const a=window.location.protocol+"//"+window.location.host+window.location.pathname;let i="";e.data.commentcode&&(i+=`?commentcode=${e.data.commentcode}`),history.pushState({path:a},"",a+i+`#comment-${e.data.comment_id}`),c.scrollIntoView({behavior:"smooth"})}else t.insertBefore(fcn_buildErrorNotice(e.data?.error??__("Error","fictioneer"),"comment-submit-error-notice"),t.firstChild)})).catch((e=>{_$$$("comment-submit-error-notice")?.remove(),t.insertBefore(fcn_buildErrorNotice(`${e.status}: ${e.statusText}`,"comment-submit-error-notice"),t.firstChild)})).then((()=>{t.classList.remove("ajax-in-progress"),n.disabled=!1,n.value=n.dataset.enabled}))}))}fcn_addJSTrap(),fcn_addModerationEvents(),fcn_addCommentMouseleaveEvents(),fcn_addCommentFormEvents(),fcn_addTextareaEvents(),fcn_addPrivateToggleEvents(),_$(".comment-section")?.addEventListener("click",(e=>{const t=e.target.closest("[data-bbcode]");t&&fcn_wrapInTag(_$$$("comment"),t.dataset.bbcode,{shortcode:!0})})),fcn_bindAJAXCommentSubmit();const fcn_commentEditActionsTemplate=_$(".comment-edit-actions-template");var fcn_commentEditUndos={};function fcn_triggerInlineCommentEdit(e){const t=e.closest(".fictioneer-comment");if(t){const e=t.querySelector(".fictioneer-comment__content"),n=t.querySelector(".fictioneer-comment__edit"),o=t.querySelector(".comment-inline-edit-content");n.appendChild(fcn_commentEditActionsTemplate.content.cloneNode(!0)),fcn_commentEditUndos[t.id]=o.value,t.classList.add("_editing"),e.hidden=!0,n.hidden=!1,o.style.height=o.scrollHeight+"px"}}function fcn_submitInlineCommentEdit(e){const t=e.closest(".fictioneer-comment"),n=t.querySelector(".fictioneer-comment__edit"),o=t.querySelector(".comment-inline-edit-content").value;let c=t.querySelector(".fictioneer-comment__edit-note");o!=fcn_commentEditUndos[t.id]?t&&(n.classList.add("ajax-in-progress"),e.innerHTML=e.dataset.disabled,e.disabled=!0,fcn_ajaxPost({action:"fictioneer_ajax_edit_comment",comment_id:t.id.replace("comment-",""),content:o}).then((e=>{if(e.success){const n=t.querySelector(".fictioneer-comment__content");n.innerHTML=e.data.content,fcn_restoreComment(t,!1,e.data.raw),c||(c=document.createElement("div")),c.classList.add("fictioneer-comment__edit-note"),c.innerHTML=e.data.edited,n.parentNode.appendChild(c)}else fcn_restoreComment(t,!0),e.data?.error&&fcn_showNotification(e.data.error,5,"warning")})).catch((e=>{fcn_restoreComment(t,!0),e.status&&e.statusText&&fcn_showNotification(`${e.status}: ${e.statusText}`,5,"warning")})).then((()=>{n.classList.remove("ajax-in-progress"),e.innerHTML=e.dataset.enabled,e.disabled=!1}))):fcn_restoreComment(t,!0)}function fcn_cancelInlineCommentEdit(e){const t=e.closest(".fictioneer-comment");t&&fcn_restoreComment(t,!0)}function fcn_restoreComment(e,t=!1,n=null){e.querySelector(".fictioneer-comment__content").hidden=!1,e.querySelector(".fictioneer-comment__edit").hidden=!0,e.querySelector(".fictioneer-comment__edit-actions")?.remove(),e.classList.remove("_editing"),t&&fcn_commentEditUndos[e.id]?e.querySelector(".comment-inline-edit-content").value=fcn_commentEditUndos[e.id]:n&&(e.querySelector(".comment-inline-edit-content").value=n)}function fcn_revealEditButton(){let e=parseInt(fcn_theRoot.dataset.editTime);e&&(e=e>0?6e4*e:e,_$$(".fictioneer-comment[data-fingerprint]").forEach((t=>{if(fcn_matchFingerprint(t.dataset.fingerprint)){if(e>0&&parseInt(t.dataset.timestamp)+e{if(fcn_matchFingerprint(e.dataset.fingerprint)){const t=e.querySelector(".fictioneer-comment__delete");t&&(t.hidden=!1)}}))}function fcn_deleteMyComment(e){if(!fcn_isLoggedIn)return;const t=prompt(e.dataset.dialogMessage);if(!t||t.toLowerCase()!=e.dataset.dialogConfirm.toLowerCase())return;const n=e.closest(".fictioneer-comment");n.classList.contains("ajax-in-progress")||(n.classList.add("ajax-in-progress"),fcn_ajaxPost(payload={action:"fictioneer_ajax_delete_my_comment",comment_id:n.dataset.id}).then((e=>{e.success?(n.classList.add("_deleted"),n.querySelector(".fictioneer-comment__container").innerHTML=e.data.html):e.data.error&&fcn_showNotification(e.data.error,5,"warning")})).catch((e=>{e.status&&e.statusText&&fcn_showNotification(`${e.status}: ${e.statusText}`,5,"warning")})).then((()=>{n.classList.remove("ajax-in-progress")})))}fcn_isLoggedIn&&fcn_revealEditButton(),fcn_isLoggedIn&&fcn_revealDeleteButton();const fcn_ajaxCommentForm=_$$$("ajax-comment-form-target");function fcn_getCommentForm(){let e;fcn_ajaxGet({action:"fictioneer_ajax_get_comment_form",post_id:_$$$("comments").dataset.postId}).then((t=>{if(t.success){const e=document.createElement("div");e.innerHTML=t.data.html;const n=e.querySelector("#comment_post_ID"),o=e.querySelector("#cancel-comment-reply-link"),c=e.querySelector(".logout-link");n&&(n.value=t.data.postId),o&&(o.href="#respond"),c&&(c.href=_$$$("comments").dataset.logoutUrl),fcn_ajaxCommentForm.innerHTML=e.innerHTML,e.remove(),fcn_addTextareaEvents(),fcn_addCommentFormEvents(),fcn_addPrivateToggleEvents(),fcn_theRoot.dataset.ajaxSubmit&&fcn_bindAJAXCommentSubmit(),fcn_addJSTrap()}else e=fcn_buildErrorNotice(t.data.error)})).catch((t=>{e=fcn_buildErrorNotice(t)})).then((()=>{fcn_ajaxCommentForm.classList.remove("comments-skeleton"),e&&(fcn_ajaxCommentForm.innerHTML="",fcn_ajaxCommentForm.appendChild(e))}))}fcn_ajaxCommentForm&&(fcn_theRoot.dataset.ajaxNonce&&!_$$$("fictioneer-ajax-nonce")?fcn_theRoot.addEventListener("nonceReady",(()=>{fcn_getCommentForm()})):fcn_getCommentForm()),_$(".fictioneer-comments")?.addEventListener("click",(e=>{const t=e.target.closest("[data-click]"),n=t?.dataset.click;if(n)switch(n){case"submit-inline-comment-edit":fcn_submitInlineCommentEdit(t);break;case"cancel-inline-comment-edit":fcn_cancelInlineCommentEdit(t);break;case"trigger-inline-comment-edit":fcn_triggerInlineCommentEdit(t);break;case"delete-my-comment":fcn_deleteMyComment(t);break;case"flag-comment":fcn_flagComment(t)}})); \ No newline at end of file diff --git a/src/js/comments.js b/src/js/comments.js index f90b03e9..c77719d6 100644 --- a/src/js/comments.js +++ b/src/js/comments.js @@ -564,6 +564,8 @@ fcn_bindAJAXCommentSubmit(); // AJAX INLINE COMMENT EDIT // ============================================================================= +const /** @const {HTMLElement} */ fcn_commentEditActionsTemplate = _$('.comment-edit-actions-template'); + var /** @type {Object} */ fcn_commentEditUndos = {}; /** @@ -588,6 +590,9 @@ function fcn_triggerInlineCommentEdit(source) { edit = red.querySelector('.fictioneer-comment__edit'), textarea = red.querySelector('.comment-inline-edit-content'); + // Append buttons + edit.appendChild(fcn_commentEditActionsTemplate.content.cloneNode(true)); + // Save unedited content fcn_commentEditUndos[red.id] = textarea.value; @@ -698,7 +703,9 @@ function fcn_cancelInlineCommentEdit(source) { function fcn_restoreComment(target, undo = false, update = null) { target.querySelector('.fictioneer-comment__content').hidden = false; target.querySelector('.fictioneer-comment__edit').hidden = true; + target.querySelector('.fictioneer-comment__edit-actions')?.remove(); target.classList.remove('_editing'); + if (undo && fcn_commentEditUndos[target.id]) { target.querySelector('.comment-inline-edit-content').value = fcn_commentEditUndos[target.id]; } else if (update) {