-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathindex.js
More file actions
210 lines (194 loc) · 6.69 KB
/
Copy pathindex.js
File metadata and controls
210 lines (194 loc) · 6.69 KB
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
import CodeMirror from './codemirror/lib/codemirror.js'
import './codemirror/mode/xml/xml.js'
(function() {
const input = document.querySelector('input[type=file]')
const result = document.getElementById('result')
const preview = document.querySelector('.preview')
const download = document.getElementById('download')
const copy = document.getElementById('copy')
const previewBg = document.getElementById('preview-bg')
const react = document.getElementById('react-checkbox')
let code = ''
let theme = 'default'
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
theme = 'material-darker'
}
const editor = CodeMirror.fromTextArea(result, {
lineNumbers: true,
mode: {
name: 'xml'
},
theme: theme,
tabSize: 2
})
editor.on('change', function(editor) {
const value = editor.getValue()
readFile(value)
})
function svgDataURL(svg) {
const svgAsXML = (new XMLSerializer).serializeToString(svg)
return 'data:image/svg+xml,' + encodeURIComponent(svgAsXML)
}
const ATTRIBUTE_MAPPING = {
'class': 'className',
'accent-height': 'accentHeight',
'alignment-baseline': 'alignmentBaseline',
'arabic-form': 'arabicForm',
'baseline-shift': 'baselineShift',
'cap-height': 'capHeight',
'clip-path': 'clipPath',
'clip-rule': 'clipRule',
'color-interpolation': 'colorInterpolation',
'color-interpolation-filters': 'colorInterpolationFilters',
'color-profile': 'colorProfile',
'color-rendering': 'colorRendering',
'dominant-baseline': 'dominantBaseline',
'enable-background': 'enableBackground',
'fill-opacity': 'fillOpacity',
'fill-rule': 'fillRule',
'flood-color': 'floodColor',
'flood-opacity': 'floodOpacity',
'font-family': 'fontFamily',
'font-size': 'fontSize',
'font-size-adjust': 'fontSizeAdjust',
'font-stretch': 'fontStretch',
'font-style': 'fontStyle',
'font-variant': 'fontVariant',
'font-weight': 'fontWeight',
'glyph-name': 'glyphName',
'glyph-orientation-horizontal': 'glyphOrientationHorizontal',
'glyph-orientation-vertical': 'glyphOrientationVertical',
'horiz-adv-x': 'horizAdvX',
'horiz-origin-x': 'horizOriginX',
'image-rendering': 'imageRendering',
'letter-spacing': 'letterSpacing',
'lighting-color': 'lightingColor',
'marker-end': 'markerEnd',
'marker-mid': 'markerMid',
'marker-start': 'markerStart',
'overline-position': 'overlinePosition',
'overline-thickness': 'overlineThickness',
'paint-order': 'paintOrder',
'panose-1': 'panose1',
'pointer-events': 'pointerEvents',
'rendering-intent': 'renderingIntent',
'shape-rendering': 'shapeRendering',
'stop-color': 'stopColor',
'stop-opacity': 'stopOpacity',
'strikethrough-position': 'strikethroughPosition',
'strikethrough-thickness': 'strikethroughThickness',
'stroke-dasharray': 'strokeDasharray',
'stroke-dashoffset': 'strokeDashoffset',
'stroke-linecap': 'strokeLinecap',
'stroke-linejoin': 'strokeLinejoin',
'stroke-miterlimit': 'strokeMiterlimit',
'stroke-opacity': 'strokeOpacity',
'stroke-width': 'strokeWidth',
'text-anchor': 'textAnchor',
'text-decoration': 'textDecoration',
'text-rendering': 'textRendering',
'underline-position': 'underlinePosition',
'underline-thickness': 'underlineThickness',
'unicode-bidi': 'unicodeBidi',
'unicode-range': 'unicodeRange',
'units-per-em': 'unitsPerEm',
'v-alphabetic': 'vAlphabetic',
'v-hanging': 'vHanging',
'v-ideographic': 'vIdeographic',
'v-mathematical': 'vMathematical',
'vector-effect': 'vectorEffect',
'vert-adv-y': 'vertAdvY',
'vert-origin-x': 'vertOriginX',
'vert-origin-y': 'vertOriginY',
'word-spacing': 'wordSpacing',
'writing-mode': 'writingMode',
'x-height': 'xHeight',
'xlink:actuate': 'xlinkActuate',
'xlink:arcrole': 'xlinkArcrole',
'xlink:href': 'xlinkHref',
'xlink:role': 'xlinkRole',
'xlink:show': 'xlinkShow',
'xlink:title': 'xlinkTitle',
'xlink:type': 'xlinkType',
'xml:base': 'xmlBase',
'xml:lang': 'xmlLang',
'xml:space': 'xmlSpace',
'xmlns:xlink': 'xmlnsXlink'
}
function htmlToJsx(htmlStr) {
let result = htmlStr
for (const [ htmlAttr, jsxAttr ] of Object.entries(ATTRIBUTE_MAPPING)) {
const escapedHtmlAttr = htmlAttr.replace(/:/g, '\\:')
const regex = new RegExp(`\\b${escapedHtmlAttr}\\s*=`, 'g')
result = result.replace(regex, `${jsxAttr}=`)
}
return result
}
function jsxToHtml(jsxStr) {
let result = jsxStr
for (const [ htmlAttr, jsxAttr ] of Object.entries(ATTRIBUTE_MAPPING)) {
const regex = new RegExp(`\\b${jsxAttr}\\s*=`, 'g')
result = result.replace(regex, `${htmlAttr}=`)
}
return result
}
function readFile(event) {
const value = (event.target && event.target.result) || event
if (event.target && event.target.result) {
const editorValue = react.checked ? htmlToJsx(value) : value
editor.setValue(editorValue)
}
const standardSvg = react.checked ? jsxToHtml(value) : value
const fragment = document.createRange().createContextualFragment(standardSvg)
if (fragment.querySelector('svg')) {
preview.innerHTML = standardSvg
code = react.checked ? htmlToJsx(standardSvg) : standardSvg
copy.removeAttribute('disabled')
const svg = preview.querySelector('svg')
download.href = svgDataURL(svg)
download.setAttribute('download', 'image.svg')
} else {
preview.innerHTML = 'Not a valid SVG code'
}
if (!value) {
copy.setAttribute('disabled', true)
preview.innerHTML = ''
}
}
function changeFile() {
const file = input.files[0]
const reader = new FileReader()
reader.addEventListener('load', readFile)
reader.readAsText(file)
}
function handleCopy (e) {
window.navigator.clipboard.writeText(code)
e.target.classList.add('copied')
setTimeout(() => {
e.target.classList.remove('copied')
}, 1000)
}
function handleDownload (e) {
if (!e.currentTarget.getAttribute('href')) {
e.preventDefault()
}
}
function handlePreviewBgChange (e) {
preview.style.background = e?.target?.value || '#e3e3e3'
}
function handleReactChange() {
const value = editor.getValue()
if (!value) return
if (react.checked) {
editor.setValue(htmlToJsx(value))
} else {
editor.setValue(jsxToHtml(value))
}
}
input.addEventListener('change', changeFile)
copy.addEventListener('click', handleCopy)
download.addEventListener('click', handleDownload)
previewBg.addEventListener('input', handlePreviewBgChange)
react.addEventListener('change', handleReactChange)
handlePreviewBgChange()
})()