refactor: improve code maintainability
This commit is contained in:
+32
-24
@@ -1,28 +1,36 @@
|
|||||||
function type() {
|
import {terminal} from "../terminal.js";
|
||||||
document.addEventListener('DOMContentLoaded', () => {
|
|
||||||
let typedTextContainer = document.querySelector('.typed-text');
|
|
||||||
if (!typedTextContainer) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
function typeWriter(container) {
|
const TYPED_TEXT_SELECTOR = ".typed-text";
|
||||||
let text = container.innerText;
|
const TYPED_FLAG = "typed";
|
||||||
container.innerText = '';
|
const MIN_TYPE_DELAY = 50;
|
||||||
let i = 0;
|
const TYPE_DELAY_VARIANCE = 200;
|
||||||
|
|
||||||
function _type() {
|
function getTypeDelay() {
|
||||||
if (i < text.length) {
|
return Math.random() * TYPE_DELAY_VARIANCE + MIN_TYPE_DELAY;
|
||||||
container.innerHTML += text.charAt(i);
|
|
||||||
i++;
|
|
||||||
setTimeout(_type, Math.random() * 200 + 50);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_type();
|
|
||||||
}
|
|
||||||
|
|
||||||
typeWriter(typedTextContainer);
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
terminal.registerRefresh(type);
|
function typeWriter(container) {
|
||||||
|
if (!container || container.dataset[TYPED_FLAG] === "true") return;
|
||||||
|
|
||||||
|
const text = container.textContent || "";
|
||||||
|
container.dataset[TYPED_FLAG] = "true";
|
||||||
|
container.textContent = "";
|
||||||
|
|
||||||
|
let index = 0;
|
||||||
|
|
||||||
|
function writeNextCharacter() {
|
||||||
|
if (index >= text.length) return;
|
||||||
|
|
||||||
|
container.textContent += text.charAt(index);
|
||||||
|
index++;
|
||||||
|
window.setTimeout(writeNextCharacter, getTypeDelay());
|
||||||
|
}
|
||||||
|
|
||||||
|
writeNextCharacter();
|
||||||
|
}
|
||||||
|
|
||||||
|
function typeTypedText() {
|
||||||
|
document.querySelectorAll(TYPED_TEXT_SELECTOR).forEach(typeWriter);
|
||||||
|
}
|
||||||
|
|
||||||
|
terminal.registerRefresh(typeTypedText);
|
||||||
|
|||||||
+32
-12
@@ -1,29 +1,49 @@
|
|||||||
function register_themeToggle() {
|
import {terminal} from "../terminal.js";
|
||||||
|
|
||||||
|
const THEME_MODE_STORAGE_KEY = "theme-mode";
|
||||||
|
const PIXEL_STYLE_STORAGE_KEY = "pixel_style";
|
||||||
|
|
||||||
|
function registerSearchButton() {
|
||||||
|
const searchButton = document.querySelector('[data-header-action="open-search"]');
|
||||||
|
if (!searchButton) return;
|
||||||
|
|
||||||
|
searchButton.addEventListener("click", function () {
|
||||||
|
if (typeof window.SearchWidget?.open === "function") {
|
||||||
|
window.SearchWidget.open();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function registerThemeToggle() {
|
||||||
const toggle = document.getElementById("theme-toggle");
|
const toggle = document.getElementById("theme-toggle");
|
||||||
|
if (!toggle || typeof window.applyTheme !== "function") return;
|
||||||
|
|
||||||
toggle.addEventListener("click", function () {
|
toggle.addEventListener("click", function () {
|
||||||
const currentScheme = document.documentElement.getAttribute("data-color-scheme");
|
const currentScheme = document.documentElement.getAttribute("data-color-scheme");
|
||||||
const nextMode = currentScheme === "dark" ? "light" : "dark";
|
const nextMode = currentScheme === "dark" ? "light" : "dark";
|
||||||
|
|
||||||
applyTheme(nextMode);
|
window.applyTheme(nextMode);
|
||||||
|
|
||||||
const isSystemMode = currentScheme === "system";
|
const isSystemMode = currentScheme === "system";
|
||||||
if (!isSystemMode) {
|
if (!isSystemMode) {
|
||||||
localStorage.setItem("theme-mode", nextMode);
|
localStorage.setItem(THEME_MODE_STORAGE_KEY, nextMode);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function register_pixelToggle() {
|
function registerPixelToggle() {
|
||||||
const pixel_toggle = document.getElementById('pixel-toggle');
|
const pixelToggle = document.getElementById("pixel-toggle");
|
||||||
|
if (!pixelToggle) return;
|
||||||
|
|
||||||
pixel_toggle.addEventListener('click', function () {
|
pixelToggle.addEventListener("click", function () {
|
||||||
let storedStatus = localStorage.getItem("pixel_style");
|
const storedStatus = localStorage.getItem(PIXEL_STYLE_STORAGE_KEY);
|
||||||
let newStatus = storedStatus === 'true' ? 'false' : 'true';
|
const newStatus = storedStatus === "true" ? "false" : "true";
|
||||||
document.documentElement.setAttribute("pixel_style", newStatus);
|
document.documentElement.setAttribute(PIXEL_STYLE_STORAGE_KEY, newStatus);
|
||||||
localStorage.setItem("pixel_style", newStatus);
|
localStorage.setItem(PIXEL_STYLE_STORAGE_KEY, newStatus);
|
||||||
|
window.TerminalTheme?.syncPixelFont?.();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
terminal.registerInitFunc(register_pixelToggle);
|
terminal.registerInitFunc(registerSearchButton);
|
||||||
terminal.registerInitFunc(register_themeToggle);
|
terminal.registerInitFunc(registerPixelToggle);
|
||||||
|
terminal.registerInitFunc(registerThemeToggle);
|
||||||
|
|||||||
+86
-33
@@ -1,50 +1,83 @@
|
|||||||
import {terminal} from "../terminal.js";
|
import {terminal} from "../terminal.js";
|
||||||
|
|
||||||
const key = 'moment';
|
const RESOURCE_KEY = "moment";
|
||||||
const group = 'moment.halo.run';
|
const RESOURCE_GROUP = "moment.halo.run";
|
||||||
const plural = 'moments';
|
const RESOURCE_PLURAL = "moments";
|
||||||
|
const UPVOTED_NAMES_STORAGE_KEY = `walker.upvoted.${RESOURCE_KEY}.names`;
|
||||||
|
const UPVOTE_BUTTON_SELECTOR = ".moment-upvote-btn";
|
||||||
|
const UPVOTE_COUNT_SELECTOR = `.upvote-count[data-${RESOURCE_KEY}-name]`;
|
||||||
|
const MOMENT_ACTION_SELECTOR = "[data-moment-action]";
|
||||||
|
const UPVOTE_API = "/apis/api.halo.run/v1alpha1/trackers/upvote";
|
||||||
|
const MOMENT_ACTIONS = {
|
||||||
|
upvote: "upvote",
|
||||||
|
toggleComment: "toggle-comment"
|
||||||
|
};
|
||||||
|
|
||||||
let upvotedNames = JSON.parse(localStorage.getItem('walker.upvoted.' + key + '.names') || '[]')
|
let upvotedNames = readUpvotedNames();
|
||||||
|
let actionListenerRegistered = false;
|
||||||
|
|
||||||
|
function readUpvotedNames() {
|
||||||
|
try {
|
||||||
|
const names = JSON.parse(localStorage.getItem(UPVOTED_NAMES_STORAGE_KEY) || "[]");
|
||||||
|
return Array.isArray(names) ? names : [];
|
||||||
|
} catch {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function saveUpvotedNames() {
|
||||||
|
localStorage.setItem(UPVOTED_NAMES_STORAGE_KEY, JSON.stringify(upvotedNames));
|
||||||
|
}
|
||||||
|
|
||||||
function upvoted(id) {
|
function upvoted(id) {
|
||||||
return upvotedNames.includes(id);
|
return upvotedNames.includes(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function recordUpvote(name) {
|
||||||
|
if (upvoted(name)) return;
|
||||||
|
upvotedNames = [...upvotedNames, name];
|
||||||
|
saveUpvotedNames();
|
||||||
|
}
|
||||||
|
|
||||||
|
function findUpvoteCountNode(name) {
|
||||||
|
return [...document.querySelectorAll(UPVOTE_COUNT_SELECTOR)]
|
||||||
|
.find(node => node.dataset[`${RESOURCE_KEY}Name`] === name);
|
||||||
|
}
|
||||||
|
|
||||||
|
function incrementUpvoteCount(name) {
|
||||||
|
const upvoteNode = findUpvoteCountNode(name);
|
||||||
|
if (!upvoteNode) return;
|
||||||
|
|
||||||
|
const upvoteCount = Number.parseInt(upvoteNode.textContent || "0", 10) || 0;
|
||||||
|
upvoteNode.textContent = String(upvoteCount + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
function syncUpvotedButtons() {
|
||||||
|
document.querySelectorAll(UPVOTE_BUTTON_SELECTOR).forEach(btn => {
|
||||||
|
btn.classList.toggle("active", upvoted(btn.dataset.momentName));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function handleUpvote(name) {
|
function handleUpvote(name) {
|
||||||
|
if (!name) return;
|
||||||
if (upvoted(name)) return;
|
if (upvoted(name)) return;
|
||||||
|
|
||||||
const xhr = new XMLHttpRequest();
|
const xhr = new XMLHttpRequest();
|
||||||
xhr.open('POST', '/apis/api.halo.run/v1alpha1/trackers/upvote');
|
xhr.open("POST", UPVOTE_API);
|
||||||
xhr.setRequestHeader('Content-Type', 'application/json');
|
xhr.setRequestHeader("Content-Type", "application/json");
|
||||||
|
|
||||||
xhr.onload = () => {
|
xhr.onload = () => {
|
||||||
upvotedNames = [...upvotedNames, name];
|
if (xhr.status < 200 || xhr.status >= 300) return;
|
||||||
localStorage.setItem('walker.upvoted.' + key + '.names', JSON.stringify(upvotedNames));
|
recordUpvote(name);
|
||||||
|
incrementUpvoteCount(name);
|
||||||
const upvoteNode = document.querySelector('[data-' + key + '-name="' + name + '"]');
|
syncUpvotedButtons();
|
||||||
if (!upvoteNode) return;
|
|
||||||
|
|
||||||
const upvoteCount = parseInt(upvoteNode.textContent || '0');
|
|
||||||
upvoteNode.textContent = upvoteCount + 1 + '';
|
|
||||||
};
|
};
|
||||||
|
|
||||||
xhr.onerror = function () {
|
xhr.onerror = function () {
|
||||||
alert('Network Error');
|
alert("Network Error");
|
||||||
};
|
};
|
||||||
|
|
||||||
xhr.send(JSON.stringify({group: group, plural: plural, name: name}));
|
xhr.send(JSON.stringify({group: RESOURCE_GROUP, plural: RESOURCE_PLURAL, name: name}));
|
||||||
}
|
|
||||||
|
|
||||||
function register_upvoted() {
|
|
||||||
document.addEventListener('DOMContentLoaded', function () {
|
|
||||||
document.querySelectorAll('.moment-upvote-btn').forEach(btn => {
|
|
||||||
const name = btn.dataset.name
|
|
||||||
if (upvoted(name)) {
|
|
||||||
btn.classList.add('active')
|
|
||||||
}
|
|
||||||
})
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function showComment(name) {
|
function showComment(name) {
|
||||||
@@ -52,16 +85,36 @@ function showComment(name) {
|
|||||||
|
|
||||||
if (!section) return;
|
if (!section) return;
|
||||||
|
|
||||||
const show = section.classList.contains('active');
|
const show = section.classList.contains("active");
|
||||||
|
|
||||||
if (show) {
|
if (show) {
|
||||||
section.classList.remove('active');
|
section.classList.remove("active");
|
||||||
} else {
|
} else {
|
||||||
section.classList.add('active');
|
section.classList.add("active");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
terminal.registerInitFunc(register_upvoted)
|
function handleMomentAction(event) {
|
||||||
|
const actionNode = event.target.closest(MOMENT_ACTION_SELECTOR);
|
||||||
|
if (!actionNode) return;
|
||||||
|
|
||||||
window.handleUpvote = handleUpvote;
|
const action = actionNode.dataset.momentAction;
|
||||||
window.showComment = showComment;
|
if (action === MOMENT_ACTIONS.upvote) {
|
||||||
|
handleUpvote(actionNode.dataset.momentName);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (action === MOMENT_ACTIONS.toggleComment) {
|
||||||
|
showComment(actionNode.dataset.commentTarget);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function registerMomentActions() {
|
||||||
|
if (actionListenerRegistered) return;
|
||||||
|
|
||||||
|
document.addEventListener("click", handleMomentAction);
|
||||||
|
actionListenerRegistered = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
terminal.registerInitFunc(registerMomentActions);
|
||||||
|
terminal.registerRefresh(syncUpvotedButtons);
|
||||||
|
|||||||
+125
-122
@@ -1,169 +1,172 @@
|
|||||||
|
import {terminal} from "../terminal.js";
|
||||||
|
import {debounce, toggleClass} from "../utils/dom.js";
|
||||||
import tocbot from "tocbot";
|
import tocbot from "tocbot";
|
||||||
|
|
||||||
let toc_icon = "☰"
|
const TOC_SELECTOR = "#toc";
|
||||||
let toc_close_icon = "✕"
|
const TOC_CONTENT_SELECTOR = ".toc-content";
|
||||||
|
const CONTENT_SELECTOR = "#content";
|
||||||
|
const HEADING_SELECTOR = "h1, h2, h3, h4";
|
||||||
|
const TOC_VISIBLE_CLASS = "toc-visible";
|
||||||
|
const TOC_TOGGLE_ID = "toc-toggle";
|
||||||
|
const TOC_OPEN_ICON = "☰";
|
||||||
|
const TOC_CLOSE_ICON = "✕";
|
||||||
|
const DESKTOP_TOC_WIDTH = 1400;
|
||||||
|
const TOC_GAP = 12;
|
||||||
|
const FALLBACK_TOC_WIDTH_REM = 17.5;
|
||||||
|
const RESIZE_DEBOUNCE_MS = 250;
|
||||||
|
|
||||||
|
let resizeListenerRegistered = false;
|
||||||
|
|
||||||
function isTocDefaultOpen(toc) {
|
function isTocDefaultOpen(toc) {
|
||||||
return toc?.dataset?.defaultOpen !== "false";
|
return toc?.dataset?.defaultOpen !== "false";
|
||||||
}
|
}
|
||||||
|
|
||||||
function toggleToc() {
|
function getToc() {
|
||||||
const toc = document.getElementById("toc");
|
return document.querySelector(TOC_SELECTOR);
|
||||||
if (!toc) return;
|
|
||||||
const toc_toggle = document.getElementById("toc-toggle");
|
|
||||||
|
|
||||||
if (!toc_toggle) return;
|
|
||||||
|
|
||||||
const isVisible = toc.classList.contains('toc-visible');
|
|
||||||
|
|
||||||
if (isVisible) {
|
|
||||||
toc.classList.remove('toc-visible');
|
|
||||||
toc_toggle.innerHTML = toc_icon;
|
|
||||||
toc_toggle.setAttribute('aria-label', 'Show Table of Contents');
|
|
||||||
|
|
||||||
} else {
|
|
||||||
toc.classList.add('toc-visible');
|
|
||||||
toc_toggle.innerHTML = toc_close_icon;
|
|
||||||
toc_toggle.setAttribute('aria-label', 'Hide Table of Contents');
|
|
||||||
}
|
|
||||||
void locateToc();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function generateToc() {
|
function getTocToggle() {
|
||||||
const toc = document.getElementById("toc");
|
return document.getElementById(TOC_TOGGLE_ID);
|
||||||
if (!toc) return;
|
}
|
||||||
|
|
||||||
const content = document.getElementById("content");
|
function getContent() {
|
||||||
const titles = content.querySelectorAll("h1, h2, h3, h4");
|
return document.querySelector(CONTENT_SELECTOR);
|
||||||
|
}
|
||||||
|
|
||||||
if (!titles || titles.length === 0) {
|
function destroyTocbot() {
|
||||||
toc.style.display = "none";
|
if (typeof tocbot !== "undefined") {
|
||||||
toc.remove();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof tocbot !== 'undefined') {
|
|
||||||
tocbot.destroy();
|
tocbot.destroy();
|
||||||
tocbot.init({
|
|
||||||
tocSelector: ".toc-content",
|
|
||||||
contentSelector: "#content",
|
|
||||||
headingSelector: "h1, h2, h3, h4",
|
|
||||||
extraListClasses: "toc-list",
|
|
||||||
extraLinkClasses: "toc-link",
|
|
||||||
headingsOffset: 96,
|
|
||||||
scrollSmoothOffset: -96,
|
|
||||||
scrollSmooth: true,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setupTocVisibility();
|
|
||||||
setupToggleButton();
|
|
||||||
void locateToc();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function setupTocVisibility() {
|
function syncToggleButton(toc = getToc()) {
|
||||||
const toc = document.querySelector(".toc");
|
const tocToggle = getTocToggle();
|
||||||
|
if (!toc || !tocToggle) return;
|
||||||
|
|
||||||
|
const isVisible = toc.classList.contains(TOC_VISIBLE_CLASS);
|
||||||
|
tocToggle.textContent = isVisible ? TOC_CLOSE_ICON : TOC_OPEN_ICON;
|
||||||
|
tocToggle.setAttribute("aria-label", isVisible ? "Hide Table of Contents" : "Show Table of Contents");
|
||||||
|
|
||||||
|
if (tocToggle.dataset.tocBound === "true") return;
|
||||||
|
tocToggle.addEventListener("click", toggleToc);
|
||||||
|
tocToggle.dataset.tocBound = "true";
|
||||||
|
}
|
||||||
|
|
||||||
|
function setTocVisible(toc, visible) {
|
||||||
|
toggleClass(toc, TOC_VISIBLE_CLASS, visible);
|
||||||
|
syncToggleButton(toc);
|
||||||
|
}
|
||||||
|
|
||||||
|
function toggleToc() {
|
||||||
|
const toc = getToc();
|
||||||
|
if (!toc) return;
|
||||||
|
|
||||||
|
const isVisible = toc.classList.contains(TOC_VISIBLE_CLASS);
|
||||||
|
setTocVisible(toc, !isVisible);
|
||||||
|
locateToc();
|
||||||
|
}
|
||||||
|
|
||||||
|
function hideToc(toc) {
|
||||||
|
toc.style.display = "none";
|
||||||
|
toc.remove();
|
||||||
|
destroyTocbot();
|
||||||
|
}
|
||||||
|
|
||||||
|
function initTocbot() {
|
||||||
|
destroyTocbot();
|
||||||
|
tocbot.init({
|
||||||
|
tocSelector: TOC_CONTENT_SELECTOR,
|
||||||
|
contentSelector: CONTENT_SELECTOR,
|
||||||
|
headingSelector: HEADING_SELECTOR,
|
||||||
|
extraListClasses: "toc-list",
|
||||||
|
extraLinkClasses: "toc-link",
|
||||||
|
headingsOffset: 96,
|
||||||
|
scrollSmoothOffset: -96,
|
||||||
|
scrollSmooth: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function shouldShowExpandedToc(toc) {
|
||||||
|
return isTocDefaultOpen(toc) && window.innerWidth > DESKTOP_TOC_WIDTH;
|
||||||
|
}
|
||||||
|
|
||||||
|
function setupTocVisibility(toc = getToc()) {
|
||||||
if (!toc) return;
|
if (!toc) return;
|
||||||
|
|
||||||
toc.style.display = "block";
|
toc.style.display = "block";
|
||||||
|
setTocVisible(toc, shouldShowExpandedToc(toc));
|
||||||
|
}
|
||||||
|
|
||||||
if (isTocDefaultOpen(toc) && window.innerWidth > 1400) {
|
function generateToc() {
|
||||||
toc.classList.add('toc-visible');
|
const toc = getToc();
|
||||||
} else {
|
const content = getContent();
|
||||||
toc.classList.remove('toc-visible');
|
|
||||||
|
if (!toc || !content) {
|
||||||
|
destroyTocbot();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const titles = content.querySelectorAll(HEADING_SELECTOR);
|
||||||
|
if (!titles || titles.length === 0) {
|
||||||
|
hideToc(toc);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
initTocbot();
|
||||||
|
setupTocVisibility(toc);
|
||||||
|
syncToggleButton(toc);
|
||||||
|
locateToc();
|
||||||
}
|
}
|
||||||
|
|
||||||
function setupToggleButton() {
|
function getTocExpandedWidth(toc) {
|
||||||
const toc = document.getElementById("toc");
|
return toc.offsetWidth > 0
|
||||||
const toc_toggle = document.getElementById("toc-toggle");
|
|
||||||
|
|
||||||
const isVisible = toc.classList.contains('toc-visible');
|
|
||||||
toc_toggle.innerHTML = isVisible ? toc_close_icon : toc_icon;
|
|
||||||
toc_toggle.setAttribute('aria-label', isVisible ? 'Hide Table of Contents' : 'Show Table of Contents');
|
|
||||||
|
|
||||||
toc_toggle.addEventListener('click', toggleToc);
|
|
||||||
}
|
|
||||||
|
|
||||||
function hasEnoughSpace() {
|
|
||||||
const content = document.getElementById("content");
|
|
||||||
const toc = document.getElementById("toc");
|
|
||||||
if (!content || !toc) return false;
|
|
||||||
|
|
||||||
const contentRect = content.getBoundingClientRect();
|
|
||||||
const gap = 12;
|
|
||||||
const tocExpandedWidth = toc.offsetWidth > 0
|
|
||||||
? toc.offsetWidth
|
? toc.offsetWidth
|
||||||
: parseInt(getComputedStyle(document.documentElement).fontSize) * 17.5;
|
: Number.parseFloat(getComputedStyle(document.documentElement).fontSize) * FALLBACK_TOC_WIDTH_REM;
|
||||||
|
}
|
||||||
|
|
||||||
const spaceRight = window.innerWidth - contentRect.right - gap;
|
function hasEnoughSpace(content, toc) {
|
||||||
|
const contentRect = content.getBoundingClientRect();
|
||||||
|
const tocExpandedWidth = getTocExpandedWidth(toc);
|
||||||
|
|
||||||
|
const spaceRight = window.innerWidth - contentRect.right - TOC_GAP;
|
||||||
return spaceRight >= tocExpandedWidth;
|
return spaceRight >= tocExpandedWidth;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function locateToc() {
|
function locateToc() {
|
||||||
const toc = document.getElementById("toc");
|
const toc = getToc();
|
||||||
if (!toc) return;
|
if (!toc) return;
|
||||||
|
|
||||||
const content = document.getElementById("content");
|
const content = getContent();
|
||||||
if (!content) return;
|
if (!content) return;
|
||||||
|
|
||||||
const contentRect = content.getBoundingClientRect();
|
const contentRect = content.getBoundingClientRect();
|
||||||
const gap = 12;
|
|
||||||
|
|
||||||
if (hasEnoughSpace()) {
|
if (hasEnoughSpace(content, toc)) {
|
||||||
toc.style.left = `${contentRect.right + gap}px`;
|
toc.style.left = `${contentRect.right + TOC_GAP}px`;
|
||||||
toc.style.right = 'auto';
|
toc.style.right = "auto";
|
||||||
} else {
|
} else {
|
||||||
toc.style.left = 'auto';
|
toc.style.left = "auto";
|
||||||
toc.style.right = `${gap}px`;
|
toc.style.right = `${TOC_GAP}px`;
|
||||||
toc.style.top = 'auto';
|
toc.style.top = "auto";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleResize() {
|
function handleResize() {
|
||||||
const toc = document.getElementById("toc");
|
const toc = getToc();
|
||||||
if (!toc) return;
|
if (!toc) return;
|
||||||
|
|
||||||
setupTocVisibility();
|
setupTocVisibility(toc);
|
||||||
setupToggleButton();
|
syncToggleButton(toc);
|
||||||
void locateToc();
|
locateToc();
|
||||||
}
|
}
|
||||||
|
|
||||||
function debounce(func, wait) {
|
function initToc() {
|
||||||
let timeout;
|
if (resizeListenerRegistered) return;
|
||||||
return function executedFunction(...args) {
|
window.addEventListener("resize", debounce(handleResize, RESIZE_DEBOUNCE_MS));
|
||||||
const later = () => {
|
resizeListenerRegistered = true;
|
||||||
clearTimeout(timeout);
|
|
||||||
func(...args);
|
|
||||||
};
|
|
||||||
clearTimeout(timeout);
|
|
||||||
timeout = setTimeout(later, wait);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function destroyToc() {
|
|
||||||
const toc = document.getElementById("toc")
|
|
||||||
if (!toc) return;
|
|
||||||
toc.style.display = "none";
|
|
||||||
toc.remove();
|
|
||||||
if (typeof tocbot !== 'undefined') {
|
|
||||||
tocbot.destroy();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function init_toc() {
|
|
||||||
void generateToc();
|
|
||||||
window.addEventListener('resize', debounce(handleResize, 250));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
function registerToc() {
|
|
||||||
document.addEventListener('DOMContentLoaded', () => {
|
|
||||||
void init_toc();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
terminal.registerRefresh(generateToc);
|
terminal.registerRefresh(generateToc);
|
||||||
terminal.registerInitFunc(registerToc);
|
terminal.registerInitFunc(initToc);
|
||||||
|
|||||||
+4
-2
@@ -6,10 +6,12 @@ export class Terminal {
|
|||||||
}
|
}
|
||||||
|
|
||||||
registerRefresh(func) {
|
registerRefresh(func) {
|
||||||
|
if (typeof func !== "function") return;
|
||||||
this._refreshFunctions.push(func);
|
this._refreshFunctions.push(func);
|
||||||
}
|
}
|
||||||
|
|
||||||
registerInitFunc(func) {
|
registerInitFunc(func) {
|
||||||
|
if (typeof func !== "function") return;
|
||||||
this._initFunctions.push(func);
|
this._initFunctions.push(func);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -22,5 +24,5 @@ export class Terminal {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export var terminal = new Terminal();
|
export const terminal = new Terminal();
|
||||||
window.terminal = terminal;
|
window.terminal = terminal;
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
export function debounce(callback, wait = 250) {
|
||||||
|
let timeoutId;
|
||||||
|
|
||||||
|
return (...args) => {
|
||||||
|
window.clearTimeout(timeoutId);
|
||||||
|
timeoutId = window.setTimeout(() => callback(...args), wait);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function toggleClass(element, className, enabled) {
|
||||||
|
if (!element) return;
|
||||||
|
element.classList.toggle(className, enabled);
|
||||||
|
}
|
||||||
@@ -10,10 +10,10 @@
|
|||||||
<div class="post-content">
|
<div class="post-content">
|
||||||
<p th:text="${error?.detail}"></p>
|
<p th:text="${error?.detail}"></p>
|
||||||
</div>
|
</div>
|
||||||
<button th:onclick="'location.href=\'' + @{/} + '\''" th:text="#{page.error.back}">
|
<a class="button" data-pjax th:href="@{/}" th:text="#{page.error.back}">
|
||||||
Back to home page
|
Back to home page
|
||||||
</button>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</th:block>
|
</th:block>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -10,10 +10,10 @@
|
|||||||
<div class="post-content">
|
<div class="post-content">
|
||||||
<p th:text="${error?.detail}"></p>
|
<p th:text="${error?.detail}"></p>
|
||||||
</div>
|
</div>
|
||||||
<button th:onclick="'location.href=\'' + @{/} + '\''" th:text="#{page.error.back}">
|
<a class="button" data-pjax th:href="@{/}" th:text="#{page.error.back}">
|
||||||
Back to home page
|
Back to home page
|
||||||
</button>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</th:block>
|
</th:block>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -10,10 +10,10 @@
|
|||||||
<div class="post-content">
|
<div class="post-content">
|
||||||
<p th:text="${error?.detail}"></p>
|
<p th:text="${error?.detail}"></p>
|
||||||
</div>
|
</div>
|
||||||
<button th:onclick="'location.href=\'' + @{/} + '\''" th:text="#{page.error.back}">
|
<a class="button" data-pjax th:href="@{/}" th:text="#{page.error.back}">
|
||||||
Back to home page
|
Back to home page
|
||||||
</button>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</th:block>
|
</th:block>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -12,8 +12,8 @@
|
|||||||
id="search-button"
|
id="search-button"
|
||||||
aria-label="Open Search"
|
aria-label="Open Search"
|
||||||
type="button"
|
type="button"
|
||||||
th:if="${pluginFinder.available('PluginSearchWidget')}"
|
data-header-action="open-search"
|
||||||
onclick="SearchWidget.open()">
|
th:if="${pluginFinder.available('PluginSearchWidget')}">
|
||||||
<svg class="icon-regular button-icon" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
|
<svg class="icon-regular button-icon" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
|
||||||
<path
|
<path
|
||||||
fill="currentColor"
|
fill="currentColor"
|
||||||
|
|||||||
+115
-82
@@ -10,80 +10,135 @@
|
|||||||
<title th:text="${title}"></title>
|
<title th:text="${title}"></title>
|
||||||
<script th:if="${theme.config.basic.assert_enable}" no-pjax th:inline="javascript">
|
<script th:if="${theme.config.basic.assert_enable}" no-pjax th:inline="javascript">
|
||||||
(function () {
|
(function () {
|
||||||
let code = /*[[${theme.config.basic.assert_code}]]*/;
|
const assertCode = /*[[${theme.config.basic.assert_code}]]*/ "";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
new Function(code)();
|
new Function(assertCode)();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
location.href = 'about:blank';
|
location.href = 'about:blank';
|
||||||
setTimeout( () => document.close(), 0.1);
|
setTimeout(() => document.close(), 0.1);
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
<script no-pjax th:inline="javascript">
|
<script no-pjax th:inline="javascript">
|
||||||
let system_font_enable = /*[[${theme.config.style.system_font_enable}]]*/;
|
window.TerminalThemeConfig = {
|
||||||
document.documentElement.setAttribute("system_font_enable", system_font_enable);
|
systemFontEnabled: /*[[${theme.config.style.system_font_enable}]]*/ false,
|
||||||
</script>
|
darkTheme: /*[[${theme.config.style.dark_color_scheme}]]*/ "dark",
|
||||||
<script no-pjax th:inline="javascript">
|
lightTheme: /*[[${theme.config.style.light_color_scheme}]]*/ "light",
|
||||||
let dark_theme = /*[[${theme.config.style.dark_color_scheme}]]*/;
|
customDarkTheme: /*[[${theme.config.style.custom_dark}]]*/ "",
|
||||||
let light_theme = /*[[${theme.config.style.light_color_scheme}]]*/;
|
customLightTheme: /*[[${theme.config.style.custom_light}]]*/ "",
|
||||||
const default_mode = /*[[${theme.config.style.default_scheme_mode}]]*/;
|
defaultMode: /*[[${theme.config.style.default_scheme_mode}]]*/ "system",
|
||||||
|
pixelStyle: /*[[${theme.config.style.pixel_style}]]*/ false,
|
||||||
if (dark_theme === 'custom') {
|
pixelCssHref: /*[[@{/assets/dist/pixel.css?v={version}(version=${theme.spec.version})}]]*/ "",
|
||||||
dark_theme = /*[[${theme.config.style.custom_dark}]]*/
|
customCss: /*[[${theme.config.style.custom}]]*/ ""
|
||||||
}
|
};
|
||||||
if (light_theme === 'custom') {
|
|
||||||
light_theme = /*[[${theme.config.style.custom_light}]]*/
|
|
||||||
}
|
|
||||||
|
|
||||||
let storedMode = localStorage.getItem("theme-mode");
|
|
||||||
|
|
||||||
function getSystemScheme() {
|
|
||||||
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
|
|
||||||
}
|
|
||||||
|
|
||||||
function applyTheme(mode) {
|
|
||||||
const actualMode = mode === 'system' ? getSystemScheme() : mode;
|
|
||||||
const targetTheme = actualMode === "light" ? light_theme : dark_theme;
|
|
||||||
document.documentElement.setAttribute("data-color-scheme", actualMode);
|
|
||||||
document.documentElement.setAttribute("theme-color-scheme", targetTheme);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if (default_mode === 'system' || !storedMode) {
|
|
||||||
localStorage.setItem("theme-mode", default_mode);
|
|
||||||
storedMode = default_mode;
|
|
||||||
}
|
|
||||||
|
|
||||||
applyTheme(storedMode);
|
|
||||||
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => {
|
|
||||||
if (localStorage.getItem("theme-mode") === "system") {
|
|
||||||
applyTheme("system");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
</script>
|
</script>
|
||||||
<script no-pjax>
|
<script no-pjax>
|
||||||
const pixel_status = [[${theme.config.style.pixel_style}]];
|
(function () {
|
||||||
let storedStatus = localStorage.getItem("pixel_style");
|
const config = window.TerminalThemeConfig || {};
|
||||||
if (!storedStatus) {
|
const root = document.documentElement;
|
||||||
localStorage.setItem("pixel_style", pixel_status);
|
const storageKeys = {
|
||||||
storedStatus = pixel_status.toString();
|
themeMode: "theme-mode",
|
||||||
}
|
pixelStyle: "pixel_style"
|
||||||
document.documentElement.setAttribute("pixel_style", storedStatus);
|
};
|
||||||
|
|
||||||
|
function resolveTheme(scheme) {
|
||||||
|
if (scheme === "light") {
|
||||||
|
return config.lightTheme === "custom" ? config.customLightTheme : config.lightTheme;
|
||||||
|
}
|
||||||
|
return config.darkTheme === "custom" ? config.customDarkTheme : config.darkTheme;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSystemScheme() {
|
||||||
|
return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
|
||||||
|
}
|
||||||
|
|
||||||
|
function applyTheme(mode) {
|
||||||
|
const actualMode = mode === "system" ? getSystemScheme() : mode;
|
||||||
|
root.setAttribute("data-color-scheme", actualMode);
|
||||||
|
root.setAttribute("theme-color-scheme", resolveTheme(actualMode));
|
||||||
|
}
|
||||||
|
|
||||||
|
function syncSystemFont() {
|
||||||
|
root.setAttribute("system_font_enable", String(config.systemFontEnabled));
|
||||||
|
}
|
||||||
|
|
||||||
|
function syncPixelStyle() {
|
||||||
|
let storedStatus = localStorage.getItem(storageKeys.pixelStyle);
|
||||||
|
|
||||||
|
if (!storedStatus) {
|
||||||
|
storedStatus = String(config.pixelStyle);
|
||||||
|
localStorage.setItem(storageKeys.pixelStyle, storedStatus);
|
||||||
|
}
|
||||||
|
|
||||||
|
root.setAttribute("pixel_style", storedStatus);
|
||||||
|
}
|
||||||
|
|
||||||
|
function syncPixelFont() {
|
||||||
|
const existingLink = document.getElementById("terminal-pixel-font");
|
||||||
|
const pixelEnabled = root.getAttribute("pixel_style") === "true";
|
||||||
|
|
||||||
|
if (!pixelEnabled && existingLink) {
|
||||||
|
existingLink.remove();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!pixelEnabled || existingLink || !config.pixelCssHref) return;
|
||||||
|
|
||||||
|
const link = document.createElement("link");
|
||||||
|
link.id = "terminal-pixel-font";
|
||||||
|
link.setAttribute("no-pjax", "");
|
||||||
|
link.rel = "stylesheet";
|
||||||
|
link.href = config.pixelCssHref;
|
||||||
|
document.head.appendChild(link);
|
||||||
|
}
|
||||||
|
|
||||||
|
function applyCustomCss(text = config.customCss) {
|
||||||
|
if (!text || document.getElementById("terminal-custom-css")) return;
|
||||||
|
|
||||||
|
const style = document.createElement("style");
|
||||||
|
style.id = "terminal-custom-css";
|
||||||
|
style.textContent = text;
|
||||||
|
document.head.appendChild(style);
|
||||||
|
}
|
||||||
|
|
||||||
|
function syncThemeMode() {
|
||||||
|
let storedMode = localStorage.getItem(storageKeys.themeMode);
|
||||||
|
|
||||||
|
if (config.defaultMode === "system" || !storedMode) {
|
||||||
|
storedMode = config.defaultMode;
|
||||||
|
localStorage.setItem(storageKeys.themeMode, storedMode);
|
||||||
|
}
|
||||||
|
|
||||||
|
applyTheme(storedMode);
|
||||||
|
}
|
||||||
|
|
||||||
|
window.applyTheme = applyTheme;
|
||||||
|
window.TerminalTheme = {
|
||||||
|
applyTheme,
|
||||||
|
applyCustomCss,
|
||||||
|
syncPixelFont,
|
||||||
|
syncPixelStyle
|
||||||
|
};
|
||||||
|
|
||||||
|
syncSystemFont();
|
||||||
|
syncThemeMode();
|
||||||
|
syncPixelStyle();
|
||||||
|
syncPixelFont();
|
||||||
|
|
||||||
|
window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", () => {
|
||||||
|
if (localStorage.getItem(storageKeys.themeMode) === "system") {
|
||||||
|
applyTheme("system");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
})();
|
||||||
</script>
|
</script>
|
||||||
<link no-pjax rel="stylesheet" th:href="@{/assets/dist/main.css?v={version}(version=${theme.spec.version})}"/>
|
<link no-pjax rel="stylesheet" th:href="@{/assets/dist/main.css?v={version}(version=${theme.spec.version})}"/>
|
||||||
<script no-pjax type="module" th:src="@{/assets/dist/main.js?v={version}(version=${theme.spec.version})}"></script>
|
<script no-pjax type="module" th:src="@{/assets/dist/main.js?v={version}(version=${theme.spec.version})}"></script>
|
||||||
<script no-pjax type="module" th:if="${theme.config.basic.pjax_enable}"
|
<script no-pjax type="module" th:if="${theme.config.basic.pjax_enable}"
|
||||||
th:src="@{/assets/dist/libs/pjax.js?v={version}(version=${theme.spec.version})}"></script>
|
th:src="@{/assets/dist/libs/pjax.js?v={version}(version=${theme.spec.version})}"></script>
|
||||||
<script no-pjax th:inline="javascript">
|
<script no-pjax>
|
||||||
function applyRawCss(text) {
|
window.TerminalTheme?.applyCustomCss?.();
|
||||||
const style = document.createElement('style');
|
|
||||||
style.textContent = text;
|
|
||||||
document.head.appendChild(style);
|
|
||||||
}
|
|
||||||
|
|
||||||
const custom = /*[[${theme.config.style.custom}]]*/;
|
|
||||||
applyRawCss(custom);
|
|
||||||
</script>
|
</script>
|
||||||
<th:block
|
<th:block
|
||||||
th:if="${theme.config?.style?.custom_font_enable and not theme.config?.style?.system_font_enable}"
|
th:if="${theme.config?.style?.custom_font_enable and not theme.config?.style?.system_font_enable}"
|
||||||
@@ -120,28 +175,6 @@
|
|||||||
</style>
|
</style>
|
||||||
/*/-->
|
/*/-->
|
||||||
</th:block>
|
</th:block>
|
||||||
<script no-pjax th:inline="javascript">
|
|
||||||
function appendPixelFont() {
|
|
||||||
const pixel_style = document.documentElement.getAttribute('pixel_style');
|
|
||||||
|
|
||||||
const link = document.createElement('link');
|
|
||||||
link.setAttribute('no-pjax', '');
|
|
||||||
link.rel = 'stylesheet';
|
|
||||||
|
|
||||||
if (pixel_style === 'true') {
|
|
||||||
link.href = /*[[@{/assets/dist/pixel.css?v={version}(version=${theme.spec.version})}]]*/;
|
|
||||||
}
|
|
||||||
document.head.appendChild(link);
|
|
||||||
}
|
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', () => {
|
|
||||||
appendPixelFont();
|
|
||||||
|
|
||||||
document.getElementById('pixel-toggle').addEventListener('click', () => {
|
|
||||||
appendPixelFont();
|
|
||||||
})
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body th:attr="layout_mode=${theme.config.style.fullscreen_layout ? 'fullscreen' : 'normal'}">
|
<body th:attr="layout_mode=${theme.config.style.fullscreen_layout ? 'fullscreen' : 'normal'}">
|
||||||
|
|||||||
@@ -17,7 +17,7 @@
|
|||||||
mediaGridClass=${mediaSize == 1 ? 'grid-cols-1' : (mediaSize < 5 ? 'grid-cols-2' : 'grid-cols-3')}
|
mediaGridClass=${mediaSize == 1 ? 'grid-cols-1' : (mediaSize < 5 ? 'grid-cols-2' : 'grid-cols-3')}
|
||||||
"
|
"
|
||||||
class="moment on-list post"
|
class="moment on-list post"
|
||||||
th:attr="x-data=|{ name: '${name}', showComment: false }|">
|
th:attr="data-moment-name=${name}">
|
||||||
|
|
||||||
<header>
|
<header>
|
||||||
<div class="post-meta">
|
<div class="post-meta">
|
||||||
@@ -58,8 +58,7 @@
|
|||||||
|
|
||||||
<footer class="moment-footer">
|
<footer class="moment-footer">
|
||||||
<button class="moment-action-btn moment-upvote-btn"
|
<button class="moment-action-btn moment-upvote-btn"
|
||||||
th:attr="data-name=${name},data-count=${upvoteCount}"
|
th:attr="data-moment-action='upvote',data-moment-name=${name},data-count=${upvoteCount}">
|
||||||
onclick="handleUpvote(this.dataset.name)">
|
|
||||||
<svg class="icon-regular" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"
|
<svg class="icon-regular" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"
|
||||||
fill="currentColor" width="16" height="16">
|
fill="currentColor" width="16" height="16">
|
||||||
<path d="M12 21.35l-1.45-1.32C5.4 15.36 2 12.28 2 8.5 2 5.42 4.42 3 7.5 3c1.74 0 3.41.81 4.5 2.09C13.09 3.81 14.76 3 16.5 3 19.58 3 22 5.42 22 8.5c0 3.78-3.4 6.86-8.55 11.54L12 21.35z"/>
|
<path d="M12 21.35l-1.45-1.32C5.4 15.36 2 12.28 2 8.5 2 5.42 4.42 3 7.5 3c1.74 0 3.41.81 4.5 2.09C13.09 3.81 14.76 3 16.5 3 19.58 3 22 5.42 22 8.5c0 3.78-3.4 6.86-8.55 11.54L12 21.35z"/>
|
||||||
@@ -75,8 +74,7 @@
|
|||||||
</button>
|
</button>
|
||||||
|
|
||||||
<button class="moment-action-btn moment-comment-btn"
|
<button class="moment-action-btn moment-comment-btn"
|
||||||
th:attr="data-target=${commentId}"
|
th:attr="data-moment-action='toggle-comment',data-comment-target=${commentId}">
|
||||||
onclick="showComment(this.dataset.target)">
|
|
||||||
<svg class="icon-regular" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"
|
<svg class="icon-regular" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"
|
||||||
fill="currentColor" width="16" height="16">
|
fill="currentColor" width="16" height="16">
|
||||||
<path d="M20 2H4c-1.1 0-2 .9-2 2v18l4-4h14c1.1 0 2-.9 2-2V4c0-1.1-.9-2-2-2z"/>
|
<path d="M20 2H4c-1.1 0-2 .9-2 2v18l4-4h14c1.1 0 2-.9 2-2V4c0-1.1-.9-2-2-2z"/>
|
||||||
|
|||||||
Reference in New Issue
Block a user