Files
laysite/app/view/section.php

100 lines
6.6 KiB
PHP

<?php
use App\Content\BlockTemplates;
use App\Content\CanvasStyle;
use App\Content\View;
$e = View::escape(...);
$canvas ??= false;
if ($canvas && isset($section['mirror_id'])) {
$source = $section['mirror_source'] ?? null;
?>
<section class="studio-mirror-block" data-studio-mirror data-studio-section="<?= $e($section['version']) ?>"
data-studio-key="<?= $e($section['key']) ?>" data-studio-type="镜像区块" data-studio-mirror-title="<?= $e($section['title']) ?>">
<div inert aria-hidden="true"><?= View::fragment('section', ['section' => $section, 'canvas' => false, 'moment' => false, 'pageSlug' => $pageSlug]) ?></div>
<div class="studio-mirror-overlay">
<div class="studio-mirror-note">
<span><?= $source ? '镜像区块 · 修改请前往主区块' : '主区块已移除 · 可取消关联后编辑' ?></span>
<?php if ($source): ?>
<a href="/admin/pages/<?= $e($source['page_slug']) ?>?block=<?= $e(rawurlencode($source['section_key'])) ?>#blocks"
target="_blank" rel="noopener noreferrer" data-studio-mirror-open>前往主区块 ↗</a>
<?php endif ?>
<button type="button" data-studio-mirror-detach>取消关联</button>
</div>
</div>
</section>
<?php
return;
}
$body = $section['body'];
$dynamic = !empty($section['feed']);
$presentation = json_decode($section['presentation'] ?? '{}', true, flags: JSON_THROW_ON_ERROR);
$style = static fn (string $role): string => CanvasStyle::classes($presentation, $role);
$text = static fn (string $role): string => CanvasStyle::text($presentation, $role, $section[$role]);
$id = static fn (string $role, string $fallback = ''): string => CanvasStyle::idAttributes($presentation, $role, $canvas, $fallback);
$anchor = CanvasStyle::originalAnchor($presentation, 'section', $section['key']);
$studio = $canvas ? ' data-studio-section="' . $e($section['version']) . '" data-studio-key="' . $e($section['key']) . '" data-studio-template="' . $e($presentation['template'] ?? '') . '" data-studio-type="' . $e(BlockTemplates::sectionType($section, $presentation)) . '" data-studio-source="' . $e(base64_encode($section['body'])) . '"' : '';
if ($canvas && isset($section['main_id'])) {
$studio .= ' data-studio-main';
}
if ($canvas && $dynamic) {
$studio .= ' data-studio-dynamic data-studio-feed="' . $e($section['feed']) . '" tabindex="0"';
}
if ($section['style'] === 'layout'):
?>
<section class="studio-layout-block<?= $style('section') ?>"<?= $id('section', $section['key']) ?><?= $studio ?>>
<?= $anchor ?>
<?php if ($canvas): ?><span data-studio-part="title" hidden><?= $text('title') ?></span><span data-studio-part="lead" hidden><?= $text('lead') ?></span><?php endif ?>
<div class="<?= trim($style('body')) ?>"<?= $id('body') ?><?= $canvas ? ' data-studio-part="body"' : '' ?>><?= $body ?></div>
</section>
<?php
return;
endif;
if ($section['style'] === 'hero-links'):
?>
<div class="wrap hero-directory-slot<?= $style('section') ?>"<?= $id('section', $section['key']) ?><?= $studio ?>>
<?= $anchor ?>
<?php if ($canvas): ?><span data-studio-part="title" hidden><?= $e($section['title']) ?></span><span data-studio-part="lead" hidden><?= $e($section['lead']) ?></span><?php endif ?>
<div class="<?= trim($style('body')) ?>"<?= $id('body') ?><?= $canvas ? ' data-studio-part="body"' : '' ?>><?= $body ?></div>
</div>
<?php
return;
endif;
$alignments = json_decode($section['alignments'] ?? '{}', true, flags: JSON_THROW_ON_ERROR);
if ($dynamic && $body === '<p><a class="back-link" href="/articles">查看全部动态</a></p>') {
$body = '';
}
$network = $section['networkFeed'] ?? false;
$catalog = $section['catalogFeed'] ?? false;
?>
<section class="section section-<?= $e($section['style']) ?><?= $network ? ' network' : '' ?><?= $style('section') ?>"<?= $id('section', $section['key']) ?><?= $section['title'] !== '' ? ' aria-labelledby="' . $e($presentation['title']['id'] ?? 'heading-' . $section['key']) . '"' : '' ?> <?= $moment ? 'data-moment="iris-opens"' : '' ?><?= $studio ?>>
<?= $anchor ?>
<div class="wrap">
<?php if ($canvas || $section['title'] !== '' || $section['lead'] !== ''): ?>
<div class="section-heading">
<?php if ($canvas || $section['title'] !== ''): ?><h2<?= $id('title', 'heading-' . $section['key']) ?> class="<?= trim(View::alignmentClass($alignments['title'] ?? null) . $style('title')) ?>"<?= $canvas ? ' data-studio-part="title"' : '' ?><?= CanvasStyle::hiddenTitle($presentation, 'title', $section['title'], $canvas) ?>><?= $text('title') ?></h2><?php endif ?>
<?php if ($canvas || $section['lead']): ?><p class="<?= trim(View::alignmentClass($alignments['lead'] ?? null) . $style('lead')) ?>"<?= $id('lead') ?><?= $canvas ? ' data-studio-part="lead"' : '' ?>><?= $text('lead') ?></p><?php endif ?>
</div>
<?php endif ?>
<?php if ($network): ?>
<?php foreach ($section['articleFeed'] as $group): ?>
<?php $slug = $group['categories'][0]; $anchor = ($pageSlug ?? '') === 'home' && $section['key'] === 'network' ? $slug : $section['key'] . '-group-' . $slug; ?>
<div class="network-group" id="<?= $e($anchor) ?>">
<h3><?= $e($group['label']) ?></h3>
<?= View::fragment('network-items', ['articles' => $group['articles'], 'standalone' => false]) ?>
</div>
<?php endforeach ?>
<?php if ($section['archivePath'] !== null): ?><p class="article-feed-archive"><a class="back-link" href="<?= $e($section['archivePath']) ?>">查看全部</a></p><?php endif ?>
<?php elseif ($catalog): ?>
<?= View::fragment('service-cards', ['products' => $section['articleFeed'][0]['articles']]) ?>
<?php if ($section['archivePath'] !== null && count($section['articleFeed'][0]['articles']) >= $section['feedLimit']): ?>
<p class="article-feed-archive"><a class="back-link" href="<?= $e($section['archivePath']) ?>">查看全部</a></p>
<?php endif ?>
<?php elseif (!empty($section['articleFeed'])): ?>
<?= View::fragment('article-feed', ['groups' => $section['articleFeed'], 'sectionKey' => $section['key'], 'archivePath' => $section['archivePath']]) ?>
<?php endif ?>
<?php if ($canvas || $body !== ''): ?><div class="rich<?= $dynamic ? ' article-feed-note' : '' ?><?= $style('body') ?>"<?= $id('body') ?><?= $canvas ? ' data-studio-part="body"' : '' ?>><?= $body ?></div><?php endif ?>
</div>
</section>