Files
ToolboxRelease/assets/shared.js
T

258 lines
11 KiB
JavaScript
Raw Normal View History

// 站点公共数据与渲染片段:文案、CHANGELOG.md 解析、时间线 HTML。
// 版式取自 FlClash 官网(GPL-3.0),此处改写为中育Toolbox 的内容与数据。
globalThis.ZytbSite = (() => {
'use strict';
// 源码在 GitHub,安装包发布在自建的 Gitea;站点从 Gitea 的 homepage 分支提供。
const SOURCE = 'https://github.com/Loshop-Studio/ZhongYuToolBox_Next';
const REPO = 'https://git.loshop.com.cn/LoshopStudio/ToolboxRelease';
// 一次全部展开,站点上的版本本来就不多。
const FIRST_PAGE = 100;
const MAX_STAGGER = 16;
const TYPE_BY_TITLE = {
不兼容变更: 'breaking',
新功能: 'feat',
问题修复: 'fix',
性能: 'perf',
其它: 'other',
};
const TYPE_KEYS = {
breaking: 'typeBreaking',
feat: 'typeFeat',
fix: 'typeFix',
perf: 'typePerf',
other: 'typeOther',
};
const ICON_EXTERNAL =
'<svg viewBox="0 0 16 16" width="12" height="12" aria-hidden="true"><path d="M6 3.5h6.5V10M12.25 3.75 4 12" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/></svg>';
const STRINGS = {
zh: {
pageTitle: '中育Toolbox – 云笔记、图库、测评、专栏、随身答,一个客户端',
pageDescription:
'中育Toolbox 是把学校云笔记、图库、测评、错题本、在线专栏、优客畅学和随身答收在一起的跨平台客户端,支持 Windows、Android、iOS 与 macOS,以 GPL-3.0 开源、无广告。',
skip: '跳到下载',
heroFor: '适用于',
heroLede:
'把云笔记、图库、测评、专栏和随身答收进同一个客户端。账号、主题和下载记录都留在本地,开源、无广告。',
heroRelease: (v, d) => `${v} · ${d}`,
ctaDownload: (p) => `下载 ${p} 版`,
ctaChoose: '选择下载版本',
ctaIos: 'iOS 版是未签名的 IPA,需要自签工具或企业签名才能安装。也可以先选其他设备的版本。',
scrollHint: '向下滚动查看功能、下载和更新日志',
featTitle: '功能',
featLead: '五个模块共用一套账号:登录一次,笔记、测评、课程和随身答就都在手边。',
featMeta: (n) => `${n} 个模块 · 多账号 · 插件 · MCP`,
dlTitle: '下载',
dlMeta: (v, d) => `版本 ${v} · ${d}发布`,
dlLead: '已为当前设备选好安装包。每个文件都标了大小和 SHA-256,下载后可以自行校验。',
dlAll: '在 Gitea 查看全部版本',
dlError: '未能读取版本信息,请前往 GitHub Releases 下载。',
platformLabel: '平台',
recommended: '推荐',
copied: '已复制',
copy: '复制',
copySha: (name) => `复制 ${name} 的 SHA-256`,
hintAndroid: '手机安装前需要允许「安装未知应用」。',
hintWindows: '安装版带开始菜单入口和卸载程序;便携版解压即用。',
hintIos: 'IPA 未签名,需要用 AltStore、Sideloadly 等工具自签后安装。',
hintMacos: '包未公证,首次打开请在「隐私与安全性」里允许。',
winSetup: '安装版',
noteWinSetup: '带开始菜单入口与卸载程序',
winPortable: '便携版',
noteWinPortable: '解压即用,无需安装',
noteAndroid: '适用于 Android 手机与平板',
noteIos: '需要自签工具或企业签名',
noteMacos: 'ZIP 包,适用于 Apple 芯片的 Mac',
tipWindowsTitle: '第一次运行',
tipWindows:
'安装包没有购买代码签名证书,SmartScreen 可能提示「已保护你的电脑」。点「更多信息」→「仍要运行」即可。',
tipAndroidTitle: '安装包体积',
tipAndroid:
'APK 里打包了 PDF、视频和 PPT 的渲染组件,体积在 150 MB 以上属正常。装好后不需要另外下载运行库。',
tipIosTitle: '安装未签名的 IPA',
tipIos:
'在电脑上用 AltStore、Sideloadly 或爱思助手把 IPA 签进自己的 Apple ID,或使用学校提供的企业签名。签名过期后需要重新签一次。',
tipMacosTitle: '首次打开',
tipMacos:
'打开「系统设置 → 隐私与安全性」,在被拦截的提示旁点「仍要打开」;也可以在 Finder 里右键应用选「打开」。',
tipAccountTitle: '登录',
tipAccount:
'用学校代码 + 平台用户名密码登录,账号只保存在本机应用数据目录。首次登录后可以在主页添加多个账号并来回切换。',
clTitle: '更新日志',
clMeta: (n, d) => `自 ${d}以来共 ${n} 个版本`,
clSearch: '搜索更新内容或版本',
clLatest: '最新',
clResults: (e, v) => `${v} 个版本中有 ${e} 条匹配`,
clNone: (q) => `没有与「${q}」相关的更新。`,
clClear: '清除',
clEmpty: '仅内部改进。',
clError: '未能加载更新日志。',
clErrorLink: '在 Gitea 查看全部发布',
clOnGithub: '发布页',
clOnGithubLabel: (v) => `在 Gitea 查看 ${v} 发布页`,
typeBreaking: '不兼容变更',
typeFeat: '新功能',
typeFix: '问题修复',
typePerf: '性能',
typeOther: '其它',
footLicense: '以 GPL-3.0 协议开源。',
footUpdated: '本页随每次发布更新。',
footNote: '非官方客户端,与学校及中育平台没有隶属关系,仅供学习交流。',
footReleases: '发布页',
footIssues: '反馈问题',
},
};
function parseChangelog(markdown) {
const versions = [];
let version = null;
let group = null;
let entry = null;
for (const raw of markdown.split(/\r?\n/)) {
const line = raw.trimEnd();
let match = line.match(/^## (v?\d[^\s(]*)(?: \((\d{4}-\d{2}-\d{2})\))?$/);
if (match) {
version = {
tag: match[1],
version: match[1].replace(/^v/, ''),
date: match[2] || '',
groups: [],
empty: false,
};
versions.push(version);
group = null;
entry = null;
continue;
}
if (!version) continue;
match = line.match(/^\*\*(.+)\*\*$/);
if (match) {
group = { type: TYPE_BY_TITLE[match[1]] || 'other', title: match[1], entries: [] };
version.groups.push(group);
entry = null;
continue;
}
match = line.match(/^[-*] (.+)$/);
if (match) {
if (!group) {
group = { type: null, title: '', entries: [] };
version.groups.push(group);
}
entry = parseEntry(match[1]);
group.entries.push(entry);
continue;
}
if (entry && /^\s+\S/.test(raw)) {
entry.text += ` ${raw.trim()}`;
continue;
}
if (line === 'Internal improvements only.') version.empty = true;
}
return versions;
}
function parseEntry(source) {
let text = source;
let scope = null;
let id = null;
const scoped = text.match(/^\*\*([^*]+)\*\* (.+)$/);
if (scoped) {
scope = scoped[1];
text = scoped[2];
}
const hashed = text.match(/^(.*) \(([0-9a-f]{7,40})\)$/);
if (hashed) {
text = hashed[1];
id = hashed[2];
}
return { scope, text, id };
}
function translate(lang, key, ...args) {
const value = STRINGS[lang]?.[key] ?? STRINGS.zh[key] ?? key;
return typeof value === 'function' ? value(...args) : value;
}
function formatDate(iso, lang) {
if (!iso) return '';
return new Intl.DateTimeFormat('zh-CN', {
year: 'numeric',
month: 'long',
day: 'numeric',
timeZone: 'UTC',
}).format(new Date(`${iso}T00:00:00Z`));
}
function changelogMeta(versions) {
const oldest = versions.filter((version) => version.date).pop();
return oldest ? translate('zh', 'clMeta', versions.length, formatDate(oldest.date)) : '';
}
function escapeHtml(text) {
return String(text).replace(/[&<>"]/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;' })[c]);
}
function highlight(text, query) {
if (!query) return escapeHtml(text);
const lower = text.toLowerCase();
let html = '';
let from = 0;
for (let at = lower.indexOf(query); at !== -1; at = lower.indexOf(query, from)) {
html += `${escapeHtml(text.slice(from, at))}<mark>${escapeHtml(text.slice(at, at + query.length))}</mark>`;
from = at + query.length;
}
return html + escapeHtml(text.slice(from));
}
function releaseHtml(version, groups, { query, isLatest, ago }) {
const t = (key, ...args) => translate('zh', key, ...args);
const tag = escapeHtml(version.tag);
let row = 0;
const order = () => ` style="--i:${Math.min(row++, MAX_STAGGER)}"`;
const sections = groups.map((group) => {
const title = group.type ? (TYPE_KEYS[group.type] ? t(TYPE_KEYS[group.type]) : group.title) : '';
const heading = title
? `<h3 class="group-title"${order()}><span class="mark" aria-hidden="true"></span>${escapeHtml(title)}</h3>`
: '';
const entries = group.entries.map((entry) => {
const scope = entry.scope ? `<span class="scope">${highlight(entry.scope, query)}</span>` : '';
const hash = entry.id ? `<a class="hash" href="${SOURCE}/commit/${entry.id}">${entry.id.slice(0, 7)}</a>` : '';
return `<li class="entry"${order()}><span class="entry-text">${scope}${highlight(entry.text, query)}</span>${hash}</li>`;
});
return (
`<section class="${group.type ? 'group' : 'group plain'}" data-type="${escapeHtml(group.type || 'plain')}">` +
`${heading}<ul class="entries">${entries.join('')}</ul></section>`
);
});
const empty = version.empty && !groups.length ? `<p class="empty">${escapeHtml(t('clEmpty'))}</p>` : '';
const title = ago ? ` title="${escapeHtml(ago(version.date))}"` : '';
const date = version.date
? `<span class="when"><time datetime="${version.date}"${title}>${formatDate(version.date)}</time></span>`
: '';
return (
`<article class="release" id="${tag}"><header class="release-head"><div class="ver-row">` +
`<a class="ver" href="#${tag}">${highlight(version.version, query)}</a>` +
(isLatest ? `<span class="latest">${escapeHtml(t('clLatest'))}</span>` : '') +
`</div>${date}<span class="when"><a href="${REPO}/releases/tag/${tag}" ` +
`aria-label="${escapeHtml(t('clOnGithubLabel', version.tag))}">${escapeHtml(t('clOnGithub'))} ${ICON_EXTERNAL}</a>` +
`</span></header><div class="release-body">${empty}${sections.join('')}</div></article>`
);
}
function timelineHtml(items, { query = '', latestTag, ago, year = null }) {
let html = '';
for (const { version, groups } of items) {
const releaseYear = version.date.slice(0, 4);
if (releaseYear && releaseYear !== year) {
html += `<div class="year">${releaseYear}</div>`;
year = releaseYear;
}
html += releaseHtml(version, groups, { query, isLatest: version.tag === latestTag, ago });
}
return { html, year };
}
return { REPO, SOURCE, FIRST_PAGE, STRINGS, parseChangelog, translate, formatDate, changelogMeta, escapeHtml, timelineHtml };
})();