WebOrbiton
v3.1.0.0

FlatlyPage

2,763 lines · 144.2 KB
  1. const FLATLY_BASE_URL = window.FLATLY_BASE || '';
  2. ​
  3. function flatlyUrl(path) {
  4. if (!FLATLY_BASE_URL || typeof path !== 'string' || path[0] !== '/' || path[1] === '/') return path;
  5. if (path === FLATLY_BASE_URL || ['/', '?', '#'].some(sep => path.startsWith(FLATLY_BASE_URL + sep))) return path;
  6. return FLATLY_BASE_URL + path;
  7. }
  8. ​
  9. function flatlyRewriteHtml(html) {
  10. if (!FLATLY_BASE_URL) return html;
  11. return html
  12. .replace(/(\s(?:href|src|action|poster|formaction|data-src)\s*=\s*)(["'])(\/(?!\/)[^"']*)\2/gi, (m, attr, quote, path) => attr + quote + flatlyUrl(path) + quote)
  13. .replace(/url\(\s*(["']?)(\/(?!\/)[^)"']*)\1\s*\)/gi, (m, quote, path) => 'url(' + quote + flatlyUrl(path) + quote + ')');
  14. }
  15. ​
  16. const ICONS = {
  17. bolt: '<svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><polygon points="13 2 3 14 12 14 11 22 21 10 12 10 13 2"/></svg>',
  18. layout: '<svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><rect x="3" y="3" width="18" height="18" rx="2"/><path d="M3 9h18M9 21V9"/></svg>',
  19. clock: '<svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/></svg>',
  20. shield: '<svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/></svg>',
  21. chart: '<svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><line x1="18" y1="20" x2="18" y2="10"/><line x1="12" y1="20" x2="12" y2="4"/><line x1="6" y1="20" x2="6" y2="14"/></svg>',
  22. code: '<svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><polyline points="16 18 22 12 16 6"/><polyline points="8 6 2 12 8 18"/></svg>',
  23. star: '<svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2"/></svg>',
  24. heart: '<svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path d="M20.84 4.61a5.5 5.5 0 0 0-7.78 0L12 5.67l-1.06-1.06a5.5 5.5 0 0 0-7.78 7.78l1.06 1.06L12 21.23l7.78-7.78 1.06-1.06a5.5 5.5 0 0 0 0-7.78z"/></svg>',
  25. globe: '<svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><circle cx="12" cy="12" r="10"/><line x1="2" y1="12" x2="22" y2="12"/><path d="M12 2a15.3 15.3 0 0 1 4 10 15.3 15.3 0 0 1-4 10 15.3 15.3 0 0 1-4-10 15.3 15.3 0 0 1 4-10z"/></svg>',
  26. users: '<svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M23 21v-2a4 4 0 0 0-3-3.87"/><path d="M16 3.13a4 4 0 0 1 0 7.75"/></svg>',
  27. settings: '<svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1 0 2.83 2 2 0 0 1-2.83 0l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-2 2 2 2 0 0 1-2-2v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83 0 2 2 0 0 1 0-2.83l.06-.06a1.65 1.65 0 0 0 .33-1.82 1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1-2-2 2 2 0 0 1 2-2h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 0-2.83 2 2 0 0 1 2.83 0l.06.06a1.65 1.65 0 0 0 1.82.33H9a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 2-2 2 2 0 0 1 2 2v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 0 2 2 0 0 1 0 2.83l-.06.06a1.65 1.65 0 0 0-.33 1.82V9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 2 2 2 2 0 0 1-2 2h-.09a1.65 1.65 0 0 0-1.51 1z"/></svg>',
  28. edit: '<svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"/><path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z"/></svg>',
  29. plus: '<svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>',
  30. trash: '<svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><polyline points="3 6 5 6 21 6"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/><line x1="10" y1="11" x2="10" y2="17"/><line x1="14" y1="11" x2="14" y2="17"/></svg>',
  31. copy: '<svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"/><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/></svg>',
  32. eye: '<svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"/><circle cx="12" cy="12" r="3"/></svg>',
  33. eyeOff: '<svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path d="M17.94 17.94A10.07 10.07 0 0 1 12 20c-7 0-11-8-11-8a18.45 18.45 0 0 1 5.06-5.94"/><path d="M9.9 4.24A9.12 9.12 0 0 1 12 4c7 0 11 8 11 8a18.5 18.5 0 0 1-2.16 3.19"/><line x1="1" y1="1" x2="23" y2="23"/></svg>',
  34. image: '<svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><rect x="3" y="3" width="18" height="18" rx="2" ry="2"/><circle cx="8.5" cy="8.5" r="1.5"/><polyline points="21 15 16 10 5 21"/></svg>',
  35. file: '<svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/></svg>',
  36. upload: '<svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="17 8 12 3 7 8"/><line x1="12" y1="3" x2="12" y2="15"/></svg>',
  37. money: '<svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><line x1="12" y1="1" x2="12" y2="23"/><path d="M17 5H9.5a3.5 3.5 0 0 0 0 7h5a3.5 3.5 0 0 1 0 7H6"/></svg>',
  38. };
  39. ​
  40. let draggedBlock = null;
  41. let currentLinkCallback = null;
  42. let previewTimeout = null;
  43. let expandedBlockIndices = new Set();
  44. ​
  45. ​
  46. function setPreviewDevice(device) {
  47. const wrapper = document.getElementById('previewFrameWrapper');
  48. const buttons = document.querySelectorAll('.preview-device-btn');
  49. ​
  50. if (!wrapper) return;
  51. ​
  52. buttons.forEach(btn => {
  53. btn.classList.remove('active');
  54. if (btn.dataset.device === device) {
  55. btn.classList.add('active');
  56. }
  57. });
  58. ​
  59. wrapper.classList.remove('desktop', 'tablet', 'mobile');
  60. if (device !== 'desktop') {
  61. wrapper.classList.add(device);
  62. }
  63. }
  64. ​
  65. function refreshPreview() {
  66. const iframe = document.getElementById('previewFrame');
  67. if (iframe) {
  68. const wrapper = document.getElementById('previewFrameWrapper');
  69. if (wrapper) wrapper.classList.add('loading');
  70. ​
  71. iframe.src = iframe.src;
  72. ​
  73. iframe.onload = function () {
  74. if (wrapper) wrapper.classList.remove('loading');
  75. };
  76. }
  77. }
  78. ​
  79. function schedulePreviewUpdate() {
  80. if (previewTimeout) {
  81. clearTimeout(previewTimeout);
  82. }
  83. previewTimeout = setTimeout(function () {
  84. updateLivePreview();
  85. }, 1000);
  86. }
  87. ​
  88. function escapeHtml(text) {
  89. if (!text) return '';
  90. const div = document.createElement('div');
  91. div.textContent = text;
  92. return div.innerHTML.replace(/"/g, '&quot;').replace(/'/g, '&#39;');
  93. }
  94. ​
  95. function renderPreviewIcon(icon) {
  96. return ICONS[icon] || ICONS['bolt'];
  97. }
  98. ​
  99. function renderPreviewBlock(block) {
  100. if (!block || block.hidden) return '';
  101. const type = block.type || '';
  102. const data = block.data || {};
  103. const id = block.id ? ` id="${escapeHtml(block.id)}"` : '';
  104. ​
  105. switch (type) {
  106. case 'hero': return renderPreviewHero(data, id);
  107. case 'blog-hero': return renderPreviewBlogHero(data, id);
  108. case 'stats': return renderPreviewStats(data, id);
  109. case 'features': return renderPreviewFeatures(data, id);
  110. case 'testimonials': return renderPreviewTestimonials(data, id);
  111. case 'pricing': return renderPreviewPricing(data, id);
  112. case 'cta': return renderPreviewCta(data, id);
  113. case 'text': return renderPreviewText(data, id);
  114. case 'image': return renderPreviewImage(data, id);
  115. case 'image-text': return renderPreviewImageText(data, id);
  116. case 'product-cards': return renderPreviewProductCards(data, id);
  117. case 'video': return renderPreviewVideo(data, id);
  118. case 'gallery': return renderPreviewGallery(data, id);
  119. case 'faq': return renderPreviewFaq(data, id);
  120. case 'team': return renderPreviewTeam(data, id);
  121. case 'audio': return renderPreviewAudio(data, id);
  122. case 'newsletter': return renderPreviewNewsletter(data, id);
  123. case 'html': return data.html || '';
  124. default: return BLOCK_EXTENSIONS[type] ? BLOCK_EXTENSIONS[type].preview(data, id) : '';
  125. }
  126. }
  127. ​
  128. function renderPreviewHero(d, id) {
  129. return `<section class="hero"${id}>
  130. <div class="hero-grid"></div>
  131. <div class="container hero-content">
  132. ${d.badge ? `<div class="badge"><span class="badge-dot"></span><span>${escapeHtml(d.badge)}</span></div>` : ''}
  133. <h1>${escapeHtml(d.title || '')}</h1>
  134. <p>${escapeHtml(d.subtitle || '')}</p>
  135. <div class="hero-buttons">
  136. ${d.button_primary ? `<a href="${escapeHtml(d.button_primary_url || '#')}" class="btn btn-primary">${escapeHtml(d.button_primary)}</a>` : ''}
  137. ${d.button_secondary ? `<a href="${escapeHtml(d.button_secondary_url || '#')}" class="btn btn-outline">${escapeHtml(d.button_secondary)}</a>` : ''}
  138. </div>
  139. </div>
  140. </section>`;
  141. }
  142. ​
  143. function renderPreviewBlogHero(d, id) {
  144. const anchor = id ? ` id="${id}"` : '';
  145. const title = escapeHtml(d.title || '');
  146. const content = escapeHtml(d.content || '');
  147. const image = d.image ? escapeHtml(d.image) : null;
  148. ​
  149. return `
  150. <section class="blog-hero-section"${anchor}>
  151. <div class="container">
  152. ${image ? `
  153. <div class="hero-image-wrapper" style="margin-bottom: 20px;">
  154. <img src="${image}" alt="${title}" style="max-width: 100%; height: auto; border-radius: 8px;">
  155. </div>` : ''}
  156. <div class="container">
  157. ${title ? `<h1 style="margin-bottom: 15px;" class="blog-h1">${title}</h1>` : ''}
  158. ${content ? `
  159. <div class="text-content" style="font-size: 1.1rem; line-height: 1.6; opacity: 0.9;">
  160. ${content.replace(/\n/g, '<br>')}
  161. </div>` : ''}
  162. </div>
  163. </div>
  164. </section>`;
  165. }
  166. ​
  167. function renderPreviewStats(d, id) {
  168. const items = d.items || [];
  169. if (!items.length) return '';
  170. return `<section class="stats"${id}>
  171. <div class="container">
  172. <div class="stats-grid">
  173. ${items.map(item => `<div class="stat-item"><h3>${escapeHtml(item.value || '')}</h3><p>${escapeHtml(item.label || '')}</p></div>`).join('')}
  174. </div>
  175. </div>
  176. </section>`;
  177. }
  178. ​
  179. function renderPreviewFeatures(d, id) {
  180. const items = d.items || [];
  181. return `<section class="features"${id}>
  182. <div class="container">
  183. ${d.title ? `<div class="section-header"><h2>${escapeHtml(d.title)}</h2>${d.subtitle ? `<p>${escapeHtml(d.subtitle)}</p>` : ''}</div>` : ''}
  184. ${items.length ? `<div class="features-grid">
  185. ${items.map(item => `<div class="feature-card">
  186. <div class="feature-icon">${renderPreviewIcon(item.icon || 'bolt')}</div>
  187. <h3>${escapeHtml(item.title || '')}</h3>
  188. <p>${escapeHtml(item.description || '')}</p>
  189. </div>`).join('')}
  190. </div>` : ''}
  191. </div>
  192. </section>`;
  193. }
  194. ​
  195. function renderPreviewTestimonials(d, id) {
  196. const items = d.items || [];
  197. return `<section class="testimonials"${id}>
  198. <div class="container">
  199. ${d.title ? `<div class="section-header"><h2>${escapeHtml(d.title)}</h2>${d.subtitle ? `<p>${escapeHtml(d.subtitle)}</p>` : ''}</div>` : ''}
  200. ${items.length ? `<div class="testimonials-grid">
  201. ${items.map(item => `<div class="testimonial-card">
  202. <p>"${escapeHtml(item.quote || '')}"</p>
  203. <div class="testimonial-author">
  204. <div class="testimonial-avatar">${escapeHtml(item.initials || '')}</div>
  205. <div class="testimonial-info"><h4>${escapeHtml(item.name || '')}</h4><span>${escapeHtml(item.role || '')}</span></div>
  206. </div>
  207. </div>`).join('')}
  208. </div>` : ''}
  209. </div>
  210. </section>`;
  211. }
  212. ​
  213. function renderPreviewPricing(d, id) {
  214. const items = d.items || [];
  215. return `<section class="pricing"${id}>
  216. <div class="container">
  217. ${d.title ? `<div class="section-header"><h2>${escapeHtml(d.title)}</h2>${d.subtitle ? `<p>${escapeHtml(d.subtitle)}</p>` : ''}</div>` : ''}
  218. ${items.length ? `<div class="pricing-grid">
  219. ${items.map(item => `<div class="pricing-card ${item.featured ? 'featured' : ''}">
  220. ${item.featured ? '<span class="pricing-popular">Most Popular</span>' : ''}
  221. <h3>${escapeHtml(item.name || '')}</h3>
  222. <div class="price">${escapeHtml(item.price || '')}<span>${escapeHtml(item.period || '')}</span></div>
  223. <p class="description">${escapeHtml(item.description || '')}</p>
  224. ${item.features && item.features.length ? `<ul class="pricing-features">
  225. ${item.features.map(f => `<li><svg width="20" height="20" fill="none" stroke="currentColor" stroke-width="2"><polyline points="20 6 9 17 4 12"/></svg>${escapeHtml(f)}</li>`).join('')}
  226. </ul>` : ''}
  227. <a href="${escapeHtml(item.button_url || '#')}" class="btn ${item.featured ? 'btn-primary' : 'btn-outline'}">${escapeHtml(item.button_text || item.button || 'Get Started')}</a>
  228. </div>`).join('')}
  229. </div>` : ''}
  230. </div>
  231. </section>`;
  232. }
  233. ​
  234. function renderPreviewCta(d, id) {
  235. return `<section class="cta"${id}>
  236. <div class="container">
  237. <div class="cta-inner">
  238. <h2>${escapeHtml(d.title || '')}</h2>
  239. <p>${escapeHtml(d.subtitle || '')}</p>
  240. <div class="cta-buttons">
  241. ${d.button_primary ? `<a href="${escapeHtml(d.button_primary_url || '#')}" class="btn btn-primary">${escapeHtml(d.button_primary)}</a>` : ''}
  242. ${d.button_secondary ? `<a href="${escapeHtml(d.button_secondary_url || '#')}" class="btn btn-outline">${escapeHtml(d.button_secondary)}</a>` : ''}
  243. </div>
  244. </div>
  245. </div>
  246. </section>`;
  247. }
  248. ​
  249. function renderPreviewText(d, id) {
  250. return `<section class="text-section"${id}>
  251. <div class="container">
  252. ${d.title ? `<h2>${escapeHtml(d.title)}</h2>` : ''}
  253. <div class="text-content">${escapeHtml(d.content || '').replace(/\n/g, '<br>')}</div>
  254. </div>
  255. </section>`;
  256. }
  257. ​
  258. function renderPreviewImage(d, id) {
  259. if (!d.url) return '';
  260. return `<section class="image-section"${id}>
  261. <div class="container">
  262. <figure>
  263. <img src="${escapeHtml(d.url)}" alt="${escapeHtml(d.alt || '')}" loading="lazy">
  264. ${d.caption ? `<figcaption>${escapeHtml(d.caption)}</figcaption>` : ''}
  265. </figure>
  266. </div>
  267. </section>`;
  268. }
  269. ​
  270. function renderPreviewImageText(d, id) {
  271. const imagePos = d.image_position || 'left';
  272. return `<section class="image-text"${id}>
  273. <div class="container">
  274. <div class="image-text-grid image-text-${escapeHtml(imagePos)}">
  275. <div class="image-text-image">
  276. <img src="${escapeHtml(d.image_url || '')}" alt="${escapeHtml(d.image_alt || '')}" loading="lazy">
  277. </div>
  278. <div class="image-text-content">
  279. ${d.subtitle ? `<p class="section-subtitle">${escapeHtml(d.subtitle)}</p>` : ''}
  280. ${d.title ? `<h2>${escapeHtml(d.title)}</h2>` : ''}
  281. <div class="text-content">${escapeHtml(d.content || '').replace(/\n/g, '<br>')}</div>
  282. ${d.button_text ? `<a href="${escapeHtml(d.button_url || '#')}" class="btn btn-primary">${escapeHtml(d.button_text)}</a>` : ''}
  283. </div>
  284. </div>
  285. </div>
  286. </section>`;
  287. }
  288. ​
  289. function renderPreviewProductCards(d, id) {
  290. const products = d.products || [];
  291. if (!products.length) return '';
  292. return `<section class="product-cards"${id}>
  293. <div class="container">
  294. ${d.title ? `<div class="section-header"><h2>${escapeHtml(d.title)}</h2>${d.subtitle ? `<p>${escapeHtml(d.subtitle)}</p>` : ''}</div>` : ''}
  295. <div class="products-grid">
  296. ${products.map(p => `<div class="product-card">
  297. ${p.image ? `<div class="product-image"><img src="${escapeHtml(p.image)}" alt="${escapeHtml(p.title || '')}" width="48" height="48" loading="lazy"></div>` : ''}
  298. <h3>${escapeHtml(p.title || '')}</h3>
  299. <p class="product-description">${escapeHtml(p.description || '')}</p>
  300. ${p.features && p.features.length ? `<ul class="product-features">${p.features.map(f => `<li>${escapeHtml(f)}</li>`).join('')}</ul>` : ''}
  301. ${p.button_text ? `<a href="${escapeHtml(p.button_url || '#')}" class="btn btn-primary btn-sm">${escapeHtml(p.button_text)}</a>` : ''}
  302. </div>`).join('')}
  303. </div>
  304. </div>
  305. </section>`;
  306. }
  307. ​
  308. function renderPreviewVideo(d, id) {
  309. if (!d.url) return '';
  310. const url = d.url;
  311. const type = d.type || 'url';
  312. const poster = d.posterUrl ? escapeHtml(d.posterUrl) : '';
  313. let videoHtml = '';
  314. ​
  315. if (type === 'youtube' || url.match(/youtube\.com|youtu\.be/i)) {
  316. const videoId = url.match(/(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/ ]{11})/i);
  317. if (videoId && videoId[1]) {
  318. videoHtml = `<iframe src="https://www.youtube.com/embed/${escapeHtml(videoId[1])}?autoplay=1" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen loading="lazy"></iframe>`;
  319. }
  320. } else if (type === 'facebook' || url.match(/facebook\.com|fb\.watch/i)) {
  321. videoHtml = `<iframe src="https://www.facebook.com/plugins/video.php?href=${encodeURIComponent(url)}&show_text=false&autoplay=true" frameborder="0" allow="autoplay; clipboard-write; encrypted-media; picture-in-picture; web-share" allowfullscreen loading="lazy"></iframe>`;
  322. } else {
  323. videoHtml = `<video controls preload="metadata" ${poster ? `poster="${poster}"` : ''}><source src="${escapeHtml(url)}" type="${mediaMime(url, 'video/mp4')}">Your browser does not support the video tag.</video>`;
  324. }
  325. ​
  326. const overlayHtml = (poster && type !== 'url')
  327. ? `<div class="video-overlay" style="background-image: url('${poster}');" onclick="this.remove()">
  328. <div class="play-button"></div>
  329. </div>`
  330. : '';
  331. ​
  332. return `<section class="video-section"${id}>
  333. <div class="container">
  334. ${d.title ? `<div class="section-header"><h2>${escapeHtml(d.title)}</h2>${d.subtitle ? `<p>${escapeHtml(d.subtitle)}</p>` : ''}</div>` : ''}
  335. <div class="video-wrapper" style="position: relative;">
  336. ${overlayHtml}
  337. ${videoHtml}
  338. </div>
  339. ${d.caption ? `<p class="video-caption">${escapeHtml(d.caption)}</p>` : ''}
  340. </div>
  341. </section>`;
  342. }
  343. ​
  344. function renderPreviewGallery(d, id) {
  345. const images = d.images || [];
  346. if (!images.length) return '';
  347. return `<section class="gallery-section"${id}>
  348. <div class="container">
  349. ${d.title ? `<div class="section-header"><h2>${escapeHtml(d.title)}</h2>${d.subtitle ? `<p>${escapeHtml(d.subtitle)}</p>` : ''}</div>` : ''}
  350. <div class="gallery-grid">
  351. ${images.map(img => `<div class="gallery-item">
  352. <img src="${escapeHtml(img.url || '')}" alt="${escapeHtml(img.alt || '')}" loading="lazy">
  353. ${img.caption ? `<div class="gallery-caption">${escapeHtml(img.caption)}</div>` : ''}
  354. </div>`).join('')}
  355. </div>
  356. </div>
  357. </section>`;
  358. }
  359. ​
  360. function renderPreviewFaq(d, id) {
  361. const items = d.items || [];
  362. if (!items.length) return '';
  363. return `<section class="faq-section"${id}>
  364. <div class="container">
  365. ${d.title ? `<div class="section-header"><h2>${escapeHtml(d.title)}</h2>${d.subtitle ? `<p>${escapeHtml(d.subtitle)}</p>` : ''}</div>` : ''}
  366. <div class="faq-list">
  367. ${items.map((item, idx) => `<details class="faq-item" ${idx === 0 ? 'open' : ''}>
  368. <summary class="faq-question">${escapeHtml(item.question || '')}</summary>
  369. <div class="faq-answer">${escapeHtml(item.answer || '').replace(/\n/g, '<br>')}</div>
  370. </details>`).join('')}
  371. </div>
  372. </div>
  373. </section>`;
  374. }
  375. ​
  376. function renderPreviewTeam(d, id) {
  377. const members = d.members || [];
  378. if (!members.length) return '';
  379. return `<section class="team-section"${id}>
  380. <div class="container">
  381. ${d.title ? `<div class="section-header"><h2>${escapeHtml(d.title)}</h2>${d.subtitle ? `<p>${escapeHtml(d.subtitle)}</p>` : ''}</div>` : ''}
  382. <div class="team-grid">
  383. ${members.map(m => `<div class="team-member">
  384. ${m.image ? `<img src="${escapeHtml(m.image)}" alt="${escapeHtml(m.name || '')}" class="team-photo" loading="lazy">` : `<div class="team-avatar">${escapeHtml(m.initials || (m.name || '?').substring(0, 2).toUpperCase())}</div>`}
  385. <h3>${escapeHtml(m.name || '')}</h3>
  386. <p class="team-role">${escapeHtml(m.role || '')}</p>
  387. ${m.bio ? `<p class="team-bio">${escapeHtml(m.bio)}</p>` : ''}
  388. ${m.social && m.social.length ? `<div class="team-social">
  389. ${m.social.map(s => `<a href="${escapeHtml(s.url || '#')}" target="_blank" rel="noopener" class="team-social-link" title="${escapeHtml(s.platform || '')}">
  390. ${s.platform === 'twitter' ? '<svg width="20" height="20" fill="currentColor" viewBox="0 0 24 24"><path d="M23 3a10.9 10.9 0 0 1-3.14 1.53 4.48 4.48 0 0 0-7.86 3v1A10.66 10.66 0 0 1 3 4s-4 9 5 13a11.64 11.64 0 0 1-7 2c9 5 20 0 20-11.5a4.5 4.5 0 0 0-.08-.83A7.72 7.72 0 0 0 23 3z"/></svg>' : ''}
  391. ${s.platform === 'linkedin' ? '<svg width="20" height="20" fill="currentColor" viewBox="0 0 24 24"><path d="M16 8a6 6 0 0 1 6 6v7h-4v-7a2 2 0 0 0-2-2 2 2 0 0 0-2 2v7h-4v-7a6 6 0 0 1 6-6zM2 9h4v12H2z"/><circle cx="4" cy="4" r="2"/></svg>' : ''}
  392. ${s.platform === 'github' ? '<svg width="20" height="20" fill="currentColor" viewBox="0 0 24 24"><path d="M9 19c-5 1.5-5-2.5-7-3m14 6v-3.87a3.37 3.37 0 0 0-.94-2.61c3.14-.35 6.44-1.54 6.44-7A5.44 5.44 0 0 0 20 4.77 5.07 5.07 0 0 0 19.91 1S18.73.65 16 2.48a13.38 13.38 0 0 0-7 0C6.27.65 5.09 1 5.09 1A5.07 5.07 0 0 0 5 4.77a5.44 5.44 0 0 0-1.5 3.78c0 5.42 3.3 6.61 6.44 7A3.37 3.37 0 0 0 9 18.13V22"/></svg>' : ''}
  393. </a>`).join('')}
  394. </div>` : ''}
  395. </div>`).join('')}
  396. </div>
  397. </div>
  398. </section>`;
  399. }
  400. ​
  401. function renderPreviewAudio(d, id) {
  402. if (!d.url) return '';
  403. return `<section class="audio-section"${id}>
  404. <div class="container">
  405. ${d.title ? `<div class="section-header"><h2>${escapeHtml(d.title)}</h2>${d.subtitle ? `<p>${escapeHtml(d.subtitle)}</p>` : ''}</div>` : ''}
  406. <div class="audio-wrapper">
  407. <audio controls preload="metadata" class="audio-player"><source src="${escapeHtml(d.url)}" type="${mediaMime(d.url, 'audio/mpeg')}">Your browser does not support the audio element.</audio>
  408. ${d.music_link ? `<a href="${escapeHtml(d.music_link)}" class="btn btn-outline" target="_blank" rel="noopener">${escapeHtml(d.music_link_text || 'Download')}</a>` : ''}
  409. </div>
  410. </div>
  411. </section>`;
  412. }
  413. ​
  414. function renderPreviewNewsletter(d, id) {
  415. return `<section class="newsletter-section"${id}>
  416. <div class="container">
  417. <div class="newsletter-inner">
  418. ${d.title ? `<h2>${escapeHtml(d.title)}</h2>` : ''}
  419. ${d.subtitle ? `<p>${escapeHtml(d.subtitle)}</p>` : ''}
  420. <form class="newsletter-form" onsubmit="event.preventDefault();">
  421. <input type="email" class="newsletter-input" placeholder="${escapeHtml(d.placeholder || 'Enter your email')}" required>
  422. <button type="submit" class="btn btn-primary">${escapeHtml(d.button_text || 'Subscribe')}</button>
  423. </form>
  424. </div>
  425. </div>
  426. </section>`;
  427. }
  428. ​
  429. function updateLivePreview() {
  430. const iframe = document.getElementById('previewFrame');
  431. if (!iframe) return;
  432. ​
  433. const wrapper = document.getElementById('previewFrameWrapper');
  434. if (wrapper) wrapper.classList.add('loading');
  435. ​
  436. const blocksHtml = blocks.map(block => renderPreviewBlock(block)).join('');
  437. ​
  438. const fullHtml = flatlyRewriteHtml(`<!DOCTYPE html>
  439. <html lang="en" data-flatly-preview>
  440. <head>
  441. <meta charset="UTF-8">
  442. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  443. <title>Preview</title>
  444. ${PREVIEW_FONT_HEAD}
  445. <link rel="stylesheet" href="/css/styles.css?v=${Date.now()}">
  446. ${HAS_THEME ? `<link rel="stylesheet" href="/css/theme.css?v=${Date.now()}">` : ""}
  447. <style>
  448. ${PREVIEW_HEADINGS_CSS}
  449. a { pointer-events: none; }
  450. </style>
  451. <script src="/assets/js/admin-theme.js"><\/script>
  452. <script src="/assets/js/blocks.js?v=${Date.now()}" defer><\/script>
  453. </head>
  454. <body>
  455. <main>${blocksHtml}</main>
  456. </body>
  457. </html>`);
  458. ​
  459. iframe.srcdoc = fullHtml
  460. .replace(/\[u\](.*?)\[\/u\]/gi, '<u>$1</u>')
  461. .replace(/\[b\](.*?)\[\/b\]/gi, '<b>$1</b>')
  462. .replace(/\[i\](.*?)\[\/i\]/gi, '<i>$1</i>')
  463. .replace(/\[s\](.*?)\[\/s\]/gi, '<del>$1</del>')
  464. .replace(/\[quote\](.*?)\[\/quote\]/gi, '<blockquote>$1</blockquote>'
  465. );
  466. ​
  467. iframe.onload = function () {
  468. if (wrapper) wrapper.classList.remove('loading');
  469. };
  470. }
  471. ​
  472. ​
  473. if (!footerData.columns) footerData.columns = [];
  474. if (!footerData.social_links) footerData.social_links = [];
  475. if (!footerData.bottom_links) footerData.bottom_links = [];
  476. ​
  477. document.addEventListener('DOMContentLoaded', function () {
  478. renderBlocks();
  479. ​
  480. setTimeout(updateLivePreview, 100);
  481. ​
  482. const previewFrameEdit = document.getElementById('previewFrame');
  483. if (previewFrameEdit) {
  484. const wrapperEdit = document.getElementById('previewFrameWrapper');
  485. if (wrapperEdit) wrapperEdit.classList.add('loading');
  486. previewFrameEdit.onload = function () {
  487. if (wrapperEdit) wrapperEdit.classList.remove('loading');
  488. };
  489. }
  490. renderNavLinks();
  491. renderNavButtons();
  492. renderFooterColumns();
  493. renderSocialLinks();
  494. renderBottomLinks();
  495. renderLanguages();
  496. renderInternalPages();
  497. ​
  498. updateBlockCount();
  499. ​
  500. const tabStorageKey = 'flatly-ui:settings-tab';
  501. ​
  502. const activateSettingsTab = (name) => {
  503. const target = document.querySelector('.settings-tab[data-tab="' + name + '"]');
  504. if (!target || !document.getElementById('panel-' + name)) return false;
  505. document.querySelectorAll('.settings-tab').forEach(t => t.classList.remove('active'));
  506. document.querySelectorAll('.settings-panel').forEach(p => p.classList.remove('active'));
  507. target.classList.add('active');
  508. document.getElementById('panel-' + name).classList.add('active');
  509. return true;
  510. };
  511. ​
  512. document.querySelectorAll('.settings-tab').forEach(tab => {
  513. tab.addEventListener('click', () => {
  514. activateSettingsTab(tab.dataset.tab);
  515. try { localStorage.setItem(tabStorageKey, tab.dataset.tab); } catch (e) {}
  516. });
  517. });
  518. ​
  519. try {
  520. const savedTab = localStorage.getItem(tabStorageKey);
  521. if (savedTab) activateSettingsTab(savedTab);
  522. } catch (e) {}
  523. ​
  524. const settingsForm = document.getElementById('settingsForm');
  525. if (settingsForm) {
  526. settingsForm.addEventListener('invalid', (event) => {
  527. const panel = event.target.closest('.settings-panel');
  528. if (panel && !panel.classList.contains('active')) activateSettingsTab(panel.id.replace(/^panel-/, ''));
  529. }, true);
  530. ​
  531. settingsForm.addEventListener('submit', () => {
  532. settingsForm.querySelectorAll('.flatly-encoded').forEach(input => input.remove());
  533. settingsForm.querySelectorAll('textarea[data-encode]').forEach(area => {
  534. const name = area.name || area.dataset.encodeName;
  535. if (!name) return;
  536. area.dataset.encodeName = name;
  537. area.removeAttribute('name');
  538. const hidden = document.createElement('input');
  539. hidden.type = 'hidden';
  540. hidden.className = 'flatly-encoded';
  541. hidden.name = name.endsWith('[]') ? name.slice(0, -2) + '_b64[]' : name + '_b64';
  542. hidden.value = flatlyBase64Encode(area.value);
  543. settingsForm.appendChild(hidden);
  544. });
  545. });
  546. ​
  547. window.addEventListener('pageshow', () => {
  548. settingsForm.querySelectorAll('.flatly-encoded').forEach(input => input.remove());
  549. settingsForm.querySelectorAll('textarea[data-encode-name]').forEach(area => { area.name = area.dataset.encodeName; });
  550. });
  551. }
  552. });
  553. ​
  554. function flatlyBase64Encode(text) {
  555. const bytes = new TextEncoder().encode(String(text));
  556. let binary = '';
  557. for (let i = 0; i < bytes.length; i += 0x8000) {
  558. binary += String.fromCharCode.apply(null, bytes.subarray(i, i + 0x8000));
  559. }
  560. return btoa(binary);
  561. }
  562. ​
  563. function flatlyBase64Decode(encoded) {
  564. const binary = atob(encoded);
  565. const bytes = new Uint8Array(binary.length);
  566. for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
  567. return new TextDecoder().decode(bytes);
  568. }
  569. ​
  570. (function () {
  571. const scrollKey = 'flatly-ui:scroll:' + location.pathname + location.search;
  572. const pendingKey = 'flatly-ui:scroll-pending';
  573. const desktop = window.matchMedia('(min-width: 769px)');
  574. let timer = 0;
  575. ​
  576. const scroller = () => (desktop.matches ? document.querySelector('.main') : document.scrollingElement) || document.documentElement;
  577. ​
  578. const save = () => {
  579. try { sessionStorage.setItem(scrollKey, String(scroller().scrollTop || 0)); } catch (e) {}
  580. };
  581. ​
  582. const navigationType = () => {
  583. const entry = performance.getEntriesByType && performance.getEntriesByType('navigation')[0];
  584. return entry ? entry.type : '';
  585. };
  586. ​
  587. let shouldRestore = ['reload', 'back_forward'].includes(navigationType());
  588. try {
  589. if (sessionStorage.getItem(pendingKey) === scrollKey) {
  590. shouldRestore = true;
  591. }
  592. sessionStorage.removeItem(pendingKey);
  593. } catch (e) {}
  594. ​
  595. document.addEventListener('submit', () => {
  596. save();
  597. try { sessionStorage.setItem(pendingKey, scrollKey); } catch (e) {}
  598. }, true);
  599. ​
  600. document.addEventListener('scroll', () => {
  601. clearTimeout(timer);
  602. timer = setTimeout(save, 150);
  603. }, { capture: true, passive: true });
  604. ​
  605. window.addEventListener('pagehide', save);
  606. ​
  607. if (!shouldRestore) return;
  608. ​
  609. let saved = 0;
  610. try { saved = parseInt(sessionStorage.getItem(scrollKey) || '0', 10) || 0; } catch (e) {}
  611. if (!saved) return;
  612. ​
  613. const apply = () => { scroller().scrollTop = saved; };
  614. window.addEventListener('load', () => {
  615. apply();
  616. setTimeout(apply, 250);
  617. });
  618. })();
  619. ​
  620. document.getElementById('pageForm')?.addEventListener('submit', function (e) {
  621. document.getElementById('blocksData').value = JSON.stringify(blocks);
  622. });
  623. ​
  624. document.getElementById('settingsForm')?.addEventListener('submit', function (e) {
  625. document.getElementById('navLinksData').value = JSON.stringify(navLinks);
  626. document.getElementById('navButtonsData').value = JSON.stringify(navButtons);
  627. updateFooterData();
  628. document.getElementById('footerData').value = JSON.stringify(footerData);
  629. const languagesField = document.getElementById('languagesDataField');
  630. if (languagesField && typeof languagesData !== 'undefined') languagesField.value = JSON.stringify(languagesData);
  631. });
  632. ​
  633. ​
  634. function generateId() {
  635. return 'block-' + Date.now() + '-' + Math.random().toString(36).substr(2, 9);
  636. }
  637. ​
  638. function updateBlockCount() {
  639. const countEl = document.getElementById('blockCount');
  640. if (countEl) countEl.textContent = blocks.length;
  641. }
  642. ​
  643. function getUniqueBlockId(baseId, currentIndex) {
  644. if (!baseId || baseId.trim() === '') return '';
  645. ​
  646. const cleanId = baseId.toLowerCase().replace(/[^a-z0-9-]/g, '-').replace(/-+/g, '-').replace(/^-|-$/g, '');
  647. ​
  648. let counter = 2;
  649. let finalId = cleanId;
  650. ​
  651. blocks.forEach((block, index) => {
  652. if (index !== currentIndex) {
  653. if (block.id === finalId) {
  654. ​
  655. while (blocks.some((b, i) => i !== currentIndex && b.id === `${cleanId}-${counter}`)) {
  656. counter++;
  657. }
  658. finalId = `${cleanId}-${counter}`;
  659. } else if (block.id === cleanId && counter === 2) {
  660. ​
  661. }
  662. }
  663. });
  664. ​
  665. return finalId;
  666. }
  667. ​
  668. function renderBlocks() {
  669. const container = document.getElementById('blocksContainer');
  670. if (!container) return;
  671. ​
  672. const blockTypes = {};
  673. blocks.forEach((block) => {
  674. if (!block.id) {
  675. const type = block.type;
  676. if (!blockTypes[type]) {
  677. blockTypes[type] = [];
  678. }
  679. blockTypes[type].push(block);
  680. }
  681. });
  682. ​
  683. Object.keys(blockTypes).forEach(type => {
  684. const existingIds = blocks
  685. .filter(b => b.type === type && b.id)
  686. .map(b => b.id);
  687. ​
  688. blockTypes[type].forEach((block) => {
  689. let counter = 1;
  690. let newId = type;
  691. ​
  692. if (existingIds.includes(newId)) {
  693. counter = 2;
  694. while (existingIds.includes(`${type}-${counter}`)) {
  695. counter++;
  696. }
  697. newId = `${type}-${counter}`;
  698. }
  699. ​
  700. block.id = newId;
  701. existingIds.push(newId);
  702. });
  703. });
  704. ​
  705. blocks.forEach((block, index) => {
  706. if (block.id) {
  707. block.id = getUniqueBlockId(block.id, index);
  708. }
  709. });
  710. ​
  711. const expandedItems = document.querySelectorAll('.block-item.expanded');
  712. expandedBlockIndices.clear();
  713. expandedItems.forEach(item => {
  714. const index = item.dataset.index;
  715. if (index !== undefined) expandedBlockIndices.add(parseInt(index));
  716. });
  717. ​
  718. const expandedRepeaters = new Set();
  719. container.querySelectorAll('.block-item').forEach(item => {
  720. item.querySelectorAll('.repeater-items').forEach((list, listIndex) => {
  721. Array.from(list.children).forEach((child, childIndex) => {
  722. if (child.classList.contains('expanded')) expandedRepeaters.add(item.dataset.index + ':' + listIndex + ':' + childIndex);
  723. });
  724. });
  725. });
  726. ​
  727. container.innerHTML = '';
  728. ​
  729. blocks.forEach((block, index) => {
  730. const el = createBlockElement(block, index);
  731. container.appendChild(el);
  732. ​
  733. if (expandedBlockIndices.has(index)) {
  734. el.classList.add('expanded');
  735. }
  736. ​
  737. el.querySelectorAll('.repeater-items').forEach((list, listIndex) => {
  738. Array.from(list.children).forEach((child, childIndex) => {
  739. if (expandedRepeaters.has(index + ':' + listIndex + ':' + childIndex)) child.classList.add('expanded');
  740. });
  741. });
  742. });
  743. ​
  744. updateBlockCount();
  745. }
  746. ​
  747. function createBlockElement(block, index) {
  748. const div = document.createElement('div');
  749. div.className = 'block-item' + (block.hidden ? ' is-hidden' : '');
  750. div.dataset.index = index;
  751. div.draggable = false;
  752. ​
  753. div.addEventListener('dragover', handleDragOver);
  754. div.addEventListener('drop', handleDrop);
  755. div.addEventListener('dragleave', handleDragLeave);
  756. ​
  757. const title = getBlockTitle(block);
  758. ​
  759. div.innerHTML = `
  760. <div class="block-header" onclick="toggleBlock(${index})">
  761. <div class="block-drag" draggable="true" onmousedown="event.stopPropagation()">
  762. <svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24" width="16" height="16"><circle cx="9" cy="5" r="1"/><circle cx="9" cy="12" r="1"/><circle cx="9" cy="19" r="1"/><circle cx="15" cy="5" r="1"/><circle cx="15" cy="12" r="1"/><circle cx="15" cy="19" r="1"/></svg>
  763. </div>
  764. <span class="block-type">${escapeHtml(block.type)}</span>
  765. <span class="block-title">${escapeHtml(title)}</span>
  766. ${block.hidden ? '<span class="block-hidden-badge" title="This block is not shown on the site">Hidden</span>' : ''}
  767. <input type="text" class="block-id-input" value="${escapeHtml(block.id || block.type)}" onmousedown="event.stopPropagation()" onclick="event.stopPropagation()" oninput="this.value = this.value.toLowerCase().replace(/[^a-z0-9-]/g, '-').replace(/-+/g, '-').replace(/^-|-$/g, ''); blocks[${index}].id = this.value;" onblur="renderBlocks();" style="max-width: 120px; font-size: 0.8125rem;">
  768. <div class="block-actions">
  769. <button type="button" class="block-toggle" onclick="event.stopPropagation(); moveBlockUp(${index})" title="Move Up">
  770. <svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><polyline points="18 15 12 9 6 15"/></svg>
  771. </button>
  772. <button type="button" class="block-toggle" onclick="event.stopPropagation(); moveBlockDown(${index})" title="Move Down">
  773. <svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><polyline points="6 9 12 15 18 9"/></svg>
  774. </button>
  775. <button type="button" class="block-toggle${block.hidden ? ' is-active' : ''}" onclick="event.stopPropagation(); toggleBlockHidden(${index})" title="${block.hidden ? 'Show on the site' : 'Hide on the site'}" aria-pressed="${block.hidden ? 'true' : 'false'}">
  776. ${block.hidden ? ICONS.eyeOff : ICONS.eye}
  777. </button>
  778. <button type="button" class="block-toggle" onclick="event.stopPropagation(); copyBlock(${index})" title="Copy to paste on another page">
  779. <svg fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" viewBox="0 0 24 24"><path d="M16 4h2a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h2"/><rect x="8" y="2" width="8" height="4" rx="1"/></svg>
  780. </button>
  781. <button type="button" class="block-toggle" onclick="event.stopPropagation(); duplicateBlock(${index})" title="Duplicate">
  782. <svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><rect x="9" y="9" width="13" height="13" rx="2"/><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/></svg>
  783. </button>
  784. <button type="button" class="block-toggle" onclick="event.stopPropagation(); deleteBlock(${index})" title="Delete" style="color: var(--error);">
  785. <svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><polyline points="3 6 5 6 21 6"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/></svg>
  786. </button>
  787. <button type="button" class="block-toggle block-toggle-arrow">
  788. <svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><polyline points="6 9 12 15 18 9"/></svg>
  789. </button>
  790. </div>
  791. </div>
  792. <div class="block-content">
  793. ${renderBlockFields(block, index)}
  794. </div>
  795. `;
  796. ​
  797. setTimeout(() => {
  798. const dragHandle = div.querySelector('.block-drag');
  799. if (dragHandle) {
  800. dragHandle.addEventListener('dragstart', function (e) {
  801. e.stopPropagation();
  802. draggedBlock = div;
  803. div.classList.add('dragging');
  804. e.dataTransfer.effectAllowed = 'move';
  805. });
  806. ​
  807. dragHandle.addEventListener('dragend', function (e) {
  808. e.stopPropagation();
  809. div.classList.remove('dragging');
  810. document.querySelectorAll('.block-item').forEach(el => el.classList.remove('drag-over'));
  811. draggedBlock = null;
  812. });
  813. }
  814. }, 0);
  815. ​
  816. return div;
  817. }
  818. ​
  819. ​
  820. function getBlockTitle(block) {
  821. switch (block.type) {
  822. case 'hero': return block.data?.title || 'Hero Section';
  823. case 'blog-hero': return block.data?.title || 'Blog Hero Section';
  824. case 'stats': return 'Statistics';
  825. case 'features': return block.data?.title || 'Features';
  826. case 'testimonials': return block.data?.title || 'Testimonials';
  827. case 'pricing': return block.data?.title || 'Pricing';
  828. case 'cta': return block.data?.title || 'Call to Action';
  829. case 'text': return block.data?.title || 'Text Block';
  830. case 'image': return block.data?.alt || 'Image';
  831. case 'image-text': return block.data?.title || 'Image + Text';
  832. case 'product-cards': return block.data?.title || 'Product Cards';
  833. case 'video': return block.data?.title || 'Video';
  834. case 'gallery': return block.data?.title || 'Gallery';
  835. case 'faq': return block.data?.title || 'FAQ';
  836. case 'team': return block.data?.title || 'Team';
  837. case 'audio': return block.data?.title || 'Audio Player';
  838. case 'newsletter': return block.data?.title || 'Newsletter';
  839. case 'html': return 'Custom HTML';
  840. default: return BLOCK_EXTENSIONS[block.type] ? BLOCK_EXTENSIONS[block.type].title(block) : block.type;
  841. }
  842. }
  843. ​
  844. function renderBlockFields(block, blockIndex) {
  845. switch (block.type) {
  846. case 'hero': {
  847. const badgeVal = escapeHtml(block.data?.badge || '');
  848. const titleVal = escapeHtml(block.data?.title || '');
  849. const subtitleVal = escapeHtml(block.data?.subtitle || '');
  850. const btn1Val = escapeHtml(block.data?.button_primary || '');
  851. const btn1Url = block.data?.button_primary_url || '';
  852. const btn2Val = escapeHtml(block.data?.button_secondary || '');
  853. const btn2Url = block.data?.button_secondary_url || '';
  854. return '<div class="form-row">' +
  855. '<div class="block-field">' +
  856. '<label class="block-field-label">Badge Text</label>' +
  857. '<input type="text" class="block-field-input" value="' + badgeVal + '" oninput="updateBlockData(' + blockIndex + ', \'badge\', this.value)">' +
  858. '</div>' +
  859. '<div class="block-field">' +
  860. '<label class="block-field-label">Title</label>' +
  861. '<input type="text" class="block-field-input" value="' + titleVal + '" oninput="updateBlockData(' + blockIndex + ', \'title\', this.value)">' +
  862. '</div>' +
  863. '</div>' +
  864. '<div class="block-field">' +
  865. '<label class="block-field-label">Subtitle</label>' +
  866. '<textarea data-rich="1" class="block-field-input block-field-textarea" oninput="updateBlockData(' + blockIndex + ', \'subtitle\', this.value)">' + subtitleVal + '</textarea>' +
  867. '</div>' +
  868. '<div class="form-row">' +
  869. '<div class="block-field">' +
  870. '<label class="block-field-label">Primary Button Text</label>' +
  871. '<input type="text" class="block-field-input" value="' + btn1Val + '" oninput="updateBlockData(' + blockIndex + ', \'button_primary\', this.value)">' +
  872. '</div>' +
  873. '<div class="block-field">' +
  874. '<label class="block-field-label">Primary Button URL</label>' +
  875. renderLinkInput(btn1Url, 'updateBlockData(' + blockIndex + ', \'button_primary_url\', VALUE)') +
  876. '</div>' +
  877. '</div>' +
  878. '<div class="form-row">' +
  879. '<div class="block-field">' +
  880. '<label class="block-field-label">Secondary Button Text</label>' +
  881. '<input type="text" class="block-field-input" value="' + btn2Val + '" oninput="updateBlockData(' + blockIndex + ', \'button_secondary\', this.value)">' +
  882. '</div>' +
  883. '<div class="block-field">' +
  884. '<label class="block-field-label">Secondary Button URL</label>' +
  885. renderLinkInput(btn2Url, 'updateBlockData(' + blockIndex + ', \'button_secondary_url\', VALUE)') +
  886. '</div>' +
  887. '</div>';
  888. }
  889. ​
  890. case 'blog-hero': {
  891. const titleVal = escapeHtml(block.data?.title || '');
  892. const contentVal = escapeHtml(block.data?.content || '');
  893. const imageUrl = block.data?.image || '';
  894. ​
  895. return `
  896. <div class="block-field">
  897. <label class="block-field-label">Header image (URL)</label>
  898. <div class="image-input-group">
  899. <input type="text" data-media="image" class="block-field-input" value="${imageUrl}"
  900. oninput="updateBlockData(${blockIndex}, 'image', this.value)"
  901. placeholder="https://example.com/image.jpg">
  902. </div>
  903. </div>
  904. ​
  905. <div class="block-field">
  906. <label class="block-field-label">Blog Title</label>
  907. <input type="text" class="block-field-input" value="${titleVal}"
  908. oninput="updateBlockData(${blockIndex}, 'title', this.value)">
  909. </div>
  910. ​
  911. <div class="block-field">
  912. <label class="block-field-label">Content / Introduction</label>
  913. <textarea data-rich="1" class="block-field-input block-field-textarea" rows="4"
  914. oninput="updateBlockData(${blockIndex}, 'content', this.value)">${contentVal}</textarea>
  915. </div>
  916. `;
  917. }
  918. ​
  919. case 'stats':
  920. return `
  921. <div class="block-field">
  922. <label class="block-field-label">Stats Items</label>
  923. <div class="repeater-items" id="stats-items-${blockIndex}">
  924. ${(block.data?.items || []).map((item, i) => `
  925. <div class="repeater-item expanded">
  926. <div class="repeater-item-header" onclick="this.parentElement.classList.toggle('expanded')">
  927. <span class="repeater-item-title">${escapeHtml(item.value || 'New Stat')} - ${escapeHtml(item.label || '')}</span>
  928. <div class="repeater-item-actions">
  929. <button type="button" class="btn btn-ghost btn-sm" onclick="event.stopPropagation(); removeRepeaterItem(${blockIndex}, 'items', ${i})" style="color: var(--error);">Remove</button>
  930. </div>
  931. </div>
  932. <div class="repeater-item-content">
  933. <div class="form-row">
  934. <div class="block-field">
  935. <label class="block-field-label">Value</label>
  936. <input type="text" class="block-field-input" value="${escapeHtml(item.value || '')}" oninput="updateRepeaterItem(${blockIndex}, 'items', ${i}, 'value', this.value)">
  937. </div>
  938. <div class="block-field">
  939. <label class="block-field-label">Label</label>
  940. <input type="text" class="block-field-input" value="${escapeHtml(item.label || '')}" oninput="updateRepeaterItem(${blockIndex}, 'items', ${i}, 'label', this.value)">
  941. </div>
  942. </div>
  943. </div>
  944. </div>
  945. `).join('')}
  946. </div>
  947. <button type="button" class="repeater-add" onclick="addRepeaterItem(${blockIndex}, 'items', {value: '', label: ''})">
  948. <svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24" width="16" height="16"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>
  949. Add Stat
  950. </button>
  951. </div>
  952. `;
  953. ​
  954. case 'features':
  955. return `
  956. <div class="form-row">
  957. <div class="block-field">
  958. <label class="block-field-label">Section Title</label>
  959. <input type="text" class="block-field-input" value="${escapeHtml(block.data?.title || '')}" oninput="updateBlockData(${blockIndex}, 'title', this.value)">
  960. </div>
  961. <div class="block-field">
  962. <label class="block-field-label">Section Subtitle</label>
  963. <input type="text" class="block-field-input" value="${escapeHtml(block.data?.subtitle || '')}" oninput="updateBlockData(${blockIndex}, 'subtitle', this.value)">
  964. </div>
  965. </div>
  966. <div class="block-field">
  967. <label class="block-field-label">Feature Items</label>
  968. <div class="repeater-items" id="features-items-${blockIndex}">
  969. ${(block.data?.items || []).map((item, i) => `
  970. <div class="repeater-item">
  971. <div class="repeater-item-header" onclick="this.parentElement.classList.toggle('expanded')">
  972. <span class="repeater-item-title">${escapeHtml(item.title || 'New Feature')}</span>
  973. <div class="repeater-item-actions">
  974. <button type="button" class="btn btn-ghost btn-sm" onclick="event.stopPropagation(); removeRepeaterItem(${blockIndex}, 'items', ${i})" style="color: var(--error);">Remove</button>
  975. </div>
  976. </div>
  977. <div class="repeater-item-content">
  978. <div class="block-field">
  979. <label class="block-field-label">Icon</label>
  980. <div class="icon-select">
  981. ${Object.keys(ICONS).map(icon => `
  982. <div class="icon-option ${item.icon === icon ? 'selected' : ''}" onclick="updateRepeaterItem(${blockIndex}, 'items', ${i}, 'icon', '${icon}'); renderBlocks();">
  983. ${ICONS[icon]}
  984. </div>
  985. `).join('')}
  986. </div>
  987. </div>
  988. <div class="block-field">
  989. <label class="block-field-label">Title</label>
  990. <input type="text" class="block-field-input" value="${escapeHtml(item.title || '')}" oninput="updateRepeaterItem(${blockIndex}, 'items', ${i}, 'title', this.value)">
  991. </div>
  992. <div class="block-field">
  993. <label class="block-field-label">Description</label>
  994. <textarea data-rich="1" class="block-field-input block-field-textarea" oninput="updateRepeaterItem(${blockIndex}, 'items', ${i}, 'description', this.value)">${escapeHtml(item.description || '')}</textarea>
  995. </div>
  996. </div>
  997. </div>
  998. `).join('')}
  999. </div>
  1000. <button type="button" class="repeater-add" onclick="addRepeaterItem(${blockIndex}, 'items', {icon: 'bolt', title: '', description: ''})">
  1001. <svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24" width="16" height="16"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>
  1002. Add Feature
  1003. </button>
  1004. </div>
  1005. `;
  1006. ​
  1007. case 'testimonials':
  1008. return `
  1009. <div class="form-row">
  1010. <div class="block-field">
  1011. <label class="block-field-label">Section Title</label>
  1012. <input type="text" class="block-field-input" value="${escapeHtml(block.data?.title || '')}" oninput="updateBlockData(${blockIndex}, 'title', this.value)">
  1013. </div>
  1014. <div class="block-field">
  1015. <label class="block-field-label">Section Subtitle</label>
  1016. <input type="text" class="block-field-input" value="${escapeHtml(block.data?.subtitle || '')}" oninput="updateBlockData(${blockIndex}, 'subtitle', this.value)">
  1017. </div>
  1018. </div>
  1019. <div class="block-field">
  1020. <label class="block-field-label">Testimonials</label>
  1021. <div class="repeater-items" id="testimonials-items-${blockIndex}">
  1022. ${(block.data?.items || []).map((item, i) => `
  1023. <div class="repeater-item">
  1024. <div class="repeater-item-header" onclick="this.parentElement.classList.toggle('expanded')">
  1025. <span class="repeater-item-title">${escapeHtml(item.name || 'New Testimonial')}</span>
  1026. <div class="repeater-item-actions">
  1027. <button type="button" class="btn btn-ghost btn-sm" onclick="event.stopPropagation(); removeRepeaterItem(${blockIndex}, 'items', ${i})" style="color: var(--error);">Remove</button>
  1028. </div>
  1029. </div>
  1030. <div class="repeater-item-content">
  1031. <div class="block-field">
  1032. <label class="block-field-label">Quote</label>
  1033. <textarea data-rich="1" class="block-field-input block-field-textarea" oninput="updateRepeaterItem(${blockIndex}, 'items', ${i}, 'quote', this.value)">${escapeHtml(item.quote || '')}</textarea>
  1034. </div>
  1035. <div class="form-row-3">
  1036. <div class="block-field">
  1037. <label class="block-field-label">Name</label>
  1038. <input type="text" class="block-field-input" value="${escapeHtml(item.name || '')}" oninput="updateRepeaterItem(${blockIndex}, 'items', ${i}, 'name', this.value)">
  1039. </div>
  1040. <div class="block-field">
  1041. <label class="block-field-label">Role</label>
  1042. <input type="text" class="block-field-input" value="${escapeHtml(item.role || '')}" oninput="updateRepeaterItem(${blockIndex}, 'items', ${i}, 'role', this.value)">
  1043. </div>
  1044. <div class="block-field">
  1045. <label class="block-field-label">Initials</label>
  1046. <input type="text" class="block-field-input" value="${escapeHtml(item.initials || '')}" oninput="updateRepeaterItem(${blockIndex}, 'items', ${i}, 'initials', this.value)" maxlength="2">
  1047. </div>
  1048. </div>
  1049. </div>
  1050. </div>
  1051. `).join('')}
  1052. </div>
  1053. <button type="button" class="repeater-add" onclick="addRepeaterItem(${blockIndex}, 'items', {quote: '', name: '', role: '', initials: ''})">
  1054. <svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24" width="16" height="16"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>
  1055. Add Testimonial
  1056. </button>
  1057. </div>
  1058. `;
  1059. ​
  1060. case 'pricing': {
  1061. const titleVal = escapeHtml(block.data?.title || '');
  1062. const subtitleVal = escapeHtml(block.data?.subtitle || '');
  1063. let itemsHtml = '';
  1064. (block.data?.items || []).forEach((item, i) => {
  1065. const nameVal = escapeHtml(item.name || 'New Plan');
  1066. const priceVal = escapeHtml(item.price || '');
  1067. const descVal = escapeHtml(item.description || '');
  1068. const periodVal = escapeHtml(item.period || '');
  1069. const btnTextVal = escapeHtml(item.button_text || item.button || '');
  1070. const btnUrlVal = item.button_url || '#';
  1071. const featVal = item.featured ? 'checked' : '';
  1072. itemsHtml += '<div class="repeater-item">' +
  1073. '<div class="repeater-item-header" onclick="this.parentElement.classList.toggle(\'expanded\')">' +
  1074. '<span class="repeater-item-title">' + nameVal + ' - ' + priceVal + '</span>' +
  1075. '<div class="repeater-item-actions">' +
  1076. '<button type="button" class="btn btn-ghost btn-sm" onclick="event.stopPropagation(); removeRepeaterItem(' + blockIndex + ', \'items\', ' + i + ')" style="color: var(--error);">Remove</button>' +
  1077. '</div>' +
  1078. '</div>' +
  1079. '<div class="repeater-item-content">' +
  1080. '<div class="form-row-3">' +
  1081. '<div class="block-field">' +
  1082. '<label class="block-field-label">Plan Name</label>' +
  1083. '<input type="text" class="block-field-input" value="' + nameVal + '" oninput="updateRepeaterItem(' + blockIndex + ', \'items\', ' + i + ', \'name\', this.value)">' +
  1084. '</div>' +
  1085. '<div class="block-field">' +
  1086. '<label class="block-field-label">Price</label>' +
  1087. '<input type="text" class="block-field-input" value="' + priceVal + '" oninput="updateRepeaterItem(' + blockIndex + ', \'items\', ' + i + ', \'price\', this.value)">' +
  1088. '</div>' +
  1089. '<div class="block-field">' +
  1090. '<label class="block-field-label">Period</label>' +
  1091. '<input type="text" class="block-field-input" value="' + periodVal + '" oninput="updateRepeaterItem(' + blockIndex + ', \'items\', ' + i + ', \'period\', this.value)" placeholder="/month">' +
  1092. '</div>' +
  1093. '</div>' +
  1094. '<div class="block-field">' +
  1095. '<label class="block-field-label">Description</label>' +
  1096. '<input type="text" class="block-field-input" value="' + descVal + '" oninput="updateRepeaterItem(' + blockIndex + ', \'items\', ' + i + ', \'description\', this.value)">' +
  1097. '</div>' +
  1098. '<div class="block-field">' +
  1099. '<label class="block-field-label">Features (one per line)</label>' +
  1100. '<textarea class="block-field-input block-field-textarea" oninput="updateRepeaterItem(' + blockIndex + ', \'items\', ' + i + ', \'features\', this.value.split(\'\\n\').filter(f => f.trim()))">' + (item.features || []).join('\n') + '</textarea>' +
  1101. '</div>' +
  1102. '<div class="form-row">' +
  1103. '<div class="block-field">' +
  1104. '<label class="block-field-label">Button Text</label>' +
  1105. '<input type="text" class="block-field-input" value="' + btnTextVal + '" oninput="updateRepeaterItem(' + blockIndex + ', \'items\', ' + i + ', \'button_text\', this.value)">' +
  1106. '</div>' +
  1107. '<div class="block-field">' +
  1108. '<label class="block-field-label">Button URL</label>' +
  1109. renderLinkInput(btnUrlVal, 'updateRepeaterItem(' + blockIndex + ', \'items\', ' + i + ', \'button_url\', VALUE)') +
  1110. '</div>' +
  1111. '</div>' +
  1112. '<div class="block-field">' +
  1113. '<label class="block-field-label" style="display: flex; align-items: center; gap: 8px;">' +
  1114. '<input type="checkbox" ' + featVal + ' onchange="updateRepeaterItem(' + blockIndex + ', \'items\', ' + i + ', \'featured\', this.checked)">' +
  1115. 'Featured Plan (highlighted)' +
  1116. '</label>' +
  1117. '</div>' +
  1118. '</div>' +
  1119. '</div>';
  1120. });
  1121. return '<div class="form-row">' +
  1122. '<div class="block-field">' +
  1123. '<label class="block-field-label">Section Title</label>' +
  1124. '<input type="text" class="block-field-input" value="' + titleVal + '" oninput="updateBlockData(' + blockIndex + ', \'title\', this.value)">' +
  1125. '</div>' +
  1126. '<div class="block-field">' +
  1127. '<label class="block-field-label">Section Subtitle</label>' +
  1128. '<input type="text" class="block-field-input" value="' + subtitleVal + '" oninput="updateBlockData(' + blockIndex + ', \'subtitle\', this.value)">' +
  1129. '</div>' +
  1130. '</div>' +
  1131. '<div class="block-field">' +
  1132. '<label class="block-field-label">Pricing Plans</label>' +
  1133. '<div class="repeater-items" id="pricing-items-' + blockIndex + '">' +
  1134. itemsHtml +
  1135. '</div>' +
  1136. '<button type="button" class="repeater-add" onclick="addRepeaterItem(' + blockIndex + ', \'items\', {name: \'\', price: \'\', period: \'/month\', description: \'\', features: [], button_text: \'Get Started\', button_url: \'#\', featured: false})">' +
  1137. '<svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24" width="16" height="16"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>' +
  1138. 'Add Plan' +
  1139. '</button>' +
  1140. '</div>';
  1141. }
  1142. ​
  1143. case 'cta': {
  1144. const titleVal = escapeHtml(block.data?.title || '');
  1145. const subtitleVal = escapeHtml(block.data?.subtitle || '');
  1146. const btn1Val = escapeHtml(block.data?.button_primary || '');
  1147. const btn1Url = block.data?.button_primary_url || '';
  1148. const btn2Val = escapeHtml(block.data?.button_secondary || '');
  1149. const btn2Url = block.data?.button_secondary_url || '';
  1150. return '<div class="form-row">' +
  1151. '<div class="block-field">' +
  1152. '<label class="block-field-label">Title</label>' +
  1153. '<input type="text" class="block-field-input" value="' + titleVal + '" oninput="updateBlockData(' + blockIndex + ', \'title\', this.value)">' +
  1154. '</div>' +
  1155. '<div class="block-field">' +
  1156. '<label class="block-field-label">Subtitle</label>' +
  1157. '<input type="text" class="block-field-input" value="' + subtitleVal + '" oninput="updateBlockData(' + blockIndex + ', \'subtitle\', this.value)">' +
  1158. '</div>' +
  1159. '</div>' +
  1160. '<div class="form-row">' +
  1161. '<div class="block-field">' +
  1162. '<label class="block-field-label">Primary Button Text</label>' +
  1163. '<input type="text" class="block-field-input" value="' + btn1Val + '" oninput="updateBlockData(' + blockIndex + ', \'button_primary\', this.value)">' +
  1164. '</div>' +
  1165. '<div class="block-field">' +
  1166. '<label class="block-field-label">Primary Button URL</label>' +
  1167. renderLinkInput(btn1Url, 'updateBlockData(' + blockIndex + ', \'button_primary_url\', VALUE)') +
  1168. '</div>' +
  1169. '</div>' +
  1170. '<div class="form-row">' +
  1171. '<div class="block-field">' +
  1172. '<label class="block-field-label">Secondary Button Text</label>' +
  1173. '<input type="text" class="block-field-input" value="' + btn2Val + '" oninput="updateBlockData(' + blockIndex + ', \'button_secondary\', this.value)">' +
  1174. '</div>' +
  1175. '<div class="block-field">' +
  1176. '<label class="block-field-label">Secondary Button URL</label>' +
  1177. renderLinkInput(btn2Url, 'updateBlockData(' + blockIndex + ', \'button_secondary_url\', VALUE)') +
  1178. '</div>' +
  1179. '</div>';
  1180. }
  1181. ​
  1182. case 'text':
  1183. return `
  1184. <div class="block-field">
  1185. <label class="block-field-label">Title (optional)</label>
  1186. <input type="text" class="block-field-input" value="${escapeHtml(block.data?.title || '')}" oninput="updateBlockData(${blockIndex}, 'title', this.value)">
  1187. </div>
  1188. <div class="block-field">
  1189. <label class="block-field-label">Content</label>
  1190. <textarea data-rich="1" class="block-field-input block-field-textarea" style="min-height: 200px;" oninput="updateBlockData(${blockIndex}, 'content', this.value)">${escapeHtml(block.data?.content || '')}</textarea>
  1191. </div>
  1192. `;
  1193. ​
  1194. case 'image':
  1195. return `
  1196. <div class="block-field">
  1197. <label class="block-field-label">Image URL</label>
  1198. <input type="text" data-media="image" class="block-field-input" value="${escapeHtml(block.data?.url || '')}" oninput="updateBlockData(${blockIndex}, 'url', this.value)" placeholder="https://...">
  1199. </div>
  1200. <div class="form-row">
  1201. <div class="block-field">
  1202. <label class="block-field-label">Alt Text</label>
  1203. <input type="text" class="block-field-input" value="${escapeHtml(block.data?.alt || '')}" oninput="updateBlockData(${blockIndex}, 'alt', this.value)">
  1204. </div>
  1205. <div class="block-field">
  1206. <label class="block-field-label">Caption (optional)</label>
  1207. <input type="text" class="block-field-input" value="${escapeHtml(block.data?.caption || '')}" oninput="updateBlockData(${blockIndex}, 'caption', this.value)">
  1208. </div>
  1209. </div>
  1210. `;
  1211. ​
  1212. case 'audio':
  1213. return `
  1214. <div class="form-row">
  1215. <div class="block-field">
  1216. <label class="block-field-label">Title</label>
  1217. <input type="text" class="block-field-input" value="${escapeHtml(block.data?.title || '')}" oninput="updateBlockData(${blockIndex}, 'title', this.value)">
  1218. </div>
  1219. <div class="block-field">
  1220. <label class="block-field-label">Subtitle</label>
  1221. <input type="text" class="block-field-input" value="${escapeHtml(block.data?.subtitle || '')}" oninput="updateBlockData(${blockIndex}, 'subtitle', this.value)">
  1222. </div>
  1223. </div>
  1224. <div class="block-field">
  1225. <label class="block-field-label">Audio File URL</label>
  1226. <input type="text" data-media="audio" class="block-field-input" value="${escapeHtml(block.data?.url || '')}" oninput="updateBlockData(${blockIndex}, 'url', this.value)" placeholder="https://example.com/audio.mp3">
  1227. </div>
  1228. <div class="form-row">
  1229. <div class="block-field">
  1230. <label class="block-field-label">Music Link URL (Optional)</label>
  1231. <input type="url" class="block-field-input" value="${escapeHtml(block.data?.music_link || '')}" oninput="updateBlockData(${blockIndex}, 'music_link', this.value)" placeholder="https://example.com/download">
  1232. </div>
  1233. <div class="block-field">
  1234. <label class="block-field-label">Music Link Text</label>
  1235. <input type="text" class="block-field-input" value="${escapeHtml(block.data?.music_link_text || 'Download')}" oninput="updateBlockData(${blockIndex}, 'music_link_text', this.value)">
  1236. </div>
  1237. </div>
  1238. `;
  1239. ​
  1240. case 'newsletter':
  1241. return `
  1242. <div class="block-field">
  1243. <label class="block-field-label">Title</label>
  1244. <input type="text" class="block-field-input" value="${escapeHtml(block.data?.title || '')}" oninput="updateBlockData(${blockIndex}, 'title', this.value)">
  1245. </div>
  1246. <div class="block-field">
  1247. <label class="block-field-label">Subtitle</label>
  1248. <textarea data-rich="1" class="block-field-input block-field-textarea" oninput="updateBlockData(${blockIndex}, 'subtitle', this.value)">${escapeHtml(block.data?.subtitle || '')}</textarea>
  1249. </div>
  1250. <div class="form-row">
  1251. <div class="block-field">
  1252. <label class="block-field-label">Input Placeholder</label>
  1253. <input type="text" class="block-field-input" value="${escapeHtml(block.data?.placeholder || '')}" oninput="updateBlockData(${blockIndex}, 'placeholder', this.value)">
  1254. </div>
  1255. <div class="block-field">
  1256. <label class="block-field-label">Button Text</label>
  1257. <input type="text" class="block-field-input" value="${escapeHtml(block.data?.button_text || '')}" oninput="updateBlockData(${blockIndex}, 'button_text', this.value)">
  1258. </div>
  1259. </div>
  1260. `;
  1261. ​
  1262. case 'html':
  1263. return `
  1264. <div class="block-field">
  1265. <label class="block-field-label">Custom HTML Code</label>
  1266. <textarea class="block-field-input block-field-textarea" style="min-height: 300px; font-family: ui-monospace, monospace; font-size: 0.8125rem;" oninput="updateBlockData(${blockIndex}, 'html', this.value)">${escapeHtml(block.data?.html || '')}</textarea>
  1267. <p style="font-size: 0.75rem; color: var(--text-subtle); margin-top: 8px;">You can use any HTML, plus CSS in style tags and JavaScript in script tags.</p>
  1268. </div>
  1269. `;
  1270. ​
  1271. case 'image-text':
  1272. return `
  1273. <div class="form-row">
  1274. <div class="block-field">
  1275. <label class="block-field-label">Section Title</label>
  1276. <input type="text" class="block-field-input" value="${escapeHtml(block.data?.title || '')}" oninput="updateBlockData(${blockIndex}, 'title', this.value)">
  1277. </div>
  1278. <div class="block-field">
  1279. <label class="block-field-label">Section Subtitle</label>
  1280. <input type="text" class="block-field-input" value="${escapeHtml(block.data?.subtitle || '')}" oninput="updateBlockData(${blockIndex}, 'subtitle', this.value)">
  1281. </div>
  1282. </div>
  1283. <div class="block-field">
  1284. <label class="block-field-label">Image URL</label>
  1285. <input type="text" data-media="image" class="block-field-input" value="${escapeHtml(block.data?.image_url || '')}" oninput="updateBlockData(${blockIndex}, 'image_url', this.value)" placeholder="https://...">
  1286. </div>
  1287. <div class="form-row">
  1288. <div class="block-field">
  1289. <label class="block-field-label">Image Alt Text</label>
  1290. <input type="text" class="block-field-input" value="${escapeHtml(block.data?.image_alt || '')}" oninput="updateBlockData(${blockIndex}, 'image_alt', this.value)">
  1291. </div>
  1292. <div class="block-field">
  1293. <label class="block-field-label">Image Position</label>
  1294. <select class="block-field-input" onchange="updateBlockData(${blockIndex}, 'image_position', this.value)">
  1295. <option value="left" ${block.data?.image_position === 'left' ? 'selected' : ''}>Left</option>
  1296. <option value="right" ${block.data?.image_position === 'right' ? 'selected' : ''}>Right</option>
  1297. </select>
  1298. </div>
  1299. </div>
  1300. <div class="block-field">
  1301. <label class="block-field-label">Content Text</label>
  1302. <textarea data-rich="1" class="block-field-input block-field-textarea" style="min-height: 150px;" oninput="updateBlockData(${blockIndex}, 'content', this.value)">${escapeHtml(block.data?.content || '')}</textarea>
  1303. </div>
  1304. <div class="form-row">
  1305. <div class="block-field">
  1306. <label class="block-field-label">Button Text (optional)</label>
  1307. <input type="text" class="block-field-input" value="${escapeHtml(block.data?.button_text || '')}" oninput="updateBlockData(${blockIndex}, 'button_text', this.value)">
  1308. </div>
  1309. <div class="block-field">
  1310. <label class="block-field-label">Button URL</label>
  1311. ${renderLinkInput(block.data?.button_url || '#', 'updateBlockData(' + blockIndex + ', \'button_url\', VALUE)')}
  1312. </div>
  1313. </div>
  1314. `;
  1315. ​
  1316. case 'product-cards':
  1317. return `
  1318. <div class="form-row">
  1319. <div class="block-field">
  1320. <label class="block-field-label">Section Title</label>
  1321. <input type="text" class="block-field-input" value="${escapeHtml(block.data?.title || '')}" oninput="updateBlockData(${blockIndex}, 'title', this.value)">
  1322. </div>
  1323. <div class="block-field">
  1324. <label class="block-field-label">Section Subtitle</label>
  1325. <input type="text" class="block-field-input" value="${escapeHtml(block.data?.subtitle || '')}" oninput="updateBlockData(${blockIndex}, 'subtitle', this.value)">
  1326. </div>
  1327. </div>
  1328. <div class="block-field">
  1329. <label class="block-field-label">Products</label>
  1330. <div class="repeater-items" id="products-items-${blockIndex}">
  1331. ${(block.data?.products || []).map((item, i) => `
  1332. <div class="repeater-item">
  1333. <div class="repeater-item-header" onclick="this.parentElement.classList.toggle('expanded')">
  1334. <span class="repeater-item-title">${escapeHtml(item.title || 'New Product')}</span>
  1335. <div class="repeater-item-actions">
  1336. <button type="button" class="btn btn-ghost btn-sm" onclick="event.stopPropagation(); removeRepeaterItem(${blockIndex}, 'products', ${i})" style="color: var(--error);">Remove</button>
  1337. </div>
  1338. </div>
  1339. <div class="repeater-item-content">
  1340. <div class="form-row">
  1341. <div class="block-field">
  1342. <label class="block-field-label">Product Title</label>
  1343. <input type="text" class="block-field-input" value="${escapeHtml(item.title || '')}" oninput="updateRepeaterItem(${blockIndex}, 'products', ${i}, 'title', this.value)">
  1344. </div>
  1345. <div class="block-field">
  1346. <label class="block-field-label">Product Image (48x48)</label>
  1347. <input type="text" data-media="image" class="block-field-input" value="${escapeHtml(item.image || '')}" oninput="updateRepeaterItem(${blockIndex}, 'products', ${i}, 'image', this.value)" placeholder="https://...">
  1348. </div>
  1349. </div>
  1350. <div class="block-field">
  1351. <label class="block-field-label">Description</label>
  1352. <textarea data-rich="1" class="block-field-input block-field-textarea" oninput="updateRepeaterItem(${blockIndex}, 'products', ${i}, 'description', this.value)">${escapeHtml(item.description || '')}</textarea>
  1353. </div>
  1354. <div class="block-field">
  1355. <label class="block-field-label">Features (one per line)</label>
  1356. <textarea class="block-field-input block-field-textarea" oninput="updateRepeaterItem(${blockIndex}, 'products', ${i}, 'features', this.value.split('\\n').filter(f => f.trim()))">${(item.features || []).join('\\n')}</textarea>
  1357. </div>
  1358. <div class="form-row">
  1359. <div class="block-field">
  1360. <label class="block-field-label">Button Text</label>
  1361. <input type="text" class="block-field-input" value="${escapeHtml(item.button_text || '')}" oninput="updateRepeaterItem(${blockIndex}, 'products', ${i}, 'button_text', this.value)">
  1362. </div>
  1363. <div class="block-field">
  1364. <label class="block-field-label">Button URL</label>
  1365. <input type="text" class="block-field-input" value="${escapeHtml(item.button_url || '#')}" oninput="updateRepeaterItem(${blockIndex}, 'products', ${i}, 'button_url', this.value)">
  1366. </div>
  1367. </div>
  1368. </div>
  1369. </div>
  1370. `).join('')}
  1371. </div>
  1372. <button type="button" class="repeater-add" onclick="addRepeaterItem(${blockIndex}, 'products', {title: '', image: '', description: '', features: [], button_text: 'View', button_url: '#'})">
  1373. <svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24" width="16" height="16"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>
  1374. Add Product
  1375. </button>
  1376. </div>
  1377. `;
  1378. ​
  1379. case 'video':
  1380. return `
  1381. <div class="form-row">
  1382. <div class="block-field">
  1383. <label class="block-field-label">Section Title</label>
  1384. <input type="text" class="block-field-input" value="${escapeHtml(block.data?.title || '')}" oninput="updateBlockData(${blockIndex}, 'title', this.value)">
  1385. </div>
  1386. <div class="block-field">
  1387. <label class="block-field-label">Section Subtitle</label>
  1388. <input type="text" class="block-field-input" value="${escapeHtml(block.data?.subtitle || '')}" oninput="updateBlockData(${blockIndex}, 'subtitle', this.value)">
  1389. </div>
  1390. </div>
  1391. <div class="form-row">
  1392. <div class="block-field">
  1393. <label class="block-field-label">Video URL</label>
  1394. <input type="text" data-media="video" class="block-field-input" value="${escapeHtml(block.data?.url || '')}" oninput="updateBlockData(${blockIndex}, 'url', this.value)" placeholder="YouTube, Facebook, or direct video URL">
  1395. </div>
  1396. <div class="block-field">
  1397. <label class="block-field-label">Video Type</label>
  1398. <select class="block-field-input" onchange="updateBlockData(${blockIndex}, 'type', this.value)">
  1399. <option value="url" ${block.data?.type === 'url' ? 'selected' : ''}>Direct URL</option>
  1400. <option value="youtube" ${block.data?.type === 'youtube' ? 'selected' : ''}>YouTube</option>
  1401. <option value="facebook" ${block.data?.type === 'facebook' ? 'selected' : ''}>Facebook</option>
  1402. </select>
  1403. </div>
  1404. </div>
  1405. <div class="block-field">
  1406. <label class="block-field-label">Poster Image URL (Thumbnail)</label>
  1407. <input type="text" data-media="image" class="block-field-input" value="${escapeHtml(block.data?.posterUrl || '')}" oninput="updateBlockData(${blockIndex}, 'posterUrl', this.value)" placeholder="https://example.com/image.jpg">
  1408. </div>
  1409. <div class="block-field">
  1410. <label class="block-field-label">Caption</label>
  1411. <input type="text" class="block-field-input" value="${escapeHtml(block.data?.caption || '')}" oninput="updateBlockData(${blockIndex}, 'caption', this.value)">
  1412. </div>
  1413. `;
  1414. ​
  1415. case 'gallery':
  1416. return `
  1417. <div class="form-row">
  1418. <div class="block-field">
  1419. <label class="block-field-label">Section Title</label>
  1420. <input type="text" class="block-field-input" value="${escapeHtml(block.data?.title || '')}" oninput="updateBlockData(${blockIndex}, 'title', this.value)">
  1421. </div>
  1422. <div class="block-field">
  1423. <label class="block-field-label">Section Subtitle</label>
  1424. <input type="text" class="block-field-input" value="${escapeHtml(block.data?.subtitle || '')}" oninput="updateBlockData(${blockIndex}, 'subtitle', this.value)">
  1425. </div>
  1426. </div>
  1427. <div class="block-field">
  1428. <label class="block-field-label">Images</label>
  1429. <div class="repeater-items" id="images-items-${blockIndex}">
  1430. ${(block.data?.images || []).map((item, i) => `
  1431. <div class="repeater-item">
  1432. <div class="repeater-item-header" onclick="this.parentElement.classList.toggle('expanded')">
  1433. <span class="repeater-item-title">${escapeHtml(item.alt || 'Image ' + (i + 1))}</span>
  1434. <div class="repeater-item-actions">
  1435. <button type="button" class="btn btn-ghost btn-sm" onclick="event.stopPropagation(); removeRepeaterItem(${blockIndex}, 'images', ${i})" style="color: var(--error);">Remove</button>
  1436. </div>
  1437. </div>
  1438. <div class="repeater-item-content">
  1439. <div class="form-row">
  1440. <div class="block-field">
  1441. <label class="block-field-label">Image URL</label>
  1442. <input type="text" data-media="image" class="block-field-input" value="${escapeHtml(item.url || '')}" oninput="updateRepeaterItem(${blockIndex}, 'images', ${i}, 'url', this.value)" placeholder="https://...">
  1443. </div>
  1444. <div class="block-field">
  1445. <label class="block-field-label">Alt Text</label>
  1446. <input type="text" class="block-field-input" value="${escapeHtml(item.alt || '')}" oninput="updateRepeaterItem(${blockIndex}, 'images', ${i}, 'alt', this.value)">
  1447. </div>
  1448. </div>
  1449. <div class="block-field">
  1450. <label class="block-field-label">Caption</label>
  1451. <input type="text" class="block-field-input" value="${escapeHtml(item.caption || '')}" oninput="updateRepeaterItem(${blockIndex}, 'images', ${i}, 'caption', this.value)">
  1452. </div>
  1453. </div>
  1454. </div>
  1455. `).join('')}
  1456. </div>
  1457. <button type="button" class="repeater-add" onclick="addRepeaterItem(${blockIndex}, 'images', {url: '', alt: '', caption: ''})">
  1458. <svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24" width="16" height="16"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>
  1459. Add Image
  1460. </button>
  1461. </div>
  1462. `;
  1463. ​
  1464. case 'faq':
  1465. return `
  1466. <div class="form-row">
  1467. <div class="block-field">
  1468. <label class="block-field-label">Section Title</label>
  1469. <input type="text" class="block-field-input" value="${escapeHtml(block.data?.title || '')}" oninput="updateBlockData(${blockIndex}, 'title', this.value)">
  1470. </div>
  1471. <div class="block-field">
  1472. <label class="block-field-label">Section Subtitle</label>
  1473. <input type="text" class="block-field-input" value="${escapeHtml(block.data?.subtitle || '')}" oninput="updateBlockData(${blockIndex}, 'subtitle', this.value)">
  1474. </div>
  1475. </div>
  1476. <div class="block-field">
  1477. <label class="block-field-label">FAQ Items</label>
  1478. <div class="repeater-items" id="items-items-${blockIndex}">
  1479. ${(block.data?.items || []).map((item, i) => `
  1480. <div class="repeater-item">
  1481. <div class="repeater-item-header" onclick="this.parentElement.classList.toggle('expanded')">
  1482. <span class="repeater-item-title">${escapeHtml(item.question || 'Question ' + (i + 1))}</span>
  1483. <div class="repeater-item-actions">
  1484. <button type="button" class="btn btn-ghost btn-sm" onclick="event.stopPropagation(); removeRepeaterItem(${blockIndex}, 'items', ${i})" style="color: var(--error);">Remove</button>
  1485. </div>
  1486. </div>
  1487. <div class="repeater-item-content">
  1488. <div class="block-field">
  1489. <label class="block-field-label">Question</label>
  1490. <input type="text" class="block-field-input" value="${escapeHtml(item.question || '')}" oninput="updateRepeaterItem(${blockIndex}, 'items', ${i}, 'question', this.value)">
  1491. </div>
  1492. <div class="block-field">
  1493. <label class="block-field-label">Answer</label>
  1494. <textarea data-rich="1" class="block-field-input block-field-textarea" oninput="updateRepeaterItem(${blockIndex}, 'items', ${i}, 'answer', this.value)">${escapeHtml(item.answer || '')}</textarea>
  1495. </div>
  1496. </div>
  1497. </div>
  1498. `).join('')}
  1499. </div>
  1500. <button type="button" class="repeater-add" onclick="addRepeaterItem(${blockIndex}, 'items', {question: '', answer: ''})">
  1501. <svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24" width="16" height="16"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>
  1502. Add Question
  1503. </button>
  1504. </div>
  1505. ${bfCheck(blockIndex, 'schema', 'Add FAQ rich results for Google (schema.org FAQPage)', block.data?.schema !== false)}
  1506. ${bfHint('Questions from all FAQ blocks on the page are combined into one FAQPage entry.')}
  1507. `;
  1508. ​
  1509. case 'team':
  1510. return `
  1511. <div class="form-row">
  1512. <div class="block-field">
  1513. <label class="block-field-label">Section Title</label>
  1514. <input type="text" class="block-field-input" value="${escapeHtml(block.data?.title || '')}" oninput="updateBlockData(${blockIndex}, 'title', this.value)">
  1515. </div>
  1516. <div class="block-field">
  1517. <label class="block-field-label">Section Subtitle</label>
  1518. <input type="text" class="block-field-input" value="${escapeHtml(block.data?.subtitle || '')}" oninput="updateBlockData(${blockIndex}, 'subtitle', this.value)">
  1519. </div>
  1520. </div>
  1521. <div class="block-field">
  1522. <label class="block-field-label">Team Members</label>
  1523. <div class="repeater-items" id="members-items-${blockIndex}">
  1524. ${(block.data?.members || []).map((member, i) => `
  1525. <div class="repeater-item">
  1526. <div class="repeater-item-header" onclick="this.parentElement.classList.toggle('expanded')">
  1527. <span class="repeater-item-title">${escapeHtml(member.name || 'Team Member ' + (i + 1))}</span>
  1528. <div class="repeater-item-actions">
  1529. <button type="button" class="btn btn-ghost btn-sm" onclick="event.stopPropagation(); removeRepeaterItem(${blockIndex}, 'members', ${i})" style="color: var(--error);">Remove</button>
  1530. </div>
  1531. </div>
  1532. <div class="repeater-item-content">
  1533. <div class="form-row">
  1534. <div class="block-field">
  1535. <label class="block-field-label">Name</label>
  1536. <input type="text" class="block-field-input" value="${escapeHtml(member.name || '')}" oninput="updateRepeaterItem(${blockIndex}, 'members', ${i}, 'name', this.value)">
  1537. </div>
  1538. <div class="block-field">
  1539. <label class="block-field-label">Role</label>
  1540. <input type="text" class="block-field-input" value="${escapeHtml(member.role || '')}" oninput="updateRepeaterItem(${blockIndex}, 'members', ${i}, 'role', this.value)">
  1541. </div>
  1542. </div>
  1543. <div class="form-row">
  1544. <div class="block-field">
  1545. <label class="block-field-label">Photo URL</label>
  1546. <input type="text" data-media="image" class="block-field-input" value="${escapeHtml(member.image || '')}" oninput="updateRepeaterItem(${blockIndex}, 'members', ${i}, 'image', this.value)" placeholder="https://... (optional)">
  1547. </div>
  1548. <div class="block-field">
  1549. <label class="block-field-label">Initials (if no photo)</label>
  1550. <input type="text" class="block-field-input" value="${escapeHtml(member.initials || '')}" oninput="updateRepeaterItem(${blockIndex}, 'members', ${i}, 'initials', this.value)" maxlength="2">
  1551. </div>
  1552. </div>
  1553. <div class="block-field">
  1554. <label class="block-field-label">Bio</label>
  1555. <textarea data-rich="1" class="block-field-input block-field-textarea" oninput="updateRepeaterItem(${blockIndex}, 'members', ${i}, 'bio', this.value)">${escapeHtml(member.bio || '')}</textarea>
  1556. </div>
  1557. <div class="block-field">
  1558. <label class="block-field-label">Social Links (JSON)</label>
  1559. <textarea class="block-field-input block-field-textarea" oninput="try { updateRepeaterItem(${blockIndex}, 'members', ${i}, 'social', JSON.parse(this.value)); } catch(e) {}" placeholder='[{"platform": "twitter", "url": "#"}]'>${escapeHtml(JSON.stringify(member.social || []))}</textarea>
  1560. </div>
  1561. </div>
  1562. </div>
  1563. `).join('')}
  1564. </div>
  1565. <button type="button" class="repeater-add" onclick="addRepeaterItem(${blockIndex}, 'members', {name: '', role: '', initials: '', image: '', bio: '', social: []})">
  1566. <svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24" width="16" height="16"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>
  1567. Add Team Member
  1568. </button>
  1569. </div>
  1570. `;
  1571. ​
  1572. default:
  1573. if (BLOCK_EXTENSIONS[block.type]) return BLOCK_EXTENSIONS[block.type].fields(block, blockIndex);
  1574. return '<p style="color: var(--text-muted);">This block type isn\'t supported.</p>';
  1575. }
  1576. }
  1577. ​
  1578. function renderLinkInput(value, callback) {
  1579. const id = 'link-' + Math.random().toString(36).substr(2, 9);
  1580. return '<div class="link-input-wrapper">' +
  1581. '<input type="text" class="block-field-input" id="' + id + '" value="' + escapeHtml(value) + '" oninput="' + callback.replace('VALUE', 'this.value') + '">' +
  1582. '<button type="button" class="link-page-btn" onclick="showLinkSelector(\'' + id + '\', \'' + callback.replace(/'/g, "\\'") + '\')" title="Select page">' +
  1583. '<svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24" width="16" height="16"><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"/><polyline points="15 3 21 3 21 9"/><line x1="10" y1="14" x2="21" y2="3"/></svg>' +
  1584. '</button>' +
  1585. '</div>';
  1586. }
  1587. ​
  1588. function updateBlockData(blockIndex, key, value) {
  1589. if (!blocks[blockIndex].data) blocks[blockIndex].data = {};
  1590. blocks[blockIndex].data[key] = value;
  1591. schedulePreviewUpdate();
  1592. }
  1593. ​
  1594. function updateRepeaterItem(blockIndex, key, itemIndex, field, value) {
  1595. if (!blocks[blockIndex].data) blocks[blockIndex].data = {};
  1596. if (!blocks[blockIndex].data[key]) blocks[blockIndex].data[key] = [];
  1597. if (!blocks[blockIndex].data[key][itemIndex]) blocks[blockIndex].data[key][itemIndex] = {};
  1598. blocks[blockIndex].data[key][itemIndex][field] = value;
  1599. schedulePreviewUpdate();
  1600. }
  1601. ​
  1602. function addRepeaterItem(blockIndex, key, defaultItem) {
  1603. if (!blocks[blockIndex].data) blocks[blockIndex].data = {};
  1604. if (!blocks[blockIndex].data[key]) blocks[blockIndex].data[key] = [];
  1605. blocks[blockIndex].data[key].push(JSON.parse(JSON.stringify(defaultItem)));
  1606. schedulePreviewUpdate();
  1607. renderBlocks();
  1608. const added = document.querySelector(`.block-item[data-index="${blockIndex}"] .repeater-items[data-key="${key}"] > .repeater-item:last-child`);
  1609. if (added) {
  1610. added.classList.add('expanded');
  1611. const field = added.querySelector('input, textarea, select');
  1612. if (field) field.focus({ preventScroll: true });
  1613. added.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
  1614. }
  1615. }
  1616. ​
  1617. function removeRepeaterItem(blockIndex, key, itemIndex) {
  1618. blocks[blockIndex].data[key].splice(itemIndex, 1);
  1619. renderBlocks();
  1620. schedulePreviewUpdate();
  1621. }
  1622. ​
  1623. function toggleBlock(index) {
  1624. const items = document.querySelectorAll('.block-item');
  1625. items[index]?.classList.toggle('expanded');
  1626. }
  1627. ​
  1628. function expandAllBlocks() {
  1629. document.querySelectorAll('.block-item').forEach(el => el.classList.add('expanded'));
  1630. }
  1631. ​
  1632. function collapseAllBlocks() {
  1633. document.querySelectorAll('.block-item').forEach(el => el.classList.remove('expanded'));
  1634. }
  1635. ​
  1636. function addBlock(type) {
  1637. const defaultId = getDefaultBlockId(type);
  1638. const newBlock = {
  1639. id: defaultId,
  1640. type: type,
  1641. data: getDefaultBlockData(type)
  1642. };
  1643. blocks.push(newBlock);
  1644. renderBlocks();
  1645. hideAddBlockModal();
  1646. schedulePreviewUpdate();
  1647. ​
  1648. setTimeout(() => {
  1649. const items = document.querySelectorAll('.block-item');
  1650. items[items.length - 1]?.classList.add('expanded');
  1651. items[items.length - 1]?.scrollIntoView({ behavior: 'smooth', block: 'center' });
  1652. }, 50);
  1653. }
  1654. ​
  1655. function getDefaultBlockId(type) {
  1656. const baseName = type;
  1657. const existingIds = blocks
  1658. .filter(b => b.type === type && b.id)
  1659. .map(b => b.id);
  1660. ​
  1661. if (existingIds.length === 0) {
  1662. return baseName;
  1663. }
  1664. ​
  1665. let counter = 2;
  1666. while (existingIds.includes(baseName + '-' + counter)) {
  1667. counter++;
  1668. }
  1669. return baseName + '-' + counter;
  1670. }
  1671. ​
  1672. function getDefaultBlockData(type) {
  1673. switch (type) {
  1674. case 'hero':
  1675. return { badge: '', title: '', subtitle: '', button_primary: '', button_primary_url: '#', button_secondary: '', button_secondary_url: '#' };
  1676. case 'stats':
  1677. return { items: [] };
  1678. case 'features':
  1679. return { title: '', subtitle: '', items: [] };
  1680. case 'testimonials':
  1681. return { title: '', subtitle: '', items: [] };
  1682. case 'pricing':
  1683. return { title: '', subtitle: '', items: [] };
  1684. case 'cta':
  1685. return { title: '', subtitle: '', button_primary: '', button_primary_url: '#', button_secondary: '', button_secondary_url: '#' };
  1686. case 'text':
  1687. return { title: '', content: '' };
  1688. case 'image':
  1689. return { url: '', alt: '', caption: '' };
  1690. case 'image-text':
  1691. return { image_url: '', image_alt: '', title: '', subtitle: '', content: '', image_position: 'left', button_text: '', button_url: '#' };
  1692. case 'product-cards':
  1693. return { title: '', subtitle: '', products: [] };
  1694. case 'video':
  1695. return { title: 'Watch Our Video', subtitle: '', url: 'https://www.youtube.com/watch?v=dQw4w9WgXcQ', type: 'youtube', caption: '' };
  1696. case 'gallery':
  1697. return {
  1698. title: 'Photo Gallery', subtitle: '', images: [
  1699. { url: 'https://placehold.co/800x600', alt: 'Gallery image 1', caption: '' },
  1700. { url: 'https://placehold.co/800x600', alt: 'Gallery image 2', caption: '' },
  1701. { url: 'https://placehold.co/800x600', alt: 'Gallery image 3', caption: '' }
  1702. ]
  1703. };
  1704. case 'faq':
  1705. return {
  1706. title: 'Frequently Asked Questions', subtitle: '', items: [
  1707. { question: 'What do you offer?', answer: 'Write a short, clear answer here.' },
  1708. { question: 'How does it work?', answer: 'Explain the steps in a sentence or two.' },
  1709. { question: 'How much does it cost?', answer: 'Mention your prices, or where to find them.' }
  1710. ]
  1711. };
  1712. case 'team':
  1713. return {
  1714. title: 'Meet Our Team', subtitle: '', members: [
  1715. { name: 'John Doe', role: 'CEO & Founder', initials: 'JD', image: '', bio: '', social: [{ platform: 'twitter', url: '#' }, { platform: 'linkedin', url: '#' }] },
  1716. { name: 'Jane Smith', role: 'Head of Design', initials: 'JS', image: '', bio: '', social: [{ platform: 'twitter', url: '#' }, { platform: 'linkedin', url: '#' }] }
  1717. ]
  1718. };
  1719. case 'audio':
  1720. return { title: 'Listen Now', subtitle: '', url: '', music_link: '', music_link_text: 'Download' };
  1721. case 'newsletter':
  1722. return { title: 'Subscribe to Our Newsletter', subtitle: 'Get new posts and updates by email.', button_text: 'Subscribe', placeholder: 'Enter your email' };
  1723. case 'html':
  1724. return { html: '' };
  1725. default:
  1726. return BLOCK_EXTENSIONS[type] ? BLOCK_EXTENSIONS[type].defaults() : {};
  1727. }
  1728. }
  1729. ​
  1730. function duplicateBlock(index) {
  1731. const newBlock = JSON.parse(JSON.stringify(blocks[index]));
  1732. const baseType = blocks[index].type;
  1733. newBlock.id = getDefaultBlockId(baseType);
  1734. blocks.splice(index + 1, 0, newBlock);
  1735. renderBlocks();
  1736. }
  1737. ​
  1738. function deleteBlock(index) {
  1739. if (confirm('Delete this block?')) {
  1740. blocks.splice(index, 1);
  1741. renderBlocks();
  1742. schedulePreviewUpdate();
  1743. }
  1744. }
  1745. ​
  1746. function moveBlockUp(index) {
  1747. if (index > 0) {
  1748. [blocks[index], blocks[index - 1]] = [blocks[index - 1], blocks[index]];
  1749. renderBlocks();
  1750. }
  1751. }
  1752. ​
  1753. function moveBlockDown(index) {
  1754. if (index < blocks.length - 1) {
  1755. [blocks[index], blocks[index + 1]] = [blocks[index + 1], blocks[index]];
  1756. renderBlocks();
  1757. }
  1758. }
  1759. ​
  1760. function handleDragStart(e) {
  1761. draggedBlock = this;
  1762. this.classList.add('dragging');
  1763. e.dataTransfer.effectAllowed = 'move';
  1764. }
  1765. ​
  1766. function handleDragEnd(e) {
  1767. this.classList.remove('dragging');
  1768. document.querySelectorAll('.block-item').forEach(el => el.classList.remove('drag-over'));
  1769. draggedBlock = null;
  1770. }
  1771. ​
  1772. function handleDragOver(e) {
  1773. e.preventDefault();
  1774. e.dataTransfer.dropEffect = 'move';
  1775. this.classList.add('drag-over');
  1776. }
  1777. ​
  1778. function handleDragLeave(e) {
  1779. this.classList.remove('drag-over');
  1780. }
  1781. ​
  1782. function handleDrop(e) {
  1783. e.preventDefault();
  1784. this.classList.remove('drag-over');
  1785. ​
  1786. if (draggedBlock && draggedBlock !== this) {
  1787. const fromIndex = parseInt(draggedBlock.dataset.index);
  1788. const toIndex = parseInt(this.dataset.index);
  1789. ​
  1790. const [movedBlock] = blocks.splice(fromIndex, 1);
  1791. blocks.splice(toIndex, 0, movedBlock);
  1792. renderBlocks();
  1793. schedulePreviewUpdate();
  1794. }
  1795. }
  1796. ​
  1797. ​
  1798. const NE_SVG = {
  1799. plus: '<svg fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" viewBox="0 0 24 24" width="16" height="16"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>',
  1800. up: '<svg fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" viewBox="0 0 24 24" width="16" height="16"><polyline points="18 15 12 9 6 15"/></svg>',
  1801. down: '<svg fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" viewBox="0 0 24 24" width="16" height="16"><polyline points="6 9 12 15 18 9"/></svg>',
  1802. trash: '<svg fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" viewBox="0 0 24 24" width="16" height="16"><polyline points="3 6 5 6 21 6"/><path d="M19 6l-1 14a2 2 0 0 1-2 2H8a2 2 0 0 1-2-2L5 6"/><path d="M10 11v6M14 11v6"/><path d="M9 6V4a1 1 0 0 1 1-1h4a1 1 0 0 1 1 1v2"/></svg>',
  1803. image: '<svg fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" viewBox="0 0 24 24" width="18" height="18"><rect x="3" y="3" width="18" height="18" rx="2"/><circle cx="8.5" cy="8.5" r="1.5"/><path d="M21 15l-5-5L5 21"/></svg>'
  1804. };
  1805. ​
  1806. const SOCIAL_PLATFORMS = [
  1807. ['twitter', 'Twitter/X'], ['github', 'GitHub'], ['linkedin', 'LinkedIn'], ['facebook', 'Facebook'],
  1808. ['instagram', 'Instagram'], ['youtube', 'YouTube'], ['tiktok', 'TikTok'], ['pinterest', 'Pinterest'],
  1809. ['reddit', 'Reddit'], ['discord', 'Discord'], ['snapchat', 'Snapchat'], ['whatsapp', 'Whatsapp'],
  1810. ['telegram', 'Telegram'], ['twitch', 'Twitch'], ['spotify', 'Spotify'], ['medium', 'Medium'],
  1811. ['slack', 'Slack'], ['dribbble', 'Dribbble'], ['mastodon', 'Mastodon'], ['patreon', 'Patreon'],
  1812. ['kofi', 'Ko-Fi'], ['vimeo', 'Vimeo'], ['tumblr', 'Tumblr'], ['stackoverflow', 'Stack Overflow'],
  1813. ['gitlab', 'GitLab'], ['bluesky', 'Bluesky'], ['line', 'Line'], ['wechat', 'WeChat'],
  1814. ['flatlypage', 'FlatlyPage CMS'], ['xing', 'Xing']
  1815. ];
  1816. ​
  1817. let navIconPickerIndex = -1;
  1818. let navButtonIconPickerIndex = -1;
  1819. ​
  1820. function neIconSvg(id) {
  1821. return `<svg class="ne-icon-svg" aria-hidden="true"><use href="${FLATLY_BASE_URL}/assets/icons.svg#${id}"></use></svg>`;
  1822. }
  1823. ​
  1824. function neActions(moveFn, removeFn, index, count) {
  1825. return `
  1826. <div class="ne-actions">
  1827. <button type="button" class="ne-btn" title="Move up" ${index === 0 ? 'disabled' : ''} onclick="${moveFn}(${index}, -1)">${NE_SVG.up}</button>
  1828. <button type="button" class="ne-btn" title="Move down" ${index >= count - 1 ? 'disabled' : ''} onclick="${moveFn}(${index}, 1)">${NE_SVG.down}</button>
  1829. <button type="button" class="ne-btn ne-btn-danger" title="Remove" onclick="${removeFn}(${index})">${NE_SVG.trash}</button>
  1830. </div>`;
  1831. }
  1832. ​
  1833. function neMove(list, index, dir) {
  1834. const target = index + dir;
  1835. if (!list || target < 0 || target >= list.length) return false;
  1836. [list[index], list[target]] = [list[target], list[index]];
  1837. return true;
  1838. }
  1839. ​
  1840. function neEmpty(text) {
  1841. return `<div class="ne-empty">${text}</div>`;
  1842. }
  1843. ​
  1844. function neFocusLast(container, selector) {
  1845. if (!container) return;
  1846. const fields = container.querySelectorAll(selector);
  1847. if (fields.length) fields[fields.length - 1].focus();
  1848. }
  1849. ​
  1850. function renderNavLinks() {
  1851. const container = document.getElementById('navLinksContainer');
  1852. if (!container) return;
  1853. ​
  1854. const hasIcons = Array.isArray(ICON_IDS) && ICON_IDS.length > 0;
  1855. ​
  1856. container.innerHTML = navLinks.length ? `<div class="ne-list">${navLinks.map((link, i) => `
  1857. <div class="ne-item">
  1858. <div class="ne-row">
  1859. ${hasIcons ? `<button type="button" class="ne-icon-btn ${navIconPickerIndex === i ? 'active' : ''}" title="Choose icon" onclick="toggleNavIconPicker(${i})">${link.icon ? neIconSvg(escapeHtml(link.icon)) : NE_SVG.image}</button>` : ''}
  1860. <input type="text" class="ne-input ne-nav-label" value="${escapeHtml(link.label || '')}" placeholder="Label" oninput="navLinks[${i}].label = this.value">
  1861. <input type="text" class="ne-input" value="${escapeHtml(link.url || '')}" placeholder="URL or #anchor" oninput="navLinks[${i}].url = this.value">
  1862. ${neActions('moveNavLink', 'removeNavLink', i, navLinks.length)}
  1863. </div>
  1864. ${hasIcons && navIconPickerIndex === i ? `
  1865. <div class="ne-picker">
  1866. <div class="ne-picker-head">
  1867. <input type="text" class="ne-input" placeholder="Search icons" oninput="filterNavIcons(this.value, this)">
  1868. <button type="button" class="ne-chip" onclick="setNavLinkIcon(${i}, '')">No icon</button>
  1869. </div>
  1870. <div class="ne-picker-grid">
  1871. ${ICON_IDS.map(id => `<button type="button" class="ne-picker-item ${link.icon === id ? 'selected' : ''}" data-icon="${id}" title="${id.replace('icon-', '')}" onclick="setNavLinkIcon(${i}, '${id}')">${neIconSvg(id)}</button>`).join('')}
  1872. </div>
  1873. </div>` : ''}
  1874. </div>`).join('')}</div>` : neEmpty('No links yet. Add your first menu link.');
  1875. ​
  1876. if (navIconPickerIndex >= 0) {
  1877. const search = container.querySelector('.ne-picker .ne-input');
  1878. if (search) search.focus();
  1879. }
  1880. }
  1881. ​
  1882. function toggleNavIconPicker(index) {
  1883. navIconPickerIndex = navIconPickerIndex === index ? -1 : index;
  1884. renderNavLinks();
  1885. }
  1886. ​
  1887. function filterNavIcons(query, input) {
  1888. const q = query.trim().toLowerCase();
  1889. const scope = input && input.closest(".ne-picker") ? input.closest(".ne-picker") : document;
  1890. scope.querySelectorAll(".ne-picker-item").forEach(btn => {
  1891. btn.style.display = !q || btn.dataset.icon.includes(q) ? "" : "none";
  1892. });
  1893. }
  1894. ​
  1895. function setNavLinkIcon(index, icon) {
  1896. if (!navLinks[index]) return;
  1897. if (icon) {
  1898. navLinks[index].icon = icon;
  1899. } else {
  1900. delete navLinks[index].icon;
  1901. }
  1902. navIconPickerIndex = -1;
  1903. renderNavLinks();
  1904. }
  1905. ​
  1906. function addNavLink() {
  1907. navLinks.push({ label: '', url: '#' });
  1908. navIconPickerIndex = -1;
  1909. renderNavLinks();
  1910. neFocusLast(document.getElementById('navLinksContainer'), '.ne-nav-label');
  1911. }
  1912. ​
  1913. function removeNavLink(index) {
  1914. navLinks.splice(index, 1);
  1915. navIconPickerIndex = -1;
  1916. renderNavLinks();
  1917. }
  1918. ​
  1919. function moveNavLink(index, dir) {
  1920. if (neMove(navLinks, index, dir)) {
  1921. navIconPickerIndex = -1;
  1922. renderNavLinks();
  1923. }
  1924. }
  1925. ​
  1926. function renderNavButtons() {
  1927. const container = document.getElementById('navButtonsContainer');
  1928. if (!container) return;
  1929. ​
  1930. const hasIcons = Array.isArray(ICON_IDS) && ICON_IDS.length > 0;
  1931. ​
  1932. container.innerHTML = navButtons.length ? `<div class="ne-list">${navButtons.map((btn, i) => `
  1933. <div class="ne-item">
  1934. <div class="ne-row">
  1935. ${hasIcons ? `<button type="button" class="ne-icon-btn ${navButtonIconPickerIndex === i ? 'active' : ''}" title="Choose icon" onclick="toggleNavButtonIconPicker(${i})">${btn.icon ? neIconSvg(escapeHtml(btn.icon)) : NE_SVG.image}</button>` : ''}
  1936. <input type="text" class="ne-input ne-btn-label" value="${escapeHtml(btn.label || '')}" placeholder="Label" oninput="navButtons[${i}].label = this.value">
  1937. <input type="text" class="ne-input" value="${escapeHtml(btn.url || '')}" placeholder="URL" oninput="navButtons[${i}].url = this.value">
  1938. <select class="ne-input ne-select" onchange="navButtons[${i}].style = this.value">
  1939. <option value="ghost" ${btn.style === 'ghost' ? 'selected' : ''}>Ghost</option>
  1940. <option value="primary" ${btn.style === 'primary' ? 'selected' : ''}>Primary</option>
  1941. <option value="outline" ${btn.style === 'outline' ? 'selected' : ''}>Outline</option>
  1942. </select>
  1943. ${neActions('moveNavButton', 'removeNavButton', i, navButtons.length)}
  1944. </div>
  1945. ${hasIcons && navButtonIconPickerIndex === i ? `
  1946. <div class="ne-picker">
  1947. <div class="ne-picker-head">
  1948. <input type="text" class="ne-input" placeholder="Search icons" oninput="filterNavIcons(this.value, this)">
  1949. <button type="button" class="ne-chip" onclick="setNavButtonIcon(${i}, '')">No icon</button>
  1950. </div>
  1951. <div class="ne-picker-grid">
  1952. ${ICON_IDS.map(id => `<button type="button" class="ne-picker-item ${btn.icon === id ? 'selected' : ''}" data-icon="${id}" title="${id.replace('icon-', '')}" onclick="setNavButtonIcon(${i}, '${id}')">${neIconSvg(id)}</button>`).join('')}
  1953. </div>
  1954. </div>` : ''}
  1955. </div>`).join('')}</div>` : neEmpty('No buttons yet.');
  1956. ​
  1957. if (navButtonIconPickerIndex >= 0) {
  1958. const search = container.querySelector('.ne-picker .ne-input');
  1959. if (search) search.focus();
  1960. }
  1961. }
  1962. ​
  1963. function toggleNavButtonIconPicker(index) {
  1964. navButtonIconPickerIndex = navButtonIconPickerIndex === index ? -1 : index;
  1965. renderNavButtons();
  1966. }
  1967. ​
  1968. function setNavButtonIcon(index, icon) {
  1969. if (!navButtons[index]) return;
  1970. if (icon) {
  1971. navButtons[index].icon = icon;
  1972. } else {
  1973. delete navButtons[index].icon;
  1974. }
  1975. navButtonIconPickerIndex = -1;
  1976. renderNavButtons();
  1977. }
  1978. ​
  1979. function addNavButton() {
  1980. navButtons.push({ label: '', url: '#', style: 'ghost' });
  1981. navButtonIconPickerIndex = -1;
  1982. renderNavButtons();
  1983. neFocusLast(document.getElementById('navButtonsContainer'), '.ne-btn-label');
  1984. }
  1985. ​
  1986. function removeNavButton(index) {
  1987. navButtons.splice(index, 1);
  1988. navButtonIconPickerIndex = -1;
  1989. renderNavButtons();
  1990. }
  1991. ​
  1992. function moveNavButton(index, dir) {
  1993. if (neMove(navButtons, index, dir)) {
  1994. navButtonIconPickerIndex = -1;
  1995. renderNavButtons();
  1996. }
  1997. }
  1998. ​
  1999. function ensureFooterColumn(colIndex) {
  2000. if (!footerData.columns) footerData.columns = [];
  2001. if (!footerData.columns[colIndex]) footerData.columns[colIndex] = { title: '', links: [] };
  2002. if (!footerData.columns[colIndex].links) footerData.columns[colIndex].links = [];
  2003. return footerData.columns[colIndex];
  2004. }
  2005. ​
  2006. function updateFooterColumnTitle(colIndex, value) {
  2007. ensureFooterColumn(colIndex).title = value;
  2008. updateFooterData();
  2009. }
  2010. ​
  2011. function updateFooterColumnLinkLabel(colIndex, linkIndex, value) {
  2012. const col = ensureFooterColumn(colIndex);
  2013. if (!col.links[linkIndex]) col.links[linkIndex] = { label: '', url: '' };
  2014. col.links[linkIndex].label = value;
  2015. updateFooterData();
  2016. }
  2017. ​
  2018. function updateFooterColumnLinkUrl(colIndex, linkIndex, value) {
  2019. const col = ensureFooterColumn(colIndex);
  2020. if (!col.links[linkIndex]) col.links[linkIndex] = { label: '', url: '' };
  2021. col.links[linkIndex].url = value;
  2022. updateFooterData();
  2023. }
  2024. ​
  2025. function renderFooterColumns() {
  2026. const container = document.getElementById('footerColumnsContainer');
  2027. if (!container) return;
  2028. if (!footerData.columns) footerData.columns = [];
  2029. ​
  2030. container.innerHTML = footerData.columns.length ? `<div class="ne-columns">${footerData.columns.map((col, i) => {
  2031. if (!col) col = { title: '', links: [] };
  2032. const links = col.links || [];
  2033. return `
  2034. <div class="ne-card">
  2035. <div class="ne-card-head">
  2036. <input type="text" class="ne-input ne-title ne-col-title" value="${escapeHtml(col.title || '')}" placeholder="Column title" oninput="updateFooterColumnTitle(${i}, this.value)">
  2037. ${neActions('moveFooterColumn', 'removeFooterColumn', i, footerData.columns.length)}
  2038. </div>
  2039. <div class="ne-card-body">
  2040. ${links.length ? links.map((link, j) => `
  2041. <div class="ne-row ne-row-compact">
  2042. <input type="text" class="ne-input ne-link-label" value="${escapeHtml(link.label || '')}" placeholder="Label" oninput="updateFooterColumnLinkLabel(${i}, ${j}, this.value)">
  2043. <input type="text" class="ne-input" value="${escapeHtml(link.url || '')}" placeholder="URL" oninput="updateFooterColumnLinkUrl(${i}, ${j}, this.value)">
  2044. <button type="button" class="ne-btn ne-btn-danger" title="Remove link" onclick="removeFooterColumnLink(${i}, ${j})">${NE_SVG.trash}</button>
  2045. </div>`).join('') : neEmpty('No links in this column.')}
  2046. <button type="button" class="ne-add" onclick="addFooterColumnLink(${i})">${NE_SVG.plus} Add Link</button>
  2047. </div>
  2048. </div>`;
  2049. }).join('')}</div>` : neEmpty('No columns yet. Add your first footer column.');
  2050. }
  2051. ​
  2052. function addFooterColumn() {
  2053. if (!footerData.columns) footerData.columns = [];
  2054. footerData.columns.push({ title: '', links: [] });
  2055. renderFooterColumns();
  2056. updateFooterData();
  2057. neFocusLast(document.getElementById('footerColumnsContainer'), '.ne-col-title');
  2058. }
  2059. ​
  2060. function removeFooterColumn(index) {
  2061. footerData.columns.splice(index, 1);
  2062. renderFooterColumns();
  2063. updateFooterData();
  2064. }
  2065. ​
  2066. function moveFooterColumn(index, dir) {
  2067. if (neMove(footerData.columns, index, dir)) {
  2068. renderFooterColumns();
  2069. updateFooterData();
  2070. }
  2071. }
  2072. ​
  2073. function addFooterColumnLink(colIndex) {
  2074. ensureFooterColumn(colIndex).links.push({ label: '', url: '#' });
  2075. renderFooterColumns();
  2076. updateFooterData();
  2077. const cards = document.querySelectorAll('#footerColumnsContainer .ne-card');
  2078. if (cards[colIndex]) neFocusLast(cards[colIndex], '.ne-link-label');
  2079. }
  2080. ​
  2081. function removeFooterColumnLink(colIndex, linkIndex) {
  2082. footerData.columns[colIndex].links.splice(linkIndex, 1);
  2083. renderFooterColumns();
  2084. updateFooterData();
  2085. }
  2086. ​
  2087. function renderSocialLinks() {
  2088. const container = document.getElementById('socialLinksContainer');
  2089. if (!container) return;
  2090. if (!footerData.social_links) footerData.social_links = [];
  2091. ​
  2092. container.innerHTML = footerData.social_links.length ? `<div class="ne-list">${footerData.social_links.map((link, i) => `
  2093. <div class="ne-item">
  2094. <div class="ne-row">
  2095. <select class="ne-input ne-select" onchange="updateSocialLink(${i}, 'platform', this.value)">
  2096. ${SOCIAL_PLATFORMS.map(([value, label]) => `<option value="${value}" ${link.platform === value ? 'selected' : ''}>${label}</option>`).join('')}
  2097. </select>
  2098. <input type="text" class="ne-input ne-social-url" value="${escapeHtml(link.url || '')}" placeholder="URL" oninput="updateSocialLink(${i}, 'url', this.value)">
  2099. ${neActions('moveSocialLink', 'removeSocialLink', i, footerData.social_links.length)}
  2100. </div>
  2101. </div>`).join('')}</div>` : neEmpty('No social links yet.');
  2102. }
  2103. ​
  2104. function updateSocialLink(index, field, value) {
  2105. if (!footerData.social_links[index]) return;
  2106. footerData.social_links[index][field] = value;
  2107. updateFooterData();
  2108. }
  2109. ​
  2110. function addSocialLink() {
  2111. if (!footerData.social_links) footerData.social_links = [];
  2112. footerData.social_links.push({ platform: 'twitter', url: '#' });
  2113. renderSocialLinks();
  2114. updateFooterData();
  2115. neFocusLast(document.getElementById('socialLinksContainer'), '.ne-social-url');
  2116. }
  2117. ​
  2118. function removeSocialLink(index) {
  2119. footerData.social_links.splice(index, 1);
  2120. renderSocialLinks();
  2121. updateFooterData();
  2122. }
  2123. ​
  2124. function moveSocialLink(index, dir) {
  2125. if (neMove(footerData.social_links, index, dir)) {
  2126. renderSocialLinks();
  2127. updateFooterData();
  2128. }
  2129. }
  2130. ​
  2131. function renderBottomLinks() {
  2132. const container = document.getElementById('bottomLinksContainer');
  2133. if (!container) return;
  2134. if (!footerData.bottom_links) footerData.bottom_links = [];
  2135. ​
  2136. container.innerHTML = footerData.bottom_links.length ? `<div class="ne-list">${footerData.bottom_links.map((link, i) => `
  2137. <div class="ne-item">
  2138. <div class="ne-row">
  2139. <input type="text" class="ne-input ne-bottom-label" value="${escapeHtml(link.label || '')}" placeholder="Label" oninput="updateBottomLink(${i}, 'label', this.value)">
  2140. <input type="text" class="ne-input" value="${escapeHtml(link.url || '')}" placeholder="URL" oninput="updateBottomLink(${i}, 'url', this.value)">
  2141. ${neActions('moveBottomLink', 'removeBottomLink', i, footerData.bottom_links.length)}
  2142. </div>
  2143. </div>`).join('')}</div>` : neEmpty('No bottom links yet.');
  2144. }
  2145. ​
  2146. function updateBottomLink(index, field, value) {
  2147. if (!footerData.bottom_links[index]) return;
  2148. footerData.bottom_links[index][field] = value;
  2149. updateFooterData();
  2150. }
  2151. ​
  2152. function addBottomLink() {
  2153. if (!footerData.bottom_links) footerData.bottom_links = [];
  2154. footerData.bottom_links.push({ label: '', url: '#' });
  2155. renderBottomLinks();
  2156. updateFooterData();
  2157. neFocusLast(document.getElementById('bottomLinksContainer'), '.ne-bottom-label');
  2158. }
  2159. ​
  2160. function removeBottomLink(index) {
  2161. footerData.bottom_links.splice(index, 1);
  2162. renderBottomLinks();
  2163. updateFooterData();
  2164. }
  2165. ​
  2166. function moveBottomLink(index, dir) {
  2167. if (neMove(footerData.bottom_links, index, dir)) {
  2168. renderBottomLinks();
  2169. updateFooterData();
  2170. }
  2171. }
  2172. ​
  2173. function updateFooterData() {
  2174. footerData.brand_description = document.getElementById('footerBrandDesc')?.value || '';
  2175. footerData.copyright = document.getElementById('footerCopyright')?.value || '';
  2176. ​
  2177. if (!footerData.columns) footerData.columns = [];
  2178. if (!footerData.social_links) footerData.social_links = [];
  2179. if (!footerData.bottom_links) footerData.bottom_links = [];
  2180. }
  2181. ​
  2182. function updateAnchorsList() {
  2183. const anchorsList = document.getElementById('anchorsList');
  2184. if (!anchorsList) return;
  2185. ​
  2186. const anchors = new Set(['#']); // Always include top
  2187. ​
  2188. blocks.forEach(block => {
  2189. if (block.id && !block.hidden && ['features', 'testimonials', 'pricing', 'faq', 'team', 'stats', 'cta', 'video', 'gallery', 'audio', 'countdown', 'newsletter', 'logo-cloud', 'steps', 'comparison', 'contact-info', 'tabs', 'carousel', 'quote', 'downloads', 'social-follow'].includes(block.type)) {
  2190. anchors.add(`#${block.id}`);
  2191. }
  2192. });
  2193. ​
  2194. anchorsList.innerHTML = '';
  2195. anchors.forEach(anchor => {
  2196. const btn = document.createElement('button');
  2197. btn.type = 'button';
  2198. btn.className = 'btn btn-secondary btn-sm';
  2199. btn.onclick = () => selectLink(anchor);
  2200. btn.textContent = anchor === '#' ? '#top' : anchor;
  2201. btn.style.margin = '0 8px 8px 0';
  2202. anchorsList.appendChild(btn);
  2203. });
  2204. }
  2205. ​
  2206. function renderInternalPages() {
  2207. const container = document.getElementById('internalPages');
  2208. if (!container) return;
  2209. ​
  2210. container.innerHTML = AVAILABLE_PAGES.map(page => `
  2211. <button type="button" class="btn btn-secondary btn-sm" style="margin: 0 8px 8px 0;" onclick="selectLink('${escapeHtml(page.url)}')">${escapeHtml(page.label)}</button>
  2212. `).join('');
  2213. }
  2214. ​
  2215. function showLinkSelector(inputId, callback) {
  2216. currentLinkCallback = { inputId, callback };
  2217. updateAnchorsList();
  2218. document.getElementById('linkSelectorModal').classList.add('active');
  2219. }
  2220. ​
  2221. function hideLinkSelector() {
  2222. document.getElementById('linkSelectorModal').classList.remove('active');
  2223. currentLinkCallback = null;
  2224. }
  2225. ​
  2226. function selectLink(url) {
  2227. if (currentLinkCallback) {
  2228. const input = document.getElementById(currentLinkCallback.inputId);
  2229. if (input) {
  2230. input.value = url;
  2231. input.dispatchEvent(new Event('input'));
  2232. }
  2233. }
  2234. hideLinkSelector();
  2235. }
  2236. ​
  2237. ​
  2238. function showAddBlockModal() {
  2239. document.getElementById('addBlockModal').classList.add('active');
  2240. if (typeof resetBlockSearch === 'function') resetBlockSearch(true);
  2241. }
  2242. ​
  2243. function hideAddBlockModal() {
  2244. document.getElementById('addBlockModal').classList.remove('active');
  2245. }
  2246. ​
  2247. function confirmDelete(productId, productName) {
  2248. document.getElementById('deleteProductId').value = productId;
  2249. document.getElementById('deleteProductName').textContent = productName;
  2250. document.getElementById('deleteModal').classList.add('active');
  2251. }
  2252. ​
  2253. function hideDeleteModal() {
  2254. document.getElementById('deleteModal').classList.remove('active');
  2255. }
  2256. ​
  2257. ​
  2258. function escapeHtml(text) {
  2259. if (!text) return '';
  2260. const div = document.createElement('div');
  2261. div.textContent = text;
  2262. return div.innerHTML.replace(/"/g, '&quot;').replace(/'/g, '&#39;');
  2263. }
  2264. ​
  2265. document.addEventListener('keydown', function (e) {
  2266. if (e.key === 'Escape') {
  2267. hideAddBlockModal();
  2268. hideDeleteModal();
  2269. hideLinkSelector();
  2270. }
  2271. });
  2272. ​
  2273. document.querySelectorAll('.modal-overlay').forEach(overlay => {
  2274. overlay.addEventListener('click', function (e) {
  2275. if (e.target === this) {
  2276. this.classList.remove('active');
  2277. }
  2278. });
  2279. });
  2280. ​
  2281. ​
  2282. ​
  2283. ​
  2284. ​
  2285. ​
  2286. ​
  2287. ​
  2288. ​
  2289. ​
  2290. ​
  2291. ​
  2292. ​
  2293. ​
  2294. ​
  2295. ​
  2296. ​
  2297. ​
  2298. ​
  2299. ​
  2300. ​
  2301. function renderLanguages() {
  2302. const container = document.getElementById('languagesContainer');
  2303. if (!container || typeof languagesData === 'undefined') return;
  2304. ​
  2305. container.innerHTML = languagesData.length ? `<div class="ne-list">${languagesData.map((lang, i) => `
  2306. <div class="ne-item">
  2307. <div class="ne-row">
  2308. <input type="text" class="ne-input ne-lang-name" value="${escapeHtml(lang.name || '')}" placeholder="Language name, like English or Polski" maxlength="50" oninput="languagesData[${i}].name = this.value">
  2309. <input type="text" class="ne-input ne-lang-code" value="${escapeHtml(lang.code || '')}" placeholder="Code (en, pl, de)" maxlength="11" style="max-width: 140px;" oninput="languagesData[${i}].code = this.value.toLowerCase().trim()">
  2310. ${neActions('moveLanguage', 'removeLanguage', i, languagesData.length)}
  2311. </div>
  2312. </div>`).join('')}</div>` : neEmpty('No languages yet. Add your main language first, then the others.');
  2313. }
  2314. ​
  2315. function addLanguage() {
  2316. languagesData.push({ name: '', code: '' });
  2317. renderLanguages();
  2318. neFocusLast(document.getElementById('languagesContainer'), '.ne-lang-name');
  2319. }
  2320. ​
  2321. function removeLanguage(index) {
  2322. languagesData.splice(index, 1);
  2323. renderLanguages();
  2324. }
  2325. ​
  2326. function moveLanguage(index, dir) {
  2327. if (neMove(languagesData, index, dir)) {
  2328. renderLanguages();
  2329. }
  2330. }(function () {
  2331. const form = document.getElementById('pageForm') || document.getElementById('settingsForm');
  2332. if (!form) return;
  2333. ​
  2334. const isSettings = form.id === 'settingsForm';
  2335. const params = new URLSearchParams(location.search);
  2336. const draftKey = 'flatly-draft:' + location.pathname + ':' + (params.get('tab') || 'index') + ':' + (params.get('id') || '') + ':' + (params.get('lang') || '');
  2337. const pendingKey = 'flatly-draft-pending';
  2338. const skipFields = new Set(['csrf_token', 'action', 'blocks_data', 'nav_links', 'nav_buttons', 'footer_data', 'product_id', 'languages_data', 'analytics_scripts', 'custom_html_location[]', 'custom_html_code[]']);
  2339. let baseline = '';
  2340. let timer = 0;
  2341. let submitting = false;
  2342. ​
  2343. const storage = {
  2344. get(key) {
  2345. try { return localStorage.getItem(key); } catch (e) { return null; }
  2346. },
  2347. set(key, value) {
  2348. try { localStorage.setItem(key, value); } catch (e) {}
  2349. },
  2350. remove(key) {
  2351. try { localStorage.removeItem(key); } catch (e) {}
  2352. }
  2353. };
  2354. ​
  2355. function snapshot() {
  2356. const fields = {};
  2357. Array.from(form.elements).forEach(el => {
  2358. if (!el.name || skipFields.has(el.name) || el.type === 'file' || el.type === 'submit' || el.type === 'button') return;
  2359. fields[el.name] = el.type === 'checkbox' ? el.checked : el.value;
  2360. });
  2361. ​
  2362. const data = { fields };
  2363. if (isSettings) {
  2364. updateFooterData();
  2365. data.navLinks = navLinks;
  2366. data.navButtons = navButtons;
  2367. data.footerData = footerData;
  2368. if (typeof languagesData !== 'undefined') data.languages = languagesData;
  2369. } else {
  2370. data.blocks = blocks;
  2371. }
  2372. return data;
  2373. }
  2374. ​
  2375. function serialize() {
  2376. try {
  2377. return JSON.stringify(snapshot());
  2378. } catch (e) {
  2379. return '';
  2380. }
  2381. }
  2382. ​
  2383. function saveDraft() {
  2384. if (submitting) return;
  2385. const current = serialize();
  2386. if (!current) return;
  2387. if (current === baseline) {
  2388. storage.remove(draftKey);
  2389. return;
  2390. }
  2391. storage.set(draftKey, JSON.stringify({ savedAt: Date.now(), data: JSON.parse(current) }));
  2392. }
  2393. ​
  2394. function scheduleSave() {
  2395. clearTimeout(timer);
  2396. timer = setTimeout(saveDraft, 700);
  2397. }
  2398. ​
  2399. function applyDraft(data) {
  2400. Array.from(form.elements).forEach(el => {
  2401. if (!el.name || !(el.name in data.fields)) return;
  2402. if (el.type === 'checkbox') {
  2403. el.checked = !!data.fields[el.name];
  2404. } else {
  2405. el.value = data.fields[el.name];
  2406. }
  2407. });
  2408. ​
  2409. if (isSettings) {
  2410. navLinks.splice(0, navLinks.length, ...(data.navLinks || []));
  2411. navButtons.splice(0, navButtons.length, ...(data.navButtons || []));
  2412. Object.keys(footerData).forEach(k => delete footerData[k]);
  2413. Object.assign(footerData, data.footerData || {});
  2414. const desc = document.getElementById('footerBrandDesc');
  2415. const copy = document.getElementById('footerCopyright');
  2416. if (desc) desc.value = footerData.brand_description || '';
  2417. if (copy) copy.value = footerData.copyright || '';
  2418. renderNavLinks();
  2419. renderNavButtons();
  2420. renderFooterColumns();
  2421. renderSocialLinks();
  2422. renderBottomLinks();
  2423. if (typeof languagesData !== 'undefined') languagesData.splice(0, languagesData.length, ...(data.languages || languagesData));
  2424. if (typeof renderLanguages === 'function') renderLanguages();
  2425. updateFooterData();
  2426. } else {
  2427. blocks.splice(0, blocks.length, ...(data.blocks || []));
  2428. renderBlocks();
  2429. updateBlockCount();
  2430. if (typeof schedulePreviewUpdate === 'function') schedulePreviewUpdate();
  2431. }
  2432. }
  2433. ​
  2434. function showBanner(draft) {
  2435. const host = document.querySelector('.main-content');
  2436. if (!host) return;
  2437. ​
  2438. const banner = document.createElement('div');
  2439. banner.className = 'draft-banner';
  2440. banner.innerHTML = `
  2441. <div class="draft-banner-text">
  2442. <strong>You have unsaved changes</strong>
  2443. <span>Saved on this device at ${new Date(draft.savedAt).toLocaleString()}. Restore them to pick up where you left off.</span>
  2444. </div>
  2445. <div class="draft-banner-actions">
  2446. <button type="button" class="btn btn-secondary btn-sm" data-draft="discard">Discard</button>
  2447. <button type="button" class="btn btn-primary btn-sm" data-draft="restore">Restore</button>
  2448. </div>`;
  2449. ​
  2450. banner.addEventListener('click', e => {
  2451. const action = e.target.closest('[data-draft]')?.dataset.draft;
  2452. if (!action) return;
  2453. if (action === 'restore') {
  2454. applyDraft(draft.data);
  2455. saveDraft();
  2456. } else {
  2457. storage.remove(draftKey);
  2458. }
  2459. banner.remove();
  2460. });
  2461. ​
  2462. host.insertBefore(banner, host.firstChild);
  2463. }
  2464. ​
  2465. try {
  2466. const pending = sessionStorage.getItem(pendingKey);
  2467. if (pending) {
  2468. sessionStorage.removeItem(pendingKey);
  2469. if (document.querySelector('.message.success')) storage.remove(pending);
  2470. }
  2471. } catch (e) {}
  2472. ​
  2473. form.addEventListener('submit', () => {
  2474. submitting = true;
  2475. clearTimeout(timer);
  2476. try { sessionStorage.setItem(pendingKey, draftKey); } catch (e) {}
  2477. });
  2478. ​
  2479. document.addEventListener('DOMContentLoaded', () => {
  2480. setTimeout(() => {
  2481. baseline = serialize();
  2482. ​
  2483. const raw = storage.get(draftKey);
  2484. if (raw) {
  2485. try {
  2486. const draft = JSON.parse(raw);
  2487. if (draft && draft.data && JSON.stringify(draft.data) !== baseline) {
  2488. showBanner(draft);
  2489. } else {
  2490. storage.remove(draftKey);
  2491. }
  2492. } catch (e) {
  2493. storage.remove(draftKey);
  2494. }
  2495. }
  2496. ​
  2497. ['input', 'change', 'click'].forEach(evt => document.addEventListener(evt, scheduleSave, true));
  2498. window.addEventListener('pagehide', saveDraft);
  2499. document.addEventListener('visibilitychange', () => {
  2500. if (document.visibilityState === 'hidden') saveDraft();
  2501. });
  2502. }, 400);
  2503. });
  2504. })();
  2505. ​
  2506. const MEDIA_ACCEPT = {
  2507. image: 'image/jpeg,image/png,image/gif,image/webp,image/avif,image/x-icon,.jpg,.jpeg,.png,.gif,.webp,.avif,.ico',
  2508. video: 'video/mp4,video/webm,video/ogg,video/quicktime,.mp4,.m4v,.webm,.ogv,.mov',
  2509. audio: 'audio/mpeg,audio/ogg,audio/wav,audio/mp4,audio/aac,audio/flac,audio/webm,.mp3,.ogg,.oga,.opus,.wav,.m4a,.aac,.flac,.weba',
  2510. document: '.pdf,.txt,.csv,.rtf,.doc,.docx,.xls,.xlsx,.ppt,.pptx,.odt,.ods,.odp,.epub,.zip,.7z,.rar'
  2511. };
  2512. ​
  2513. const MEDIA_MIME = {
  2514. mp4: 'video/mp4', m4v: 'video/mp4', webm: 'video/webm', ogv: 'video/ogg', mov: 'video/mp4',
  2515. mp3: 'audio/mpeg', ogg: 'audio/ogg', oga: 'audio/ogg', opus: 'audio/ogg', wav: 'audio/wav',
  2516. m4a: 'audio/mp4', aac: 'audio/aac', flac: 'audio/flac', weba: 'audio/webm'
  2517. };
  2518. ​
  2519. const MEDIA_ICONS = {
  2520. upload: '<svg fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" viewBox="0 0 24 24" width="15" height="15"><path d="M12 16V4M6 10l6-6 6 6"/><path d="M4 20h16"/></svg>',
  2521. library: '<svg fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" viewBox="0 0 24 24" width="15" height="15"><rect x="3" y="3" width="7" height="7" rx="1.5"/><rect x="14" y="3" width="7" height="7" rx="1.5"/><rect x="3" y="14" width="7" height="7" rx="1.5"/><rect x="14" y="14" width="7" height="7" rx="1.5"/></svg>',
  2522. close: '<svg fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" viewBox="0 0 24 24" width="16" height="16"><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></svg>',
  2523. video: '<svg fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" viewBox="0 0 24 24" width="28" height="28"><rect x="3" y="5" width="14" height="14" rx="2"/><path d="M17 9.5l4-2.5v10l-4-2.5"/></svg>',
  2524. audio: '<svg fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" viewBox="0 0 24 24" width="28" height="28"><path d="M9 18V5l12-2v13"/><circle cx="6" cy="18" r="3"/><circle cx="18" cy="16" r="3"/></svg>',
  2525. document: '<svg fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" viewBox="0 0 24 24" width="28" height="28"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/><line x1="8" y1="13" x2="16" y2="13"/><line x1="8" y1="17" x2="13" y2="17"/></svg>'
  2526. };
  2527. ​
  2528. function mediaMime(url, fallback) {
  2529. const path = String(url || '').split('?')[0].split('#')[0];
  2530. const ext = path.includes('.') ? path.split('.').pop().toLowerCase() : '';
  2531. return MEDIA_MIME[ext] || fallback;
  2532. }
  2533. ​
  2534. function mediaFieldFor(node) {
  2535. return node.closest('.media-field');
  2536. }
  2537. ​
  2538. function mediaSetStatus(field, text, state) {
  2539. const status = field.querySelector('.media-status');
  2540. if (!status) return;
  2541. status.textContent = text || '';
  2542. status.className = 'media-status' + (state ? ' ' + state : '');
  2543. clearTimeout(field._statusTimer);
  2544. if (state === 'success' || state === 'error') {
  2545. field._statusTimer = setTimeout(() => {
  2546. status.textContent = '';
  2547. status.className = 'media-status';
  2548. }, state === 'error' ? 8000 : 3000);
  2549. }
  2550. }
  2551. ​
  2552. function mediaApply(field, url) {
  2553. const input = field.querySelector('input[data-media]');
  2554. if (!input) return;
  2555. input.value = url;
  2556. input.dispatchEvent(new Event('input', { bubbles: true }));
  2557. input.dispatchEvent(new Event('change', { bubbles: true }));
  2558. }
  2559. ​
  2560. function mediaUploadFile(field, file) {
  2561. const kind = field.dataset.kind;
  2562. if (field.classList.contains('uploading')) return;
  2563. ​
  2564. const body = new FormData();
  2565. body.append('csrf_token', CSRF_TOKEN);
  2566. body.append('kind', kind);
  2567. body.append('file', file);
  2568. ​
  2569. const bar = field.querySelector('.media-progress-bar');
  2570. field.classList.add('uploading');
  2571. if (bar) bar.style.width = '0%';
  2572. mediaSetStatus(field, 'Uploading ' + file.name + '…', 'busy');
  2573. ​
  2574. const xhr = new XMLHttpRequest();
  2575. xhr.open('POST', FLATLY_BASE_URL + '/admin/upload');
  2576. xhr.upload.onprogress = e => {
  2577. if (e.lengthComputable && bar) bar.style.width = Math.round((e.loaded / e.total) * 100) + '%';
  2578. };
  2579. xhr.onerror = () => {
  2580. field.classList.remove('uploading');
  2581. mediaSetStatus(field, "Couldn't upload, check your connection and try again.", 'error');
  2582. };
  2583. xhr.onload = () => {
  2584. field.classList.remove('uploading');
  2585. let response = null;
  2586. try { response = JSON.parse(xhr.responseText); } catch (e) {}
  2587. if (response && response.success) {
  2588. mediaApply(field, response.url);
  2589. mediaSetStatus(field, 'Uploaded, ' + response.size, 'success');
  2590. } else {
  2591. mediaSetStatus(field, response && response.error ? response.error : 'Upload failed (HTTP ' + xhr.status + ').', 'error');
  2592. }
  2593. };
  2594. xhr.send(body);
  2595. }
  2596. ​
  2597. function mediaPickFile(button) {
  2598. const field = mediaFieldFor(button);
  2599. if (!field) return;
  2600. const picker = document.createElement('input');
  2601. picker.type = 'file';
  2602. picker.accept = MEDIA_ACCEPT[field.dataset.kind] || '';
  2603. picker.onchange = () => {
  2604. if (picker.files && picker.files[0]) mediaUploadFile(field, picker.files[0]);
  2605. };
  2606. picker.click();
  2607. }
  2608. ​
  2609. function mediaLibraryKey(e) {
  2610. if (e.key === 'Escape') mediaCloseLibrary();
  2611. }
  2612. ​
  2613. function mediaCloseLibrary() {
  2614. document.querySelector('.media-modal')?.remove();
  2615. document.removeEventListener('keydown', mediaLibraryKey);
  2616. }
  2617. ​
  2618. function mediaOpenLibrary(button) {
  2619. const field = mediaFieldFor(button);
  2620. if (!field) return;
  2621. const kind = field.dataset.kind;
  2622. ​
  2623. mediaCloseLibrary();
  2624. const modal = document.createElement('div');
  2625. modal.className = 'media-modal';
  2626. modal.innerHTML = `
  2627. <div class="media-modal-box">
  2628. <div class="media-modal-head">
  2629. <h3>Media library</h3>
  2630. <input type="text" class="ne-input" placeholder="Search files" data-media-search>
  2631. <button type="button" class="ne-btn" data-media-close title="Close">${MEDIA_ICONS.close}</button>
  2632. </div>
  2633. <div class="media-modal-body"><div class="media-empty">Loading...</div></div>
  2634. </div>`;
  2635. document.body.appendChild(modal);
  2636. document.addEventListener('keydown', mediaLibraryKey);
  2637. ​
  2638. const body = modal.querySelector('.media-modal-body');
  2639. let items = [];
  2640. ​
  2641. const draw = query => {
  2642. const q = (query || '').trim().toLowerCase();
  2643. const visible = items.filter(item => !q || item.name.toLowerCase().includes(q));
  2644. if (!visible.length) {
  2645. body.innerHTML = `<div class="media-empty">${items.length ? 'No matching files.' : 'No files uploaded yet.'}</div>`;
  2646. return;
  2647. }
  2648. body.innerHTML = `<div class="media-grid">${visible.map(item => `
  2649. <button type="button" class="media-item" data-url="${escapeHtml(item.url)}" title="${escapeHtml(item.name)}">
  2650. <span class="media-thumb">${kind === 'image' ? `<img src="${escapeHtml(flatlyUrl(item.url))}" alt="" loading="lazy">` : MEDIA_ICONS[kind] || ''}</span>
  2651. <span class="media-name">${escapeHtml(item.name)}</span>
  2652. <span class="media-size">${escapeHtml(item.size)}</span>
  2653. </button>`).join('')}</div>`;
  2654. };
  2655. ​
  2656. modal.addEventListener('click', e => {
  2657. if (e.target === modal || e.target.closest('[data-media-close]')) {
  2658. mediaCloseLibrary();
  2659. return;
  2660. }
  2661. const item = e.target.closest('.media-item');
  2662. if (item) {
  2663. mediaApply(field, item.dataset.url);
  2664. mediaCloseLibrary();
  2665. }
  2666. });
  2667. modal.querySelector('[data-media-search]').addEventListener('input', e => draw(e.target.value));
  2668. ​
  2669. fetch(FLATLY_BASE_URL + '/admin/upload?list=' + encodeURIComponent(kind), { credentials: 'same-origin' })
  2670. .then(r => r.json())
  2671. .then(data => {
  2672. if (!data.success) throw new Error(data.error || "Couldn't load your files.");
  2673. items = data.items || [];
  2674. draw('');
  2675. })
  2676. .catch(err => {
  2677. body.innerHTML = `<div class="media-empty">${escapeHtml(err.message)}</div>`;
  2678. });
  2679. }
  2680. ​
  2681. function enhanceMediaInputs(root) {
  2682. (root || document).querySelectorAll('input[data-media]:not([data-media-ready])').forEach(input => {
  2683. const kind = input.dataset.media;
  2684. if (!MEDIA_ACCEPT[kind]) return;
  2685. input.dataset.mediaReady = '1';
  2686. ​
  2687. const field = document.createElement('div');
  2688. field.className = 'media-field';
  2689. field.dataset.kind = kind;
  2690. input.parentNode.insertBefore(field, input);
  2691. ​
  2692. const row = document.createElement('div');
  2693. row.className = 'media-row';
  2694. field.appendChild(row);
  2695. row.appendChild(input);
  2696. ​
  2697. const actions = document.createElement('div');
  2698. actions.className = 'media-actions';
  2699. actions.innerHTML = `
  2700. <button type="button" class="btn btn-secondary btn-sm" onclick="mediaPickFile(this)">${MEDIA_ICONS.upload} Upload</button>
  2701. <button type="button" class="btn btn-secondary btn-sm" onclick="mediaOpenLibrary(this)">${MEDIA_ICONS.library} Library</button>`;
  2702. row.appendChild(actions);
  2703. ​
  2704. field.insertAdjacentHTML('beforeend', '<div class="media-progress"><div class="media-progress-bar"></div></div><div class="media-status"></div>');
  2705. });
  2706. }
  2707. ​
  2708. (function () {
  2709. let frame = 0;
  2710. const schedule = () => {
  2711. if (frame) return;
  2712. frame = requestAnimationFrame(() => {
  2713. frame = 0;
  2714. enhanceMediaInputs(document);
  2715. });
  2716. };
  2717. ​
  2718. const start = () => {
  2719. enhanceMediaInputs(document);
  2720. const host = document.querySelector('.main-content') || document.body;
  2721. new MutationObserver(schedule).observe(host, { childList: true, subtree: true });
  2722. };
  2723. ​
  2724. if (document.readyState === 'loading') {
  2725. document.addEventListener('DOMContentLoaded', start);
  2726. } else {
  2727. start();
  2728. }
  2729. ​
  2730. document.addEventListener('dragover', e => {
  2731. const field = e.target.closest && e.target.closest('.media-field');
  2732. if (!field || !e.dataTransfer || !Array.from(e.dataTransfer.types || []).includes('Files')) return;
  2733. e.preventDefault();
  2734. field.classList.add('dragging');
  2735. });
  2736. ​
  2737. document.addEventListener('dragleave', e => {
  2738. const field = e.target.closest && e.target.closest('.media-field');
  2739. if (field && !field.contains(e.relatedTarget)) field.classList.remove('dragging');
  2740. });
  2741. ​
  2742. document.addEventListener('drop', e => {
  2743. const field = e.target.closest && e.target.closest('.media-field');
  2744. if (!field) return;
  2745. field.classList.remove('dragging');
  2746. const file = e.dataTransfer && e.dataTransfer.files && e.dataTransfer.files[0];
  2747. if (!file) return;
  2748. e.preventDefault();
  2749. mediaUploadFile(field, file);
  2750. });
  2751. })();
  2752. ​
  2753. function switchEditLang(code) {
  2754. const url = new URL(location.href);
  2755. if (code) {
  2756. url.searchParams.set('lang', code);
  2757. } else {
  2758. url.searchParams.delete('lang');
  2759. }
  2760. location.href = url.toString();
  2761. }