Skip to content

Commit ecf91a4

Browse files
authored
multi route hash to both frames (microsoft#6996)
* route hash to both frames * fix multi routing * reorder
1 parent 55aacb2 commit ecf91a4

2 files changed

Lines changed: 46 additions & 23 deletions

File tree

docs/multi.html

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
(function () {
7474
var left = document.getElementById("left");
7575
var right = document.getElementById("right");
76+
var frames = [left, right]
7677
var selectLeft = document.getElementById("selectleft");
7778
var selectRight = document.getElementById("selectright");
7879
var divider = document.getElementById("divider");
@@ -86,8 +87,7 @@
8687
var source = msg.source;
8788
if (!!data.broadcast) {
8889
data.outer = true;
89-
[left, right]
90-
.filter(function (ifrm) {
90+
frames.filter(function (ifrm) {
9191
return ifrm.contentWindow !== source;
9292
})
9393
.forEach(function (ifrm) {
@@ -96,18 +96,26 @@
9696
}
9797
};
9898

99-
function updateSrc(ifrm, select) {
99+
function updateSrc(ifrm, select, hash) {
100+
var h = hash ? "#" + hash : "";
100101
if (localhost) {
101-
ifrm.src = "/index.html" + flags;
102+
ifrm.src = "/index.html" + flags + h;
102103
} else {
103-
ifrm.src = select.value + flags;
104+
ifrm.src = select.value + flags + h;
104105
}
105106
}
107+
function handleHash() {
108+
var hashLeft = ""
109+
var hashRight = ""
110+
var route = (window.location.hash || "#");
111+
var parts = route.replace(/^#/, '').split(':|:', 2);
112+
updateSrc(left, selectLeft, parts[0]);
113+
updateSrc(right, selectRight, parts[1]);
114+
window.history.replaceState('', '', '#')
115+
}
106116

107117
selectLeft.onchange = function () { updateSrc(left, selectLeft); }
108118
selectRight.onchange = function () { updateSrc(right, selectRight); }
109-
updateSrc(left, selectLeft);
110-
updateSrc(right, selectRight);
111119

112120
function setWidths() {
113121
var t = document.body.clientWidth;
@@ -146,7 +154,8 @@
146154
}
147155
}
148156
window.onresize = setWidths;
149-
setWidths();
157+
window.onhashchange = handleHash;
158+
150159
divider.onmouseover = function () {
151160
document.body.style.cursor = "w-resize";
152161
divider.onmousedown = startDrag;
@@ -155,6 +164,9 @@
155164
document.body.style.cursor = "default";
156165
divider.onmousedown = null;
157166
}
167+
168+
setWidths();
169+
handleHash();
158170
})();
159171
</script>
160172
<!-- @include tracking.html -->

webapp/public/multi.html

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,38 +49,45 @@
4949

5050
var left = document.getElementById("left");
5151
var right = document.getElementById("right");
52+
var frames = [left, right]
5253
var divider = document.getElementById("divider");
5354
var localhost = window.location.hostname == "localhost";
5455
var editor = (pxtConfig ? pxtConfig.relprefix : '/').replace(/-*$/, '');
5556
var flags = "?nestededitorsim=1&editorlayout=ide&nosandbox=1";
5657
var ratio = .5;
5758
var dividerWidth = 14;
5859

59-
window.onmessage = function (msg) {
60+
function updateSrc(ifrm, hash) {
61+
var h = hash ? "#" + hash : "";
62+
if (localhost) {
63+
ifrm.src = "/index.html" + flags + h;
64+
} else {
65+
ifrm.src = editor + flags + h;
66+
}
67+
}
68+
69+
function handleHash() {
70+
var hashLeft = ""
71+
var hashRight = ""
72+
var route = (window.location.hash || "#");
73+
var parts = route.replace(/^#/, '').split(':|:', 2);
74+
updateSrc(left, parts[0]);
75+
updateSrc(right, parts[1]);
76+
window.history.replaceState('', '', '#')
77+
}
78+
function handleMessage(msg) {
6079
var data = msg.data;
6180
var source = msg.source;
6281
if (!!data.broadcast) {
6382
data.outer = true;
64-
[left, right]
65-
.filter(function (ifrm) {
83+
frames.filter(function (ifrm) {
6684
return ifrm.contentWindow !== source;
6785
})
6886
.forEach(function (ifrm) {
6987
ifrm.contentWindow.postMessage(data, window.location.origin)
7088
});
7189
}
72-
};
73-
74-
function updateSrc(ifrm) {
75-
if (localhost) {
76-
ifrm.src = "/index.html" + flags;
77-
} else {
78-
ifrm.src = editor + flags;
79-
}
8090
}
81-
updateSrc(left);
82-
updateSrc(right);
83-
8491
function setWidths() {
8592
var t = document.body.clientWidth;
8693
var n = Math.floor(t * ratio);
@@ -115,7 +122,9 @@
115122
}
116123
}
117124
window.onresize = setWidths;
118-
setWidths();
125+
window.onhashchange = handleHash;
126+
window.onmessage = handleMessage;
127+
119128
divider.onmouseover = function () {
120129
document.body.style.cursor = "w-resize";
121130
divider.onmousedown = startDrag;
@@ -124,6 +133,8 @@
124133
document.body.style.cursor = "default";
125134
divider.onmousedown = null;
126135
}
136+
setWidths();
137+
handleHash();
127138
})();
128139
</script>
129140
<!-- @include tracking.html -->

0 commit comments

Comments
 (0)