Files
site-builder/preview.html
Josh Knapp a71b58c2c7 Initial commit: Site Builder with PHP API backend
Visual drag-and-drop website builder using GrapesJS with:
- Multi-page editor with live preview
- File-based asset storage via PHP API (no localStorage base64)
- Template library, Docker support, and Playwright test suite

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-28 19:25:42 +00:00

344 lines
11 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Site Preview</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&family=Roboto:wght@300;400;500;700&family=Open+Sans:wght@300;400;500;600;700&family=Poppins:wght@300;400;500;600;700&family=Montserrat:wght@300;400;500;600;700&family=Playfair+Display:wght@400;500;600;700&family=Merriweather:wght@300;400;700&family=Source+Code+Pro:wght@400;500;600&display=swap" rel="stylesheet">
<style id="preview-styles">
/* Base reset */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: Inter, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
line-height: 1.5;
color: #1f2937;
}
img {
max-width: 100%;
height: auto;
}
video {
max-width: 100%;
height: auto;
}
a {
color: inherit;
}
/* Hide editor-only elements in preview */
.editor-anchor,
.editor-anchor *,
[data-anchor="true"],
[data-anchor="true"] * {
display: none !important;
visibility: hidden !important;
opacity: 0 !important;
position: absolute !important;
width: 0 !important;
height: 0 !important;
overflow: hidden !important;
}
/* Responsive columns on mobile */
@media (max-width: 480px) {
.row {
flex-direction: column !important;
}
.row .cell {
flex-basis: 100% !important;
width: 100% !important;
}
}
/* Preview bar */
.preview-bar {
position: fixed;
top: 0;
left: 0;
right: 0;
background: #16161a;
color: #fff;
padding: 12px 20px;
display: flex;
align-items: center;
justify-content: space-between;
z-index: 9999;
font-family: Inter, sans-serif;
box-shadow: 0 2px 10px rgba(0,0,0,0.3);
}
.preview-bar-left {
display: flex;
align-items: center;
gap: 12px;
}
.preview-bar-center {
display: flex;
align-items: center;
gap: 8px;
}
.preview-bar-title {
font-size: 14px;
font-weight: 500;
}
.preview-bar-badge {
background: #3b82f6;
color: #fff;
font-size: 11px;
font-weight: 600;
padding: 4px 8px;
border-radius: 4px;
text-transform: uppercase;
}
.preview-bar-btn {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 8px 16px;
background: #3b82f6;
color: #fff;
border: none;
border-radius: 6px;
font-size: 13px;
font-weight: 500;
cursor: pointer;
text-decoration: none;
transition: background 0.2s;
}
.preview-bar-btn:hover {
background: #2563eb;
}
.page-selector {
display: flex;
align-items: center;
gap: 4px;
background: #2d2d3a;
padding: 4px;
border-radius: 6px;
}
.page-selector-btn {
padding: 6px 12px;
background: transparent;
border: none;
border-radius: 4px;
color: #a1a1aa;
font-size: 13px;
cursor: pointer;
transition: all 0.2s;
}
.page-selector-btn:hover {
background: #3f3f46;
color: #e4e4e7;
}
.page-selector-btn.active {
background: #3b82f6;
color: #fff;
}
.preview-content {
margin-top: 52px;
}
/* Error state */
.preview-error {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: calc(100vh - 52px);
padding: 40px;
text-align: center;
background: #f9fafb;
}
.preview-error h2 {
font-size: 24px;
color: #1f2937;
margin-bottom: 12px;
}
.preview-error p {
color: #6b7280;
margin-bottom: 24px;
}
</style>
</head>
<body>
<!-- Preview Bar -->
<div class="preview-bar">
<div class="preview-bar-left">
<span class="preview-bar-badge">Preview</span>
<span class="preview-bar-title">Site Preview</span>
</div>
<div class="preview-bar-center">
<div id="page-selector" class="page-selector">
<!-- Page buttons will be added here -->
</div>
</div>
<a href="index.html" class="preview-bar-btn">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"></path>
<path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z"></path>
</svg>
Back to Editor
</a>
</div>
<!-- Preview Content -->
<div class="preview-content" id="preview-content">
<!-- Content will be injected here -->
</div>
<script>
/**
* Preview Page Script
*
* Security Note: This page intentionally renders user-created HTML content
* from localStorage. The content originates from the same user's editing
* session (same-origin localStorage) and is loaded for preview purposes.
*
* When this system is integrated with a backend, server-side sanitization
* should be applied before storing user content.
*/
(function() {
'use strict';
const STORAGE_KEY = 'sitebuilder-project-preview';
const contentContainer = document.getElementById('preview-content');
const pageSelector = document.getElementById('page-selector');
let pages = [];
let currentPageId = null;
let customStylesEl = null;
// Load preview data from localStorage
const previewData = localStorage.getItem(STORAGE_KEY);
if (previewData) {
try {
const data = JSON.parse(previewData);
// Handle both old format (single page) and new format (multi-page)
if (data.pages) {
pages = data.pages;
currentPageId = data.currentPageId;
} else if (data.html !== undefined) {
// Old format - convert to single page
pages = [{
id: 'legacy',
name: 'Home',
slug: 'index',
html: data.html,
css: data.css
}];
currentPageId = 'legacy';
}
if (pages.length > 0) {
renderPageSelector();
loadPage(currentPageId || pages[0].id);
} else {
showError();
}
} catch (err) {
console.error('Error loading preview:', err);
showError();
}
} else {
showError();
}
function renderPageSelector() {
// Clear existing buttons
pageSelector.innerHTML = '';
if (pages.length <= 1) {
pageSelector.style.display = 'none';
return;
}
pages.forEach(page => {
const btn = document.createElement('button');
btn.className = 'page-selector-btn' + (page.id === currentPageId ? ' active' : '');
btn.textContent = page.name;
btn.addEventListener('click', () => loadPage(page.id));
pageSelector.appendChild(btn);
});
}
function loadPage(pageId) {
const page = pages.find(p => p.id === pageId);
if (!page) return;
currentPageId = pageId;
// Clear content
contentContainer.innerHTML = '';
// Remove old custom styles
if (customStylesEl) {
customStylesEl.remove();
}
// Render page content
// Note: This is user-created content from the same origin
contentContainer.insertAdjacentHTML('beforeend', page.html || '');
// Inject the page's CSS
customStylesEl = document.createElement('style');
customStylesEl.textContent = page.css || '';
document.head.appendChild(customStylesEl);
// Update page selector active state
pageSelector.querySelectorAll('.page-selector-btn').forEach((btn, index) => {
btn.classList.toggle('active', pages[index].id === pageId);
});
}
function showError() {
pageSelector.style.display = 'none';
const errorDiv = document.createElement('div');
errorDiv.className = 'preview-error';
const h2 = document.createElement('h2');
h2.textContent = 'No Preview Available';
const p = document.createElement('p');
p.textContent = "There's no saved content to preview. Go back to the editor and create something!";
const link = document.createElement('a');
link.href = 'index.html';
link.className = 'preview-bar-btn';
link.textContent = 'Open Editor';
errorDiv.appendChild(h2);
errorDiv.appendChild(p);
errorDiv.appendChild(link);
contentContainer.appendChild(errorDiv);
}
})();
</script>
</body>
</html>