Launch LaySite visual editor and MCP workspace
This commit is contained in:
@@ -0,0 +1,171 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Content\BlockTemplates;
|
||||
use App\Content\Html;
|
||||
use App\Content\Media;
|
||||
use App\Content\PageTemplates;
|
||||
use App\Content\Repository;
|
||||
use App\Http\Request;
|
||||
use RuntimeException;
|
||||
use support\Response;
|
||||
use Webman\Http\UploadFile;
|
||||
|
||||
final class StudioController extends AdminController
|
||||
{
|
||||
public function newPage(Request $request): Response
|
||||
{
|
||||
if ($failure = $this->guard($request)) {
|
||||
return $failure;
|
||||
}
|
||||
$data = [
|
||||
'title' => '新建页面', 'name' => '', 'slug' => '', 'error' => '', 'pageTemplateDrawer' => true,
|
||||
'openTemplates' => $request->get('templates') === '1',
|
||||
];
|
||||
if ($request->method() === 'POST') {
|
||||
$data['name'] = $this->input($request, 'name');
|
||||
$data['slug'] = $this->input($request, 'slug');
|
||||
try {
|
||||
if (!$this->slug($data['slug']) || in_array($data['slug'], ['admin', 'assets', 'home', 'account', 'mcp', 'palette', 'favicon'], true)
|
||||
|| $data['name'] === '' || mb_strlen($data['name']) > 100) {
|
||||
throw new RuntimeException('请填写页面名称及可用的网址标识。');
|
||||
}
|
||||
$key = $this->input($request, 'template');
|
||||
$template = $key === '' ? [] : PageTemplates::find($key);
|
||||
if ($template === null) {
|
||||
throw new RuntimeException('找不到所选整页模板。');
|
||||
}
|
||||
$template['title'] = $data['name'];
|
||||
(new Repository())->createPage($data['slug'], $data['name'], $template);
|
||||
|
||||
return $this->success($request, '/admin/pages/' . $data['slug']);
|
||||
} catch (RuntimeException $error) {
|
||||
$data['error'] = $this->validationMessage($error);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render($request, 'studio-new-page', $data, $data['error'] === '' ? 200 : 422);
|
||||
}
|
||||
|
||||
public function previewTemplate(Request $request, string $key): Response
|
||||
{
|
||||
if ($failure = $this->guard($request)) {
|
||||
return $failure;
|
||||
}
|
||||
$template = PageTemplates::find($key);
|
||||
|
||||
return $template ? (new SiteController())->renderPage(new Repository(), $template, true)
|
||||
: $this->error($request, '找不到所选整页模板。', 404);
|
||||
}
|
||||
|
||||
public function previewPage(Request $request, string $slug): Response
|
||||
{
|
||||
if ($failure = $this->guard($request)) {
|
||||
return $failure;
|
||||
}
|
||||
$repo = new Repository();
|
||||
$page = $repo->page($slug);
|
||||
|
||||
return $page ? (new SiteController())->renderPage($repo, $page, true, $request->get('canvas') === '1') : $this->error($request, '找不到此页面。', 404);
|
||||
}
|
||||
|
||||
public function blocks(Request $request, string $slug): Response
|
||||
{
|
||||
if ($failure = $this->guard($request)) {
|
||||
return $failure;
|
||||
}
|
||||
$repo = new Repository();
|
||||
if (!$repo->page($slug)) {
|
||||
return $this->error($request, '找不到此页面。', 404);
|
||||
}
|
||||
$action = $this->input($request, 'operation');
|
||||
$key = $this->input($request, 'key');
|
||||
try {
|
||||
if (!$this->slug($key) || in_array($key, ['preview', 'home', 'contact', 'main-content', 'partners', 'supported-projects'], true)) {
|
||||
throw new RuntimeException('区块锚点不可用。');
|
||||
}
|
||||
if ($action === 'create') {
|
||||
$type = $this->input($request, 'preset');
|
||||
$preset = BlockTemplates::find($type);
|
||||
if ($preset === null) {
|
||||
throw new RuntimeException('请选择区块类型。');
|
||||
}
|
||||
$repo->createSection($slug, $key, $preset[0], $preset[4] ?? 'plain', Html::sanitize($preset[1], $key), $preset[3] ?? null, $preset[2] ?? '');
|
||||
|
||||
return $this->success($request, "/admin/pages/$slug/$key");
|
||||
}
|
||||
if (!in_array($action, ['up', 'down', 'delete'], true)) {
|
||||
throw new RuntimeException('操作不可用。');
|
||||
}
|
||||
$pageVersion = $this->input($request, 'page_version');
|
||||
$repo->arrangeSection($slug, $key, $action, $this->version($request), ctype_digit($pageVersion) ? (int) $pageVersion : -1);
|
||||
|
||||
return $this->success($request, "/admin/pages/$slug#blocks");
|
||||
} catch (RuntimeException $error) {
|
||||
return $this->error($request, $this->validationMessage($error), 422);
|
||||
}
|
||||
}
|
||||
|
||||
public function media(Request $request): Response
|
||||
{
|
||||
if ($failure = $this->guard($request)) {
|
||||
return $failure;
|
||||
}
|
||||
$error = '';
|
||||
if ($request->method() === 'POST') {
|
||||
try {
|
||||
$file = $request->file('file');
|
||||
if (!$file instanceof UploadFile || !$file->isValid()) {
|
||||
throw new RuntimeException('请选择要上传的文件。');
|
||||
}
|
||||
$label = $this->input($request, 'label') ?: mb_substr((string) $file->getUploadName(), 0, 160);
|
||||
if (mb_strlen($label) > 160) {
|
||||
throw new RuntimeException('资源名称最多 160 个字符。');
|
||||
}
|
||||
Media::upload($file, $label);
|
||||
|
||||
return $this->success($request, '/admin/media?uploaded=1');
|
||||
} catch (RuntimeException $exception) {
|
||||
$error = $this->validationMessage($exception);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render($request, 'studio-media', [
|
||||
'title' => '媒体库', 'files' => Media::all(true), 'error' => $error, 'saved' => $request->get('uploaded') === '1',
|
||||
], $error === '' ? 200 : 422);
|
||||
}
|
||||
|
||||
public function mediaOptions(Request $request): Response
|
||||
{
|
||||
if ($failure = $this->guard($request)) {
|
||||
return $failure;
|
||||
}
|
||||
|
||||
return $this->json(['files' => Media::all()]);
|
||||
}
|
||||
|
||||
public function mediaMeta(Request $request, string $name): Response
|
||||
{
|
||||
if ($failure = $this->guard($request)) {
|
||||
return $failure;
|
||||
}
|
||||
$label = $this->input($request, 'label');
|
||||
if (!Media::find($name)) {
|
||||
return $this->error($request, '找不到此资源。', 404);
|
||||
}
|
||||
if ($label === '' || mb_strlen($label) > 160) {
|
||||
return $this->error($request, '请输入资源名称,最多 160 个字符。', 422);
|
||||
}
|
||||
Media::update($name, $label, $this->input($request, 'archived') === '1');
|
||||
|
||||
return $this->success($request, '/admin/media');
|
||||
}
|
||||
|
||||
private function slug(string $value): bool
|
||||
{
|
||||
return strlen($value) <= 70 && preg_match('/^[a-z0-9]+(?:-[a-z0-9]+)*$/D', $value) === 1;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user