fix(web): admin layout, ext add native, keyframes; document quoted CSS ratios

Widen admin main/table display, ensure marqdo ext add builds/copies the
native plugin before mq install, and emit valid @keyframes from style
tables. Prefer quoted cells like "16/9" for CSS values that contain /.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-09-01 09:59:03 +08:00
1 parent 416914a346
commit 2079577314
12 files changed
+354 -148

No files matched your search

+16 -23
View File
@@ -1211,16 +1211,6 @@ fn flash_html(flash: Option<&str>) -> String {
}
}
fn truncate_cell(s: &str, max: usize) -> String {
let mut it = s.chars();
let head: String = it.by_ref().take(max).collect();
if it.next().is_some() {
format!("{head}…")
} else {
head
}
}
fn admin_shell(st: &AppState, title: &str, active: Option<&str>, inner: &str) -> String {
let tables = st
.db_url
@@ -1270,7 +1260,7 @@ fn admin_shell(st: &AppState, title: &str, active: Option<&str>, inner: &str) ->
<style>
:root {{ --ink:#1c1917; --muted:#78716c; --paper:#fafaf9; --line:#e7e5e4; --accent:#0f766e; --ok:#166534; --err:#b91c1c; --side:#f5f5f4; }}
* {{ box-sizing:border-box; }}
body {{ margin:0; font-family:"IBM Plex Sans","Noto Sans SC",sans-serif; background:var(--paper); color:var(--ink); min-height:100vh; display:grid; grid-template-columns:14rem 1fr; }}
body {{ margin:0; font-family:"IBM Plex Sans","Noto Sans SC",sans-serif; background:var(--paper); color:var(--ink); min-height:100vh; display:grid; grid-template-columns:14rem minmax(0,1fr); }}
@media (max-width:720px) {{ body {{ grid-template-columns:1fr; }} .admin-nav {{ border-right:0; border-bottom:1px solid var(--line); }} }}
.admin-nav {{ background:var(--side); border-right:1px solid var(--line); padding:1.1rem 1rem 1.5rem; }}
.admin-nav .brand {{ display:block; font-weight:600; font-size:1.05rem; margin-bottom:1rem; color:var(--ink); }}
@@ -1280,7 +1270,7 @@ body {{ margin:0; font-family:"IBM Plex Sans","Noto Sans SC",sans-serif; backgro
.admin-nav .nav-label {{ margin:1.1rem 0 .35rem; font-size:.75rem; text-transform:uppercase; letter-spacing:.04em; color:var(--muted); }}
.admin-nav .nav-foot {{ margin-top:1.5rem; font-size:.9rem; }}
.admin-nav .muted {{ color:var(--muted); font-size:.9rem; }}
.admin-main {{ padding:1.25rem 1.5rem 2rem; max-width:56rem; }}
.admin-main {{ padding:1.25rem 1.5rem 2rem; min-width:0; width:100%; max-width:none; overflow-x:auto; }}
.admin-main h1 {{ margin:0 0 .35rem; font-size:1.65rem; }}
.crumbs {{ color:var(--muted); font-size:.85rem; margin-bottom:1rem; }}
.crumbs a {{ color:var(--muted); }}
@@ -1290,21 +1280,23 @@ body {{ margin:0; font-family:"IBM Plex Sans","Noto Sans SC",sans-serif; backgro
.btn:hover {{ filter:brightness(1.05); }}
.btn-muted {{ background:#78716c; }}
.btn-ghost {{ background:transparent; color:var(--accent)!important; border:1px solid var(--line); }}
table.data {{ width:100%; border-collapse:collapse; background:#fff; border:1px solid var(--line); border-radius:6px; overflow:hidden; }}
table.data th,table.data td {{ border-bottom:1px solid var(--line); padding:.55rem .7rem; text-align:left; vertical-align:top; font-size:.92rem; }}
table.data th {{ background:#f5f5f4; font-weight:600; }}
.table-wrap {{ width:100%; overflow-x:auto; -webkit-overflow-scrolling:touch; border:1px solid var(--line); border-radius:6px; background:#fff; }}
table.data {{ width:100%; min-width:100%; border-collapse:collapse; background:#fff; }}
table.data th,table.data td {{ border-bottom:1px solid var(--line); padding:.55rem .7rem; text-align:left; vertical-align:top; font-size:.92rem; word-break:break-word; overflow-wrap:anywhere; white-space:pre-wrap; }}
table.data th {{ background:#f5f5f4; font-weight:600; white-space:nowrap; }}
table.data tr:last-child td {{ border-bottom:0; }}
table.data .actions {{ white-space:nowrap; }}
table.data .actions {{ white-space:nowrap; overflow-wrap:normal; word-break:normal; }}
.empty {{ background:#fff; border:1px dashed var(--line); border-radius:6px; padding:2rem 1.25rem; text-align:center; color:var(--muted); }}
.empty strong {{ display:block; color:var(--ink); margin-bottom:.35rem; font-size:1.05rem; }}
.flash {{ padding:.65rem .85rem; border-radius:4px; margin:0 0 1rem; }}
.flash.ok {{ background:#ecfdf5; color:var(--ok); border:1px solid #a7f3d0; }}
.flash.err {{ background:#fef2f2; color:var(--err); border:1px solid #fecaca; }}
.danger {{ color:var(--err); }}
.site-form {{ max-width:28rem; margin-top:.5rem; }}
.site-form {{ max-width:min(48rem,100%); width:100%; margin-top:.5rem; }}
.site-form form {{ display:grid; gap:.85rem; }}
.site-form label {{ display:grid; gap:.25rem; font-size:.9rem; }}
.site-form input,.site-form textarea {{ padding:.5rem .6rem; border:1px solid var(--line); border-radius:4px; font:inherit; background:#fff; }}
.site-form input,.site-form textarea {{ padding:.5rem .6rem; border:1px solid var(--line); border-radius:4px; font:inherit; background:#fff; width:100%; box-sizing:border-box; }}
.site-form textarea {{ min-height:8rem; resize:vertical; field-sizing:content; }}
.site-form input[readonly] {{ background:#f5f5f4; color:var(--muted); }}
.site-form .err {{ color:var(--err); font-size:.85rem; }}
.site-form .actions {{ display:flex; gap:.75rem; align-items:center; flex-wrap:wrap; }}
@@ -1363,14 +1355,14 @@ async fn admin_home(
"<div class=\"empty\"><strong>No tables</strong>Initialize a table via <code>db.init</code> / <code>数据库.初始化</code>.</div>",
);
} else {
inner.push_str("<table class=\"data\"><thead><tr><th>Table</th><th></th></tr></thead><tbody>");
inner.push_str("<div class=\"table-wrap\"><table class=\"data\"><thead><tr><th>Table</th><th></th></tr></thead><tbody>");
for t in &tables {
inner.push_str(&format!(
"<tr><td><a href=\"/admin/{t}\">{t}</a></td><td class=\"actions\"><a class=\"btn\" href=\"/admin/{t}/new\">New</a></td></tr>",
t = esc(t)
));
}
inner.push_str("</tbody></table>");
inner.push_str("</tbody></table></div>");
}
Html(admin_shell(&st, "Admin", None, &inner)).into_response()
}
@@ -1433,7 +1425,7 @@ async fn admin_table(
esc(&table)
));
} else {
inner.push_str("<table class=\"data\"><thead><tr>");
inner.push_str("<div class=\"table-wrap\"><table class=\"data\"><thead><tr>");
for c in &cols {
inner.push_str(&format!("<th>{}</th>", esc(c)));
}
@@ -1450,7 +1442,8 @@ async fn admin_table(
other => other.to_string(),
})
.unwrap_or_default();
inner.push_str(&format!("<td>{}</td>", esc(&truncate_cell(&cell, 80))));
// Show full cell text; CSS wraps / table-wrap scrolls when needed.
inner.push_str(&format!("<td>{}</td>", esc(&cell)));
}
let id = m
.get("id")
@@ -1468,7 +1461,7 @@ async fn admin_table(
}
inner.push_str("</tr>");
}
inner.push_str("</tbody></table>");
inner.push_str("</tbody></table></div>");
}
Html(admin_shell(&st, &table, Some(&table), &inner)).into_response()
}
+238 -109
View File
@@ -310,13 +310,16 @@ pub fn normalize_slot(name: &str) -> String {
/// Style table → CSS text.
///
/// Three table shapes are supported:
/// Table shapes:
/// 1. Rule rows `|媒体|选择器|属性|值|` — rows grouped by `媒体` emit
/// `@media { … }` blocks; rows sharing a selector are merged into one rule.
/// 2. Rule rows `|选择器|属性|值|` — each row emits `selector { prop: value; }`,
/// rows sharing a selector are merged into one rule. `selector` may be any
/// CSS selector (`aside.side`, `ul.side-nav a:hover`).
/// 3. Plain property rows `|属性|值|` — emitted as `.name { prop: value; }`.
/// 4. **`@keyframes`** — selector `@keyframes name` (optional stop in the
/// selector: `@keyframes name from`) or stop in the `属性` column:
/// `|@keyframes fade|from|opacity: 0|` → nested keyframe rules.
///
/// Examples (rule shape):
/// ```text
@@ -327,8 +330,9 @@ pub fn normalize_slot(name: &str) -> String {
/// | 选择器 | 属性 | 值 |
/// |--------|------|-----|
/// | aside.side | padding | 2rem 1.25rem |
/// | aside.side | background | #f5f5f4 |
/// | ul.side-nav a:hover | color | var(--accent) |
/// | @keyframes pulse | 0% | opacity: 0 |
/// | @keyframes pulse | 100% | opacity: 1 |
/// | @keyframes pulse from | opacity | 0 |
/// ```
pub fn as_css_named(name: &str, table: &Value) -> String {
let name = normalize_ref(name);
@@ -338,9 +342,6 @@ pub fn as_css_named(name: &str, table: &Value) -> String {
let mut out = String::new();
match table {
Value::Object(m) => {
// Column-first table object: `{选择器:[…], 属性:[…], 值:[…], 媒体:[…]}`
// (how GFM tables reach the plugin). Row-major maps (one rule per map)
// are handled too: if 选择器 is a scalar, treat it as a single row.
let sels = pick(m, SEL_KEYS).map(as_str_list).unwrap_or_default();
let props = pick(m, PROP_KEYS).map(as_str_list).unwrap_or_default();
let vals = pick(m, VAL_KEYS).map(as_str_list).unwrap_or_default();
@@ -359,117 +360,201 @@ pub fn as_css_named(name: &str, table: &Value) -> String {
)
})
.collect();
// Group by media then selector; or plain `.name { … }` when no selectors.
let mut by_media: Vec<(String, Vec<(String, Vec<String>)>)> = Vec::new();
let mut plain: Vec<String> = Vec::new();
for (media, sel, p, v) in rows {
if p.is_empty() {
continue;
}
let decl = format!(" {p}: {v};");
if !media.is_empty() {
let group = match by_media.iter_mut().find(|(mq, _)| *mq == media) {
Some((_, g)) => g,
None => {
by_media.push((media.clone(), Vec::new()));
let (_, g) = by_media.last_mut().unwrap();
g
}
};
push_rule(group, &sel, decl);
} else if !sel.is_empty() {
let group = match by_media.iter_mut().find(|(mq, _)| mq.is_empty()) {
Some((_, g)) => g,
None => {
by_media.push((String::new(), Vec::new()));
let (_, g) = by_media.last_mut().unwrap();
g
}
};
push_rule(group, &sel, decl);
} else {
plain.push(decl);
}
}
for (media, rules) in by_media {
if media.is_empty() {
for (sel, decls) in rules {
out.push_str(&format!("{} {{\n{}\n}}\n", sel, decls.join("\n")));
}
} else {
let inner = rules
.iter()
.map(|(sel, decls)| format!("{} {{\n{}\n}}", sel, decls.join("\n")))
.collect::<Vec<_>>()
.join("\n");
out.push_str(&format!("@media {} {{\n{}\n}}\n", media, inner));
}
}
if !plain.is_empty() {
out.push_str(&format!(".{} {{\n{}\n}}\n", name, plain.join("\n")));
}
emit_css_rows(&rows, &name, &mut out);
}
Value::Array(rows) => {
// Group rule rows by media query and selector; plain rows as `.name { … }`.
let mut by_media: Vec<(String, Vec<(String, Vec<String>)>)> = Vec::new();
let mut plain: Vec<String> = Vec::new();
for row in rows {
let Some(m) = row.as_object() else { continue };
let media = pick(m, MEDIA_KEYS).map(cell_str).unwrap_or_default();
let sel = pick(m, SEL_KEYS).map(cell_str).unwrap_or_default();
let p = pick(m, PROP_KEYS).map(cell_str).unwrap_or_default();
let v = pick(m, VAL_KEYS).map(cell_str).unwrap_or_default();
if p.is_empty() {
continue;
}
let decl = format!(" {p}: {v};");
if !media.is_empty() {
let group = match by_media.iter_mut().find(|(mq, _)| *mq == media) {
Some((_, g)) => g,
None => {
by_media.push((media.clone(), Vec::new()));
let (_, g) = by_media.last_mut().unwrap();
g
}
};
push_rule(group, &sel, decl);
} else if !sel.is_empty() {
let group = match by_media.iter_mut().find(|(mq, _)| mq.is_empty()) {
Some((_, g)) => g,
None => {
by_media.push((String::new(), Vec::new()));
let (_, g) = by_media.last_mut().unwrap();
g
}
};
push_rule(group, &sel, decl);
} else {
plain.push(decl);
}
}
for (media, rules) in by_media {
if media.is_empty() {
for (sel, decls) in rules {
out.push_str(&format!("{} {{\n{}\n}}\n", sel, decls.join("\n")));
}
} else {
let inner = rules
.iter()
.map(|(sel, decls)| format!("{} {{\n{}\n}}", sel, decls.join("\n")))
.collect::<Vec<_>>()
.join("\n");
out.push_str(&format!("@media {} {{\n{}\n}}\n", media, inner));
}
}
if !plain.is_empty() {
out.push_str(&format!(".{} {{\n{}\n}}\n", name, plain.join("\n")));
}
let rows: Vec<(String, String, String, String)> = rows
.iter()
.filter_map(|row| {
let m = row.as_object()?;
Some((
pick(m, MEDIA_KEYS).map(cell_str).unwrap_or_default(),
pick(m, SEL_KEYS).map(cell_str).unwrap_or_default(),
pick(m, PROP_KEYS).map(cell_str).unwrap_or_default(),
pick(m, VAL_KEYS).map(cell_str).unwrap_or_default(),
))
})
.collect();
emit_css_rows(&rows, &name, &mut out);
}
_ => {}
}
out
}
/// Parse `@keyframes name` or `@keyframes name stop` from a selector cell.
fn parse_keyframes_sel(sel: &str) -> Option<(String, Option<String>)> {
let t = sel.trim();
if t.len() < 10 || !t[..10].eq_ignore_ascii_case("@keyframes") {
return None;
}
let rest = t[10..].trim();
if rest.is_empty() {
return None;
}
// First token = animation name; optional second = keyframe stop.
let mut parts = rest.split_whitespace();
let anim = parts.next()?.to_string();
let stop = parts.next().map(|s| s.to_string());
Some((anim, stop))
}
fn is_keyframe_stop(s: &str) -> bool {
let t = s.trim();
if t.is_empty() {
return false;
}
if t.eq_ignore_ascii_case("from") || t.eq_ignore_ascii_case("to") {
return true;
}
// 0%, 50%, 100.5%, etc.
let stripped = t.trim_end_matches('%');
!stripped.is_empty()
&& stripped
.chars()
.all(|c| c.is_ascii_digit() || c == '.')
&& t.ends_with('%')
}
fn keyframe_decl_lines(css_prop: &str, value: &str) -> Vec<String> {
// Value may be a full declaration (`opacity: 0`) or bare value when the
// CSS property lived in the selector form `@keyframes name from` + 属性.
let v = value.trim().trim_end_matches(';').trim();
if v.is_empty() {
return Vec::new();
}
if v.contains(':') {
v.split(';')
.map(|p| p.trim())
.filter(|p| !p.is_empty())
.map(|p| format!(" {p};"))
.collect()
} else if !css_prop.is_empty() && !is_keyframe_stop(css_prop) {
// `@keyframes name from` + 属性=opacity + 值=0
vec![format!(" {css_prop}: {v};")]
} else {
// Bare value — prefer `opacity: 0` in the 值 column.
vec![format!(" {v};")]
}
}
fn push_keyframe(
groups: &mut Vec<(String, Vec<(String, Vec<String>)>)>,
anim: &str,
stop: &str,
decls: Vec<String>,
) {
if anim.is_empty() || stop.is_empty() || decls.is_empty() {
return;
}
let anim_group = match groups.iter_mut().find(|(n, _)| n == anim) {
Some((_, g)) => g,
None => {
groups.push((anim.to_string(), Vec::new()));
&mut groups.last_mut().unwrap().1
}
};
match anim_group.iter_mut().find(|(s, _)| s == stop) {
Some((_, d)) => d.extend(decls),
None => anim_group.push((stop.to_string(), decls)),
}
}
fn emit_css_rows(rows: &[(String, String, String, String)], class_name: &str, out: &mut String) {
let mut by_media: Vec<(String, Vec<(String, Vec<String>)>)> = Vec::new();
let mut by_keyframes: Vec<(String, Vec<(String, Vec<String>)>)> = Vec::new();
let mut plain: Vec<String> = Vec::new();
for (media, sel, p, v) in rows {
let media = media.trim();
let sel = sel.trim();
let p = p.trim();
let v = v.trim();
if let Some((anim, stop_in_sel)) = parse_keyframes_sel(sel) {
let stop_from_sel = stop_in_sel.is_some();
let (stop, prop_for_decl) = match stop_in_sel {
Some(s) => (s, p.to_string()),
None => {
// 属性 = keyframe stop (from / to / N%); 值 = declaration(s)
if !is_keyframe_stop(p) {
continue;
}
(p.to_string(), String::new())
}
};
let decls = if stop_from_sel {
keyframe_decl_lines(&prop_for_decl, v)
} else {
keyframe_decl_lines("", v)
};
let decls = if decls.is_empty() && !v.is_empty() && !prop_for_decl.is_empty() {
keyframe_decl_lines(&prop_for_decl, v)
} else {
decls
};
push_keyframe(&mut by_keyframes, &anim, &stop, decls);
continue;
}
if p.is_empty() {
continue;
}
let decl = format!(" {p}: {v};");
if !media.is_empty() {
let group = match by_media.iter_mut().find(|(mq, _)| *mq == media) {
Some((_, g)) => g,
None => {
by_media.push((media.to_string(), Vec::new()));
let (_, g) = by_media.last_mut().unwrap();
g
}
};
push_rule(group, sel, decl);
} else if !sel.is_empty() {
let group = match by_media.iter_mut().find(|(mq, _)| mq.is_empty()) {
Some((_, g)) => g,
None => {
by_media.push((String::new(), Vec::new()));
let (_, g) = by_media.last_mut().unwrap();
g
}
};
push_rule(group, sel, decl);
} else {
plain.push(decl);
}
}
for (media, rules) in by_media {
if media.is_empty() {
for (sel, decls) in rules {
out.push_str(&format!("{} {{\n{}\n}}\n", sel, decls.join("\n")));
}
} else {
let inner = rules
.iter()
.map(|(sel, decls)| format!("{} {{\n{}\n}}", sel, decls.join("\n")))
.collect::<Vec<_>>()
.join("\n");
out.push_str(&format!("@media {} {{\n{}\n}}\n", media, inner));
}
}
for (anim, stops) in by_keyframes {
out.push_str(&format!("@keyframes {} {{\n", anim));
for (stop, decls) in stops {
out.push_str(&format!(" {} {{\n", stop));
out.push_str(&decls.join("\n"));
out.push('\n');
out.push_str(" }\n");
}
out.push_str("}\n");
}
if !plain.is_empty() {
out.push_str(&format!(".{} {{\n{}\n}}\n", class_name, plain.join("\n")));
}
}
/// Push `decl` under `sel` into a `(selector, declarations)` group list,
/// merging declarations when the selector already exists.
fn push_rule(groups: &mut Vec<(String, Vec<String>)>, sel: &str, decl: String) {
@@ -614,3 +699,47 @@ fn meta_text(v: &Value) -> String {
other => other.to_string(),
}
}
#[cfg(test)]
mod css_tests {
use super::*;
use serde_json::json;
#[test]
fn keyframes_stop_in_prop() {
let t = json!([
{"选择器": "@keyframes pulse", "属性": "0%", "值": "opacity: 0"},
{"选择器": "@keyframes pulse", "属性": "100%", "值": "opacity: 1"},
]);
let css = as_css_named("anim", &t);
assert!(css.contains("@keyframes pulse {"), "{css}");
assert!(css.contains("0% {"), "{css}");
assert!(css.contains("opacity: 0;"), "{css}");
assert!(css.contains("100% {"), "{css}");
assert!(!css.contains("0%: opacity"), "{css}");
}
#[test]
fn keyframes_stop_in_selector() {
let t = json!([
{"选择器": "@keyframes fade from", "属性": "opacity", "值": "0"},
{"选择器": "@keyframes fade to", "属性": "opacity", "值": "1"},
]);
let css = as_css_named("anim", &t);
assert!(css.contains("@keyframes fade {"), "{css}");
assert!(css.contains("from {"), "{css}");
assert!(css.contains("opacity: 0;"), "{css}");
assert!(css.contains("to {"), "{css}");
}
#[test]
fn media_still_works() {
let t = json!([
{"媒体": "(max-width: 800px)", "选择器": ".x", "属性": "color", "值": "red"},
]);
let css = as_css_named("x", &t);
assert!(css.contains("@media (max-width: 800px)"), "{css}");
assert!(css.contains(".x {"), "{css}");
assert!(css.contains("color: red;"), "{css}");
}
}