From 814adbd334f1271227cb13c3497dc2070818462e Mon Sep 17 00:00:00 2001 From: "Max W." Date: Fri, 11 Apr 2025 01:16:24 +0200 Subject: [PATCH] working state --- public/index.html | 2 +- public/index.js | 34 +++++++++++++++++++--------------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/public/index.html b/public/index.html index aa0be82..50d7608 100644 --- a/public/index.html +++ b/public/index.html @@ -74,7 +74,7 @@ diff --git a/public/index.js b/public/index.js index 90b1f5b..14e7ff7 100644 --- a/public/index.js +++ b/public/index.js @@ -20,7 +20,10 @@ $(document).ready(function() { html: ` Format JSON - ` + `, + css: { + display: $languageSelector.val() === 'json' ? 'inline-flex' : 'none' + } }); $('.controls').prepend($formatJsonBtn); @@ -164,6 +167,7 @@ $(document).ready(function() { const detectedLanguage = detectContentType($editor.val()); if (detectedLanguage === 'json') { $languageSelector.val('json'); + $formatJsonBtn.show(); } } @@ -245,10 +249,14 @@ $(document).ready(function() { window.history.pushState({}, '', '/'); // Reset URL to homepage $editorContainer.removeClass('viewer-active'); $languageSelector.val('javascript'); + $formatJsonBtn.hide(); // Hide JSON button when switching to new paste with JavaScript }); // Language change $languageSelector.on('change', () => { + // Toggle Format JSON button visibility based on language selection + $formatJsonBtn.toggle($languageSelector.val() === 'json'); + if ($editorContainer.hasClass('viewer-active')) { $viewer.attr('class', `language-${$languageSelector.val()}`); hljs.highlightElement($viewer[0]); @@ -264,20 +272,16 @@ $(document).ready(function() { // Format JSON handler $formatJsonBtn.on('click', () => { - if ($languageSelector.val() === 'json') { - try { - const parsed = JSON.parse($editor.val()); - const formatted = JSON.stringify(parsed, null, 2); - $editor.val(formatted); - $viewer.text(formatted); - hljs.highlightElement($viewer[0]); - updateLineNumbers(); - showNotification('JSON formatted successfully!', 'success'); - } catch (e) { - showNotification('Invalid JSON: ' + e.message, 'error'); - } - } else { - showNotification('Select JSON language first', 'error'); + try { + const parsed = JSON.parse($editor.val()); + const formatted = JSON.stringify(parsed, null, 2); + $editor.val(formatted); + $viewer.text(formatted); + hljs.highlightElement($viewer[0]); + updateLineNumbers(); + showNotification('JSON formatted successfully!', 'success'); + } catch (e) { + showNotification('Invalid JSON: ' + e.message, 'error'); } });