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

Svg viewer #544

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions web/package-lock.json

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

3 changes: 2 additions & 1 deletion web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@vicons/ionicons5": "^0.12.0",
"@vscode/markdown-it-katex": "^1.0.3",
"@vueuse/core": "^9.13.0",
"dompurify": "^3.1.6",
"dot-env": "^0.0.1",
"highlight.js": "^11.7.0",
"html2canvas": "^1.4.1",
Expand Down Expand Up @@ -74,4 +75,4 @@
"pnpm lint:fix"
]
}
}
}
5 changes: 5 additions & 0 deletions web/src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ const routes: RouteRecordRaw[] = [
name: 'Prompt',
component: () => import('@/views/prompt/creator.vue')
},
{
path: '/prompt/svg',
name: 'Prompt',
component: () => import('@/views/prompt/svgViewer.vue')
},
{
path: '/bot',
name: 'Bot',
Expand Down
67 changes: 67 additions & 0 deletions web/src/views/prompt/svgViewer.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<template>
<div class="flex flex-row w-full">
<div class="flex-1 m-5">
<div class="flex justify-between mb-4">
<n-button type="primary" class="rounded-lg">Raw</n-button>
<n-button @click="clearSvgContent" type="error" class="rounded-lg">Clear</n-button>
</div>
<n-input v-model:value="svgContent" type="textarea" :autosize="{ minRows: 20, maxRows: 60 }"
placeholder="Paste your SVG code here" class="w-full border rounded-lg shadow-md" />
</div>
<div class="flex-1 m-5">
<div class="flex justify-between mb-4">
<n-button @click="saveToPng" type="success" class="rounded-lg">Save</n-button>
</div>
<div v-html="sanitizedSvgContent" class="w-full h-full">
</div>
</div>
</div>
</template>

<script setup>
import { ref, computed, watch } from 'vue'
import { NInput, NDivider, NButton } from 'naive-ui'
import DOMPurify from 'dompurify'

const svgContent = ref(localStorage.getItem('svgContent') || '')

watch(svgContent, (newValue) => {
localStorage.setItem('svgContent', newValue);
});

const sanitizedSvgContent = computed(() => {
return DOMPurify.sanitize(svgContent.value)
})

const saveToPng = () => {
const svgElement = new Blob([sanitizedSvgContent.value], { type: 'image/svg+xml;charset=utf-8' });
const url = URL.createObjectURL(svgElement);
const img = new Image();
img.onload = () => {
const canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0);
const pngUrl = canvas.toDataURL('image/png');
const a = document.createElement('a');
a.href = pngUrl;
a.download = 'image.png';
a.click();
URL.revokeObjectURL(url);
};
img.src = url;
}

const clearSvgContent = () => {
svgContent.value = '';
}

</script>

<style scoped>
.svg-container {
overflow: auto;
/* Light background for contrast */
}
</style>
Loading