| 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, path as p, invoke } from "@tauri-apps/api"; |
| 9 | |
| 10 | export let title; |
| 11 | export let buttons; |
| 12 | export let description = ""; |
| 13 | export let options = {label: "Name: ", 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 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 | // {name: "Cancel", cancel: true, style: "danger", action: () => {}} |
| 49 | </script> |
| 50 | <Popup bind:open={$_openPopup} {title} {description}> |
| 51 | <div class="rename-input"> |
| 52 | <Input bind:value bind:invalid label={"File Name"} placeholder={"Enter name..."} hintText={helpText} invalidText={invalidText} /> |
| 53 | </div> |
| 54 | <svelte:fragment slot="buttons"> |
| 55 | {#each buttons as button} |
| 56 | <Button label={button.name} style={button.style} on:click={() => {validateInput(button)}} /> |
| 57 | {/each} |
| 58 | </svelte:fragment> |
| 59 | </Popup> |
| 60 | |
| 61 | <style lang="scss"> |
| 62 | .rename-input { |
| 63 | display: flex; |
| 64 | } |
| 65 | </style> |