fix: 修复 valueChange 不会因为子属性变化而通知父属性事件监听, 考虑到后续推荐直接使用 setValue, 也实现了 valueChange 事件

This commit is contained in:
力皓 2021-06-09 14:57:52 +08:00
parent 6176608ac7
commit 1297e3c0e5
3 changed files with 40 additions and 16 deletions

View File

@ -4,6 +4,7 @@ import { SettingPropEntry } from './setting-prop-entry';
import { SettingEntry } from './setting-entry'; import { SettingEntry } from './setting-entry';
import { computed, obx } from '@ali/lowcode-editor-core'; import { computed, obx } from '@ali/lowcode-editor-core';
import { cloneDeep } from '@ali/lowcode-utils'; import { cloneDeep } from '@ali/lowcode-utils';
import { ISetValueOptions } from '../../types';
function getSettingFieldCollectorKey(parent: SettingEntry, config: FieldConfig) { function getSettingFieldCollectorKey(parent: SettingEntry, config: FieldConfig) {
let cur = parent; let cur = parent;
@ -140,7 +141,7 @@ export class SettingField extends SettingPropEntry implements SettingEntry {
private hotValue: any; private hotValue: any;
setValue(val: any, isHotValue?: boolean, force?: boolean, extraOptions?: any) { setValue(val: any, isHotValue?: boolean, force?: boolean, extraOptions?: ISetValueOptions) {
if (isHotValue) { if (isHotValue) {
this.setHotValue(val, extraOptions); this.setHotValue(val, extraOptions);
return; return;
@ -172,9 +173,14 @@ export class SettingField extends SettingPropEntry implements SettingEntry {
this.valueChange(); this.valueChange();
} }
setHotValue(data: any, options?: any) { setHotValue(data: any, options?: ISetValueOptions) {
this.hotValue = data; this.hotValue = data;
const value = this.transducer.toNative(data); const value = this.transducer.toNative(data);
if (options) {
options.fromSetHotValue = true;
} else {
options = { fromSetHotValue: true };
}
if (this.isUseVariable()) { if (this.isUseVariable()) {
const oldValue = this.getValue(); const oldValue = this.getValue();
this.setValue({ this.setValue({
@ -191,7 +197,7 @@ export class SettingField extends SettingPropEntry implements SettingEntry {
return; return;
} }
this.valueChange(); this.valueChange(options);
} }
onEffect(action: () => void): () => void { onEffect(action: () => void): () => void {

View File

@ -6,6 +6,8 @@ import { Node } from '../../document';
import { ComponentMeta } from '../../component-meta'; import { ComponentMeta } from '../../component-meta';
import { Designer } from '../designer'; import { Designer } from '../designer';
import { EventEmitter } from 'events'; import { EventEmitter } from 'events';
import { ISetValueOptions } from '../../types';
import { isSettingField } from './setting-field';
export class SettingPropEntry implements SettingEntry { export class SettingPropEntry implements SettingEntry {
// === static properties === // === static properties ===
@ -162,7 +164,7 @@ export class SettingPropEntry implements SettingEntry {
/** /**
* *
*/ */
setValue(val: any, isHotValue?: boolean, force?: boolean, extraOptions?: any) { setValue(val: any, isHotValue?: boolean, force?: boolean, extraOptions?: ISetValueOptions) {
const oldValue = this.getValue(); const oldValue = this.getValue();
if (this.type === 'field') { if (this.type === 'field') {
this.parent.setPropValue(this.name, val); this.parent.setPropValue(this.name, val);
@ -178,6 +180,10 @@ export class SettingPropEntry implements SettingEntry {
} }
} }
this.notifyValueChange(oldValue, val); this.notifyValueChange(oldValue, val);
// 如果 fromSetHotValue那么在 setHotValue 中已经调用过 valueChange 了
if (!extraOptions?.fromSetHotValue) {
this.valueChange(extraOptions);
}
} }
/** /**
@ -272,8 +278,12 @@ export class SettingPropEntry implements SettingEntry {
/** /**
* @deprecated * @deprecated
*/ */
valueChange() { valueChange(options: ISetValueOptions = {}) {
this.emitter.emit('valuechange'); this.emitter.emit('valuechange', options);
if (this.parent && isSettingField(this.parent)) {
this.parent.valueChange(options);
}
} }
notifyValueChange(oldValue: any, newValue:any) { notifyValueChange(oldValue: any, newValue:any) {
@ -288,15 +298,6 @@ export class SettingPropEntry implements SettingEntry {
return false; return false;
} }
/*
getConfig<K extends keyof IPropConfig>(configName?: K): IPropConfig[K] | IPropConfig {
if (configName) {
return this.config[configName];
}
return this.config;
}
*/
getVariableValue() { getVariableValue() {
const v = this.getValue(); const v = this.getValue();
if (isJSExpression(v)) { if (isJSExpression(v)) {

View File

@ -19,3 +19,20 @@ const utils = {
getNodeSchemaById, getNodeSchemaById,
}; };
export type Utils = typeof utils; export type Utils = typeof utils;
export enum PROP_VALUE_CHANGED_TYPE {
/**
* normal set value
*/
SET_VALUE = 'SET_VALUE',
/**
* value changed caused by sub-prop value change
*/
SUB_VALUE_CHANGE = 'SUB_VALUE_CHANGE',
}
export interface ISetValueOptions {
disableMutator?: boolean;
type?: PROP_VALUE_CHANGED_TYPE;
fromSetHotValue?: boolean;
}