Refactor loadPreview promises

This commit is contained in:
Gabriel Augendre 2020-12-30 07:49:49 +01:00
parent 70c0ab3807
commit f4a0ac6d6c
No known key found for this signature in database
GPG key ID: 1E693F4CE4AEE7B4

View file

@ -1,3 +1,5 @@
let preview = null;
window.onload = function () { window.onload = function () {
const previewButton = document.querySelector("input#_live_preview"); const previewButton = document.querySelector("input#_live_preview");
previewButton.addEventListener("click", openPreviewPopup); previewButton.addEventListener("click", openPreviewPopup);
@ -8,8 +10,6 @@ window.onbeforeunload = function () {
} }
}; };
let preview = null;
function openPreviewPopup(event) { function openPreviewPopup(event) {
event.preventDefault(); event.preventDefault();
const params = "width=800,height=1000,menubar=no,toolbar=no,location=no,status=no,resizable=yes,scrollbars=yes"; const params = "width=800,height=1000,menubar=no,toolbar=no,location=no,status=no,resizable=yes,scrollbars=yes";
@ -17,7 +17,6 @@ function openPreviewPopup(event) {
preview.close(); preview.close();
} }
preview = window.open("about:blank", "Preview", params); preview = window.open("about:blank", "Preview", params);
setTimeout(loadPreview, 1000); setTimeout(loadPreview, 1000);
setupLivePreview(); setupLivePreview();
} }
@ -26,13 +25,15 @@ function loadPreview() {
const id = Number(window.location.pathname.match(/\d+/)[0]); const id = Number(window.location.pathname.match(/\d+/)[0]);
const body = prepareBody(); const body = prepareBody();
fetch(`/api/render/${id}/`, {method: "POST", body: body}) fetch(`/api/render/${id}/`, {method: "POST", body: body})
.then(function (response) { .then(response => {
response.text().then(value => { return response.text();
preview.document.open("text/html", "replace");
preview.document.write(value);
preview.document.close();
});
}) })
.then(value => {
preview.document.open("text/html", "replace");
preview.document.write(value);
preview.document.close();
})
.catch(console.error);
} }
function prepareBody() { function prepareBody() {