mirror of
https://github.com/hugo-fixit/FixIt.git
synced 2026-08-24 23:38:56 +00:00
17d7bc1781
* feat(assets): refactor SCSS variables with Hugo config via hugo:vars Use Hugo's hugo:vars mechanism (v0.161.0+) to configure SCSS variables via [params.appearance] in hugo.toml, replacing the override.scss file. - Add [params.appearance] config section in hugo.toml - Create scss-vars.html partial for SCSS default values - Pass vars to toCSS via head/index.html - Rewrite _variables.scss to use @use "hugo:vars" as h with !default - Update all 20 consumer files from @use "override" to @use "variables" - Delete override.scss - Add admonition mixin in core/mixins/_admonition.scss - Update deprecation detection to use fileExists * refactor(assets): standardize module setup() and simplify main.ts init - Add setup() method to all modules (menu, theme, search, enc, misc, events) - Rename initMenu to setup, initMenuDesktop/initMenuMobile to initDesktop/initMobile - Rename initSearch, initFixItDecryptor to setup - Standardize setup() position at end of each module class and interface - Simplify main.ts init sequence to uniform setup() calls * refactor(assets): replace config.template.scss with hugo:vars/internal - Delete config.template.scss and its ExecuteAsTemplate pipeline - Pass internal config (base URL, logo, loading) via hugo:vars/internal namespace - Move CSS custom properties to _root.scss using set-fi-vars mixin - Simplify _variables.scss by removing config namespace dependency - Update CLAUDE.md to reflect new SCSS architecture * style(i18n): fix spacing in Korean translation strings * fix(assets): resolve dead SCSS config keys and deduplicate defaults - Wire up 5 config keys with v.$ references: menu-active-color, tag-cloud-start, tag-cloud-end, tag-cloud-end-dark, pagination-link-color-dark (were reading from SCSS vars, ignoring hugo:vars overrides) - Remove dead !default flags from all v.$ references (hugo:vars always provides values, making !default unreachable) - Compute derived defaults in scss-vars.html from base values instead of hardcoding duplicates - Extract admonition mixin set-fi-vars map to shared $vars variable
177 lines
6.6 KiB
TypeScript
177 lines
6.6 KiB
TypeScript
import type { CodeService, CoreService, EventsService, TocService } from '../core/tokens'
|
|
import { eventBus } from '../core/event-bus'
|
|
import { animateCSS, getScrollTop, isMobile, scrollIntoView } from '../utils'
|
|
|
|
/**
|
|
* Events module — scroll, resize, mask click, and print event handling.
|
|
*
|
|
* Responsibilities:
|
|
* - Throttled scroll handler for back-to-top button, header show/hide, and TOC active state sync.
|
|
* - Resize handler for TOC height and table wrapping recalculation.
|
|
* - Mask click handler to close search and menu drawers.
|
|
* - Print preparation handler.
|
|
*/
|
|
export class EventsModule implements EventsService {
|
|
#resizeTimeout: number | null = null
|
|
#newScrollTop = 0
|
|
#oldScrollTop = 0
|
|
|
|
constructor(
|
|
private readonly core: CoreService,
|
|
private readonly toc: TocService,
|
|
private readonly code: CodeService,
|
|
) {}
|
|
|
|
/** Bind scroll listener: auto-hide headers, reading progress, back-to-top, and TOC sync. */
|
|
onScroll() {
|
|
const ACCURACY = 50
|
|
const $autoHeaders: HTMLElement[] = []
|
|
const $backToTop = document.querySelector<HTMLElement>('.back-to-top')
|
|
const $readingProgressBar = document.querySelector<HTMLElement>('.reading-progress-bar')
|
|
if (document.body.dataset.headerDesktop === 'auto') {
|
|
$autoHeaders.push(document.getElementById('header-desktop')!)
|
|
}
|
|
if (document.body.dataset.headerMobile === 'auto') {
|
|
$autoHeaders.push(document.getElementById('header-mobile')!)
|
|
}
|
|
$backToTop?.addEventListener('click', () => {
|
|
scrollIntoView('body')
|
|
})
|
|
window.addEventListener('scroll', () => {
|
|
this.#newScrollTop = getScrollTop()
|
|
const scroll = this.#newScrollTop - this.#oldScrollTop
|
|
if (Math.abs(scroll) > ACCURACY) {
|
|
this.core.closeActiveMaskOverlay()
|
|
const isScrollingDown = scroll > 0
|
|
$autoHeaders.forEach(($header) => {
|
|
if (isScrollingDown) {
|
|
$header.classList.remove('header__fadeInDown')
|
|
animateCSS($header, ['header__fadeOutUp'], true)
|
|
}
|
|
else {
|
|
$header.classList.remove('header__fadeOutUp')
|
|
animateCSS($header, ['header__fadeInDown'], true)
|
|
}
|
|
})
|
|
}
|
|
else if (this.#newScrollTop <= 0) {
|
|
$autoHeaders.forEach(($header) => {
|
|
$header.classList.remove('header__fadeOutUp')
|
|
animateCSS($header, ['header__fadeInDown'], true)
|
|
})
|
|
}
|
|
const contentHeight = document.body.scrollHeight - window.innerHeight
|
|
const scrollPercent = Math.max(Math.min(100 * Math.max(this.#newScrollTop, 0) / contentHeight, 100), 0)
|
|
if ($readingProgressBar) {
|
|
$readingProgressBar.style.setProperty('--fi-progress', `${scrollPercent.toFixed(2)}%`)
|
|
}
|
|
if ($backToTop) {
|
|
if (scrollPercent > 1) {
|
|
$backToTop.classList.remove('d-none', 'animate__fadeOut')
|
|
animateCSS($backToTop, ['animate__fadeIn'], true)
|
|
}
|
|
else {
|
|
$backToTop.classList.remove('animate__fadeIn')
|
|
animateCSS($backToTop, ['animate__fadeOut'], true, () => {
|
|
$backToTop.classList.contains('animate__fadeOut') && $backToTop.classList.add('d-none')
|
|
})
|
|
}
|
|
$backToTop.style.setProperty('--fi-b2t-progress', scrollPercent.toFixed(2))
|
|
if (navigator.userAgent.toLowerCase().includes('firefox')) {
|
|
const dashoffset = 2 * Math.PI * 50 * (1 - scrollPercent / 100)
|
|
$backToTop.querySelector<SVGCircleElement>('circle.progress')!.style.strokeDashoffset = String(dashoffset.toFixed(2))
|
|
}
|
|
}
|
|
eventBus.emit('fixit:scroll')
|
|
this.toc.syncTocHeight()
|
|
this.toc.syncTocActiveState()
|
|
this.#oldScrollTop = this.#newScrollTop
|
|
}, false)
|
|
}
|
|
|
|
/** Bind resize listener with debounce: re-init TOC, search, and sync state. */
|
|
onResize() {
|
|
let resizeBefore = isMobile()
|
|
window.addEventListener('resize', () => {
|
|
if (!this.#resizeTimeout) {
|
|
this.#resizeTimeout = window.setTimeout(() => {
|
|
this.#resizeTimeout = null
|
|
eventBus.emit('fixit:resize')
|
|
this.toc.initToc()
|
|
this.toc.syncTocHeight()
|
|
this.toc.syncTocActiveState()
|
|
|
|
const _isMobile = isMobile()
|
|
if (_isMobile !== resizeBefore) {
|
|
this.core.closeActiveMaskOverlay()
|
|
resizeBefore = _isMobile
|
|
}
|
|
}, 100)
|
|
}
|
|
}, false)
|
|
}
|
|
|
|
/** Bind mask click to close the active overlay. */
|
|
onClickMask() {
|
|
document.getElementById('mask')!.addEventListener('click', (e) => {
|
|
if (!(e.target as HTMLElement).classList.contains('blur'))
|
|
return
|
|
this.core.closeActiveMaskOverlay()
|
|
}, false)
|
|
}
|
|
|
|
/** Bind beforeprint/afterprint to expand admonitions, code blocks, details, and file trees. */
|
|
initPrint() {
|
|
window.addEventListener('beforeprint', () => {
|
|
const $content = document.getElementById('content')!
|
|
const printConfig = this.core.config.print || {}
|
|
|
|
if (printConfig.expandAdmonition) {
|
|
$content.querySelectorAll('.admonition').forEach(($el: Element) => $el.classList.add('open'))
|
|
}
|
|
if (printConfig.expandCode) {
|
|
$content.querySelectorAll<HTMLElement>('.code-tabs').forEach(($codeTabs) => {
|
|
if ($codeTabs.dataset.diagram)
|
|
return
|
|
const $actions = $codeTabs.querySelector<HTMLElement>('.tabs-actions')
|
|
const $activeBlock = $codeTabs.querySelector<HTMLElement>('.code-block.active')
|
|
if ($actions && $activeBlock) {
|
|
const $codeHeader = $activeBlock.querySelector<HTMLElement>('.code-header')
|
|
if ($codeHeader) {
|
|
Array.from($actions.children).forEach(btn => $codeHeader.appendChild(btn))
|
|
}
|
|
}
|
|
const $codeBlocks = $codeTabs.querySelectorAll<HTMLElement>('.code-block')
|
|
$codeBlocks.forEach(($codeBlock) => {
|
|
delete $codeBlock.dataset.tabInit
|
|
$codeTabs.parentElement!.insertBefore($codeBlock, $codeTabs)
|
|
})
|
|
$codeTabs.parentElement!.removeChild($codeTabs)
|
|
})
|
|
$content.querySelectorAll<HTMLElement>('.code-block').forEach(($el) => {
|
|
$el.classList.add('line-wrapping')
|
|
$el.classList.remove('is-collapsed')
|
|
if ($el.querySelector('.code-expand-btn')) {
|
|
$el.classList.add('is-expanded')
|
|
}
|
|
})
|
|
}
|
|
if (printConfig.expandDetails) {
|
|
$content.querySelectorAll('details').forEach(($el: Element) => $el.setAttribute('open', ''))
|
|
}
|
|
}, false)
|
|
|
|
window.addEventListener('afterprint', () => {
|
|
this.code.initCodeTabs()
|
|
}, false)
|
|
}
|
|
|
|
/** Initialize all event listeners. */
|
|
setup() {
|
|
this.onScroll()
|
|
this.onResize()
|
|
this.onClickMask()
|
|
this.initPrint()
|
|
}
|
|
}
|