From 6107291a672ffc912d2cfae9ffa33dcfc7b50399 Mon Sep 17 00:00:00 2001 From: Polesznyák Márk Date: Mon, 20 Oct 2025 13:35:38 +0200 Subject: feat: add `markdown` example --- examples/markdown/src/main.rs | 74 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 examples/markdown/src/main.rs (limited to 'examples/markdown/src') diff --git a/examples/markdown/src/main.rs b/examples/markdown/src/main.rs new file mode 100644 index 0000000..fae13ca --- /dev/null +++ b/examples/markdown/src/main.rs @@ -0,0 +1,74 @@ +use iced::widget::{operation, row, scrollable, text_editor}; +use iced::{Element, Fill, Font, Theme, highlighter}; +use iced_selection::markdown; + +fn main() -> iced::Result { + iced::application(State::new, State::update, State::view) + .theme(Theme::CatppuccinMocha) + .run() +} + +#[derive(Default)] +struct State { + content: markdown::Content, + raw: text_editor::Content, +} + +#[derive(Debug, Clone)] +enum Message { + Edit(text_editor::Action), + LinkClicked(markdown::Url), +} + +impl State { + fn new() -> (Self, iced::Task) { + const INITIAL_CONTENT: &str = include_str!("../overview.md"); + + ( + Self { + content: markdown::Content::parse(INITIAL_CONTENT), + raw: text_editor::Content::with_text(INITIAL_CONTENT), + }, + operation::focus_next(), + ) + } + + fn update(&mut self, message: Message) { + match message { + Message::Edit(action) => { + let is_edit = action.is_edit(); + + self.raw.perform(action); + + if is_edit { + self.content = markdown::Content::parse(&self.raw.text()); + } + } + Message::LinkClicked(link) => { + let _ = open::that_in_background(link.to_string()); + } + } + } + + fn view(&self) -> Element<'_, Message> { + let editor = text_editor(&self.raw) + .placeholder("Type your Markdown here...") + .on_action(Message::Edit) + .height(Fill) + .padding(10) + .font(Font::MONOSPACE) + .highlight("markdown", highlighter::Theme::Base16Ocean); + + let preview = + markdown::view(self.content.items(), &Theme::CatppuccinMocha) + .map(Message::LinkClicked); + + row![ + editor, + scrollable(preview).spacing(10).width(Fill).height(Fill) + ] + .spacing(10) + .padding(10) + .into() + } +} -- cgit v1.2.3