2023-06-04 16:59:38 +02:00
|
|
|
<script setup lang="ts">
|
|
|
|
|
import { ref } from 'vue';
|
2025-12-12 09:25:39 +01:00
|
|
|
import ButtonMenu from '../base/ButtonMenu.vue';
|
2023-06-04 16:59:38 +02:00
|
|
|
import { useIconStyleContext } from '../../composables/useIconStyle';
|
|
|
|
|
import useConfetti from '../../composables/useConfetti';
|
2023-06-28 21:04:37 +02:00
|
|
|
import getSVGIcon from '../../utils/getSVGIcon';
|
|
|
|
|
import downloadData from '../../utils/downloadData';
|
2023-06-04 16:59:38 +02:00
|
|
|
|
2025-12-12 09:25:39 +01:00
|
|
|
const downloadText = 'Download!';
|
|
|
|
|
const copiedText = 'Copied!';
|
|
|
|
|
const confettiText = ref(copiedText);
|
2023-06-04 16:59:38 +02:00
|
|
|
const props = defineProps<{
|
2025-12-12 09:25:39 +01:00
|
|
|
name: string;
|
|
|
|
|
popoverPosition?: 'top' | 'bottom';
|
|
|
|
|
}>();
|
2023-06-04 16:59:38 +02:00
|
|
|
|
2025-12-12 09:25:39 +01:00
|
|
|
const { size } = useIconStyleContext();
|
2023-06-04 16:59:38 +02:00
|
|
|
|
2025-12-12 09:25:39 +01:00
|
|
|
const { animate, confetti } = useConfetti();
|
2023-06-04 16:59:38 +02:00
|
|
|
|
|
|
|
|
function copySVG() {
|
2025-12-12 09:25:39 +01:00
|
|
|
confettiText.value = copiedText;
|
|
|
|
|
const svgString = getSVGIcon();
|
2023-06-04 16:59:38 +02:00
|
|
|
|
2025-12-12 09:25:39 +01:00
|
|
|
navigator.clipboard.writeText(svgString);
|
2023-06-04 16:59:38 +02:00
|
|
|
|
2025-12-12 09:25:39 +01:00
|
|
|
confetti();
|
2023-06-04 16:59:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function copyDataUrl() {
|
2025-12-12 09:25:39 +01:00
|
|
|
confettiText.value = copiedText;
|
|
|
|
|
const svgString = getSVGIcon();
|
2023-06-04 16:59:38 +02:00
|
|
|
|
|
|
|
|
// Create SVG data url
|
2025-12-12 09:25:39 +01:00
|
|
|
const dataUrl = `data:image/svg+xml;base64,${btoa(svgString)}`;
|
|
|
|
|
navigator.clipboard.writeText(dataUrl);
|
2023-06-04 16:59:38 +02:00
|
|
|
|
2025-12-12 09:25:39 +01:00
|
|
|
confetti();
|
2023-06-04 16:59:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function downloadSVG() {
|
2025-12-12 09:25:39 +01:00
|
|
|
confettiText.value = downloadText;
|
|
|
|
|
const svgString = getSVGIcon();
|
2023-06-04 16:59:38 +02:00
|
|
|
|
2025-12-12 09:25:39 +01:00
|
|
|
downloadData(`${props.name}.svg`, `data:image/svg+xml;base64,${btoa(svgString)}`);
|
|
|
|
|
confetti();
|
2023-06-04 16:59:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function downloadPNG() {
|
2025-12-12 09:25:39 +01:00
|
|
|
confettiText.value = downloadText;
|
|
|
|
|
const svgString = getSVGIcon();
|
2023-06-04 16:59:38 +02:00
|
|
|
|
|
|
|
|
const canvas = document.createElement('canvas');
|
|
|
|
|
canvas.width = size.value;
|
|
|
|
|
canvas.height = size.value;
|
2025-12-12 09:25:39 +01:00
|
|
|
const ctx = canvas.getContext('2d');
|
2023-06-04 16:59:38 +02:00
|
|
|
|
|
|
|
|
const image = new Image();
|
|
|
|
|
image.src = `data:image/svg+xml;base64,${btoa(svgString)}`;
|
2025-12-12 09:25:39 +01:00
|
|
|
image.onload = function () {
|
2023-06-04 16:59:38 +02:00
|
|
|
ctx.drawImage(image, 0, 0);
|
2025-12-12 09:25:39 +01:00
|
|
|
downloadData(`${props.name}.png`, canvas.toDataURL('image/png'));
|
|
|
|
|
confetti();
|
|
|
|
|
};
|
2023-06-04 16:59:38 +02:00
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<template>
|
|
|
|
|
<ButtonMenu
|
|
|
|
|
:buttonClass="`confetti-button ${animate ? 'animate' : ''}`"
|
|
|
|
|
callOptionOnClick
|
|
|
|
|
id="copy-svg-button"
|
|
|
|
|
:data-confetti-text="confettiText"
|
|
|
|
|
:popoverPosition="popoverPosition"
|
|
|
|
|
:options="[
|
2025-12-12 09:25:39 +01:00
|
|
|
{ text: 'Copy SVG', onClick: copySVG },
|
|
|
|
|
{ text: 'Copy Data URL', onClick: copyDataUrl },
|
|
|
|
|
{ text: 'Download SVG', onClick: downloadSVG },
|
|
|
|
|
{ text: 'Download PNG', onClick: downloadPNG },
|
2023-06-04 16:59:38 +02:00
|
|
|
]"
|
|
|
|
|
/>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<style src="./confetti.css" />
|