-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
147 lines (127 loc) · 4.36 KB
/
script.js
File metadata and controls
147 lines (127 loc) · 4.36 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
import { codeToHtml } from "/lib/shiki@1.5.2.js";
async function formatCode() {
const codeBlockWrapper = document.getElementById("code-block");
const content = codeBlockWrapper.innerText;
console.log(content);
const lang = document.getElementById("language-selector").value;
codeBlockWrapper.innerHTML = await codeToHtml(content, {
lang,
theme: "github-dark",
});
const codeBlock = codeBlockWrapper.getElementsByTagName("code")[0];
codeBlock.contentEditable = true;
// Handle focus and input events
codeBlock.addEventListener("focus", () => {
if (codeBlock.textContent === "// Your code here") {
codeBlock.textContent = "";
codeBlock.style.color = ""; // Reset style
}
});
const addPlaceholder = () => {
if (codeBlock.textContent === "") {
codeBlock.textContent = "// Your code here";
codeBlock.style.color = "lightgray";
}
};
codeBlock.addEventListener("blur", addPlaceholder());
addPlaceholder();
// Ensure format free copy-paste
codeBlock.addEventListener("paste", (event) => {
event.preventDefault(); // Prevent default paste behavior
const text = event.clipboardData.getData("text/plain"); // Get plain text
document.execCommand("insertText", false, text); // Insert the plain text
});
}
async function onFormat() {
await formatCode();
addListeners();
}
function addListeners() {
const codeBlock = document
.getElementById("code-block")
.getElementsByTagName("pre")[0]
.getElementsByTagName("code")[0];
codeBlock.addEventListener("blur", onFormat);
}
async function elementToImageBlob(elementId) {
const element = document.getElementById(elementId);
if (!element) {
console.error(`Element with ID "${elementId}" not found.`);
return;
}
try {
const scaleFactor = parseInt(document.getElementById("scale-factor").value);
const canvas = await html2canvas(element, {
scale: scaleFactor,
backgroundColor: "transparent",
});
const blob = await new Promise((resolve) => canvas.toBlob(resolve));
return blob;
} catch (error) {
console.error("Error capturing element as image:", error);
}
}
async function copyElementAsImage(elementId) {
const blob = await elementToImageBlob(elementId);
try {
await navigator.clipboard.write([new ClipboardItem({ "image/png": blob })]);
console.log("Image copied to clipboard!");
} catch (error) {
console.log(blob);
console.error("Error writing to clipboard:", error);
}
}
async function downloadElementAsImage(elementId) {
const blob = await elementToImageBlob(elementId);
const url = window.URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = "code.png";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
window.URL.revokeObjectURL(url);
}
async function onCopy() {
const codeBlockWrapper = document.getElementById("code-block");
codeBlockWrapper.classList.add("code-block-screenshot");
await copyElementAsImage("code-block");
codeBlockWrapper.classList.remove("code-block-screenshot");
}
async function onDownload() {
const codeBlockWrapper = document.getElementById("code-block");
codeBlockWrapper.classList.add("code-block-screenshot");
await downloadElementAsImage("code-block");
codeBlockWrapper.classList.remove("code-block-screenshot");
}
function addBrowserThemeColor() {
// Get the meta tag and the computed style
const themeColorMeta = document.querySelector('meta[name="theme-color"]');
const rootStyle = getComputedStyle(document.documentElement);
// Function to update the theme color
function updateThemeColor() {
const themeColor = rootStyle.getPropertyValue("--background-color");
themeColorMeta.content = themeColor;
}
// Initial update and update on theme change
updateThemeColor();
window
.matchMedia("(prefers-color-scheme: dark)")
.addEventListener("change", updateThemeColor);
}
async function onLoad() {
addBrowserThemeColor();
await formatCode();
addListeners();
document.getElementById("copy-button").addEventListener("click", onCopy);
document
.getElementById("download-button")
.addEventListener("click", onDownload);
document
.getElementById("language-selector")
.addEventListener("change", onFormat);
if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("/service-worker.js");
}
}
onLoad();