fictioneer/js/chapter.min.js
2024-10-13 01:49:50 +02:00

1 line
14 KiB
JavaScript

const fcn_chapterFormatting=_$(".chapter-formatting");var fcn_formatting=fcn_getFormatting();const fcn_paragraphTools=_$$$("paragraph-tools");var fcn_lastSelectedParagraphId,fcn_bookmarkColor="none";function fcn_toggleParagraphTools(t=!1,e=null){e&&e.classList.contains("spoiler")&&!e.classList.contains("_open")||(_$$$(`paragraph-${fcn_lastSelectedParagraphId}`)?.classList.remove("selected-paragraph"),t&&fcn_formatting["show-paragraph-tools"]?(fcn_lastSelectedParagraphId=t,e.classList.add("selected-paragraph"),e.classList.contains("is-wrapped")||(e.innerHTML=`<span class="paragraph-inner">${e.innerHTML}</span>`,e.classList.add("is-wrapped")),e.append(fcn_paragraphTools)):fcn_lastSelectedParagraphId=null)}function fcn_touchParagraph(t){if(t.target.classList.contains("spoiler")||t.target.closest(".popup-menu-toggle, .skip-tools, a, button, label, input, textarea")||!t.target.closest("p")?.textContent.trim().length)return;if(!t.target.closest("p")?.parentElement?.classList.contains("chapter-formatting"))return;if(t.target.closest(".hidden, .inside-epub"))return;if(""!=window.getSelection().toString())return;const e=t.target.closest("p[data-paragraph-id]");if(t.target.closest(".tts-interface, .paragraph-tools__actions"))return;if(!e)return void fcn_toggleParagraphTools(!1);let n=!!e.dataset.paragraphId&&e.dataset.paragraphId;n=n!=fcn_lastSelectedParagraphId&&n;const a=(new Date).getTime();e.addEventListener("mouseup",(()=>{(new Date).getTime()<=a+300&&fcn_toggleParagraphTools(n,e)}),{once:!0})}function fcn_getQuote(t){const e=fcn_cleanTextSelectionFromButtons(window.getSelection().toString()),n=`[anchor]${t.target.closest("p[data-paragraph-id]").id}[/anchor]`;let a=t.target.closest("p[data-paragraph-id]").querySelector(".paragraph-inner").innerText,o="[…] ",r=" […]";if(a.length>16&&e.replace(/\s/g,"").length){const t=Math.ceil(.25*e.length),n=a.substring(0,t+1),c=a.substring(a.length-t,a.length);e.startsWith(n)&&(o=""),e.endsWith(c)&&(r=""),a=`${o}${e}${r}`}a=`${a} ${n}`,fcn_addQuoteToStack(a),fcn_showNotification(fictioneer_tl.notification.quoteAppendedToComment)}function fcn_addQuoteToStack(t){const e=_$(fictioneer_comments.form_selector??"#comment");e?"TEXTAREA"==e.tagName?(e.value+=`\n[quote]${t}[/quote]\n`,fcn_textareaAdjust(_$("textarea#comment"))):"DIV"==e.tagName&&(e.innerHTML+=`\n[quote]${t}[/quote]\n`):fcn_commentStack?.push(`\n[quote]${t}[/quote]\n`)}function fcn_openFullscreen(){document.documentElement.requestFullscreen?document.documentElement.requestFullscreen():document.documentElement.webkitRequestFullscreen&&document.documentElement.webkitRequestFullscreen()}function fcn_closeFullscreen(){document.exitFullscreen?document.exitFullscreen():document.webkitExitFullscreen&&document.webkitExitFullscreen()}function fcn_getFormatting(){let t=fcn_parseJSON(localStorage.getItem("fcnChapterFormatting"))??fcn_defaultFormatting();return Object.keys(t).length<15&&(t=fcn_defaultFormatting()),t.timestamp<1651164557584&&(t=fcn_defaultFormatting(),t.timestamp=Date.now()),fcn_setFormatting(t),t}function fcn_defaultFormatting(){return{"font-saturation":0,"font-color":fictioneer_font_colors[0].css,"font-name":fictioneer_fonts[0].css,"font-size":100,"letter-spacing":0,"line-height":1.7,"paragraph-spacing":1.5,"site-width":fcn_theRoot.dataset.siteWidthDefault??"960",indent:!0,"show-sensitive-content":!0,"show-chapter-notes":!0,justify:!1,"show-comments":!0,"show-paragraph-tools":!0,timestamp:1664797604825,...JSON.parse(fcn_theRoot.dataset.defaultFormatting??"{}")}}function fcn_setFormatting(t){"object"==typeof t&&(fcn_formatting=t,localStorage.setItem("fcnChapterFormatting",JSON.stringify(t)))}function fcn_updateToggle(t,e,n,a={}){a={save:!0,...a};const o=fcn_evaluateAsBoolean(t,!0),r=_$(e);if(r&&(r.checked=o,r.closest("label").setAttribute("aria-checked",o)),a.toggleClass&&fcn_chapterFormatting.classList.toggle(a.toggleClass,a.invertClass?!o:o),a.sensitiveContent){const t=_$$$("inline-sensitive-content-toggle");t?.classList.toggle("hide-sensitive",!o),t?.setAttribute("aria-checked",!o)}a.notes&&_$$(".chapter-note-hideable").forEach((t=>{t.classList.toggle("hidden",!o)})),a.comments&&_$$(".chapter-comments-hideable").forEach((t=>{t.classList.toggle("hidden",!o)})),fcn_formatting[n]=o,a.save&&fcn_setFormatting(fcn_formatting)}document.addEventListener("click",(t=>{fcn_paragraphTools?.closest("p")?.contains(t.target)||fcn_toggleParagraphTools(!1)})),fcn_paragraphTools&&(document.addEventListener("mousedown",(t=>{fcn_touchParagraph(t)})),_$$$("button-close-paragraph-tools").onclick=t=>{fcn_toggleParagraphTools(!1)},_$$$("button-get-link").onclick=t=>{fcn_copyToClipboard(`${location.host}${location.pathname}#${t.target.closest("p[data-paragraph-id]").id}`,fictioneer_tl.notification.linkCopiedToClipboard)},_$$$("button-comment-stack")?.addEventListener("click",(t=>{fcn_getQuote(t)})),_$$(".paragraph-tools__bookmark-colors > div").forEach((t=>{t.onclick=t=>{fcn_bookmarkColor=t.target.dataset.color}})),_$$$("button-set-bookmark")?.addEventListener("click",(t=>{fcn_toggleBookmark(t.target.closest("p[data-paragraph-id]").dataset.paragraphId,fcn_bookmarkColor),window.matchMedia("(min-width: 1024px)").matches&&fcn_toggleParagraphTools(!1),fcn_bookmarkColor="none"}))),_$$(".open-fullscreen").forEach((t=>{t.addEventListener("click",(()=>{fcn_openFullscreen()}))})),_$$(".close-fullscreen").forEach((t=>{t.addEventListener("click",(()=>{fcn_closeFullscreen()}))})),(()=>{"use strict";const t=_$$$("reader-settings-font-size-text"),e=_$$$("reader-settings-font-size-range"),n=_$$$("reader-settings-font-size-reset");function a(a,o=!0){a=fcn_clamp(50,200,a??100),t.value=a,e.value=a,n.classList.toggle("_modified",100!=a),_$$(".resize-font").forEach((t=>{t.style.fontSize=`${a}%`})),fcn_formatting["font-size"]=a,o&&fcn_setFormatting(fcn_formatting)}function o(){a(this.value)}function r(){a(parseFloat(fcn_formatting["font-size"])+parseFloat(this.dataset.modifier))}n&&(_$$$("reset-font")?.addEventListener("click",(()=>{a(100)})),n?.addEventListener("click",(()=>{a(100)})),e?.addEventListener("input",fcn_throttle(o,1e3/24)),t?.addEventListener("input",o),_$$$("increase-font")?.addEventListener("click",r),_$$$("decrease-font")?.addEventListener("click",r),a(fcn_formatting["font-size"],!1))})(),(()=>{"use strict";const t=_$$$("reader-settings-font-color-reset"),e=_$$$("reader-settings-font-color-select");function n(n,a=!0){n=fcn_clamp(0,fictioneer_font_colors.length-1,n),t.classList.toggle("_modified",n>0),e.value=n,fcn_chapterFormatting.style.setProperty("--text-chapter",fictioneer_font_colors[n].css),fcn_formatting["font-color"]=fictioneer_font_colors[n].css,a&&fcn_setFormatting(fcn_formatting)}t&&(t.onclick=()=>{n(0)},e.onchange=t=>{n(t.target.value)},_$$(".font-color-stepper").forEach((t=>{t.addEventListener("click",(t=>{!function(t=1){let a=(e.selectedIndex+parseInt(t))%fictioneer_font_colors.length;a=a<0?fictioneer_font_colors.length-1:a,n(a)}(t.currentTarget.value)}))})),n(fictioneer_font_colors.findIndex((t=>t.css==fcn_formatting["font-color"])),!1))})(),(()=>{"use strict";const t=_$$$("reader-settings-font-reset"),e=_$$$("reader-settings-font-select");function n(a,o=!0){a=fcn_clamp(0,fictioneer_fonts.length-1,a);let r=fictioneer_fonts[a].css;fictioneer_fonts[a].alt&&(r=`${r}, ${fictioneer_fonts[a].alt}`),a<0?n(0):(t.classList.toggle("_modified",a>0),e.value=a,_$$(".chapter-font-family").forEach((t=>{t.style.fontFamily=""===r?"var(--ff-system)":r+", var(--ff-system)"})),fcn_formatting["font-name"]=fictioneer_fonts[a].css,o&&fcn_setFormatting(fcn_formatting))}t&&(t.onclick=()=>{n(0)},e.onchange=t=>{n(t.target.value)},_$$(".font-stepper").forEach((t=>{t.addEventListener("click",(t=>{!function(t=1){let a=(e.selectedIndex+parseInt(t))%fictioneer_fonts.length;a=a<0?fictioneer_fonts.length-1:a,n(a)}(t.currentTarget.value)}))})),n(fictioneer_fonts.findIndex((t=>t.css==fcn_formatting["font-name"])),!1))})(),(()=>{"use strict";const t=_$$$("reader-settings-font-saturation-text"),e=_$$$("reader-settings-font-saturation-range"),n=_$$$("reader-settings-font-saturation-reset");function a(a,o=!0){a=fcn_clamp(-1,1,a??0),t.value=parseInt(100*a),e.value=a,n.classList.toggle("_modified",0!=a),fcn_chapterFormatting.style.setProperty("--font-saturation",`(${a>=0?1+a**2:1-a**2} + var(--font-saturation-offset))`),fcn_formatting["font-saturation"]=a,o&&fcn_setFormatting(fcn_formatting)}n&&(n?.addEventListener("click",(()=>{a(0)})),e?.addEventListener("input",fcn_throttle((function(){a(this.value)}),1e3/24)),t?.addEventListener("input",(function(){a(parseInt(this.value)/100)})),a(fcn_formatting["font-saturation"],!1))})(),(()=>{"use strict";const t=_$$$("reader-settings-letter-spacing-text"),e=_$$$("reader-settings-letter-spacing-range"),n=_$$$("reader-settings-letter-spacing-reset"),a=fcn_defaultFormatting()["letter-spacing"];function o(o,r=!0){o=fcn_clamp(-.1,.2,o??a),t.value=o,e.value=o,n.classList.toggle("_modified",o!=a),fcn_chapterFormatting.style.letterSpacing=`calc(${o}em + var(--font-letter-spacing-base))`,fcn_formatting["letter-spacing"]=o,r&&fcn_setFormatting(fcn_formatting)}function r(){o(this.value)}n&&(n?.addEventListener("click",(()=>{o(a)})),e?.addEventListener("input",fcn_throttle(r,1e3/24)),t?.addEventListener("input",r),o(fcn_formatting["letter-spacing"],!1))})(),(()=>{"use strict";const t=_$$$("reader-settings-paragraph-spacing-text"),e=_$$$("reader-settings-paragraph-spacing-range"),n=_$$$("reader-settings-paragraph-spacing-reset"),a=fcn_defaultFormatting()["paragraph-spacing"];function o(o,r=!0){o=fcn_clamp(0,3,o??a),t.value=o,e.value=o,n.classList.toggle("_modified",o!=a),fcn_chapterFormatting.style.setProperty("--paragraph-spacing",`${o}em`),fcn_formatting["paragraph-spacing"]=o,r&&fcn_setFormatting(fcn_formatting)}function r(){o(this.value)}n&&(n?.addEventListener("click",(()=>{o(a)})),e?.addEventListener("input",fcn_throttle(r,1e3/24)),t?.addEventListener("input",r),o(fcn_formatting["paragraph-spacing"],!1))})(),(()=>{"use strict";const t=_$$$("reader-settings-line-height-text"),e=_$$$("reader-settings-line-height-range"),n=_$$$("reader-settings-line-height-reset"),a=fcn_defaultFormatting()["line-height"];function o(o,r=!0){o=fcn_clamp(.8,3,o??a),t.value=o,e.value=o,n.classList.toggle("_modified",o!=a),fcn_chapterFormatting.style.lineHeight=`${o}`,fcn_formatting["line-height"]=o,r&&fcn_setFormatting(fcn_formatting)}function r(){o(this.value)}n&&(n?.addEventListener("click",(()=>{o(a)})),e?.addEventListener("input",fcn_throttle(r,1e3/24)),t?.addEventListener("input",r),o(fcn_formatting["line-height"],!1))})(),(()=>{"use strict";const t=_$$$("reader-settings-site-width-text"),e=_$$$("reader-settings-site-width-range"),n=_$$$("reader-settings-site-width-reset"),a=fcn_defaultFormatting()["site-width"];function o(o,r=!0){const c=_$("main");o=fcn_clamp(640,1920,o??a),t.value=o,e.value=o,n.classList.toggle("_modified",o!=a),c.style.setProperty("--site-width",`${o}px`),c.classList.toggle("_default-width",o==fcn_theRoot.dataset.siteWidthDefault),c.classList.toggle("_below-1024",o<1024&&o>=768),c.classList.toggle("_below-768",o<768&&o>640),c.classList.toggle("_640-and-below",o<=640),fcn_formatting["site-width"]=o,r&&fcn_setFormatting(fcn_formatting)}function r(){o(this.value)}n&&(n?.addEventListener("click",(()=>{o(a)})),e?.addEventListener("input",fcn_throttle(r,1e3/24)),t?.addEventListener("input",r),o(fcn_formatting["site-width"],!1))})(),_$$("#reader-settings-indent-toggle").forEach((t=>{const e={invertClass:!0,toggleClass:"no-indent"},n="indent";t.onclick=a=>{fcn_updateToggle(a.currentTarget.checked,`#${t.id}`,n,e)},fcn_updateToggle(fcn_formatting[n],`#${t.id}`,n,{save:!1,...e})})),_$$("#reader-settings-justify-toggle").forEach((t=>{const e={toggleClass:"justify"},n="justify";t.onclick=a=>{fcn_updateToggle(a.currentTarget.checked,`#${t.id}`,n,e)},fcn_updateToggle(fcn_formatting[n],`#${t.id}`,n,{save:!1,...e})})),_$$("#reader-settings-paragraph-tools-toggle").forEach((t=>{const e="show-paragraph-tools";t.onclick=n=>{fcn_updateToggle(n.currentTarget.checked,`#${t.id}`,e)},fcn_updateToggle(fcn_formatting[e],`#${t.id}`,e,{save:!1})})),_$$("#reader-settings-chapter-notes-toggle").forEach((t=>{const e={notes:!0},n="show-chapter-notes";t.onclick=a=>{fcn_updateToggle(a.currentTarget.checked,`#${t.id}`,n,e)},fcn_updateToggle(fcn_formatting[n],`#${t.id}`,n,{save:!1,...e})})),_$$("#reader-settings-comments-toggle").forEach((t=>{const e={comments:!0},n="show-comments";t.onclick=a=>{fcn_updateToggle(a.currentTarget.checked,`#${t.id}`,n,e)},fcn_updateToggle(fcn_formatting[n],`#${t.id}`,n,{save:!1,...e})})),_$$("#reader-settings-sensitive-content-toggle").forEach((t=>{const e={toggleClass:"hide-sensitive",invertClass:!0,sensitiveContent:!0},n="show-sensitive-content";t.onclick=a=>{fcn_updateToggle(a.currentTarget.checked,`#${t.id}`,n,e)},fcn_updateToggle(fcn_formatting[n],`#${t.id}`,n,{save:!1,...e})}));const fcn_progressBar=_$(".progress__bar"),fcn_chapterContent=_$$$("chapter-content");var fcn_chapterCheckmarkUpdated=!1;function fcn_trackProgress(){fcn_chapterContent&&(fcn_readingProgress(),window.addEventListener("scroll.rAF",fcn_throttle(fcn_readingProgress,1e3/48)))}function fcn_readingProgress(){if(fcn_settingMinimal.checked||!fcn_settingChapterProgressBar.checked)return;const t=fcn_chapterContent.getBoundingClientRect(),e=t.height,n=window.innerHeight,a=e-t.bottom-Math.max(t.top,0)+n;let o=100*a/e;if(fcn_theBody.classList.toggle("hasProgressBar",!(o<0||a>e+500)),o=fcn_clamp(0,100,o),fcn_progressBar.style.width=`${o}%`,o>=100&&!fcn_chapterCheckmarkUpdated&&fcn_isLoggedIn){const t=fcn_theBody.dataset.storyId;if(fcn_chapterCheckmarkUpdated=!0,!t||"function"!=typeof fcn_toggleCheckmark)return;fcn_toggleCheckmark(t,"progress",parseInt(fcn_theBody.dataset.postId),null,"set")}}_$("article:not(._password)")&&fcn_trackProgress(),_$$(".chapter-list-popup-toggle").forEach((t=>{t.addEventListener("click",(()=>{t.querySelector('[data-target="popup-chapter-list"]')?.appendChild(fcn_chapterList.cloneNode(!0))}),{once:!0})}));const fcn_chapterKeyboardNavigation=t=>{if(["INPUT","TEXTAREA","SELECT","OPTION"].includes(t.target.tagName)||t.target.isContentEditable)return;let e=null;"ArrowLeft"===t.code?e=_$("a.button._navigation._prev"):"ArrowRight"===t.code&&(e=_$("a.button._navigation._next")),e&&e.href&&(window.location.href=e+"#start")};if(document.addEventListener("keydown",fcn_chapterKeyboardNavigation),"#start"===window.location.hash){history.replaceState(null,document.title,window.location.pathname);const t=_$(".chapter__article");t&&fcn_scrollTo(t,128)}