-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refactor the demo to make it a little nicer to use
- Loading branch information
Showing
5 changed files
with
133 additions
and
110 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
demo/src/commonmark_demo/message.gleam → demo/src/commonmark/demo/message.gleam
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import commonmark/demo/message.{type Msg} | ||
import commonmark/demo/model.{type Model, Model} | ||
import gleam/dynamic | ||
import gleam/result | ||
import lustre/attribute | ||
import lustre/element | ||
import lustre/element/html | ||
import lustre/event | ||
import lustre/ui | ||
import lustre/ui/button | ||
import lustre/ui/layout/aside | ||
import pprint | ||
|
||
fn on_input(event: dynamic.Dynamic) -> Result(Msg, dynamic.DecodeErrors) { | ||
use target <- result.try(dynamic.field("target", dynamic.dynamic)(event)) | ||
use value <- result.try(dynamic.field("value", dynamic.string)(target)) | ||
// do your stuff! | ||
Ok(message.UpdateInput(value)) | ||
} | ||
|
||
fn tab_button( | ||
model: Model, | ||
tab: model.Tab, | ||
label: String, | ||
) -> element.Element(Msg) { | ||
ui.button( | ||
[ | ||
event.on_click(message.SetTab(tab)), | ||
attribute.style([#("margin-right", "1em")]), | ||
case model.tab == tab { | ||
True -> button.primary() | ||
False -> button.outline() | ||
}, | ||
], | ||
[element.text(label)], | ||
) | ||
} | ||
|
||
fn edit_area(model: Model) -> element.Element(Msg) { | ||
html.textarea( | ||
[ | ||
event.on("input", on_input), | ||
attribute.style([ | ||
#("padding", "0.25em"), | ||
#("max-width", "50%"), | ||
#("height", "100vh"), | ||
#("background", "#eeeeee"), | ||
#("border-right", "2px solid var(--element-border-strong)"), | ||
#("flex-grow", "1"), | ||
#("font-family", "var(--font-mono)"), | ||
#("resize", "none"), | ||
]), | ||
], | ||
model.input, | ||
) | ||
} | ||
|
||
pub fn view(model: Model) -> element.Element(Msg) { | ||
ui.centre( | ||
[], | ||
ui.aside( | ||
[ | ||
aside.content_first(), | ||
aside.align_centre(), | ||
attribute.style([ | ||
#("width", "100vw"), | ||
#("--gap", "0"), | ||
#("--min", "40%"), | ||
]), | ||
], | ||
edit_area(model), | ||
html.div( | ||
[ | ||
attribute.style([ | ||
#("max-width", "50%"), | ||
#("height", "100vh"), | ||
#("overflow-y", "scroll"), | ||
]), | ||
], | ||
[ | ||
html.div([], [ | ||
html.div( | ||
[ | ||
attribute.style([ | ||
#("padding", "0.75em 1em"), | ||
#("width", "100%"), | ||
#("position", "sticky"), | ||
#("background", "var(--primary-app-background-subtle)"), | ||
#("border-bottom", "2px solid var(--element-border-strong)"), | ||
#("top", "0"), | ||
]), | ||
], | ||
[ | ||
tab_button(model, model.Preview, "Preview"), | ||
tab_button(model, model.AST, "AST"), | ||
], | ||
), | ||
html.div([attribute.style([#("padding", "1em")])], [ | ||
case model.tab { | ||
model.AST -> | ||
html.div([], [ | ||
html.pre([attribute.style([#("white-space", "pre-wrap")])], [ | ||
element.text(pprint.format(model.document)), | ||
]), | ||
]) | ||
model.Preview -> | ||
ui.prose( | ||
[ | ||
attribute.attribute( | ||
"dangerous-unescaped-html", | ||
model.html, | ||
), | ||
], | ||
[], | ||
) | ||
}, | ||
]), | ||
]), | ||
], | ||
), | ||
), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.