This repository was archived by the owner on Jun 22, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCodeContainer.vue
153 lines (137 loc) · 3.99 KB
/
CodeContainer.vue
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
<script lang="ts" setup>
import { camelCase } from 'scule'
import { copyTextToClipboard } from '@/util/copy-text-to-clipboard'
defineEmits(['setlang'])
const toast = useToast()
const { editorCode } = useTool()
const { template, email, props, renderEmail } = useEmail()
function handleDownload(lang: 'html' | 'txt' | 'vue') {
const content = template.value[lang]
const file = new File([content], `${camelCase(email.value.label)}.${lang}`)
const url = URL.createObjectURL(file)
const a = document.createElement('a')
a.href = url
a.download = file.name
document.body.appendChild(a)
a.click()
toast.add({
title: 'Downloaded',
description: 'Check your downloads folder.',
icon: 'i-ph-download-simple-bold',
})
}
async function handleClipboard(lang: 'html' | 'txt' | 'vue') {
await copyTextToClipboard(template.value[lang])
toast.add({
title: 'Copied to clipboard',
description: 'You can now paste it anywhere you want.',
icon: 'i-ph-copy-bold',
})
}
const items = computed(() => {
let arr = []
if (editorCode.value.id === 'all') {
arr = [
{
key: 'vue',
label: 'Vue',
icon: 'i-ph-file-vue-duotone',
code: template.value.vue,
},
{
key: 'html',
label: 'HTML',
icon: 'i-ph-file-html-duotone',
code: template.value.html,
},
{
key: 'txt',
label: 'Plain Text',
icon: 'i-ph-text-t-duotone',
code: template.value.txt,
},
{
key: 'props',
label: 'Props',
icon: 'i-ph-code-duotone',
},
]
}
else if (editorCode.value.id === 'html') {
arr.push({
key: 'html',
label: 'HTML',
icon: 'i-ph-file-html-duotone',
code: template.value.html,
})
}
else if (editorCode.value.id === 'txt') {
arr.push({
key: 'txt',
label: 'Plain Text',
icon: 'i-ph-text-t-duotone',
code: template.value.txt,
})
}
else if (editorCode.value.id === 'vue') {
arr.push({
key: 'vue',
label: 'Vue',
icon: 'i-ph-file-vue-duotone',
code: template.value.vue,
})
}
return arr
})
const tab = ref(0)
</script>
<template>
<UTabs
v-model="tab" :items="items" :ui="{
wrapper: 'relative space-y-0',
}"
>
<template #default="{ item, selected }">
<div class="flex items-center gap-2 relative truncate">
<UIcon :name="item.icon" class="w-7 h-7 flex-shrink-0" />
<span class="truncate">{{ item.label }}</span>
<template v-if="selected && item.code">
<UTooltip text="Copy to clipboard">
<UButton class="ml-6" icon="i-ph-copy-duotone" size="xs" square color="gray" variant="solid" @click="handleClipboard(item.key)" />
</UTooltip>
<UTooltip :text="`Download .${item.key} file`">
<UButton icon="i-ph-download-simple-duotone" size="xs" square color="gray" variant="solid" @click="handleDownload(item.key)" />
</UTooltip>
</template>
<span v-if="selected" class="absolute -right-4 w-2 h-2 rounded-full bg-primary-500 dark:bg-primary-400" />
</div>
</template>
<template #item="{ item }">
<div v-if="item.code" class="w-full h-full" v-html="highlight(item.code, item.key)" />
<div v-else-if="item.key === 'props'" class="w-full h-full">
<UContainer class="py-5 flex flex-col gap-y-4">
<template v-for="(prop, idx) in props" :key="idx">
<UFormGroup v-if="prop.type === 'string'" :label="prop.label" :description="prop.description">
<UInput v-model="prop.value" type="text" />
</UFormGroup>
<UButton label="Update" @click="renderEmail()" />
</template>
</UContainer>
</div>
</template>
</UTabs>
</template>
<style>
/* TODO: fix content height issues */
.shiki {
width: 100%;
height: 90vh;
padding-bottom: 100px;
padding-inline: 20px;
font-size: 16px;
outline: none;
border: none;
overflow: auto;
white-space: break-spaces;
}
</style>