typecho-core
Version:
An element-drived engine working well with XHR-intensive front-end. Originally designed for Typecho.
1 lines • 4.16 kB
JavaScript
function gReExpired(){TypechoCore.Element.gCaptchaCallback(!1)}function gReSuccess(){TypechoCore.Element.gCaptchaCallback(!0)}!function(e,t){const o=TypechoCore.state("citizenRoot"),c=[o+"discussion/commentEditor.js"];let n="https://registry.npmmirror.com/typecho-core/latest/files/citizen/carol/carolExtends.min.js";TypechoCore.Element.discussion=function(e){const o=t.querySelector("[data-comment-title]"),c=t.querySelector("[data-comment-form]"),n=t.querySelector("[data-comment-submit]"),r=o?.innerText||"",s=t.createElement("input");s.setAttribute("type","hidden"),s.setAttribute("name","parent");const i=t.createElement("button");i.innerText="取消回复",i.classList.add("p-button","p-button--destructive"),i.addEventListener("click",(()=>{o.innerText=r,s.remove(),TypechoCore.ui.detach(i)}));const a=e.querySelectorAll("[data-comment-reply]");for(const t of a)t.addEventListener("click",(()=>{TypechoCore.utils.scrollTo(e);const r=t.dataset.commentReply,a=t.closest(".comment-item").id,l=`@${r}:`,d=a.lastIndexOf("-"),m=a.substring(d+1,a.length);s.setAttribute("value",m),c.appendChild(s),o.innerText=l,TypechoCore.ui.detach(i),n.before(i)}))},TypechoCore.Element.discussionCaptcha=async function(o){const c=t.querySelector(".g-recaptcha");c.innerHTML="";const n=TypechoCore.engine.isResolved("api.js");await TypechoCore.engine.requestResource("https://recaptcha.net/recaptcha/api.js");try{n&&e.grecaptcha.render(c)}catch(e){}TypechoCore.ui.switchable(o,c),TypechoCore.xhr.registerOnce((function(){const e=t.querySelector(".g-recaptcha-bubble-arrow");e&&e.parentElement.remove()}),"send")},TypechoCore.Callback.discussionCallback=function(e,o){if(!e||!e.success)return TypechoCore.Element.gCaptchaCallback(!1);const c=e.parentId,n="comment-list",r="."+n;let s=t.getElementById("comments"),i="DESC"===s.getAttribute("data-order"),a=e.content;s.querySelector("textarea").value="";const l=TypechoCore.State.commentEditorInstance;l&&l.setData("");const d=s.querySelector(r);d?c?(s=t.getElementById("comment-"+c),i=!1):s=d:(a='<ol class="'+n+'">'+a+"</ol>",i=!1);const m=i?0:1,u=TypechoCore.ui.setupHtmlInsert(s,a,m,!0,"comment-new");u.querySelectorAll("[data-comment-reply] > *").forEach((e=>{e.style.pointerEvents="none"})),u.querySelectorAll("[data-comment-reply]").forEach((e=>{e.setAttribute("title","你刚刚发布这条评论。在重新加载此页面之前,还不能对它本身进行回复。"),e.style.cursor="not-allowed"}))},TypechoCore.Element.gCaptchaCallback=function(e,o){const c=t.querySelector(".comment-captcha"),n=t.querySelector(".g-recaptcha"),r="p-button--",s=r+"interactive",i=r+"destructive";let a="人机验证已通过。";e?(c.classList.add(s),c.classList.remove(i),n.classList.add("u-hidden")):(c.classList.add(i),c.classList.remove(s),a="reCAPTCHA 未通过。请验证。",o&&TypechoCore.ui.flashMessage(a)),c.setAttribute("title",a)},TypechoCore.Element.commentEditor=function(o){TypechoCore.engine.requestResource(TypechoCore.state("citizenRoot")+"css/editor.css").then();const r=t.getElementById(o.dataset.editor),s=r.closest(".editor-container"),i=o.dataset.version,a=!!r.getAttribute("data-external-smiley");function l(e,t,o,c){TypechoCore.State.commentEditorInstance=e;const n=e.ui.view.toolbar.element;TypechoCore.ui.setupHtmlInsert(o,n,-1),c.remove(),e.model.document.on("change:data",(()=>{e.sourceElement.innerText=e.getData()})),t.remove()}if(TypechoCore.registered("instant-editor")||TypechoCore.registered("classic-editor"))return TypechoCore.Element.commentEditorCarol(o,r,s,i,a,l);o.addEventListener("click",(function(t){const d=TypechoCore.ui.inspectorButton(o),m=TypechoCore.ui.inspector(s,"正在加载评论编辑器。");"undefined"==typeof Carol&&(Carol={editorOptions:{},extendEditorOptions:function(e,t){this.editorOptions[e]=t}}),a||(n="/common/themes/citizen/assets/discussion/smiley-compiled.js?v="+i);const u=c.concat(n);TypechoCore.engine.requestResource(u).then((t=>{e.CommentEditor.create(r,Carol.editorOptions).then((e=>{d.remove(),l(e,o,r,m)}))}))}))},TypechoCore.register("discussion"),TypechoCore.register("discussion-captcha"),TypechoCore.register("comment-editor")}(window,document);