aotds-docks/src/routes/(editor)/export/Serialized.svelte

82 lines
1.8 KiB
Svelte
Raw Normal View History

2023-04-23 14:53:19 +00:00
<article>
<nav>
<button
2023-04-24 15:47:38 +00:00
use:clipboard={{ text: data }}
2023-04-23 14:53:19 +00:00
on:copied={copied}
on:error={copyError}>{copyLabel} <i>content_paste</i></button
>
<button on:click={handleSave}>download <i>download</i></button>
2023-05-08 20:07:16 +00:00
<div class="field suffix border">
<select bind:value={format}>
<option>json</option>
<option>yaml</option>
</select>
<i>arrow_drop_down</i>
</div>
2023-04-23 14:53:19 +00:00
</nav>
2023-05-08 20:07:16 +00:00
<pre><code>{serialized}</code></pre>
2023-04-23 14:53:19 +00:00
<a hidden {href} {download} bind:this={fileDownload} />
</article>
<script>
2023-04-24 15:47:38 +00:00
import { clipboard } from "$lib/actions/clipboard.js";
2023-05-08 20:07:16 +00:00
import yaml from "yaml";
2023-04-23 14:53:19 +00:00
2023-05-08 20:07:16 +00:00
export let data = {};
export let serialized = "Loading...";
export let format = "json";
2023-04-23 14:53:19 +00:00
let copyLabel = "clipboard";
const copied = () => {
copyLabel = "copied!";
setTimeout(() => (copyLabel = "clipboard"), 2000);
};
const copyError = () => {
copyLabel = "error copying";
setTimeout(() => (copyLabel = "clipboard"), 2000);
};
2023-04-24 15:47:38 +00:00
$: href = "data:text/plain;charset=utf-8," + encodeURIComponent(data);
$: download = (data?.identification?.shipClass || "ship") + "." + format;
2023-04-23 14:53:19 +00:00
let fileDownload;
function handleSave() {
fileDownload?.click();
}
2023-05-08 20:07:16 +00:00
const serialize = async (data, format) => {
if (format === "json") return JSON.stringify(data, null, 2);
return yaml.stringify(data);
};
$: serialize(data, format).then((s) => (serialized = s));
2023-04-23 14:53:19 +00:00
</script>
<style>
article {
background-color: var(--primary-container);
}
button {
font-size: var(--font-scale-10);
}
nav {
position: absolute;
right: 5em;
}
2023-05-08 20:07:16 +00:00
.my-switch {
display: flex;
align-items: center;
}
.my-switch > span {
display: inline-block;
font-size: var(--font-scale-10);
}
label {
margin: 0px 0.5rem;
}
2023-04-23 14:53:19 +00:00
</style>