Browse Source

feat(store): 添加草稿存储功能及UUID生成工具

- 在pinia中创建draft store用于管理草稿数据
- 添加generateUUID工具函数用于生成唯一标识符
- 实现草稿的添加和存储功能
Sakulin 2 months ago
parent
commit
5b65768c78
2 changed files with 35 additions and 2 deletions
  1. 24 0
      src/stores/draft.ts
  2. 11 2
      src/utils/index.ts

+ 24 - 0
src/stores/draft.ts

@@ -0,0 +1,24 @@
+import { defineStore } from 'pinia'
+import { generateUUID } from '@/utils'
+
+export interface DraftBody {
+  content: string
+  tags: string[]
+  title: string
+  cate: string
+  timemill: number
+}
+
+export const useDraftStore = defineStore('draft', {
+  state: () => ({
+    drafts: {} as { [key: string]: DraftBody },
+  }),
+  actions: {
+    push(draft: DraftBody, id: string | null) {
+      const s = (typeof id == 'string') ? id : generateUUID()
+      this.drafts[s] = draft
+      return s
+    },
+  },
+  persist: true,
+})

+ 11 - 2
src/utils/index.ts

@@ -32,9 +32,9 @@ export function isDark() {
 
 export function colorMatchTheme(hexColor: string) {
   if (isDark()) {
-    return lightenHexColor(hexColor);
+    return lightenHexColor(hexColor)
   }
-  return hexColor;
+  return hexColor
 }
 
 export function formateDateAccurateToDay(date: Date | string | number, nick = true): string {
@@ -68,3 +68,12 @@ export async function delay(ms: number) {
     setTimeout(resolve, ms)
   })
 }
+
+export function generateUUID() {
+  let d = new Date().getTime()
+  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
+    const r = (d + Math.random() * 16) % 16 | 0
+    d = Math.floor(d / 16)
+    return (c == 'x' ? r : (r & 0x3) | 0x8).toString(16)
+  })
+}