2023-01-19 08:57:25 +01:00
|
|
|
<script>
|
2023-06-11 09:34:00 +02:00
|
|
|
import { looseJsonIsValid } from '$lib/strings';
|
2023-05-29 17:08:52 +02:00
|
|
|
import { createEventDispatcher, onDestroy } from 'svelte';
|
2023-01-19 08:57:25 +01:00
|
|
|
import Icon from './icon.svelte';
|
|
|
|
import Modal from './modal.svelte';
|
2023-05-29 17:07:58 +02:00
|
|
|
import ObjectEditor from './objecteditor.svelte';
|
2023-06-02 22:33:43 +02:00
|
|
|
import { EJSON } from 'bson';
|
2023-01-19 08:57:25 +01:00
|
|
|
|
|
|
|
export let data;
|
2023-05-29 17:07:58 +02:00
|
|
|
export let saveable = false;
|
2023-06-02 22:33:43 +02:00
|
|
|
export let successMessage = '';
|
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-06-02 22:33:43 +02:00
|
|
|
let text = EJSON.stringify(data, undefined, '\t');
|
|
|
|
$: invalid = !looseJsonIsValid(text);
|
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() {
|
2023-06-02 22:33:43 +02:00
|
|
|
dispatch('save', { text, originalData: data });
|
2023-05-29 17:07:58 +02:00
|
|
|
}
|
|
|
|
|
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-06-02 22:33:43 +02:00
|
|
|
<ObjectEditor bind:text on:updated={() => successMessage = ''} />
|
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>
|
2023-06-02 22:33:43 +02:00
|
|
|
|
|
|
|
{#if successMessage}
|
|
|
|
<span class="flash-green">{successMessage}</span>
|
|
|
|
{/if}
|
2023-05-29 17:07:58 +02:00
|
|
|
</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
|
|
|
}
|
2023-06-02 22:33:43 +02:00
|
|
|
|
|
|
|
.flash-green {
|
|
|
|
margin-left: 0.5rem;
|
|
|
|
}
|
2023-01-19 08:57:25 +01:00
|
|
|
</style>
|