refactor: Add importPreset function to handle importing presets in characterCards.ts

This commit is contained in:
kwaroran
2024-05-30 13:12:49 +09:00
parent 170e673a19
commit c4651408a6
2 changed files with 38 additions and 4 deletions

View File

@@ -1,9 +1,9 @@
import { get, writable, type Writable } from "svelte/store"
import { alertCardExport, alertConfirm, alertError, alertInput, alertMd, alertNormal, alertSelect, alertStore, alertTOS, alertWait } from "./alert"
import { DataBase, defaultSdDataFunc, type character, setDatabase, type customscript, type loreSettings, type loreBook, type triggerscript } from "./storage/database"
import { DataBase, defaultSdDataFunc, type character, setDatabase, type customscript, type loreSettings, type loreBook, type triggerscript, importPreset } from "./storage/database"
import { checkNullish, decryptBuffer, encryptBuffer, isKnownUri, selectFileByDom, selectMultipleFile, sleep } from "./util"
import { language } from "src/lang"
import { v4 as uuidv4 } from 'uuid';
import { v4 as uuidv4, v4 } from 'uuid';
import { characterFormatUpdate } from "./characters"
import { AppendableBuffer, checkCharOrder, downloadFile, loadAsset, LocalWriter, openURL, readImage, saveAsset, VirtualWriter } from "./storage/globalApi"
import { CurrentCharacter, selectedCharID } from "./stores"
@@ -242,6 +242,35 @@ export async function characterURLImport() {
alertError(language.errors.noData)
return null
}
const hash = location.hash
if(hash.startsWith('#import_module=')){
const data = hash.replace('#import_module=', '')
const importData = JSON.parse(Buffer.from(decodeURIComponent(data), 'base64').toString('utf-8'))
importData.id = v4()
const db = get(DataBase)
if(importData.lowLevelAccess){
const conf = await alertConfirm(language.lowLevelAccessConfirm)
if(!conf){
return false
}
}
db.modules.push(importData)
setDatabase(db)
return
}
if(hash.startsWith('#import_preset=')){
const data = hash.replace('#import_preset=', '')
const importData =Buffer.from(decodeURIComponent(data), 'base64')
await importPreset({
name: 'imported.risupreset',
data: importData
})
return
}
}

View File

@@ -1301,8 +1301,13 @@ export async function downloadPreset(id:number, type:'json'|'risupreset'|'return
}
export async function importPreset(){
const f = await selectSingleFile(["json", "preset", "risupreset"])
export async function importPreset(f:{
name:string
data:Uint8Array
}|null = null){
if(!f){
f = await selectSingleFile(["json", "preset", "risupreset"])
}
if(!f){
return
}