From ac6224339634d36de1c04cff9f1572a9972fd843 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Rodr=C3=ADguez=20Royo?= Date: Wed, 7 Jun 2023 18:53:06 +0000 Subject: [PATCH 1/2] Added possibility to use encoded JSONs at designer's query params This will allow people to easily share its adaptive cards JSONs should be uri encoded --- .../themes/adaptivecards/layout/designer.ejs | 85 +++++++++++-------- 1 file changed, 49 insertions(+), 36 deletions(-) diff --git a/source/nodejs/adaptivecards-site/themes/adaptivecards/layout/designer.ejs b/source/nodejs/adaptivecards-site/themes/adaptivecards/layout/designer.ejs index be99a26bf4..ac052b7693 100644 --- a/source/nodejs/adaptivecards-site/themes/adaptivecards/layout/designer.ejs +++ b/source/nodejs/adaptivecards-site/themes/adaptivecards/layout/designer.ejs @@ -144,27 +144,34 @@ designer.attachTo(document.getElementById("designerRootHost")); designer.monacoModuleLoaded(); - var cardUrl = getParameterByName("card", null); - var dataUrl = getParameterByName("data", null); - - if (dataUrl) { - var dataXhttp = new XMLHttpRequest(); - dataXhttp.onload = function () { - if (dataXhttp.responseText && dataXhttp.responseText != "") { - var sampleData = JSON.parse(dataXhttp.responseText); - designer.sampleData = sampleData; - } - }; - - // TODO: when the designer has an addError API we should log that we failed to load the requested card - // xhttp.onerror = function() { - // designer. - // }; + var cardParam = getParameterByName("card", null); + var dataParam = getParameterByName("data", null); + if (dataParam) { try { - dataXhttp.open("GET", dataUrl, true); + var dataUrl = new URL(dataParam); + var dataXhttp = new XMLHttpRequest(); + dataXhttp.onload = function () { + if (dataXhttp.responseText && dataXhttp.responseText != "") { + var sampleData = JSON.parse(dataXhttp.responseText); + designer.sampleData = sampleData; + } + }; + + // TODO: when the designer has an addError API we should log that we failed to load the requested card + // xhttp.onerror = function() { + // designer. + // }; + + dataXhttp.open("GET", dataParam, true); dataXhttp.send(); } catch (e) { + // Check if the data is a valid JSON + try { + var sampleData = JSON.parse(decodeURIComponent(dataParam)); + designer.sampleData = sampleData; + } catch (e) { + } } } else { // Add sample data @@ -188,27 +195,33 @@ designer.sampleData = sampleData; } - if (cardUrl) { - var cardXhttp = new XMLHttpRequest(); - cardXhttp.onload = function () { - if (cardXhttp.responseText && cardXhttp.responseText != "") { - designer.setCard(JSON.parse(cardXhttp.responseText)); - } - - // TODO: Talk to David: I want to put the sampleData setter here but it was failing - - history.replaceState(null, null, "<%- config.root %>designer"); - }; - - // TODO: when the designer has an addError API we should log that we failed to load the requested card - // xhttp.onerror = function() { - // designer. - // }; - - try { - cardXhttp.open("GET", cardUrl, true); + if (cardParam) { + try{ + var cardUrl = new URL(cardParam); + var cardXhttp = new XMLHttpRequest(); + cardXhttp.onload = function () { + if (cardXhttp.responseText && cardXhttp.responseText != "") { + designer.setCard(JSON.parse(cardXhttp.responseText)); + } + + // TODO: Talk to David: I want to put the sampleData setter here but it was failing + + history.replaceState(null, null, "<%- config.root %>designer"); + }; + + // TODO: when the designer has an addError API we should log that we failed to load the requested card + // xhttp.onerror = function() { + // designer. + // }; + + cardXhttp.open("GET", cardParam, true); cardXhttp.send(); } catch (e) { + // Check if the card is a valid JSON + try { + designer.setCard(JSON.parse(decodeURIComponent(cardParam))); + } catch (e) { + } } } else { designer.setCard({ From 95ddfb45079d050d9bcd29b4658fc1f1ce0c975d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Rodr=C3=ADguez=20Royo?= Date: Sat, 10 Jun 2023 09:29:26 +0000 Subject: [PATCH 2/2] Removed URI decoders when parsing JSON --- .../themes/adaptivecards/layout/designer.ejs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/nodejs/adaptivecards-site/themes/adaptivecards/layout/designer.ejs b/source/nodejs/adaptivecards-site/themes/adaptivecards/layout/designer.ejs index ac052b7693..4ca9a85034 100644 --- a/source/nodejs/adaptivecards-site/themes/adaptivecards/layout/designer.ejs +++ b/source/nodejs/adaptivecards-site/themes/adaptivecards/layout/designer.ejs @@ -168,7 +168,7 @@ } catch (e) { // Check if the data is a valid JSON try { - var sampleData = JSON.parse(decodeURIComponent(dataParam)); + var sampleData = JSON.parse(dataParam); designer.sampleData = sampleData; } catch (e) { } @@ -219,7 +219,7 @@ } catch (e) { // Check if the card is a valid JSON try { - designer.setCard(JSON.parse(decodeURIComponent(cardParam))); + designer.setCard(JSON.parse(cardParam)); } catch (e) { } }