From 410ca9ce66d0ad55fc543da079b553c85fd064e8 Mon Sep 17 00:00:00 2001 From: "Laetitia (O-01-67)" <68935009+O-01-67@users.noreply.github.com> Date: Thu, 8 Dec 2022 17:56:33 -0500 Subject: [PATCH] add option to copy as text (trollge) --- mods/save_loading.js | 60 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/mods/save_loading.js b/mods/save_loading.js index b31c28c2..fc3892c3 100644 --- a/mods/save_loading.js +++ b/mods/save_loading.js @@ -14,6 +14,20 @@ function getSimulationState() { return simulationState; }; +//https://stackoverflow.com/a/46118025 +function copyToClipboard(text) { + var dummy = document.createElement("textarea"); + // to avoid breaking orgain page when copying more words + // cant copy when adding below this code + // dummy.style.display = 'none' + document.body.appendChild(dummy); + //Be careful if you use textarea. setAttribute('value', value), which works with "input" does not work with "textarea". – Eduard + dummy.value = text; + dummy.select(); + document.execCommand("copy"); + document.body.removeChild(dummy); +} + const saveTemplateAsFile = (filename, dataObjToWrite) => { //from https://stackoverflow.com/a/65939108 const blob = new Blob([JSON.stringify(dataObjToWrite)], { type: "text/json" }); const link = document.createElement("a"); @@ -74,6 +88,11 @@ function downloadSave(filename=null) { saveTemplateAsFile(filename, getSimulationState()); }; +function copySaveJSON(doAlert=true) { + copyToClipboard(JSON.stringify(getSimulationState())); + if(doAlert) { alert("Save copied as JSON") }; +}; + function loadFile() { //Initialize var json; @@ -114,6 +133,40 @@ function loadFile() { }; }; +function loadText() { + //Initialize + var json; + + //load JSON + var json = document.getElementById('mytext').value; + if(json === "") { + if(document.getElementById("textFormStatus") !== "null") { + document.getElementById("textFormStatus").style.color = "red"; + document.getElementById("textFormStatus").innerHTML = "No text was present!"; + }; + throw new Error("No text was present"); + }; + + //validate + try { + json = JSON.parse(json); + } catch (error) { + if(document.getElementById("textFormStatus") !== "null") { + document.getElementById("textFormStatus").style.color = "red"; + document.getElementById("textFormStatus").innerHTML = "The text wasn't valid JSON!"; + }; + throw error; + }; + + if(document.getElementById("textFormStatus") !== "null") { + document.getElementById("textFormStatus").style.color = "yellow"; + document.getElementById("textFormStatus").innerHTML = "JSON was parsed successfully"; + }; + + //return json; + return importJsonState(json); +}; + function importJsonState(json) { //check keys var jsonKeys = Object.keys(json); @@ -214,10 +267,15 @@ function setPixelSize(size=null) { var saveLoaderDescription = `
Download simulation +Alternatively, copy simulation JSON No file loader status -One file, please: +One file, please: +Or paste JSON +No text loader status + + No size setter status Pixel size (rendering only): (Use if the save looks cut off)