Skip to content

Commit 8ad54fa

Browse files
committed
disable ctrl-s key press and image tag for images instead of iframes
1 parent 4d1ae60 commit 8ad54fa

1 file changed

Lines changed: 30 additions & 2 deletions

File tree

docs/pageLoader.html

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626

2727
<head>
2828
<title>Phoenix live preview</title>
29+
<style>
30+
.hidden {
31+
display: none;
32+
}
33+
</style>
2934
<script>
3035
const worker = new Worker('pageLoaderWorker.js');
3136
worker.onmessage = (event) => {
@@ -35,6 +40,13 @@
3540
default: console.error("Live Preview page loader: received unknown message from worker:", event);
3641
}
3742
}
43+
window.savePageCtrlSDisabledByPhoenix = true;
44+
addEventListener("keydown", function(e) {
45+
// inside live preview iframe, we disable ctrl-s browser save page dialog
46+
if (e.key === 's' && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
47+
e.preventDefault();
48+
}
49+
}, false);
3850
// https://developer.mozilla.org/en-US/docs/Web/Privacy/Storage_Access_Policy/Errors/CookiePartitionedForeign
3951
// iframes are sandboxed by default and if we popout the live preview, those previews wont be
4052
// reachable from this sandboxed iframe server. So we have to request access.
@@ -50,6 +62,16 @@
5062
</script>
5163
<script type="module">
5264
const clientID = "" + Math.round( Math.random()*1000000000);
65+
function getExtension(url) {
66+
url = url || '';
67+
let pathSplit = url.split('.');
68+
return pathSplit && pathSplit.length>1 ? pathSplit[pathSplit.length-1] : '';
69+
}
70+
function isImage(url) {
71+
let extension = getExtension(url);
72+
return ["jpg", "jpeg", "png", "gif", "svg", "webp", "bmp", "ico", "avif"]
73+
.includes(extension.toLowerCase());
74+
}
5375
window.handleLoad = function () {
5476
const urlSearchParams = new URLSearchParams(window.location.search);
5577
const params = Object.fromEntries(urlSearchParams.entries());
@@ -62,7 +84,12 @@
6284
broadcastChannel: params.broadcastChannel,
6385
clientID});
6486
if(params.URL){
65-
document.getElementById("contentFrame").src = decodeURIComponent(params.URL);
87+
let element = document.getElementById("contentFrame");
88+
if(isImage(decodeURIComponent(params.URL))){
89+
element = document.getElementById("contentImage");
90+
}
91+
element.classList.remove('hidden');
92+
element.src = decodeURIComponent(params.URL);
6693
const path = params.URL.replace(/\/$/, "").split("/");
6794
document.title = path[path.length -1];
6895
}
@@ -84,7 +111,8 @@
84111
<body onload="handleLoad()">
85112
<div id="content">
86113
<iframe id="contentFrame" allowfullscreen width="100%" height="100%" frameborder="0"
87-
src="" title="Live Preview"></iframe>
114+
src="about:blank" title="Live Preview" class="hidden"></iframe>
115+
<img id="contentImage" src="" alt="Image Live Preview" class="hidden"/>
88116
</div>
89117
</body>
90118

0 commit comments

Comments
 (0)