Add Copy action to code blocks in markdown example

This commit is contained in:
Héctor Ramón Jiménez 2025-02-04 20:58:06 +01:00
parent 387abafa3a
commit e8020f3eaf
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
9 changed files with 93 additions and 12 deletions

View file

@ -16,3 +16,8 @@ image.workspace = true
tokio.workspace = true
open = "5.3"
# Disabled to keep amount of build dependencies low
# This can be re-enabled on demand
# [build-dependencies]
# iced_fontello = "0.13"

View file

@ -0,0 +1,5 @@
pub fn main() {
// println!("cargo::rerun-if-changed=fonts/markdown-icons.toml");
// iced_fontello::build("fonts/markdown-icons.toml")
// .expect("Build icons font");
}

View file

@ -0,0 +1,4 @@
module = "icon"
[glyphs]
copy = "fontawesome-docs"

Binary file not shown.

View file

@ -0,0 +1,15 @@
// Generated automatically by iced_fontello at build time.
// Do not edit manually. Source: ../fonts/markdown-icons.toml
// dcd2f0c969d603e2ee9237a4b70fa86b1a6e84d86f4689046d8fdd10440b06b9
use iced::widget::{text, Text};
use iced::Font;
pub const FONT: &[u8] = include_bytes!("../fonts/markdown-icons.ttf");
pub fn copy<'a>() -> Text<'a> {
icon("\u{F0C5}")
}
fn icon(codepoint: &str) -> Text<'_> {
text(codepoint).font(Font::with_name("markdown-icons"))
}

View file

@ -1,10 +1,13 @@
mod icon;
use iced::animation;
use iced::clipboard;
use iced::highlighter;
use iced::task;
use iced::time::{self, milliseconds, Instant};
use iced::widget::{
self, center_x, horizontal_space, hover, image, markdown, pop, right, row,
scrollable, text_editor, toggler,
self, button, center_x, horizontal_space, hover, image, markdown, pop,
right, row, scrollable, text_editor, toggler,
};
use iced::window;
use iced::{Animation, Element, Fill, Font, Subscription, Task, Theme};
@ -15,6 +18,7 @@ use std::sync::Arc;
pub fn main() -> iced::Result {
iced::application("Markdown - Iced", Markdown::update, Markdown::view)
.font(icon::FONT)
.subscription(Markdown::subscription)
.theme(Markdown::theme)
.run_with(Markdown::new)
@ -49,6 +53,7 @@ enum Image {
#[derive(Debug, Clone)]
enum Message {
Edit(text_editor::Action),
Copy(String),
LinkClicked(markdown::Url),
ImageShown(markdown::Url),
ImageDownloaded(markdown::Url, Result<image::Handle, Error>),
@ -91,6 +96,7 @@ impl Markdown {
Task::none()
}
Message::Copy(content) => clipboard::write(content),
Message::LinkClicked(link) => {
let _ = open::that_in_background(link.to_string());
@ -141,6 +147,8 @@ impl Markdown {
}
Message::ToggleStream(enable_stream) => {
if enable_stream {
self.content = markdown::Content::new();
self.mode = Mode::Stream {
pending: self.raw.text(),
};
@ -282,6 +290,26 @@ impl<'a> markdown::Viewer<'a, Message> for CustomViewer<'a> {
.into()
}
}
fn code_block(
&self,
settings: markdown::Settings,
code: &'a str,
lines: &'a [markdown::Text],
) -> Element<'a, Message> {
let code_block =
markdown::code_block(settings, code, lines, Message::LinkClicked);
hover(
code_block,
right(
button(icon::copy().size(12))
.padding(settings.spacing / 2)
.on_press_with(|| Message::Copy(code.to_owned()))
.style(button::text),
),
)
}
}
async fn download_image(url: markdown::Url) -> Result<image::Handle, Error> {