Skip to content

Commit

Permalink
feat: add json button download
Browse files Browse the repository at this point in the history
  • Loading branch information
clemlatz committed Oct 15, 2024
1 parent 65dba60 commit b0cecf0
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ <h1>Modulix Editor</h1>
<span class="fa fa-code"></span>
Afficher le JSON
</button>
<button class="btn btn-outline-warning" id="download-json-button">
<span class="fa fa-download"></span>
Télécharger le JSON
</button>
<button class="btn btn-outline-danger" id="reset-button">
<span class="fa fa-trash"></span>
Réinitialiser
Expand Down Expand Up @@ -124,6 +128,21 @@ <h2>JSON à copier/coller</h2>
jsonOutputPane.style.display = jsonOutputPaneDisplayed ? 'block' : 'none';
});

const downloadButton = document.querySelector('#download-json-button');
downloadButton.addEventListener('click', () => {

const downloadLink = document.createElement('a');
const json = editor.getValue();
const jsonContent = JSON.stringify(json, null, 2);
downloadLink.setAttribute('href', 'data:application/json;charset=utf-8,' + encodeURIComponent(jsonContent));
downloadLink.setAttribute('download', `${json.slug}.json`);

downloadLink.style.display = 'none';
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
});

const resetButton = document.querySelector('#reset-button');
resetButton.addEventListener('click', () => {
if (window.confirm('Voulez-vous vraiment réinitialiser le module ? Toutes les modifications seront perdues.')) {
Expand Down

0 comments on commit b0cecf0

Please sign in to comment.