按分类预览区块模板,修复画布媒体和链接交互 #4

Merged
DeepSeek merged 2 commits from ChatGPT/laysense.cn:studio-template-gallery into main 2026-11-21 21:17:47 +08:00
22 changed files with 448 additions and 33 deletions
Showing only changes of commit d4548393d5 - Show all commits
+13 -1
View File
@@ -27,7 +27,7 @@ php bin/admin.php reset admin@example.com
## 后台编辑
- **页面设计**支持新建草稿、发布和撤回。首屏标题与简介可在桌面或手机画布中直接编辑。区块支持新增、排序、删除、可视化编辑、媒体插入和 HTML 源码。
- **页面设计**支持新建草稿、发布和撤回。首屏标题与简介可在桌面或手机画布中直接编辑。区块支持新增、排序、删除、可视化编辑、媒体插入和 HTML 源码。编辑器左侧可按分类和名称查找模板。悬停或键盘聚焦缩略图可放大,点击可打开预览。
- **媒体库**集中列出项目静态文件及上传文件,支持查找、选取、复制路径、改名和归档。归档资源的已有 URL 保持可访问。上传文件放在 `LAYSENSE_MEDIA_DIR`,由 `/assets/uploads/` 提供。
- **产品与服务**在 `/services` 显示独立的服务端 HTML 目录。后台支持添加、排序、修改和移除条目,并设置分类、封面、简介和按钮链接。
- **网站设置**分别编辑页头标志、导航及按钮,和页脚的公司信息、链接与备注。CDN 设置接受 HTTPS 源站地址。浏览器看到的公开 `/assets/…` 路径使用该源站。
@@ -81,3 +81,15 @@ SQLite 已启用 WAL。迁移服务器或做线上备份时,可用 `sqlite3 st
- 旧站 2024 年 11 月 10 日关于减少全球服务器部署的公告返回空白正文。迁移保留标题、日期、标签和旧链接,正文补录前会标记为不索引。
- SHNU 旧页首屏写 ¥69,详细报价表与正文写 ¥66。新版保留报价表中的 ¥66,并保留旧页学生资格及学分说明。两处普通软著页面的咨询按钮现在直达该页既有的企业微信入口。
- 中文公开页复用页头、页尾、首屏、内容区块、动态列表及同一套 CSS。历史迁移内容见 `resources/seed.json`,结构见 `resources/schema.sql`。
## 画布浏览器回归
`tests/studio-browser.cjs` 用 Playwright 在真实 Chromium 内检查媒体及链接编辑、模板分类、搜索、预览、触屏操作和保存。使用已启动的测试实例、独立的 SQLite 数据库和管理员账户。测试会在该库创建一个草稿页面。
```sh
LAYSENSE_TEST_URL=http://127.0.0.1:8787 \
LAYSENSE_TEST_EMAIL=studio-test@example.com \
node tests/studio-browser.cjs
```
先安装 Playwright 和 Chromium,并在测试进程的环境里设置 `LAYSENSE_TEST_PASSWORD`。已安装在独立目录的 Playwright 可通过 `PLAYWRIGHT_MODULE` 指定模块路径。系统 Chromium 可通过 `CHROMIUM_EXECUTABLE` 指定。
+47
View File
@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
namespace App\Content;
final class BlockTemplates
{
public const GROUPS = [
'content' => ['标题与正文', [
'headline' => ['标题与简介', '<div class="studio-intro"><h3>填写区块标题</h3><p>用一句话补充背景或说明。</p></div>'],
'text' => ['正文', '<p>在这里输入区块内容。</p>'],
'list' => ['步骤列表', '<ol><li>填写第一步</li><li>填写第二步</li><li>填写第三步</li></ol>'],
]],
'cover' => ['封面与横幅', [
'cover' => ['图文封面', '<div class="studio-cover"><img src="/assets/original/pagepan-example.webp" alt="" loading="lazy"><div><h3>用一句话介绍你的产品</h3><p>在首段说明访客能获得什么。</p><a class="button button-primary" href="/services">查看服务</a></div></div>'],
'banner' => ['横幅', '<div class="studio-banner"><h3>用一句话介绍你的项目</h3><p>简要说明项目能为访客做什么。</p><a class="button button-primary" href="/services">了解更多</a></div>'],
]],
'layout' => ['图文与布局', [
'image' => ['图文', '<div class="studio-split"><img src="/assets/original/home-hero.webp" alt="" loading="lazy"><div><h3>介绍主题</h3><p>在这里说明图片或产品。</p></div></div>'],
'grid' => ['双栏网格', '<div class="studio-two-column"><article class="content-card"><h3>第一栏</h3><p>填写主要内容。</p></article><article class="content-card"><h3>第二栏</h3><p>补充相关信息。</p></article></div>'],
]],
'features' => ['特性与口碑', [
'features' => ['特性卡片', '<div class="card-grid feature-grid"><article class="content-card feature-card"><h3>特点一</h3><p>介绍这个特点。</p></article><article class="content-card feature-card"><h3>特点二</h3><p>介绍这个特点。</p></article><article class="content-card feature-card"><h3>特点三</h3><p>介绍这个特点。</p></article></div>'],
'quote' => ['引文', '<blockquote><p>在这里填写引用或用户反馈。</p></blockquote>'],
]],
'details' => ['表格与问答', [
'table' => ['对比表格', '<table><thead><tr><th scope="col">项目</th><th scope="col">说明</th></tr></thead><tbody><tr><td>项目一</td><td>填写说明</td></tr><tr><td>项目二</td><td>填写说明</td></tr></tbody></table>'],
'faq' => ['常见问题', '<div class="faq"><details><summary>请输入问题</summary><div><p>请输入答案。</p></div></details></div>'],
]],
'actions' => ['导航与引导', [
'navigation' => ['快速导航', '<ul class="studio-quick-nav"><li><a href="/services">产品与服务</a></li><li><a href="/articles">动态与文章</a></li><li><a href="/#contact">联系来笙</a></li></ul>'],
'cta' => ['行动按钮', '<p>说明下一步。</p><p><a class="button button-primary" href="#contact">联系来笙</a></p>'],
]],
];
public static function find(string $key): ?array
{
foreach (self::GROUPS as [, $templates]) {
if (isset($templates[$key])) {
return $templates[$key];
}
}
return null;
}
}
+2 -13
View File
@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace App\Controller;
use App\Content\Assets;
use App\Content\BlockTemplates;
use App\Content\Html;
use App\Content\Media;
use App\Content\Repository;
@@ -16,18 +17,6 @@ use Webman\Http\UploadFile;
final class StudioController extends AdminController
{
public const BLOCKS = [
'text' => ['正文', '<p>在这里输入区块内容。</p>'],
'image' => ['图文', '<div class="studio-split"><img src="/assets/original/home-hero.webp" alt="" loading="lazy"><div><h3>介绍主题</h3><p>在这里说明图片或产品。</p></div></div>'],
'banner' => ['横幅', '<div class="studio-banner"><h3>用一句话介绍你的项目</h3><p>简要说明项目能为访客做什么。</p><a class="button button-primary" href="/services">了解更多</a></div>'],
'features' => ['特性卡片', '<div class="card-grid feature-grid"><article class="content-card feature-card"><h3>特点一</h3><p>介绍这个特点。</p></article><article class="content-card feature-card"><h3>特点二</h3><p>介绍这个特点。</p></article><article class="content-card feature-card"><h3>特点三</h3><p>介绍这个特点。</p></article></div>'],
'quote' => ['引文', '<blockquote><p>在这里填写引用或用户反馈。</p></blockquote>'],
'table' => ['对比表格', '<table><thead><tr><th scope="col">项目</th><th scope="col">说明</th></tr></thead><tbody><tr><td>项目一</td><td>填写说明</td></tr><tr><td>项目二</td><td>填写说明</td></tr></tbody></table>'],
'list' => ['步骤列表', '<ol><li>填写第一步</li><li>填写第二步</li><li>填写第三步</li></ol>'],
'cta' => ['行动按钮', '<p>说明下一步。</p><p><a class="button button-primary" href="#contact">联系来笙</a></p>'],
'faq' => ['常见问题', '<div class="faq"><details><summary>请输入问题</summary><div><p>请输入答案。</p></div></details></div>'],
];
public function newPage(Request $request): Response
{
if ($failure = $this->guard($request)) {
@@ -80,7 +69,7 @@ final class StudioController extends AdminController
throw new RuntimeException('区块锚点不可用。');
}
if ($action === 'create') {
$preset = self::BLOCKS[$this->input($request, 'preset')] ?? null;
$preset = BlockTemplates::find($this->input($request, 'preset'));
if ($preset === null) {
throw new RuntimeException('请选择区块类型。');
}
+8 -12
View File
@@ -1,7 +1,7 @@
<?php
use App\Content\SiteDesign;
use App\Content\View;
use App\Controller\StudioController;
use App\Content\BlockTemplates;
$e = View::escape(...);
$slug = $page['slug'];
@@ -19,16 +19,8 @@ $previewUrl = '/admin/pages/' . $slug . '/preview';
<?= View::fragment('admin-feedback', compact('error', 'saved')) ?>
<div class="studio-layout">
<aside class="admin-panel studio-library" aria-labelledby="studio-library-title">
<h2 id="studio-library-title">元素模板</h2>
<div class="studio-presets" data-canvas-presets>
<?php foreach (StudioController::BLOCKS as $preset => [$label, $markup]): ?>
<button type="button" disabled data-canvas-template="<?= $e($preset) ?>"><?= $e($label) ?></button>
<template data-preset="<?= $e($preset) ?>"><?= $markup ?></template>
<?php endforeach ?>
<label class="studio-insert-mode"><input type="checkbox" data-template-inline> 插入当前正文</label>
<p class="field-note" data-template-status role="status"></p>
<ul class="studio-pending" data-template-pending hidden></ul>
</div>
<h2 id="studio-library-title">区块模板</h2>
<?= View::fragment('studio-templates') ?>
<details class="studio-layers">
<summary>首屏与目录</summary>
<button type="button" disabled data-hero-control="image">首屏图片</button>
@@ -43,9 +35,13 @@ $previewUrl = '/admin/pages/' . $slug . '/preview';
<input type="hidden" name="operation" value="create">
<label>锚点<input name="key" pattern="[a-z0-9]+(-[a-z0-9]+)*" maxlength="70" placeholder="overview" required></label>
<label>模板<select name="preset">
<?php foreach (StudioController::BLOCKS as $preset => [$label]): ?>
<?php foreach (BlockTemplates::GROUPS as [$category, $templates]): ?>
<optgroup label="<?= $e($category) ?>">
<?php foreach ($templates as $preset => [$label]): ?>
<option value="<?= $e($preset) ?>"><?= $e($label) ?></option>
<?php endforeach ?>
</optgroup>
<?php endforeach ?>
</select></label>
<button type="submit" class="button button-outline">新建</button>
</form>
+55
View File
@@ -0,0 +1,55 @@
<?php
use App\Content\BlockTemplates;
use App\Content\View;
$e = View::escape(...);
?>
<div class="studio-presets" data-canvas-presets>
<label class="studio-template-search">搜索模板<input type="search" data-template-search placeholder="按名称或分类搜索" autocomplete="off"></label>
<div class="studio-template-filters" role="group" aria-label="模板分类">
<button type="button" data-template-filter="" aria-pressed="true">全部</button>
<?php foreach (BlockTemplates::GROUPS as $group => [$category]): ?>
<button type="button" data-template-filter="<?= $e($group) ?>" aria-pressed="false"><?= $e($category) ?></button>
<?php endforeach ?>
</div>
<label class="studio-insert-mode"><input type="checkbox" data-template-inline> 插入当前正文</label>
<p class="field-note" data-template-status role="status"></p>
<ul class="studio-pending" data-template-pending hidden></ul>
<p class="field-note" data-template-empty hidden role="status">没有找到匹配的模板。</p>
<div class="studio-template-groups">
<?php foreach (BlockTemplates::GROUPS as $group => [$category, $templates]): ?>
<section class="studio-template-group" data-template-group="<?= $e($group) ?>" aria-labelledby="studio-template-<?= $e($group) ?>">
<h3 id="studio-template-<?= $e($group) ?>"><?= $e($category) ?></h3>
<div class="studio-template-cards">
<?php foreach ($templates as $preset => [$label, $markup]): ?>
<article class="studio-template-card" data-template-card data-template-search-text="<?= $e($label . ' ' . $category . ' ' . $preset) ?>">
<button type="button" class="studio-template-image" data-template-preview="<?= $e($preset) ?>" data-template-title="<?= $e($label) ?>"
aria-label="预览<?= $e($label) ?>模板">
<img src="/assets/template-previews/<?= $e($preset) ?>.webp" alt="" width="768" height="480" loading="lazy">
</button>
<div class="studio-template-meta">
<span><?= $e($label) ?></span>
<button type="button" class="button button-outline" disabled data-canvas-template="<?= $e($preset) ?>"
data-template-title="<?= $e($label) ?>" aria-label="添加<?= $e($label) ?>模板">添加</button>
</div>
<template data-preset="<?= $e($preset) ?>"><?= $markup ?></template>
</article>
<?php endforeach ?>
</div>
</section>
<?php endforeach ?>
</div>
<div class="studio-template-peek" data-template-peek aria-hidden="true" hidden>
<img alt="">
<p></p>
</div>
<dialog class="media-picker studio-template-dialog" data-template-dialog aria-labelledby="studio-template-preview-title">
<header class="admin-heading compact">
<h2 id="studio-template-preview-title"></h2>
<button type="button" class="button button-outline" data-template-close>关闭</button>
</header>
<img alt="" width="768" height="480">
<div class="actions"><button type="button" class="button button-primary" data-template-use>添加到画布</button></div>
</dialog>
</div>
+67 -4
View File
@@ -100,7 +100,7 @@
grid-template-areas: "library workspace" "library settings";
gap: .9rem;
}
.studio-library { grid-area: library; position: sticky; top: .75rem; max-height: calc(100vh - 1.5rem); overflow: auto; }
.studio-library { grid-area: library; position: sticky; z-index: 3; top: .75rem; max-height: calc(100vh - 1.5rem); overflow: auto; }
.studio-library h2 { font-size: 1.05rem; margin-bottom: .7rem; }
.studio-workspace { grid-area: workspace; min-width: 0; }
.studio-inspector { grid-area: settings; }
@@ -108,7 +108,7 @@
.studio-workspace .studio-frame-window { height: max(34rem, calc(100dvh - 16rem)); }
.studio-workspace .studio-preview h2 { font-size: 1.2rem; }
.studio-presets { display: grid; gap: .4rem; }
.studio-library :is(.studio-presets, .studio-layers) button:not(.button) {
.studio-library .studio-layers button:not(.button), .studio-pending button {
width: 100%;
min-height: 2.75rem;
padding: .55rem .75rem;
@@ -136,7 +136,70 @@
@media (max-width: 54rem) {
.studio-layout { grid-template-columns: minmax(0, 1fr); grid-template-areas: "library" "workspace" "settings"; }
.studio-library { position: static; max-height: none; }
.studio-presets { grid-template-columns: repeat(auto-fit, minmax(5.35rem, 1fr)); }
.studio-insert-mode, .studio-presets [role="status"], .studio-pending { grid-column: 1 / -1; }
.studio-workspace .studio-frame-window { height: max(25rem, 64dvh); }
}
.studio-template-search { display: grid; gap: .3rem; font-size: .9rem; font-weight: 600; }
.studio-template-search input {
width: 100%;
min-height: 42px;
padding: .45rem .6rem;
color: var(--ink);
background: var(--white);
border: 1px solid var(--line);
border-radius: .5rem;
}
.studio-template-filters { display: flex; gap: .3rem; flex-wrap: wrap; margin-block: .25rem; }
.studio-template-filters button {
min-height: 36px;
padding: .2rem .5rem;
color: var(--muted);
font-size: .825rem;
border: 1px solid var(--line);
border-radius: .35rem;
background: var(--white);
}
.studio-template-filters button[aria-pressed="true"] { border-color: #9bb5fa; color: var(--accent-deep); background: var(--accent-light); }
.studio-template-group + .studio-template-group { margin-top: .9rem; }
.studio-template-group h3 { margin: 0 0 .4rem; font-size: .9rem; font-weight: 650; }
.studio-template-cards { display: grid; gap: .48rem; }
.studio-template-card { min-width: 0; border: 1px solid var(--line); border-radius: .55rem; background: var(--white); }
.studio-template-card:focus-within { border-color: var(--accent); }
.studio-template-image {
display: block;
width: 100%;
padding: 0;
overflow: hidden;
border: 0;
border-radius: .5rem .5rem 0 0;
background: var(--paper);
}
.studio-template-image img { display: block; width: 100%; height: auto; aspect-ratio: 8 / 5; transition: transform .16s ease; }
.studio-template-image:hover img, .studio-template-image:focus-visible img { transform: scale(1.07); }
.studio-template-meta { display: flex; align-items: center; justify-content: space-between; gap: .3rem; padding: .3rem .38rem .3rem .55rem; font-size: .875rem; }
.studio-template-meta .button { min-height: 33px; font-size: .8rem; flex: 0 0 auto; padding: .2rem .45rem; }
.studio-template-peek {
position: fixed;
z-index: 2;
overflow: hidden;
pointer-events: none;
background: var(--white);
border: 1px solid #adbfdf;
border-radius: .65rem;
box-shadow: 0 14px 35px #142e5626;
}
.studio-template-peek img, .studio-template-dialog img { width: 100%; height: auto; aspect-ratio: 8 / 5; display: block; }
.studio-template-peek p { padding: .5rem .8rem; margin: 0; font-size: .9rem; color: var(--ink); }
.studio-template-dialog { width: min(90vw, 49rem); }
.studio-template-dialog > img { border: 1px solid var(--line); border-radius: .55rem; }
.studio-template-dialog > .actions { justify-content: flex-end; margin-top: .75rem; }
@media (max-width: 54rem) {
.studio-template-groups { max-height: 44dvh; overflow-y: auto; padding: .2rem; }
.studio-template-cards { grid-template-columns: repeat(auto-fill, minmax(8.5rem, 1fr)); }
.studio-template-meta { flex-wrap: wrap; }
.studio-template-peek { display: none; }
}
@media (prefers-reduced-motion: reduce) {
.studio-template-image img { transition: none; }
}
+12
View File
@@ -554,3 +554,15 @@ progress { width: 100%; height: .8rem; accent-color: var(--accent); }
.rich :is(img, video)[class*="studio-media-"] { height: auto; max-width: 100%; }
.hero-visual[class*="studio-media-"] { justify-self: center; }
[data-studio-editing] [data-studio-selected] { outline: 2px solid #4477f9; outline-offset: 3px; }
.rich .studio-intro { max-width: 47rem; }
.rich .studio-intro h3, .rich .studio-cover h3 { font-size: clamp(1.75rem, 4vw, 2.8rem); }
.rich .studio-cover { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.05fr); align-items: center; gap: clamp(1rem, 4vw, 3rem); background: var(--paper); padding: clamp(.8rem, 3vw, 2rem); border-radius: var(--radius); }
.rich .studio-cover img { width: 100%; }
.rich .studio-cover p { margin-bottom: .85rem; }
.rich .studio-two-column { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1rem; }
.rich .studio-quick-nav { display: flex; flex-wrap: wrap; gap: .55rem 1rem; list-style: none; padding: .75rem; border: 1px solid var(--line); border-radius: .6rem; }
.rich .studio-quick-nav a { display: inline-flex; align-items: center; min-height: 44px; padding-inline: .4rem; font-weight: 600; }
@media (max-width: 44rem) {
.rich .studio-cover, .rich .studio-two-column { grid-template-columns: minmax(0, 1fr); }
}
+80 -2
View File
@@ -179,6 +179,83 @@ jQuery(function ($) {
return edits;
}
$('[data-canvas-presets]').each(function () {
const $gallery = $(this);
const $search = $gallery.find('[data-template-search]');
const $groups = $gallery.find('[data-template-group]');
const $filters = $gallery.find('[data-template-filter]');
const $peek = $gallery.find('[data-template-peek]');
const $dialog = $gallery.find('[data-template-dialog]');
const $use = $dialog.find('[data-template-use]');
let category = '';
let $selection;
function hidePeek() {
$peek.prop('hidden', true);
}
function filter() {
const query = String($search.val()).trim().toLowerCase();
let count = 0;
$groups.each(function () {
const $group = $(this);
const allowed = !category || this.dataset.templateGroup === category;
let groupCount = 0;
$group.find('[data-template-card]').each(function () {
const visible = allowed && this.dataset.templateSearchText.toLowerCase().includes(query);
$(this).prop('hidden', !visible);
if (visible) groupCount++;
});
$group.prop('hidden', groupCount === 0);
count += groupCount;
});
$gallery.find('[data-template-empty]').prop('hidden', count !== 0);
hidePeek();
}
$search.on('input', filter);
$filters.on('click', function () {
category = this.dataset.templateFilter;
$filters.attr('aria-pressed', 'false');
$(this).attr('aria-pressed', 'true');
filter();
});
$gallery.on('mouseenter focusin', '[data-template-preview]', function (event) {
if (event.type === 'mouseenter' && !matchMedia('(hover: hover)').matches) return;
const bounds = this.getBoundingClientRect();
const width = Math.min(430, window.innerWidth - bounds.right - 24);
if (width < 240) return;
const top = Math.max(12, Math.min(bounds.top, window.innerHeight - width * 0.625 - 56));
$peek.find('img').attr('src', $(this).find('img').attr('src'));
$peek.find('p').text(this.dataset.templateTitle);
$peek.css({width, top, left: bounds.right + 12}).prop('hidden', false);
});
$gallery.on('mouseleave focusout', '[data-template-preview]', hidePeek);
$gallery.closest('.studio-library').on('scroll', hidePeek);
$(window).on('scroll resize', hidePeek);
$gallery.on('click', '[data-template-preview]', function () {
hidePeek();
const $card = $(this).closest('[data-template-card]');
$selection = $card.find('[data-canvas-template]');
$dialog.find('h2').text(this.dataset.templateTitle);
$dialog.find('img').attr('src', $(this).find('img').attr('src'));
$use.prop('disabled', $selection.prop('disabled'));
$dialog.get(0).showModal();
});
$dialog.on('click', '[data-template-close]', () => $dialog.get(0).close());
$use.on('click', () => {
if (!$selection || $selection.prop('disabled')) return;
$dialog.get(0).close();
hidePeek();
$selection.trigger('click');
});
$gallery.on('studio:canvas-ready', () => {
if ($selection) $use.prop('disabled', $selection.prop('disabled'));
});
});
$('[data-preview-studio]').each(function () {
const $studio = $(this);
const $frame = $studio.find('iframe');
@@ -578,7 +655,7 @@ jQuery(function ($) {
section.id = id;
const title = section.querySelector('[data-studio-part="title"]');
title.id = 'heading-' + id;
title.textContent = this.textContent.trim();
title.textContent = this.dataset.templateTitle;
section.setAttribute('aria-labelledby', title.id);
section.querySelector('[data-studio-part="body"]').innerHTML = markup;
end.before(section);
@@ -621,7 +698,7 @@ jQuery(function ($) {
if (!event.target.closest?.('[data-studio-selected]')) selectMedia(null);
});
$doc.on('click.canvas auxclick.canvas', 'a', (event) => event.preventDefault());
const mediaTargets = '[data-studio-hero-image], [data-studio-part="body"] :is(img, audio, video)';
const mediaTargets = '[data-studio-hero-image], [data-studio-part="body"] img, [data-studio-part="body"] audio, [data-studio-part="body"] video';
$doc.on('click.canvas dblclick.canvas', mediaTargets, function (event) {
event.preventDefault();
event.stopPropagation();
@@ -656,6 +733,7 @@ jQuery(function ($) {
$toolbar.prop('hidden', false);
$layout.find('[data-canvas-jump]').prop('hidden', false);
$layout.find('[data-canvas-template], [data-hero-control]').prop('disabled', false);
$presets.triggerHandler('studio:canvas-ready');
}
$frame.on('load', attachCanvas);
Binary file not shown.

After

Width:  |  Height:  |  Size: 5.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB

+163
View File
@@ -0,0 +1,163 @@
'use strict';
const assert = require('node:assert/strict');
const {chromium} = require(process.env.PLAYWRIGHT_MODULE || 'playwright');
const baseURL = process.env.LAYSENSE_TEST_URL || 'http://127.0.0.1:8787';
const email = process.env.LAYSENSE_TEST_EMAIL;
const password = process.env.LAYSENSE_TEST_PASSWORD;
if (!email || !password) {
throw new Error('Set LAYSENSE_TEST_EMAIL and LAYSENSE_TEST_PASSWORD for a disposable test database.');
}
async function run(browser) {
const context = await browser.newContext({baseURL, viewport: {width: 1480, height: 960}, reducedMotion: 'reduce'});
const page = await context.newPage();
const errors = [];
page.on('pageerror', (error) => errors.push(error.message));
await page.goto('/admin/login');
await page.locator('[name=email]').fill(email);
await page.locator('[name=password]').fill(password);
await page.locator('form button[type=submit]').click();
await page.waitForURL('**/admin');
const csrf = await page.locator('[name=csrf]').first().inputValue();
const slug = 'studio-browser-' + Date.now().toString(36);
async function post(path, values) {
const response = await context.request.post(path, {
form: {csrf, ...values},
headers: {'X-Requested-With': 'XMLHttpRequest'},
});
assert.equal(response.status(), 200, await response.text());
return response.json();
}
await post('/admin/new-page', {slug, name: '画布测试页'});
await post('/admin/page-blocks/' + slug, {operation: 'create', key: 'fixtures', preset: 'text'});
await post('/admin/pages/' + slug + '/fixtures', {
version: '0', title: '媒体和链接', lead: '', style: 'plain',
body: '<p><img src="/assets/favicon.png" alt="示例图片" width="96"></p>'
+ '<p><a href="/services">示例链接</a></p>'
+ '<video controls width="260" preload="none"><source src="/assets/original/drag-in-components.mp4"></video>'
+ '<audio controls src="/assets/original/drag-in-components.mp4" preload="none"></audio>',
});
const editorPath = '/admin/pages/' + slug;
await page.goto(editorPath);
const version = await page.locator('#studio-page-settings [name=version]').inputValue();
await post(editorPath, {
version, name: '画布测试页', status: 'draft', title: '画布测试页',
description: '', heading: '画布测试页', accent: '', lead: '',
image: '/assets/original/home-hero.webp', alt: '首屏示例', image_scale: '0',
});
await page.reload();
const frame = page.frameLocator('iframe[title="可编辑页面画布"]');
const sizing = page.locator('[data-media-sizing]');
const properties = page.locator('[data-canvas-properties]');
const closeProperties = () => properties.locator('[data-property-close]').click();
const image = frame.locator('#fixtures img');
await image.click();
await sizing.waitFor({state: 'visible'});
assert.equal(await image.getAttribute('data-studio-selected'), '');
await sizing.locator('[data-media-width]').selectOption('50');
assert.match(await image.getAttribute('class'), /studio-media-50/);
await image.dblclick();
await properties.waitFor({state: 'visible'});
assert.equal(await properties.locator('h2').textContent(), '正文图片');
await properties.locator('[name=url]').fill('/assets/original/home-hero.webp');
await properties.locator('button[type=submit]').click();
assert.equal(await image.getAttribute('src'), '/assets/original/home-hero.webp');
const hero = frame.locator('[data-studio-hero-image]');
await page.locator('iframe[title="可编辑页面画布"]').evaluate((iframe) => iframe.contentWindow.scrollTo(0, 0));
await hero.click();
await sizing.waitFor({state: 'visible'});
assert.equal(await hero.getAttribute('data-studio-selected'), '');
await sizing.locator('[data-replace-media]').click();
assert.equal(await properties.locator('h2').textContent(), '首屏图片');
await closeProperties();
await frame.locator('#fixtures a').click();
await properties.waitFor({state: 'visible'});
assert.equal(await properties.locator('h2').textContent(), '正文链接');
await properties.locator('[name=url]').fill('/articles');
await properties.locator('button[type=submit]').click();
assert.equal(await frame.locator('#fixtures a').getAttribute('href'), '/articles');
for (const [tag, label] of [['video', '视频'], ['audio', '音频']]) {
const media = frame.locator('#fixtures ' + tag);
await media.dispatchEvent('dblclick');
await properties.waitFor({state: 'visible'});
assert.equal(await properties.locator('h2').textContent(), label);
await closeProperties();
}
const gallery = page.locator('[data-canvas-presets]');
assert.equal(await gallery.locator('[data-template-group]').count(), 6);
assert.equal(await gallery.locator('[data-template-card]').count(), 13);
const brokenPreviews = await gallery.locator('[data-template-card] img').evaluateAll(async (images) => {
for (const image of images) image.loading = 'eager';
await Promise.all(images.map((image) => image.decode()));
return images.filter((image) => image.naturalWidth !== 768).length;
});
assert.equal(brokenPreviews, 0);
await gallery.locator('[data-template-filter=cover]').click();
assert.equal(await gallery.locator('[data-template-card]:visible').count(), 2);
const cover = gallery.locator('[data-template-preview=cover]');
await cover.hover();
const peek = gallery.locator('[data-template-peek]');
await peek.waitFor({state: 'visible'});
assert((await peek.boundingBox()).width > (await cover.boundingBox()).width);
await cover.focus();
await page.keyboard.press('Enter');
const dialog = gallery.locator('[data-template-dialog]');
await dialog.waitFor({state: 'visible'});
assert.equal(await dialog.locator('h2').textContent(), '图文封面');
await dialog.locator('[data-template-use]').click();
await frame.locator('section[id^=block-] h2').filter({hasText: '图文封面'}).waitFor();
await gallery.locator('[data-template-filter=""]').click();
const search = gallery.locator('[data-template-search]');
await search.fill('does-not-exist');
await gallery.locator('[data-template-empty]').waitFor({state: 'visible'});
await search.fill('双栏');
assert.equal(await gallery.locator('[data-template-card]:visible').count(), 1);
await Promise.all([
page.waitForResponse((response) => response.url().endsWith(editorPath) && response.request().method() === 'POST' && response.ok()),
page.getByRole('button', {name: '保存页面', exact: true}).first().click(),
]);
await page.locator('[data-save-confirmation]').waitFor({state: 'visible'});
await frame.locator('section[id^=block-] .studio-cover').waitFor();
assert.equal(await frame.locator('#fixtures a').getAttribute('href'), '/articles');
assert.equal(await frame.locator('#fixtures img').getAttribute('src'), '/assets/original/home-hero.webp');
const phone = await browser.newContext({
baseURL, viewport: {width: 390, height: 844}, isMobile: true, hasTouch: true,
storageState: await context.storageState(),
});
const phonePage = await phone.newPage();
phonePage.on('pageerror', (error) => errors.push(error.message));
await phonePage.goto(editorPath);
const phoneGallery = phonePage.locator('[data-canvas-presets]');
await phoneGallery.locator('[data-template-filter=cover]').tap();
await phoneGallery.locator('[data-template-preview=banner]').tap();
await phoneGallery.locator('[data-template-dialog]').waitFor({state: 'visible'});
assert.equal(await phoneGallery.locator('[data-template-dialog] h2').textContent(), '横幅');
assert.deepEqual(errors, []);
await phone.close();
await context.close();
}
(async () => {
const browser = await chromium.launch({executablePath: process.env.CHROMIUM_EXECUTABLE || undefined});
try {
await run(browser);
console.log('Passed Chromium canvas media/link interactions, template gallery, and persistence.');
} finally {
await browser.close();
}
})().catch((error) => {
console.error(error);
process.exitCode = 1;
});