Skip to content

Commit 8dd95ba

Browse files
committed
added clickhandler to copy badge snippet button; refs #29
1 parent 72d2aae commit 8dd95ba

File tree

1 file changed

+83
-7
lines changed

1 file changed

+83
-7
lines changed

pages/software/v0.1/BadgeSnippets.vue

Lines changed: 83 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,16 @@
1313
v-bind:value="markdownBadge"
1414
wrap="off"
1515
></textarea>
16-
<button role="button" onclick="console.log('not implemented yet')">Copy</button>
16+
<div class="col">
17+
<button
18+
role="button"
19+
v-on:click="copyButtonClickHandler">
20+
Copy
21+
</button>
22+
<div class="copied-text">
23+
copied
24+
</div>
25+
</div>
1726
</div>
1827

1928
<h3>
@@ -27,7 +36,16 @@
2736
v-bind:value="rstBadge"
2837
wrap="off"
2938
></textarea>
30-
<button role="button" onclick="console.log('not implemented yet')">Copy</button>
39+
<div class="col">
40+
<button
41+
role="button"
42+
v-on:click="copyButtonClickHandler">
43+
Copy
44+
</button>
45+
<div class="copied-text">
46+
copied
47+
</div>
48+
</div>
3149
</div>
3250

3351
<h3>
@@ -41,7 +59,16 @@
4159
v-bind:value="htmlBadge"
4260
wrap="off"
4361
></textarea>
44-
<button role="button" onclick="console.log('not implemented yet')">Copy</button>
62+
<div class="col">
63+
<button
64+
role="button"
65+
v-on:click="copyButtonClickHandler">
66+
Copy
67+
</button>
68+
<div class="copied-text">
69+
copied
70+
</div>
71+
</div>
4572
</div>
4673

4774
</div>
@@ -59,12 +86,38 @@ const appBaseUrl = ref('');
5986
6087
const htmlBadge = computed(() => `<a href="${href.value}?${fairQueryParams.value}">\n`
6188
+ ` <img src="${appBaseUrl.value}badge.svg" `
62-
+ `alt="${alt}">\n</a>`);
89+
+ `alt="${alt}">\n</a>\n`);
6390
const markdownBadge = computed(() => `[![${alt}](${appBaseUrl.value}badge.svg)]`
64-
+ `(${href.value}?${fairQueryParams.value})`);
91+
+ `(${href.value}?${fairQueryParams.value})\n`);
6592
const rstBadge = computed(() => `.. image:: ${appBaseUrl.value}badge.svg\n`
6693
+ ` :target: ${href.value}?${fairQueryParams.value}\n`
67-
+ ` :alt: ${alt}`);
94+
+ ` :alt: ${alt}\n`);
95+
96+
const copyButtonClickHandler = (event: Event) => {
97+
if (event.target === null) {
98+
return;
99+
}
100+
const button = event.target as HTMLInputElement;
101+
if (button.parentElement === null) {
102+
return;
103+
}
104+
if (button.parentElement.previousElementSibling === null) {
105+
return;
106+
}
107+
const textArea = button.parentElement.previousElementSibling as HTMLInputElement;
108+
const text = textArea.value;
109+
navigator.clipboard.writeText(text);
110+
if (button.nextElementSibling === null) {
111+
return;
112+
}
113+
const feedbackString = button.nextElementSibling as HTMLElement;
114+
feedbackString.classList.remove('transparent');
115+
feedbackString.classList.add('opaque');
116+
setTimeout(() => {
117+
feedbackString.classList.remove('opaque');
118+
feedbackString.classList.add('transparent');
119+
}, 1000);
120+
};
68121
69122
onMounted(() => {
70123
href.value = `${window.location.origin}/${window.location.pathname.split('/').filter(e => e !== '').join('/')}`;
@@ -96,7 +149,15 @@ textarea {
96149
.row {
97150
display: flex;
98151
flex-direction: row;
99-
flex-wrap: nowrap
152+
flex-wrap: nowrap;
153+
}
154+
155+
.col {
156+
align-items: center;
157+
display: flex;
158+
flex-direction: column;
159+
flex-wrap: nowrap;
160+
justify-content: center;
100161
}
101162
102163
button {
@@ -122,4 +183,19 @@ button:disabled {
122183
opacity: 0.5;
123184
}
124185
186+
.copied-text {
187+
font-size: 80%;
188+
margin-left: 2em;
189+
margin-right: 1em;
190+
opacity: 0;
191+
}
192+
193+
.copied-text.opaque {
194+
opacity: 1;
195+
transition: opacity 0s;
196+
}
197+
.copied-text.transparent {
198+
opacity: 0;
199+
transition: opacity 2s;
200+
}
125201
</style>

0 commit comments

Comments
 (0)