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
import React, { useState, useRef, useEffect } from 'react';
import styled from 'styled-components';
import { Title, Wrapper, Container, InputText, PreviewContainer, Preview } from './SharedStyles';
import { useTranslation } from '../js/i18n';
const DownloadButton = styled.button`
padding: 8px 16px;
background-color: #3498db;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
transition: background-color 0.3s ease;
align-self: flex-end;
margin-top: 10px;
&:hover {
background-color: #2980b9;
}
`;
function TextToImage() {
const { t } = useTranslation();
const [text, setText] = useState('');
const previewRef = useRef(null);
const formatText = (text) => {
return text
.replace(/^### (.*$)/gim, '<h4>$1</h4>')
.replace(/^## (.*$)/gim, '<h3>$1</h3>')
.replace(/^# (.*$)/gim, '<h2>$1</h2>')
.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>')
.replace(/\n{2,}/g, '<br/><br/>')
.replace(/\n/g, '<br/>');
};
const handleDownload = async () => {
const previewClone = previewRef.current.cloneNode(true);
document.body.appendChild(previewClone);
previewClone.style.position = 'absolute';
previewClone.style.left = '-9999px';
previewClone.style.width = 'auto';
previewClone.style.maxWidth = '800px';
previewClone.style.height = 'auto';
previewClone.style.whiteSpace = 'pre-wrap';
previewClone.style.backgroundColor = 'white';
previewClone.style.padding = '40px';
try {
const html2canvas = (await import('html2canvas')).default;
const canvas = await html2canvas(previewClone, {
backgroundColor: 'white',
scale: 2,
width: previewClone.offsetWidth,
height: previewClone.offsetHeight
});
const link = document.createElement('a');
link.download = 'text_image.png';
link.href = canvas.toDataURL('image/png');
link.click();
} catch (error) {
console.error('Failed to load html2canvas:', error);
} finally {
document.body.removeChild(previewClone);
}
};
return (
<Wrapper>
<Title>{t('tools.text2image.title')}</Title>
<Container>
<InputText
placeholder={t('tools.text2image.inputPlaceholder')}
value={text}
onChange={(e) => setText(e.target.value)}
/>
<PreviewContainer>
<Preview
ref={previewRef}
dangerouslySetInnerHTML={{ __html: formatText(text) }}
/>
<DownloadButton onClick={handleDownload}>
{t('tools.text2image.downloadButton')}
</DownloadButton>
</PreviewContainer>
</Container>
</Wrapper>
);
}
export default TextToImage;