pocketbase/ui/src/components/settings/EmailTemplateAccordion.svelte

150 lines
4.9 KiB
Svelte
Raw Normal View History

2022-10-30 16:28:14 +08:00
<script context="module">
let cachedEditorComponent;
</script>
<script>
import { scale } from "svelte/transition";
import tooltip from "@/actions/tooltip";
import { errors, removeError } from "@/stores/errors";
import { addInfoToast } from "@/stores/toasts";
import CommonHelper from "@/utils/CommonHelper";
import Field from "@/components/base/Field.svelte";
2022-08-16 12:36:15 +08:00
import Accordion from "@/components/base/Accordion.svelte";
export let key;
export let title;
export let config = {};
let accordion;
2022-10-30 16:28:14 +08:00
let editorComponent = cachedEditorComponent;
2022-08-16 12:36:15 +08:00
let isEditorComponentLoading = false;
$: hasErrors = !CommonHelper.isEmpty(CommonHelper.getNestedVal($errors, key));
$: if (!config.enabled) {
removeError(key);
}
export function expand() {
accordion?.expand();
}
export function collapse() {
accordion?.collapse();
}
export function collapseSiblings() {
accordion?.collapseSiblings();
}
2022-08-16 12:36:15 +08:00
async function loadEditorComponent() {
if (editorComponent || isEditorComponentLoading) {
return; // already loaded or in the process
}
isEditorComponentLoading = true;
editorComponent = (await import("@/components/base/CodeEditor.svelte")).default;
2022-10-30 16:28:14 +08:00
cachedEditorComponent = editorComponent;
2022-08-16 12:36:15 +08:00
isEditorComponentLoading = false;
}
function copy(param) {
CommonHelper.copyToClipboard(param);
addInfoToast(`Copied ${param} to clipboard`, 2000);
}
loadEditorComponent();
</script>
<Accordion bind:this={accordion} on:expand on:collapse on:toggle {...$$restProps}>
<svelte:fragment slot="header">
<div class="inline-flex">
<i class="ri-draft-line" />
<span class="txt">{title}</span>
</div>
<div class="flex-fill" />
{#if hasErrors}
<i
class="ri-error-warning-fill txt-danger"
2023-01-08 04:25:56 +08:00
transition:scale|local={{ duration: 150, start: 0.7 }}
use:tooltip={{ text: "Has errors", position: "left" }}
/>
{/if}
</svelte:fragment>
<Field class="form-field required" name="{key}.subject" let:uniqueId>
<label for={uniqueId}>Subject</label>
<input type="text" id={uniqueId} bind:value={config.subject} spellcheck="false" required />
<div class="help-block">
Available placeholder parameters:
<span class="label label-sm link-primary txt-mono" on:click={() => copy("{APP_NAME}")}>
{"{APP_NAME}"}
</span>,
<span class="label label-sm link-primary txt-mono" on:click={() => copy("{APP_URL}")}>
{"{APP_URL}"}
</span>.
</div>
</Field>
<Field class="form-field required" name="{key}.actionUrl" let:uniqueId>
<label for={uniqueId}>Action URL</label>
<input type="text" id={uniqueId} bind:value={config.actionUrl} spellcheck="false" required />
<div class="help-block">
Available placeholder parameters:
<span class="label label-sm link-primary txt-mono" on:click={() => copy("{APP_NAME}")}>
{"{APP_NAME}"}
</span>,
<span class="label label-sm link-primary txt-mono" on:click={() => copy("{APP_URL}")}>
{"{APP_URL}"}
</span>,
<span
class="label label-sm link-primary txt-mono"
title="Required parameter"
on:click={() => copy("{TOKEN}")}>{"{TOKEN}"}</span
>.
</div>
</Field>
<Field class="form-field m-0 required" name="{key}.body" let:uniqueId>
<label for={uniqueId}>Body (HTML)</label>
2022-08-16 12:36:15 +08:00
{#if editorComponent && !isEditorComponentLoading}
<svelte:component this={editorComponent} id={uniqueId} language="html" bind:value={config.body} />
2022-08-16 12:36:15 +08:00
{:else}
<textarea
id={uniqueId}
class="txt-mono"
spellcheck="false"
rows="14"
2022-08-16 12:36:15 +08:00
required
bind:value={config.body}
/>
{/if}
<div class="help-block">
Available placeholder parameters:
<span class="label label-sm link-primary txt-mono" on:click={() => copy("{APP_NAME}")}>
{"{APP_NAME}"}
</span>,
<span class="label label-sm link-primary txt-mono" on:click={() => copy("{APP_URL}")}>
{"{APP_URL}"}
</span>,
<span class="label label-sm link-primary txt-mono" on:click={() => copy("{TOKEN}")}>
{"{TOKEN}"}
</span>,
<span
class="label label-sm link-primary txt-mono"
title="Required parameter"
on:click={() => copy("{ACTION_URL}")}
>
{"{ACTION_URL}"}
</span>.
</div>
</Field>
</Accordion>