|
26 | 26 |
|
27 | 27 | <head> |
28 | 28 | <title>Phoenix live preview</title> |
| 29 | + <style> |
| 30 | + .hidden { |
| 31 | + display: none; |
| 32 | + } |
| 33 | + </style> |
29 | 34 | <script> |
30 | 35 | const worker = new Worker('pageLoaderWorker.js'); |
31 | 36 | worker.onmessage = (event) => { |
|
35 | 40 | default: console.error("Live Preview page loader: received unknown message from worker:", event); |
36 | 41 | } |
37 | 42 | } |
| 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); |
38 | 50 | // https://developer.mozilla.org/en-US/docs/Web/Privacy/Storage_Access_Policy/Errors/CookiePartitionedForeign |
39 | 51 | // iframes are sandboxed by default and if we popout the live preview, those previews wont be |
40 | 52 | // reachable from this sandboxed iframe server. So we have to request access. |
|
50 | 62 | </script> |
51 | 63 | <script type="module"> |
52 | 64 | 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 | + } |
53 | 75 | window.handleLoad = function () { |
54 | 76 | const urlSearchParams = new URLSearchParams(window.location.search); |
55 | 77 | const params = Object.fromEntries(urlSearchParams.entries()); |
|
62 | 84 | broadcastChannel: params.broadcastChannel, |
63 | 85 | clientID}); |
64 | 86 | 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); |
66 | 93 | const path = params.URL.replace(/\/$/, "").split("/"); |
67 | 94 | document.title = path[path.length -1]; |
68 | 95 | } |
|
84 | 111 | <body onload="handleLoad()"> |
85 | 112 | <div id="content"> |
86 | 113 | <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"/> |
88 | 116 | </div> |
89 | 117 | </body> |
90 | 118 |
|
|
0 commit comments