Skip to content

Commit 779d8ae

Browse files
committed
perfect resize
1 parent f326e27 commit 779d8ae

File tree

2 files changed

+44
-11
lines changed

2 files changed

+44
-11
lines changed

modules/markup/renderer.go

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -205,20 +205,41 @@ func renderIFrame(ctx *RenderContext, output io.Writer, iframeSandbox string) er
205205
// "allow-same-origin" should never be used, it leads to XSS attack, and it makes the JS in iframe can access parent window's config and CSRF token
206206
// when there is a strict CORS policy, the "onload" script can not read the loaded height at the moment.
207207
// TODO: when using dark theme, if the rendered content doesn't have proper style, the default text color is black, which is not easy to read
208-
_, err := io.WriteString(output, fmt.Sprintf(`
209-
<iframe src="%s/%s/%s/render/%s/%s"
210-
name="giteaExternalRender"
211-
onload="try { this.height=giteaExternalRender.document.documentElement.scrollHeight; } catch(e) { this.style.height='80vh'; }"
208+
_, err := io.WriteString(output,
209+
`
210+
<script type='module' >
211+
window.addEventListener('message', (e) => {
212+
const el = document.getElementById('gitea-external-render');
213+
if (e.data && e.data.giteaIframeCmd === 'resize') {
214+
el.setAttribute('data-iframe-resized', 'true');
215+
el.style.height = e.data.height+'px';
216+
}
217+
});
218+
window.giteaExternalRenderOnload = (el) => {
219+
setTimeout(() => {
220+
if(el.getAttribute('data-iframe-resized')) return;
221+
try {
222+
el.height = el.document.documentElement.scrollHeight;
223+
} catch(e) {
224+
el.style.height = '80vh';
225+
}
226+
}, 100);
227+
};
228+
</script>
229+
`+fmt.Sprintf(`
230+
<iframe id="gitea-external-render"
231+
src="%s/%s/%s/render/%s/%s"
212232
width="100%%" height="0" scrolling="auto" frameborder="0" style="overflow: hidden"
233+
onload="giteaExternalRenderOnload(this)"
213234
sandbox="%s"
214235
></iframe>`,
215-
setting.AppSubURL,
216-
url.PathEscape(ctx.Metas["user"]),
217-
url.PathEscape(ctx.Metas["repo"]),
218-
ctx.Metas["BranchNameSubURL"],
219-
url.PathEscape(ctx.RelativePath),
220-
html.EscapeString(iframeSandbox),
221-
))
236+
setting.AppSubURL,
237+
url.PathEscape(ctx.Metas["user"]),
238+
url.PathEscape(ctx.Metas["repo"]),
239+
ctx.Metas["BranchNameSubURL"],
240+
url.PathEscape(ctx.RelativePath),
241+
html.EscapeString(iframeSandbox),
242+
))
222243
return err
223244
}
224245

routers/web/repo/render.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package repo
77
import (
88
"net/http"
99
"path"
10+
"strings"
1011

1112
"code.gitea.io/gitea/modules/context"
1213
"code.gitea.io/gitea/modules/git"
@@ -57,6 +58,7 @@ func RenderFile(ctx *context.Context) {
5758
}
5859

5960
ctx.Resp.Header().Add("Content-Security-Policy", externalCSP)
61+
ctx.Resp.Header().Add("Content-Type", "text/html")
6062

6163
if err = markup.RenderDirect(&markup.RenderContext{
6264
Ctx: ctx,
@@ -69,4 +71,14 @@ func RenderFile(ctx *context.Context) {
6971
ctx.ServerError("RenderDirect", err)
7072
return
7173
}
74+
75+
if strings.HasPrefix(ctx.Resp.Header().Get("Content-Type"), "text/html") {
76+
_, _ = ctx.Resp.Write([]byte(`
77+
<script type='module'>
78+
const fn = () => parent.postMessage({'giteaIframeCmd':'resize','height':document.documentElement.scrollHeight}, "*");
79+
fn();
80+
setInterval(fn, 500);
81+
</script>
82+
`))
83+
}
7284
}

0 commit comments

Comments
 (0)