Skip to content

Commit 64299d2

Browse files
authored
refactor: add storage engine (#1118)
* feat: add storage adapter * refactor: add storage engine
1 parent a349c95 commit 64299d2

File tree

18 files changed

+746
-363
lines changed

18 files changed

+746
-363
lines changed

apps/web/src/components/ai/chat-box/AIAssistantPanel.vue

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import useAIConfigStore from '@/stores/aiConfig'
2626
import { useEditorStore } from '@/stores/editor'
2727
import { useQuickCommands } from '@/stores/quickCommands'
2828
import { useUIStore } from '@/stores/ui'
29+
import { store } from '@/utils'
2930
import { copyPlain } from '@/utils/clipboard'
3031
3132
/* ---------- 组件属性 ---------- */
@@ -107,7 +108,7 @@ function applyQuickCommand(cmd: QuickCommandRuntime) {
107108
108109
/* ---------- 初始数据 ---------- */
109110
onMounted(async () => {
110-
const saved = localStorage.getItem(memoryKey)
111+
const saved = await store.get(memoryKey)
111112
messages.value = saved ? JSON.parse(saved) : getDefaultMessages()
112113
await scrollToBottom(true)
113114
})
@@ -186,13 +187,13 @@ function editMessage(content: string) {
186187
})
187188
}
188189
189-
function resetMessages() {
190+
async function resetMessages() {
190191
if (fetchController.value) {
191192
fetchController.value.abort()
192193
fetchController.value = null
193194
}
194195
messages.value = getDefaultMessages()
195-
localStorage.setItem(memoryKey, JSON.stringify(messages.value))
196+
await store.setJSON(memoryKey, messages.value)
196197
scrollToBottom(true)
197198
}
198199
@@ -380,7 +381,7 @@ async function sendMessage() {
380381
await scrollToBottom(true)
381382
}
382383
finally {
383-
localStorage.setItem(memoryKey, JSON.stringify(messages.value))
384+
await store.setJSON(memoryKey, messages.value)
384385
loading.value = false
385386
fetchController.value = null
386387
}

apps/web/src/components/ai/image-generator/AIImageGeneratorPanel.vue

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { Textarea } from '@/components/ui/textarea'
2222
import useAIImageConfigStore from '@/stores/aiImageConfig'
2323
import { useEditorStore } from '@/stores/editor'
2424
import { useUIStore } from '@/stores/ui'
25+
import { store } from '@/utils'
2526
import { copyPlain } from '@/utils/clipboard'
2627
import AIImageConfig from './AIImageConfig.vue'
2728
@@ -69,28 +70,27 @@ function isImageExpired(timestamp: number): boolean {
6970
return now - timestamp > EXPIRY_TIME
7071
}
7172
72-
function cleanExpiredImages() {
73-
const savedImages = localStorage.getItem(`ai_generated_images`)
74-
const savedPrompts = localStorage.getItem(`ai_image_prompts`)
75-
const savedTimestamps = localStorage.getItem(`ai_image_timestamps`)
73+
async function cleanExpiredImages() {
74+
const savedImages = await store.get(`ai_generated_images`)
75+
const savedTimestamps = await store.get(`ai_image_timestamps`)
7676
7777
if (!savedImages) {
7878
return
7979
}
8080
81-
const images = JSON.parse(savedImages)
82-
const prompts = savedPrompts ? JSON.parse(savedPrompts) : []
83-
const timestamps = savedTimestamps ? JSON.parse(savedTimestamps) : []
81+
const images = await store.getJSON(`ai_generated_images`, [])
82+
const prompts = await store.getJSON(`ai_image_prompts`, [])
83+
const timestamps = await store.getJSON(`ai_image_timestamps`, [])
8484
8585
// 如果没有时间戳数据,说明是旧版本,默认清除所有数据
8686
if (!savedTimestamps || timestamps.length === 0) {
8787
console.log(`🧹 检测到旧版本数据,清除所有过期图片`)
8888
generatedImages.value = []
8989
imagePrompts.value = []
9090
imageTimestamps.value = []
91-
localStorage.removeItem(`ai_generated_images`)
92-
localStorage.removeItem(`ai_image_prompts`)
93-
localStorage.removeItem(`ai_image_timestamps`)
91+
await store.remove(`ai_generated_images`)
92+
await store.remove(`ai_image_prompts`)
93+
await store.remove(`ai_image_timestamps`)
9494
return
9595
}
9696
@@ -111,28 +111,28 @@ function cleanExpiredImages() {
111111
imagePrompts.value = validPrompts
112112
imageTimestamps.value = validTimestamps
113113
114-
// 如果有数据被清除,更新localStorage
114+
// 如果有数据被清除,更新存储
115115
if (validImages.length < images.length) {
116116
console.log(`🧹 清除了 ${images.length - validImages.length} 张过期图片`)
117117
if (validImages.length > 0) {
118-
localStorage.setItem(`ai_generated_images`, JSON.stringify(validImages))
119-
localStorage.setItem(`ai_image_prompts`, JSON.stringify(validPrompts))
120-
localStorage.setItem(`ai_image_timestamps`, JSON.stringify(validTimestamps))
118+
await store.setJSON(`ai_generated_images`, validImages)
119+
await store.setJSON(`ai_image_prompts`, validPrompts)
120+
await store.setJSON(`ai_image_timestamps`, validTimestamps)
121121
}
122122
else {
123-
localStorage.removeItem(`ai_generated_images`)
124-
localStorage.removeItem(`ai_image_prompts`)
125-
localStorage.removeItem(`ai_image_timestamps`)
123+
await store.remove(`ai_generated_images`)
124+
await store.remove(`ai_image_prompts`)
125+
await store.remove(`ai_image_timestamps`)
126126
}
127127
}
128128
129129
console.log(`📊 过期检查完成,有效图片数量:`, validImages.length)
130130
}
131131
132132
/* ---------- 初始数据 ---------- */
133-
onMounted(() => {
133+
onMounted(async () => {
134134
// 先进行过期检查和清理
135-
cleanExpiredImages()
135+
await cleanExpiredImages()
136136
137137
// 确保数组长度一致
138138
const imagesLength = generatedImages.value.length
@@ -147,9 +147,9 @@ onMounted(() => {
147147
generatedImages.value = []
148148
imagePrompts.value = []
149149
imageTimestamps.value = []
150-
localStorage.removeItem(`ai_generated_images`)
151-
localStorage.removeItem(`ai_image_prompts`)
152-
localStorage.removeItem(`ai_image_timestamps`)
150+
await store.remove(`ai_generated_images`)
151+
await store.remove(`ai_image_prompts`)
152+
await store.remove(`ai_image_timestamps`)
153153
}
154154
else {
155155
// 补齐较短的数组
@@ -274,9 +274,9 @@ async function generateImage() {
274274
imageTimestamps.value = imageTimestamps.value.slice(0, 20)
275275
}
276276
277-
localStorage.setItem(`ai_generated_images`, JSON.stringify(generatedImages.value))
278-
localStorage.setItem(`ai_image_prompts`, JSON.stringify(imagePrompts.value))
279-
localStorage.setItem(`ai_image_timestamps`, JSON.stringify(imageTimestamps.value))
277+
await store.setJSON(`ai_generated_images`, generatedImages.value)
278+
await store.setJSON(`ai_image_prompts`, imagePrompts.value)
279+
await store.setJSON(`ai_image_timestamps`, imageTimestamps.value)
280280
281281
// 清空输入框
282282
prompt.value = ``
@@ -311,14 +311,14 @@ function cancelGeneration() {
311311
}
312312
313313
/* ---------- 清空图像 ---------- */
314-
function clearImages() {
314+
async function clearImages() {
315315
generatedImages.value = []
316316
imagePrompts.value = []
317317
imageTimestamps.value = []
318318
currentImageIndex.value = 0
319-
localStorage.removeItem(`ai_generated_images`)
320-
localStorage.removeItem(`ai_image_prompts`)
321-
localStorage.removeItem(`ai_image_timestamps`)
319+
await store.remove(`ai_generated_images`)
320+
await store.remove(`ai_image_prompts`)
321+
await store.remove(`ai_image_timestamps`)
322322
}
323323
324324
/* ---------- 下载图像 ---------- */
@@ -446,9 +446,9 @@ async function regenerateWithPrompt(promptText: string) {
446446
imageTimestamps.value = imageTimestamps.value.slice(0, 20)
447447
}
448448
449-
localStorage.setItem(`ai_generated_images`, JSON.stringify(generatedImages.value))
450-
localStorage.setItem(`ai_image_prompts`, JSON.stringify(imagePrompts.value))
451-
localStorage.setItem(`ai_image_timestamps`, JSON.stringify(imageTimestamps.value))
449+
await store.setJSON(`ai_generated_images`, generatedImages.value)
450+
await store.setJSON(`ai_image_prompts`, imagePrompts.value)
451+
await store.setJSON(`ai_image_timestamps`, imageTimestamps.value)
452452
}
453453
}
454454
else {

apps/web/src/components/editor/CustomUploadForm.vue

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import { Compartment } from '@codemirror/state'
33
import { EditorView } from '@codemirror/view'
44
import { javascriptSetup, theme } from '@md/shared'
55
import { useUIStore } from '@/stores/ui'
6-
import { removeLeft } from '@/utils'
6+
import { removeLeft, store } from '@/utils'
77
8-
const code = useLocalStorage(`formCustomConfig`, removeLeft(`
8+
const code = store.reactive(`formCustomConfig`, removeLeft(`
99
const { file, util, okCb, errCb } = CUSTOM_ARG
10-
const param = new FormData()
10+
param = new FormData()
1111
param.append('file', file)
1212
util.axios.post('${window.location.origin}/upload', param, {
1313
headers: { 'Content-Type': 'multipart/form-data' }
@@ -51,7 +51,6 @@ onUnmounted(() => {
5151
5252
function formCustomSave() {
5353
const str = editor.value!.state.doc.toString()
54-
localStorage.setItem(`formCustomConfig`, str)
5554
code.value = str
5655
toast.success(`保存成功`)
5756
}

apps/web/src/components/editor/InsertMpCardDialog.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { toTypedSchema } from '@vee-validate/yup'
33
import { Field, Form } from 'vee-validate'
44
import * as yup from 'yup'
55
import { useEditorStore } from '@/stores/editor'
6-
import { addPrefix } from '@/utils'
6+
import { addPrefix, store } from '@/utils'
77
88
/** 编辑器实例和全局弹窗状态 */
99
const editorStore = useEditorStore()
@@ -29,7 +29,7 @@ interface Config {
2929
}
3030
3131
/** 表单字段 */
32-
const config = useStorage<Config>(addPrefix(`mp-profile`), {
32+
const config = store.reactive<Config>(addPrefix(`mp-profile`), {
3333
id: ``,
3434
name: ``,
3535
logo: ``,

0 commit comments

Comments
 (0)