-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Snippets for randomize config implemented.
- Loading branch information
Showing
5 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/web/components/config-editor/components/widgets/randomize-config-snippets.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<script setup lang="ts"> | ||
import { ref } from "vue"; | ||
import { useSnippetsStore } from "@/web/store/snippets"; | ||
import { useConfigStore } from "@/web/store/config"; | ||
import type { RandomTypesConfig } from "@/lib/types/config"; | ||
const snippetsStore = useSnippetsStore(); | ||
const configStore = useConfigStore(); | ||
const newSnippetName = ref(''); | ||
const saveSnippet = () => { | ||
snippetsStore.addRandomTypesConfig(configStore.randomTypesConfig, newSnippetName.value); | ||
newSnippetName.value = ''; | ||
} | ||
const removeSnippet = (index: number) => { | ||
if (!confirm('Are you sure?')) { | ||
return; | ||
} | ||
snippetsStore.removeRandomTypesConfig(index); | ||
} | ||
const applySnippet = (config: RandomTypesConfig) => { | ||
console.log('APPLY', config); | ||
configStore.setRandomTypesConfig(config); | ||
} | ||
</script> | ||
|
||
<template> | ||
<div class="input-group mb-3"> | ||
<input type="text" class="form-control" placeholder="Input snippet name" v-model="newSnippetName" /> | ||
<div class="input-group-append"> | ||
<button class="btn btn-outline-secondary" @click="saveSnippet" :disabled="!newSnippetName">Save</button> | ||
</div> | ||
</div> | ||
<div class="list-group"> | ||
<div class="list-group-item d-flex justify-content-between align-items-center" v-for="(snippet, index) in snippetsStore.randomTypesConfigList" :key="index"> | ||
<div class="list-item__name"> | ||
{{ snippet.name }} | ||
</div> | ||
<div class="btn-group"> | ||
<button class="btn btn-outline-secondary" @click="applySnippet(snippet.value)">Apply</button> | ||
<button class="btn btn-outline-secondary" @click="removeSnippet(index)">Remove</button> | ||
</div> | ||
</div> | ||
</div> | ||
</template> | ||
|
||
<style scoped lang="scss"> | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { defineStore } from "pinia"; | ||
|
||
type StoredData = Record<string, unknown> | unknown[]; | ||
|
||
export const useLocalStore = defineStore("local", () => { | ||
const get = (alias: string) => { | ||
return JSON.parse(localStorage.getItem(alias) as string); | ||
} | ||
|
||
const set = (alias: string, value: StoredData) => { | ||
localStorage.setItem(alias, JSON.stringify(value)); | ||
} | ||
|
||
const remove = (alias: string) => { | ||
localStorage.removeItem(alias); | ||
} | ||
|
||
const addToArray = (alias: string, value: StoredData): number => { | ||
const old = (get(alias) ?? []) as unknown[]; | ||
set(alias, [...old, value]); | ||
return old.length; | ||
} | ||
|
||
const getFromArray = (alias: string, index: number): StoredData => { | ||
const old = get(alias); | ||
return old[index]; | ||
} | ||
|
||
const removeFromArray = (alias: string, index: number) => { | ||
const old = get(alias); | ||
set(alias, [...old.slice(0, index), ...old.slice(index + 1)]); | ||
} | ||
|
||
return { | ||
set, | ||
get, | ||
remove, | ||
addToArray, | ||
getFromArray, | ||
removeFromArray, | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import type { RandomTypesConfig } from "@/lib/types/config"; | ||
import { defineStore } from "pinia"; | ||
import { useLocalStore } from "./local"; | ||
import { ref } from "vue"; | ||
|
||
type StoredRandomTypesConfig = { | ||
name: string; | ||
value: RandomTypesConfig; | ||
}; | ||
|
||
export const useSnippetsStore = defineStore("snippets", () => { | ||
const localStore = useLocalStore(); | ||
const randomTypesConfigList = ref<StoredRandomTypesConfig[]>(localStore.get('randomTypesConfig') ?? []); | ||
|
||
const updateRandomTypesConfigList = () => { | ||
randomTypesConfigList.value = localStore.get('randomTypesConfig') ?? []; | ||
} | ||
|
||
const getRandomTypesConfigItem = (index: number): StoredRandomTypesConfig => { | ||
return localStore.getFromArray('randomTypesConfig', index) as StoredRandomTypesConfig; | ||
} | ||
|
||
const addRandomTypesConfig = (config: RandomTypesConfig, name: string) => { | ||
localStore.addToArray('randomTypesConfig', { name, value: config}); | ||
updateRandomTypesConfigList(); | ||
} | ||
|
||
const removeRandomTypesConfig = (index: number) => { | ||
localStore.removeFromArray('randomTypesConfig', index); | ||
updateRandomTypesConfigList(); | ||
} | ||
|
||
return { | ||
randomTypesConfigList, | ||
getRandomTypesConfigItem, | ||
addRandomTypesConfig, | ||
removeRandomTypesConfig, | ||
} | ||
}); |