[feat] regex import and export
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
<script lang="ts">
|
||||
import { PlusIcon } from "lucide-svelte";
|
||||
import { DownloadIcon, FolderUpIcon, PlusIcon } from "lucide-svelte";
|
||||
import { language } from "src/lang";
|
||||
import Help from "src/lib/Others/Help.svelte";
|
||||
import RegexData from "src/lib/SideBars/RegexData.svelte";
|
||||
import { DataBase } from "src/ts/database";
|
||||
import { exportRegex, importRegex } from "src/ts/process/scripts";
|
||||
</script>
|
||||
<h2 class="mb-2 text-2xl font-bold mt-2">{language.globalRegexScript} <Help key="regexScript" /></h2>
|
||||
<table class="contain w-full max-w-full tabler mt-4 flex flex-col p-2 gap-2 border-selected border-1">
|
||||
<table class="contain w-full max-w-full tabler mt-4 flex flex-col p-2 gap-2 border-selected border-1 bg-darkbg">
|
||||
{#if $DataBase.globalscript.length === 0}
|
||||
<div class="text-gray-500">No Scripts</div>
|
||||
{/if}
|
||||
@@ -18,13 +19,21 @@
|
||||
}}/>
|
||||
{/each}
|
||||
</table>
|
||||
<button class="font-medium cursor-pointer hover:text-green-500 mb-2" on:click={() => {
|
||||
let script = $DataBase.globalscript
|
||||
script.push({
|
||||
comment: "",
|
||||
in: "",
|
||||
out: "",
|
||||
type: "editinput"
|
||||
})
|
||||
$DataBase.globalscript = script
|
||||
}}><PlusIcon /></button>
|
||||
<div class="text-gray-500 mt-2 flex gap-2">
|
||||
<button class="font-medium cursor-pointer hover:text-green-500" on:click={() => {
|
||||
let script = $DataBase.globalscript
|
||||
script.push({
|
||||
comment: "",
|
||||
in: "",
|
||||
out: "",
|
||||
type: "editinput"
|
||||
})
|
||||
$DataBase.globalscript = script
|
||||
}}><PlusIcon /></button>
|
||||
<button class="font-medium cursor-pointer hover:text-green-500" on:click={() => {
|
||||
exportRegex()
|
||||
}}><DownloadIcon /></button>
|
||||
<button class="font-medium cursor-pointer hover:text-green-500" on:click={() => {
|
||||
importRegex()
|
||||
}}><FolderUpIcon /></button>
|
||||
</div>
|
||||
@@ -1,6 +1,10 @@
|
||||
import { get } from "svelte/store";
|
||||
import { CharEmotion, selectedCharID } from "../stores";
|
||||
import { DataBase, type character } from "../database";
|
||||
import { DataBase, setDatabase, type character, type customscript } from "../database";
|
||||
import { downloadFile } from "../globalApi";
|
||||
import { alertError, alertNormal } from "../alert";
|
||||
import { language } from "src/lang";
|
||||
import { selectSingleFile } from "../util";
|
||||
|
||||
const dreg = /{{data}}/g
|
||||
const randomness = /\|\|\|/g
|
||||
@@ -11,10 +15,47 @@ export function processScript(char:character, data:string, mode:ScriptMode){
|
||||
return processScriptFull(char, data, mode).data
|
||||
}
|
||||
|
||||
export function exportRegex(){
|
||||
let db = get(DataBase)
|
||||
const script = db.globalscript
|
||||
const data = Buffer.from(JSON.stringify({
|
||||
type: 'regex',
|
||||
data: script
|
||||
}), 'utf-8')
|
||||
downloadFile(`regexscript_export.json`,data)
|
||||
alertNormal(language.successExport)
|
||||
}
|
||||
|
||||
export async function importRegex(){
|
||||
const filedata = (await selectSingleFile(['json'])).data
|
||||
if(!filedata){
|
||||
return
|
||||
}
|
||||
let db = get(DataBase)
|
||||
try {
|
||||
const imported= JSON.parse(Buffer.from(filedata).toString('utf-8'))
|
||||
if(imported.type === 'regex' && imported.data){
|
||||
const datas:customscript[] = imported.data
|
||||
const script = db.globalscript
|
||||
for(const data of datas){
|
||||
script.push(data)
|
||||
}
|
||||
db.globalscript = script
|
||||
setDatabase(db)
|
||||
}
|
||||
else{
|
||||
alertError("File invaid or corrupted")
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
alertError(`${error}`)
|
||||
}
|
||||
}
|
||||
|
||||
export function processScriptFull(char:character, data:string, mode:ScriptMode){
|
||||
let db = get(DataBase)
|
||||
let emoChanged = false
|
||||
const scripts = char.customscript.concat(db.globalscript ?? [])
|
||||
const scripts = (db.globalscript ?? []).concat(char.customscript)
|
||||
for (const script of scripts){
|
||||
if(script.type === mode){
|
||||
const reg = new RegExp(script.in,'g')
|
||||
|
||||
Reference in New Issue
Block a user