Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy button #30

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@ console_error_panic_hook = "0.1.7"
hex-conservative = "0.1.1"
js-sys = "0.3.69"

[dependencies.web-sys]
features = ["Clipboard", "Navigator"]

[dev-dependencies]
wasm-bindgen-test = "0.3.40"
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Deploy local website
serve:
trunk serve
RUSTFLAGS=--cfg=web_sys_unstable_apis trunk serve

# Deploy local website and open in browser
open:
Expand Down
6 changes: 6 additions & 0 deletions src/assets/js/button_effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,10 @@ function clear_circle_classes(button){
button.classList.remove("button_color");
button.classList.remove("green");
button.classList.remove("red");
}

export function copy_success(){
let button = document.querySelector("#copy-button-success")
button.classList.add("copy-show");
setTimeout(() => button.classList.remove("copy-show"), 1000)
}
22 changes: 22 additions & 0 deletions src/assets/style/components/program_input.scss
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,26 @@
background-color: #ccc !important;
cursor: default !important;
}

.program-input-field-container{
position: relative;

.copy-button{
position: absolute;
top: 10px;
right: 10px;
color: #ccc;
}
#copy-button-success{
position: absolute;
top: 10px;
right: 34px;
color: #ccc;
transition: .5s;
opacity: 0;
}
.copy-show{
opacity: 1 !important;
}
}
}
35 changes: 24 additions & 11 deletions src/components/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ use crate::util;
extern "C" {
fn button_success_animation();
fn button_fail_animation();
fn copy_success();
}

#[cfg(web_sys_unstable_apis)]
fn copy(text: &str) {
if let Some(clipboard) = window().navigator().clipboard() {
let _ = clipboard.write_text(text);
copy_success();
}
}

#[component]
Expand Down Expand Up @@ -86,18 +95,22 @@ pub fn App() -> impl IntoView {
<SelectExampleProgram update_program_str=update_program_str set_name=set_name/>
</div>

<textarea class="program-input-field"
on:keydown=move |event: web_sys::KeyboardEvent| {
if event.ctrl_key() && event.key_code() == 13 { // 13 is the Enter key
run_program();
<div class="program-input-field-container">
<span class="copy-button"><i class="far fa-copy" on:click=move |_| copy(&program_str.get())></i></span>
<span id="copy-button-success">Program copied</span>
<textarea class="program-input-field"
on:keydown=move |event: web_sys::KeyboardEvent| {
if event.ctrl_key() && event.key_code() == 13 { // 13 is the Enter key
run_program();
}
}
}
prop:value=move || program_str.get()
on:input=move |event| update_program_str(event_target_value(&event))
placeholder="Enter your program here"
rows="15" cols="80"
spellcheck="false"
/>
prop:value=move || program_str.get()
on:input=move |event| update_program_str(event_target_value(&event))
placeholder="Enter your program here"
rows="15" cols="80"
spellcheck="false"
/>
</div>

<div class="flex program-input-footer">
<ExampleProgramDescription name=name/>
Expand Down
Loading