auto modeling

This commit is contained in:
pengfei.zhou 2019-07-16 15:30:46 +08:00
parent b76851e044
commit d255c05aef
4 changed files with 38 additions and 10 deletions

View File

@ -1,6 +1,10 @@
import { Text } from "./view/view"; import { Text } from "./view/view";
import { Color } from "./util/color";
const v = new Text const v = new Text
v.width = 1 v.width = 20
console.log('start:') v.height = 30
v.height v.left = 5
v.top = 5
v.bgColor = Color.parse('#00ff00')
console.log(v.toModel())

View File

@ -1,7 +1,10 @@
import { Modeling } from "./types";
/** /**
* Store color as format AARRGGBB or RRGGBB * Store color as format AARRGGBB or RRGGBB
*/ */
export class Color { export class Color implements Modeling {
static TRANSPARENT = new Color(0) static TRANSPARENT = new Color(0)
_value: number = 0 _value: number = 0
@ -32,7 +35,8 @@ export class Color {
return color return color
} }
} }
toNumber() {
toModel() {
return this._value return this._value
} }
} }

View File

@ -18,3 +18,9 @@ export function Property(target: IWatcher, propKey: string) {
} }
}) })
} }
export interface Modeling {
toModel(): Model
}
export type Model = string | number | boolean | Modeling | { [index: string]: Model | undefined }

View File

@ -1,9 +1,8 @@
import { Color, GradientColor } from "../util/color" import { Color, GradientColor } from "../util/color"
import { Property, IWatcher } from "../util/types"; import { Property, IWatcher, Modeling, Model } from "../util/types";
export abstract class View implements IWatcher { export abstract class View implements IWatcher, Modeling {
@Property @Property
width: number = 0 width: number = 0
@ -63,8 +62,23 @@ export abstract class View implements IWatcher {
} }
/** Anchor end*/ /** Anchor end*/
onPropertyChanged(propKey: string, oldV: any, newV: any): void { __dirty_props__?: { [index: string]: Model | undefined }
console.log(`onPropertyChanged:${propKey},old value is ${oldV},new value is ${newV}`)
onPropertyChanged(propKey: string, oldV: Model, newV: Model): void {
//console.log(`onPropertyChanged:${propKey},old value is ${oldV},new value is ${newV}`)
if (this.__dirty_props__ === undefined) {
this.__dirty_props__ = {}
}
if (newV instanceof Object
&& Reflect.has(newV, 'toModel')
&& Reflect.get(newV, 'toModel') instanceof Function) {
newV = Reflect.apply(Reflect.get(newV, 'toModel'), newV, [])
}
this.__dirty_props__[propKey] = newV
}
toModel() {
return this.__dirty_props__ || {}
} }
} }