use autumn_web::Markup;
use super::{Behavior, button, marker};
use crate::markup::{Attributes, common};
#[derive(Debug, Clone, PartialEq, Eq)]
#[must_use]
pub struct CopyText {
source: Source,
messages: Option<(String, String)>,
extra: Attributes,
}
#[derive(Debug, Clone, PartialEq, Eq)]
enum Source {
Selector(String),
Text(String),
}
impl CopyText {
pub fn selector(selector: impl Into<String>) -> Self {
Self::with(Source::Selector(selector.into()))
}
pub fn text(text: impl Into<String>) -> Self {
Self::with(Source::Text(text.into()))
}
const fn with(source: Source) -> Self {
Self {
source,
messages: None,
extra: Vec::new(),
}
}
pub fn messages(mut self, done: impl Into<String>, failed: impl Into<String>) -> Self {
self.messages = Some((done.into(), failed.into()));
self
}
#[must_use]
pub fn attributes(&self) -> Attributes {
let option = match &self.source {
Source::Selector(selector) => ("data-vanilla-copy", selector.clone()),
Source::Text(text) => ("data-vanilla-copy-text", text.clone()),
};
let mut attributes = vec![marker(Behavior::Copy), option];
if let Some((done, failed)) = &self.messages {
attributes.push(("data-vanilla-copy-done", done.clone()));
attributes.push(("data-vanilla-copy-failed", failed.clone()));
}
attributes.extend(self.extra.iter().cloned());
attributes
}
#[must_use]
pub fn button(&self, content: Markup) -> Markup {
button(self.attributes(), content)
}
}
common!(CopyText, wrap);