diff --git a/webapp/titanembeds/static/js/usercss.js b/webapp/titanembeds/static/js/usercss.js index 489ae7a..978f65b 100644 --- a/webapp/titanembeds/static/js/usercss.js +++ b/webapp/titanembeds/static/js/usercss.js @@ -1,14 +1,7 @@ -/*global $, ace, Materialize, newCSS*/ +/*global $, ace, Materialize, newCSS, CSS_ID*/ (function () { if($("#css_editor").length != 0) { var editor = ace.edit("css_editor"); - editor.commands.addCommand({ - name: 'save', - bindKey: {win: "Ctrl-S", "mac": "Cmd-S"}, - exec: function(editor) { - $('#submit-btn').trigger('click'); - } - }); } function postForm() { @@ -34,12 +27,37 @@ if($("#css_editor").length != 0) { editor.getSession().setMode("ace/mode/css"); editor.setTheme("ace/theme/chrome"); + + editor.commands.addCommand({ + name: 'save', + bindKey: {win: "Ctrl-S", "mac": "Cmd-S"}, + exec: function(editor) { + $('#submit-btn').trigger('click'); + } + }); + editor.on("blur", function () { + update_live_preview(); + }); } $("#submit-btn").click(submitForm); if (!newCSS) { $("#delete-btn").click(delete_css); } + + $(".updateLivePreview").on("blur", function () { + update_live_preview(); + }); + + $("#toggleCSSVar").on("change", function () { + update_live_preview(); + }); + + $("#preview_guildid").keyup(function (event) { + if (event.keyCode == 13) { + change_live_preview(); + } + }); }); function formatCSSVars() { @@ -65,6 +83,8 @@ window.location.href = "edit/" + data.id; } else { Materialize.toast('CSS Updated!', 10000); + update_live_preview(true); + $("#live_preview_warning").hide(); } }); formPost.fail(function () { @@ -89,4 +109,39 @@ } }); } + + function update_live_preview(refresh=false) { + if (refresh) { + $('#iframepreview')[0].contentWindow.location.reload(true); + return; + } + $("#live_preview_warning").show(); + var output = ""; + + if ($("#toggleCSSVar").is(':checked')) { + var cssVars = formatCSSVars(); + output += ":root {"; + for (var key in cssVars) { + if (cssVars.hasOwnProperty(key)) { + output += "--" + key + ":" + cssVars[key] + ";"; + } + } + output += "}"; + } + + if($("#css_editor").length != 0) { + output += editor.getValue(); + } + + $("#iframepreview").contents().find('#user-defined-css').html(output); + } + + function change_live_preview() { + var src = "/embed/" + $("#preview_guildid").val(); + if (!newCSS) { + src += "?css=" + CSS_ID; + } + $("#iframepreview").attr("src", src); + update_live_preview(); + } })(); \ No newline at end of file diff --git a/webapp/titanembeds/templates/embed.html.j2 b/webapp/titanembeds/templates/embed.html.j2 index 3f389aa..4ecd5e4 100644 --- a/webapp/titanembeds/templates/embed.html.j2 +++ b/webapp/titanembeds/templates/embed.html.j2 @@ -19,9 +19,12 @@
Edit your CSS code here
Remove any text and leave this box blank when submitting to not use up your custom css slots.
-