prep js logic for reducer

pull/5370/head
Joseph Izang 3 years ago committed by yann300
parent 47ac61e834
commit 4942292c6f
  1. 2
      apps/remix-ide/src/app/tabs/theme-module.js
  2. 176
      libs/remix-ui/theme-module/src/lib/remix-ui-theme-module.tsx
  3. 0
      libs/remix-ui/theme-module/src/reducers/themeModuleReducers.ts

@ -43,6 +43,7 @@ export class ThemeModule extends Plugin {
theme.url = window.location.origin + window.location.pathname + theme.url theme.url = window.location.origin + window.location.pathname + theme.url
return { ...acc, [theme.name]: theme } return { ...acc, [theme.name]: theme }
}, {}) }, {})
this._paq = _paq
let queryTheme = (new QueryParams()).get().theme let queryTheme = (new QueryParams()).get().theme
queryTheme = this.themes[queryTheme] ? queryTheme : null queryTheme = this.themes[queryTheme] ? queryTheme : null
let currentTheme = this._deps.config.get('settings/theme') let currentTheme = this._deps.config.get('settings/theme')
@ -83,6 +84,7 @@ export class ThemeModule extends Plugin {
* Init the theme * Init the theme
*/ */
initTheme (callback) { // callback is setTimeOut in app.js which is always passed initTheme (callback) { // callback is setTimeOut in app.js which is always passed
if (callback) this.initCallback = callback
if (this.active) { if (this.active) {
const nextTheme = this.themes[this.active] // Theme const nextTheme = this.themes[this.active] // Theme
document.documentElement.style.setProperty('--theme', nextTheme.quality) document.documentElement.style.setProperty('--theme', nextTheme.quality)

@ -1,68 +1,166 @@
/* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-explicit-any */
import React, { useEffect, useRef, useState } from 'react' import React, { useEffect, useRef, useState } from 'react';
import { ThemeModule } from '../../types/theme-module' import { Theme, ThemeModule } from '../../types/theme-module';
import './remix-ui-theme-module.module.css' import './remix-ui-theme-module.module.css';
/* eslint-disable-next-line */ /* eslint-disable-next-line */
export interface RemixUiThemeModuleProps { 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) { export function RemixUiThemeModule({ themeModule }: RemixUiThemeModuleProps) {
const [themeName, setThemeName] = useState('') const [themeName, setThemeName] = useState('');
const themeRef = useRef<any>(null) const [themes, _] = useState<Theme[]>(defaultThemes);
const themeRef = useRef<any>(null);
useEffect(() => { useEffect(() => {
themeModule.switchTheme() themeModule.switchTheme();
}, [themeName, themeModule]) }, [themeName, themeModule]);
/**
function ThemeHead () { * Change the current theme
const [nextTheme, setNextTheme] = useState<any>() * @param {string} [themeName] - The name of the theme
const linkRef = useRef<any>(null) */
function initTheme (callback: () => void) { function switchTheme(themeName: string) {
// const theme = yo`<link rel="stylesheet" href="${nextTheme.url}" id="theme-link"/>` if (themeName && !Object.keys(themes).includes(themeName)) {
if (themeModule.active) { throw new Error(`Theme ${themeName} doesn't exist`);
setNextTheme(themeModule.themes[themeModule.active]) // Theme
document.documentElement.style.setProperty('--theme', nextTheme.quality)
}
} }
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`<link rel="stylesheet" href="${nextTheme.url}" id="theme-link"/>`;
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<any>();
const linkRef = useRef<any>(null);
useEffect(() => { useEffect(() => {
const shell = document.querySelector('div[data-id="remixIDE"]') as any const shell = document.querySelector('div[data-id="remixIDE"]') as any;
const splashScreen = document.querySelector('div[data-id="remixIDESplash"]') as Node const splashScreen = document.querySelector(
'div[data-id="remixIDESplash"]'
) as Node;
const callback = () => { const callback = () => {
// setTimeout(() => { // setTimeout(() => {
// document.body.removeChild(splashScreen) // document.body.removeChild(splashScreen)
// // eslint-disable-next-line @typescript-eslint/no-non-null-assertion // // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
// shell!.style.visibility = 'visible' // shell!.style.visibility = 'visible'
// }, 1500) // }, 1500)
} };
document.addEventListener('load', () => { document.addEventListener('load', () => {
if (callback) callback() if (callback) callback();
}) });
document.head.insertBefore(linkRef.current, document.head.firstChild) document.head.insertBefore(linkRef.current, document.head.firstChild);
}, []) }, []);
return ( return (
<link rel="stylesheet" href={nextTheme.url} ref={linkRef} id="theme-link"/> <link
) rel="stylesheet"
href={nextTheme.url}
ref={linkRef}
id="theme-link"
/>
);
} }
return ( return (
<div className="border-top"> <div className="border-top">
<div className="card-body pt-3 pb-2"> <div className="card-body pt-3 pb-2">
<h6 className="card-title">Themes Module</h6> <h6 className="card-title">Themes</h6>
<div className="card-text themes-container"> <div className="card-text themes-container">
{themeModule.getThemes() ? themeModule.getThemes().map((theme, idx) => ( {themeModule.getThemes()
<div className="radio custom-control custom-radio mb-1 form-check" key={idx}> ? themeModule.getThemes().map((theme, idx) => (
<input type="radio" onChange={event => { 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 }/> <div
<label className="form-check-label custom-control-label" data-id={`settingsTabThemeLabel${theme.name}`} htmlFor={theme.name}>{theme.name} ({theme.quality})</label> className="radio custom-control custom-radio mb-1 form-check"
</div> key={idx}
)) : null >
} <input
</div> type="radio"
onChange={event => {
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}
/>
<label
className="form-check-label custom-control-label"
data-id={`settingsTabThemeLabel${theme.name}`}
htmlFor={theme.name}
>
{theme.name} ({theme.quality})
</label>
</div>
))
: null}
</div> </div>
</div> </div>
) </div>
);
} }
export default RemixUiThemeModule; export default RemixUiThemeModule;

Loading…
Cancel
Save