This commit is contained in:
parent
947cc56ba4
commit
7d361da167
@ -8,70 +8,127 @@ export const setLanguage = (selectedLanguage: string, i18n: any) => {
|
|||||||
i18n.locale = locale
|
i18n.locale = locale
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getDefaultSettings() {
|
export interface SettingsCheckbox {
|
||||||
|
name: string
|
||||||
|
selected: boolean,
|
||||||
|
type: 'checkbox'
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface SettingsOptions {
|
||||||
|
name: string
|
||||||
|
options: string[]
|
||||||
|
selected: string
|
||||||
|
type: 'option'
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Settings {
|
||||||
|
version: string
|
||||||
|
settings: {
|
||||||
|
audioOnly: SettingsCheckbox
|
||||||
|
defaultQuality: SettingsOptions
|
||||||
|
language: SettingsOptions
|
||||||
|
chatVisible: SettingsCheckbox
|
||||||
|
streamTagsVisible: SettingsCheckbox
|
||||||
|
streamerAboutSectionVisible: SettingsCheckbox
|
||||||
|
autoplay: SettingsCheckbox
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export function getDefaultSettings(): Settings {
|
||||||
return {
|
return {
|
||||||
version: import.meta.env.SAFETWITCH_TAG,
|
version: import.meta.env.SAFETWITCH_TAG,
|
||||||
audioOnly: {
|
settings: {
|
||||||
name: 'audioOnly',
|
audioOnly: {
|
||||||
selected: false,
|
name: 'audioOnly',
|
||||||
type: 'checkbox'
|
selected: false,
|
||||||
},
|
type: 'checkbox'
|
||||||
defaultQuality: {
|
},
|
||||||
name: 'defaultQuality',
|
defaultQuality: {
|
||||||
options: ['160p', '360p', '480p', '720p', '1080p'],
|
name: 'defaultQuality',
|
||||||
selected: '480p',
|
options: ['160p', '360p', '480p', '720p', '1080p'],
|
||||||
type: 'option'
|
selected: '480p',
|
||||||
},
|
type: 'option'
|
||||||
language: {
|
},
|
||||||
name: 'language',
|
language: {
|
||||||
options: languages,
|
name: 'language',
|
||||||
selected: 'English',
|
options: languages,
|
||||||
type: 'option'
|
selected: 'English',
|
||||||
},
|
type: 'option'
|
||||||
chatVisible: {
|
},
|
||||||
name: 'chatVisible',
|
chatVisible: {
|
||||||
selected: false,
|
name: 'chatVisible',
|
||||||
type: 'checkbox'
|
selected: false,
|
||||||
},
|
type: 'checkbox'
|
||||||
streamTagsVisible: {
|
},
|
||||||
name: 'streamTagsVisible',
|
streamTagsVisible: {
|
||||||
selected: true,
|
name: 'streamTagsVisible',
|
||||||
type: 'checkbox'
|
selected: true,
|
||||||
},
|
type: 'checkbox'
|
||||||
streamerAboutSectionVisible: {
|
},
|
||||||
name: 'streamerAboutSectionVisible',
|
streamerAboutSectionVisible: {
|
||||||
selected: true,
|
name: 'streamerAboutSectionVisible',
|
||||||
type: 'checkbox'
|
selected: true,
|
||||||
},
|
type: 'checkbox'
|
||||||
autoplay: {
|
},
|
||||||
name: 'autoplay',
|
autoplay: {
|
||||||
selected: false,
|
name: 'autoplay',
|
||||||
type: 'checkbox'
|
selected: false,
|
||||||
|
type: 'checkbox'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function syncUserSettings() {
|
/**
|
||||||
|
* Syncs older user setting to the latest version
|
||||||
|
* @param settings Settings of the Settings type
|
||||||
|
* @returns The synced settings and a boolean stating if the settings were modified
|
||||||
|
*/
|
||||||
|
export function syncUserSettings(settings: Settings): {settings: Settings, changed: boolean}{
|
||||||
const defaultSettings = getDefaultSettings()
|
const defaultSettings = getDefaultSettings()
|
||||||
const userSettings = localStorage.getItem('settings')
|
let userSettings = settings
|
||||||
if (!userSettings) return
|
|
||||||
const parsedUserSettings = JSON.parse(userSettings)
|
|
||||||
|
|
||||||
if (parsedUserSettings.version === import.meta.env.SAFETWITCH_TAG) {
|
// converting settings storage from versions older
|
||||||
|
// than 2.4.1
|
||||||
|
let oldMigration = false
|
||||||
|
|
||||||
|
|
||||||
|
if (userSettings.version === import.meta.env.SAFETWITCH_TAG) {
|
||||||
console.log('Settings up to date!')
|
console.log('Settings up to date!')
|
||||||
return
|
return { settings: userSettings, changed: false }
|
||||||
} else {
|
} else {
|
||||||
console.log('Settings outdated... Migrating')
|
console.log('Settings outdated... Migrating')
|
||||||
|
// converts v2.4.1 to 241
|
||||||
|
let settingsVersion = Number(userSettings.version.slice(1, defaultSettings.version.length).split(".").join(""))
|
||||||
|
|
||||||
|
if (settingsVersion < 241) {
|
||||||
|
oldMigration = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (oldMigration) {
|
||||||
|
let oldSettings: any = userSettings
|
||||||
|
delete oldSettings.version
|
||||||
|
let migrated: Settings = {
|
||||||
|
version: defaultSettings.version,
|
||||||
|
settings: {
|
||||||
|
...oldSettings
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const synced = { ...defaultSettings, ...parsedUserSettings }
|
userSettings = migrated
|
||||||
|
}
|
||||||
|
console.log(userSettings)
|
||||||
|
|
||||||
|
const synced = { ...defaultSettings, ...userSettings }
|
||||||
|
|
||||||
// update avaliable languages
|
// update avaliable languages
|
||||||
synced.language.options = defaultSettings.language.options
|
synced.settings.language.options = defaultSettings.settings.language.options
|
||||||
synced.version = import.meta.env.SAFETWITCH_TAG
|
synced.version = import.meta.env.SAFETWITCH_TAG
|
||||||
localStorage.setItem('settings', JSON.stringify(synced))
|
localStorage.setItem('settings', JSON.stringify(synced))
|
||||||
console.log('Migrated!')
|
console.log('Migrated!')
|
||||||
|
return { settings: synced, changed: true }
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getSetting(key: string): boolean | string {
|
export function getSetting(key: string): boolean | string {
|
||||||
|
@ -1,12 +1,11 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { ref } from 'vue'
|
import { ref } from 'vue'
|
||||||
import { getDefaultSettings, syncUserSettings, setLanguage, themeList } from '@/settingsManager'
|
import { getDefaultSettings, syncUserSettings, setLanguage, themeList } from '@/settingsManager'
|
||||||
|
import type { Settings } from '@/settingsManager'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
setup() {
|
setup() {
|
||||||
let settings
|
let settings: Settings
|
||||||
syncUserSettings()
|
|
||||||
|
|
||||||
let storedSettings = localStorage.getItem('settings')
|
let storedSettings = localStorage.getItem('settings')
|
||||||
if (storedSettings === null) {
|
if (storedSettings === null) {
|
||||||
settings = getDefaultSettings()
|
settings = getDefaultSettings()
|
||||||
@ -14,6 +13,11 @@ export default {
|
|||||||
settings = JSON.parse(storedSettings)
|
settings = JSON.parse(storedSettings)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const syncResp = syncUserSettings(settings)
|
||||||
|
if (syncResp.changed) {
|
||||||
|
settings = syncResp.settings
|
||||||
|
}
|
||||||
|
|
||||||
let selectedTheme = localStorage.getItem('theme') || "light"
|
let selectedTheme = localStorage.getItem('theme') || "light"
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -26,7 +30,8 @@ export default {
|
|||||||
save() {
|
save() {
|
||||||
const settings = JSON.stringify(this.settings)
|
const settings = JSON.stringify(this.settings)
|
||||||
localStorage.setItem('settings', settings)
|
localStorage.setItem('settings', settings)
|
||||||
setLanguage(this.settings.language.selected, this.$i18n)
|
console.log(this.settings)
|
||||||
|
setLanguage(this.settings.settings.language.selected, this.$i18n)
|
||||||
|
|
||||||
this.setTheme()
|
this.setTheme()
|
||||||
// Reload needed
|
// Reload needed
|
||||||
@ -40,26 +45,30 @@ export default {
|
|||||||
return 'border-purple'
|
return 'border-purple'
|
||||||
}
|
}
|
||||||
return "border-none"
|
return "border-none"
|
||||||
}
|
},
|
||||||
// download() {
|
download() {
|
||||||
// var hiddenElement = document.createElement('a');
|
var hiddenElement = document.createElement('a');
|
||||||
|
|
||||||
// hiddenElement.href = 'data:attachment/text,' + encodeURI(JSON.stringify(this.settings));
|
hiddenElement.href = 'data:attachment/text,' + encodeURI(JSON.stringify(this.settings));
|
||||||
// hiddenElement.target = '_blank';
|
hiddenElement.target = '_blank';
|
||||||
// hiddenElement.download = 'safetwitch_prefs.json';
|
hiddenElement.download = 'safetwitch_prefs.json';
|
||||||
// hiddenElement.click();
|
hiddenElement.click();
|
||||||
// },
|
},
|
||||||
// async handleImport(event: any) {
|
async handleImport(event: any) {
|
||||||
// const file = await event.target.files[0].text()
|
const file = await event.target.files[0].text()
|
||||||
// const parsed = JSON.parse(file)
|
const parsed = JSON.parse(file)
|
||||||
|
let settings: Settings
|
||||||
|
|
||||||
// if (parsed.audioOnly == undefined) {
|
if (parsed.version !== getDefaultSettings().version && parsed.version !== undefined) {
|
||||||
// return
|
const syncResp = syncUserSettings(parsed)
|
||||||
// }
|
settings = syncResp.settings
|
||||||
|
} else {
|
||||||
|
settings = parsed
|
||||||
|
}
|
||||||
|
|
||||||
// this.settings = file
|
this.settings = settings
|
||||||
// this.save()
|
this.save()
|
||||||
// },
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
@ -69,7 +78,7 @@ export default {
|
|||||||
<h1 class="font-bold text-3xl">{{ $t("nav.settings") }}</h1>
|
<h1 class="font-bold text-3xl">{{ $t("nav.settings") }}</h1>
|
||||||
<hr class="my-2" />
|
<hr class="my-2" />
|
||||||
<ul class="w-full space-y-1">
|
<ul class="w-full space-y-1">
|
||||||
<li v-for="setting in settings" :key="setting.type">
|
<li v-for="setting in settings.settings" :key="setting.type">
|
||||||
<div v-if="setting.type == 'checkbox'" class="justify-between items-center flex">
|
<div v-if="setting.type == 'checkbox'" class="justify-between items-center flex">
|
||||||
<label :for="setting.name">{{ $t(`settings.${setting.name}`) }}</label>
|
<label :for="setting.name">{{ $t(`settings.${setting.name}`) }}</label>
|
||||||
<input :name="setting.name" type="checkbox" v-model="setting.selected" />
|
<input :name="setting.name" type="checkbox" v-model="setting.selected" />
|
||||||
@ -93,8 +102,10 @@ export default {
|
|||||||
<!--
|
<!--
|
||||||
Use theme colors for preview
|
Use theme colors for preview
|
||||||
-->
|
-->
|
||||||
<li v-for="theme in themeList" :key="theme.name" class="hover:scale-110 border-2 rounded-md transition-transform" :class="highlight(theme.name)">
|
<li v-for="theme in themeList" :key="theme.name" class="hover:scale-110 border-2 rounded-md transition-transform"
|
||||||
<button @click="selectedTheme = theme.name" class="p-5 py-1.5 border-4 rounded-md" :style="`border-color: ${theme.extend.colors.primary}; background:${theme.extend.colors.crust}; color:${theme.extend.colors.contrast};`">
|
:class="highlight(theme.name)">
|
||||||
|
<button @click="selectedTheme = theme.name" class="p-5 py-1.5 border-4 rounded-md"
|
||||||
|
:style="`border-color: ${theme.extend.colors.primary}; background:${theme.extend.colors.crust}; color:${theme.extend.colors.contrast};`">
|
||||||
<p>{{ theme.name }}</p>
|
<p>{{ theme.name }}</p>
|
||||||
</button>
|
</button>
|
||||||
</li>
|
</li>
|
||||||
@ -102,9 +113,8 @@ export default {
|
|||||||
|
|
||||||
<div class="space-x-2 mt-3">
|
<div class="space-x-2 mt-3">
|
||||||
<button @click="save" class="bg-surface0 p-4 py-2 rounded-md">{{ $t('settings.saveButton') }}</button>
|
<button @click="save" class="bg-surface0 p-4 py-2 rounded-md">{{ $t('settings.saveButton') }}</button>
|
||||||
<!-- <button @click="download" class="bg-surface0 p-4 py-2 rounded-md">Export</button>
|
<button @click="download" class="bg-surface0 p-4 py-2 rounded-md">Export</button>
|
||||||
<input type="file" @change="handleImport" name="fileinput" ref="fileinput"
|
<input type="file" @change="handleImport" name="fileinput" ref="fileinput" class="bg-surface0 p-4 py-2 rounded-md">
|
||||||
class="bg-surface0 p-4 py-2 rounded-md"> -->
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user