tsx:add ref and parent support

This commit is contained in:
pengfei.zhou 2021-09-02 11:39:51 +08:00 committed by osborn
parent c1f39de8e3
commit aeff0a06dc
13 changed files with 265 additions and 62 deletions

View File

@ -1,51 +1,55 @@
import { import {
Panel,
Group,
VLayout,
layoutConfig,
Gravity,
Text,
Color,
navbar,
jsx, jsx,
VLayout,
Panel,
Gravity,
Group,
LayoutSpec,
Image,
Text,
makeRef,
} from "doric"; } from "doric";
// class MyPanel extends Panel {
// build(root: Group) {
// <VLayout
// space={20}
// gravity={Gravity.Center}
// layoutConfig={{
// widthSpec: LayoutSpec.MOST,
// heightSpec: LayoutSpec.MOST,
// }}
// parent={root}
// >
// <Image imageUrl="https://doric.pub/logo.png" />
// <Text text="Hello,Doric" textSize={20} />
// </VLayout>;
// }
// }
@Entry @Entry
class Example extends Panel { class Counter extends Panel {
onShow() { build(root: Group) {
navbar(context).setTitle("Example"); const ref = makeRef<Text>();
}
build(rootView: Group) {
let count = 0; let count = 0;
(
<VLayout <VLayout
layoutConfig={layoutConfig().just().configAlignment(Gravity.Center)}
width={200}
height={200}
space={20} space={20}
border={{ color: Color.BLUE, width: 1 }}
gravity={Gravity.Center} gravity={Gravity.Center}
layoutConfig={{
widthSpec: LayoutSpec.MOST,
heightSpec: LayoutSpec.MOST,
}}
parent={root}
> >
<Text <Text text={`${count}`} textSize={40} ref={ref} />
tag="Label"
text={`${count}`}
textSize={40}
layoutConfig={layoutConfig().fit()}
/>
<Text <Text
text="Click to count" text="Click to count"
textSize={20} textSize={20}
backgroundColor={Color.parse("#70a1ff")}
textColor={Color.WHITE}
onClick={() => { onClick={() => {
count++; count++;
const label = rootView.findViewByTag("Label") as Text; ref.current.text = `${count}`;
label.text = `${count}`;
}} }}
width={200}
height={50}
/> />
</VLayout> </VLayout>;
).in(rootView);
} }
} }

View File

@ -205,6 +205,27 @@ function ViewComponent(constructor) {
var name = Reflect.get(constructor, PROP_KEY_VIEW_TYPE) || Object.getPrototypeOf(constructor).name; var name = Reflect.get(constructor, PROP_KEY_VIEW_TYPE) || Object.getPrototypeOf(constructor).name;
Reflect.set(constructor, PROP_KEY_VIEW_TYPE, name); Reflect.set(constructor, PROP_KEY_VIEW_TYPE, name);
} }
var Ref = /** @class */ (function () {
function Ref() {
}
Object.defineProperty(Ref.prototype, "current", {
get: function () {
if (!!!this.view) {
throw new Error("Ref is empty");
}
return this.view;
},
set: function (v) {
this.view = v;
},
enumerable: false,
configurable: true
});
return Ref;
}());
function makeRef() {
return new Ref;
}
var View = /** @class */ (function () { var View = /** @class */ (function () {
function View() { function View() {
this.width = 0; this.width = 0;
@ -402,6 +423,27 @@ var View = /** @class */ (function () {
View.prototype.getLocationOnScreen = function (context) { View.prototype.getLocationOnScreen = function (context) {
return this.nativeChannel(context, "getLocationOnScreen")(); return this.nativeChannel(context, "getLocationOnScreen")();
}; };
Object.defineProperty(View.prototype, "props", {
set: function (props) {
this.apply(props);
},
enumerable: false,
configurable: true
});
Object.defineProperty(View.prototype, "parent", {
set: function (v) {
this.in(v);
},
enumerable: false,
configurable: true
});
Object.defineProperty(View.prototype, "ref", {
set: function (ref) {
ref.current = this;
},
enumerable: false,
configurable: true
});
View.prototype.doAnimation = function (context, animation) { View.prototype.doAnimation = function (context, animation) {
var _this = this; var _this = this;
return this.nativeChannel(context, "doAnimation")(animation.toModel()).then(function (args) { return this.nativeChannel(context, "doAnimation")(animation.toModel()).then(function (args) {
@ -2649,10 +2691,9 @@ var jsx = {
children[_i - 2] = arguments$1[_i]; children[_i - 2] = arguments$1[_i];
} }
var e = new constructor(); var e = new constructor();
e.layoutConfig = layoutConfig().fit();
if (config) { if (config) {
for (var key in config) { e.apply(config);
Reflect.set(e, key, Reflect.get(config, key, config), e);
}
} }
if (children && children.length > 0) { if (children && children.length > 0) {
if (e instanceof Group) { if (e instanceof Group) {
@ -3999,6 +4040,7 @@ exports.Panel = Panel;
exports.Property = Property; exports.Property = Property;
exports.Provider = Provider; exports.Provider = Provider;
exports.RIGHT = RIGHT; exports.RIGHT = RIGHT;
exports.Ref = Ref;
exports.Refreshable = Refreshable; exports.Refreshable = Refreshable;
exports.Root = Root; exports.Root = Root;
exports.RotationAnimation = RotationAnimation; exports.RotationAnimation = RotationAnimation;
@ -4040,6 +4082,7 @@ exports.listItem = listItem;
exports.log = log; exports.log = log;
exports.loge = loge; exports.loge = loge;
exports.logw = logw; exports.logw = logw;
exports.makeRef = makeRef;
exports.modal = modal; exports.modal = modal;
exports.navbar = navbar; exports.navbar = navbar;
exports.navigator = navigator; exports.navigator = navigator;

View File

@ -141,6 +141,20 @@ function ViewComponent(constructor) {
const name = Reflect.getMetadata(PROP_KEY_VIEW_TYPE, constructor) || Object.getPrototypeOf(constructor).name; const name = Reflect.getMetadata(PROP_KEY_VIEW_TYPE, constructor) || Object.getPrototypeOf(constructor).name;
Reflect.defineMetadata(PROP_KEY_VIEW_TYPE, name, constructor); Reflect.defineMetadata(PROP_KEY_VIEW_TYPE, name, constructor);
} }
class Ref {
set current(v) {
this.view = v;
}
get current() {
if (!!!this.view) {
throw new Error("Ref is empty");
}
return this.view;
}
}
function makeRef() {
return new Ref;
}
class View { class View {
constructor() { constructor() {
this.width = 0; this.width = 0;
@ -321,6 +335,12 @@ class View {
set props(props) { set props(props) {
this.apply(props); this.apply(props);
} }
set parent(v) {
this.in(v);
}
set ref(ref) {
ref.current = this;
}
doAnimation(context, animation) { doAnimation(context, animation) {
return this.nativeChannel(context, "doAnimation")(animation.toModel()).then((args) => { return this.nativeChannel(context, "doAnimation")(animation.toModel()).then((args) => {
for (let key in args) { for (let key in args) {
@ -2043,10 +2063,9 @@ exports.Display = void 0;
const jsx = { const jsx = {
createElement: function (constructor, config, ...children) { createElement: function (constructor, config, ...children) {
const e = new constructor(); const e = new constructor();
e.layoutConfig = layoutConfig().fit();
if (config) { if (config) {
for (let key in config) { e.apply(config);
Reflect.set(e, key, Reflect.get(config, key, config), e);
}
} }
if (children && children.length > 0) { if (children && children.length > 0) {
if (e instanceof Group) { if (e instanceof Group) {
@ -3107,6 +3126,7 @@ exports.Panel = Panel;
exports.Property = Property; exports.Property = Property;
exports.Provider = Provider; exports.Provider = Provider;
exports.RIGHT = RIGHT; exports.RIGHT = RIGHT;
exports.Ref = Ref;
exports.Refreshable = Refreshable; exports.Refreshable = Refreshable;
exports.Root = Root; exports.Root = Root;
exports.RotationAnimation = RotationAnimation; exports.RotationAnimation = RotationAnimation;
@ -3148,6 +3168,7 @@ exports.listItem = listItem;
exports.log = log; exports.log = log;
exports.loge = loge; exports.loge = loge;
exports.logw = logw; exports.logw = logw;
exports.makeRef = makeRef;
exports.modal = modal; exports.modal = modal;
exports.navbar = navbar; exports.navbar = navbar;
exports.navigator = navigator; exports.navigator = navigator;

View File

@ -1665,6 +1665,20 @@ function ViewComponent(constructor) {
const name = Reflect.getMetadata(PROP_KEY_VIEW_TYPE, constructor) || Object.getPrototypeOf(constructor).name; const name = Reflect.getMetadata(PROP_KEY_VIEW_TYPE, constructor) || Object.getPrototypeOf(constructor).name;
Reflect.defineMetadata(PROP_KEY_VIEW_TYPE, name, constructor); Reflect.defineMetadata(PROP_KEY_VIEW_TYPE, name, constructor);
} }
class Ref {
set current(v) {
this.view = v;
}
get current() {
if (!!!this.view) {
throw new Error("Ref is empty");
}
return this.view;
}
}
function makeRef() {
return new Ref;
}
class View { class View {
constructor() { constructor() {
this.width = 0; this.width = 0;
@ -1845,6 +1859,12 @@ class View {
set props(props) { set props(props) {
this.apply(props); this.apply(props);
} }
set parent(v) {
this.in(v);
}
set ref(ref) {
ref.current = this;
}
doAnimation(context, animation) { doAnimation(context, animation) {
return this.nativeChannel(context, "doAnimation")(animation.toModel()).then((args) => { return this.nativeChannel(context, "doAnimation")(animation.toModel()).then((args) => {
for (let key in args) { for (let key in args) {
@ -3567,10 +3587,9 @@ exports.Display = void 0;
const jsx = { const jsx = {
createElement: function (constructor, config, ...children) { createElement: function (constructor, config, ...children) {
const e = new constructor(); const e = new constructor();
e.layoutConfig = layoutConfig().fit();
if (config) { if (config) {
for (let key in config) { e.apply(config);
Reflect.set(e, key, Reflect.get(config, key, config), e);
}
} }
if (children && children.length > 0) { if (children && children.length > 0) {
if (e instanceof Group) { if (e instanceof Group) {
@ -4872,6 +4891,7 @@ exports.Panel = Panel;
exports.Property = Property; exports.Property = Property;
exports.Provider = Provider; exports.Provider = Provider;
exports.RIGHT = RIGHT; exports.RIGHT = RIGHT;
exports.Ref = Ref;
exports.Refreshable = Refreshable; exports.Refreshable = Refreshable;
exports.Root = Root; exports.Root = Root;
exports.RotationAnimation = RotationAnimation; exports.RotationAnimation = RotationAnimation;
@ -4913,6 +4933,7 @@ exports.listItem = listItem;
exports.log = log; exports.log = log;
exports.loge = loge; exports.loge = loge;
exports.logw = logw; exports.logw = logw;
exports.makeRef = makeRef;
exports.modal = modal; exports.modal = modal;
exports.navbar = navbar; exports.navbar = navbar;
exports.navigator = navigator; exports.navigator = navigator;

7
doric-js/index.d.ts vendored
View File

@ -191,6 +191,11 @@ declare module 'doric/lib/src/ui/view' {
[index: string]: Model; [index: string]: Model;
}; };
}; };
export class Ref<T extends View> {
set current(v: T);
get current(): T;
}
export function makeRef<T extends View>(): Ref<T>;
export abstract class View implements Modeling { export abstract class View implements Modeling {
width: number; width: number;
height: number; height: number;
@ -304,6 +309,8 @@ declare module 'doric/lib/src/ui/view' {
*/ */
flexConfig?: FlexConfig; flexConfig?: FlexConfig;
set props(props: Partial<this>); set props(props: Partial<this>);
set parent(v: Group);
set ref(ref: Ref<this>);
doAnimation(context: BridgeContext, animation: IAnimation): Promise<void>; doAnimation(context: BridgeContext, animation: IAnimation): Promise<void>;
clearAnimation(context: BridgeContext, animation: IAnimation): Promise<void>; clearAnimation(context: BridgeContext, animation: IAnimation): Promise<void>;
cancelAnimation(context: BridgeContext, animation: IAnimation): Promise<void>; cancelAnimation(context: BridgeContext, animation: IAnimation): Promise<void>;

View File

@ -14,6 +14,12 @@ export declare type NativeViewModel = {
[index: string]: Model; [index: string]: Model;
}; };
}; };
export declare class Ref<T extends View> {
private view?;
set current(v: T);
get current(): T;
}
export declare function makeRef<T extends View>(): Ref<T>;
export declare abstract class View implements Modeling { export declare abstract class View implements Modeling {
width: number; width: number;
height: number; height: number;
@ -132,6 +138,8 @@ export declare abstract class View implements Modeling {
*/ */
flexConfig?: FlexConfig; flexConfig?: FlexConfig;
set props(props: Partial<this>); set props(props: Partial<this>);
set parent(v: Group);
set ref(ref: Ref<this>);
doAnimation(context: BridgeContext, animation: IAnimation): Promise<void>; doAnimation(context: BridgeContext, animation: IAnimation): Promise<void>;
clearAnimation(context: BridgeContext, animation: IAnimation): Promise<void>; clearAnimation(context: BridgeContext, animation: IAnimation): Promise<void>;
cancelAnimation(context: BridgeContext, animation: IAnimation): Promise<void>; cancelAnimation(context: BridgeContext, animation: IAnimation): Promise<void>;

View File

@ -23,6 +23,20 @@ export function ViewComponent(constructor) {
const name = Reflect.getMetadata(PROP_KEY_VIEW_TYPE, constructor) || Object.getPrototypeOf(constructor).name; const name = Reflect.getMetadata(PROP_KEY_VIEW_TYPE, constructor) || Object.getPrototypeOf(constructor).name;
Reflect.defineMetadata(PROP_KEY_VIEW_TYPE, name, constructor); Reflect.defineMetadata(PROP_KEY_VIEW_TYPE, name, constructor);
} }
export class Ref {
set current(v) {
this.view = v;
}
get current() {
if (!!!this.view) {
throw new Error("Ref is empty");
}
return this.view;
}
}
export function makeRef() {
return new Ref;
}
export class View { export class View {
constructor() { constructor() {
this.width = 0; this.width = 0;
@ -203,6 +217,12 @@ export class View {
set props(props) { set props(props) {
this.apply(props); this.apply(props);
} }
set parent(v) {
this.in(v);
}
set ref(ref) {
ref.current = this;
}
doAnimation(context, animation) { doAnimation(context, animation) {
return this.nativeChannel(context, "doAnimation")(animation.toModel()).then((args) => { return this.nativeChannel(context, "doAnimation")(animation.toModel()).then((args) => {
for (let key in args) { for (let key in args) {

View File

@ -1,11 +1,11 @@
import { Group } from "../ui/view"; import { Group } from "../ui/view";
import { layoutConfig } from "./layoutconfig";
export const jsx = { export const jsx = {
createElement: function (constructor, config, ...children) { createElement: function (constructor, config, ...children) {
const e = new constructor(); const e = new constructor();
e.layoutConfig = layoutConfig().fit();
if (config) { if (config) {
for (let key in config) { e.apply(config);
Reflect.set(e, key, Reflect.get(config, key, config), e);
}
} }
if (children && children.length > 0) { if (children && children.length > 0) {
if (e instanceof Group) { if (e instanceof Group) {

View File

@ -65,6 +65,25 @@ export type NativeViewModel = {
}; };
} }
export class Ref<T extends View> {
private view?: T;
set current(v: T) {
this.view = v
}
get current() {
if (!!!this.view) {
throw new Error("Ref is empty")
}
return this.view
}
}
export function makeRef() {
return new Ref
}
export abstract class View implements Modeling { export abstract class View implements Modeling {
private __dirty_props__!: { [index: string]: Model | undefined } private __dirty_props__!: { [index: string]: Model | undefined }
@ -345,6 +364,18 @@ export abstract class View implements Modeling {
@Property @Property
flexConfig?: FlexConfig flexConfig?: FlexConfig
set props(props: Partial<this>) {
this.apply(props)
}
set parent(v: Group) {
this.in(v)
}
set ref(ref: Ref<this>) {
ref.current = this
}
doAnimation(context: BridgeContext, animation: IAnimation) { doAnimation(context: BridgeContext, animation: IAnimation) {
return this.nativeChannel(context, "doAnimation")(animation.toModel()).then((args) => { return this.nativeChannel(context, "doAnimation")(animation.toModel()).then((args) => {
for (let key in args) { for (let key in args) {

View File

@ -47,6 +47,25 @@ export type NativeViewModel = {
}; };
} }
export class Ref<T extends View> {
private view?: T;
set current(v: T) {
this.view = v
}
get current() {
if (!!!this.view) {
throw new Error("Ref is empty")
}
return this.view
}
}
export function makeRef<T extends View>(): Ref<T> {
return new Ref
}
export abstract class View implements Modeling { export abstract class View implements Modeling {
@Property @Property
width: number = 0 width: number = 0
@ -358,6 +377,14 @@ export abstract class View implements Modeling {
this.apply(props) this.apply(props)
} }
set parent(v: Group) {
this.in(v)
}
set ref(ref: Ref<this>) {
ref.current = this
}
doAnimation(context: BridgeContext, animation: IAnimation) { doAnimation(context: BridgeContext, animation: IAnimation) {
return this.nativeChannel(context, "doAnimation")(animation.toModel()).then((args) => { return this.nativeChannel(context, "doAnimation")(animation.toModel()).then((args) => {
for (let key in args) { for (let key in args) {

View File

@ -1,4 +1,5 @@
import { Group, View } from "../ui/view"; import { Group, View } from "../ui/view";
import { layoutConfig } from "./layoutconfig";
import { ClassType } from "./types"; import { ClassType } from "./types";
export const jsx = { export const jsx = {
@ -8,10 +9,9 @@ export const jsx = {
...children: View[] ...children: View[]
): T { ): T {
const e = new constructor(); const e = new constructor();
e.layoutConfig = layoutConfig().fit()
if (config) { if (config) {
for (let key in config) { e.apply(config)
Reflect.set(e, key, Reflect.get(config, key, config), e);
}
} }
if (children && children.length > 0) { if (children && children.length > 0) {
if (e instanceof Group) { if (e instanceof Group) {

View File

@ -1719,6 +1719,20 @@ function ViewComponent(constructor) {
const name = Reflect.getMetadata(PROP_KEY_VIEW_TYPE, constructor) || Object.getPrototypeOf(constructor).name; const name = Reflect.getMetadata(PROP_KEY_VIEW_TYPE, constructor) || Object.getPrototypeOf(constructor).name;
Reflect.defineMetadata(PROP_KEY_VIEW_TYPE, name, constructor); Reflect.defineMetadata(PROP_KEY_VIEW_TYPE, name, constructor);
} }
class Ref {
set current(v) {
this.view = v;
}
get current() {
if (!!!this.view) {
throw new Error("Ref is empty");
}
return this.view;
}
}
function makeRef() {
return new Ref;
}
class View { class View {
constructor() { constructor() {
this.width = 0; this.width = 0;
@ -1899,6 +1913,12 @@ class View {
set props(props) { set props(props) {
this.apply(props); this.apply(props);
} }
set parent(v) {
this.in(v);
}
set ref(ref) {
ref.current = this;
}
doAnimation(context, animation) { doAnimation(context, animation) {
return this.nativeChannel(context, "doAnimation")(animation.toModel()).then((args) => { return this.nativeChannel(context, "doAnimation")(animation.toModel()).then((args) => {
for (let key in args) { for (let key in args) {
@ -3621,10 +3641,9 @@ exports.Display = void 0;
const jsx = { const jsx = {
createElement: function (constructor, config, ...children) { createElement: function (constructor, config, ...children) {
const e = new constructor(); const e = new constructor();
e.layoutConfig = layoutConfig().fit();
if (config) { if (config) {
for (let key in config) { e.apply(config);
Reflect.set(e, key, Reflect.get(config, key, config), e);
}
} }
if (children && children.length > 0) { if (children && children.length > 0) {
if (e instanceof Group) { if (e instanceof Group) {
@ -4685,6 +4704,7 @@ exports.Panel = Panel;
exports.Property = Property; exports.Property = Property;
exports.Provider = Provider; exports.Provider = Provider;
exports.RIGHT = RIGHT; exports.RIGHT = RIGHT;
exports.Ref = Ref;
exports.Refreshable = Refreshable; exports.Refreshable = Refreshable;
exports.Root = Root; exports.Root = Root;
exports.RotationAnimation = RotationAnimation; exports.RotationAnimation = RotationAnimation;
@ -4726,6 +4746,7 @@ exports.listItem = listItem;
exports.log = log; exports.log = log;
exports.loge = loge; exports.loge = loge;
exports.logw = logw; exports.logw = logw;
exports.makeRef = makeRef;
exports.modal = modal; exports.modal = modal;
exports.navbar = navbar; exports.navbar = navbar;
exports.navigator = navigator; exports.navigator = navigator;

File diff suppressed because one or more lines are too long