From c8a93c28f834a63c15e907a6d1ce277bc8aa60df Mon Sep 17 00:00:00 2001 From: Jack Date: Fri, 25 Sep 2020 20:24:18 +0100 Subject: [PATCH] sharing custom themes now done via a url --- public/js/misc.js | 22 ++++++++++++++++++++++ public/js/script.js | 17 +++++++++++++++++ public/js/settings.js | 23 +++++++++++++++++++---- 3 files changed, 58 insertions(+), 4 deletions(-) diff --git a/public/js/misc.js b/public/js/misc.js index cf889b6be..7362d2b58 100644 --- a/public/js/misc.js +++ b/public/js/misc.js @@ -397,4 +397,26 @@ function getPositionString(number) { numend = "rd"; } return number + numend; +} + +function findGetParameter(parameterName) { + var result = null, + tmp = []; + location.search + .substr(1) + .split("&") + .forEach(function (item) { + tmp = item.split("="); + if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]); + }); + return result; +} + +function objectToQueryString(obj) { + var str = []; + for (var p in obj) + if (obj.hasOwnProperty(p)) { + str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p])); + } + return str.join("&"); } \ No newline at end of file diff --git a/public/js/script.js b/public/js/script.js index 10f7a2741..7ded312da 100644 --- a/public/js/script.js +++ b/public/js/script.js @@ -4764,6 +4764,23 @@ $(document).ready(() => { .removeClass("hidden") .stop(true, true) .animate({ opacity: 1 }, 250, () => { + let theme = findGetParameter("customTheme"); + if (theme !== null) { + try { + theme = theme.split(","); + config.customThemeColors = theme; + showNotification("Custom theme applied", 1000); + } catch (e) { + showNotification( + "Something went wrong. Reverting to default custom colors.", + 3000 + ); + config.customThemeColors = defaultConfig.customThemeColors; + } + setCustomTheme(true); + setCustomThemeInputs(); + applyCustomThemeColors(); + } if (window.location.pathname === "/account") { history.replaceState("/", null, "/"); } else if (window.location.pathname !== "/") { diff --git a/public/js/settings.js b/public/js/settings.js index dc40095fd..5b451c0ec 100644 --- a/public/js/settings.js +++ b/public/js/settings.js @@ -445,12 +445,27 @@ $("#customThemeShareWrapper").click((e) => { } }); -$("#customThemeShare .button").click((e) => { - hideCustomThemeShare(); -}); +// $("#customThemeShare .button").click((e) => { +// hideCustomThemeShare(); +// }); $("#shareCustomThemeButton").click((e) => { - showCustomThemeShare(); + // showCustomThemeShare(); + + let share = []; + $.each( + $(".pageSettings .section.customTheme [type='color']"), + (index, element) => { + share.push($(element).attr("value")); + } + ); + + let url = "https://monkey-type.com?" + objectToQueryString({ customTheme: share }); + navigator.clipboard.writeText(url).then(function() { + showNotification("URL Copied to clipboard", 2000); + }, function(err) { + showNotification("Something went wrong when copying the URL: "+ err, 5000); + }); }); function toggleFavouriteTheme(themename) {