fix: 修复 setDevice 里获取 currentDocument 的逻辑

This commit is contained in:
力皓 2020-11-20 11:37:12 +08:00
parent 447ed9b8f9
commit 275b7aaef9
7 changed files with 44 additions and 34 deletions

View File

@ -1308,26 +1308,6 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
return nearBy; return nearBy;
} }
_innerWaitForCurrentDocument(): Promise<any> {
const timeGap = 200;
return new Promise((resolve) => {
setTimeout(() => {
if (this.project.currentDocument) {
resolve();
}
}, timeGap);
}).catch(() => {
return this.waitForCurrentDocument();
});
}
waitForCurrentDocument(): Promise<any> {
if (this.project.currentDocument) {
return Promise.resolve();
}
return this._innerWaitForCurrentDocument();
}
// #endregion // #endregion
} }

View File

@ -36,6 +36,7 @@ editor.set('designer', designer);
designer.project.onCurrentDocumentChange((doc) => { designer.project.onCurrentDocumentChange((doc) => {
bus.emit(VE_EVENTS.VE_PAGE_PAGE_READY); bus.emit(VE_EVENTS.VE_PAGE_PAGE_READY);
editor.set('currentDocuemnt', doc);
}); });
// 升级 Props // 升级 Props

View File

@ -1,6 +1,6 @@
import { EventEmitter } from 'events'; import { EventEmitter } from 'events';
import Flags from './flags'; import Flags from './flags';
import { designer } from './editor'; import { editor } from './editor';
const domReady = require('domready'); const domReady = require('domready');
@ -192,10 +192,11 @@ export class Viewport {
return this.preview; return this.preview;
} }
setDevice(device = 'pc') { async setDevice(device = 'pc') {
if (this.getDevice() !== device) { if (this.getDevice() !== device) {
this.device = device; this.device = device;
designer.currentDocument?.simulator?.set('device', device === 'mobile' ? 'mobile' : 'default'); const currentDocument = await editor.onceGot('currentDocuemnt');
currentDocument?.simulator?.set('device', device === 'mobile' ? 'mobile' : 'default');
// Flags.setSimulator(device); // Flags.setSimulator(device);
// this.applyMediaCSS(); // this.applyMediaCSS();
this.emitter.emit('devicechange', device); this.emitter.emit('devicechange', device);

View File

@ -2,6 +2,7 @@ import React, { Component, PureComponent, createElement as reactCreateElement }
import ReactDOM from 'react-dom'; import ReactDOM from 'react-dom';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import Debug from 'debug'; import Debug from 'debug';
import { ConfigProvider } from '@alifd/next';
import { isEmpty } from '@ali/b3-one/lib/obj'; import { isEmpty } from '@ali/b3-one/lib/obj';
import AppContext from './context/appContext'; import AppContext from './context/appContext';
import { isFileSchema, goldlog } from './utils'; import { isFileSchema, goldlog } from './utils';
@ -196,6 +197,7 @@ export default class Renderer extends Component {
engine: this, engine: this,
}} }}
> >
<ConfigProvider device={this.props.device}>
<Comp <Comp
key={schema.__ctx && `${schema.__ctx.lunaKey}_${schema.__ctx.idx || '0'}`} key={schema.__ctx && `${schema.__ctx.lunaKey}_${schema.__ctx.idx || '0'}`}
ref={this.__getRef} ref={this.__getRef}
@ -205,6 +207,7 @@ export default class Renderer extends Component {
__designMode={designMode} __designMode={designMode}
{...this.props} {...this.props}
/> />
</ConfigProvider>
</AppContext.Provider> </AppContext.Provider>
); );
} }

View File

@ -177,7 +177,7 @@ export class SimulatorRendererContainer implements BuiltinSimulatorRenderer {
} }
constructor() { constructor() {
this.dispose = host.connect(this, async () => { this.dispose = host.connect(this, () => {
// sync layout config // sync layout config
this._layout = host.project.get('config').layout; this._layout = host.project.get('config').layout;

View File

@ -16,6 +16,7 @@
"dependencies": { "dependencies": {
"@ali/lowcode-types": "^0.13.1-15", "@ali/lowcode-types": "^0.13.1-15",
"@alifd/next": "^1.19.16", "@alifd/next": "^1.19.16",
"lodash.get": "^4.4.2",
"react": "^16" "react": "^16"
}, },
"devDependencies": { "devDependencies": {

View File

@ -1,5 +1,6 @@
import { isI18NObject } from './is-object'; import { isI18NObject } from './is-object';
import get from 'lodash.get';
export function isUseI18NSetter(prototype: any, propName: string) { export function isUseI18NSetter(prototype: any, propName: string) {
const configure = prototype?.options?.configure; const configure = prototype?.options?.configure;
@ -19,3 +20,26 @@ export function convertToI18NObject(v: string | object, locale: string = 'zh_CN'
export function isString(v: any): v is string { export function isString(v: any): v is string {
return typeof v === 'string'; return typeof v === 'string';
} }
function _innerWaitForThing(obj: any, path: string): Promise<any> {
const timeGap = 200;
return new Promise((resolve, reject) => {
setTimeout(() => {
const thing = get(obj, path);
if (thing) {
return resolve(thing);
}
reject();
}, timeGap);
}).catch(() => {
return _innerWaitForThing(obj, path);
});
}
export function waitForThing(obj: any, path: string): Promise<any> {
const thing = get(obj, path);
if (thing) {
return Promise.resolve(thing);
}
return _innerWaitForThing(obj, path);
}