refactor(editor,stage): 完善页面变化后画布大小更新

This commit is contained in:
roymondchen 2025-06-06 19:29:20 +08:00
parent b78b2011e5
commit 0bd8496fac
4 changed files with 35 additions and 7 deletions

View File

@ -164,9 +164,23 @@ watch(page, (page) => {
}, 3000); }, 3000);
runtime.updatePageId?.(page.id); runtime.updatePageId?.(page.id);
nextTick(() => {
stage?.select(page.id); const unWatch = watch(
}); stageLoading,
() => {
if (stageLoading.value) {
return;
}
nextTick(() => {
stage?.select(page.id);
unWatch();
});
},
{
immediate: true,
},
);
} }
}); });

View File

@ -236,6 +236,12 @@ export default class StageMask extends Rule {
const { clientHeight, clientWidth } = entry.target; const { clientHeight, clientWidth } = entry.target;
this.wrapperHeight = clientHeight; this.wrapperHeight = clientHeight;
this.wrapperWidth = clientWidth; this.wrapperWidth = clientWidth;
if (this.mode === Mode.FIXED) {
this.content.style.width = `${this.wrapperWidth}px`;
this.content.style.height = `${this.wrapperHeight}px`;
}
this.setMaxScrollLeft(); this.setMaxScrollLeft();
this.setMaxScrollTop(); this.setMaxScrollTop();
}); });
@ -286,7 +292,9 @@ export default class StageMask extends Rule {
private setHeight(height: number): void { private setHeight(height: number): void {
this.height = height; this.height = height;
this.setMaxScrollTop(); this.setMaxScrollTop();
this.content.style.height = `${height}px`; if (this.mode !== Mode.FIXED) {
this.content.style.height = `${height}px`;
}
} }
/** /**
@ -296,7 +304,9 @@ export default class StageMask extends Rule {
private setWidth(width: number): void { private setWidth(width: number): void {
this.width = width; this.width = width;
this.setMaxScrollLeft(); this.setMaxScrollLeft();
this.content.style.width = `${width}px`; if (this.mode !== Mode.FIXED) {
this.content.style.width = `${width}px`;
}
} }
/** /**

View File

@ -19,7 +19,7 @@
import { EventEmitter } from 'events'; import { EventEmitter } from 'events';
import type { Id } from '@tmagic/core'; import type { Id } from '@tmagic/core';
import { getElById, getHost, injectStyle, isSameDomain } from '@tmagic/core'; import { getElById, getHost, guid, injectStyle, isSameDomain } from '@tmagic/core';
import { DEFAULT_ZOOM, RenderType } from './const'; import { DEFAULT_ZOOM, RenderType } from './const';
import style from './style.css?raw'; import style from './style.css?raw';
@ -54,7 +54,10 @@ export default class StageRender extends EventEmitter {
} }
public getMagicApi = () => ({ public getMagicApi = () => ({
onPageElUpdate: (el: HTMLElement) => this.emit('page-el-update', el), id: guid(),
onPageElUpdate: (el: HTMLElement) => {
this.emit('page-el-update', el);
},
onRuntimeReady: (runtime: Runtime) => { onRuntimeReady: (runtime: Runtime) => {
if (this.runtime) { if (this.runtime) {
return; return;

View File

@ -214,6 +214,7 @@ export interface Runtime {
} }
export interface Magic { export interface Magic {
id: string;
/** 当前页面的根节点变化时调用该方法编辑器会同步该el和stage的大小该方法由stage注入到iframe.contentWindow中 */ /** 当前页面的根节点变化时调用该方法编辑器会同步该el和stage的大小该方法由stage注入到iframe.contentWindow中 */
onPageElUpdate: (el: HTMLElement) => void; onPageElUpdate: (el: HTMLElement) => void;