2023-01-19 08:57:25 +01:00
|
|
|
<script>
|
|
|
|
import Icon from './icon.svelte';
|
|
|
|
import Modal from './modal.svelte';
|
2023-05-29 17:07:58 +02:00
|
|
|
import { createEventDispatcher, onDestroy } from 'svelte';
|
|
|
|
import ObjectEditor from './objecteditor.svelte';
|
|
|
|
import { jsonLooseParse } from '$lib/strings';
|
2023-01-19 08:57:25 +01:00
|
|
|
|
|
|
|
export let data;
|
2023-05-29 17:07:58 +02:00
|
|
|
export let saveable = false;
|
2023-01-19 08:57:25 +01:00
|
|
|
|
2023-05-29 17:07:58 +02:00
|
|
|
const dispatch = createEventDispatcher();
|
2023-01-19 08:57:25 +01:00
|
|
|
let copySucceeded = false;
|
|
|
|
let timeout;
|
2023-05-29 17:07:58 +02:00
|
|
|
let text = JSON.stringify(data, undefined, '\t');
|
|
|
|
let newData;
|
|
|
|
let invalid = false;
|
2023-01-20 13:54:57 +01:00
|
|
|
|
2023-05-29 17:07:58 +02:00
|
|
|
$: {
|
|
|
|
try {
|
|
|
|
newData = jsonLooseParse(text);
|
|
|
|
}
|
|
|
|
catch {
|
|
|
|
invalid = true;
|
2023-01-20 13:54:57 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-19 08:57:25 +01:00
|
|
|
|
|
|
|
async function copy() {
|
2023-05-29 17:07:58 +02:00
|
|
|
await navigator.clipboard.writeText(text);
|
2023-01-19 08:57:25 +01:00
|
|
|
copySucceeded = true;
|
|
|
|
timeout = setTimeout(() => copySucceeded = false, 1500);
|
|
|
|
}
|
|
|
|
|
2023-05-29 17:07:58 +02:00
|
|
|
function close() {
|
|
|
|
data = undefined;
|
|
|
|
text = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
function save() {
|
|
|
|
dispatch('save', text);
|
|
|
|
}
|
|
|
|
|
2023-01-19 08:57:25 +01:00
|
|
|
onDestroy(() => clearTimeout(timeout));
|
|
|
|
</script>
|
|
|
|
|
|
|
|
{#if data}
|
2023-05-29 17:07:58 +02:00
|
|
|
<Modal bind:show={data} contentPadding={false}>
|
2023-01-19 08:57:25 +01:00
|
|
|
<div class="objectviewer">
|
2023-05-29 17:07:58 +02:00
|
|
|
<ObjectEditor {text} />
|
2023-01-19 08:57:25 +01:00
|
|
|
</div>
|
2023-05-29 17:07:58 +02:00
|
|
|
|
|
|
|
<svelte:fragment slot="footer">
|
|
|
|
{#if saveable}
|
|
|
|
<button class="btn" on:click={save} disabled={invalid}>
|
|
|
|
<Icon name="save" /> Save
|
|
|
|
</button>
|
|
|
|
{/if}
|
|
|
|
|
|
|
|
<button class="btn secondary" on:click={close}>
|
|
|
|
<Icon name="x" /> Close
|
|
|
|
</button>
|
|
|
|
|
|
|
|
<button class="btn secondary" on:click={copy}>
|
|
|
|
<Icon name={copySucceeded ? 'check' : 'clipboard'} /> Copy
|
|
|
|
</button>
|
|
|
|
</svelte:fragment>
|
2023-01-19 08:57:25 +01:00
|
|
|
</Modal>
|
|
|
|
{/if}
|
|
|
|
|
|
|
|
<style>
|
|
|
|
.objectviewer {
|
2023-05-29 17:07:58 +02:00
|
|
|
display: flex;
|
2023-01-19 08:57:25 +01:00
|
|
|
position: relative;
|
2023-05-29 17:07:58 +02:00
|
|
|
justify-content: stretch;
|
|
|
|
align-items: stretch;
|
|
|
|
height: 100%;
|
2023-01-19 08:57:25 +01:00
|
|
|
}
|
|
|
|
</style>
|