fix: merge settings with default

This commit is contained in:
Xylight committed 2024-11-01 10:24:40 -07:00
1 parent b291aeebf8
commit 2b27e99784
4 files changed
+47 -26

No files matched your search

@@ -58,8 +58,8 @@
})
}
let replyReason
run(() => {
let replyReason = $state('')
$effect(() => {
replyReason = commentReason ? getReplyReason(reason, preset) : ''
})
@@ -162,7 +162,6 @@
const resetText = () => {
reason = ''
replyReason = ''
commentReason = false
}
@@ -192,7 +191,6 @@
children: [],
comment_view: item,
depth: 1,
ui: {},
}}
postId={item.post.id}
actions={false}
@@ -227,7 +225,7 @@
placeholder={replyReason}
rows={3}
>
{#snippet label()}
{#snippet customLabel()}
<div class="flex justify-between items-end mb-1">
{$t('comment.reply')}
<Select bind:value={preset} placeholder="No preset">
@@ -35,8 +35,6 @@
<Button
color={isPanel ? 'secondary' : 'tertiary'}
{...rest}
onclick
on:contextmenu
class="rounded-full w-10 h-10 flex-shrink-0 {adaptive
? '@3xl:h-8 @3xl:px-3 @3xl:rounded-[10px] @3xl:w-auto'
: ''} {isSelected
+1 -1
View File
@@ -57,7 +57,7 @@
{style}
>
<NavButton
on:contextmenu={(e) => {
oncontextmenu={(e) => {
e.preventDefault()
promptOpen = true
return true
+43 -18
View File
@@ -191,31 +191,29 @@ export const defaultSettings: Settings = {
logoColorMonth: null,
}
function createSettingsState(initial: Settings) {
function createSettingsState(initial: Settings): Settings {
let settings = $state(initial)
if (browser) {
try {
const localSettings = JSON.parse(localStorage.getItem('settings') ?? '{}')
const merged = mergeDeep(initial, localSettings)
settings = merged
} catch (e) {
}
}
return settings
}
export let settings = createSettingsState(defaultSettings)
if (typeof window != 'undefined') {
let oldUserSettings = JSON.parse(
localStorage.getItem('settings') ?? JSON.stringify(defaultSettings)
)
// settings = {
// ...defaultSettings,
// ...oldUserSettings,
// settingsVer: defaultSettings.settingsVer,
// }
}
$effect.root(() => {
$effect(() => {
if (typeof window != 'undefined') {
localStorage.setItem('settings', JSON.stringify(settings))
}
localStorage.setItem('settings', JSON.stringify(settings))
if (settings.language) {
locale.set(settings.language)
} else {
@@ -226,4 +224,31 @@ $effect.root(() => {
return () => {
}
})
})
function isObject(item: any) {
return (item && typeof item === 'object' && !Array.isArray(item));
}
/**
* Deep merge two objects.
* @param target
* @param ...sources
*/
function mergeDeep(target: any, ...sources: any[]) {
if (!sources.length) return target;
const source = sources.shift();
if (isObject(target) && isObject(source)) {
for (const key in source) {
if (isObject(source[key])) {
if (!target[key]) Object.assign(target, { [key]: {} });
mergeDeep(target[key], source[key]);
} else {
Object.assign(target, { [key]: source[key] });
}
}
}
return mergeDeep(target, ...sources);
}