diff --git a/app/Content/Repository.php b/app/Content/Repository.php index d8f1355..402408b 100644 --- a/app/Content/Repository.php +++ b/app/Content/Repository.php @@ -172,13 +172,17 @@ final class Repository }); } - public function deletePage(string $slug, int $version): void + public function deletePage(string $slug, int $version): bool { if ($slug === 'home') { throw new RuntimeException('首页是站点入口,不能删除。'); } - Database::write(function (PDO $pdo) use ($slug, $version): void { - $page = $this->page($slug); + return Database::write(function (PDO $pdo) use ($slug, $version): bool { + $page = $this->one('SELECT pages.layout_id, d.kind AS layout_kind FROM pages + LEFT JOIN design_presets d ON pages.layout_id = d.id WHERE pages.slug = ?', [$slug]); + if ($page === null) { + return false; + } $delete = $pdo->prepare('DELETE FROM pages WHERE slug = ? AND version = ?'); $delete->execute([$slug, $version]); self::checkVersion($delete->rowCount()); @@ -186,6 +190,8 @@ final class Repository LayoutPage::deletePreset($pdo, $page['layout_kind'], $page['layout_id']); } SiteDesign::unsetSystemPage($pdo, $slug); + + return true; }); } @@ -193,7 +199,7 @@ final class Repository { Database::write(function (PDO $pdo) use ($page, $key, $data, $version): void { self::updateSection($pdo, $page, $key, $data, $version); - self::touchPage($pdo, $page); + self::touchPage($pdo, $page, bumpVersion: false); }); } @@ -297,9 +303,10 @@ final class Repository } } - private static function touchPage(PDO $pdo, string $slug): void + private static function touchPage(PDO $pdo, string $slug, bool $bumpVersion = true): void { - $pdo->prepare('UPDATE pages SET updated_at = ?, version = version + 1 WHERE slug = ?')->execute([self::now(), $slug]); + $revision = $bumpVersion ? ', version = version + 1' : ''; + $pdo->prepare('UPDATE pages SET updated_at = ?' . $revision . ' WHERE slug = ?')->execute([self::now(), $slug]); } public function categories(): array @@ -316,15 +323,16 @@ final class Repository } } - public function deleteCategory(string $slug): void + public function deleteCategory(string $slug): bool { - Database::write(static function (PDO $pdo) use ($slug): void { + return Database::write(function (PDO $pdo) use ($slug): bool { + if ($this->one('SELECT 1 FROM article_categories WHERE slug = ?', [$slug]) === null) { + return false; + } $pdo->prepare('UPDATE articles SET version = version + 1 WHERE id IN (SELECT article_id FROM article_category_links WHERE category_slug = ?)')->execute([$slug]); $pdo->prepare('DELETE FROM article_category_links WHERE category_slug = ?')->execute([$slug]); - $delete = $pdo->prepare('DELETE FROM article_categories WHERE slug = ?'); - $delete->execute([$slug]); - self::checkVersion($delete->rowCount()); + $pdo->prepare('DELETE FROM article_categories WHERE slug = ?')->execute([$slug]); $sections = $pdo->prepare("SELECT page_slug, key, feed FROM sections WHERE EXISTS (SELECT 1 FROM json_each(sections.feed, '$.categories') WHERE value = ?)"); @@ -340,6 +348,8 @@ final class Repository foreach (array_keys($affected) as $page) { self::touchPage($pdo, $page); } + + return true; }); } diff --git a/app/Controller/AdminController.php b/app/Controller/AdminController.php index 01ffbf8..310a493 100644 --- a/app/Controller/AdminController.php +++ b/app/Controller/AdminController.php @@ -247,12 +247,13 @@ class AdminController return $failure; } try { - (new Repository())->deleteCategory($slug); + $deleted = (new Repository())->deleteCategory($slug); } catch (RuntimeException $exception) { return $this->error($request, $this->validationMessage($exception), 422); } - return $this->success($request, '/admin/article-categories?deleted=1'); + return $deleted ? $this->success($request, '/admin/article-categories?deleted=1') + : $this->error($request, '找不到此文章分类。', 404); } public function article(Request $request, ?string $id = null): Response @@ -437,7 +438,7 @@ class AdminController } } $saved = $repo->savePage($slug, $data, $this->version($request), $sections, sectionOrder: $order === '' ? null : $order); - if ($request->isAjax()) { + if ($request->expectsJson()) { return $this->json([ 'saved' => true, 'version' => $saved['version'], 'name' => $saved['name'], 'status' => $saved['status'], 'sections' => array_map(static function (array $section) use ($sections): array { @@ -470,12 +471,13 @@ class AdminController return $failure; } try { - (new Repository())->deletePage($slug, $this->version($request)); + $deleted = (new Repository())->deletePage($slug, $this->version($request)); } catch (RuntimeException $exception) { return $this->error($request, $this->validationMessage($exception), 422); } - return $this->success($request, '/admin?deleted=1'); + return $deleted ? $this->success($request, '/admin?deleted=1') + : $this->error($request, '找不到此页面。', 404); } public function section(Request $request, string $slug, string $key): Response @@ -645,7 +647,7 @@ class AdminController protected function guard(Request $request): ?Response { if (!$this->signedIn($request)) { - return $request->isAjax() + return $request->expectsJson() ? $this->json(['message' => '登录已过期。请在新标签页重新登录,再返回继续编辑。'], 401) : View::redirect('/admin/login'); } @@ -802,7 +804,7 @@ class AdminController protected function success(Request $request, string $path): Response { - return $request->isAjax() ? $this->json(['redirect' => $path]) : View::redirect($path); + return $request->expectsJson() ? $this->json(['redirect' => $path]) : View::redirect($path); } protected function json(array $payload, int $status = 200): Response @@ -818,7 +820,7 @@ class AdminController protected function render(Request $request, string $view, array $data, int $status = 200): Response { $token = $this->token($request); - if ($request->isAjax() && $status >= 400) { + if ($request->expectsJson() && $status >= 400) { return $this->json([ 'message' => $data['error'] ?? $data['title'], ...(!empty($data['field']) ? ['field' => $data['field']] : []), diff --git a/public/assets/studio.js b/public/assets/studio.js index e5579eb..2557078 100644 --- a/public/assets/studio.js +++ b/public/assets/studio.js @@ -688,20 +688,23 @@ jQuery(function ($) { } $settings.on('studio:saved', (_event, saved, submitter) => { + if (!Array.isArray(saved?.sections)) return; const versions = new Map(saved.sections.map((section) => [section.key, section])); const dynamic = []; for (const entry of sections) { const revision = versions.get(entry.key); if (!revision) continue; - if (entry.feed && (Object.hasOwn(revision, 'feed') || submitter?.name === 'status' + const committed = Object.hasOwn(revision, 'body'); + // A receipt for an untouched section does not replace its canvas contents. + if (!committed && revision.version !== entry.version) continue; + if (entry.feed && (committed || submitter?.name === 'status' || entry.node.querySelector('[data-studio-module-note]'))) dynamic.push(entry.key); + if (!committed) continue; entry.version = revision.version; entry.node.dataset.studioSection = revision.version; - if (Object.hasOwn(revision, 'body')) entry.source = revision.body; - if (Object.hasOwn(revision, 'feed')) { - entry.feed = revision.feed ? JSON.parse(revision.feed) : null; - if (entry.node.classList.contains('section')) entry.node.classList.toggle('network', entry.feed?.mode === 'network'); - } + entry.source = revision.body; + entry.feed = revision.feed ? JSON.parse(revision.feed) : null; + if (entry.node.classList.contains('section')) entry.node.classList.toggle('network', entry.feed?.mode === 'network'); entry.initialFeed = JSON.stringify(entry.feed); for (const [field, state] of Object.entries(entry.fields)) { state.initial = field === 'body' ? cleanBody(state.node) : plainText(state.node); @@ -869,12 +872,22 @@ jQuery(function ($) { return textTarget(selectedElement); } - function backgroundTarget(node) { + function backgroundTarget() { + const node = selectedElement || (textHost?.matches('[data-studio-part="title"]') && doc.activeElement === textHost ? textHost : activeBody); if (!node || node.matches(mediaTargets)) return null; + if (node.matches('[data-studio-part="title"], [data-studio-part="body"]')) { + return node.closest('[data-studio-section], [data-studio-hero]') || styleTarget(node); + } const target = styleTarget(node); return target?.matches(fillElements) ? target : null; } + function backgroundLabel(target) { + if (target?.hasAttribute('data-studio-section')) return '区块背景填充'; + if (target?.hasAttribute('data-studio-hero')) return '首屏背景填充'; + return '背景填充'; + } + function insertionBody() { if (activeBody?.isConnected) return activeBody; return selectedElement?.closest('[data-studio-section]')?.querySelector('[data-studio-part="body"]') || null; @@ -909,12 +922,13 @@ jQuery(function ($) { const target = doc && formatTarget(); const canInsertText = Boolean(target && activeBody); const canInsertMedia = Boolean(insertionBody()); - const canFill = Boolean(backgroundTarget(selectedElement)); + const fill = backgroundTarget(); + const fillLabel = backgroundLabel(fill); $textTools.prop('hidden', !target); $format.add($fontSize).add($fontPreset).prop('disabled', !target); $textCommands.prop('hidden', !canInsertText); - $insertTools.prop('hidden', !canInsertMedia && !canFill); - $background.prop('hidden', !canFill); + $insertTools.prop('hidden', !canInsertMedia && !fill); + $background.prop('hidden', !fill).attr({title: fillLabel, 'aria-label': fillLabel}); $insertMedia.prop('hidden', !canInsertMedia); const sample = target && (selectedText()[0]?.node || target); const element = sample?.nodeType === 1 ? sample : sample?.parentElement; @@ -1011,7 +1025,7 @@ jQuery(function ($) { } function openFill(text) { - fillTarget = text ? formatTarget() : backgroundTarget(selectedElement); + fillTarget = text ? formatTarget() : backgroundTarget(); if (!fillTarget?.isConnected) return; textColorDialog = text; const computed = doc.defaultView.getComputedStyle(fillTarget); @@ -1025,7 +1039,7 @@ jQuery(function ($) { fillInput('end').val(type === 'gradient' ? '#' + parts[2] : '#214ed7'); fillInput('angle').val(type === 'gradient' ? parts[0] : '90'); fillInput('image').val(type === 'image' ? decodeImage(parts.join('-')) : ''); - $fill.find('[data-fill-title]').text(text ? '文字颜色' : '背景填充'); + $fill.find('[data-fill-title]').text(text ? '文字颜色' : backgroundLabel(fillTarget)); $fill.find('[data-fill-tabs]').prop('hidden', text); $fill.find('[data-fill-panel="solid"]').attr('aria-labelledby', text ? 'canvas-fill-title' : 'fill-tab-solid'); $fillError.prop('hidden', true);