Files
TypechoToObsidian/Plugin.php

1265 lines
36 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
/**
* TypechoToObsidian REST API 接口 - 插件主文件
*
* @package TypechoToObsidian
* @author 石头厝
* @version 2.0.9
* @link https://www.shitoucuo.com/
*/
namespace TypechoPlugin\TypechoToObsidian;
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
}
use Typecho\Plugin as TypechoPlugin;
use Typecho\Plugin\Exception;
use Typecho\Plugin\PluginInterface;
use Typecho\Request;
use Typecho\Widget\Helper\Form;
use Typecho\Widget\Helper\Form\Element\Radio;
use Typecho\Widget\Helper\Form\Element\Text;
use Typecho\Widget\Helper\Form\Element\Textarea;
use Typecho\Widget\Helper\Layout;
use Utils\Helper;
/**
* 文章、分类、标签、评论REST_API接口
* @author 石头厝
* @link https://www.shitoucuo.com/
* @package TypechoToObsidian
* @version 2.0.9
*/
class Plugin implements PluginInterface
{
const ACTION_CLASS = 'TypechoPlugin\TypechoToObsidian\Action';
/**
* 设置页里接口的分组,只决定显示顺序,不影响路由本身
*
* @var array
*/
private const ROUTE_GROUPS = array(
'content' => array(
'title' => '内容读取',
'note' => '只读,客户端拉取文章、分类、标签与评论时用到',
'names' => array(
'posts', 'post', 'postDetail', 'pages', 'categories', 'tags',
'archives', 'recentComments', 'comments', 'settings', 'users', 'userList',
),
),
'publish' => array(
'title' => '发布与编辑',
'note' => '从 Obsidian 写回文章、分类与标签',
'names' => array('postArticle', 'addMetas', 'editMeta'),
),
'comment' => array(
'title' => '评论管理',
'note' => '发表、审核、回复与删除评论',
'names' => array('comment', 'manageComments', 'manageComment'),
),
'file' => array(
'title' => '文件管理',
'note' => '附件上传、删除与列表',
'names' => array('upload', 'deleteFile', 'fileList'),
),
);
/**
* 接口在设置页里显示的一句话说明
*
* 注释里的原文会被 getRoutes() 去掉斜杠、且大多带「的接口」尾巴,
* 这里单独给一份干净的展示文案;新增的接口没写在这里时自动回退到注释第一行。
*
* @var array
*/
private const ROUTE_INFO = array(
'posts' => '文章列表与搜索',
'post' => '文章 / 独立页面详情(渲染后的 HTML)',
'postDetail' => '文章详情(原始 Markdown,供下载后继续编辑)',
'pages' => '独立页面列表',
'categories' => '分类列表',
'tags' => '标签列表',
'archives' => '归档列表',
'recentComments' => '最新评论',
'comments' => '文章评论列表',
'settings' => '站点设置项',
'users' => '作者信息与作者文章',
'userList' => '用户列表',
'postArticle' => '发布 / 更新文章',
'addMetas' => '新增分类 / 标签',
'editMeta' => '修改分类 / 标签',
'comment' => '发表评论',
'manageComments' => '评论列表(管理用,可按状态筛选)',
'manageComment' => '评论操作:审核 / 回复 / 删除 / 修改',
'upload' => '上传附件',
'deleteFile' => '删除附件',
'fileList' => '附件列表',
);
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return string
* @throws Exception
*/
public static function activate()
{
$routes = call_user_func(array(self::ACTION_CLASS, 'getRoutes'));
foreach ($routes as $route) {
Helper::addRoute($route['name'], $route['uri'], self::ACTION_CLASS, $route['action']);
}
TypechoPlugin::factory('Widget_Feedback')->comment = array(__CLASS__, 'comment');
return 'TypechoToObsidian 已启用';
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return string
* @throws Exception
*/
public static function deactivate()
{
$routes = call_user_func(array(self::ACTION_CLASS, 'getRoutes'));
foreach ($routes as $route) {
Helper::removeRoute($route['name']);
}
return 'TypechoToObsidian 已停用';
}
/**
* 获取插件配置面板
*
* @access public
* @param Form $form 配置面板
* @return void
*/
public static function config(Form $form)
{
$form->setAttribute('class', 'tto-form');
$routes = call_user_func(array(self::ACTION_CLASS, 'getRoutes'));
$methods = self::routeMethods();
/** 先按原有 name / 默认值把输入项建好,保证配置键名与提交逻辑完全不变 */
$switches = array();
foreach ($routes as $route) {
if ($route['shortName'] === 'upgrade') {
continue;
}
$switch = new Radio(
$route['shortName'],
array(0 => _t('禁用'), 1 => _t('启用')),
1,
self::switchLabel($route, $methods),
self::routeSummary($route)
);
$switch->setAttribute('class', 'typecho-option tto-row tto-row--switch');
$switch->setAttribute('data-tto-route', $route['shortName']);
$form->addInput($switch);
$switches[$route['shortName']] = $switch;
}
$fields = array();
$fields['apiToken'] = new Text(
'apiToken',
null,
'123456',
_t('APITOKEN'),
_t('客户端请求时通过 token 请求头携带。留空则不校验,任何拿到接口地址的人都能读写。')
);
$fields['apiToken']->input->setAttribute('placeholder', _t('例如 8f3c1d7a9b…(留空表示不校验)'));
$fields['csrfSalt'] = new Text(
'csrfSalt',
null,
'05faabd6637f7e30c797973a558d4372',
_t('CSRF 加密盐'),
_t('请务必改成你自己的随机字符串,用于签发和校验请求令牌。')
);
$fields['csrfSalt']->input->setAttribute('placeholder', _t('一串随机字符,越长越好'));
$fields['validateLogin'] = new Radio(
'validateLogin',
array(0 => _t('否'), 1 => _t('是')),
0,
_t('高敏接口校验后台登录'),
_t('开启后,高敏接口需要携带后台登录 Cookie 才能调用,只适合在浏览器里直接调试时使用。')
);
$fields['origin'] = new Textarea(
'origin',
null,
null,
_t('跨域域名白名单'),
_t('一行一个来源域名,需带协议头,例如 https://blog.example.com。填 * 表示不限制跨域来源,不推荐。')
);
$fields['origin']->input->setAttribute('placeholder', 'https://blog.example.com');
$fields['fieldsPrivacy'] = new Text(
'fieldsPrivacy',
null,
null,
_t('自定义字段过滤'),
_t('获取文章时忽略的自定义字段名,半角英文逗号分隔,例如 fields1,fields2')
);
$fields['fieldsPrivacy']->input->setAttribute('placeholder', 'fields1,fields2');
$fields['allowedOptions'] = new Text(
'allowedOptions',
null,
null,
_t('设置项白名单'),
_t('/api/settings 默认只返回安全的站点设置。这里可以追加允许通过 ?key= 读取的 typecho_options 字段名,英文逗号分隔,例如 keywords,theme')
);
$fields['allowedOptions']->input->setAttribute('placeholder', 'keywords,theme');
/* 这两项是开关样式,其余是输入框样式 */
$switchStyleFields = array('validateLogin');
foreach ($fields as $name => $field) {
$field->setAttribute(
'class',
'typecho-option tto-row '
. (in_array($name, $switchStyleFields, true) ? 'tto-row--switch' : 'tto-row--field')
);
$form->addInput($field);
}
self::buildLayout($form, $switches, $fields);
}
/**
* 把建好的输入项重新组织进分组卡片里
*
* 所有输入项都已通过 addInput 注册,这里只是把它们从表单顶层搬到卡片内部,
* 因此提交的字段名、默认值、校验逻辑都不受影响。
*
* @param Form $form
* @param array $switches 接口开关
* @param array $fields 其余配置项
* @return void
*/
private static function buildLayout(Form $form, array $switches, array $fields)
{
$form->addItem(self::layout('style', array('id' => 'tto-config-style'), self::stylesheet()));
/* 顶部说明 */
$form->addItem(self::layout('div', array('class' => 'tto-hero'), self::heroHtml(count($switches))));
/* 接口开关 */
$card = self::card(
_t('接口开关'),
_t('关闭后该接口对客户端立即失效,请求会返回 403。'),
'<span class="tto-count">' . _t('已启用') . ' <b data-tto="count">–</b> / ' . count($switches) . '</span>'
. '<button type="button" class="tto-mini" data-tto="all-on">' . _t('全部启用') . '</button>'
. '<button type="button" class="tto-mini" data-tto="all-off">' . _t('全部关闭') . '</button>'
);
foreach (self::ROUTE_GROUPS as $group) {
$names = array();
foreach ($group['names'] as $name) {
if (isset($switches[$name])) {
$names[] = $name;
}
}
if (!$names) {
continue;
}
$card->addItem(self::layout(
'div',
array('class' => 'tto-group-head'),
'<span class="tto-group-title">' . _t($group['title']) . '</span>'
. '<span class="tto-group-note">' . _t($group['note']) . '</span>'
. '<span class="tto-group-total">' . count($names) . '</span>'
));
foreach ($names as $name) {
$form->removeItem($switches[$name]);
$card->addItem($switches[$name]);
unset($switches[$name]);
}
}
/* 以后新增但没写进分组的接口,兜底显示在最后,避免漏掉 */
if ($switches) {
$card->addItem(self::layout(
'div',
array('class' => 'tto-group-head'),
'<span class="tto-group-title">' . _t('其他接口') . '</span>'
. '<span class="tto-group-total">' . count($switches) . '</span>'
));
foreach ($switches as $name => $switch) {
$form->removeItem($switch);
$card->addItem($switch);
}
}
$form->addItem($card);
/* 安全与访问 */
$security = self::card(
_t('安全与访问'),
_t('这几项决定谁能调用本站接口,建议逐条确认。')
);
foreach (array('apiToken', 'csrfSalt', 'validateLogin', 'origin') as $name) {
$form->removeItem($fields[$name]);
$security->addItem($fields[$name]);
}
$form->addItem($security);
/* 高级设置 */
$advanced = self::card(
_t('高级设置'),
_t('不确定用途的话保持默认即可。')
);
foreach (array('fieldsPrivacy', 'allowedOptions') as $name) {
$form->removeItem($fields[$name]);
$advanced->addItem($fields[$name]);
}
$form->addItem($advanced);
$form->addItem(self::layout('script', array(), self::script()));
}
/**
* 顶部说明区
*
* @param int $total 接口总数
* @return string
*/
private static function heroHtml(int $total): string
{
$prefix = defined('__TYPECHO_TO_OBSIDIAN_PREFIX__') ? __TYPECHO_TO_OBSIDIAN_PREFIX__ : '/api/';
return '<div class="tto-hero-main">'
. '<div class="tto-hero-title">TypechoToObsidian'
. '<span class="tto-hero-ver">v' . self::version() . '</span></div>'
. '<p class="tto-hero-desc">' . _t('把本站文章、分类、标签与评论以 REST API 形式开放,供 Obsidian 客户端发布与同步使用。当前共 %d 个接口,可直接在下方逐个开关。', $total) . '</p>'
. '</div>'
. '<div class="tto-hero-side">'
. '<code class="tto-chip">' . htmlspecialchars($prefix, ENT_QUOTES, 'UTF-8') . '</code>'
. '<span class="tto-chip-label">' . _t('接口前缀') . '</span>'
. '</div>';
}
/**
* 一个分组卡片
*
* @param string $title 标题
* @param string $desc 说明
* @param string $tools 右上角工具区 HTML
* @return Layout
*/
private static function card(string $title, string $desc = '', string $tools = ''): Layout
{
$card = new Layout('section', array('class' => 'tto-card'));
$head = '<div class="tto-card-text">'
. '<h4 class="tto-card-title">' . $title . '</h4>'
. ($desc !== '' ? '<p class="tto-card-desc">' . $desc . '</p>' : '')
. '</div>';
if ($tools !== '') {
$head .= '<div class="tto-card-tools">' . $tools . '</div>';
}
$card->addItem(self::layout('div', array('class' => 'tto-card-head'), $head));
return $card;
}
/**
* 造一个带内容的 HTML 元素
*
* @param string $tag 标签名
* @param array $attributes 属性
* @param string $html 内部 HTML
* @return Layout
*/
private static function layout(string $tag, array $attributes, string $html = ''): Layout
{
$item = new Layout($tag, $attributes);
if ($html !== '') {
$item->html($html);
}
return $item;
}
/**
* 接口开关的标题:请求方法 + 接口路径
*
* @param array $route
* @param array $methods
* @return string
*/
private static function switchLabel(array $route, array $methods): string
{
$method = isset($methods[$route['action']]) ? $methods[$route['action']] : 'GET';
$class = strtoupper($method) === 'POST' ? 'tto-method tto-method--post' : 'tto-method tto-method--get';
return '<span class="' . $class . '">' . $method . '</span>'
. '<code class="tto-path">' . htmlspecialchars($route['uri'], ENT_QUOTES, 'UTF-8') . '</code>';
}
/**
* 接口说明只取注释第一行,避免把整段判定规则塞进设置页
*
* @param array $route
* @return string
*/
private static function routeSummary(array $route): string
{
if (isset(self::ROUTE_INFO[$route['shortName']])) {
return htmlspecialchars(self::ROUTE_INFO[$route['shortName']], ENT_QUOTES, 'UTF-8');
}
$lines = preg_split('/\r\n|\r|\n/', (string) $route['description']);
foreach ((array) $lines as $line) {
$line = trim($line, " \t\n\r\0\x0B*/");
if ($line !== '') {
return htmlspecialchars($line, ENT_QUOTES, 'UTF-8');
}
}
return '';
}
/**
* 从接口源码里读出每个接口限定的请求方法
*
* @return array action 名 => GET / POST
*/
private static function routeMethods(): array
{
$map = array();
try {
$class = new \ReflectionClass(self::ACTION_CLASS);
$file = $class->getFileName();
$lines = $file ? @file($file) : null;
if (!$lines) {
return $map;
}
foreach ($class->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
if (!preg_match('/(.*)Action$/', $method->getName())) {
continue;
}
$start = $method->getStartLine() - 1;
$body = implode('', array_slice($lines, $start, $method->getEndLine() - $start));
$map[$method->getName()] = preg_match('/lockMethod\(\s*[\'"]post[\'"]/i', $body) ? 'POST' : 'GET';
}
} catch (\Throwable $e) {
/* 反射失败时按 GET 显示,不影响配置本身 */
}
return $map;
}
/**
* 当前插件版本,跟随文件头注释
*
* @return string
*/
private static function version(): string
{
try {
$doc = (string) (new \ReflectionClass(__CLASS__))->getDocComment();
if (preg_match('/@version\s+([\w.\-]+)/', $doc, $matches)) {
return $matches[1];
}
} catch (\Throwable $e) {
}
return '2.0.1';
}
/**
* 设置页样式
*
* 全部限定在 .tto-form 内,不影响后台其他页面。
*
* @return string
*/
private static function stylesheet(): string
{
return <<<'CSS'
.tto-form {
/* ---------- 浅色(默认) ---------- */
--tto-accent: #467B96;
--tto-accent-soft: #EDF3F6;
--tto-accent-line: #C7D8E0;
--tto-accent-strong: #3C6A81;
--tto-ink: #363633;
--tto-ink-strong: #2E2E2B;
--tto-label: #33332F;
--tto-path-text: #33332F;
--tto-subtle: #5F5F58;
--tto-muted: #72726B;
--tto-dim: #8A8A82;
--tto-total: #898981;
--tto-line: #E3E3DD;
--tto-line-soft: #F1F1EC;
--tto-card: #FFFFFF;
--tto-card-head: #FCFCFA;
--tto-soft: #FAFAF7;
--tto-page: #F6F6F3;
--tto-input-bg: #FFFFFF;
--tto-input-line: #D9D9D3;
--tto-ctrl-text: #66665F;
--tto-ctrl-bg: #FFFFFF;
--tto-ctrl-line: #D9D9D3;
--tto-ctrl-hover: #F4F4EF;
--tto-seg-on-bg: #E4E4DB;
--tto-seg-on-line: #C4C4B9;
--tto-seg-on-text: #3D3D37;
--tto-count-text: #3C6A81;
--tto-get-text: #2E6F52;
--tto-get-bg: #E9F3EE;
--tto-post-text: #8F6220;
--tto-post-bg: #FAF0DE;
--tto-chip-text: #4A4A45;
--tto-chip-bg: #F4F4EF;
--tto-focus-ring: 0 0 0 3px rgba(70, 123, 150, .12);
--tto-bar-shadow: 0 -8px 14px -10px rgba(0, 0, 0, .18);
font-size: 14px;
color: var(--tto-ink);
}
/*
* 深色:脚本按 NewAdmin 的开关状态(#newadmin.dark / dark cookie / 系统偏好)
* 把 tto-dark 写到表单上,这里只换一套变量,其余规则不用复制一份。
*/
.tto-form.tto-dark {
--tto-accent: #7FBEDF;
--tto-accent-soft: rgba(127, 190, 223, .14);
--tto-accent-line: rgba(127, 190, 223, .34);
--tto-accent-strong: #2D578A;
--tto-ink: #D3DCE8;
--tto-ink-strong: #EAF1F8;
--tto-label: #DDE6F0;
--tto-path-text: #CBD6E4;
--tto-subtle: #BFCBDA;
--tto-muted: #9CABBC;
--tto-dim: #6C7B8E;
--tto-total: #8595A8;
--tto-line: #26354A;
--tto-line-soft: #1E2C3F;
--tto-card: #16202F;
--tto-card-head: #1A2635;
--tto-soft: #131C29;
--tto-page: #101928;
--tto-input-bg: #0E1723;
--tto-input-line: #2C3D54;
--tto-ctrl-text: #B4C1D2;
--tto-ctrl-bg: #1B2739;
--tto-ctrl-line: #2E4059;
--tto-ctrl-hover: #223148;
--tto-seg-on-bg: #3A424C;
--tto-seg-on-line: #4B545F;
--tto-seg-on-text: #E8EDF4;
--tto-count-text: #8FC4E4;
--tto-get-text: #82D8AC;
--tto-get-bg: rgba(130, 216, 172, .14);
--tto-post-text: #E8B76E;
--tto-post-bg: rgba(232, 183, 110, .14);
--tto-chip-text: #C2CDDC;
--tto-chip-bg: #1B2739;
--tto-focus-ring: 0 0 0 3px rgba(127, 190, 223, .18);
--tto-bar-shadow: 0 -8px 14px -10px rgba(0, 0, 0, .55);
}
/*
* 兜底:脚本没跑起来时跟随系统深浅色。
* 脚本正常时会显式加 tto-light / tto-dark,这里的规则不会误伤手动切到亮色的情况。
*/
@media (prefers-color-scheme: dark) {
.tto-form:not(.tto-light) {
--tto-accent: #7FBEDF;
--tto-accent-soft: rgba(127, 190, 223, .14);
--tto-accent-line: rgba(127, 190, 223, .34);
--tto-accent-strong: #2D578A;
--tto-ink: #D3DCE8;
--tto-ink-strong: #EAF1F8;
--tto-label: #DDE6F0;
--tto-path-text: #CBD6E4;
--tto-subtle: #BFCBDA;
--tto-muted: #9CABBC;
--tto-dim: #6C7B8E;
--tto-total: #8595A8;
--tto-line: #26354A;
--tto-line-soft: #1E2C3F;
--tto-card: #16202F;
--tto-card-head: #1A2635;
--tto-soft: #131C29;
--tto-page: #101928;
--tto-input-bg: #0E1723;
--tto-input-line: #2C3D54;
--tto-ctrl-text: #B4C1D2;
--tto-ctrl-bg: #1B2739;
--tto-ctrl-line: #2E4059;
--tto-ctrl-hover: #223148;
--tto-seg-on-bg: #3A424C;
--tto-seg-on-line: #4B545F;
--tto-seg-on-text: #E8EDF4;
--tto-count-text: #8FC4E4;
--tto-get-text: #82D8AC;
--tto-get-bg: rgba(130, 216, 172, .14);
--tto-post-text: #E8B76E;
--tto-post-bg: rgba(232, 183, 110, .14);
--tto-chip-text: #C2CDDC;
--tto-chip-bg: #1B2739;
--tto-focus-ring: 0 0 0 3px rgba(127, 190, 223, .18);
--tto-bar-shadow: 0 -8px 14px -10px rgba(0, 0, 0, .55);
}
}
.tto-form *,
.tto-form *::before,
.tto-form *::after {
box-sizing: border-box;
}
/* ---------------- 顶部说明 ---------------- */
.tto-form .tto-hero {
display: flex;
align-items: center;
gap: 16px;
margin: 0 0 16px;
padding: 15px 18px;
background: var(--tto-card);
border: 1px solid var(--tto-line);
border-left: 3px solid var(--tto-accent);
border-radius: 5px;
}
.tto-form .tto-hero-main {
flex: 1 1 auto;
min-width: 0;
}
.tto-form .tto-hero-title {
display: flex;
align-items: center;
gap: 8px;
font-size: 15px;
font-weight: 700;
color: var(--tto-ink-strong);
}
.tto-form .tto-hero-ver {
padding: 1px 7px;
font-size: 11px;
font-weight: 600;
line-height: 1.6;
color: var(--tto-count-text);
background: var(--tto-accent-soft);
border-radius: 999px;
}
.tto-form .tto-hero-desc {
margin: 6px 0 0;
font-size: 12px;
line-height: 1.65;
color: var(--tto-muted);
}
.tto-form .tto-hero-side {
flex: 0 0 auto;
text-align: right;
}
.tto-form .tto-chip {
display: inline-block;
padding: 3px 9px;
font-family: Menlo, Monaco, Consolas, "Courier New", monospace;
font-size: 12px;
color: var(--tto-chip-text);
background: var(--tto-chip-bg);
border: 1px solid var(--tto-line);
border-radius: 4px;
}
.tto-form .tto-chip-label {
display: block;
margin-top: 4px;
font-size: 11px;
color: var(--tto-muted);
}
/* ---------------- 卡片 ---------------- */
.tto-form .tto-card {
margin: 0 0 16px;
background: var(--tto-card);
border: 1px solid var(--tto-line);
border-radius: 5px;
overflow: hidden;
/* 让卡片按自身宽度决定是否折行,而不是跟着窗口宽度走 */
container-type: inline-size;
}
.tto-form .tto-card-head {
display: flex;
align-items: center;
gap: 12px;
padding: 11px 16px;
background: var(--tto-card-head);
border-bottom: 1px solid var(--tto-line);
}
.tto-form .tto-card-text {
flex: 1 1 auto;
min-width: 0;
}
.tto-form .tto-card-title {
margin: 0;
font-size: 14px;
font-weight: 700;
color: var(--tto-ink-strong);
}
.tto-form .tto-card-desc {
margin: 3px 0 0;
font-size: 12px;
line-height: 1.5;
color: var(--tto-muted);
}
.tto-form .tto-card-tools {
flex: 0 0 auto;
display: flex;
align-items: center;
gap: 6px;
}
.tto-form .tto-count {
padding: 3px 10px;
font-size: 11px;
color: var(--tto-count-text);
background: var(--tto-accent-soft);
border-radius: 999px;
white-space: nowrap;
}
.tto-form .tto-count b {
font-weight: 700;
}
.tto-form .tto-mini {
height: 26px;
padding: 0 10px;
font-size: 12px;
color: var(--tto-ctrl-text);
background: var(--tto-ctrl-bg);
border: 1px solid var(--tto-ctrl-line);
border-radius: 3px;
cursor: pointer;
transition: background-color .15s ease, border-color .15s ease, color .15s ease;
}
.tto-form .tto-mini:hover {
color: var(--tto-accent);
background: var(--tto-accent-soft);
border-color: var(--tto-accent-line);
}
/* ---------------- 分组标题 ---------------- */
.tto-form .tto-group-head {
display: flex;
align-items: baseline;
gap: 8px;
padding: 10px 16px 7px;
background: var(--tto-soft);
border-bottom: 1px solid var(--tto-line-soft);
}
.tto-form .tto-group-title {
font-size: 12px;
font-weight: 700;
color: var(--tto-subtle);
}
.tto-form .tto-group-note {
flex: 1 1 auto;
min-width: 0;
font-size: 11px;
color: var(--tto-muted);
}
.tto-form .tto-group-total {
flex: 0 0 auto;
font-size: 11px;
color: var(--tto-total);
}
/* ---------------- 通用行 ---------------- */
.tto-form ul.tto-row {
list-style: none;
margin: 0;
padding: 10px 16px;
border-bottom: 1px solid var(--tto-line-soft);
}
.tto-form ul.tto-row:last-child {
border-bottom: 0;
}
.tto-form ul.tto-row > li {
margin: 0;
padding: 0;
list-style: none;
}
/* ---------------- 开关行 ---------------- */
.tto-form ul.tto-row--switch > li {
display: grid;
grid-template-columns: minmax(0, 1fr) auto auto;
align-items: center;
row-gap: 3px;
column-gap: 0;
}
.tto-form .tto-row--switch label.typecho-label {
grid-column: 1;
grid-row: 1;
display: flex;
align-items: center;
gap: 8px;
min-width: 0;
margin: 0;
font-size: 13px;
font-weight: 500;
color: var(--tto-label);
}
.tto-form .tto-row--switch .description {
grid-column: 1;
grid-row: 2;
margin: 0;
font-size: 12px;
line-height: 1.55;
color: var(--tto-muted);
}
.tto-form .tto-row--switch > li > span {
display: flex;
grid-row: 1 / span 2;
margin: 0;
}
.tto-form .tto-row--switch > li > span:nth-of-type(1) {
grid-column: 2;
}
.tto-form .tto-row--switch > li > span:nth-of-type(2) {
grid-column: 3;
}
.tto-form .tto-row--switch input[type="radio"] {
position: absolute;
width: 1px;
height: 1px;
margin: 0;
opacity: 0;
pointer-events: none;
}
.tto-form .tto-row--switch span > label {
display: inline-flex;
align-items: center;
justify-content: center;
min-width: 46px;
height: 26px;
padding: 0 10px;
font-size: 12px;
line-height: 1;
color: var(--tto-ctrl-text);
background: var(--tto-ctrl-bg);
border: 1px solid var(--tto-ctrl-line);
cursor: pointer;
white-space: nowrap;
transition: background-color .15s ease, border-color .15s ease, color .15s ease;
}
.tto-form .tto-row--switch span:nth-of-type(1) > label {
border-radius: 3px 0 0 3px;
}
.tto-form .tto-row--switch span:nth-of-type(2) > label {
border-radius: 0 3px 3px 0;
border-left-width: 0;
}
.tto-form .tto-row--switch span > label:hover {
background: var(--tto-ctrl-hover);
}
.tto-form .tto-row--switch span:nth-of-type(1) input:checked + label {
color: var(--tto-seg-on-text);
font-weight: 600;
background: var(--tto-seg-on-bg);
border-color: var(--tto-seg-on-line);
box-shadow: inset 0 1px 2px rgba(0, 0, 0, .05);
}
.tto-form .tto-row--switch span:nth-of-type(2) input:checked + label {
color: #FFF;
font-weight: 600;
background: var(--tto-accent-strong);
border-color: var(--tto-accent-strong);
}
/* 关掉的接口把路径压淡,扫一眼就知道哪些没开 */
.tto-form ul.tto-row--switch.is-off .tto-path {
color: var(--tto-dim);
}
.tto-form ul.tto-row--switch.is-off .tto-method {
opacity: .6;
}
.tto-form .tto-row--switch input[type="radio"]:focus-visible + label {
outline: 2px solid var(--tto-accent);
outline-offset: 1px;
}
/* 请求方法与路径 */
.tto-form .tto-method {
flex: 0 0 auto;
min-width: 44px;
margin: 0;
padding: 1px 0;
font-family: Menlo, Monaco, Consolas, "Courier New", monospace;
font-size: 10px;
font-weight: 700;
line-height: 1.6;
letter-spacing: .04em;
text-align: center;
border-radius: 3px;
}
.tto-form .tto-method--get {
color: var(--tto-get-text);
background: var(--tto-get-bg);
}
.tto-form .tto-method--post {
color: var(--tto-post-text);
background: var(--tto-post-bg);
}
.tto-form .tto-path {
min-width: 0;
padding: 0;
font-family: Menlo, Monaco, Consolas, "Courier New", monospace;
font-size: 12.5px;
color: var(--tto-path-text);
background: none;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
/* ---------------- 输入行 ---------------- */
.tto-form ul.tto-row--field {
padding: 14px 16px;
}
.tto-form .tto-row--field label.typecho-label {
display: block;
margin: 0 0 6px;
font-size: 13px;
font-weight: 600;
color: var(--tto-label);
}
.tto-form .tto-row--field .description {
margin: 6px 0 0;
font-size: 12px;
line-height: 1.65;
color: var(--tto-muted);
}
.tto-form .tto-row--field input.text {
display: block;
width: 100%;
height: 34px;
padding: 0 10px;
font-size: 13px;
color: var(--tto-ink);
background: var(--tto-input-bg);
border: 1px solid var(--tto-input-line);
border-radius: 3px;
transition: border-color .15s ease, box-shadow .15s ease;
}
.tto-form .tto-row--field textarea {
display: block;
width: 100%;
min-height: 92px;
padding: 8px 10px;
font-family: Menlo, Monaco, Consolas, "Courier New", monospace;
font-size: 12.5px;
line-height: 1.7;
color: var(--tto-ink);
background: var(--tto-input-bg);
border: 1px solid var(--tto-input-line);
border-radius: 3px;
resize: vertical;
transition: border-color .15s ease, box-shadow .15s ease;
}
.tto-form .tto-row--field input.text:focus,
.tto-form .tto-row--field textarea:focus {
border-color: var(--tto-accent);
outline: none;
box-shadow: var(--tto-focus-ring);
}
/* ---------------- 保存 ---------------- */
.tto-form .typecho-option-submit {
position: sticky;
bottom: 0;
z-index: 5;
margin: 0;
padding: 12px 0;
background: var(--tto-page);
border-top: 1px solid var(--tto-line);
box-shadow: var(--tto-bar-shadow);
text-align: center;
}
.tto-form .typecho-option-submit li {
border: 0;
text-align: center;
}
.tto-form .typecho-option-submit button[type="submit"] {
display: inline-block;
width: auto;
min-width: 132px;
height: 36px;
margin: 0 auto;
font-size: 13px;
font-weight: 600;
border-radius: 3px;
}
/* ---------------- 窄屏 ---------------- */
/* 卡片本身变窄时(后台两栏、窄侧栏),工具按钮换到下一行,避免挤压标题 */
@container (max-width: 560px) {
.tto-form .tto-card-head {
flex-wrap: wrap;
}
.tto-form .tto-card-tools {
width: 100%;
justify-content: flex-end;
}
}
@media (max-width: 620px) {
.tto-form .tto-hero {
flex-wrap: wrap;
}
.tto-form .tto-hero-side {
width: 100%;
text-align: left;
}
.tto-form ul.tto-row {
padding: 10px 12px;
}
.tto-form .tto-path {
font-size: 11.5px;
}
}
CSS;
}
/**
* 设置页里的小交互:统计已启用数量、一键全开/全关
*
* @return string
*/
private static function script(): string
{
return <<<'JS'
(function () {
/*
* 深色适配
* 后台主题(NewAdmin)把开关状态放在三处: #newadmin 上的 dark 类、
* dark=dark/light 的 cookie、以及 #darkcss 样式表的 disabled 状态。
* 按这个顺序判断,都没有时跟随系统偏好。
*/
function isDark() {
var host = document.getElementById('newadmin');
if (host && host.classList.contains('dark')) {
return true;
}
if (document.body && document.body.classList.contains('dark')) {
return true;
}
var matched = document.cookie.match(/(?:^|;\s*)dark=([^;]+)/i);
if (matched) {
var value = decodeURIComponent(matched[1]).toLowerCase();
return value === 'dark' || value === 'true' || value === '1';
}
var sheet = document.getElementById('darkcss');
if (sheet) {
return !sheet.disabled;
}
return !!(window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches);
}
function syncTheme() {
var form = document.querySelector('form.tto-form');
if (!form) {
return;
}
var dark = isDark();
if (dark) {
form.classList.add('tto-dark');
form.classList.remove('tto-light');
} else {
form.classList.add('tto-light');
form.classList.remove('tto-dark');
}
}
/* 后台点太阳/月亮切换时不刷新页面,也要跟着变 */
function watchTheme() {
var host = document.getElementById('newadmin');
if (host && window.MutationObserver) {
new MutationObserver(syncTheme).observe(host, {
attributes: true,
attributeFilter: ['class']
});
}
if (window.matchMedia) {
var query = window.matchMedia('(prefers-color-scheme: dark)');
if (query.addEventListener) {
query.addEventListener('change', syncTheme);
} else if (query.addListener) {
query.addListener(syncTheme);
}
}
}
function init() {
var form = document.querySelector('form.tto-form');
if (!form) {
return;
}
syncTheme();
watchTheme();
var rows = form.querySelectorAll('[data-tto-route]');
var counter = form.querySelector('[data-tto="count"]');
function refresh() {
var enabled = 0;
for (var i = 0; i < rows.length; i++) {
var checked = rows[i].querySelector('input[type="radio"]:checked');
var on = !!checked && checked.value === '1';
if (on) {
enabled++;
}
rows[i].classList.toggle('is-off', !on);
}
if (counter) {
counter.textContent = String(enabled);
}
}
function setAll(value) {
for (var i = 0; i < rows.length; i++) {
var input = rows[i].querySelector('input[value="' + value + '"]');
if (input) {
input.checked = true;
}
}
refresh();
}
form.addEventListener('change', function (event) {
if (event.target && event.target.name) {
refresh();
}
});
var onButton = form.querySelector('[data-tto="all-on"]');
var offButton = form.querySelector('[data-tto="all-off"]');
if (onButton) {
onButton.addEventListener('click', function () { setAll('1'); });
}
if (offButton) {
offButton.addEventListener('click', function () { setAll('0'); });
}
refresh();
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
})();
JS;
}
/**
* 个人用户的配置面板
*
* @access public
* @param Form $form
* @return void
*/
public static function personalConfig(Form $form)
{
}
/**
* 构造评论真实IP
*
* @return array
*/
public static function comment($comment, $post)
{
$request = Request::getInstance();
$customIp = $request->getServer('HTTP_X_TYPECHO_TO_OBSIDIAN_IP');
if ($customIp != null) {
$comment['ip'] = $customIp;
}
return $comment;
}
}