default styleSaverName ti take current name

This commit is contained in:
Azgaar 2021-07-11 13:18:02 +03:00
parent 25c97df2d0
commit 68f1861603

View file

@ -965,6 +965,8 @@ function addStylePreset() {
position: {my: "center", at: "center", of: "svg"} position: {my: "center", at: "center", of: "svg"}
}); });
const currentStyle = document.getElementById("stylePreset").selectedOptions[0].text;
document.getElementById("styleSaverName").value = currentStyle;
styleSaverJSON.value = JSON.stringify(getStyle(), null, 2); styleSaverJSON.value = JSON.stringify(getStyle(), null, 2);
checkName(); checkName();
@ -1075,22 +1077,11 @@ function addStylePreset() {
} }
function saveStyle() { function saveStyle() {
if (!styleSaverJSON.value) { if (!styleSaverJSON.value) return tip("Please provide a style JSON", false, "error");
tip("Please provide a style JSON", false, "error"); if (!JSON.isValid(styleSaverJSON.value)) return tip("JSON string is not valid, please check the format", false, "error");
return; if (!styleSaverName.value) return tip("Please provide a preset name", false, "error");
} if (styleSaverTip.innerHTML === "default") return tip("You cannot overwrite default preset, please change the name", false, "error");
if (!JSON.isValid(styleSaverJSON.value)) {
tip("JSON string is not valid, please check the format", false, "error");
return;
}
if (!styleSaverName.value) {
tip("Please provide a preset name", false, "error");
return;
}
if (styleSaverTip.innerHTML === "default") {
tip("You cannot overwrite default preset, please change the name", false, "error");
return;
}
const preset = "style" + styleSaverName.value; const preset = "style" + styleSaverName.value;
applyOption(stylePreset, preset, styleSaverName.value); // add option applyOption(stylePreset, preset, styleSaverName.value); // add option
localStorage.setItem("presetStyle", preset); // mark preset as default localStorage.setItem("presetStyle", preset); // mark preset as default
@ -1101,41 +1092,25 @@ function addStylePreset() {
} }
function styleDownload() { function styleDownload() {
if (!styleSaverJSON.value) { if (!styleSaverJSON.value) return tip("Please provide a style JSON", false, "error");
tip("Please provide a style JSON", false, "error"); if (!JSON.isValid(styleSaverJSON.value)) return tip("JSON string is not valid, please check the format", false, "error");
return; if (!styleSaverName.value) return tip("Please provide a preset name", false, "error");
}
if (!JSON.isValid(styleSaverJSON.value)) {
tip("JSON string is not valid, please check the format", false, "error");
return;
}
if (!styleSaverName.value) {
tip("Please provide a preset name", false, "error");
return;
}
const data = styleSaverJSON.value; const data = styleSaverJSON.value;
if (!data) { if (!data) return tip("Please provide a style JSON", false, "error");
tip("Please provide a style JSON", false, "error");
return;
}
downloadFile(data, "style" + styleSaverName.value + ".json", "application/json"); downloadFile(data, "style" + styleSaverName.value + ".json", "application/json");
} }
function styleUpload(dataLoaded) { function styleUpload(dataLoaded) {
if (!dataLoaded) { if (!dataLoaded) return tip("Cannot load the file. Please check the data format", false, "error");
tip("Cannot load the file. Please check the data format", false, "error");
return;
}
const data = JSON.stringify(JSON.parse(dataLoaded), null, 2); const data = JSON.stringify(JSON.parse(dataLoaded), null, 2);
styleSaverJSON.value = data; styleSaverJSON.value = data;
} }
} }
function removeStylePreset() { function removeStylePreset() {
if (stylePreset.selectedOptions[0].dataset.system) { if (stylePreset.selectedOptions[0].dataset.system) return tip("Cannot remove system preset", false, "error");
tip("Cannot remove system preset", false, "error");
return;
}
localStorage.removeItem("presetStyle"); localStorage.removeItem("presetStyle");
localStorage.removeItem(stylePreset.value); localStorage.removeItem(stylePreset.value);
stylePreset.selectedOptions[0].remove(); stylePreset.selectedOptions[0].remove();
@ -1148,10 +1123,8 @@ function applyMapFilter(event) {
if (event.target.tagName !== "BUTTON") return; if (event.target.tagName !== "BUTTON") return;
const button = event.target; const button = event.target;
svg.attr("data-filter", null).attr("filter", null); svg.attr("data-filter", null).attr("filter", null);
if (button.classList.contains("pressed")) { if (button.classList.contains("pressed")) return button.classList.remove("pressed");
button.classList.remove("pressed");
return;
}
mapFilters.querySelectorAll(".pressed").forEach(button => button.classList.remove("pressed")); mapFilters.querySelectorAll(".pressed").forEach(button => button.classList.remove("pressed"));
button.classList.add("pressed"); button.classList.add("pressed");
svg.attr("data-filter", button.id).attr("filter", "url(#filter-" + button.id + ")"); svg.attr("data-filter", button.id).attr("filter", "url(#filter-" + button.id + ")");
@ -1179,23 +1152,17 @@ function loadDefaultFonts() {
function fetchFonts(url) { function fetchFonts(url) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (url === "") { if (url === "") return tip("Use a direct link to any @font-face declaration or just font name to fetch from Google Fonts");
tip("Use a direct link to any @font-face declaration or just font name to fetch from Google Fonts");
return;
}
if (url.indexOf("http") === -1) { if (url.indexOf("http") === -1) {
url = url.replace(url.charAt(0), url.charAt(0).toUpperCase()).split(" ").join("+"); url = url.replace(url.charAt(0), url.charAt(0).toUpperCase()).split(" ").join("+");
url = "https://fonts.googleapis.com/css?family=" + url; url = "https://fonts.googleapis.com/css?family=" + url;
} }
const fetched = addFonts(url).then(fetched => {
if (fetched === undefined) { addFonts(url).then(fetched => {
tip("Cannot fetch font for this value!", false, "error"); if (fetched === undefined) return tip("Cannot fetch font for this value!", false, "error");
return; if (fetched === 0) return tip("Already in the fonts list!", false, "error");
}
if (fetched === 0) {
tip("Already in the fonts list!", false, "error");
return;
}
updateFontOptions(); updateFontOptions();
if (fetched === 1) { if (fetched === 1) {
tip("Font " + fonts[fonts.length - 1] + " is fetched"); tip("Font " + fonts[fonts.length - 1] + " is fetched");