Skip to content

Commit 75b67e2

Browse files
committed
feat: add tailwindcss support, add copy image btn
1 parent a130017 commit 75b67e2

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

src/components/ui/ShareModal.tsx

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { useStore } from '@nanostores/solid'
2-
import { For, Show, createSignal } from 'solid-js'
2+
import { For, Show, createEffect, createSignal } from 'solid-js'
33
import satori from 'satori'
44
import * as resvg from '@resvg/resvg-wasm'
5-
import { useClipboardCopy, useI18n } from '@/hooks'
5+
import { useClipboardCopy, useDark, useI18n } from '@/hooks'
66
import { currentConversationId } from '@/stores/conversation'
77
import { getMessagesByConversationId } from '@/stores/messages'
88
import { showSelectMessageModal, showShareModal } from '@/stores/ui'
@@ -14,24 +14,43 @@ export default () => {
1414
const $currentConversationId = useStore(currentConversationId)
1515
const messages = getMessagesByConversationId($currentConversationId()).filter(item => item.isSelected)
1616
const [imageUrl, setImageUrl] = createSignal('')
17+
const [imageBuffer, setImageBuffer] = createSignal<Blob>()
1718
const [loading, setLoading] = createSignal(false)
19+
const [isDark] = useDark()
1820

1921
console.log($currentConversationId(), messages)
2022

2123
const [copied, copy] = useClipboardCopy(messages.map(item => `${item.role}: ${item.content}`).join('\n'))
2224

25+
const copyImage = () => {
26+
const [,copy] = useClipboardCopy(imageBuffer()!)
27+
copy()
28+
}
29+
30+
createEffect(async() => {
31+
try {
32+
await resvg.initWasm(fetch('https://unpkg.com/@resvg/resvg-wasm/index_bg.wasm'))
33+
} catch (error) {
34+
}
35+
}, [])
36+
2337
const handleLoadImage = async() => {
2438
let _result = ''
2539
setLoading(true)
2640
try {
2741
const fontData = await fetch('https://cdn.jsdelivr.net/gh/yzh990918/static@master/20230609/Inter-Medium.388xm374fse8.ttf').then(res => res.arrayBuffer())
2842
_result = await satori(
29-
// TODO: context image dom
3043
{
3144
type: 'div',
3245
props: {
33-
children: 'hello, world',
34-
style: { color: 'black' },
46+
tw: isDark() ? 'flex flex-col items-stretch w-full h-full text-white bg-[#3333333] p-0' : 'flex flex-col items-stretch w-full h-full text-black bg-white/90 p-0',
47+
children: messages.map(item => ({
48+
type: 'div',
49+
props: {
50+
tw: 'flex items-center w-full h-12 px-4',
51+
children: item.content as string,
52+
},
53+
})),
3554
},
3655
},
3756
{
@@ -46,7 +65,6 @@ export default () => {
4665
],
4766
},
4867
)
49-
await resvg.initWasm(fetch('https://unpkg.com/@resvg/resvg-wasm/index_bg.wasm'))
5068
const res = new resvg.Resvg(_result, {
5169
fitTo: {
5270
mode: 'width',
@@ -56,6 +74,7 @@ export default () => {
5674

5775
const png = res.render()
5876
const pngBuffer = png.asPng()
77+
setImageBuffer(new Blob([pngBuffer], { type: 'image/png' }))
5978
const url = URL.createObjectURL(new Blob([pngBuffer], { type: 'image/png' }))
6079
if (url) {
6180
setLoading(false)
@@ -100,7 +119,8 @@ export default () => {
100119
<div class="flex flex-col gap-2">
101120
<div class="inline-block text-left">
102121
<Show when={imageUrl().length}>
103-
<div class="button inline-block mt-0 cursor-pointer mb-2" onClick={() => { window.open(imageUrl()) }}>{t('conversations.share.image.open')}</div>
122+
<div class="button inline-block mt-0 cursor-pointer mb-2" onClick={() => { copyImage() }}>{t('conversations.share.image.copy')}</div>
123+
<div class="button inline-block mt-0 cursor-pointer mb-2 ml-2" onClick={() => { window.open(imageUrl()) }}>{t('conversations.share.image.open')}</div>
104124
</Show>
105125
<Show when={!imageUrl().length}>
106126
<div class="emerald-light-button inline-block mt-0 cursor-pointer mb-2" onClick={() => handleLoadImage()}>{loading() ? t('conversations.share.image.loading') : t('conversations.share.image.btn')}</div>

src/hooks/useCopy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { createEffect, createSignal } from 'solid-js'
22
import { writeClipboard } from '@solid-primitives/clipboard'
33

4-
export const useClipboardCopy = (source: string, delay = 1000) => {
4+
export const useClipboardCopy = (source: string | Blob, delay = 1000) => {
55
const [copied, setCopied] = createSignal(false)
66

77
const copy = async() => {
8-
writeClipboard(source)
8+
writeClipboard(typeof source === 'string' ? source : [new ClipboardItem({ [source.type]: source })])
99
setCopied(true)
1010
}
1111

src/locale/lang/en.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export const en = {
5252
btn: 'Generate Image',
5353
open: 'Open in Tab',
5454
loading: 'Generating...',
55+
copy: 'Copy Image',
5556
},
5657
},
5758
},

src/locale/lang/zh-cn.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export const zhCN = {
5252
btn: '生成图片',
5353
open: '新窗口打开',
5454
loading: '生成中...',
55+
copy: '复制图片',
5556
},
5657
},
5758
},

0 commit comments

Comments
 (0)