Skip to content

Commit 6eea5d6

Browse files
committed
fix: intel macs opens tabs twice on clicking markdown live preview a tags
1 parent ca9390c commit 6eea5d6

File tree

4 files changed

+62
-18
lines changed

4 files changed

+62
-18
lines changed

src/extensionsIntegrated/Phoenix-live-preview/BrowserStaticServer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,8 @@ define(function (require, exports, module) {
435435
markdownContent: markdownHtml,
436436
BOOTSTRAP_LIB_CSS: BootstrapCSSText,
437437
HIGHLIGHT_JS_CSS: GithubCSSText,
438+
TRUSTED_ORIGINS_EMBED:
439+
`const TRUSTED_ORIGINS_EMBED = ${JSON.stringify(Phoenix.TRUSTED_ORIGINS)};`,
438440
HIGHLIGHT_JS: HilightJSText,
439441
GFM_CSS: GFMCSSText,
440442
PARENT_ORIGIN: location.origin

src/extensionsIntegrated/Phoenix-live-preview/NodeStaticServer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@ define(function (require, exports, module) {
306306
BOOTSTRAP_LIB_CSS: BootstrapCSSText,
307307
HIGHLIGHT_JS_CSS: GithubCSSText,
308308
HIGHLIGHT_JS: HilightJSText,
309+
TRUSTED_ORIGINS_EMBED:
310+
`const TRUSTED_ORIGINS_EMBED = ${JSON.stringify(Phoenix.TRUSTED_ORIGINS)};`,
309311
GFM_CSS: GFMCSSText,
310312
PARENT_ORIGIN: location.origin
311313
};

src/extensionsIntegrated/Phoenix-live-preview/main.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ define(function (require, exports, module) {
6666

6767
const StaticServer = Phoenix.browser.isTauri? NodeStaticServer : BrowserStaticServer;
6868

69+
const EVENT_EMBEDDED_IFRAME_WHO_AM_I = 'whoAmIframePhoenix';
70+
6971
const PREVIEW_TRUSTED_PROJECT_KEY = "preview_trusted";
7072
const PREVIEW_PROJECT_README_KEY = "preview_readme";
7173

@@ -79,6 +81,24 @@ define(function (require, exports, module) {
7981
</iframe>
8082
`;
8183

84+
// jQuery objects
85+
let $icon,
86+
$iframe,
87+
$panel,
88+
$pinUrlBtn,
89+
$highlightBtn,
90+
$livePreviewPopBtn,
91+
$reloadBtn;
92+
93+
StaticServer.on(EVENT_EMBEDDED_IFRAME_WHO_AM_I, function () {
94+
if($iframe && $iframe[0]) {
95+
const iframeDom = $iframe[0];
96+
iframeDom.contentWindow.postMessage({
97+
isTauri: Phoenix.browser.isTauri
98+
}, "*"); // this is not sensitive info, and is only dispatched if requested by the iframe
99+
}
100+
});
101+
82102
function _isLiveHighlightEnabled() {
83103
return CommandManager.get(Commands.FILE_LIVE_HIGHLIGHT).getChecked();
84104
}
@@ -183,16 +203,6 @@ define(function (require, exports, module) {
183203
return new StaticServer.StaticServer(config);
184204
}
185205

186-
// jQuery objects
187-
let $icon,
188-
$iframe,
189-
$panel,
190-
$pinUrlBtn,
191-
$highlightBtn,
192-
$livePreviewPopBtn,
193-
$reloadBtn;
194-
195-
196206
// Templates
197207
ExtensionUtils.loadStyleSheet(module, "live-preview.css");
198208
// Other vars

src/extensionsIntegrated/Phoenix-live-preview/markdown.html

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,27 @@
1717
<style>
1818
{{{GFM_CSS}}}
1919
</style>
20+
<script>
21+
{{{TRUSTED_ORIGINS_EMBED}}}
22+
// this is for managing who am i context in iframes embedded in phoenix to have special handling.
23+
window.addEventListener('message', function(event) {
24+
if (!TRUSTED_ORIGINS_EMBED[event.origin]) {
25+
return; // Ignore messages from unexpected origins
26+
}
27+
28+
window.__PHOENIX_EMBED_INFO = {
29+
isTauri: event.data.isTauri
30+
};
31+
});
32+
if(window.self !== window.parent){
33+
// in an iframe
34+
window.parent.postMessage({
35+
handlerName: "ph-liveServer",
36+
eventName: 'whoAmIframePhoenix',
37+
href: location.href
38+
}, "{{{PARENT_ORIGIN}}}");
39+
}
40+
</script>
2041
<script type="text/javascript">
2142
function inIframe () {
2243
try {
@@ -31,7 +52,10 @@
3152
// It will confuse the use seeing the browser save dialog inside phoenix.
3253
document.savePageCtrlSDisabledByPhoenix = true;
3354
document.addEventListener("keydown", function(e) {
34-
if (e.key === 's' && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
55+
if (window.__PHOENIX_EMBED_INFO &&
56+
e.key === 's' && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
57+
// we only do this in iframes embedded directly inside phoenix, in popped out iframes
58+
// we don't need to capture anything.
3559
e.preventDefault();
3660
}
3761
}, false);
@@ -46,13 +70,19 @@
4670
return absoluteUrl.href;
4771
}
4872
document.addEventListener('click', function(event) {
49-
if (event.target.tagName === 'A' && (event.target.target === '_blank')) {
50-
const href = getAbsoluteUrl(event.target.getAttribute('href'));
51-
window.parent.postMessage({
52-
handlerName: "ph-liveServer",
53-
eventName: 'embeddedIframeHrefClick',
54-
href: href
55-
}, "{{{PARENT_ORIGIN}}}");
73+
// in desktop phoenix builds, tauri will not open anchor tags in browser if it is in
74+
// an iframe(except for the intel mac bug)
75+
// in normal browsers, we dont need to do this.
76+
if (window.__PHOENIX_EMBED_INFO && window.__PHOENIX_EMBED_INFO.isTauri &&
77+
event.target.tagName === 'A' && (event.target.target === '_blank')) {
78+
const href = getAbsoluteUrl(event.target.getAttribute('href'));
79+
window.parent.postMessage({
80+
handlerName: "ph-liveServer",
81+
eventName: 'embeddedIframeHrefClick',
82+
href: href
83+
}, "{{{PARENT_ORIGIN}}}");
84+
event.preventDefault(); // in intel mac desktop, tauri seems to open in browser
85+
// causing 2 tabs to open. in m1 macs its not there. so we prevent default behavior.
5686
}
5787
});
5888
}

0 commit comments

Comments
 (0)