forked from laysense/laysite
Launch LaySite visual editor and MCP workspace
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Content\Assets;
|
||||
use App\Content\Buttons;
|
||||
use App\Content\DesignLibrary;
|
||||
use App\Content\Repository;
|
||||
use App\Content\SiteDesign;
|
||||
use App\Http\Request;
|
||||
use RuntimeException;
|
||||
use support\Response;
|
||||
|
||||
final class DesignController extends AdminController
|
||||
{
|
||||
public function index(Request $request): Response
|
||||
{
|
||||
if ($failure = $this->guard($request)) {
|
||||
return $failure;
|
||||
}
|
||||
|
||||
return $this->render($request, 'admin-design', [
|
||||
'title' => '站点设计', 'items' => DesignLibrary::all(), 'site' => (new Repository())->site(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function edit(Request $request, string $kind, string $key): Response
|
||||
{
|
||||
if ($failure = $this->guard($request)) {
|
||||
return $failure;
|
||||
}
|
||||
$id = $key === 'new' ? null : (int) $key;
|
||||
$item = $id === null ? [
|
||||
'name' => '', 'preset' => array_key_first(DesignLibrary::PRESETS[$kind]),
|
||||
'payload' => DesignLibrary::defaults($kind), 'version' => 0,
|
||||
] : DesignLibrary::find($id, $kind);
|
||||
if (!$item) {
|
||||
return $this->error($request, '找不到这项设计配置。', 404);
|
||||
}
|
||||
$error = '';
|
||||
$default = $id !== null && ((new Repository())->site()[$kind . '_id'] ?? null) === $id;
|
||||
if ($request->method() === 'POST') {
|
||||
try {
|
||||
$item['name'] = $this->input($request, 'name');
|
||||
$item['preset'] = $this->input($request, 'preset');
|
||||
if ($kind === 'palette') {
|
||||
foreach (DesignLibrary::COLORS as $color => $definition) {
|
||||
$item['payload'][$color] = $this->input($request, $color);
|
||||
}
|
||||
} else {
|
||||
$url = $this->input($request, 'action_url');
|
||||
$label = $this->input($request, 'action_label');
|
||||
$item['payload'] = [
|
||||
'components' => $request->post('components', []),
|
||||
'brand' => $this->input($request, 'brand'), 'logo' => $this->input($request, 'logo'),
|
||||
'text' => $this->input($request, 'text'), 'legal' => $this->input($request, 'legal'),
|
||||
'links' => SiteDesign::parseLinks($this->input($request, 'links')),
|
||||
'action' => $url === '' && $label === '' ? [] : [[$url, $label, $this->input($request, 'action_style'), $request->post('action_blank') === '1']],
|
||||
'provider' => [],
|
||||
];
|
||||
foreach (['mode', 'issuer', 'authorization', 'token', 'userinfo', 'client_id', 'secret_env', 'scope', 'subject_field', 'name_field', 'avatar_field', 'token_auth'] as $field) {
|
||||
$item['payload']['provider'][$field] = $this->input($request, 'provider_' . $field);
|
||||
}
|
||||
}
|
||||
$default = $request->post('make_default') === '1';
|
||||
$id = DesignLibrary::save($kind, $id, $item['name'], $item['preset'], $item['payload'], $this->version($request), $default);
|
||||
|
||||
return $this->success($request, "/admin/design/$kind/$id?saved=1");
|
||||
} catch (RuntimeException $ex) {
|
||||
$error = $this->validationMessage($ex);
|
||||
$item['payload'] = DesignLibrary::defaults($kind);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render($request, 'admin-design-edit', [
|
||||
'title' => '设计配置', 'kind' => $kind, 'key' => $key, 'item' => $item, 'default' => $default,
|
||||
'error' => $error, 'saved' => $request->get('saved') === '1',
|
||||
], $error === '' ? 200 : 422);
|
||||
}
|
||||
|
||||
public function site(Request $request): Response
|
||||
{
|
||||
if ($failure = $this->guard($request)) {
|
||||
return $failure;
|
||||
}
|
||||
$site = (new Repository())->site();
|
||||
$error = '';
|
||||
$name = $site['name'];
|
||||
$cdn = $site['cdn']['origin'] ?? '';
|
||||
$version = (int) ($site['revisions']['general'] ?? 0);
|
||||
if ($request->method() === 'POST') {
|
||||
try {
|
||||
$name = $this->input($request, 'name');
|
||||
$cdn = Assets::cdn($this->input($request, 'cdn'));
|
||||
if ($name === '' || mb_strlen($name) > 100) {
|
||||
throw new RuntimeException('站点名称必须为 1 至 100 个字符。');
|
||||
}
|
||||
SiteDesign::saveGeneral($name, $cdn, $this->version($request));
|
||||
|
||||
return $this->success($request, '/admin/settings?saved=1');
|
||||
} catch (RuntimeException $ex) {
|
||||
$error = $this->validationMessage($ex);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render($request, 'studio-settings', [
|
||||
'title' => '站点设置', 'name' => $name, 'cdn' => $cdn, 'version' => $version, 'error' => $error,
|
||||
'saved' => $request->get('saved') === '1',
|
||||
], $error === '' ? 200 : 422);
|
||||
}
|
||||
|
||||
public function preview(Request $request, string $kind, string $id): Response
|
||||
{
|
||||
if ($failure = $this->guard($request)) {
|
||||
return $failure;
|
||||
}
|
||||
if (!DesignLibrary::find((int) $id, $kind)) {
|
||||
return $this->error($request, '找不到这项设计配置。', 404);
|
||||
}
|
||||
$repo = new Repository();
|
||||
$page = $repo->page('home');
|
||||
$page[$kind . '_id'] = (int) $id;
|
||||
if ($kind !== 'palette') {
|
||||
$page['visibility'][$kind] = true;
|
||||
}
|
||||
|
||||
return (new SiteController())->renderPage($repo, $page, true);
|
||||
}
|
||||
|
||||
public function palette(Request $request, string $id): Response
|
||||
{
|
||||
$palette = DesignLibrary::find((int) $id, 'palette');
|
||||
if (!$palette) {
|
||||
return new Response(404);
|
||||
}
|
||||
$declarations = [];
|
||||
foreach ($palette['payload'] as $variable => $value) {
|
||||
$declarations[] = '--' . $variable . ':' . $value;
|
||||
}
|
||||
|
||||
return new Response(200, [
|
||||
'Content-Type' => 'text/css; charset=utf-8', 'Cache-Control' => 'public, max-age=60',
|
||||
'X-Content-Type-Options' => 'nosniff',
|
||||
], ':root, #main-content.page-theme {' . implode(';', $declarations) . '} .page-theme .hero, .hero-home {background:var(--paper)}'
|
||||
. ' #main-content .button-primary, #main-content .button-primary:hover {color:var(--button-ink);background:var(--accent)}');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user