use crate::pcontent::{
PLabelId, pabstractcontent::{PAbstractContent, PAbstractLectureBackend}, preferenceurl::PReferenceUrl
};
#[derive(Debug, Clone, Default, PartialEq)]
pub struct PContentClipboardText{
p_display_text: String,
p_copy_text: String,
p_style: String,
p_button_text: String,
}
impl PContentClipboardText {
pub fn new(display_text: &String, copy_text: &String, style: &String, button_text: &String) -> Self{
PContentClipboardText {
p_display_text: display_text.clone(),
p_copy_text: copy_text.clone(),
p_style: style.clone(),
p_button_text: button_text.clone(),
}
}
}
impl PAbstractContent for PContentClipboardText{
fn has_embeded_label(&self) -> bool{
false
}
fn get_reference_url(&self, current_file: &String, id: usize) -> PReferenceUrl{
PReferenceUrl::from_text(current_file, id, &String::from(&format!("PContentClipboardText {}", id)))
}
fn to_html<TLectureBackend>(&self, backend: &mut TLectureBackend, id: &PLabelId)
where TLectureBackend: PAbstractLectureBackend
{
backend.write(&String::from(format!("<span id=\"{}\">", id.get_id())));
backend.write(&self.p_display_text);
if !self.p_display_text.is_empty(){
backend.write(&String::from(" "));
}
backend.write(&String::from("<button type=\"button\" "));
if !self.p_style.is_empty() {
backend.write(&String::from(format!("class=\"{}\" ", self.p_style)));
}
backend.write(&String::from(format!("onclick=\"phoenix_writeClipboardText('{}')\">{}</button>", self.p_copy_text.replace("'", "\\'"), self.p_button_text)));
backend.write(&String::from("</span>"));
}
}
#[cfg(test)]
mod tests{
use super::*;
use crate::pcontent::pstrbackend::PStrBackend;
#[test]
fn test_content_clipboard_text(){
let text: PContentClipboardText = PContentClipboardText::new(&String::from("Display text"), &String::from("Copy text"), &String::from("Style"), &String::from("Button Text"));
let mut backend = PStrBackend::new();
text.to_html(&mut backend, &PLabelId::new(42));
assert_eq!(backend.get_body(), &String::from("<span id=\"42\">Display text <button type=\"button\" class=\"Style\" onclick=\"phoenix_writeClipboardText('Copy text')\">Button Text</button></span>"));
}
#[test]
fn test_content_clipboard_text_no_style(){
let text: PContentClipboardText = PContentClipboardText::new(&String::from(""), &String::from("Copy text"), &String::from(""), &String::from("Button Text"));
let mut backend = PStrBackend::new();
text.to_html(&mut backend, &PLabelId::new(42));
assert_eq!(backend.get_body(), &String::from("<span id=\"42\"><button type=\"button\" onclick=\"phoenix_writeClipboardText('Copy text')\">Button Text</button></span>"));
}
}