design.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. design.js
  3. The "Design" panel - site-wide styling rather than one element:
  4. theme colours, the two default fonts, page background and the web-font
  5. switch. Changing the accent colour recolours every element that currently
  6. uses the old accent, which is what makes a palette swap actually useful.
  7. */
  8. var WBDesign = (function () {
  9. var bodyEl;
  10. var PRESETS = [
  11. { name: "Sunset", accent: "#f97316", text: "#16181d", bg: "#ffffff" },
  12. { name: "Ember", accent: "#ea580c", text: "#1c1917", bg: "#fffbf7" },
  13. { name: "Ocean", accent: "#0ea5e9", text: "#0f172a", bg: "#ffffff" },
  14. { name: "Forest", accent: "#16a34a", text: "#14201a", bg: "#ffffff" },
  15. { name: "Grape", accent: "#7c3aed", text: "#1b1523", bg: "#ffffff" },
  16. { name: "Carbon", accent: "#f97316", text: "#f5f5f5", bg: "#111214" }
  17. ];
  18. function init() {
  19. bodyEl = document.querySelector("#wb-panel-design .wb-panel-bd");
  20. render();
  21. }
  22. function theme() {
  23. var t = WBModel.get().settings.theme;
  24. if (!t.accent) { t.accent = "#f97316"; }
  25. return t;
  26. }
  27. function render() {
  28. if (!bodyEl) { return; }
  29. WBUI.clear(bodyEl);
  30. var t = theme();
  31. /* ---- presets ---- */
  32. var sec = section("Theme");
  33. var grid = WBUI.el("div", { class: "wb-swatch-grid" });
  34. PRESETS.forEach(function (p) {
  35. var sw = WBUI.el("button", {
  36. class: "wb-swatch" + (p.accent === t.accent && p.bg === t.background ? " active" : ""),
  37. type: "button",
  38. title: p.name,
  39. style: "background:linear-gradient(135deg," + p.accent + " 0 55%," + p.bg + " 55% 100%)"
  40. });
  41. sw.addEventListener("click", function () { applyPreset(p); });
  42. grid.appendChild(sw);
  43. });
  44. sec.appendChild(grid);
  45. bodyEl.appendChild(sec);
  46. /* ---- colours ---- */
  47. var colours = section("Colors");
  48. colours.appendChild(WBUI.field("Accent", WBUI.colorRow(t.accent, function (v) {
  49. recolorAccent(t.accent, v);
  50. t.accent = v;
  51. WBModel.commit("Change accent", "accent");
  52. WBApp.rerenderCanvas();
  53. })));
  54. colours.appendChild(WBUI.field("Page Background", WBUI.colorRow(t.background || "#ffffff", function (v) {
  55. t.background = v;
  56. applyToBody("backgroundColor", v);
  57. })));
  58. colours.appendChild(WBUI.field("Body Text", WBUI.colorRow(t.text || "#16181d", function (v) {
  59. t.text = v;
  60. applyToBody("color", v);
  61. })));
  62. bodyEl.appendChild(colours);
  63. /* ---- fonts ---- */
  64. var fonts = section("Typography");
  65. fonts.appendChild(WBUI.field("Body Font", fontSelect(t.bodyFont, function (v) {
  66. t.bodyFont = v;
  67. applyToBody("fontFamily", v);
  68. })));
  69. fonts.appendChild(WBUI.field("Heading Font", fontSelect(t.headingFont, function (v) {
  70. t.headingFont = v;
  71. applyToHeadings(v);
  72. })));
  73. fonts.appendChild(WBUI.el("div", { class: "wb-row" }, [
  74. WBUI.el("div", { class: "wb-row-text" }, [
  75. WBUI.el("div", { class: "wb-row-label", text: "Load web fonts" }),
  76. WBUI.el("div", { class: "wb-row-desc",
  77. text: "Adds a Google Fonts stylesheet to the published pages for the fonts you used. Turn off to stay fully offline." })
  78. ]),
  79. WBUI.switchControl(WBModel.get().settings.webFonts, function (v) {
  80. WBModel.get().settings.webFonts = v;
  81. WBModel.commit("Toggle web fonts");
  82. WBApp.rerenderCanvas();
  83. })
  84. ]));
  85. bodyEl.appendChild(fonts);
  86. /* ---- page background ---- */
  87. var pageSec = section("This Page");
  88. var page = WBModel.activePage();
  89. pageSec.appendChild(WBUI.field("Background",
  90. WBUI.colorRow(page.root.styles.base.backgroundColor || "", function (v) {
  91. WBModel.setStyle(page.root.id, "backgroundColor", v, "base");
  92. WBModel.commit("Page background", "pagebg");
  93. WBCanvas.refreshStyles();
  94. })));
  95. pageSec.appendChild(WBUI.el("button", {
  96. class: "wb-btn wb-btn-block wb-btn-sm",
  97. type: "button",
  98. html: WBIcon("box", 13) + "<span>Select page body</span>",
  99. onclick: function () { WBApp.selectNode(WBModel.activePage().root.id); }
  100. }));
  101. bodyEl.appendChild(pageSec);
  102. bodyEl.appendChild(WBUI.el("div", {
  103. class: "wb-note",
  104. text: "Theme changes rewrite the styles already on your elements - they are undoable like any other edit."
  105. }));
  106. }
  107. function section(title) {
  108. return WBUI.el("div", { class: "wb-section" }, [
  109. WBUI.el("div", { class: "wb-section-title", text: title })
  110. ]);
  111. }
  112. function fontSelect(current, onChange) {
  113. var opts = WBFonts.map(function (f) { return { value: f.stack, label: f.name }; });
  114. var known = opts.some(function (o) { return o.value === current; });
  115. if (current && !known) { opts.unshift({ value: current, label: "Custom" }); }
  116. return WBUI.selectControl(opts, current, onChange);
  117. }
  118. function applyPreset(p) {
  119. var t = theme();
  120. recolorAccent(t.accent, p.accent);
  121. t.accent = p.accent;
  122. t.background = p.bg;
  123. t.text = p.text;
  124. for (var i = 0; i < WBModel.get().pages.length; i++) {
  125. var root = WBModel.get().pages[i].root;
  126. root.styles.base.backgroundColor = p.bg;
  127. root.styles.base.color = p.text;
  128. }
  129. WBModel.commit("Apply theme");
  130. WBApp.rerenderCanvas();
  131. render();
  132. }
  133. function applyToBody(key, value) {
  134. var pages = WBModel.get().pages;
  135. for (var i = 0; i < pages.length; i++) {
  136. pages[i].root.styles.base[key] = value;
  137. }
  138. WBModel.commit("Site " + key, "sitebody:" + key);
  139. WBCanvas.refreshStyles();
  140. }
  141. function applyToHeadings(value) {
  142. var pages = WBModel.get().pages;
  143. function walk(n) {
  144. if (n.type === "heading") { n.styles.base.fontFamily = value; }
  145. for (var i = 0; i < n.children.length; i++) { walk(n.children[i]); }
  146. }
  147. for (var p = 0; p < pages.length; p++) { walk(pages[p].root); }
  148. WBModel.commit("Heading font", "headingfont");
  149. WBApp.rerenderCanvas();
  150. }
  151. /* Swap one colour for another everywhere it is used as a style value. */
  152. function recolorAccent(from, to) {
  153. if (!from || !to || from.toLowerCase() === to.toLowerCase()) { return; }
  154. var keys = ["color", "backgroundColor", "borderColor", "borderTopColor",
  155. "borderBottomColor", "outlineColor", "fill", "stroke", "border"];
  156. var pages = WBModel.get().pages;
  157. function fix(styleObj) {
  158. for (var i = 0; i < keys.length; i++) {
  159. var k = keys[i];
  160. if (typeof styleObj[k] === "string" &&
  161. styleObj[k].toLowerCase().indexOf(from.toLowerCase()) >= 0) {
  162. styleObj[k] = styleObj[k].replace(new RegExp(from, "ig"), to);
  163. }
  164. }
  165. }
  166. function walk(n) {
  167. fix(n.styles.base); fix(n.styles.tablet); fix(n.styles.mobile);
  168. for (var i = 0; i < n.children.length; i++) { walk(n.children[i]); }
  169. }
  170. for (var p = 0; p < pages.length; p++) { walk(pages[p].root); }
  171. }
  172. return { init: init, render: render };
  173. })();