diff --git a/assets/script.js b/assets/script.js index eebe469..706cced 100644 --- a/assets/script.js +++ b/assets/script.js @@ -107,3 +107,7 @@ document openEditor(); }) + +if (document.querySelector(".container").classList.contains("edit")) { + openEditor(); +} diff --git a/src/article_resource.rs b/src/article_resource.rs index a492d09..a2e1fc4 100644 --- a/src/article_resource.rs +++ b/src/article_resource.rs @@ -47,7 +47,7 @@ impl Resource for ArticleResource { revision: i32, created: &'a chrono::DateTime, - slug: &'a str, + cancel_url: Option<&'a str>, title: &'a str, raw: &'a str, rendered: String, @@ -68,7 +68,7 @@ impl Resource for ArticleResource { article_id: data.article_id, revision: data.revision, created: &Local.from_utc_datetime(&data.created), - slug: &data.slug, + cancel_url: Some(&data.slug), title: &data.title, raw: &data.body, rendered: render_markdown(&data.body), diff --git a/src/new_article_resource.rs b/src/new_article_resource.rs index b9c52cd..7b861a3 100644 --- a/src/new_article_resource.rs +++ b/src/new_article_resource.rs @@ -26,11 +26,11 @@ fn title_from_slug(slug: &str) -> String { pub struct NewArticleResource { state: State, - slug: String, + slug: Option, } impl NewArticleResource { - pub fn new(state: State, slug: String) -> Self { + pub fn new(state: State, slug: Option) -> Self { Self { state, slug } } } @@ -56,7 +56,7 @@ impl Resource for NewArticleResource { revision: &'a str, created: &'a str, - slug: &'a str, + cancel_url: Option<&'a str>, title: &'a str, raw: &'a str, rendered: &'a str, @@ -64,7 +64,8 @@ impl Resource for NewArticleResource { script_js_checksum: &'a str, } - let title = title_from_slug(&self.slug); + let title = self.slug.as_ref() + .map_or("".to_owned(), |x| title_from_slug(x)); Box::new(self.head() .and_then(move |head| { @@ -75,7 +76,7 @@ impl Resource for NewArticleResource { article_id: NDASH, revision: NDASH, created: NDASH, - slug: &self.slug, + cancel_url: self.slug.as_ref().map(|x| &**x), title: &title, raw: "", rendered: EMPTY_ARTICLE_MESSAGE, diff --git a/src/state.rs b/src/state.rs index 2d75b01..bf4effd 100644 --- a/src/state.rs +++ b/src/state.rs @@ -38,20 +38,22 @@ struct NewRevision<'a> { latest: bool, } -fn decide_slug(conn: &SqliteConnection, article_id: i32, prev_title: &str, title: &str, prev_slug: &str) -> Result { - if prev_slug == "" { - // Never give a non-empty slug to the front page - return Ok(String::new()); - } - - if title == prev_title { - return Ok(prev_slug.to_owned()); - } - +fn decide_slug(conn: &SqliteConnection, article_id: i32, prev_title: &str, title: &str, prev_slug: Option<&str>) -> Result { let base_slug = ::slug::slugify(title); - if base_slug == prev_slug { - return Ok(base_slug); + if let Some(prev_slug) = prev_slug { + if prev_slug == "" { + // Never give a non-empty slug to the front page + return Ok(String::new()); + } + + if title == prev_title { + return Ok(prev_slug.to_owned()); + } + + if base_slug == prev_slug { + return Ok(base_slug); + } } use schema::article_revisions; @@ -170,7 +172,7 @@ impl State { } let new_revision = base_revision + 1; - let slug = decide_slug(&*conn, article_id, &prev_title, &title, &prev_slug)?; + let slug = decide_slug(&*conn, article_id, &prev_title, &title, Some(&prev_slug))?; diesel::update( article_revisions::table @@ -200,7 +202,7 @@ impl State { }) } - pub fn create_article(&self, target_slug: String, title: String, body: String) + pub fn create_article(&self, target_slug: Option, title: String, body: String) -> CpuFuture { let connection_pool = self.connection_pool.clone(); @@ -225,7 +227,7 @@ impl State { .pop().expect("Statement must evaluate to an integer") }; - let slug = decide_slug(&*conn, article_id, "", &title, &target_slug)?; + let slug = decide_slug(&*conn, article_id, "", &title, target_slug.as_ref().map(|x| &**x))?; let new_revision = 1; diff --git a/src/wiki_lookup.rs b/src/wiki_lookup.rs index 69187f8..c3388f9 100644 --- a/src/wiki_lookup.rs +++ b/src/wiki_lookup.rs @@ -13,25 +13,34 @@ use web::{Lookup, Resource}; type BoxResource = Box; lazy_static! { - static ref LOOKUP_MAP: HashMap BoxResource + Sync + Send>> = { + static ref LOOKUP_MAP: HashMap BoxResource + Sync + Send>> = { let mut lookup_map = HashMap::new(); + lookup_map.insert( + "/_new".to_string(), + Box::new(|state| + Box::new( + NewArticleResource::new(state, None) + ) as BoxResource + ) as Box BoxResource + Sync + Send> + ); + lookup_map.insert( format!("/_assets/style-{}.css", StyleCss::checksum()), - Box::new(|| Box::new(StyleCss) as BoxResource) - as Box Box + Sync + Send> + Box::new(|_| Box::new(StyleCss) as BoxResource) + as Box BoxResource + Sync + Send> ); lookup_map.insert( format!("/_assets/script-{}.js", ScriptJs::checksum()), - Box::new(|| Box::new(ScriptJs) as BoxResource) - as Box Box + Sync + Send> + Box::new(|_| Box::new(ScriptJs) as BoxResource) + as Box BoxResource + Sync + Send> ); lookup_map.insert( format!("/_assets/amatic-sc-v9-latin-regular.woff"), - Box::new(|| Box::new(AmaticFont) as BoxResource) - as Box Box + Sync + Send> + Box::new(|_| Box::new(AmaticFont) as BoxResource) + as Box BoxResource + Sync + Send> ); lookup_map @@ -61,7 +70,7 @@ impl Lookup for WikiLookup { // Reserved namespace return Box::new(finished( - LOOKUP_MAP.get(path).map(|x| x()) + LOOKUP_MAP.get(path).map(|x| x(self.state.clone())) )); } @@ -92,7 +101,7 @@ impl Lookup for WikiLookup { Box::new(self.state.lookup_slug(slug.clone()) .and_then(|x| Ok(Some(match x { SlugLookup::Miss => - Box::new(NewArticleResource::new(state, slug)) as BoxResource, + Box::new(NewArticleResource::new(state, Some(slug))) as BoxResource, SlugLookup::Hit { article_id, revision } => Box::new(ArticleResource::new(state, article_id, revision)) as BoxResource, SlugLookup::Redirect(slug) => diff --git a/templates/article_revision.html b/templates/article_revision.html index 224a193..387c715 100644 --- a/templates/article_revision.html +++ b/templates/article_revision.html @@ -1,6 +1,6 @@ -
+
{{>article_revision_contents.html}}
@@ -19,7 +19,9 @@
- Cancel +{{#cancel_url}} + Cancel +{{/cancel_url}}