feat: current DocuemntInstance add refresh method

This commit is contained in:
wanying.jwy 2020-08-24 16:04:28 +08:00
parent cf502929af
commit b18a0d27c7
2 changed files with 20 additions and 2 deletions

View File

@ -5,7 +5,7 @@ import { Component, createElement, Fragment } from 'rax';
import { useRouter } from './rax-use-router'; import { useRouter } from './rax-use-router';
import { DocumentInstance, SimulatorRendererContainer } from './renderer'; import { DocumentInstance, SimulatorRendererContainer } from './renderer';
import './renderer.less'; import './renderer.less';
import { uniqueId } from '@ali/lowcode-utils';
// patch cloneElement avoid lost keyProps // patch cloneElement avoid lost keyProps
const originCloneElement = (window as any).Rax.cloneElement; const originCloneElement = (window as any).Rax.cloneElement;
@ -43,6 +43,7 @@ export default class SimulatorRendererView extends Component<{ rendererContainer
componentDidMount() { componentDidMount() {
const { rendererContainer } = this.props; const { rendererContainer } = this.props;
this.unlisten = rendererContainer.onLayoutChange(() => { this.unlisten = rendererContainer.onLayoutChange(() => {
debugger;
this.forceUpdate(); this.forceUpdate();
}); });
} }
@ -142,10 +143,18 @@ class Renderer extends Component<{
documentInstance: DocumentInstance; documentInstance: DocumentInstance;
}> { }> {
private unlisten: any; private unlisten: any;
private key: string;
componentWillMount() {
this.key = uniqueId('renderer');
}
componentDidMount() { componentDidMount() {
const { documentInstance } = this.props; const { documentInstance } = this.props;
this.unlisten = documentInstance.onReRender(() => { this.unlisten = documentInstance.onReRender((params) => {
if (params && params.shouldRemount) {
this.key = uniqueId('renderer');
}
this.forceUpdate(); this.forceUpdate();
}); });
} }
@ -168,6 +177,7 @@ class Renderer extends Component<{
components={renderer.components} components={renderer.components}
appHelper={renderer.context} appHelper={renderer.context}
designMode={renderer.designMode} designMode={renderer.designMode}
key={this.key}
suspended={documentInstance.suspended} suspended={documentInstance.suspended}
self={documentInstance.scope} self={documentInstance.scope}
onCompGetRef={(schema: any, ref: any) => { onCompGetRef={(schema: any, ref: any) => {

View File

@ -141,6 +141,10 @@ export class DocumentInstance {
} }
} }
refresh() {
this.emitter.emit('rerender', { shouldRemount: true });
}
onReRender(fn: () => void) { onReRender(fn: () => void) {
this.emitter.on('rerender', fn); this.emitter.on('rerender', fn);
return () => { return () => {
@ -234,6 +238,10 @@ export class SimulatorRendererContainer implements BuiltinSimulatorRenderer {
return this._documentInstances; return this._documentInstances;
} }
get currentDocumentInstance() {
return this._documentInstances.find((item) => item.id === host.project.currentDocument?.id);
}
constructor() { constructor() {
this.dispose = host.connect(this, () => { this.dispose = host.connect(this, () => {
// sync layout config // sync layout config