From bb1a5dc91791a1eca4668cae5af4c75676adecce Mon Sep 17 00:00:00 2001 From: Joseph Izang Date: Tue, 7 Dec 2021 00:38:10 +0100 Subject: [PATCH] prep js logic for reducer --- apps/remix-ide/src/app/tabs/theme-module.js | 2 + .../src/lib/remix-ui-theme-module.tsx | 176 ++++++++++++++---- .../src/reducers/themeModuleReducers.ts | 0 3 files changed, 139 insertions(+), 39 deletions(-) create mode 100644 libs/remix-ui/theme-module/src/reducers/themeModuleReducers.ts diff --git a/apps/remix-ide/src/app/tabs/theme-module.js b/apps/remix-ide/src/app/tabs/theme-module.js index 8b80ab6654..f40746bfa7 100644 --- a/apps/remix-ide/src/app/tabs/theme-module.js +++ b/apps/remix-ide/src/app/tabs/theme-module.js @@ -43,6 +43,7 @@ export class ThemeModule extends Plugin { theme.url = window.location.origin + window.location.pathname + theme.url return { ...acc, [theme.name]: theme } }, {}) + this._paq = _paq let queryTheme = (new QueryParams()).get().theme queryTheme = this.themes[queryTheme] ? queryTheme : null let currentTheme = this._deps.config.get('settings/theme') @@ -83,6 +84,7 @@ export class ThemeModule extends Plugin { * Init the theme */ initTheme (callback) { // callback is setTimeOut in app.js which is always passed + if (callback) this.initCallback = callback if (this.active) { const nextTheme = this.themes[this.active] // Theme document.documentElement.style.setProperty('--theme', nextTheme.quality) diff --git a/libs/remix-ui/theme-module/src/lib/remix-ui-theme-module.tsx b/libs/remix-ui/theme-module/src/lib/remix-ui-theme-module.tsx index cf79d113e7..aa780c6a62 100644 --- a/libs/remix-ui/theme-module/src/lib/remix-ui-theme-module.tsx +++ b/libs/remix-ui/theme-module/src/lib/remix-ui-theme-module.tsx @@ -1,68 +1,166 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ -import React, { useEffect, useRef, useState } from 'react' -import { ThemeModule } from '../../types/theme-module' -import './remix-ui-theme-module.module.css' +import React, { useEffect, useRef, useState } from 'react'; +import { Theme, ThemeModule } from '../../types/theme-module'; +import './remix-ui-theme-module.module.css'; /* eslint-disable-next-line */ export interface RemixUiThemeModuleProps { - themeModule: ThemeModule + themeModule: ThemeModule; } +const defaultThemes = [ + { + name: 'Dark', + quality: 'dark', + url: 'assets/css/themes/remix-dark_tvx1s2.css' + }, + { + name: 'Light', + quality: 'light', + url: 'assets/css/themes/remix-light_powaqg.css' + }, + { + name: 'Midcentury', + quality: 'light', + url: 'assets/css/themes/remix-midcentury_hrzph3.css' + }, + { + name: 'Black', + quality: 'dark', + url: 'assets/css/themes/remix-black_undtds.css' + }, + { + name: 'Candy', + quality: 'light', + url: 'assets/css/themes/remix-candy_ikhg4m.css' + }, + + { + name: 'Cerulean', + quality: 'light', + url: 'assets/css/themes/bootstrap-cerulean.min.css' + }, + { + name: 'Flatly', + quality: 'light', + url: 'assets/css/themes/bootstrap-flatly.min.css' + }, + { + name: 'Spacelab', + quality: 'light', + url: 'assets/css/themes/bootstrap-spacelab.min.css' + }, + { + name: 'Cyborg', + quality: 'dark', + url: 'assets/css/themes/bootstrap-cyborg.min.css' + } +]; + export function RemixUiThemeModule({ themeModule }: RemixUiThemeModuleProps) { - const [themeName, setThemeName] = useState('') - const themeRef = useRef(null) + const [themeName, setThemeName] = useState(''); + const [themes, _] = useState(defaultThemes); + const themeRef = useRef(null); useEffect(() => { - themeModule.switchTheme() - }, [themeName, themeModule]) - - function ThemeHead () { - const [nextTheme, setNextTheme] = useState() - const linkRef = useRef(null) - function initTheme (callback: () => void) { - // const theme = yo`` - if (themeModule.active) { - setNextTheme(themeModule.themes[themeModule.active]) // Theme - document.documentElement.style.setProperty('--theme', nextTheme.quality) - } + themeModule.switchTheme(); + }, [themeName, themeModule]); + /** + * Change the current theme + * @param {string} [themeName] - The name of the theme + */ + function switchTheme(themeName: string) { + if (themeName && !Object.keys(themes).includes(themeName)) { + throw new Error(`Theme ${themeName} doesn't exist`); } + const next = themeName || this.active; // Name + if (next === this.active) return; + themeModule._paq.push(['trackEvent', 'themeModule', 'switchTo', next]); + const nextTheme = themeModule.themes[next]; // Theme + if (!themeModule.forced) + themeModule._deps.config.set('settings/theme', next); + document.getElementById('theme-link').remove(); + const theme = yo``; + theme.addEventListener('load', () => { + themeModule.emit('themeLoaded', nextTheme); + themeModule.events.emit('themeLoaded', nextTheme); + }); + document.head.insertBefore(theme, document.head.firstChild); + document.documentElement.style.setProperty('--theme', nextTheme.quality); + if (themeName) themeModule.active = themeName; + // TODO: Only keep `this.emit` (issue#2210) + themeModule.emit('themeChanged', nextTheme); + themeModule.events.emit('themeChanged', nextTheme); + } + + function ThemeHead() { + const [nextTheme, setNextTheme] = useState(); + const linkRef = useRef(null); useEffect(() => { - const shell = document.querySelector('div[data-id="remixIDE"]') as any - const splashScreen = document.querySelector('div[data-id="remixIDESplash"]') as Node + const shell = document.querySelector('div[data-id="remixIDE"]') as any; + const splashScreen = document.querySelector( + 'div[data-id="remixIDESplash"]' + ) as Node; const callback = () => { // setTimeout(() => { // document.body.removeChild(splashScreen) // // eslint-disable-next-line @typescript-eslint/no-non-null-assertion // shell!.style.visibility = 'visible' // }, 1500) - } + }; document.addEventListener('load', () => { - if (callback) callback() - }) - document.head.insertBefore(linkRef.current, document.head.firstChild) - }, []) + if (callback) callback(); + }); + document.head.insertBefore(linkRef.current, document.head.firstChild); + }, []); return ( - - ) + + ); } return (
-
-
Themes Module
-
- {themeModule.getThemes() ? themeModule.getThemes().map((theme, idx) => ( -
- { themeModule.switchTheme(theme.name); setThemeName(theme.name) }} className="align-middle custom-control-input" name='theme' id={theme.name} data-id={`settingsTabTheme${theme.name}`} checked = {themeModule.active === theme.name }/> - -
- )) : null - } -
+
+
Themes
+
+ {themeModule.getThemes() + ? themeModule.getThemes().map((theme, idx) => ( +
+ { + themeModule.switchTheme(theme.name); + setThemeName(theme.name); + }} + className="align-middle custom-control-input" + name="theme" + id={theme.name} + data-id={`settingsTabTheme${theme.name}`} + checked={themeModule.active === theme.name} + /> + +
+ )) + : null}
- ) +
+ ); } export default RemixUiThemeModule; diff --git a/libs/remix-ui/theme-module/src/reducers/themeModuleReducers.ts b/libs/remix-ui/theme-module/src/reducers/themeModuleReducers.ts new file mode 100644 index 0000000000..e69de29bb2