rustango_cms/
admin_bar.rs1use std::future::Future;
12
13rustango::__private_runtime::tokio::task_local! {
14 static LABELS: Option<Labels>;
15}
16
17#[derive(Debug, Clone)]
19pub struct Labels {
20 pub edit: String,
21 pub admin: String,
22 pub hide: String,
23 pub templates: String,
24}
25
26pub async fn scope<F: Future>(labels: Option<Labels>, fut: F) -> F::Output {
28 LABELS.scope(labels, fut).await
29}
30
31fn current() -> Option<Labels> {
32 LABELS.try_with(Clone::clone).ok().flatten()
33}
34
35#[must_use]
37pub fn active() -> bool {
38 current().is_some()
39}
40
41fn escape(text: &str) -> String {
42 text.replace('&', "&").replace('<', "<").replace('>', ">").replace('"', """)
43}
44
45#[must_use]
48pub fn add(html: String, page_id: i64) -> String {
49 let Some(labels) = current() else { return html };
50 let edit = if page_id > 0 {
51 format!(
52 "<a href=\"/cms-admin/pages/{page_id}/edit\" class=\"rcms-adminbar-link\">\
53 <svg viewBox=\"0 0 24 24\" aria-hidden=\"true\"><path d=\"M3 17.25V21h3.75L17.81 9.94l-3.75-3.75L3 17.25zm17.71-10.21a1 1 0 0 0 0-1.41l-2.34-2.34a1 1 0 0 0-1.41 0l-1.83 1.83 3.75 3.75 1.83-1.83z\"/></svg>\
54 {}</a>",
55 escape(&labels.edit)
56 )
57 } else {
58 String::new()
59 };
60 let bar = format!(
61 "{STYLE}<div id=\"rcms-adminbar\" role=\"navigation\" aria-label=\"Rustango-CMS\">\
62 <button type=\"button\" id=\"rcms-adminbar-toggle\" aria-expanded=\"true\" title=\"{hide}\">\
63 <img src=\"/cms-admin/static/brand/icon-light.png\" alt=\"Rustango-CMS\" width=\"28\" height=\"28\"></button>\
64 <div id=\"rcms-adminbar-links\">{edit}\
65 <a href=\"/cms-admin/\" class=\"rcms-adminbar-link\">\
66 <svg viewBox=\"0 0 24 24\" aria-hidden=\"true\"><path d=\"M3 13h8V3H3v10zm0 8h8v-6H3v6zm10 0h8V11h-8v10zm0-18v6h8V3h-8z\"/></svg>\
67 {admin}</a>{templates}</div></div>{SCRIPT}",
68 hide = escape(&labels.hide),
69 admin = escape(&labels.admin),
70 templates = if crate::template_inspector::active() {
71 format!(
73 "<button type=\"button\" class=\"rcms-adminbar-link\" data-rcms-templates aria-controls=\"rcms-tpl-panel\" aria-expanded=\"false\">\
74 <svg viewBox=\"0 0 24 24\" aria-hidden=\"true\"><path d=\"M9.4 16.6 4.8 12l4.6-4.6L8 6l-6 6 6 6 1.4-1.4zm5.2 0 4.6-4.6-4.6-4.6L16 6l6 6-6 6-1.4-1.4z\"/></svg>\
75 {}</button>",
76 escape(&labels.templates)
77 )
78 } else {
79 String::new()
80 },
81 );
82 match html.rfind("</body>") {
83 Some(at) => {
84 let mut out = html;
85 out.insert_str(at, &bar);
86 out
87 }
88 None => html + &bar,
89 }
90}
91
92const STYLE: &str = "<style>\
93#rcms-adminbar{position:fixed;right:16px;bottom:16px;z-index:2147483645;display:flex;align-items:center;gap:4px;\
94padding:4px;font:600 13px/1 system-ui,sans-serif;background:#1b1b1f;color:#fff;border-radius:999px;\
95box-shadow:0 10px 30px rgb(0 0 0/.25)}\
96#rcms-adminbar-toggle{all:unset;display:block;width:28px;height:28px;border-radius:50%;cursor:pointer;background:#fff}\
97#rcms-adminbar-toggle img{display:block;width:28px;height:28px}\
98#rcms-adminbar-toggle:focus-visible,.rcms-adminbar-link:focus-visible{outline:2px solid #a78bfa;outline-offset:2px}\
99#rcms-adminbar-links{display:flex;gap:2px}\
100#rcms-adminbar[data-collapsed] #rcms-adminbar-links{display:none}\
101.rcms-adminbar-link{display:inline-flex;align-items:center;gap:6px;padding:8px 12px;border-radius:999px;\
102color:#fff!important;text-decoration:none!important;border:0;background:none;font:inherit;cursor:pointer}\
103.rcms-adminbar-link[aria-expanded=\"true\"]{background:rgb(255 255 255/.2)}\
104.rcms-adminbar-link:hover{background:rgb(255 255 255/.14)}\
105.rcms-adminbar-link svg{width:16px;height:16px;fill:currentColor}\
106@media print{#rcms-adminbar{display:none}}\
107</style>";
108
109const SCRIPT: &str = "<script>(()=>{\
110const bar=document.getElementById('rcms-adminbar');\
111const toggle=document.getElementById('rcms-adminbar-toggle');\
112const set=(collapsed)=>{bar.toggleAttribute('data-collapsed',collapsed);toggle.setAttribute('aria-expanded',String(!collapsed));};\
113try{set(localStorage.getItem('rcms-adminbar')==='collapsed');}catch(e){}\
114toggle.addEventListener('click',()=>{const c=!bar.hasAttribute('data-collapsed');set(c);\
115try{localStorage.setItem('rcms-adminbar',c?'collapsed':'open');}catch(e){}});\
116})();</script>";
117
118#[cfg(test)]
119mod tests {
120 use super::*;
121
122 fn labels() -> Labels {
123 Labels { edit: "Edit this page".into(), admin: "Admin".into(), hide: "Hide".into(), templates: "Templates".into() }
124 }
125
126 #[tokio::test]
127 async fn the_bar_links_to_the_page_editor_and_the_admin() {
128 let out = scope(Some(labels()), async { add("<body><p>x</p></body>".into(), 7) }).await;
129 assert!(out.contains("href=\"/cms-admin/pages/7/edit\""), "{out}");
130 assert!(out.contains("href=\"/cms-admin/\""));
131 assert!(out.find("rcms-adminbar").unwrap() < out.find("</body>").unwrap());
132 let no_page = scope(Some(labels()), async { add("<body></body>".into(), 0) }).await;
133 assert!(!no_page.contains("/edit\""), "no edit link without a page");
134 }
135
136 #[tokio::test]
137 async fn without_a_scope_the_page_is_unchanged() {
138 assert_eq!(add("<body></body>".into(), 7), "<body></body>");
139 assert!(!scope(None, async { active() }).await);
140 }
141}