From 5911bf09f9c9247ead9986c67dd6e1f184fec230 Mon Sep 17 00:00:00 2001 From: Adam Bem Date: Wed, 26 Apr 2023 12:01:50 +0200 Subject: [PATCH] Removed duplicate code --- Frontend/assets/scripts/tools/scripts.js | 38 ++++++++++++ Frontend/tools/jsonFormatter.html | 41 ++----------- Frontend/tools/xmlFormatter.html | 38 +----------- Frontend/tools/xpath.html | 39 ++---------- Frontend/tools/xsd.html | 78 ++---------------------- Frontend/tools/xslt.html | 75 +++-------------------- 6 files changed, 62 insertions(+), 247 deletions(-) diff --git a/Frontend/assets/scripts/tools/scripts.js b/Frontend/assets/scripts/tools/scripts.js index 2b69577..124baa2 100644 --- a/Frontend/assets/scripts/tools/scripts.js +++ b/Frontend/assets/scripts/tools/scripts.js @@ -426,3 +426,41 @@ async function restRequest(port, endpoint, xmlData, transformData) { }); return result; } + + +function configurePastingInElement(elementId) { + const editorEle = document.getElementById(elementId); + + // Handle the `paste` event + editorEle.addEventListener('paste', function (e) { + // Prevent the default action + e.preventDefault(); + + // Get the copied text from the clipboard + const text = e.clipboardData + ? (e.originalEvent || e).clipboardData.getData('text/plain') + : // For IE + window.clipboardData + ? window.clipboardData.getData('Text') + : ''; + + if (document.queryCommandSupported('insertText')) { + document.execCommand('insertText', false, text); + } else { + // Insert text at the current position of caret + const range = document.getSelection().getRangeAt(0); + range.deleteContents(); + + const textNode = document.createTextNode(text); + range.insertNode(textNode); + range.selectNodeContents(textNode); + range.collapse(false); + + const selection = window.getSelection(); + selection.removeAllRanges(); + selection.addRange(range); + } + highlightSyntax(editorEle.id); + + }); +} \ No newline at end of file diff --git a/Frontend/tools/jsonFormatter.html b/Frontend/tools/jsonFormatter.html index a24b29a..e316c13 100644 --- a/Frontend/tools/jsonFormatter.html +++ b/Frontend/tools/jsonFormatter.html @@ -13,7 +13,7 @@ - +
@@ -224,40 +224,11 @@ hljs.addPlugin(mergeHTMLPlugin); - const editorEle = document.getElementById('jsonBlock'); - - // Handle the `paste` event - editorEle.addEventListener('paste', function (e) { - // Prevent the default action - e.preventDefault(); - - // Get the copied text from the clipboard - const text = e.clipboardData - ? (e.originalEvent || e).clipboardData.getData('text/plain') - : // For IE - window.clipboardData - ? window.clipboardData.getData('Text') - : ''; - - if (document.queryCommandSupported('insertText')) { - document.execCommand('insertText', false, text); - - } else { - // Insert text at the current position of caret - const range = document.getSelection().getRangeAt(0); - range.deleteContents(); - - const textNode = document.createTextNode(text); - range.insertNode(textNode); - range.selectNodeContents(textNode); - range.collapse(false); - - const selection = window.getSelection(); - selection.removeAllRanges(); - selection.addRange(range); - } - highlightSyntax(editorEle.id); - }); + function init() { + // Make sure that only plain text is pasted + configurePastingInElement("jsonBlock"); + + } diff --git a/Frontend/tools/xmlFormatter.html b/Frontend/tools/xmlFormatter.html index 9a2331a..c9419ea 100644 --- a/Frontend/tools/xmlFormatter.html +++ b/Frontend/tools/xmlFormatter.html @@ -72,44 +72,8 @@ } function init() { - setDefaultContent(document.getElementById("xmlArea"), 'Insert XML here'); + configurePastingInElement("xmlArea"); } - - const editorEle = document.getElementById('xmlArea'); - - // Handle the `paste` event - editorEle.addEventListener('paste', function (e) { - // Prevent the default action - e.preventDefault(); - - // Get the copied text from the clipboard - const text = e.clipboardData - ? (e.originalEvent || e).clipboardData.getData('text/plain') - : // For IE - window.clipboardData - ? window.clipboardData.getData('Text') - : ''; - - if (document.queryCommandSupported('insertText')) { - document.execCommand('insertText', false, text); - } else { - // Insert text at the current position of caret - const range = document.getSelection().getRangeAt(0); - range.deleteContents(); - - const textNode = document.createTextNode(text); - range.insertNode(textNode); - range.selectNodeContents(textNode); - range.collapse(false); - - const selection = window.getSelection(); - selection.removeAllRanges(); - selection.addRange(range); - - } - highlightSyntax(editorEle.id); - - }); diff --git a/Frontend/tools/xpath.html b/Frontend/tools/xpath.html index 742d4f8..f228041 100644 --- a/Frontend/tools/xpath.html +++ b/Frontend/tools/xpath.html @@ -17219,6 +17219,10 @@ } function init() { + // Make sure that only plain text is pasted + configurePastingInElement("xmlArea"); + configurePastingInElement("transformArea"); + //Handle clicks in whole form and set info in tooltip setDefaultContent(document.getElementById("xmlArea"), 'Insert XML here'); setDefaultContent(document.getElementById("transformArea"), 'Insert XPath expression here'); @@ -17257,40 +17261,7 @@ }) } - const editorEle = document.getElementById('xmlArea'); - - // Handle the `paste` event - editorEle.addEventListener('paste', function (e) { - // Prevent the default action - e.preventDefault(); - - // Get the copied text from the clipboard - const text = e.clipboardData - ? (e.originalEvent || e).clipboardData.getData('text/plain') - : // For IE - window.clipboardData - ? window.clipboardData.getData('Text') - : ''; - - if (document.queryCommandSupported('insertText')) { - document.execCommand('insertText', false, text); - } else { - // Insert text at the current position of caret - const range = document.getSelection().getRangeAt(0); - range.deleteContents(); - - const textNode = document.createTextNode(text); - range.insertNode(textNode); - range.selectNodeContents(textNode); - range.collapse(false); - - const selection = window.getSelection(); - selection.removeAllRanges(); - selection.addRange(range); - } - highlightSyntax(editorEle.id); - - }); + diff --git a/Frontend/tools/xsd.html b/Frontend/tools/xsd.html index ffe5274..9fe0608 100644 --- a/Frontend/tools/xsd.html +++ b/Frontend/tools/xsd.html @@ -81,6 +81,10 @@ diff --git a/Frontend/tools/xslt.html b/Frontend/tools/xslt.html index 2407332..5bc9ca7 100644 --- a/Frontend/tools/xslt.html +++ b/Frontend/tools/xslt.html @@ -1199,6 +1199,10 @@ } function init() { + // Make sure that only plain text is pasted + configurePastingInElement("xmlArea"); + configurePastingInElement("transformArea"); + //Handle clicks in whole form and set info in tooltip setDefaultContent(document.getElementById("xmlArea"), 'Insert XML here'); setDefaultContent(document.getElementById("transformArea"), 'Insert XSLT here'); @@ -1223,76 +1227,11 @@ } processTooltip(); + }) + } - - const editorEle = document.getElementById('xmlArea'); - - // Handle the `paste` event - editorEle.addEventListener('paste', function (e) { - // Prevent the default action - e.preventDefault(); - - // Get the copied text from the clipboard - const text = e.clipboardData - ? (e.originalEvent || e).clipboardData.getData('text/plain') - : // For IE - window.clipboardData - ? window.clipboardData.getData('Text') - : ''; - - if (document.queryCommandSupported('insertText')) { - document.execCommand('insertText', false, text); - } else { - // Insert text at the current position of caret - const range = document.getSelection().getRangeAt(0); - range.deleteContents(); - - const textNode = document.createTextNode(text); - range.insertNode(textNode); - range.selectNodeContents(textNode); - range.collapse(false); - - const selection = window.getSelection(); - selection.removeAllRanges(); - selection.addRange(range); - } - highlightSyntax(editorEle.id); - }); - - const transformEle = document.getElementById('transformArea'); - - // Handle the `paste` event - transformEle.addEventListener('paste', function (e) { - // Prevent the default action - e.preventDefault(); - - // Get the copied text from the clipboard - const text = e.clipboardData - ? (e.originalEvent || e).clipboardData.getData('text/plain') - : // For IE - window.clipboardData - ? window.clipboardData.getData('Text') - : ''; - - if (document.queryCommandSupported('insertText')) { - document.execCommand('insertText', false, text); - } else { - // Insert text at the current position of caret - const range = document.getSelection().getRangeAt(0); - range.deleteContents(); - - const textNode = document.createTextNode(text); - range.insertNode(textNode); - range.selectNodeContents(textNode); - range.collapse(false); - - const selection = window.getSelection(); - selection.removeAllRanges(); - selection.addRange(range); - } - highlightSyntax(transformEle.id); - }); +