-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.min.js
More file actions
10 lines (10 loc) · 4.04 KB
/
plugin.min.js
File metadata and controls
10 lines (10 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
/**
* Docsify Interactive Checkboxes Plugin
*
* Transforms standard markdown checkboxes into interactive, persistent task lists.
*
* @author Andrea Ferrario
* @license MIT
* @version 2.0.1
*/
!function () { "use strict"; const t = { storagePrefix: "docsify-checkbox-", strikethroughCompleted: !0, fadeCompleted: !0, fadeOpacity: .6, cleanOrphanedStates: !0 }; function e(e, o) { const c = Object.assign({}, t, window.$docsify?.interactiveCheckboxes || {}); function n() { return c.storagePrefix + (o.route.path || "index") } function r() { const t = n(); try { const e = localStorage.getItem(t); return e ? JSON.parse(e) : {} } catch (t) { return console.warn("Docsify Interactive Checkboxes: Error loading states", t), {} } } function i(t) { const e = n(); try { localStorage.setItem(e, JSON.stringify(t)) } catch (t) { console.warn("Docsify Interactive Checkboxes: Error saving states", t) } } function s(t, e) { const o = t.closest("li"); if (!o) return "checkbox-" + e; const c = o.cloneNode(!0), n = c.querySelector('input[type="checkbox"]'); n && n.remove(); return `cb-${function (t) { let e = 0; for (let o = 0; o < t.length; o++)e = (e << 5) - e + t.charCodeAt(o), e &= e; return Math.abs(e).toString(36) }(c.textContent.trim().slice(0, 50))}-${e}` } function a(t, e) { e ? (t.classList.add("checked"), c.strikethroughCompleted && (t.style.textDecoration = "line-through"), c.fadeCompleted && (t.style.opacity = c.fadeOpacity)) : (t.classList.remove("checked"), t.style.textDecoration = "", c.fadeCompleted && (t.style.opacity = "")) } function l() { const t = document.querySelectorAll('.task-list-item input[type="checkbox"], li input[type="checkbox"]'), e = t.length, o = Array.from(t).filter(t => t.checked).length; return { total: e, checked: o, percentage: e > 0 ? Math.round(o / e * 100) : 0 } } e.doneEach(function () { const t = document.querySelectorAll('.task-list-item input[type="checkbox"], li input[type="checkbox"]'); if (0 === t.length) return; const e = r(), n = []; t.forEach((t, c) => { const h = s(t, c); n.push(h), t.id = h, t.setAttribute("data-checkbox-id", h), t.removeAttribute("disabled"), t.style.cursor = "pointer"; const d = t.closest("li"); if (!d) return; const u = e[h]; void 0 !== u && (t.checked = u), a(d, t.checked), t.hasAttribute("data-listener-attached") || (t.setAttribute("data-listener-attached", "true"), t.addEventListener("change", function (t) { const e = t.target.checked, c = t.target.closest("li"); c && a(c, e); const n = r(); n[h] = e, i(n), document.dispatchEvent(new CustomEvent("docsify-checkbox-change", { detail: { checkboxId: h, checked: e, page: o.route.path, progress: l() } })) })) }), function (t) { if (!c.cleanOrphanedStates) return; const e = r(), o = new Set(t), n = {}; let s = !1; Object.keys(e).forEach(t => { o.has(t) ? n[t] = e[t] : s = !0 }), s && i(n) }(n) }) } window.clearAllDocsifyCheckboxes = function (e) { const o = e || t.storagePrefix, c = []; for (let t = 0; t < localStorage.length; t++) { const e = localStorage.key(t); e && e.startsWith(o) && c.push(e) } c.forEach(t => localStorage.removeItem(t)), location.reload() }, window.getDocsifyCheckboxProgress = function () { const t = document.querySelectorAll('.task-list-item input[type="checkbox"], li input[type="checkbox"]'), e = t.length, o = Array.from(t).filter(t => t.checked).length; return { total: e, checked: o, percentage: e > 0 ? Math.round(o / e * 100) : 0 } }, window.exportDocsifyCheckboxStates = function () { const e = t.storagePrefix, o = {}; for (let t = 0; t < localStorage.length; t++) { const c = localStorage.key(t); if (c && c.startsWith(e)) try { o[c] = JSON.parse(localStorage.getItem(c)) } catch (t) { console.warn("Error exporting key:", c, t) } } return o }, window.importDocsifyCheckboxStates = function (t) { try { return Object.keys(t).forEach(e => { localStorage.setItem(e, JSON.stringify(t[e])) }), location.reload(), !0 } catch (t) { return console.error("Error importing states:", t), !1 } }, window.$docsify ? (window.$docsify.plugins = window.$docsify.plugins || [], window.$docsify.plugins.push(e)) : window.DocsifyInteractiveCheckboxes = e }();