|
| 1 | +/* devtemDesign builder: shared helpers (load before builder.js and projects.js) */ |
| 2 | +(function (w) { |
| 3 | + 'use strict'; |
| 4 | + |
| 5 | + var VERSION = '0.1.0'; |
| 6 | + var BASE = 'https://cdn.jsdelivr.net/npm/devtemdesign@' + VERSION + '/'; |
| 7 | + var LS_KEY = 'dtd_projects'; |
| 8 | + |
| 9 | + /* Old 0.0.x keys -> 0.1.0 keys. The editor can keep using the old names. */ |
| 10 | + var RENAMES = [ |
| 11 | + ['heroTag', 'heroBadge'], |
| 12 | + ['featuresSectionLabel', 'featuresLabel'], |
| 13 | + ['featuresSectionTitle', 'featuresTitle'] |
| 14 | + ]; |
| 15 | + |
| 16 | + function loadProjects() { |
| 17 | + try { return JSON.parse(localStorage.getItem(LS_KEY) || '[]'); } catch (e) { return []; } |
| 18 | + } |
| 19 | + function saveProjects(arr) { |
| 20 | + try { localStorage.setItem(LS_KEY, JSON.stringify(arr)); } catch (e) {} |
| 21 | + } |
| 22 | + |
| 23 | + function esc(s) { |
| 24 | + return String(s == null ? '' : s).replace(/&/g, '&').replace(/"/g, '"').replace(/</g, '<').replace(/>/g, '>'); |
| 25 | + } |
| 26 | + |
| 27 | + /* Returns a copy that the 0.1.0 engine understands. Safe to run on any config. */ |
| 28 | + function migrate(c) { |
| 29 | + var o = JSON.parse(JSON.stringify(c || {})); |
| 30 | + RENAMES.forEach(function (p) { |
| 31 | + if (o[p[0]] != null) { |
| 32 | + if (o[p[1]] == null) o[p[1]] = o[p[0]]; |
| 33 | + delete o[p[0]]; |
| 34 | + } |
| 35 | + }); |
| 36 | + if (o.footer && o.footer.copy == null && o.footerCopy) o.footer.copy = o.footerCopy; |
| 37 | + return o; |
| 38 | + } |
| 39 | + |
| 40 | + /* preview = true: content is shown immediately (no reveal), and the page |
| 41 | + accepts postMessage({ dtdCfg }) so the builder can update without a reload. */ |
| 42 | + function buildHTML(c, preview) { |
| 43 | + var cfg = migrate(c); |
| 44 | + var json = JSON.stringify(cfg, null, 2).replace(/</g, '\\u003c'); // blocks </script> breakouts |
| 45 | + var head = '<!DOCTYPE html>\n<html lang="en">\n<head>\n <meta charset="UTF-8">\n' + |
| 46 | + ' <meta name="viewport" content="width=device-width, initial-scale=1">\n' + |
| 47 | + ' <title>' + esc(cfg.logoName || 'Page') + '</title>\n' + |
| 48 | + ' <link rel="stylesheet" href="' + BASE + 'style.min.css">\n' + |
| 49 | + ' <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css">\n' + |
| 50 | + (preview ? ' <style>.reveal{opacity:1!important;animation:none!important}</style>\n' : '') + |
| 51 | + ' <script src="' + BASE + 'script.min.js"><\/script>\n</head>\n'; |
| 52 | + var body = '<body>\n <div id="app"></div>\n <script>\n devtemDesign("app", ' + json + ');\n' + |
| 53 | + (preview |
| 54 | + ? ' addEventListener("message", function (e) {\n' + |
| 55 | + ' if (e.source !== parent || !e.data || !e.data.dtdCfg) return;\n' + |
| 56 | + ' var y = scrollY; devtemDesign("app", e.data.dtdCfg); scrollTo(0, y);\n' + |
| 57 | + ' });\n' |
| 58 | + : '') + |
| 59 | + ' <\/script>\n</body>\n</html>'; |
| 60 | + return head + body; |
| 61 | + } |
| 62 | + |
| 63 | + w.DTD = { VERSION: VERSION, loadProjects: loadProjects, saveProjects: saveProjects, esc: esc, migrate: migrate, buildHTML: buildHTML }; |
| 64 | +})(window); |
0 commit comments