1
0
mirror of https://github.com/garraflavatra/rolens.git synced 2025-04-19 08:51:03 +00:00
rolens/frontend/src/components/codeviewer.svelte

68 lines
1.5 KiB
Svelte
Raw Normal View History

2023-01-15 12:02:17 +01:00
<script>
import hljs from 'highlight.js/lib/core';
2023-01-15 12:09:27 +01:00
import hljsJSON from 'highlight.js/lib/languages/json';
import hljsJavaScript from 'highlight.js/lib/languages/javascript';
2023-01-15 12:02:17 +01:00
import Icon from './icon.svelte';
import Modal from './modal.svelte';
2023-01-15 12:09:27 +01:00
import 'highlight.js/styles/atom-one-dark.css';
export let code = '';
export let language = 'json';
const languageNames = {
json: 'JSON',
js: 'JavaScript',
};
2023-01-15 12:02:17 +01:00
2023-01-15 12:09:27 +01:00
hljs.registerLanguage('json', hljsJSON);
hljs.registerLanguage('js', hljsJavaScript);
2023-01-15 12:02:17 +01:00
2023-01-15 12:09:27 +01:00
$: highlighted = code ? hljs.highlight(language, code).value : '';
2023-01-15 12:02:17 +01:00
function copy() {
2023-01-15 12:09:27 +01:00
navigator.clipboard.writeText(code);
2023-01-15 12:02:17 +01:00
}
</script>
2023-01-15 12:09:27 +01:00
{#if code}
<Modal bind:show={code} title="{languageNames[language]} viewer" contentPadding={false}>
2023-01-15 12:02:17 +01:00
<div class="codeblock">
<div class="buttons">
<button class="btn" on:click={copy}>
<Icon name="clipboard" />
</button>
</div>
<pre><code class="hljs">{@html highlighted}</code></pre>
</div>
</Modal>
{/if}
<style>
.codeblock {
position: relative;
}
.buttons {
position: absolute;
top: 0;
right: 0;
margin: 1rem;
}
.buttons button {
margin-left: 1rem;
background: none;
border: 1px solid rgba(255, 255, 255, 0.3);
}
.buttons button:hover {
background-color: rgba(0, 0, 0, 0.3);
}
.hljs {
user-select: text;
line-height: 1.2;
}
pre :global(::selection) {
background: rgba(5, 5, 5, 0.8);
}
</style>