import { merge } from 'lodash'; import { ConvertConfig, convert } from '../../../common/convert'; import { EditorOptionlMaterial, EditorOptionHook } from './types'; import { parseHooks, getDefaultValue } from './utils'; import { guid, IDisposable, IInital, KV } from 'ts-toolset'; import { isUndefinedOrNull } from 'ts-toolset/dist/common/types'; import { IEditorOptionEntity, IPropEntity } from '../../../models/base'; export interface IEditorOption extends IInital, IDisposable { readonly id: string; readonly key: string; readonly value: any; readonly realValue: any; readonly matchData: any; readonly data: any; readonly propName: string | undefined; readonly propKey: string; readonly propEntity: IPropEntity; readonly editorOptionEntity: IEditorOptionEntity; readonly editorOptionConfig: any; setValue(value: any, matchData?: any): void; setRealValue(value: any, matchData?: any): void; setData(data: any): void; setVisible(visible: boolean): void; addHook(hookName: string, callback: EditorOptionHook): void; getHook(hookName: string): EditorOptionHook | undefined; } export class EditorOption implements IEditorOption { private _id?: string; private _value: any; private _realValue: any; private _matchData: any; private _data: any = {}; private _visible: boolean = true; private _convert?: ConvertConfig; private _hooks: KV = {}; private _propEntity?: IPropEntity; private _editorOptionEntity?: IEditorOptionEntity; get id() { return this._id!; } get value() { return this._value; } get realValue() { return this._realValue; } get matchData() { return this._matchData; } get data() { return this._data; } get visible() { return this._visible; } get key() { return this._editorOptionEntity!.key; } get propName() { return this.propEntity.name; } get propKey() { return this._propEntity!.key; } get propEntity() { return this._propEntity!; } get editorOptionEntity() { return this._editorOptionEntity!; } get editorOptionConfig() { return this._editorOptionEntity!.config || {}; } private _setValue(value: any, matchData?: any) { this._value = this._propEntity!.combination ? { ...this._value, ...value } : value; this._realValue = this._convert ? convert(this._convert, this._value, true) : this._value; this._matchData = matchData; } private _setRealValue(value: any, matchData?: any) { this._realValue = this._propEntity!.combination ? { ...this._value, ...value } : value; this._value = this._convert ? convert(this._convert, this._realValue) : this._realValue; this._matchData = matchData; } initial({ prop, editorOptionEntity, value }: EditorOptionlMaterial) { this._id = guid(); this._propEntity = prop; if (prop.editorOption && editorOptionEntity) { prop.editorOption.hooks && (this._hooks = { ...this._hooks, ...parseHooks(prop.editorOption.hooks) }); this._editorOptionEntity = merge({}, prop.editorOption, editorOptionEntity); this._convert = prop.editorOption.convert; } let defaultValue = getDefaultValue(prop, value); this._realValue = defaultValue; this._value = this._convert ? convert(this._convert, defaultValue) : defaultValue; this._visible = !!prop.editorOption; // editorOption 有无代表编辑器是否渲染,prop 的 innore 代表值是否收集 } setValue(value: any, matchData?: any) { if (this._propEntity) { let targetValue = getDefaultValue(this._propEntity, value); isUndefinedOrNull(value) && ('defaultValue' in this._propEntity) ? this._setRealValue(targetValue, matchData) : this._setValue(targetValue, matchData); } } setRealValue(value: any, matchData?: any) { this._propEntity && this._setRealValue(getDefaultValue(this._propEntity, value), matchData); } setData(data: any) { this._data = { ...this._data, ...data }; } setVisible(visible: boolean) { this._visible = visible; } addHook(hookName: string, callback: EditorOptionHook) { this._hooks[hookName] = callback; } getHook(hookName: string) { return this._hooks[hookName]; } dispose() { this._id = undefined; this._value = undefined; this._realValue = undefined; this._matchData = undefined; this._data = {}; this._propEntity = undefined; this._editorOptionEntity = undefined; this._hooks = {}; this._visible = false; } }