refactor(modules/web): use deep watcher to sync read config;remove unnecessary refs

This commit is contained in:
Xwite
2024-10-02 21:38:39 +08:00
parent 312a33aea3
commit 4dde09c6a4
+46 -80
View File
@@ -16,7 +16,7 @@
:style="themeColor" :style="themeColor"
ref="themes" ref="themes"
@click="setTheme(index)" @click="setTheme(index)"
:class="{ selected: selectedTheme == index }" :class="{ selected: theme == index }"
><em v-if="index < 6" class="iconfont">&#58980;</em ><em v-if="index < 6" class="iconfont">&#58980;</em
><em v-else class="moon-icon">{{ moonIcon }}</em></span ><em v-else class="moon-icon">{{ moonIcon }}</em></span
> >
@@ -188,11 +188,23 @@ import API from "@api";
const store = useBookStore(); const store = useBookStore();
const theme = ref(0); //阅读界面设置改变时保存同步配置
watch(
() => store.config,
(newValue) => {
localStorage.setItem("config", JSON.stringify(newValue));
API.saveReadConfig(newValue);
},
{
deep: 2 //深度为2
}
)
const isNight = ref(store.config.theme == 6); //主题颜色
const moonIcon = ref(""); const theme = computed(() => store.theme);
const themeColors = shallowRef([ const isNight = computed(() => store.isDark);
const moonIcon = computed(() => theme.value == 6 ? "" : "");
const themeColors = [
{ {
background: "rgba(250, 245, 235, 0.8)", background: "rgba(250, 245, 235, 0.8)",
}, },
@@ -214,62 +226,30 @@ const themeColors = shallowRef([
{ {
background: "rgba(0, 0, 0, 0.5)", background: "rgba(0, 0, 0, 0.5)",
}, },
]); ];
const moonIconStyle = ref({
display: "inline",
color: "rgba(255,255,255,0.2)",
});
const fonts = ref(["雅黑", "宋体", "楷书"]);
const customFontName = ref(store.config.customFontName);
const customFontSavePopVisible = ref(false);
onMounted(() => {
//初始化设置项目
var config = store.config;
theme.value = config.theme;
if (theme.value == 6) {
moonIcon.value = "";
} else {
moonIcon.value = "";
}
});
const config = computed(() => {
return store.config;
});
const popupTheme = computed(() => { const popupTheme = computed(() => {
return { return {
background: settings.themes[config.value.theme].popup, background: settings.themes[theme.value].popup,
}; };
}); });
const selectedTheme = computed(() => { const setTheme = (theme) => {
return store.config.theme; store.config.theme = theme;
}); }
//预置字体
const fonts = ref(["雅黑", "宋体", "楷书"]);
const setFont = (font) => {
store.config.font = font;
};
const selectedFont = computed(() => { const selectedFont = computed(() => {
return store.config.font; return store.config.font;
}); });
//自定义字体
const setTheme = (theme) => { const customFontName = ref(store.config.customFontName);
if (theme == 6) { const customFontSavePopVisible = ref(false);
isNight.value = true;
moonIcon.value = "";
moonIconStyle.value.color = "#ed4259";
} else {
isNight.value = false;
moonIcon.value = "";
moonIconStyle.value.color = "rgba(255,255,255,0.2)";
}
config.value.theme = theme;
saveConfig(config.value);
};
const setFont = (font) => {
config.value.font = font;
saveConfig(config.value);
};
const setCustomFont = () => { const setCustomFont = () => {
config.value.font = -1; store.config.font = -1;
config.value.customFontName = customFontName.value; store.config.customFontName = customFontName.value;
saveConfig(config.value);
}; };
// 加载网络字体 // 加载网络字体
const loadFontFromURL = () => { const loadFontFromURL = () => {
@@ -319,85 +299,71 @@ const loadFontFromURL = () => {
); );
} }
//字体大小
const fontSize = computed(() => { const fontSize = computed(() => {
return store.config.fontSize; return store.config.fontSize;
}); });
const moreFontSize = () => { const moreFontSize = () => {
if (config.value.fontSize < 48) config.value.fontSize += 2; if (store.config.fontSize < 48) store.config.fontSize += 2;
saveConfig(config.value);
}; };
const lessFontSize = () => { const lessFontSize = () => {
if (config.value.fontSize > 12) config.value.fontSize -= 2; if (store.config.fontSize > 12) store.config.fontSize -= 2;
saveConfig(config.value);
}; };
//字 行 段落间距
const spacing = computed(() => { const spacing = computed(() => {
return store.config.spacing; return store.config.spacing;
}); });
const lessLetterSpacing = () => { const lessLetterSpacing = () => {
store.config.spacing.letter -= 0.01; store.config.spacing.letter -= 0.01;
saveConfig(config.value);
}; };
const moreLetterSpacing = () => { const moreLetterSpacing = () => {
store.config.spacing.letter += 0.01; store.config.spacing.letter += 0.01;
saveConfig(config.value);
}; };
const lessLineSpacing = () => { const lessLineSpacing = () => {
store.config.spacing.line -= 0.1; store.config.spacing.line -= 0.1;
saveConfig(config.value);
}; };
const moreLineSpacing = () => { const moreLineSpacing = () => {
store.config.spacing.line += 0.1; store.config.spacing.line += 0.1;
saveConfig(config.value);
}; };
const lessParagraphSpacing = () => { const lessParagraphSpacing = () => {
store.config.spacing.paragraph -= 0.1; store.config.spacing.paragraph -= 0.1;
saveConfig(config.value);
}; };
const moreParagraphSpacing = () => { const moreParagraphSpacing = () => {
store.config.spacing.paragraph += 0.1; store.config.spacing.paragraph += 0.1;
saveConfig(config.value);
}; };
//页面宽度
const readWidth = computed(() => { const readWidth = computed(() => {
return store.config.readWidth; return store.config.readWidth;
}); });
const moreReadWidth = () => { const moreReadWidth = () => {
// 此时会截断页面 // 此时会截断页面
if (config.value.readWidth + 160 + 2 * 68 > window.innerWidth) return; if (store.config.readWidth + 160 + 2 * 68 > window.innerWidth) return;
config.value.readWidth += 160; store.config.readWidth += 160;
saveConfig(config.value);
}; };
const lessReadWidth = () => { const lessReadWidth = () => {
if (config.value.readWidth > 640) config.value.readWidth -= 160; if (store.config.readWidth > 640) store.config.readWidth -= 160;
saveConfig(config.value);
}; };
//翻页速度
const jumpDuration = computed(() => { const jumpDuration = computed(() => {
return store.config.jumpDuration; return store.config.jumpDuration;
}); });
const moreJumpDuration = () => { const moreJumpDuration = () => {
store.config.jumpDuration += 100; store.config.jumpDuration += 100;
saveConfig(config.value);
}; };
const lessJumpDuration = () => { const lessJumpDuration = () => {
if (store.config.jumpDuration === 0) return; if (store.config.jumpDuration === 0) return;
store.config.jumpDuration -= 100; store.config.jumpDuration -= 100;
saveConfig(config.value);
}; };
//无限加载
const infiniteLoading = computed(() => { const infiniteLoading = computed(() => {
return store.config.infiniteLoading; return store.config.infiniteLoading;
}); });
const setInfiniteLoading = (loading) => { const setInfiniteLoading = (loading) => {
config.value.infiniteLoading = loading; store.config.infiniteLoading = loading;
saveConfig(config.value);
};
const saveConfig = (config) => {
store.setConfig(config);
localStorage.setItem("config", JSON.stringify(config));
uploadConfig(config);
};
const uploadConfig = (config) => {
API.saveReadConfig(config);
}; };
</script> </script>