Files
ArticleToObsidian/manage.php

841 lines
34 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* 文章导出(后台菜单「文章导出」)
*
* 从老插件 ArticleExport 平移过来:按年份 / 分类 / 标签 / 全部,
* 分批把文章导出成 Markdown 并打包成 zip 下载。
* 与 Plugin.php 的自动同步互不影响,只是共用一个菜单入口。
*/
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
// 增加超时和内存限制
set_time_limit(0);
ini_set('memory_limit', '1024M');
$db = Typecho_Db::get();
$prefix = $db->getPrefix();
// ==================== 导出处理 ====================
if (isset($_POST['do_export'])) {
$exportType = $_POST['export_type'];
$exportValue = $_POST['export_value'];
$batch = isset($_POST['batch']) ? intval($_POST['batch']) : 0;
$batchSize = 300; // 每批导出300篇
// 构建查询条件
$whereClause = "";
if ($exportType == 'year' && !empty($exportValue)) {
$year = intval($exportValue);
$start = strtotime($year . '-01-01');
$end = strtotime($year . '-12-31');
$whereClause = " AND created >= " . intval($start) . " AND created <= " . intval($end);
} elseif ($exportType == 'category' && !empty($exportValue)) {
$mid = intval($exportValue);
$whereClause = " AND cid IN (SELECT cid FROM " . $prefix . "relationships WHERE mid = " . $mid . ")";
} elseif ($exportType == 'tag' && !empty($exportValue)) {
$mid = intval($exportValue);
$whereClause = " AND cid IN (SELECT cid FROM " . $prefix . "relationships WHERE mid = " . $mid . ")";
}
// 获取总文章数
$totalSql = "SELECT COUNT(*) as total FROM " . $prefix . "contents WHERE type = 'post' AND status = 'publish'" . $whereClause;
$totalResult = $db->fetchRow($db->query($totalSql));
$totalPosts = $totalResult['total'];
// 如果是第一次请求(非分批),检查是否需要分批
if ($batch === 0 && $totalPosts > $batchSize) {
// 需要分批导出,返回提示页面
$totalBatches = ceil($totalPosts / $batchSize);
/* 这一页是 target="_blank" 打开的独立页面(没有 header.php /
menu.php,不在 #newadmin 框架里),拿不到 NewAdmin 的深浅色,
直接读同一个 cookie 跟随后台主题 —— 用户在后台选了深色,
这里就跟着是深色,两边观感一致。 */
$isDarkPage = (isset($_COOKIE['dark']) && $_COOKIE['dark'] === 'dark');
?>
<!DOCTYPE html>
<html<?php echo $isDarkPage ? ' class="ae-dark"' : ''; ?>>
<head>
<meta charset="utf-8">
<title>分批导出</title>
<style>
body { font-family: system-ui, -apple-system, sans-serif; background: #f5f5f5; display: flex; justify-content: center; align-items: center; min-height: 100vh; margin: 0; padding: 20px; }
.container { background: #fff; border-radius: 16px; padding: 32px; max-width: 500px; width: 100%; box-shadow: 0 4px 20px rgba(0,0,0,0.1); }
h2 { margin: 0 0 16px 0; color: #1a2a3a; }
.info { background: #f8f9fc; border-left: 4px solid #667eea; padding: 16px; margin-bottom: 24px; border-radius: 8px; }
.info p { margin: 8px 0; }
.total { font-size: 24px; font-weight: bold; color: #667eea; }
.batch-list { display: flex; flex-direction: column; gap: 10px; margin-bottom: 24px; max-height: 400px; overflow-y: auto; }
.batch-item { display: flex; justify-content: space-between; align-items: center; padding: 12px 16px; background: #f8f9fa; border-radius: 8px; }
.batch-item span { font-size: 14px; }
.btn { padding: 8px 20px; background: #28a745; color: #fff; border: none; border-radius: 8px; cursor: pointer; text-decoration: none; font-size: 14px; }
.btn:hover { background: #218838; }
.btn-secondary { background: #6c757d; }
.btn-secondary:hover { background: #5a6268; }
.export-all { margin-top: 20px; padding-top: 20px; border-top: 1px solid #e9ecef; text-align: center; }
/* ---------- 深色(跟随后台 cookie,class 由服务端打在 <html> 上) ----------
取值与后台 #newadmin.dark 那一套完全一致。 */
.ae-dark { color-scheme:dark; }
.ae-dark body { background:#1f2937; color:#c3d1de; }
.ae-dark .container { background:#17212f; box-shadow:0 4px 20px rgba(0,0,0,.5); }
.ae-dark h2 { color:#e6edf3; }
.ae-dark .info { background:#1a2634; border-left-color:#8b9ef7; }
.ae-dark .total { color:#8b9ef7; }
.ae-dark .batch-item { background:#1a2634; }
.ae-dark .batch-item span { color:#c3d1de; }
.ae-dark .export-all { border-top-color:#23303f; }
</style>
</head>
<body>
<div class="container">
<h2>📦 文章数量较多,建议分批导出</h2>
<div class="info">
<p>📊 共找到 <span class="total"><?php echo $totalPosts; ?></span> 篇文章</p>
<p>⚡ 每批导出 <strong><?php echo $batchSize; ?></strong> 篇,共 <strong><?php echo $totalBatches; ?></strong> 批</p>
<p>💡 建议分批导出,避免超时或内存不足</p>
</div>
<div class="batch-list">
<?php for ($i = 1; $i <= $totalBatches; $i++):
$start = ($i - 1) * $batchSize + 1;
$end = min($i * $batchSize, $totalPosts);
?>
<div class="batch-item">
<span>📄 第 <?php echo $i; ?> 批 (第 <?php echo $start; ?>-<?php echo $end; ?> 篇)</span>
<form method="post" target="_blank" style="margin:0;">
<input type="hidden" name="do_export" value="1">
<input type="hidden" name="export_type" value="<?php echo htmlspecialchars($exportType); ?>">
<input type="hidden" name="export_value" value="<?php echo htmlspecialchars($exportValue); ?>">
<input type="hidden" name="batch" value="<?php echo $i; ?>">
<button type="submit" class="btn">导出这批</button>
</form>
</div>
<?php endfor; ?>
</div>
<div class="export-all">
<form method="post" target="_blank" onsubmit="return confirm('确定要一次性导出全部 <?php echo $totalPosts; ?> 篇文章吗?\n这可能需要较长时间,建议分批导出。');">
<input type="hidden" name="do_export" value="1">
<input type="hidden" name="export_type" value="<?php echo htmlspecialchars($exportType); ?>">
<input type="hidden" name="export_value" value="<?php echo htmlspecialchars($exportValue); ?>">
<input type="hidden" name="batch" value="all">
<button type="submit" class="btn btn-secondary">⚠️ 仍要一次性全部导出</button>
</form>
</div>
</div>
</body>
</html>
<?php
exit;
}
// 分批导出处理
$limitClause = "";
if ($batch !== 0 && $batch !== 'all') {
$batchNum = intval($batch);
$offset = ($batchNum - 1) * $batchSize;
$limitClause = " LIMIT " . intval($offset) . ", " . intval($batchSize);
}
$sql = "SELECT * FROM " . $prefix . "contents WHERE type = 'post' AND status = 'publish'" . $whereClause . " ORDER BY created DESC" . $limitClause;
$posts = $db->fetchAll($db->query($sql));
if (empty($posts)) {
die('没有找到符合条件的文章');
}
$zip = new ZipArchive();
$batchSuffix = ($batch !== 0 && $batch !== 'all') ? "_batch{$batch}" : "";
$tmpFile = sys_get_temp_dir() . '/export_' . uniqid() . $batchSuffix . '.zip';
$zip->open($tmpFile, ZipArchive::CREATE);
$exportCount = 0;
foreach ($posts as $post) {
$cid = $post['cid'];
$cat = $db->fetchRow($db->query("SELECT m.name FROM " . $prefix . "metas m INNER JOIN " . $prefix . "relationships r ON m.mid=r.mid WHERE r.cid=$cid AND m.type='category' LIMIT 1"));
$catName = $cat ? $cat['name'] : '未分类';
$tags = $db->fetchAll($db->query("SELECT m.name FROM " . $prefix . "metas m INNER JOIN " . $prefix . "relationships r ON m.mid=r.mid WHERE r.cid=$cid AND m.type='tag'"));
$tagNames = array_column($tags, 'name');
$tagStr = implode(',', $tagNames);
$date = date('Y-m-d', $post['created']);
$filename = preg_replace('/[\/:*?"<>|]/', '-', $post['title']) . "-{$date}.md";
$md = "---\ntitle: {$post['title']}\ndate: " . date('Y-m-d H:i:s', $post['created']) . "\n";
if ($catName != '未分类') $md .= "category: [{$catName}]\n";
if ($tagStr) $md .= "tags: [{$tagStr}]\n";
$md .= "---\n\n{$post['text']}";
$zip->addFromString($filename, $md);
$exportCount++;
}
$zip->close();
$filename = 'export_' . date('Ymd');
if ($exportType == 'year' && !empty($exportValue)) {
$filename .= "_{$exportValue}年";
} elseif ($exportType == 'category' && !empty($exportValue)) {
$catName = $db->fetchRow($db->query("SELECT name FROM " . $prefix . "metas WHERE mid = " . intval($exportValue)));
$filename .= "_{$catName['name']}";
} elseif ($exportType == 'tag' && !empty($exportValue)) {
$tagName = $db->fetchRow($db->query("SELECT name FROM " . $prefix . "metas WHERE mid = " . intval($exportValue)));
$filename .= "_{$tagName['name']}";
}
if ($batch !== 0 && $batch !== 'all') {
$filename .= "_第{$batch}批";
}
$filename .= '.zip';
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Length: ' . filesize($tmpFile));
readfile($tmpFile);
unlink($tmpFile);
exit;
}
// ==================== 获取数据 ====================
// 获取有文章的年份列表(含文章数量)
// 不用 FROM_UNIXTIME/YEAR():那是 MySQL 专有函数,SQLite 上直接报
// "no such function: FROM_UNIXTIME",整页 500。改在 PHP 里按 created 归档。
$yearMap = array();
$yearRows = $db->fetchAll($db->query("SELECT created FROM " . $prefix . "contents WHERE type = 'post' AND status = 'publish'"));
foreach ($yearRows as $yearRow) {
$yearKey = date('Y', (int) $yearRow['created']);
$yearMap[$yearKey] = isset($yearMap[$yearKey]) ? $yearMap[$yearKey] + 1 : 1;
}
krsort($yearMap);
$years = array();
foreach ($yearMap as $yearKey => $yearCount) {
$years[] = array('year' => $yearKey, 'count' => $yearCount);
}
// 获取分类列表(含文章数量)
$categories = $db->fetchAll($db->query("SELECT m.mid, m.name, COUNT(r.cid) as count FROM " . $prefix . "metas m LEFT JOIN " . $prefix . "relationships r ON m.mid=r.mid WHERE m.type='category' GROUP BY m.mid ORDER BY m.`order`"));
// 获取标签列表(含文章数量)
$tagsList = $db->fetchAll($db->query("SELECT m.mid, m.name, COUNT(r.cid) as count FROM " . $prefix . "metas m LEFT JOIN " . $prefix . "relationships r ON m.mid=r.mid WHERE m.type='tag' GROUP BY m.mid ORDER BY m.name LIMIT 100"));
include 'header.php';
include 'menu.php';
?>
<style>
.typecho-page-title h2 {
display: none;
}
/* 固定宽度容器 */
.export-wrapper {
width: 900px;
max-width: 100%;
margin: 0 auto;
box-sizing: border-box;
}
/* 主容器 */
.export-container {
background: #fff;
border-radius: 16px;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.05);
overflow: hidden;
border: 1px solid #e9ecef;
}
/* 头部装饰 */
.export-header {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
padding: 28px 32px;
text-align: center;
}
.export-header h3 {
margin: 0 0 8px 0;
font-size: 22px;
font-weight: 600;
color: #fff;
}
.export-header p {
margin: 0;
font-size: 14px;
color: rgba(255, 255, 255, 0.85);
}
/* 内容区域 */
.export-body {
padding: 32px;
}
/* 使用说明卡片 */
.info-card {
margin-bottom: 28px;
}
.info-card h4 {
margin: 0 0 10px 0;
font-size: 15px;
font-weight: 600;
color: #1a2a3a;
}
.info-card ul {
margin: 0;
padding-left: 20px;
}
.info-card li {
margin: 6px 0;
font-size: 13px;
color: #5a6e7c;
line-height: 1.5;
}
/* 表单区域 */
.form-section {
margin-bottom: 28px;
}
.form-row {
margin-bottom: 24px;
}
.form-row label {
display: block;
margin-bottom: 10px;
font-weight: 600;
font-size: 14px;
color: #1a2a3a;
}
/* 下拉选择框 */
.form-row select {
width: 100%;
padding: 12px 16px;
border: 1px solid #e0e4e8;
border-radius: 10px;
font-size: 14px;
height: 45px;
background: #fff;
cursor: pointer;
transition: all 0.2s ease;
box-sizing: border-box;
appearance: none;
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%23667eea' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
background-repeat: no-repeat;
background-position: right 16px center;
}
.form-row select:hover {
border-color: #667eea;
}
.form-row select:focus {
outline: none;
border-color: #667eea;
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}
/* 快速选择区域 */
.quick-section {
margin-top: 16px;
padding-top: 16px;
border-top: 1px dashed #e9ecef;
}
.quick-label {
font-size: 12px;
color: #8a9bb0;
margin-bottom: 12px;
display: flex;
align-items: center;
gap: 6px;
}
.quick-list {
display: flex;
flex-wrap: wrap;
gap: 10px;
max-height: 200px;
overflow-y: auto;
}
.quick-item {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 6px 16px;
background: #f1f3f5;
border-radius: 24px;
font-size: 12px;
cursor: pointer;
transition: all 0.2s ease;
color: #495057;
}
.quick-item:hover {
background: #667eea;
color: #fff;
transform: translateY(-1px);
}
.quick-item .count {
font-size: 10px;
background: rgba(0, 0, 0, 0.06);
padding: 2px 6px;
border-radius: 20px;
}
.quick-item:hover .count {
background: rgba(255, 255, 255, 0.2);
}
/* 帮助文字 */
.help-text {
font-size: 12px;
color: #8a9bb0;
margin-top: 8px;
}
/* 导出按钮 */
.btn-export {
width: 100%;
padding: 14px 24px;
background: linear-gradient(135deg, #28a745 0%, #20c997 100%);
color: #fff;
border: none;
border-radius: 10px;
font-size: 15px;
font-weight: 600;
cursor: pointer;
transition: all 0.2s ease;
margin-top: 8px;
}
.btn-export:hover {
transform: translateY(-2px);
box-shadow: 0 6px 20px rgba(40, 167, 69, 0.3);
}
.btn-export:active {
transform: translateY(0);
}
/* 响应式 */
@media (max-width: 960px) {
.export-wrapper {
width: 100%;
padding: 0 16px;
}
.export-body {
padding: 24px;
}
.export-header {
padding: 20px 24px;
}
}
/* ===================================================================
ArticleToObsidian 后台深浅色适配 —— 文章导出页
配色、层级思路全部对齐 UrlNav / Memo / ChangeLog /
UserCard(#newadmin.dark 一套值)。原则一致:浅色一律保持插件原样,
只在 #newadmin.dark 下逐条覆盖深色值;规则都带 #newadmin 前缀且
放在本文件最末,能稳定压过上面所有原始配色。class 由 NewAdmin 的
Alpine 实时切换(<div id="newadmin" :class="{'dark':$store.ze.dark}">),
点右上角 ☀ / 🌙 立即生效、不用刷新,全站后台共用同一 cookie。
=================================================================== */
/* ---------- 顶部工具条层级修复(浅色深色都要) ----------
后台右侧顶部的 ☀ / 🌙 在 .newadmin 里,它默认只有 z-index:20,
本页任何 z-index 大于 20 的浮层都会盖住它、导致点不动。
取值照抄 UrlNav / Memo。注意别写 #newadmin > .side / > .zhezhao
—— 它俩其实是 .newadmin 的子元素,那样选不中,写了也是死规则。 */
#newadmin > .newadmin{ z-index:9000; }
/* ---------- 深色基础 ----------
打上 color-scheme:dark,一次性解决 select 的下拉面板、
滚动条、聚焦环等原生控件的深浅色。 */
#newadmin.dark{ color-scheme:dark; }
/* ---------- 主容器 ---------- */
#newadmin.dark .export-container{
background:#17212f;
border-color:#2b3a4d;
box-shadow:0 4px 20px rgba(0,0,0,.4);
}
/* 头部紫色渐变 + 白字本身在深色下就清楚,保持不动 */
#newadmin.dark .export-header p{ color:rgba(255,255,255,.85); }
/* ---------- 说明卡片 ---------- */
#newadmin.dark .info-card h4{ color:#e6edf3; }
#newadmin.dark .info-card li{ color:#9cb0c6; }
/* ---------- 表单 ---------- */
#newadmin.dark .form-row label{ color:#e6edf3; }
#newadmin.dark .form-row select{
background-color:#1a2634;
border-color:#2b3a4d;
color:#e6edf3;
/* 下拉箭头是内联 SVG(stroke 写死 %23667eea),深色底上偏暗,
换成提亮后的品牌紫 %238b9ef7,只动 background-image 不影响箭头位置 */
background-image:url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%238b9ef7' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
}
#newadmin.dark .form-row select:hover,
#newadmin.dark .form-row select:focus{
border-color:#8b9ef7;
}
#newadmin.dark .form-row select:focus{
box-shadow:0 0 0 3px rgba(139,158,247,.22);
}
/* ---------- 快速选择 ---------- */
#newadmin.dark .quick-section{ border-top-color:#23303f; }
#newadmin.dark .quick-label{ color:#9cb0c6; }
#newadmin.dark .quick-item{
background:#1f2937;
color:#c3d1de;
}
#newadmin.dark .quick-item:hover{
background:#667eea;
color:#fff;
}
#newadmin.dark .quick-item .count{
/* 原来是半透明黑底,深色下会糊成一团,换成半透明白 */
background:rgba(255,255,255,.1);
}
#newadmin.dark .quick-item:hover .count{ background:rgba(255,255,255,.2); }
#newadmin.dark .help-text{ color:#9cb0c6; }
/* ---------- 导出按钮 ----------
绿色渐变 + 白字本身足够清楚,保留原样,只把 hover 外发光换暗场版 */
#newadmin.dark .btn-export:hover{ box-shadow:0 6px 20px rgba(0,0,0,.45); }
</style>
<div class="main">
<div class="body container">
<div class="typecho-page-title"><h2>文章导出</h2></div>
<div class="export-wrapper">
<div class="export-container">
<div class="export-header">
<h3>文章导出工具</h3>
<p>将文章导出为Obsidian支持的Markdown格式ZIP压缩包</p>
</div>
<div class="export-body">
<div class="info-card">
<h4>💡 插件特色</h4>
<ul>
<li>导出格式为ZIP压缩包,一篇一个 .md 文件,包含完整 Frontmatter(标题、日期、分类、标签等)</li>
<li>支持按年份、分类、标签筛选导出特定范围的文章</li>
<li>文章数量较多时(超过300篇),会自动提示分批导出,避免超时</li>
</ul>
</div>
<form method="post" target="_blank">
<div class="form-section">
<div class="form-row">
<label>📋 导出方式</label>
<select name="export_type" id="export_type">
<option value="year">按年份导出</option>
<option value="category">按分类导出</option>
<option value="tag"> 按标签导出</option>
</select>
</div>
<!-- 年份选择区域 -->
<div class="form-row" id="year_group">
<label>📅 选择年份</label>
<select name="export_value" id="year_select">
<option value="">请选择年份</option>
<?php foreach ($years as $year): ?>
<option value="<?php echo $year['year']; ?>"><?php echo $year['year']; ?> 年 (<?php echo $year['count']; ?>)</option>
<?php endforeach; ?>
</select>
<!--<div class="help-text">选择要导出的文章年份</div>-->
<div class="quick-section">
<!--<div class="quick-label">
⚡ 快速选择
</div>-->
<div class="quick-list" id="year_quick_list">
<?php foreach ($years as $year): ?>
<span class="quick-item" data-id="<?php echo $year['year']; ?>">
<?php echo $year['year']; ?>年
<span class="count"><?php echo $year['count']; ?></span>
</span>
<?php endforeach; ?>
</div>
</div>
</div>
<!-- 分类选择区域 -->
<div class="form-row" id="category_group" style="display: none;">
<label>📂 选择分类</label>
<select name="export_value" id="category_select">
<option value="">请选择分类</option>
<?php foreach ($categories as $cat): ?>
<option value="<?php echo $cat['mid']; ?>"><?php echo htmlspecialchars($cat['name']); ?> (<?php echo $cat['count']; ?>)</option>
<?php endforeach; ?>
</select>
<!--<div class="help-text">选择要导出的文章分类</div>-->
<div class="quick-section">
<!--<div class="quick-label">
⚡ 快速选择
</div>-->
<div class="quick-list" id="category_quick_list">
<?php foreach ($categories as $cat): ?>
<span class="quick-item" data-id="<?php echo $cat['mid']; ?>">
<?php echo htmlspecialchars($cat['name']); ?>
<span class="count"><?php echo $cat['count']; ?></span>
</span>
<?php endforeach; ?>
</div>
</div>
</div>
<!-- 标签选择区域 -->
<div class="form-row" id="tag_group" style="display: none;">
<label>🏷️ 选择标签</label>
<select name="export_value" id="tag_select">
<option value="">请选择标签</option>
<?php foreach ($tagsList as $tag): ?>
<option value="<?php echo $tag['mid']; ?>"><?php echo htmlspecialchars($tag['name']); ?> (<?php echo $tag['count']; ?>)</option>
<?php endforeach; ?>
</select>
<!--<div class="help-text">选择要导出的文章标签</div>-->
<div class="quick-section">
<!--<div class="quick-label">
⚡ 快速选择
</div>-->
<div class="quick-list" id="tag_quick_list">
<?php foreach ($tagsList as $tag): ?>
<span class="quick-item" data-id="<?php echo $tag['mid']; ?>">
<?php echo htmlspecialchars($tag['name']); ?>
<span class="count"><?php echo $tag['count']; ?></span>
</span>
<?php endforeach; ?>
</div>
</div>
</div>
</div>
<button type="submit" name="do_export" class="btn-export">
立即导出
</button>
</form>
</div>
</div>
</div>
</div>
</div>
<script>
(function() {
var exportType = document.getElementById('export_type');
var yearGroup = document.getElementById('year_group');
var categoryGroup = document.getElementById('category_group');
var tagGroup = document.getElementById('tag_group');
var yearSelect = document.getElementById('year_select');
var categorySelect = document.getElementById('category_select');
var tagSelect = document.getElementById('tag_select');
function switchExportType() {
var type = exportType.value;
yearGroup.style.display = 'none';
categoryGroup.style.display = 'none';
tagGroup.style.display = 'none';
if (type === 'year') {
yearGroup.style.display = 'block';
yearSelect.name = 'export_value';
categorySelect.name = '';
tagSelect.name = '';
} else if (type === 'category') {
categoryGroup.style.display = 'block';
yearSelect.name = '';
categorySelect.name = 'export_value';
tagSelect.name = '';
} else if (type === 'tag') {
tagGroup.style.display = 'block';
yearSelect.name = '';
categorySelect.name = '';
tagSelect.name = 'export_value';
}
}
// 年份快速点击
document.querySelectorAll('#year_quick_list .quick-item').forEach(function(el) {
el.addEventListener('click', function() {
var yearId = this.dataset.id;
yearSelect.value = yearId;
this.style.background = '#28a745';
this.style.color = '#fff';
setTimeout(function() {
el.style.background = '';
el.style.color = '';
}, 200);
});
});
// 分类快速点击
document.querySelectorAll('#category_quick_list .quick-item').forEach(function(el) {
el.addEventListener('click', function() {
var catId = this.dataset.id;
categorySelect.value = catId;
this.style.background = '#28a745';
this.style.color = '#fff';
setTimeout(function() {
el.style.background = '';
el.style.color = '';
}, 200);
});
});
// 标签快速点击
document.querySelectorAll('#tag_quick_list .quick-item').forEach(function(el) {
el.addEventListener('click', function() {
var tagId = this.dataset.id;
tagSelect.value = tagId;
this.style.background = '#28a745';
this.style.color = '#fff';
setTimeout(function() {
el.style.background = '';
el.style.color = '';
}, 200);
});
});
exportType.addEventListener('change', switchExportType);
switchExportType();
})();
</script>
<script>
/* ===================================================================
深浅色联动:让右上角 ☀ / 🌙 切换的是"整套后台",而不是只切本页
背景:NewAdmin 的深浅色其实是两套互相独立的机制
1) #newadmin 上的 .dark 类(Alpine 的 $store.ze.dark 驱动)
2) <link id="darkcss"> 这条深色样式表(它才是真正把侧栏、顶栏、
页面底色整体换成深色的那一套)
点按钮时 NewAdmin 会先改 ①,再在 document.startViewTransition 的
回调里异步改 ②。只要这两步有一次没对上,就会出现"插件页面(跟着
① 走)变深色、整套后台(跟着 ② 走)还是浅色"的现象,而且在
cookie 不存在时 NewAdmin 的 x-init 会把 store 设成字符串 'light'
(真值),一进页面就是反相的,之后再点也是一直反着切。
本脚本不改 NewAdmin 的代码,只在本页把 ①② 和 cookie 三个状态
收口,谁先变都以最终目标值为准,因此不会和 NewAdmin 打架。
没有装 NewAdmin 时(取不到 #newadmin / #darkcss)直接不介入。
与 Memo / ChangeLog / UserCard 用的是同一套实现。
=================================================================== */
(function () {
var box = document.getElementById('newadmin');
var link = document.getElementById('darkcss');
if (!box || !link) { return; }
/* pinned=false:还只是"进页面时的被动对齐",不写 cookie。
否则首次访问会被强行钉成浅色,NewAdmin 原本"没 cookie 就跟随
系统偏好"的默认行为就失效了。 */
var pinned = false;
function isDark() {
return box.classList.contains('dark');
}
/* 三处状态一次性对齐:store + .dark 类 + dark.css + cookie */
function setTheme(on, writeCookie) {
if (window.Alpine && Alpine.store('ze')) {
try { Alpine.store('ze').dark = on ? true : false; } catch (err) {}
}
box.classList.toggle('dark', on);
/* 用 DOM 属性开关样式表,比改 disabled="false" 这种写法可靠 */
if (link.disabled === !!on) { link.disabled = !on; }
if (writeCookie) {
document.cookie = (on ? 'dark=dark' : 'dark=light') + ';path=/;max-age=31536000';
}
}
/* 期望状态:进页面时以服务端按 cookie 渲染出来的 dark.css 开关为准,
顺手把被 x-init 弄反的 .dark 类纠正回来。 */
var intent = !link.disabled;
/* 把界面拉回 intent:只有真的存在偏差时才写,
避免和下面的 MutationObserver 互相触发形成死循环 */
function enforce() {
if (link.disabled !== !intent || isDark() !== intent) {
setTheme(intent, pinned);
}
}
function setIntent(on) {
intent = !!on;
pinned = true;
setTheme(intent, true);
}
/* 1) 进页面先对一次 */
setTheme(intent, false);
setTimeout(enforce, 0);
setTimeout(enforce, 200);
/* 2) 状态守卫:NewAdmin 那次异步改动是在 view transition 回调里做的,
页面重的时候可能晚于下面几个定时器落点,落晚了又会把状态翻反。
这里盯住 dark.css 的 disabled 属性和 #newadmin 的 class,
只要和 intent 不一致就立刻拉回来 —— 持续生效,不依赖时间点。 */
if (window.MutationObserver) {
var guard = new MutationObserver(enforce);
guard.observe(link, { attributes: true, attributeFilter: ['disabled'] });
guard.observe(box, { attributes: true, attributeFilter: ['class'] });
}
/* 3) 点 ☀ / 🌙:捕获阶段只"记目标",不动任何状态。
这里千万不能在点击当时就把 store/dark.css 改掉:NewAdmin 的
@click="darkmode(!$store.ze.dark)" 紧接着要读 $store.ze.dark
来决定往哪边切、并据此广播,我们先改会让它算出相反方向,
于是它广播出去的值和我们的目标正好相反,又把自己翻回去。
所以目标先记下,等这一轮事件走完(0ms)再收口。 */
document.addEventListener('click', function (e) {
var btn = (e.target && e.target.closest) ? e.target.closest('.rightmore > button') : null;
if (!btn) { return; }
intent = !intent;
pinned = true;
setTimeout(enforce, 0);
setTimeout(enforce, 120);
}, true);
/* 4) 跟随系统 / 其它标签页切换时同样收口
(NewAdmin 用 BroadcastChannel 'zeze_channel' 广播) */
try {
var aeChannel = new BroadcastChannel('zeze_channel');
aeChannel.onmessage = function (ev) {
var data = null;
try { data = JSON.parse(ev.data); } catch (err) { return; }
if (!data || !data.DarkMode) { return; }
if (data.DarkMode === 'auto') {
/* NewAdmin 收到 auto 时会自己按系统偏好切,这里跟着定目标 */
setIntent(window.matchMedia('(prefers-color-scheme: dark)').matches);
return;
}
setIntent(data.DarkMode === 'dark');
};
} catch (err) {}
/* 5) 系统偏好变化(NewAdmin 只在 x-init 里挂过监听,切系统时不会广播) */
try {
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', function (ev) {
var cookieDark = document.cookie.indexOf('dark=dark') > -1;
var cookieLight = document.cookie.indexOf('dark=light') > -1;
/* 用户手动选过深浅就不跟系统走 */
if (cookieDark || cookieLight) { return; }
setIntent(ev.matches);
});
} catch (err) {}
})();
</script>
<?php include 'footer.php'; ?>