-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
208 lines (189 loc) · 7.46 KB
/
index.html
File metadata and controls
208 lines (189 loc) · 7.46 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Turn code into art! 🌈 Paste your code, and get a colorful image. It's code makeover fun! 💻🪄">
<title>ChromaCode - Create beautiful images of your code</title>
<link rel="icon"
href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>✨</text></svg>">
<link rel="stylesheet" href="style.css">
<!-- <link rel="stylesheet" href="highlight/styles/default.css"> -->
<link rel="stylesheet" href="highlight/styles/one-dark-pro.css">
</head>
<body>
<header>✨</header>
<div class="code-image">
<main class="main-code-block">
<div class="card">
<div class="card__header">
<div class="card__dots">
<span></span>
<span></span>
<span></span>
</div>
<div contenteditable class="title">Untitled-1</div>
</div>
<pre class="language-auto"><code style="font-weight:700;background: transparent;" spellcheck="false" data-gramm="false" data-gramm_editor="false" data-enable-grammarly="false" class="CODE_BOX language-javascript" contenteditable="true">import { Detail } from "@chromacode/api";
export default function Command() {
return <Detail markdown="Hello World" />;
}</code></pre>
</div>
<div class="resize-handle right"></div>
</main>
</div>
<div class="controls">
<div class="themes">
<h5>Themes</h5>
<div class="custom-theme">
<div class="selected-theme"><span class="theme-color-block"></span> Breeze</div>
<div class="options-container"></div>
</div>
</div>
<!--background toggle button -->
<div class="--background-toggle">
<h5>Background</h5>
<input type="checkbox" checked id="background">
<label for="background" class="--background toggler">
</label>
</div>
<!--background toggle button -->
<!-- Dark mode toggle -->
<div class="--dark-mode-toggle">
<h5>Dark mode</h5>
<input type="checkbox" checked id="dark-mode">
<label for="dark-mode" class="--dark-mode toggler">
</label>
</div>
<!-- Dark mode toggle -->
<!-- Padding selector -->
<div class="padding">
<h5>Padding</h5>
<div class="padding-child">
<div class="buttons">
<input type="radio" name="pad" id="16">
<label for="16">16</label>
</div>
<div class="buttons">
<input type="radio" name="pad" id="32">
<label for="32">32</label>
</div>
<div class="buttons">
<input type="radio" checked name="pad" id="64">
<label for="64">64</label>
</div>
<div class="buttons">
<input type="radio" name="pad" id="128">
<label for="128">128</label>
</div>
</div>
</div>
<div class="wrap">
<h5>Language</h5>
<div class="custom-select">
<div class="selected-option">Select a language</div>
<div class="options"></div>
</div>
</div>
<div class="export">
<h5>Download</h5>
<div>
<a href="#PNG" download class="btn">PNG</a>
<a href="#JPEG" download class="btn">JPEG</a>
<a href="#SVG" download class="btn">SVG</a>
</div>
</div>
</div>
<footer>
Made by
<a href="https://github.com/sarfarazstark" target="_blank">
Sarfaraz
</a> -
<a href="https://github.com/sarfarazstark/ChromaCode">
<img src="github.svg" alt="">
</a>
</footer>
<script src="dom-to-image-more.min.js"></script>
<script src="highlight/highlight.min.js"></script>
<script>
// Adding functionality for downloading the card as an image and svg
const node = document.querySelector('.main-code-block');
const href = document.querySelectorAll('.btn');
const code = document.querySelector('code');
const resizeHandle = document.querySelector('.resize-handle');
const originalDisplay = resizeHandle.style.display;
const download = function (event) {
// Prevent the default action of the click event
// event.preventDefault();
resizeHandle.style.display = 'none';
// Remove the contenteditable attribute
code.removeAttribute('contenteditable');
node.querySelector('.title').removeAttribute('contenteditable');
node.querySelector('.title').style.outline = 'none';
node.querySelector('code').style.overflow = 'hidden';
domtoimage.toSvg(node, { height: node.scrollHeight, width: node.scrollWidth })
.then(function (dataUrl) {
// The dataUrl variable now contains the SVG data in URL form
href[2].href = dataUrl; // Keep the SVG data in href[2]
// Create a new Image object
const img = new Image();
img.src = dataUrl;
img.style.width = node.offsetWidth + 'px'; // Use the original width of the image
img.style.height = node.offsetHeight + 'px'; // Use the original height of the image
img.onload = function () {
// Create a new Canvas element
const canvas = document.createElement('canvas');
canvas.width = node.offsetWidth; // Use the original width of the image
canvas.height = node.offsetHeight; // Use the original height of the image
// Draw the image onto the Canvas
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, node.offsetWidth, node.offsetHeight);
// Convert the Canvas to PNG
const pngData = canvas.toDataURL('image/png');
href[0].href = pngData;
// Convert the Canvas to JPEG
const jpegData = canvas.toDataURL('image/jpeg');
href[1].href = jpegData;
// Restore the visibility of the resize handle
resizeHandle.style.display = originalDisplay;
// Add back the contenteditable attribute
code.setAttribute('contenteditable', 'true');
node.querySelector('.title').setAttribute('contenteditable', 'true');
node.querySelector('code').style.overflow = 'visible';
};
})
.catch(function (error) {
console.error('Error converting DOM to SVG:', error);
});
}
const titleDiv = document.querySelector('.title');
// When the user clicks on the div, clear the default text
titleDiv.addEventListener('focus', () => {
if (titleDiv.textContent === 'Untitled-1') {
titleDiv.textContent = '';
}
});
const debounce = (func, wait) => {
let timeout;
return (...args) => {
clearTimeout(timeout);
timeout = setTimeout(() => func(...args), wait);
};
};
const debouncedDownload = debounce(download, 250);
// When the user moves the focus away from the div, check if they entered anything
titleDiv.addEventListener('blur', () => {
if (titleDiv.textContent === '') {
// If they didn't enter anything, set the value back to the default text
titleDiv.textContent = 'Untitled-1';
}
// Call the download function
debouncedDownload();
});
debouncedDownload();
href.forEach(button => button.addEventListener('click', debouncedDownload));
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.0/beautify.min.js"></script>
<script src="index.js"></script>
</body>
</html>