From 43863f1dd739d54c7e25a917bc673c4824a99256 Mon Sep 17 00:00:00 2001 From: jacob-8 Date: Wed, 20 Mar 2024 07:48:20 +0800 Subject: [PATCH] fix: string and colorize JSON when JSON component clicked, not mounted --- src/lib/data/JSON.svelte | 65 ++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 40 deletions(-) diff --git a/src/lib/data/JSON.svelte b/src/lib/data/JSON.svelte index d691461..1e64994 100644 --- a/src/lib/data/JSON.svelte +++ b/src/lib/data/JSON.svelte @@ -2,28 +2,30 @@ import Button from '../ui/Button.svelte'; import ShowHide from '../functions/ShowHide.svelte'; export let obj: Record; - $: json = JSON.stringify(obj, null, 2) - .replace(/&/g, '&') - .replace(//g, '>') - .replace( - /("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+-]?\d+)?)/g, - function (match) { - var color = 'darkorange'; // number' - if (/^"/.test(match)) { - if (/:$/.test(match)) { - color = 'red'; // key - } else { - color = 'green'; // string + + function string_and_colorize(obj: Record): string { + return JSON.stringify(obj, null, 2) + .replace(/&/g, '&') + .replace(//g, '>') + .replace( + /("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+-]?\d+)?)/g, + (match) => { + var color = 'darkorange'; // number' + if (/^"/.test(match)) { + if (/:$/.test(match)) + color = 'red'; // key + else + color = 'green'; // string + } else if (/true|false/.test(match)) { + color = 'blue'; // boolean + } else if (/null/.test(match)) { + color = 'magenta'; // null } - } else if (/true|false/.test(match)) { - color = 'blue'; // boolean - } else if (/null/.test(match)) { - color = 'magenta'; // null + return `${match}`; } - return '' + match + ''; - } - ); + ); + } @@ -31,26 +33,9 @@ {#if show} -
- -
-      {@html json}
-    
+
+ +
{@html string_and_colorize(obj)}
{/if} - -