#!/usr/bin/env python3 from pathlib import Path import gzip import re ASSETS_ROOT = Path('/app') def rewrite(path: Path, text: str) -> None: path.write_text(text, encoding='utf-8') gz_path = path.with_suffix(path.suffix + '.gz') if gz_path.exists(): with gzip.open(gz_path, 'wb', compresslevel=9) as gz: gz.write(text.encode('utf-8')) def patch_file_manage(path: Path) -> bool: """Do not mutate Vite's shared file-management bundle. It imports the clipboard chunk, which is also shared by the upload/download service. Structural text replacement in a minified bundle is unsafe and can invalidate the module graph. Lifecycle presentation is handled by the standalone DOM script below instead. """ return False def patch_clipboard(path: Path) -> bool: """Never rewrite Vite's shared clipboard chunk after compilation. The source utility already implements Clipboard API, DOM and manual-copy fallbacks. Rewriting a minified ESM module can invalidate every page that imports it, including FileManageView. """ return False def build_lifecycle_script() -> str: return r'''(() => { const LIFE_RE = /(生命周期|Lifecycle)/i; const RETRIEVAL_RE = /^(第\s*\d+\s*次取件|retrieval\s*#?\d+)/i; const STATE = new WeakMap(); const getSection = section => { const title = section.querySelector('h1,h2,h3,h4,h5'); if (!title || !LIFE_RE.test(title.textContent || '')) return null; const box = title.nextElementSibling; if (!box) return null; return { title, box }; }; const getRows = section => { const ctx = getSection(section); if (!ctx) return []; return [...ctx.box.querySelectorAll(':scope > .fcb-expand-content')]; }; const apply = section => { const ctx = getSection(section); if (!ctx) return; const rows = getRows(section); const events = rows.filter(row => RETRIEVAL_RE.test((row.querySelector('p')?.textContent || '').trim())); if (events.length <= 3) return; const expanded = STATE.get(section) === true; events.forEach((row, idx) => { row.classList.add('fcbx-retrieval-event'); row.style.display = expanded || idx < 3 ? '' : 'none'; }); let button = section.querySelector(':scope > .fcbx-retrieval-toggle'); if (!button) { button = document.createElement('button'); button.type = 'button'; button.className = 'fcbx-retrieval-toggle'; button.style.cssText = 'display:inline-flex;align-items:center;margin:0 0 12px 44px;padding:8px 12px;border:1px solid rgba(148,163,184,.55);border-radius:10px;background:transparent;cursor:pointer;touch-action:manipulation;'; button.onclick = event => { event.preventDefault(); event.stopPropagation(); STATE.set(section, STATE.get(section) !== true); apply(section); }; ctx.box.parentNode.insertBefore(button, ctx.box); } button.textContent = expanded ? '收起取件记录' : `展开全部取件记录(共 ${events.length} 次)`; button.setAttribute('aria-expanded', expanded ? 'true' : 'false'); }; const markRows = root => { const scope = root?.querySelectorAll ? root : document; scope.querySelectorAll('section').forEach(section => { const ctx = getSection(section); if (!ctx) return; ctx.box.style.overflow = 'visible'; if (ctx.box.parentElement) ctx.box.parentElement.style.overflow = 'visible'; getRows(section).forEach(row => { row.style.overflow = 'visible'; row.style.position = 'relative'; row.style.zIndex = '0'; const card = row.lastElementChild; if (card) { card.style.overflow = 'visible'; card.style.position = 'relative'; } row.onmouseenter = () => { row.style.zIndex = '2'; }; row.onmouseleave = () => { row.style.zIndex = '0'; }; }); apply(section); }); }; let raf = 0; const schedule = root => { cancelAnimationFrame(raf); raf = requestAnimationFrame(() => markRows(root)); }; window.addEventListener('hashchange', () => schedule(document)); document.addEventListener('DOMContentLoaded', () => schedule(document)); new MutationObserver(() => schedule(document)).observe(document.body || document.documentElement, { childList: true, subtree: true }); schedule(document); })();''' def write_detail_collapse_script(theme_dir: Path) -> bool: assets = theme_dir / 'assets' index = theme_dir / 'index.html' if not assets.exists() or not index.exists(): return False js_path = assets / 'fcb-lifecycle-collapse-v3.js' js = build_lifecycle_script() rewrite(js_path, js) html = index.read_text(encoding='utf-8') # Remove old detail-collapse injection so browsers cannot keep loading the stale “取件记录” script. html = html.replace('\n', '') html = html.replace('', '') html = html.replace('\n', '') html = html.replace('', '') tag = '' if 'fcb-lifecycle-collapse-v3.js' not in html: html = html.replace('', tag + '\n') if '' in html else html + '\n' + tag + '\n' index.write_text(html, encoding='utf-8') gz = index.with_suffix(index.suffix + '.gz') if gz.exists(): with gzip.open(gz, 'wb', compresslevel=9) as fh: fh.write(html.encode('utf-8')) return True def main() -> None: changed = [] for theme in (ASSETS_ROOT / 'themes/2024', ASSETS_ROOT / 'themes/2026'): assets = theme / 'assets' if assets.exists(): for path in assets.glob('clipboard-*.js'): if patch_clipboard(path): changed.append(str(path.relative_to(ASSETS_ROOT))) if write_detail_collapse_script(theme): changed.append(str((assets / 'fcb-lifecycle-collapse-v3.js').relative_to(ASSETS_ROOT))) if not changed: # The standalone lifecycle script is idempotently regenerated even when # the clipboard bundle was already patched in a cached build layer. return print('patched admin assets:', ', '.join(changed)) if __name__ == '__main__': main()