|
| 1 | +import * as ThemeController from "../controllers/theme-controller"; |
| 2 | +import Config from "../config"; |
| 3 | +import * as Notifications from "../elements/notifications"; |
| 4 | +import * as CustomThemePopup from "../popups/custom-theme-popup"; |
| 5 | +import { createErrorMessage } from "../utils/misc"; |
| 6 | +import AnimatedModal from "../utils/animated-modal"; |
| 7 | + |
| 8 | +type State = { |
| 9 | + includeBackground: boolean; |
| 10 | +}; |
| 11 | + |
| 12 | +const state: State = { |
| 13 | + includeBackground: false, |
| 14 | +}; |
| 15 | + |
| 16 | +export function show(): void { |
| 17 | + void modal.show({ |
| 18 | + beforeAnimation: async (m) => { |
| 19 | + (m.querySelector("input[type='checkbox']") as HTMLInputElement).checked = |
| 20 | + false; |
| 21 | + state.includeBackground = false; |
| 22 | + }, |
| 23 | + }); |
| 24 | +} |
| 25 | + |
| 26 | +async function generateUrl(): Promise<string> { |
| 27 | + const newTheme: { |
| 28 | + c: string[]; //colors |
| 29 | + i?: string; //image |
| 30 | + s?: string; //size |
| 31 | + f?: object; //filter |
| 32 | + } = { |
| 33 | + c: ThemeController.colorVars.map( |
| 34 | + (color) => |
| 35 | + $( |
| 36 | + `.pageSettings .customTheme .customThemeEdit #${color}[type='color']` |
| 37 | + ).attr("value") as string |
| 38 | + ), |
| 39 | + }; |
| 40 | + |
| 41 | + if (state.includeBackground) { |
| 42 | + newTheme.i = Config.customBackground; |
| 43 | + newTheme.s = Config.customBackgroundSize; |
| 44 | + newTheme.f = Config.customBackgroundFilter; |
| 45 | + } |
| 46 | + |
| 47 | + return ( |
| 48 | + window.location.origin + "?customTheme=" + btoa(JSON.stringify(newTheme)) |
| 49 | + ); |
| 50 | +} |
| 51 | + |
| 52 | +async function copy(): Promise<void> { |
| 53 | + const url = await generateUrl(); |
| 54 | + |
| 55 | + navigator.clipboard.writeText(url).then( |
| 56 | + function () { |
| 57 | + Notifications.add("URL Copied to clipboard", 1); |
| 58 | + void modal.hide(); |
| 59 | + }, |
| 60 | + function (e) { |
| 61 | + Notifications.add( |
| 62 | + createErrorMessage(e, "Failed to copy to clipboard"), |
| 63 | + -1 |
| 64 | + ); |
| 65 | + Notifications.add( |
| 66 | + "Looks like we couldn't copy the link straight to your clipboard. Please copy it manually.", |
| 67 | + 0, |
| 68 | + { |
| 69 | + duration: 5, |
| 70 | + } |
| 71 | + ); |
| 72 | + void modal.hide(); |
| 73 | + CustomThemePopup.show(url); |
| 74 | + } |
| 75 | + ); |
| 76 | +} |
| 77 | + |
| 78 | +const modal = new AnimatedModal("shareCustomThemeModal", "popups", undefined, { |
| 79 | + setup: (modal): void => { |
| 80 | + modal.querySelector("button")?.addEventListener("click", copy); |
| 81 | + modal |
| 82 | + .querySelector("input[type='checkbox']") |
| 83 | + ?.addEventListener("change", (e) => { |
| 84 | + state.includeBackground = (e.target as HTMLInputElement).checked; |
| 85 | + }); |
| 86 | + }, |
| 87 | +}); |
0 commit comments