iframe 自动高度

This commit is contained in:
fofolee 2022-04-22 00:27:36 +08:00
parent 3ede4cdd73
commit 3e1e72b7bc

View File

@ -19,6 +19,8 @@
<iframe <iframe
ref="iframe" ref="iframe"
:srcdoc="runResult" :srcdoc="runResult"
:height="frameHeight"
@load="frameLoad"
frameborder="0" frameborder="0"
v-if="htmlOutput" v-if="htmlOutput"
></iframe> ></iframe>
@ -41,7 +43,9 @@
<iframe <iframe
ref="iframe" ref="iframe"
:srcdoc="runResult" :srcdoc="runResult"
:height="frameHeight"
frameborder="0" frameborder="0"
@load="frameLoad"
v-if="htmlOutput" v-if="htmlOutput"
></iframe> ></iframe>
<pre <pre
@ -75,6 +79,7 @@ export default {
history: [], history: [],
historyIdx: null, historyIdx: null,
htmlOutput: false, htmlOutput: false,
frameHeight: 0,
}; };
}, },
props: { props: {
@ -94,8 +99,8 @@ export default {
needTempPayload() { needTempPayload() {
return ["edit", "new", "config"].includes(this.action.type); return ["edit", "new", "config"].includes(this.action.type);
}, },
iframeCtw() { maxHeight() {
return this.$refs?.iframe?.contentWindow; return this.fromUtools ? 600 : 300;
}, },
}, },
methods: { methods: {
@ -221,7 +226,7 @@ export default {
// //
showRunResult(content, isSuccess, action) { showRunResult(content, isSuccess, action) {
this.isResultShow = true; this.isResultShow = true;
this.setIframe(); this.frameInit();
this.runResultStatus = isSuccess; this.runResultStatus = isSuccess;
let contlength = content?.length || 0; let contlength = content?.length || 0;
if (contlength > this.resultMaxLength) if (contlength > this.resultMaxLength)
@ -233,22 +238,23 @@ export default {
content.slice(contlength - 100); content.slice(contlength - 100);
let pretreatment = action(content); let pretreatment = action(content);
pretreatment && (this.runResult += pretreatment); pretreatment && (this.runResult += pretreatment);
this.fromUtools && this.outputAutoHeight();
this.$nextTick(() => {
this.outputAutoHeight();
});
}, },
// utools // utools
outputAutoHeight(autoScroll = true, autoHeight = true) { outputAutoHeight(autoScroll = true, autoHeight = true) {
let clientHeight = document.body.clientHeight; if (!this.fromUtools) return;
let pluginHeight = clientHeight < 600 ? clientHeight : 600; this.$nextTick(() => {
autoHeight && utools.setExpendHeight(pluginHeight); let clientHeight = document.body.clientHeight;
autoScroll && let pluginHeight =
window.scroll({ clientHeight < this.maxHeight ? clientHeight : this.maxHeight;
top: clientHeight, autoHeight && utools.setExpendHeight(pluginHeight);
left: 0, autoScroll &&
behavior: "smooth", window.scroll({
}); top: clientHeight,
left: 0,
behavior: "smooth",
});
});
}, },
stopRun() { stopRun() {
this.runResult = ""; this.runResult = "";
@ -258,17 +264,30 @@ export default {
document.removeEventListener("keydown", this.listener, true); document.removeEventListener("keydown", this.listener, true);
} }
}, },
setIframe() { //
frameInit() {
this.$nextTick(() => { this.$nextTick(() => {
if (!this.iframeCtw) return; let cfw = this.$refs?.iframe?.contentWindow;
if (!cfw) return;
let ctx = { let ctx = {
quickcommand, quickcommand,
utools, utools,
parent: undefined, parent: undefined,
}; };
Object.assign(this.iframeCtw, _.cloneDeep(ctx)); Object.assign(cfw, _.cloneDeep(ctx));
}); });
}, },
//
frameLoad() {
let cfw = this.$refs?.iframe?.contentWindow;
if (this.$q.dark.isActive) cfw.document.body.style.color = "white";
cfw.document.body.style.margin = "0";
this.frameHeight = Math.min(
cfw.document.body.scrollHeight,
this.maxHeight
);
this.outputAutoHeight();
},
}, },
unmounted() { unmounted() {
this.stopRun(); this.stopRun();
@ -285,6 +304,5 @@ export default {
} }
iframe { iframe {
width: 100%; width: 100%;
height: 550px;
} }
</style> </style>