Release LaySite 1.4.0 canvas editing and layers
This commit is contained in:
@@ -8,7 +8,7 @@ use RuntimeException;
|
||||
|
||||
final class CanvasStyle
|
||||
{
|
||||
public const PAGE = ['hero', 'heading', 'accent', 'lead', 'image', 'actions', 'navigation'];
|
||||
public const PAGE = ['hero', 'heading', 'accent', 'lead', 'eyebrow', 'image', 'actions', 'navigation', 'breadcrumbs'];
|
||||
public const SECTION = ['section', 'title', 'lead', 'body'];
|
||||
|
||||
private const PROPERTIES = [
|
||||
@@ -40,13 +40,34 @@ final class CanvasStyle
|
||||
|
||||
public static function normalize(mixed $input, array $roles): array
|
||||
{
|
||||
if (!is_array($input) || count($input) > count($roles) + 28) {
|
||||
if (!is_array($input) || count($input) > count($roles) + 50) {
|
||||
throw new RuntimeException('画布样式无效。');
|
||||
}
|
||||
$styles = [];
|
||||
foreach ($input as $role => $style) {
|
||||
if ($roles === self::PAGE && $role === 'breadcrumbLinks') {
|
||||
if (!is_string($style)) {
|
||||
throw new RuntimeException('面包屑无效。');
|
||||
}
|
||||
$styles[$role] = SiteDesign::linkText(SiteDesign::parseLinks($style));
|
||||
continue;
|
||||
}
|
||||
if ($roles === self::PAGE && $role === 'heroEyebrow') {
|
||||
if (!is_string($style) || mb_strlen($style) > 100) {
|
||||
throw new RuntimeException('首屏眉题过长。');
|
||||
}
|
||||
$styles[$role] = $style;
|
||||
continue;
|
||||
}
|
||||
if ($roles === self::PAGE && $role === 'heroHtml') {
|
||||
if (!is_string($style) || strlen($style) > 300000) {
|
||||
throw new RuntimeException('首屏 HTML 无效或过长。');
|
||||
}
|
||||
$styles[$role] = Html::sanitize($style);
|
||||
continue;
|
||||
}
|
||||
if ((!in_array($role, $roles, true) && !($roles === self::PAGE && is_string($role)
|
||||
&& preg_match('/^(?:action-[0-7]|nav-(?:[0-9]|1[0-9]))$/D', $role))) || !is_array($style)) {
|
||||
&& preg_match('/^(?:action-[0-7]|(?:nav|crumb)-(?:[0-9]|1[0-9]))$/D', $role))) || !is_array($style)) {
|
||||
throw new RuntimeException('画布样式无效。');
|
||||
}
|
||||
$classes = self::classList($style['classes'] ?? '');
|
||||
@@ -85,6 +106,10 @@ final class CanvasStyle
|
||||
[$property, $size] = array_pad(explode('-', $token, 2), 2, '');
|
||||
if (isset(self::TYPOGRAPHY[$token])) {
|
||||
$classes[$property] = $class;
|
||||
} elseif ($property === 'color' && ($size === 'theme' || preg_match('/^[a-f0-9]{6}$/D', $size))) {
|
||||
$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])
|
||||
&& preg_match('/^(?:0|-?[1-9]\d{0,2})$/D', $size)) {
|
||||
[, $min, $max] = self::PROPERTIES[$property];
|
||||
@@ -131,6 +156,17 @@ final class CanvasStyle
|
||||
return $markup;
|
||||
}
|
||||
|
||||
public static function colorRules(string $markup): string
|
||||
{
|
||||
preg_match_all('/\bstudio-style-color-([a-f0-9]{6})(?![\w-])/', $markup, $matches);
|
||||
$css = '';
|
||||
foreach (array_unique($matches[1]) as $hex) {
|
||||
$css .= ".studio-style-color-{$hex}{color:#{$hex}!important}\n";
|
||||
}
|
||||
|
||||
return $css;
|
||||
}
|
||||
|
||||
public static function stylesheet(): string
|
||||
{
|
||||
$css = '';
|
||||
@@ -139,6 +175,9 @@ final class CanvasStyle
|
||||
$css .= '.' . self::PREFIX . "$key-$value{{$property}:{$value}px!important}\n";
|
||||
}
|
||||
}
|
||||
for ($width = 10; $width <= 100; $width++) {
|
||||
$css .= ".studio-media-{$width}{--studio-media-factor:calc({$width} / 100)}\n";
|
||||
}
|
||||
foreach (self::TYPOGRAPHY as $key => $value) {
|
||||
$css .= '.' . self::PREFIX . "$key{{$value}!important}\n";
|
||||
}
|
||||
|
||||
+124
-13
@@ -10,26 +10,133 @@ use RuntimeException;
|
||||
final class Updates
|
||||
{
|
||||
public const REPOSITORY = 'https://git.laysense.com/laysense/laysite';
|
||||
private const TAGS = 'https://git.laysense.com/api/v1/repos/laysense/laysite/tags';
|
||||
|
||||
public static function current(): string
|
||||
{
|
||||
return trim(file_get_contents(dirname(__DIR__, 2) . '/VERSION'));
|
||||
$version = trim((string) @file_get_contents(dirname(__DIR__, 2) . '/VERSION'));
|
||||
|
||||
return preg_match('/^\d+\.\d+\.\d+$/D', $version) ? $version : '0.0.0';
|
||||
}
|
||||
|
||||
public static function check(): array
|
||||
{
|
||||
$curl = curl_init(self::REPOSITORY . '/raw/branch/main/VERSION');
|
||||
$directory = dirname(__DIR__, 2) . '/runtime';
|
||||
if (!is_dir($directory) && !@mkdir($directory, 0750, true) && !is_dir($directory)) {
|
||||
throw new RuntimeException('无法创建版本检查缓存。');
|
||||
}
|
||||
$path = $directory . '/laysite-update.json';
|
||||
$record = self::cached($path);
|
||||
if (($record['until'] ?? 0) <= time()) {
|
||||
$lock = @fopen($path . '.lock', 'c');
|
||||
if (!$lock) {
|
||||
throw new RuntimeException('无法打开版本检查缓存。');
|
||||
}
|
||||
try {
|
||||
if (!flock($lock, LOCK_EX | LOCK_NB)) {
|
||||
if (!array_key_exists('latest', $record)) {
|
||||
throw new RuntimeException('版本检查正在进行,请稍后重试。');
|
||||
}
|
||||
} else {
|
||||
$record = self::cached($path);
|
||||
if (($record['until'] ?? 0) <= time()) {
|
||||
try {
|
||||
$record = ['latest' => self::latestTag(), 'until' => time() + 300];
|
||||
} catch (RuntimeException $exception) {
|
||||
$record = ['error' => $exception->getMessage(), 'until' => time() + 30];
|
||||
}
|
||||
self::writeCache($path, $record);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
fclose($lock);
|
||||
}
|
||||
}
|
||||
if (isset($record['error'])) {
|
||||
throw new RuntimeException($record['error']);
|
||||
}
|
||||
$current = self::current();
|
||||
$latest = $record['latest'];
|
||||
|
||||
return ['current' => $current, 'latest' => $latest, 'comparison' => $latest === null ? null : version_compare($latest, $current)];
|
||||
}
|
||||
|
||||
private static function cached(string $path): array
|
||||
{
|
||||
$record = json_decode((string) @file_get_contents($path), true);
|
||||
|
||||
if (!is_array($record) || !is_int($record['until'] ?? null)) {
|
||||
return [];
|
||||
}
|
||||
if (is_string($record['error'] ?? null)) {
|
||||
return $record;
|
||||
}
|
||||
$latest = $record['latest'] ?? null;
|
||||
|
||||
return array_key_exists('latest', $record) && ($latest === null || (is_string($latest)
|
||||
&& preg_match('/^\d+\.\d+\.\d+$/D', $latest))) ? $record : [];
|
||||
}
|
||||
|
||||
private static function writeCache(string $path, array $record): void
|
||||
{
|
||||
$temporary = @tempnam(dirname($path), 'laysite-update-');
|
||||
if ($temporary === false) {
|
||||
throw new RuntimeException('无法写入版本检查缓存。');
|
||||
}
|
||||
try {
|
||||
if (file_put_contents($temporary, Database::json($record)) === false || !rename($temporary, $path)) {
|
||||
throw new RuntimeException('无法写入版本检查缓存。');
|
||||
}
|
||||
} finally {
|
||||
if (is_file($temporary)) {
|
||||
unlink($temporary);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static function latestTag(): ?string
|
||||
{
|
||||
$latest = null;
|
||||
$deadline = hrtime(true) + 3_000_000_000;
|
||||
$page = 1;
|
||||
do {
|
||||
$remaining = (int) (($deadline - hrtime(true)) / 1_000_000);
|
||||
if ($remaining <= 0) {
|
||||
throw new RuntimeException('读取上游版本超时,请稍后重试。');
|
||||
}
|
||||
$tags = self::tags($page++, $remaining);
|
||||
foreach ($tags as $tag) {
|
||||
$name = $tag['name'] ?? null;
|
||||
if (!is_string($name)) {
|
||||
throw new RuntimeException('上游仓库返回的版本列表无效。');
|
||||
}
|
||||
if (preg_match('/^v(\d+\.\d+\.\d+)$/D', $name, $match)
|
||||
&& ($latest === null || version_compare($match[1], $latest, '>'))) {
|
||||
$latest = $match[1];
|
||||
}
|
||||
}
|
||||
} while (count($tags) === 50);
|
||||
|
||||
return $latest;
|
||||
}
|
||||
|
||||
private static function tags(int $page, int $timeout): array
|
||||
{
|
||||
$curl = curl_init(self::TAGS . '?limit=50&page=' . $page);
|
||||
if ($curl === false) {
|
||||
throw new RuntimeException('无法连接上游仓库,请稍后重试。');
|
||||
}
|
||||
$body = '';
|
||||
$overflow = false;
|
||||
curl_setopt_array($curl, [
|
||||
CURLOPT_CONNECTTIMEOUT_MS => 2500,
|
||||
CURLOPT_TIMEOUT_MS => 6000,
|
||||
CURLOPT_CONNECTTIMEOUT_MS => min(1500, $timeout),
|
||||
CURLOPT_TIMEOUT_MS => $timeout,
|
||||
CURLOPT_FOLLOWLOCATION => false,
|
||||
CURLOPT_HTTPHEADER => ['Accept: application/json'],
|
||||
CURLOPT_USERAGENT => 'LaySite version check',
|
||||
CURLOPT_WRITEFUNCTION => static function (CurlHandle $handle, string $chunk) use (&$body): int {
|
||||
if (strlen($body) + strlen($chunk) > 128) {
|
||||
CURLOPT_WRITEFUNCTION => static function (CurlHandle $handle, string $chunk) use (&$body, &$overflow): int {
|
||||
if (strlen($body) + strlen($chunk) > 256 * 1024) {
|
||||
$overflow = true;
|
||||
return 0;
|
||||
}
|
||||
$body .= $chunk;
|
||||
@@ -37,15 +144,19 @@ final class Updates
|
||||
return strlen($chunk);
|
||||
},
|
||||
]);
|
||||
if (curl_exec($curl) === false || curl_getinfo($curl, CURLINFO_RESPONSE_CODE) !== 200) {
|
||||
throw new RuntimeException('无法读取上游版本,请稍后重试。');
|
||||
if (curl_exec($curl) === false) {
|
||||
throw new RuntimeException($overflow ? '上游版本数据超出长度限制。'
|
||||
: (curl_errno($curl) === CURLE_OPERATION_TIMEDOUT ? '读取上游版本超时,请稍后重试。' : '无法连接上游仓库,请稍后重试。'));
|
||||
}
|
||||
if (!preg_match('/^v?(\d+\.\d+\.\d+)$/D', trim($body), $version)) {
|
||||
throw new RuntimeException('上游仓库返回的版本格式无效。');
|
||||
$status = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);
|
||||
if ($status !== 200) {
|
||||
throw new RuntimeException('上游版本列表暂不可用(HTTP ' . $status . ')。');
|
||||
}
|
||||
$tags = json_decode($body);
|
||||
if (!is_array($tags) || !array_is_list($tags)) {
|
||||
throw new RuntimeException('上游仓库返回的版本列表无效。');
|
||||
}
|
||||
$current = self::current();
|
||||
$latest = $version[1];
|
||||
|
||||
return ['current' => $current, 'latest' => $latest, 'comparison' => version_compare($latest, $current)];
|
||||
return array_map(static fn ($tag): array => is_object($tag) ? get_object_vars($tag) : [], $tags);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,8 +49,11 @@ final class View
|
||||
}
|
||||
}
|
||||
$html = self::fragment($template, $data);
|
||||
$data['canvasColors'] = $admin ? '' : CanvasStyle::colorRules($html . ($data['headerMarkup'] ?? '') . ($data['footerMarkup'] ?? ''));
|
||||
$data['styleNonce'] = $data['canvasColors'] !== '' || !empty($data['canvas']) ? base64_encode(random_bytes(18)) : '';
|
||||
$page = Assets::rewrite(self::fragment($admin ? 'admin-layout' : 'layout', $data + ['content' => $html]), $cdn);
|
||||
$assetSources = $cdn === '' ? "'self'" : "'self' " . $cdn;
|
||||
$styleSources = $assetSources . ($data['styleNonce'] === '' ? '' : " 'nonce-{$data['styleNonce']}'");
|
||||
$articleEditor = $admin && !empty($data['articleEditor']);
|
||||
$editorImages = $articleEditor ? ' data:' : '';
|
||||
$editorStyles = $articleEditor ? "style-src-attr 'unsafe-inline'; " : '';
|
||||
@@ -60,7 +63,7 @@ final class View
|
||||
'Referrer-Policy' => 'strict-origin-when-cross-origin',
|
||||
'X-Frame-Options' => !empty($data['previewMode']) ? 'SAMEORIGIN' : 'DENY',
|
||||
'Content-Security-Policy' => "default-src 'self'; img-src 'self' https:$editorImages; media-src 'self' https:; "
|
||||
. "style-src $assetSources; {$editorStyles}script-src $assetSources; font-src $assetSources; frame-src 'self'; frame-ancestors "
|
||||
. "style-src $styleSources; {$editorStyles}script-src $assetSources; font-src $assetSources; frame-src 'self'; frame-ancestors "
|
||||
. (!empty($data['previewMode']) ? "'self'" : "'none'") . "; form-action 'self'; base-uri 'none'; object-src 'none'",
|
||||
];
|
||||
if ($admin || !empty($data['previewMode'])) {
|
||||
|
||||
Reference in New Issue
Block a user