master
svelte 56 lines 2.02 KB
Raw
1 <script lang="ts">
2 import { onMount } from "svelte";
3 import Input from "../utility/Input.svelte";
4 import Popup from "../utility/Popup.svelte";
5 import Button from "../utility/Button.svelte";
6 import { _openPopup } from "../../App.svelte";
7 import { checkValidFileName } from "../File";
8 import { fs, invoke, path as p } from "@tauri-apps/api";
9
10 export let title;
11 export let buttons;
12 export let description = "";
13 export let options = {label: null, placeholder: "Enter text..."};
14 export let path = "";
15 let invalid = false;
16 let helpText = ""
17 let invalidText = "";
18
19 onMount(() => {
20 if (!options.placeholder) {
21 options.placeholder = "Enter text..."
22 }
23 })
24
25 let value = "";
26 async function handleButtonClick(buttonAction, cancel) {
27 await buttonAction(value);
28 if (!invalid || cancel) {
29 _openPopup.set(false);
30 }
31 }
32 async function validateInput(button) {
33 if (!button.cancel && (!checkValidFileName(value) || value === undefined || !value)) {
34 invalid = true;
35 invalidText = `{${value === "" ? "null" : value}} is not a valid file or directory name`;
36 return;
37 }
38 if (!button.cancel && await fs.exists(`${path}${p.sep}${value}`)) {
39 invalid = true;
40 if (await invoke("is_file", {path: path})) {
41 path = path.slice(0, path.indexOf(value));
42 }
43 invalidText = `${value} in ${path} already exists`;
44 return;
45 }
46 await handleButtonClick(button.action, button.cancel);
47 }
48 </script>
49 <Popup bind:open={$_openPopup} {title} {description}>
50 <Input bind:value bind:invalid label={options.label} placeholder={options.placeholder} hintText={helpText} invalidText={invalidText} />
51 <svelte:fragment slot="buttons">
52 {#each buttons as button}
53 <Button label={button.name} style={button.style} on:click={() => {validateInput(button)}} />
54 {/each}
55 </svelte:fragment>
56 </Popup>