-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
231 lines (199 loc) · 7.03 KB
/
index.js
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
const canvas = document.getElementById('canvas')
const ctx = canvas.getContext('2d')
const errorDiv = document.getElementById('error')
const outputFormatSelect = document.getElementById('outputFormat')
const colorPicker = document.getElementById('colorPicker')
const scaleInput = document.getElementById('scale')
const widthInput = document.getElementById('width')
const heightInput = document.getElementById('height')
const selector = '*' // '*:not(head *, style, script, #preview, #preview *)'
// Initialize tenoxui at first render
document.querySelectorAll(selector).forEach((element) => {
new __tenoxui_core.MakeTenoxUI({ element, ...tenoxuiConfig }).useDOM()
})
widthInput.addEventListener('change', updateCanvasSize)
heightInput.addEventListener('change', updateCanvasSize)
function updateCanvasSize() {
canvas.width = parseInt(widthInput.value)
canvas.height = parseInt(heightInput.value)
updatePreview()
}
function loadTemplate(name) {
document.getElementById('htmlInput').value = templates[name]
updatePreview()
}
function insertElement(elementTemplate) {
const textarea = document.getElementById('htmlInput')
const position = textarea.selectionStart
const content = textarea.value
const before = content.substring(0, position)
const after = content.substring(position)
textarea.value = before + elementTemplate + after
updatePreview()
}
function saveDesign() {
const designData = {
html: document.getElementById('htmlInput').value,
width: canvas.width,
height: canvas.height,
scale: scaleInput.value,
format: outputFormatSelect.value
}
const blob = new Blob([JSON.stringify(designData)], { type: 'application/json' })
const link = document.createElement('a')
link.href = URL.createObjectURL(blob)
link.download = 'tenoxui.design.json'
link.click()
URL.revokeObjectURL(link.href)
}
// Load design from json file
function loadDesign(event) {
const file = event.target.files[0]
const reader = new FileReader()
reader.onload = (e) => {
try {
const designData = JSON.parse(e.target.result)
document.getElementById('htmlInput').value = designData.html
widthInput.value = designData.width
heightInput.value = designData.height
scaleInput.value = designData.scale
outputFormatSelect.value = designData.format
updatePreview()
generateImage()
} catch (error) {
errorDiv.textContent = 'Failed to load design file: ' + error.message
}
}
reader.readAsText(file)
}
async function getGoogleFontsStyles() {
const googleFontsLink = document.getElementById('google-fonts')
if (!googleFontsLink) return
try {
const response = await fetch(googleFontsLink.href)
const css = await response.text()
const fontFaceRules = []
const fontFaceRegex = /@font-face\s*{[^}]+}/g
const matches = css.match(fontFaceRegex)
if (matches) {
for (const rule of matches) {
const urlMatch = rule.match(/url\(([^)]+)\)/)
if (urlMatch) {
let fontUrl = urlMatch[1].replace(/['\"]/g, '')
try {
const fontResponse = await fetch(fontUrl)
const fontBuffer = await fontResponse.arrayBuffer()
const base64Font = btoa(String.fromCharCode(...new Uint8Array(fontBuffer)))
const fontFormat = fontUrl.endsWith('woff2')
? 'woff2'
: fontUrl.endsWith('woff')
? 'woff'
: fontUrl.endsWith('ttf')
? 'truetype'
: 'opentype'
const processedRule = rule.replace(
/url\([^)]+\)/,
`url(data:application/font-${fontFormat};charset=utf-8;base64,${base64Font})`
)
fontFaceRules.push(processedRule)
} catch (error) {
console.warn('Failed to fetch font:', fontUrl, error)
fontFaceRules.push(rule)
}
}
}
}
return fontFaceRules.join('\n')
} catch (error) {
console.error('Failed to process Google Fonts:', error)
return
}
}
async function generateSVG() {
const fontFaceRules = await getGoogleFontsStyles()
const scale = parseFloat(scaleInput.value) || 1
const scaledWidth = canvas.width * scale
const scaledHeight = canvas.height * scale
const temp = document.createElement('div')
temp.innerHTML = document.getElementById('htmlInput').value
const contentDiv = temp.querySelector('div')
if (contentDiv) {
const currentStyle = contentDiv.getAttribute('style') || ''
contentDiv.classList.add(`[transform]-[scale(${scale})]`, '[transform-origin]-[top_left]')
}
temp.querySelectorAll('*').forEach((element) => {
new __tenoxui_core.MakeTenoxUI({ element, ...tenoxuiConfig }).useDOM()
})
const removeAttributesAndElements = (element) => {
if (element.tagName.toLowerCase() !== 'style') {
Array.from(element.attributes).forEach((attr) => {
if (attr.name !== 'style') {
element.removeAttribute(attr.name)
}
})
Array.from(element.children).forEach((child) => {
if (child.tagName.toLowerCase() !== 'style') {
removeAttributesAndElements(child)
}
})
}
}
removeAttributesAndElements(temp)
const data = `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Generated by NOuSantx -->
<svg xmlns="http://www.w3.org/2000/svg" width="${scaledWidth}" height="${scaledHeight}">
<defs>
<style type="text/css">
${fontFaceRules}
</style>
</defs>
<foreignObject width="100%" height="100%">
<div xmlns="http://www.w3.org/1999/xhtml">${temp.innerHTML}</div>
</foreignObject>
</svg>`
return data.replace(/>\s+</g, '><')
}
async function generateImage() {
try {
const scale = parseFloat(scaleInput.value) || 1
const scaledWidth = canvas.width * scale
const scaledHeight = canvas.height * scale
errorDiv.textContent = ''
canvas.width = scaledWidth
canvas.height = scaledHeight
ctx.clearRect(0, 0, canvas.width, canvas.height)
const svgData = await generateSVG()
const img = new Image()
img.src = 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(svgData)
await new Promise((resolve, reject) => {
img.onload = resolve
img.onerror = reject
})
ctx.drawImage(img, 0, 0)
} catch (error) {
errorDiv.textContent = 'Failed to generate image: ' + error.message
}
}
async function downloadImage() {
try {
const outputFormat = outputFormatSelect.value
const link = document.createElement('a')
link.download = `generated-image.${outputFormat}`
if (outputFormat === 'svg') {
const svgData = await generateSVG()
const blob = new Blob([svgData], { type: 'image/svg+xml' })
link.href = URL.createObjectURL(blob)
} else {
link.href = canvas.toDataURL(`image/${outputFormat}`)
}
link.click()
if (outputFormat === 'svg') {
URL.revokeObjectURL(link.href)
}
} catch (error) {
errorDiv.textContent = 'Failed to download image: ' + error.message
}
}
updatePreview()
generateImage()