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