Add canvas element tools and categorized article editing

This commit is contained in:
2026-11-21 22:24:58 +08:00
parent 6efcdefadf
commit a5b3408358
45 changed files with 1607 additions and 660 deletions
+5 -68
View File
@@ -27,7 +27,7 @@ final class StudioController extends AdminController
$data['name'] = $this->input($request, 'name');
$data['slug'] = $this->input($request, 'slug');
try {
if (!$this->slug($data['slug']) || in_array($data['slug'], ['admin', 'assets', 'articles', 'home', 'services', 'news', 'main', 'favicon'], true)
if (!$this->slug($data['slug']) || in_array($data['slug'], ['admin', 'assets', 'articles', 'home', 'services', 'network', 'news', 'main', 'favicon'], true)
|| $data['name'] === '' || mb_strlen($data['name']) > 100) {
throw new RuntimeException('请填写页面名称及可用的网址标识。');
}
@@ -65,15 +65,16 @@ final class StudioController extends AdminController
$action = $this->input($request, 'operation');
$key = $this->input($request, 'key');
try {
if (!$this->slug($key) || in_array($key, ['preview', 'home', 'contact', 'main-content', 'blog', 'partners', 'supported-projects'], true)) {
if (!$this->slug($key) || in_array($key, ['preview', 'home', 'contact', 'main-content', 'partners', 'supported-projects'], true)) {
throw new RuntimeException('区块锚点不可用。');
}
if ($action === 'create') {
$preset = BlockTemplates::find($this->input($request, 'preset'));
$type = $this->input($request, 'preset');
$preset = BlockTemplates::find($type);
if ($preset === null) {
throw new RuntimeException('请选择区块类型。');
}
$repo->createSection($slug, $key, $preset[0], 'plain', $preset[1]);
$repo->createSection($slug, $key, $preset[0], 'plain', $preset[1], $type === 'articles' ? BlockTemplates::ARTICLE_FEED : null);
return $this->success($request, "/admin/pages/$slug/$key");
}
@@ -121,53 +122,6 @@ final class StudioController extends AdminController
], $error === '' ? 200 : 422);
}
public function catalog(Request $request): Response
{
if ($failure = $this->guard($request)) {
return $failure;
}
return $this->render($request, 'studio-catalog', ['title' => '产品与服务', 'products' => SiteDesign::catalog()]);
}
public function product(Request $request, ?string $id = null): Response
{
if ($failure = $this->guard($request)) {
return $failure;
}
$fields = ['name', 'category', 'body', 'image', 'alt', 'url', 'label', 'extra_url', 'extra_label', 'position'];
$product = $id !== null ? SiteDesign::product((int) $id) : array_fill_keys($fields, '') + ['version' => 0];
if (!$product) {
return $this->error($request, '找不到此产品或服务。', 404);
}
$error = '';
if ($request->method() === 'POST') {
try {
if ($id !== null && $this->input($request, 'operation') === 'delete') {
SiteDesign::deleteProduct((int) $id, $this->version($request));
return $this->success($request, '/admin/catalog');
}
foreach ($fields as $field) {
$product[$field] = $this->input($request, $field);
}
$product['version'] = $this->version($request);
$this->checkProduct($product);
$product['body'] = Html::sanitize($product['body']);
$savedId = SiteDesign::saveProduct($id === null ? null : (int) $id, $product, $product['version']);
return $this->success($request, '/admin/catalog/' . $savedId . '?saved=1');
} catch (RuntimeException $exception) {
$error = $this->validationMessage($exception);
}
}
return $this->render($request, 'studio-product', [
'title' => $id === null ? '添加产品或服务' : '编辑产品或服务', 'product' => $product,
'id' => $id, 'error' => $error, 'saved' => $request->get('saved') === '1',
], $error === '' ? 200 : 422);
}
public function media(Request $request): Response
{
if ($failure = $this->guard($request)) {
@@ -256,23 +210,6 @@ final class StudioController extends AdminController
return $result;
}
private function checkProduct(array $item): void
{
if ($item['name'] === '' || mb_strlen($item['name']) > 100 || $item['category'] === '' || mb_strlen($item['category']) > 70
|| strlen($item['body']) > 300000 || strlen($item['image']) > 1000 || mb_strlen($item['alt']) > 200
|| ($item['image'] !== '' && !Html::url($item['image'], true)) || !preg_match('/^-?\d{1,6}$/D', $item['position'])) {
throw new RuntimeException('请检查名称、分类、简介、封面及排序字段。');
}
foreach ([['url', 'label'], ['extra_url', 'extra_label']] as [$href, $label]) {
if ($href === 'extra_url' && $item[$href] === '' && $item[$label] === '') {
continue;
}
if (strlen($item[$href]) > 1000 || !Html::url($item[$href]) || $item[$label] === '' || mb_strlen($item[$label]) > 70) {
throw new RuntimeException('请填写按钮文案和有效链接。');
}
}
}
private function slug(string $value): bool
{
return strlen($value) <= 70 && preg_match('/^[a-z0-9]+(?:-[a-z0-9]+)*$/D', $value) === 1;