Refactor server-side initiated edit mode.

Implement support for opening articles for editing without javascript
This commit is contained in:
Magnus Hoff 2017-10-02 21:30:31 +02:00
parent 413675e152
commit cdcdf8e72f
4 changed files with 17 additions and 7 deletions

View file

@ -16,11 +16,12 @@ pub struct ArticleResource {
state: State, state: State,
article_id: i32, article_id: i32,
revision: i32, revision: i32,
edit: bool,
} }
impl ArticleResource { impl ArticleResource {
pub fn new(state: State, article_id: i32, revision: i32) -> Self { pub fn new(state: State, article_id: i32, revision: i32, edit: bool) -> Self {
Self { state, article_id, revision } Self { state, article_id, revision, edit }
} }
} }
@ -47,6 +48,7 @@ impl Resource for ArticleResource {
revision: i32, revision: i32,
created: &'a chrono::DateTime<Local>, created: &'a chrono::DateTime<Local>,
edit: bool,
cancel_url: Option<&'a str>, cancel_url: Option<&'a str>,
title: &'a str, title: &'a str,
raw: &'a str, raw: &'a str,
@ -68,6 +70,7 @@ impl Resource for ArticleResource {
article_id: data.article_id, article_id: data.article_id,
revision: data.revision, revision: data.revision,
created: &Local.from_utc_datetime(&data.created), created: &Local.from_utc_datetime(&data.created),
edit: self.edit,
cancel_url: Some(&data.slug), cancel_url: Some(&data.slug),
title: &data.title, title: &data.title,
raw: &data.body, raw: &data.body,

View file

@ -56,6 +56,7 @@ impl Resource for NewArticleResource {
revision: &'a str, revision: &'a str,
created: &'a str, created: &'a str,
edit: bool,
cancel_url: Option<&'a str>, cancel_url: Option<&'a str>,
title: &'a str, title: &'a str,
raw: &'a str, raw: &'a str,
@ -76,6 +77,11 @@ impl Resource for NewArticleResource {
article_id: NDASH, article_id: NDASH,
revision: NDASH, revision: NDASH,
created: NDASH, created: NDASH,
// Implicitly start in edit-mode when no slug is given. This
// currently directly corresponds to the /_new endpoint
edit: self.slug.is_none(),
cancel_url: self.slug.as_ref().map(|x| &**x), cancel_url: self.slug.as_ref().map(|x| &**x),
title: &title, title: &title,
raw: "", raw: "",

View file

@ -59,7 +59,7 @@ impl Lookup for WikiLookup {
type Error = Box<::std::error::Error + Send + Sync>; type Error = Box<::std::error::Error + Send + Sync>;
type Future = Box<Future<Item = Option<Self::Resource>, Error = Self::Error>>; type Future = Box<Future<Item = Option<Self::Resource>, Error = Self::Error>>;
fn lookup(&self, path: &str, _query: Option<&str>, _fragment: Option<&str>) -> Self::Future { fn lookup(&self, path: &str, query: Option<&str>, _fragment: Option<&str>) -> Self::Future {
assert!(path.starts_with("/")); assert!(path.starts_with("/"));
if path.starts_with("/_") { if path.starts_with("/_") {
@ -92,14 +92,15 @@ impl Lookup for WikiLookup {
} }
let state = self.state.clone(); let state = self.state.clone();
let edit = query == Some("edit");
use state::SlugLookup; use state::SlugLookup;
Box::new(self.state.lookup_slug(slug.clone()) Box::new(self.state.lookup_slug(slug.clone())
.and_then(|x| Ok(Some(match x { .and_then(move |x| Ok(Some(match x {
SlugLookup::Miss => SlugLookup::Miss =>
Box::new(NewArticleResource::new(state, Some(slug))) as BoxResource, Box::new(NewArticleResource::new(state, Some(slug))) as BoxResource,
SlugLookup::Hit { article_id, revision } => SlugLookup::Hit { article_id, revision } =>
Box::new(ArticleResource::new(state, article_id, revision)) as BoxResource, Box::new(ArticleResource::new(state, article_id, revision, edit)) as BoxResource,
SlugLookup::Redirect(slug) => SlugLookup::Redirect(slug) =>
Box::new(ArticleRedirectResource::new(slug)) as BoxResource, Box::new(ArticleRedirectResource::new(slug)) as BoxResource,
}))) })))

View file

@ -1,6 +1,6 @@
<script src="_assets/script-{{script_js_checksum}}.js" defer></script> <script src="_assets/script-{{script_js_checksum}}.js" defer></script>
<div class="container {{^cancel_url}}edit{{/cancel_url}}"> <div class="container {{#edit?}}edit{{/edit}}">
<div class="rendered"> <div class="rendered">
{{>article_revision_contents.html}} {{>article_revision_contents.html}}
</div> </div>
@ -32,7 +32,7 @@
<footer> <footer>
<ul> <ul>
<li><a href="_new">Create article</a></li> <li><a href="_new">Create article</a></li>
<li><a id="openEditor" href="?editor">Edit</a></li> <li><a id="openEditor" href="?edit">Edit</a></li>
</ul> </ul>
<dl> <dl>
<dt>Article ID</dt> <dt>Article ID</dt>