refactor: use nuxt compatiable 4 folder

This commit is contained in:
YunYouJun
2024-09-15 18:07:50 +08:00
parent 7a52b024dd
commit 41bdc3346f
96 changed files with 2577 additions and 2673 deletions

11
app/utils/cookbook.ts Normal file
View File

@@ -0,0 +1,11 @@
import type { Cookbook } from '~/types'
export const defaultCookbook: Cookbook = {
id: 'default',
title: '默认菜谱',
description: '记录了一些特殊时期常用的菜谱',
author: [''],
recipes: [],
updatedAt: '',
createdAt: '2021-04-04',
}

32
app/utils/db.ts Normal file
View File

@@ -0,0 +1,32 @@
import type { Table } from 'dexie'
import Dexie from 'dexie'
import type { RecipeItem } from '~/types'
export interface DbRecipeItem extends RecipeItem {
id?: number
}
export class MySubClassedDexie extends Dexie {
recipes!: Table<DbRecipeItem>
constructor() {
super('cook-db')
this.version(1).stores({
recipes: '++id, name, stuff, bv, difficulty, tags, methods, tools, link, description', // Primary key and indexed props
})
}
}
export const db = new MySubClassedDexie()
export async function initDb() {
const { default: recipeData } = await import('../data/recipe.json')
return db.recipes.bulkPut(
(recipeData as RecipeItem[]).map((item, i) => ({
id: i,
...item,
})),
)
}

16
app/utils/index.ts Normal file
View File

@@ -0,0 +1,16 @@
import { meat, staple, vegetable } from '~/data/food'
const foodItems = [...vegetable, ...meat, ...staple]
const foodEmojiMap = new Map()
foodItems.forEach((item) => {
foodEmojiMap.set(item.name, item.emoji)
})
/**
* get emojis from stuff name array
* @param stuff
*/
export function getEmojisFromStuff(stuff: string[]) {
const emojis: string[] = stuff.map(name => foodEmojiMap.get(name)).filter(item => !!item)
return emojis
}

35
app/utils/pwa.ts Normal file
View File

@@ -0,0 +1,35 @@
import { isClient } from '@vueuse/core'
/**
* - https://web.dev/customize-install/#detect-install
* - [Trigger installation from your PWA](https://developer.mozilla.org/en-US/docs/Web/Progressive_web_apps/How_to/Trigger_install_prompt)
*/
export function installPrompt() {
if (!isClient)
return
const app = useAppStore()
window.addEventListener('beforeinstallprompt', (e) => {
// Prevent the mini-infobar from appearing on mobile
e.preventDefault()
// Stash the event so it can be triggered later.
app.deferredPrompt = e
// Update UI notify the user they can install the PWA
// showInstallPromotion()
// Optionally, send analytics event that PWA install promo was shown.
// eslint-disable-next-line no-console
console.log('\'beforeinstallprompt\' event was fired.')
})
window.addEventListener('appinstalled', () => {
// Hide the app-provided install promotion
// hideInstallPromotion()
// Clear the deferredPrompt so it can be garbage collected
app.deferredPrompt = null
// Optionally, send analytics event to indicate successful install
// eslint-disable-next-line no-console
console.log('PWA was installed')
})
}

15
app/utils/random.ts Normal file
View File

@@ -0,0 +1,15 @@
/**
* 生成随机数组
*/
export function generateRandomArray(length: number, total = 1) {
const randomArr: number[] = []
for (let i = 0; i < total; i++) {
const randomIndex = Math.floor(Math.random() * length)
if (randomArr.includes(randomIndex)) {
i--
continue
}
randomArr.push(randomIndex)
}
return randomArr
}

10
app/utils/settings.ts Normal file
View File

@@ -0,0 +1,10 @@
export interface UserSettings {
/**
* 保留本地数据
*/
keepLocalData: boolean
}
export const defaultSettings: UserSettings = {
keepLocalData: true,
}