fix: the issue of local cache not being cleared after saving a new post (#3551)

#### What type of PR is this?

/kind bug
/area console

#### What this PR does / why we need it:

修复新建文章或页面保存后,本地内容缓存可能不会及时清空的问题。

#### Which issue(s) this PR fixes:

Fixes https://github.com/halo-dev/halo/issues/3380

#### Special notes for your reviewer:

测试方式:

1. 连续创建多篇新的文章,然后反复进入新建页面,观察是否有恢复缓存的提示。

#### Does this PR introduce a user-facing change?

```release-note
修复新建文章或页面保存后,本地内容缓存可能不会及时清空的问题。
```
pull/3563/head^2
Ryan Wang 2023-03-23 21:52:36 +08:00 committed by GitHub
parent 4ce5d0c2cf
commit 194850bc80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 9 deletions

View File

@ -17,16 +17,16 @@ interface useContentCacheReturn {
export function useContentCache( export function useContentCache(
key: string, key: string,
name: string, name: Ref<string | undefined>,
raw: Ref<string | undefined> raw: Ref<string | undefined>
): useContentCacheReturn { ): useContentCacheReturn {
const content_caches = useLocalStorage<ContentCache[]>(key, []); const content_caches = useLocalStorage<ContentCache[]>(key, []);
const { t } = useI18n(); const { t } = useI18n();
const handleResetCache = () => { const handleResetCache = () => {
if (name) { if (name.value) {
const cache = content_caches.value.find( const cache = content_caches.value.find(
(c: ContentCache) => c.name === name (c: ContentCache) => c.name === name.value
); );
if (cache) { if (cache) {
Toast.info(t("core.composables.content_cache.toast_recovered")); Toast.info(t("core.composables.content_cache.toast_recovered"));
@ -44,15 +44,15 @@ export function useContentCache(
}; };
const handleSetContentCache = debounce(() => { const handleSetContentCache = debounce(() => {
if (name) { if (name.value) {
const cache = content_caches.value.find( const cache = content_caches.value.find(
(c: ContentCache) => c.name === name (c: ContentCache) => c.name === name.value
); );
if (cache) { if (cache) {
cache.content = raw?.value; cache.content = raw?.value;
} else { } else {
content_caches.value.push({ content_caches.value.push({
name: name, name: name.value || "",
content: raw?.value, content: raw?.value,
}); });
} }

View File

@ -320,7 +320,7 @@ onMounted(async () => {
const { handleSetContentCache, handleResetCache, handleClearCache } = const { handleSetContentCache, handleResetCache, handleClearCache } =
useContentCache( useContentCache(
"singlePage-content-cache", "singlePage-content-cache",
routeQueryName.value as string, routeQueryName,
toRef(formState.value.content, "raw") toRef(formState.value.content, "raw")
); );
</script> </script>

View File

@ -298,7 +298,7 @@ const onSettingPublished = (post: Post) => {
}; };
// Get post data when the route contains the name parameter // Get post data when the route contains the name parameter
const name = useRouteQuery("name"); const name = useRouteQuery<string>("name");
onMounted(async () => { onMounted(async () => {
if (name.value) { if (name.value) {
// fetch post // fetch post
@ -333,7 +333,7 @@ onMounted(async () => {
const { handleSetContentCache, handleResetCache, handleClearCache } = const { handleSetContentCache, handleResetCache, handleClearCache } =
useContentCache( useContentCache(
"post-content-cache", "post-content-cache",
name.value as string, name,
toRef(formState.value.content, "raw") toRef(formState.value.content, "raw")
); );
</script> </script>