fictioneer/js/suggestion.min.js

1 line
4.6 KiB
JavaScript

diff_match_patch.prototype.fcn_prettyHtml=function(t){for(var e=[],n=/&/g,i=/</g,o=/>/g,s=/\n/g,l=0;l<t.length;l++){var a=t[l][0],r=t[l][1].replace(n,"&amp;").replace(i,"&lt;").replace(o,"&gt;").replace(s,"&para;<br>");switch(a){case 1:e[l]=`<ins>${r}</ins>`;break;case-1:e[l]=`<del>${r}</del>`;break;case 0:e[l]=`${r}`}}return e.join("")};class FCN_Suggestion{constructor(){this.toggle=_$$$("suggestions-modal-toggle"),this.tools=_$$$("selection-tools"),this.button=_$$$("button-add-suggestion"),this.toolsButton=_$$$("button-tools-add-suggestion"),this.reset=_$$$("button-suggestion-reset"),this.submit=_$$$("button-suggestion-submit"),this.current=_$$$("suggestions-modal-original"),this.input=_$$$("suggestions-modal-input"),this.output=_$$$("suggestions-modal-diff"),this.chapter=_$(".chapter__article"),this.text="",this.original="",this.latest="",this.paragraph=null,this.dmp=new diff_match_patch,this.bindEvents()}getCaretCoordinates(){let t=0,e=0;if(void 0!==window.getSelection){const n=window.getSelection();if(0!==n.rangeCount){let i=n.getRangeAt(0).cloneRange().getClientRects();i=i[i.length-1],i&&(t=i.right+window.scrollX,e=i.bottom+window.scrollY)}}return{x:t,y:e}}getClosestParagraph(){const t=window.getSelection();return t.rangeCount?t.getRangeAt(0).startContainer.parentNode.closest("p"):null}showTools(t,e){const n=document.documentElement.offsetWidth/2+this.tools.offsetWidth,i=_$$$("wpadminbar")?_$$$("wpadminbar").offsetHeight:0;this.tools.style.transform=e>n?"translate(-100%)":"translate(0)",this.tools.style.top=t+4-i+"px",this.tools.style.left=`${e}px`,this.tools.classList.remove("invisible")}hideTools(){this.tools.style.top="0",this.tools.style.left="-1000px",this.tools.classList.add("invisible")}textSelection(){return fcn_cleanTextSelectionFromButtons(window.getSelection().toString())}clearSelection(){window.getSelection?window.getSelection().empty?window.getSelection().empty():window.getSelection().removeAllRanges&&window.getSelection().removeAllRanges():document.selection&&document.selection.empty()}getDiff(t,e){const n=this.dmp.diff_main(t,e);return this.dmp.diff_cleanupEfficiency(n),this.dmp.fcn_prettyHtml(n)}toggleTools(t){fcn_theSite.classList.contains("transformed-site")||window.getSelection().rangeCount<1||!window.getSelection().getRangeAt(0).startContainer.parentNode.closest(".content-section")||setTimeout((()=>{if(t.text=t.textSelection().replaceAll("\n\n","\n"),""!==t.text){const e=t.getCaretCoordinates();t.showTools(e.y,e.x)}else t.hideTools()}),10)}toggleViaParagraphTools(t){fcn_theSite.classList.contains("transformed-site")||(t.text=_$(".selected-paragraph").querySelector(".paragraph-inner").innerText,t.showModal(t))}resizeInput(){this.input.style.height="auto",this.input.style.height=`${fcn_clamp(32,108,this.input.scrollHeight+4)}px`}showModal(t){fcn_lastSelectedParagraphId&&fcn_toggleParagraphTools(!1),t.original=t.text,t.current.innerHTML=t.text.replaceAll("\n","<br>"),t.input.value=t.text,t.output.innerHTML=t.getDiff(t.original,t.text),t.paragraph=t.getClosestParagraph(),t.toggle.click(),t.toggle.checked=!0,t.clearSelection(),t.hideTools(),t.resizeInput(),t.input.focus()}editSuggestion(t){t.resizeInput(),t.output.innerHTML=t.getDiff(t.original,t.input.value)}resetSuggestion(t){t.input.value=t.original,t.resizeInput(),t.output.innerHTML=t.getDiff(t.original,t.original)}submitSuggestion(t){const e=_$(fictioneer_comments.form_selector??"#comment"),n=t.paragraph?.id??null;let i=t.output.innerHTML;[["¶","&para;\n"],["<br>","\n"],["<ins>","[ins]"],["</ins>","[/ins]"],["<del>","[del]"],["</del>","[/del]"]].forEach((([t,e])=>{i=i.replaceAll(t,e)})),t.latest=`\n[quote]${i} [anchor]${n}[/anchor][/quote]\n`,e?"TEXTAREA"==e.tagName?(e.value+=t.latest,fcn_textareaAdjust(_$("textarea#comment"))):"DIV"==e.tagName&&(e.innerHTML+=t.latest):fcn_commentStack?.push(t.latest),t.toggle.click(),t.toggle.checked=!1,fcn_showNotification(fictioneer_tl.notification.suggestionAppendedToComment)}bindEvents(){this.chapter?.addEventListener("mouseup",this.toggleTools.bind(null,this)),this.button?.addEventListener("click",this.showModal.bind(null,this)),this.toolsButton?.addEventListener("click",this.toggleViaParagraphTools.bind(null,this)),this.input?.addEventListener("input",this.editSuggestion.bind(null,this)),this.reset?.addEventListener("click",this.resetSuggestion.bind(null,this)),this.submit?.addEventListener("click",this.submitSuggestion.bind(null,this))}}const fcn_suggestions=_$(".chapter__article")&&_$(".comment-section")&&_$$$("selection-tools")?new FCN_Suggestion:null;fcn_suggestions&&document.addEventListener("click",(function(t){t.target.closest(".content-section")||fcn_suggestions.hideTools()}));