Spaces:
Sleeping
Sleeping
File size: 7,840 Bytes
96b445e 8661be0 96b445e 8661be0 96b445e |
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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
const { createApp, ref, onMounted, watch, nextTick } = Vue;
const codeSnippets = {
python: `def hello_world():
print("Hello, Trae!")
return True
# Build something amazing
if __name__ == "__main__":
hello_world()`,
javascript: `function greet(name) {
return \`Hello, \${name}!\`;
}
// Welcome to Code Snapshot Studio
console.log(greet('Developer'));`,
typescript: `interface User {
id: number;
name: string;
}
const user: User = {
id: 1,
name: "Trae User"
};
console.log(user);`,
html: `<!DOCTYPE html>
<html lang="en">
<body>
<h1>Hello World</h1>
<p>Welcome to my website.</p>
</body>
</html>`,
css: `.container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background: #f0f0f0;
}`,
java: `public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}`,
go: `package main
import "fmt"
func main() {
fmt.Println("Hello, Go!")
}`,
rust: `fn main() {
println!("Hello, Rust!");
}`,
sql: `SELECT id, username, email
FROM users
WHERE status = 'active'
ORDER BY created_at DESC;`,
bash: `#!/bin/bash
echo "Starting deployment..."
npm install
npm run build
echo "Done!"`,
json: `{
"name": "code-snapshot-studio",
"version": "1.0.0",
"private": true
}`
};
createApp({
setup() {
const code = ref(codeSnippets.python);
const language = ref('python');
const theme = ref('tomorrow');
const padding = ref(64);
const showWindowControls = ref(true);
const showLineNumbers = ref(true);
const showShadow = ref(true);
const windowTitle = ref('main.py');
const snapshotCard = ref(null);
const codeBlock = ref(null);
const isExporting = ref(false);
const showSidebar = ref(true); // For mobile toggle
// Background presets
const backgrounds = [
{ name: 'Gradient 1', value: 'linear-gradient(135deg, #FF9D6C 0%, #BB4E75 100%)' },
{ name: 'Gradient 2', value: 'linear-gradient(135deg, #85FFBD 0%, #FFFB7D 100%)' },
{ name: 'Gradient 3', value: 'linear-gradient(135deg, #8EC5FC 0%, #E0C3FC 100%)' },
{ name: 'Gradient 4', value: 'linear-gradient(135deg, #4158D0 0%, #C850C0 46%, #FFCC70 100%)' },
{ name: 'Solid Dark', value: '#1e293b' },
{ name: 'Solid Light', value: '#f8fafc' },
{ name: 'Mesh Gradient', value: 'radial-gradient(at 40% 20%, hsla(28,100%,74%,1) 0px, transparent 50%), radial-gradient(at 80% 0%, hsla(189,100%,56%,1) 0px, transparent 50%), radial-gradient(at 0% 50%, hsla(355,100%,93%,1) 0px, transparent 50%)' }
];
const currentBg = ref(backgrounds[3]);
const themeBgColor = ref('#2d2d2d'); // Default for Tomorrow
const changeTheme = () => {
const link = document.getElementById('prism-theme');
const baseUrl = 'https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism';
if (theme.value === 'default') {
link.href = `${baseUrl}.min.css`;
themeBgColor.value = '#f5f2f0';
} else {
link.href = `${baseUrl}-${theme.value}.min.css`;
// Update bg color approximation
if(theme.value === 'tomorrow') themeBgColor.value = '#2d2d2d';
if(theme.value === 'okaidia') themeBgColor.value = '#272822';
if(theme.value === 'solarizedlight') themeBgColor.value = '#fdf6e3';
if(theme.value === 'twilight') themeBgColor.value = '#141414';
}
};
const refreshHighlight = () => {
nextTick(() => {
if (window.Prism) {
Prism.highlightAll();
}
});
};
// Auto-load snippet when language changes (if code is empty or matches another snippet)
const onLanguageChange = () => {
// Optional: You could ask user or just automatically switch if it's default
// For now, let's just refresh highlight
refreshHighlight();
};
const loadExample = () => {
if (codeSnippets[language.value]) {
code.value = codeSnippets[language.value];
}
};
watch([code, language, showLineNumbers], () => {
refreshHighlight();
});
onMounted(() => {
refreshHighlight();
// Check screen size for initial sidebar state
if (window.innerWidth < 1024) {
showSidebar.value = false;
}
});
const clearCode = () => {
code.value = '';
};
const copyImage = async () => {
if (!snapshotCard.value || isExporting.value) return;
isExporting.value = true;
try {
await nextTick();
const canvas = await html2canvas(snapshotCard.value, {
scale: 2,
useCORS: true,
backgroundColor: null,
logging: false,
allowTaint: true
});
canvas.toBlob(async (blob) => {
try {
await navigator.clipboard.write([
new ClipboardItem({
[blob.type]: blob
})
]);
alert('图片已复制到剪贴板');
} catch (err) {
console.error('Copy failed:', err);
alert('复制失败,浏览器可能不支持此功能');
}
});
} catch (err) {
console.error('Render failed:', err);
alert('生成图片失败,请重试');
} finally {
isExporting.value = false;
}
};
const exportImage = async () => {
if (!snapshotCard.value || isExporting.value) return;
isExporting.value = true;
try {
// Wait for any renders
await nextTick();
const canvas = await html2canvas(snapshotCard.value, {
scale: 2, // Retina support
useCORS: true,
backgroundColor: null,
logging: false,
allowTaint: true
});
const link = document.createElement('a');
link.download = `code-snapshot-${Date.now()}.png`;
link.href = canvas.toDataURL('image/png');
link.click();
} catch (err) {
console.error('Export failed:', err);
alert('导出失败,请重试');
} finally {
isExporting.value = false;
}
};
const toggleSidebar = () => {
showSidebar.value = !showSidebar.value;
};
return {
code,
language,
theme,
padding,
showWindowControls,
showLineNumbers,
showShadow,
windowTitle,
backgrounds,
currentBg,
snapshotCard,
codeBlock,
copyImage,
clearCode,
exportImage,
changeTheme,
refreshHighlight,
themeBgColor,
onLanguageChange,
loadExample,
isExporting,
showSidebar,
toggleSidebar
};
}
}).mount('#app');
|