<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SVG Test Site</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Google Fonts -->
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@500&family=Roboto:wght@400;500&display=swap" rel="stylesheet">
<style>
/* ... (CSS styles remain unchanged) ... */
</style>
</head>
<body>
<!-- Header -->
<header>
<h1>SVG Test Site</h1>
</header>
<!-- Canvases Wrapper -->
<div class="canvases-wrapper" id="canvases"></div>
<!-- Add Canvas Button -->
<button id="add-canvas-btn" title="Add New Canvas" aria-label="Add New Canvas">+</button>
<!-- Toast Notification -->
<div class="toast" id="toast">Action Successful</div>
<script>
(function() {
// ... (Variable declarations remain unchanged) ...
// Initialize with one canvas
addCanvas();
// ... (Event listener for addCanvasBtn remains unchanged) ...
function addCanvas() {
// ... (Canvas creation logic remains mostly unchanged) ...
// Preview Container - Fix: Give unique ID to preview div based on canvasId
const previewDiv = document.createElement('div');
previewDiv.setAttribute('id', `svg-preview-${canvasId}`); // Use canvasId here
previewDiv.innerHTML = defaultSVG;
previewContainer.appendChild(previewDiv);
// ... (Append elements to DOM remains unchanged) ...
// Event Listeners - Fix: Use canvasId for previewDiv selector
textarea.addEventListener('input', () => {
updatePreview(textarea.value, previewDiv);
});
// ... other event listeners
// Load SVG from URL hash for all canvases - Improvement
loadSVGFromHash(textarea, previewDiv);
// ... (Disable Add Canvas button logic remains unchanged) ...
}
function updatePreview(svgContent, previewDiv) {
// ... (updatePreview logic remains unchanged)
}
// ... (Other functions: toggleFullscreen, downloadSVG, shareLink, fallbackCopyTextToClipboard, showToast remain unchanged) ...
// Load SVG from URL hash on initial load and hash change
function loadSVGFromHash(textarea, previewDiv) {
const hash = window.location.hash.substring(1);
if (hash) {
try {
const decoded = decodeURIComponent(hash);
textarea.value = decoded;
updatePreview(decoded, previewDiv);
showToast('SVG Loaded from URL');
// Clear the hash after loading to prevent reload issues
window.location.hash = ""; // Improvement: Clear the hash after loading.
} catch (e) {
console.error('Invalid SVG code in URL hash.');
// If hash is invalid, use default SVG but DON'T show error toast.
textarea.value = defaultSVG;
updatePreview(defaultSVG, previewDiv);
}
}
}
// Remove unnecessary load event listener and hashchange listener duplication
window.addEventListener('hashchange', () => {
// Loop through all canvases and update if there's a hash
const canvasContainers = document.querySelectorAll('.canvas-container');
canvasContainers.forEach(container => {
const textarea = container.querySelector('textarea');
const previewDiv = container.querySelector(`#svg-preview-${container.id}`); // Correct selector based on canvasId
loadSVGFromHash(textarea, previewDiv);
});
});
})();
</script>
</body>
</html>
These changes address the identified bugs, improve functionality, and enhance the overall user experience of the SVG Test Site.