forked from laysense/laysite
Prepare LaySite 1.5.0 canvas and publishing improvements
This commit is contained in:
@@ -18,6 +18,8 @@ final class CanvasStyle
|
||||
'mt' => ['margin-top', -128, 128], 'mr' => ['margin-right', -128, 128],
|
||||
'mb' => ['margin-bottom', -128, 128], 'ml' => ['margin-left', -128, 128],
|
||||
'size' => ['font-size', 8, 144],
|
||||
'letter' => ['letter-spacing', -5, 24],
|
||||
'line' => ['line-height', 8, 30],
|
||||
];
|
||||
private const TYPOGRAPHY = [
|
||||
'weight-bold' => 'font-weight:700', 'weight-normal' => 'font-weight:400',
|
||||
@@ -95,7 +97,7 @@ final class CanvasStyle
|
||||
|
||||
private static function classList(mixed $value, bool $textOnly = false): string
|
||||
{
|
||||
if (!is_string($value) || strlen($value) > 320) {
|
||||
if (!is_string($value) || strlen($value) > 1800) {
|
||||
throw new RuntimeException('画布样式无效。');
|
||||
}
|
||||
$classes = [];
|
||||
@@ -109,9 +111,11 @@ final class CanvasStyle
|
||||
$classes[$property] = $class;
|
||||
} elseif ($property === 'color' && ($size === 'theme' || preg_match('/^[a-f0-9]{6}$/D', $size))) {
|
||||
$classes[$property] = $class;
|
||||
} elseif (!$textOnly && $property === 'fill' && self::fill($size) !== null) {
|
||||
$classes[$property] = $class;
|
||||
} elseif (!$textOnly && $property === 'display' && in_array($size, ['block', 'inline', 'inline-block'], true)) {
|
||||
$classes[$property] = $class;
|
||||
} elseif ((!$textOnly || $property === 'size') && isset(self::PROPERTIES[$property])
|
||||
} elseif ((!$textOnly || in_array($property, ['size', 'letter', 'line'], true)) && isset(self::PROPERTIES[$property])
|
||||
&& preg_match('/^(?:0|-?[1-9]\d{0,2})$/D', $size)) {
|
||||
[, $min, $max] = self::PROPERTIES[$property];
|
||||
if ((int) $size >= $min && (int) $size <= $max) {
|
||||
@@ -163,12 +167,46 @@ final class CanvasStyle
|
||||
return $markup;
|
||||
}
|
||||
|
||||
public static function colorRules(string $markup): string
|
||||
private static function fill(string $token): ?string
|
||||
{
|
||||
preg_match_all('/\bstudio-style-color-([a-f0-9]{6})(?![\w-])/', $markup, $matches);
|
||||
if (preg_match('/^solid-([a-f0-9]{6})$/D', $token, $parts)) {
|
||||
return 'background:#' . $parts[1] . '!important';
|
||||
}
|
||||
if (preg_match('/^gradient-(0|[1-9]\d{0,2})-([a-f0-9]{6})-([a-f0-9]{6})$/D', $token, $parts) && (int) $parts[1] <= 360) {
|
||||
return "background:linear-gradient({$parts[1]}deg,#{$parts[2]},#{$parts[3]})!important";
|
||||
}
|
||||
if (!preg_match('/^image-([A-Za-z0-9_-]{1,1334})$/D', $token, $parts)) {
|
||||
return null;
|
||||
}
|
||||
$url = base64_decode(strtr($parts[1], '-_', '+/'), true);
|
||||
if ($url === false || strlen($url) > 1000 || !mb_check_encoding($url, 'UTF-8') || !Html::url($url, true)
|
||||
|| rtrim(strtr(base64_encode($url), '+/', '-_'), '=') !== $parts[1]) {
|
||||
return null;
|
||||
}
|
||||
// Quote the URL for CSS and keep the result safe inside a raw-text HTML style element.
|
||||
$escaped = str_replace(['"', '<', '>'], ['\\22 ', '\\3c ', '\\3e '], $url);
|
||||
|
||||
return 'background-image:url("' . $escaped . '")!important;background-position:center!important;background-size:cover!important;background-repeat:no-repeat!important';
|
||||
}
|
||||
|
||||
public static function dynamicRules(string $markup): string
|
||||
{
|
||||
preg_match_all('/\bstudio-style-(color|fill)-([A-Za-z0-9_-]+)(?![\w-])/', $markup, $matches, PREG_SET_ORDER);
|
||||
$css = '';
|
||||
foreach (array_unique($matches[1]) as $hex) {
|
||||
$css .= ".studio-style-color-{$hex}{color:#{$hex}!important}\n";
|
||||
$seen = [];
|
||||
foreach ($matches as [$class, $property, $value]) {
|
||||
if (isset($seen[$class])) {
|
||||
continue;
|
||||
}
|
||||
$seen[$class] = true;
|
||||
if ($property === 'color') {
|
||||
$rule = preg_match('/^[a-f0-9]{6}$/D', $value) ? "color:#{$value}!important" : null;
|
||||
} else {
|
||||
$rule = self::fill($value);
|
||||
}
|
||||
if ($rule !== null) {
|
||||
$css .= ".$class" . '{' . $rule . "}\n";
|
||||
}
|
||||
}
|
||||
|
||||
return $css;
|
||||
@@ -177,9 +215,11 @@ final class CanvasStyle
|
||||
public static function stylesheet(): string
|
||||
{
|
||||
$css = '';
|
||||
foreach (self::PROPERTIES as $key => [$property, $min, $max]) {
|
||||
foreach (self::PROPERTIES as $key => $definition) {
|
||||
[$property, $min, $max] = $definition;
|
||||
for ($value = $min; $value <= $max; $value++) {
|
||||
$css .= '.' . self::PREFIX . "$key-$value{{$property}:{$value}px!important}\n";
|
||||
$length = $key === 'line' ? $value / 10 : $value . 'px';
|
||||
$css .= '.' . self::PREFIX . "$key-$value{{$property}:{$length}!important}\n";
|
||||
}
|
||||
}
|
||||
for ($width = self::MIN_MEDIA_SCALE; $width <= 100; $width++) {
|
||||
|
||||
+13
-11
@@ -68,17 +68,19 @@ final class Database
|
||||
(slug, name, title, description, heading, accent, lead, image, alt, actions, quicklinks)
|
||||
VALUES ('home', '首页', '我的网站', '欢迎访问我的网站。', '我的网站', '', '', '', '', '[]', '[]')")
|
||||
->execute();
|
||||
$articles = $pdo->prepare("INSERT OR IGNORE INTO pages
|
||||
(slug, name, title, description, heading, accent, lead, image, alt, actions, quicklinks)
|
||||
VALUES ('articles', '文章与动态', '文章与动态', '浏览站点发布的文章。', '文章与动态', '', '', '', '', '[]', '[]')");
|
||||
$articles->execute();
|
||||
if ($articles->rowCount() === 1) {
|
||||
$pdo->prepare("INSERT INTO sections (page_slug, key, title, lead, body, feed, position)
|
||||
VALUES ('articles', 'posts', '最新文章', '', '', ?, 0)")->execute([BlockTemplates::ARTICLE_FEED]);
|
||||
}
|
||||
$category = $pdo->prepare('INSERT OR IGNORE INTO article_categories (slug, name) VALUES (?, ?)');
|
||||
foreach (Repository::CATEGORIES as $slug => $name) {
|
||||
$category->execute([$slug, $name]);
|
||||
if ($version === 0) {
|
||||
$articles = $pdo->prepare("INSERT OR IGNORE INTO pages
|
||||
(slug, name, title, description, heading, accent, lead, image, alt, actions, quicklinks)
|
||||
VALUES ('articles', '文章与动态', '文章与动态', '浏览站点发布的文章。', '文章与动态', '', '', '', '', '[]', '[]')");
|
||||
$articles->execute();
|
||||
if ($articles->rowCount() === 1) {
|
||||
$pdo->prepare("INSERT INTO sections (page_slug, key, title, lead, body, feed, position)
|
||||
VALUES ('articles', 'posts', '最新文章', '', '', ?, 0)")->execute([BlockTemplates::ARTICLE_FEED]);
|
||||
}
|
||||
$category = $pdo->prepare('INSERT OR IGNORE INTO article_categories (slug, name) VALUES (?, ?)');
|
||||
foreach (Repository::CATEGORIES as $slug => $name) {
|
||||
$category->execute([$slug, $name]);
|
||||
}
|
||||
}
|
||||
if ($version < 2) {
|
||||
OAuthCredentials::migrateLegacy($pdo);
|
||||
|
||||
@@ -39,6 +39,18 @@ final class DesignLibrary
|
||||
return $query->fetchAll();
|
||||
}
|
||||
|
||||
public static function palettes(): array
|
||||
{
|
||||
$rows = Database::connect()->query("SELECT id, name, preset, payload FROM design_presets
|
||||
WHERE kind = 'palette' ORDER BY id")->fetchAll();
|
||||
foreach ($rows as &$row) {
|
||||
$row['colors'] = self::normalize('palette', $row['preset'], json_decode($row['payload'], true, flags: JSON_THROW_ON_ERROR));
|
||||
unset($row['payload'], $row['preset']);
|
||||
}
|
||||
|
||||
return $rows;
|
||||
}
|
||||
|
||||
public static function find(int $id, string $kind): ?array
|
||||
{
|
||||
if ($id < 1) {
|
||||
|
||||
@@ -128,4 +128,19 @@ final class LayoutPage
|
||||
DesignLibrary::setDefault($pdo, $kind, $id, $config['default']);
|
||||
}
|
||||
}
|
||||
|
||||
public static function deletePreset(PDO $pdo, string $kind, int $id): void
|
||||
{
|
||||
$field = $kind . '_id';
|
||||
$pdo->prepare("UPDATE pages SET $field = NULL, version = version + 1 WHERE $field = ?")->execute([$id]);
|
||||
$site = json_decode($pdo->query('SELECT payload FROM site_settings WHERE id = 1')->fetchColumn(), true, flags: JSON_THROW_ON_ERROR);
|
||||
if (($site['layout_defaults'][$kind] ?? null) === $id) {
|
||||
unset($site['layout_defaults'][$kind]);
|
||||
}
|
||||
if (($site[$field] ?? null) === $id) {
|
||||
$site[$field] = $site['layout_defaults'][$kind] ?? null;
|
||||
}
|
||||
$pdo->prepare('UPDATE site_settings SET payload = ? WHERE id = 1')->execute([Database::json($site)]);
|
||||
$pdo->prepare('DELETE FROM design_presets WHERE id = ?')->execute([$id]);
|
||||
}
|
||||
}
|
||||
|
||||
+64
-11
@@ -98,7 +98,7 @@ final class Repository
|
||||
$catalog = $feed['mode'] === 'catalog';
|
||||
$grouped = $network || ($feed['mode'] === 'tabs' && count($feed['categories']) > 1);
|
||||
$archivePath = '/articles/feed/' . $page['slug'] . '/' . $section['key'];
|
||||
$section['archivePath'] = empty($page['layout_kind']) && ($page['page_type'] ?? 'page') === 'page' && $page['status'] === 'published' ? $archivePath : null;
|
||||
$section['archivePath'] = $feed['categories'] && empty($page['layout_kind']) && ($page['page_type'] ?? 'page') === 'page' && $page['status'] === 'published' ? $archivePath : null;
|
||||
$section['articleFeed'] = [];
|
||||
$section['networkFeed'] = $network;
|
||||
$section['catalogFeed'] = $catalog;
|
||||
@@ -129,9 +129,9 @@ final class Repository
|
||||
AND sections.key = ? AND sections.feed IS NOT NULL", [$page, $key]);
|
||||
}
|
||||
|
||||
public function savePage(string $slug, array $data, int $version, array $sections = [], bool $replaceDraftSections = false, ?array $sectionOrder = null): void
|
||||
public function savePage(string $slug, array $data, int $version, array $sections = [], bool $replaceDraftSections = false, ?array $sectionOrder = null): array
|
||||
{
|
||||
Database::write(function (PDO $pdo) use ($slug, $data, $version, $sections, $replaceDraftSections, $sectionOrder): void {
|
||||
return Database::write(function (PDO $pdo) use ($slug, $data, $version, $sections, $replaceDraftSections, $sectionOrder): array {
|
||||
$head = $data['head_html'] ?? null;
|
||||
$query = $pdo->prepare('UPDATE pages SET name = ?, title = ?, description = ?, heading = ?, accent = ?, lead = ?,
|
||||
image = ?, alt = ?, image_scale = ?, alignments = ?, actions = ?, quicklinks = ?, visibility = ?, header_id = ?, footer_id = ?, palette_id = ?, status = ?, updated_at = ?,
|
||||
@@ -163,9 +163,29 @@ final class Repository
|
||||
if ($sectionOrder !== null) {
|
||||
self::writeSectionOrder($pdo, $slug, $sectionOrder);
|
||||
}
|
||||
$page = $this->page($slug);
|
||||
if (isset($data['layoutConfig'])) {
|
||||
LayoutPage::saveSettings($pdo, $this->page($slug), $data['layoutConfig']);
|
||||
LayoutPage::saveSettings($pdo, $page, $data['layoutConfig']);
|
||||
}
|
||||
|
||||
return $page;
|
||||
});
|
||||
}
|
||||
|
||||
public function deletePage(string $slug, int $version): void
|
||||
{
|
||||
if ($slug === 'home') {
|
||||
throw new RuntimeException('首页是站点入口,不能删除。');
|
||||
}
|
||||
Database::write(function (PDO $pdo) use ($slug, $version): void {
|
||||
$page = $this->page($slug);
|
||||
$delete = $pdo->prepare('DELETE FROM pages WHERE slug = ? AND version = ?');
|
||||
$delete->execute([$slug, $version]);
|
||||
self::checkVersion($delete->rowCount());
|
||||
if ($page['layout_id'] !== null) {
|
||||
LayoutPage::deletePreset($pdo, $page['layout_kind'], $page['layout_id']);
|
||||
}
|
||||
SiteDesign::unsetSystemPage($pdo, $slug);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -189,7 +209,7 @@ final class Repository
|
||||
{
|
||||
Database::write(function (PDO $pdo) use ($page, $key, $title, $style, $body, $feed, $lead): void {
|
||||
self::insertSection($pdo, $page, $key, compact('title', 'style', 'body', 'feed', 'lead'));
|
||||
self::touchPage($pdo, $page, structureChanged: true);
|
||||
self::touchPage($pdo, $page);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -197,7 +217,14 @@ final class Repository
|
||||
{
|
||||
$query = $pdo->prepare('INSERT OR IGNORE INTO sections (page_slug, key, title, lead, style, body, feed, alignments, presentation, position)
|
||||
SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, COALESCE(MAX(position), -1) + 1 FROM sections WHERE page_slug = ?');
|
||||
$query->execute([$page, $key, $data['title'], $data['lead'], $data['style'], $data['body'], $data['feed'] ?? null, $data['alignments'] ?? '{}', $data['presentation'] ?? '{}', $page]);
|
||||
$feed = $data['feed'] ?? null;
|
||||
if ($feed !== null) {
|
||||
$config = json_decode($feed, true, flags: JSON_THROW_ON_ERROR);
|
||||
$available = $pdo->query('SELECT slug FROM article_categories')->fetchAll(PDO::FETCH_COLUMN);
|
||||
$config['categories'] = array_values(array_intersect($config['categories'], $available));
|
||||
$feed = Database::json($config);
|
||||
}
|
||||
$query->execute([$page, $key, $data['title'], $data['lead'], $data['style'], $data['body'], $feed, $data['alignments'] ?? '{}', $data['presentation'] ?? '{}', $page]);
|
||||
if ($query->rowCount() !== 1) {
|
||||
throw new RuntimeException('此区块锚点已被使用。');
|
||||
}
|
||||
@@ -233,7 +260,7 @@ final class Repository
|
||||
self::checkVersion($revision !== false && (int) $revision === $pageVersion ? 1 : 0);
|
||||
if ($operation === 'delete') {
|
||||
self::deleteSection($pdo, $page, $key, $version);
|
||||
self::touchPage($pdo, $page, structureChanged: true);
|
||||
self::touchPage($pdo, $page);
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -252,7 +279,7 @@ final class Repository
|
||||
}
|
||||
[$keys[$index], $keys[$other]] = [$keys[$other], $keys[$index]];
|
||||
self::writeSectionOrder($pdo, $page, $keys);
|
||||
self::touchPage($pdo, $page, structureChanged: true);
|
||||
self::touchPage($pdo, $page);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -270,10 +297,9 @@ final class Repository
|
||||
}
|
||||
}
|
||||
|
||||
private static function touchPage(PDO $pdo, string $slug, bool $structureChanged = false): void
|
||||
private static function touchPage(PDO $pdo, string $slug): void
|
||||
{
|
||||
$revision = $structureChanged ? ', version = version + 1' : '';
|
||||
$pdo->prepare('UPDATE pages SET updated_at = ?' . $revision . ' WHERE slug = ?')->execute([self::now(), $slug]);
|
||||
$pdo->prepare('UPDATE pages SET updated_at = ?, version = version + 1 WHERE slug = ?')->execute([self::now(), $slug]);
|
||||
}
|
||||
|
||||
public function categories(): array
|
||||
@@ -290,6 +316,33 @@ final class Repository
|
||||
}
|
||||
}
|
||||
|
||||
public function deleteCategory(string $slug): void
|
||||
{
|
||||
Database::write(static function (PDO $pdo) use ($slug): void {
|
||||
$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());
|
||||
|
||||
$sections = $pdo->prepare("SELECT page_slug, key, feed FROM sections WHERE EXISTS
|
||||
(SELECT 1 FROM json_each(sections.feed, '$.categories') WHERE value = ?)");
|
||||
$sections->execute([$slug]);
|
||||
$update = $pdo->prepare('UPDATE sections SET feed = ?, version = version + 1 WHERE page_slug = ? AND key = ?');
|
||||
$affected = [];
|
||||
foreach ($sections->fetchAll() as $section) {
|
||||
$feed = json_decode($section['feed'], true, flags: JSON_THROW_ON_ERROR);
|
||||
$feed['categories'] = array_values(array_diff($feed['categories'], [$slug]));
|
||||
$update->execute([Database::json($feed), $section['page_slug'], $section['key']]);
|
||||
$affected[$section['page_slug']] = true;
|
||||
}
|
||||
foreach (array_keys($affected) as $page) {
|
||||
self::touchPage($pdo, $page);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function articles(string|array|null $category = null, ?string $tag = null, bool $public = true,
|
||||
int $limit = 30, int $offset = 0, bool $directoryOrder = false): array
|
||||
{
|
||||
|
||||
@@ -49,7 +49,7 @@ final class View
|
||||
}
|
||||
}
|
||||
$html = self::fragment($template, $data);
|
||||
$data['canvasColors'] = $admin ? '' : CanvasStyle::colorRules($html . ($data['headerMarkup'] ?? '') . ($data['footerMarkup'] ?? ''));
|
||||
$data['canvasColors'] = $admin ? '' : CanvasStyle::dynamicRules($html . ($data['headerMarkup'] ?? '') . ($data['footerMarkup'] ?? ''));
|
||||
$headSources = $admin ? [] : array_filter([$data['site']['head_html'] ?? '', $data['page']['head_html'] ?? '']);
|
||||
$data['styleNonce'] = $data['canvasColors'] !== '' || !empty($data['canvas']) || $headSources !== []
|
||||
? base64_encode(random_bytes(18)) : '';
|
||||
|
||||
Reference in New Issue
Block a user