Skip to content

Commit 3359926

Browse files
authored
Refactor href handling and improve preview functions
1 parent 84b20e6 commit 3359926

1 file changed

Lines changed: 84 additions & 32 deletions

File tree

js/script.js

Lines changed: 84 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1979,17 +1979,9 @@ Options -Indexes
19791979
event.preventDefault();
19801980
let href = event.srcElement.attributes['href'];
19811981
if (!href) href = event.srcElement.parentElement.attributes['href'];
1982-
let hrefValue = href.value;
1983-
1984-
if (hrefValue === globalThis.lb.globalVariables()['website_url'] || hrefValue === "/") {
1985-
hrefValue = "/index.html";
1986-
} else {
1987-
hrefValue += globalThis.lb.dotHtmlForLinks() ? "" : ".html";
1988-
if (hrefValue.startsWith(globalThis.lb.globalVariables()['website_url'])) {
1989-
hrefValue = hrefValue.replace(globalThis.lb.globalVariables()['website_url'], "");
1990-
} else if (hrefValue.startsWith("http")) {
1991-
return;
1992-
}
1982+
let hrefValue = fixHrefValue(href.value);
1983+
if (hrefValue.startsWith("http")) {
1984+
return;
19931985
}
19941986

19951987
generateWebsite(false, (wf) => loadFileIntoIframe(wf, hrefValue, iframe));
@@ -2006,6 +1998,19 @@ Options -Indexes
20061998
});
20071999
}
20082000

2001+
const fixHrefValue = function(hrefValue) {
2002+
if (hrefValue === globalThis.lb.globalVariables()['website_url'] || hrefValue === "/") {
2003+
hrefValue = "/index.html";
2004+
} else {
2005+
hrefValue += globalThis.lb.dotHtmlForLinks() ? "" : ".html";
2006+
if (hrefValue.startsWith(globalThis.lb.globalVariables()['website_url'])) {
2007+
hrefValue = hrefValue.replace(globalThis.lb.globalVariables()['website_url'], "");
2008+
}
2009+
}
2010+
2011+
return hrefValue;
2012+
}
2013+
20092014
const removeLogo = function() {
20102015
const horizontalBarLogo = globalThis.document.getElementById("horizontal-bar-logo");
20112016
horizontalBarLogo.innerHTML = "<img id='close-preview-icon' class='icon' src='images/close.svg' />";
@@ -2042,6 +2047,17 @@ Options -Indexes
20422047
}
20432048

20442049
const loadFileIntoIframe = function(wFolder, path, iframe) {
2050+
const [xmlDoc, styles] = preparePageForPreview(wFolder, path);
2051+
2052+
const doc = iframe.contentDocument;
2053+
doc.documentElement.innerHTML = xmlDoc.getElementsByTagName("html")[0].innerHTML;
2054+
doc.documentElement.getElementsByTagName("head")[0].innerHTML += styles;
2055+
enableSearch(doc);
2056+
enableShareButton(doc);
2057+
formatDates(doc);
2058+
}
2059+
2060+
const preparePageForPreview = function(wFolder, path) {
20452061
if (path.startsWith("/")) path = path.substring(1);
20462062

20472063
let file = wFolder.getFile(path);
@@ -2073,11 +2089,7 @@ Options -Indexes
20732089
}
20742090
}
20752091

2076-
iframe.contentDocument.documentElement.innerHTML = xmlDoc.getElementsByTagName("html")[0].innerHTML;
2077-
iframe.contentDocument.documentElement.getElementsByTagName("head")[0].innerHTML += styles;
2078-
enableIframeSearch();
2079-
enableShareButton();
2080-
formatIframeDates();
2092+
return [xmlDoc, styles];
20812093
}
20822094

20832095
const createElementStr = function(data, tag) {
@@ -2088,11 +2100,10 @@ Options -Indexes
20882100
return "";
20892101
}
20902102

2091-
const formatIframeDates = function() {
2092-
const iframe = globalThis.document.getElementById("preview-iframe");
2093-
if (!iframe) return;
2103+
const formatDates = function(doc) {
2104+
if (!doc) return;
20942105

2095-
const itemDates = iframe.contentDocument.getElementsByClassName("item-date");
2106+
const itemDates = doc.getElementsByClassName("item-date");
20962107
if (!itemDates || itemDates.length === 0) return;
20972108

20982109
const formatDate = function(dateStr, options) {
@@ -2116,14 +2127,14 @@ Options -Indexes
21162127
}
21172128
}
21182129

2119-
const enableIframeSearch = function() {
2130+
const enableSearch = function(doc) {
2131+
if (!doc) return;
21202132
const libreblogSearch = globalThis.lb.getLibreblogSearch();
2121-
const iframe = globalThis.document.getElementById("preview-iframe");
2122-
if (!iframe) return;
2123-
const searchInput = iframe.contentDocument.getElementById("search-input");
2133+
const searchInput = doc.getElementById("search-input");
21242134
if (!searchInput) return;
2135+
21252136
searchInput.addEventListener("input", (event) => {
2126-
const wrapper = iframe.contentDocument.getElementById("search-box-wrapper");
2137+
const wrapper = doc.getElementById("search-box-wrapper");
21272138
if (!wrapper) return;
21282139
let value = event.target.value;
21292140

@@ -2163,10 +2174,9 @@ Options -Indexes
21632174
});
21642175
}
21652176

2166-
const enableShareButton = function() {
2167-
const iframe = globalThis.document.getElementById("preview-iframe");
2168-
if (!iframe) return;
2169-
const shareButton = iframe.contentDocument.getElementById("share-button");
2177+
const enableShareButton = function(doc) {
2178+
if (!doc) return;
2179+
const shareButton = doc.getElementById("share-button");
21702180
if (!shareButton) return;
21712181

21722182
const url = shareButton.attributes["data-url"].value;
@@ -2352,6 +2362,7 @@ Options -Indexes
23522362

23532363
const initSettingsFormHandlers = function() {
23542364
processSettingsFormField("twig-inside-articles", "change", null);
2365+
processSettingsFormField("preview-in-new-tab", "change", null);
23552366
processSettingsFormField("rss-for-mainpage", "change", null);
23562367
processSettingsFormField("rss-for-sections", "change", null);
23572368
processSettingsFormField("remove-dot-html", "change", null);
@@ -3115,11 +3126,47 @@ Options -Indexes
31153126
modal.style.display = "flex";
31163127
}
31173128

3118-
const openPreviewModal = function(page) {
3129+
const openPreviewModal = function(pagePath) {
31193130
const modal = globalThis.document.getElementById("preview-modal");
31203131
modal.style.display = "flex";
31213132

3122-
previewWebsite('preview-container', page, null);
3133+
previewWebsite('preview-container', pagePath, null);
3134+
}
3135+
3136+
const openPreviewWindow = function(pagePath) {
3137+
const newTab = window.open();
3138+
const doc = newTab.document;
3139+
3140+
const loadPage = (d, pP) => generateWebsite(false, (wF) => {
3141+
const [xmlDoc, styles] = preparePageForPreview(wF, pP);
3142+
d.documentElement.innerHTML = xmlDoc.getElementsByTagName("html")[0].innerHTML;
3143+
d.documentElement.getElementsByTagName("head")[0].innerHTML += styles;
3144+
enableSearch(d);
3145+
enableShareButton(d);
3146+
formatDates(d);
3147+
});
3148+
3149+
loadPage(doc, pagePath);
3150+
3151+
doc.addEventListener("click", (event) => {
3152+
if (!event.target) return;
3153+
3154+
if (clickCameFromA(event.target)) {
3155+
event.preventDefault();
3156+
let href = event.target.attributes['href'];
3157+
if (!href) href = event.target.parentElement.attributes['href'];
3158+
let hrefValue = fixHrefValue(href.value);
3159+
if (hrefValue.startsWith("http")) {
3160+
return;
3161+
}
3162+
3163+
loadPage(doc, hrefValue);
3164+
}
3165+
});
3166+
3167+
doc.addEventListener("submit", (event) => {
3168+
event.preventDefault();
3169+
});
31233170
}
31243171

31253172
const openMediaModal = function(type, callback) { //the type is always "image" for now
@@ -3784,7 +3831,11 @@ Options -Indexes
37843831
buttons.push(["button", {
37853832
ref: "previewButton",
37863833
onclick: () => {
3787-
openPreviewModal(pagePath);
3834+
if (globalThis.lb.getSetting("preview-in-new-tab") === 'true') {
3835+
openPreviewWindow(pagePath);
3836+
} else {
3837+
openPreviewModal(pagePath);
3838+
}
37883839
}
37893840
}, ""]);
37903841
hasPrev = true;
@@ -3948,6 +3999,7 @@ Options -Indexes
39483999
await globalThis.lb.setSetting("main-js", default_main_js);
39494000
await globalThis.lb.setSetting("twig-inside-articles", "true");
39504001
await globalThis.lb.setSetting("rss-for-mainpage", "true");
4002+
await globalThis.lb.setSetting("preview-in-new-tab", "true");
39514003
await globalThis.lb.setSetting("share-button", "true");
39524004
await globalThis.lb.setSetting("articles-in-author-profile", "true");
39534005
await globalThis.lb.setSetting("article-in-feedback", "false");

0 commit comments

Comments
 (0)