1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
use std::time::Duration;
use wasm_bindgen::prelude::*;
use yew::prelude::*;
use yew::services::{Task, TimeoutService};

use crate::button::*;
use crate::form::*;
use crate::icon::*;
use crate::*;
use yew::web_sys::{Element, HtmlInputElement};

#[derive(Clone, PartialEq, Properties)]
pub struct Props {
    #[prop_or_default]
    pub value: String,
    #[prop_or_default]
    pub readonly: bool,
    #[prop_or_default]
    pub code: bool,
    #[prop_or_default]
    pub variant: ClipboardVariant,
}

#[derive(Clone, PartialEq, Eq, Debug)]
pub enum ClipboardVariant {
    // default
    Inline,
    // expandable
    Expandable,
    // expandable and initially expanded
    Expanded,
}

impl ClipboardVariant {
    pub fn is_expandable(&self) -> bool {
        match self {
            Self::Expandable | Self::Expanded => true,
            _ => false,
        }
    }
}

impl Default for ClipboardVariant {
    fn default() -> Self {
        Self::Inline
    }
}

#[derive(Clone, Debug)]
pub enum Msg {
    Copy,
    Copied,
    Reset,
    ToggleExpand,
}

const DEFAULT_MESSAGE: &'static str = "Copy to clipboard";

pub struct Clipboard {
    props: Props,
    link: ComponentLink<Self>,
    message: &'static str,
    task: Option<Box<dyn Task>>,
    expanded: bool,
    value: String,
    text_ref: NodeRef,
    details_ref: NodeRef,
}

impl Component for Clipboard {
    type Message = Msg;
    type Properties = Props;

    fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
        let expanded = match props.variant {
            ClipboardVariant::Expanded => true,
            _ => false,
        };

        let value = props.value.clone();

        Self {
            props,
            link,
            message: DEFAULT_MESSAGE,
            task: None,
            expanded,
            value,
            text_ref: NodeRef::default(),
            details_ref: NodeRef::default(),
        }
    }

    fn update(&mut self, msg: Self::Message) -> ShouldRender {
        match msg {
            Msg::Copy => {
                self.sync_value();
                self.do_copy();
            }
            Msg::Copied => {
                // log::info!("Copied");
                self.message = "Copied!";
                self.task = Some(Box::new(TimeoutService::spawn(
                    Duration::from_secs(2),
                    self.link.callback(|_| Msg::Reset),
                )));
            }
            Msg::Reset => {
                self.message = DEFAULT_MESSAGE;
                self.task.take();
            }
            Msg::ToggleExpand => {
                self.sync_value();
                self.expanded = !self.expanded;
            }
        }
        true
    }

    fn change(&mut self, props: Self::Properties) -> ShouldRender {
        if self.props != props {
            self.value = props.value.clone();
            self.props = props;
            true
        } else {
            false
        }
    }

    fn view(&self) -> Html {
        let mut classes = Classes::from("pf-c-clipboard-copy");

        if self.expanded {
            classes.push("pf-m-expanded");
        }

        return html! {
            <div class=classes>
                <div class="pf-c-clipboard-copy__group">
                    { self.expander() }
                    <TextInput ref=self.text_ref.clone() readonly={self.props.readonly | self.expanded} value=&self.value/>
                    <Tooltip text=self.message>
                        <Button variant=Variant::Control icon=Icon::Copy onclick=self.link.callback(|_|Msg::Copy)/>
                    </Tooltip>
                </div>
                { self.expanded() }
            </div>
        };
    }
}

impl Clipboard {
    fn do_copy(&self) {
        let s = self.value.clone();

        let cb: Callback<()> = self.link.callback(|_| Msg::Copied);

        wasm_bindgen_futures::spawn_local(async move {
            match copy_to_clipboard(s).await {
                Ok(_) => cb.emit(()),
                Err(_) => {}
            };
        });
    }

    fn expander(&self) -> Html {
        if !self.props.variant.is_expandable() {
            return Default::default();
        }

        let onclick = self.link.callback(|_| Msg::ToggleExpand);

        return html! {
            <Button
                expanded=self.expanded
                variant=Variant::Control
                onclick=onclick>
                <div class="pf-c-clipboard-copy__toggle-icon">
                    { Icon::AngleRight }
                </div>
            </Button>
        };
    }

    fn expanded(&self) -> Html {
        if !self.expanded {
            return Default::default();
        }

        return html! {
            <div
                ref=self.details_ref.clone()
                class="pf-c-clipboard-copy__expandable-content"
                contenteditable=!self.props.readonly>

                { if self.props.code {
                    html!{ <pre>{&self.value}</pre> }
                } else {
                    html!{ &self.value}
                } }

            </div>
        };
    }

    /// Sync the value between internal, text field or details.
    fn sync_value(&mut self) {
        if self.props.readonly {
            return;
        }

        let value = if !self.expanded {
            let ele: Option<HtmlInputElement> = self.text_ref.cast::<HtmlInputElement>();
            ele.map(|ele| ele.value()).unwrap_or_else(|| "".into())
        } else {
            let ele: Option<Element> = self.details_ref.cast::<Element>();
            ele.and_then(|ele| ele.text_content())
                .unwrap_or_else(|| "".into())
        };

        // log::info!("New value: {}", value);

        self.value = value;
    }
}

#[wasm_bindgen(inline_js="export function copy_to_clipboard(value) {return window.navigator.clipboard.writeText(value);}")]
#[rustfmt::skip] // required to keep the "async" keyword
extern "C" { 
    #[wasm_bindgen(catch)]
    async fn copy_to_clipboard(value: String) -> Result<(), JsValue>;
}