Add Copy action to code blocks in markdown example
This commit is contained in:
parent
387abafa3a
commit
e8020f3eaf
9 changed files with 93 additions and 12 deletions
|
|
@ -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"
|
||||
|
|
|
|||
5
examples/markdown/build.rs
Normal file
5
examples/markdown/build.rs
Normal 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");
|
||||
}
|
||||
4
examples/markdown/fonts/markdown-icons.toml
Normal file
4
examples/markdown/fonts/markdown-icons.toml
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
module = "icon"
|
||||
|
||||
[glyphs]
|
||||
copy = "fontawesome-docs"
|
||||
BIN
examples/markdown/fonts/markdown-icons.ttf
Normal file
BIN
examples/markdown/fonts/markdown-icons.ttf
Normal file
Binary file not shown.
15
examples/markdown/src/icon.rs
Normal file
15
examples/markdown/src/icon.rs
Normal 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"))
|
||||
}
|
||||
|
|
@ -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> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue