Refactor loadPreview promises
This commit is contained in:
parent
70c0ab3807
commit
f4a0ac6d6c
1 changed files with 10 additions and 9 deletions
|
@ -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();
|
||||||
|
})
|
||||||
|
.then(value => {
|
||||||
preview.document.open("text/html", "replace");
|
preview.document.open("text/html", "replace");
|
||||||
preview.document.write(value);
|
preview.document.write(value);
|
||||||
preview.document.close();
|
preview.document.close();
|
||||||
});
|
|
||||||
})
|
})
|
||||||
|
.catch(console.error);
|
||||||
}
|
}
|
||||||
|
|
||||||
function prepareBody() {
|
function prepareBody() {
|
||||||
|
|
Reference in a new issue