aboutsummaryrefslogtreecommitdiff
path: root/src/forms.rs
blob: 2fd2844c2cb1772b610116341640a897b46d8629 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use hyper::http::request::Parts;
use serde::Deserialize;
use sputnik::html_escape;

use crate::{action_links, controller::Controller, get_renderer, Context, Error, Page, Response};

const FILE_EXT_WITH_SPELLCHECK: &[&[u8]] = &[b"md", b"txt"];

fn render_error(message: &str) -> String {
    format!("<div class=error>error: {}</div>", html_escape(message))
}

#[derive(Deserialize, Default)]
pub struct EditForm {
    pub text: String,
    pub msg: Option<String>,
    pub oid: Option<String>,
}

pub fn edit_text_form<'a, C: Controller>(
    data: &EditForm,
    error: Option<&str>,
    controller: &'a C,
    ctx: &'a Context,
    parts: &Parts,
) -> Page {
    let mut page = Page {
        title: format!(
            "{} {}",
            if data.oid.is_some() {
                "Editing"
            } else {
                "Creating"
            },
            ctx.path.file_name().unwrap()
        ),
        header: data
            .oid
            .is_some()
            .then(|| action_links("edit", controller, ctx, parts))
            .unwrap_or_default(),
        scripts: vec![include_str!("static/edit_script.js")],
        script_src: vec![include_str!("static/edit_script.js.sha")],
        ..Default::default()
    };
    if let Some(hint_html) = controller.edit_hint_html(ctx) {
        page.body
            .push_str(&format!("<div class=edit-hint>{}</div>", hint_html));
    }
    if let Some(error) = error {
        page.body.push_str(&render_error(error));
    }
    page.body.push_str(&format!(
        "<form method=post action='?action=edit' class=edit-form>\
        <textarea name=text autofocus autocomplete=off spellcheck={}>{}</textarea>",
        FILE_EXT_WITH_SPELLCHECK.contains(
            &ctx.path
                .extension()
                .map(|e| e.as_bytes())
                .unwrap_or_default()
        ),
        html_escape(&data.text)
    ));
    page.body
        .push_str("<div class=buttons><button>Save</button>");
    if let Some(oid) = &data.oid {
        page.body.push_str(&format!(
            "<input name=oid type=hidden value='{}'>
            <button formaction='?action=diff'>Diff</button>",
            oid
        ));
    }
    if get_renderer(&ctx.path).is_some() {
        page.body
            .push_str(" <button formaction='?action=preview'>Preview</button>")
    }
    page.body.push_str(&format!(
        "<input name=msg placeholder=Message value='{}' autocomplete=off></div></form>",
        html_escape(data.msg.as_deref().unwrap_or_default())
    ));
    page
}

#[derive(Deserialize)]
pub struct MoveForm {
    pub dest: String,
    pub msg: Option<String>,
}

pub fn move_form<C: Controller>(
    filename: &str,
    data: &MoveForm,
    error: Option<&str>,
    controller: &C,
    ctx: &Context,
    parts: &Parts,
) -> Result<Response, Error> {
    let mut page = Page {
        title: format!("Move {}", filename),
        header: action_links("move", controller, ctx, parts),
        ..Default::default()
    };

    if let Some(error) = error {
        page.body.push_str(&render_error(error));
    }

    page.body.push_str(&format!(
        "<form method=post autocomplete=off>
        <label>Destination <input name=dest value='{}' autofocus></label>
        <label>Message <input name=msg value='{}'></label>
        <button>Move</button>
        </form>",
        html_escape(&data.dest),
        data.msg.as_ref().map(html_escape).unwrap_or_default(),
    ));

    Ok(page.into())
}

pub fn upload_form<'a, C: Controller>(
    file_exists: bool,
    controller: &'a C,
    ctx: &'a Context,
    parts: &Parts,
) -> Page {
    let filename = ctx.path.file_name().unwrap();
    Page {
        title: format!("Uploading {}", filename),
        // TODO: add input for commit message
        body: "<form action=?action=upload method=post enctype='multipart/form-data'>\
            <input name=file type=file>\
            <button>Upload</button>\
        </form>"
            .into(),
        header: file_exists
            .then(|| action_links("edit", controller, ctx, parts))
            .unwrap_or_default(),
        ..Default::default()
    }
}