|
@@ -5,14 +5,16 @@
|
|
|
|
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
|
|
|
|
- import { getContext, getAllContexts, onMount } from 'svelte';
|
|
|
|
|
|
+ import { getContext, getAllContexts, onMount, tick, createEventDispatcher } from 'svelte';
|
|
import { copyToClipboard } from '$lib/utils';
|
|
import { copyToClipboard } from '$lib/utils';
|
|
|
|
|
|
import 'highlight.js/styles/github-dark.min.css';
|
|
import 'highlight.js/styles/github-dark.min.css';
|
|
|
|
|
|
import PyodideWorker from '$lib/workers/pyodide.worker?worker';
|
|
import PyodideWorker from '$lib/workers/pyodide.worker?worker';
|
|
|
|
+ import CodeEditor from '$lib/components/common/CodeEditor.svelte';
|
|
|
|
|
|
const i18n = getContext('i18n');
|
|
const i18n = getContext('i18n');
|
|
|
|
+ const dispatch = createEventDispatcher();
|
|
|
|
|
|
export let id = '';
|
|
export let id = '';
|
|
|
|
|
|
@@ -20,6 +22,15 @@
|
|
export let lang = '';
|
|
export let lang = '';
|
|
export let code = '';
|
|
export let code = '';
|
|
|
|
|
|
|
|
+ let _code = '';
|
|
|
|
+ $: if (code) {
|
|
|
|
+ updateCode();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ const updateCode = () => {
|
|
|
|
+ _code = code;
|
|
|
|
+ };
|
|
|
|
+
|
|
let _token = null;
|
|
let _token = null;
|
|
|
|
|
|
let mermaidHtml = null;
|
|
let mermaidHtml = null;
|
|
@@ -32,6 +43,18 @@
|
|
let result = null;
|
|
let result = null;
|
|
|
|
|
|
let copied = false;
|
|
let copied = false;
|
|
|
|
+ let saved = false;
|
|
|
|
+
|
|
|
|
+ const saveCode = () => {
|
|
|
|
+ saved = true;
|
|
|
|
+
|
|
|
|
+ code = _code;
|
|
|
|
+ dispatch('save', code);
|
|
|
|
+
|
|
|
|
+ setTimeout(() => {
|
|
|
|
+ saved = false;
|
|
|
|
+ }, 1000);
|
|
|
|
+ };
|
|
|
|
|
|
const copyCode = async () => {
|
|
const copyCode = async () => {
|
|
copied = true;
|
|
copied = true;
|
|
@@ -233,22 +256,11 @@ __builtins__.input = input`);
|
|
(async () => {
|
|
(async () => {
|
|
await drawMermaidDiagram();
|
|
await drawMermaidDiagram();
|
|
})();
|
|
})();
|
|
- } else {
|
|
|
|
- // Function to perform the code highlighting
|
|
|
|
- const highlightCode = () => {
|
|
|
|
- highlightedCode = hljs.highlightAuto(code, hljs.getLanguage(lang)?.aliases).value || code;
|
|
|
|
- };
|
|
|
|
-
|
|
|
|
- // Clear the previous timeout if it exists
|
|
|
|
- clearTimeout(debounceTimeout);
|
|
|
|
- // Set a new timeout to debounce the code highlighting
|
|
|
|
- debounceTimeout = setTimeout(highlightCode, 10);
|
|
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
|
|
$: if (token) {
|
|
$: if (token) {
|
|
if (JSON.stringify(token) !== JSON.stringify(_token)) {
|
|
if (JSON.stringify(token) !== JSON.stringify(_token)) {
|
|
- console.log('hi');
|
|
|
|
_token = token;
|
|
_token = token;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -295,28 +307,50 @@ __builtins__.input = input`);
|
|
{:else}
|
|
{:else}
|
|
<button
|
|
<button
|
|
class="copy-code-button bg-none border-none p-1"
|
|
class="copy-code-button bg-none border-none p-1"
|
|
- on:click={() => {
|
|
|
|
|
|
+ on:click={async () => {
|
|
|
|
+ code = _code;
|
|
|
|
+ await tick();
|
|
executePython(code);
|
|
executePython(code);
|
|
}}>{$i18n.t('Run')}</button
|
|
}}>{$i18n.t('Run')}</button
|
|
>
|
|
>
|
|
{/if}
|
|
{/if}
|
|
{/if}
|
|
{/if}
|
|
|
|
+
|
|
|
|
+ <button class="copy-code-button bg-none border-none p-1" on:click={saveCode}>
|
|
|
|
+ {saved ? $i18n.t('Saved') : $i18n.t('Save')}
|
|
|
|
+ </button>
|
|
|
|
+
|
|
<button class="copy-code-button bg-none border-none p-1" on:click={copyCode}
|
|
<button class="copy-code-button bg-none border-none p-1" on:click={copyCode}
|
|
>{copied ? $i18n.t('Copied') : $i18n.t('Copy')}</button
|
|
>{copied ? $i18n.t('Copied') : $i18n.t('Copy')}</button
|
|
>
|
|
>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
- <pre
|
|
|
|
|
|
+ <div
|
|
|
|
+ class="language-{lang} rounded-t-none {executing || stdout || stderr || result
|
|
|
|
+ ? ''
|
|
|
|
+ : 'rounded-b-lg'} overflow-hidden"
|
|
|
|
+ >
|
|
|
|
+ <CodeEditor
|
|
|
|
+ value={code}
|
|
|
|
+ {id}
|
|
|
|
+ {lang}
|
|
|
|
+ on:save={() => {
|
|
|
|
+ saveCode();
|
|
|
|
+ }}
|
|
|
|
+ on:change={(e) => {
|
|
|
|
+ _code = e.detail.value;
|
|
|
|
+ }}
|
|
|
|
+ />
|
|
|
|
+ </div>
|
|
|
|
+
|
|
|
|
+ <!-- <pre
|
|
class=" hljs p-4 px-5 overflow-x-auto"
|
|
class=" hljs p-4 px-5 overflow-x-auto"
|
|
style="border-top-left-radius: 0px; border-top-right-radius: 0px; {(executing ||
|
|
style="border-top-left-radius: 0px; border-top-right-radius: 0px; {(executing ||
|
|
stdout ||
|
|
stdout ||
|
|
stderr ||
|
|
stderr ||
|
|
result) &&
|
|
result) &&
|
|
- 'border-bottom-left-radius: 0px; border-bottom-right-radius: 0px;'}"><code
|
|
|
|
- class="language-{lang} rounded-t-none whitespace-pre"
|
|
|
|
- >{#if highlightedCode}{@html highlightedCode}{:else}{code}{/if}</code
|
|
|
|
- ></pre>
|
|
|
|
|
|
+ 'border-bottom-left-radius: 0px; border-bottom-right-radius: 0px;'}"><code></code></pre> -->
|
|
|
|
|
|
<div
|
|
<div
|
|
id="plt-canvas-{id}"
|
|
id="plt-canvas-{id}"
|