Skip to content

Commit

Permalink
feat: isDarkTheme, getTheme util
Browse files Browse the repository at this point in the history
  • Loading branch information
surunzi committed Jul 9, 2024
1 parent 0e59091 commit e6559dc
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
8 changes: 5 additions & 3 deletions eruda.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ declare module 'eruda' {

/**
* Eruda Plugin
* @see https://github.com/liriliri/eruda/blob/master/doc/PLUGIN.md
* @see https://eruda.liriliri.io/docs/plugin.html
*/
export interface Tool {
/**
Expand Down Expand Up @@ -389,16 +389,18 @@ declare module 'eruda' {

/**
* Eruda Util
* @see https://github.com/liriliri/eruda/blob/master/doc/UTIL_API.md
* @see https://eruda.liriliri.io/docs/plugin.html#utility
*/
export interface Util {
evalCss(css: string): HTMLStyleElement
isErudaEl(val: any): boolean
isDarkTheme(theme?: string): boolean
getTheme(): string
}

/**
* Eruda APIs
* @see https://github.com/liriliri/eruda/blob/master/doc/API.md
* @see https://eruda.liriliri.io/docs/api.html
*/
export interface ErudaApis {
/**
Expand Down
27 changes: 26 additions & 1 deletion src/eruda.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@ import Settings from './Settings/Settings'
import emitter from './lib/emitter'
import logger from './lib/logger'
import * as util from './lib/util'
import { isDarkTheme } from './lib/themes'
import themes from './lib/themes'
import isFn from 'licia/isFn'
import isNum from 'licia/isNum'
import isObj from 'licia/isObj'
import each from 'licia/each'
import isMobile from 'licia/isMobile'
import viewportScale from 'licia/viewportScale'
import detectBrowser from 'licia/detectBrowser'
import $ from 'licia/$'
import toArr from 'licia/toArr'
import upperFirst from 'licia/upperFirst'
import nextTick from 'licia/nextTick'
import isEqual from 'licia/isEqual'
import extend from 'licia/extend'
import evalCss from './lib/evalCss'
import chobitsu from './lib/chobitsu'
Expand Down Expand Up @@ -51,7 +55,28 @@ export default {
},
_isInit: false,
version: VERSION,
util,
util: {
isErudaEl: util.isErudaEl,
evalCss,
isDarkTheme(theme) {
if (!theme) {
theme = this.getTheme()
}
return isDarkTheme(theme)
},
getTheme: () => {
const curTheme = evalCss.getCurTheme()

let result = 'Light'
each(themes, (theme, name) => {
if (isEqual(theme, curTheme)) {
result = name
}
})

return result
},
},
chobitsu,
Tool,
Console,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/themes.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ export default {
'#994cc3', '#aa0982', '#7d818b', '#994cc3', '#546e7a',
'#994cc3', '#4876d6', '#4876d6', '#637777',
]),
'AMOLED': createDarkTheme([
AMOLED: createDarkTheme([
'#000000', '#8F93A2', '#FFFFFF', '#68FFAE', '#000000',
'#41465b', '#4B526D', '#000000', '#DEFDF7', '#9CFFCF',
'#D596FF', '#A76DF7', '#9FC5FF', '#86F3C7', '#8F93A2',
Expand Down
3 changes: 0 additions & 3 deletions src/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import root from 'licia/root'
import toNum from 'licia/toNum'
import trim from 'licia/trim'
import html from 'licia/html'
import evalCssUtil from './evalCss'

// https://stackoverflow.com/questions/46318395/detecting-mobile-device-notch
export function hasSafeArea() {
Expand Down Expand Up @@ -108,8 +107,6 @@ export function isChobitsuEl(el) {
return false
}

export const evalCss = evalCssUtil

export function classPrefix(str) {
if (/<[^>]*>/g.test(str)) {
try {
Expand Down

0 comments on commit e6559dc

Please sign in to comment.