Check for theme params on the URL (#2678)

---------

Co-authored-by: dni  <office@dnilabs.com>
This commit is contained in:
Tiago Vasconcelos 2024-09-24 10:44:07 +01:00 committed by GitHub
parent 9d7e54f6b2
commit daee2b3418
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 65 additions and 17 deletions

File diff suppressed because one or more lines are too long

View File

@ -80,20 +80,6 @@ window.app = Vue.createApp({
this.applyGradient() this.applyGradient()
} }
}, },
setColors: function () {
this.$q.localStorage.set(
'lnbits.primaryColor',
LNbits.utils.getPaletteColor('primary')
)
this.$q.localStorage.set(
'lnbits.secondaryColor',
LNbits.utils.getPaletteColor('secondary')
)
this.$q.localStorage.set(
'lnbits.darkBgColor',
LNbits.utils.getPaletteColor('dark')
)
},
updateAccount: async function () { updateAccount: async function () {
try { try {
const {data} = await LNbits.api.request( const {data} = await LNbits.api.request(

View File

@ -470,6 +470,7 @@ window.windowMixin = {
}, },
applyGradient: function () { applyGradient: function () {
if (this.$q.localStorage.getItem('lnbits.gradientBg')) { if (this.$q.localStorage.getItem('lnbits.gradientBg')) {
this.setColors()
darkBgColor = this.$q.localStorage.getItem('lnbits.darkBgColor') darkBgColor = this.$q.localStorage.getItem('lnbits.darkBgColor')
primaryColor = this.$q.localStorage.getItem('lnbits.primaryColor') primaryColor = this.$q.localStorage.getItem('lnbits.primaryColor')
const gradientStyle = `linear-gradient(to bottom right, ${LNbits.utils.hexDarken(String(primaryColor), -70)}, #0a0a0a)` const gradientStyle = `linear-gradient(to bottom right, ${LNbits.utils.hexDarken(String(primaryColor), -70)}, #0a0a0a)`
@ -487,6 +488,20 @@ window.windowMixin = {
document.head.appendChild(style) document.head.appendChild(style)
} }
}, },
setColors: function () {
this.$q.localStorage.set(
'lnbits.primaryColor',
LNbits.utils.getPaletteColor('primary')
)
this.$q.localStorage.set(
'lnbits.secondaryColor',
LNbits.utils.getPaletteColor('secondary')
)
this.$q.localStorage.set(
'lnbits.darkBgColor',
LNbits.utils.getPaletteColor('dark')
)
},
copyText: function (text, message, position) { copyText: function (text, message, position) {
var notify = this.$q.notify var notify = this.$q.notify
Quasar.copyToClipboard(text).then(function () { Quasar.copyToClipboard(text).then(function () {
@ -536,6 +551,52 @@ window.windowMixin = {
LNbits.utils.notifyApiError(e) LNbits.utils.notifyApiError(e)
} }
}) })
},
themeParams() {
const url = new URL(window.location.href)
const params = new URLSearchParams(window.location.search)
const fields = ['theme', 'dark', 'gradient']
const toBoolean = value =>
value.trim().toLowerCase() === 'true' || value === '1'
// Check if any of the relevant parameters ('theme', 'dark', 'gradient') are present in the URL.
if (fields.some(param => params.has(param))) {
const theme = params.get('theme')
const darkMode = params.get('dark')
const gradient = params.get('gradient')
if (
theme &&
this.g.allowedThemes.includes(theme.trim().toLowerCase())
) {
const normalizedTheme = theme.trim().toLowerCase()
document.body.setAttribute('data-theme', normalizedTheme)
this.$q.localStorage.set('lnbits.theme', normalizedTheme)
}
if (darkMode) {
const isDark = toBoolean(darkMode)
this.$q.localStorage.set('lnbits.darkMode', isDark)
if (!isDark) {
this.$q.localStorage.set('lnbits.gradientBg', false)
}
}
if (gradient) {
const isGradient = toBoolean(gradient)
this.$q.localStorage.set('lnbits.gradientBg', isGradient)
if (isGradient) {
this.$q.localStorage.set('lnbits.darkMode', true)
}
}
// Remove processed parameters
fields.forEach(param => params.delete(param))
window.history.replaceState(null, null, url.pathname)
}
this.setColors()
} }
}, },
created: async function () { created: async function () {
@ -550,8 +611,6 @@ window.windowMixin = {
this.reactionChoice = this.reactionChoice =
this.$q.localStorage.getItem('lnbits.reactions') || 'confettiBothSides' this.$q.localStorage.getItem('lnbits.reactions') || 'confettiBothSides'
this.applyGradient()
this.g.allowedThemes = window.allowedThemes ?? ['bitcoin'] this.g.allowedThemes = window.allowedThemes ?? ['bitcoin']
let locale = this.$q.localStorage.getItem('lnbits.lang') let locale = this.$q.localStorage.getItem('lnbits.lang')
@ -590,6 +649,8 @@ window.windowMixin = {
) )
} }
this.applyGradient()
if (window.user) { if (window.user) {
this.g.user = Object.freeze(window.LNbits.map.user(window.user)) this.g.user = Object.freeze(window.LNbits.map.user(window.user))
} }
@ -628,6 +689,7 @@ window.windowMixin = {
this.g.extensions = extensions this.g.extensions = extensions
} }
await this.checkUsrInUrl() await this.checkUsrInUrl()
this.themeParams()
} }
} }