From f693719974e7c688a15e5a14fe29cef2eac16759 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=B6=9B?= Date: Sat, 4 Jul 2020 10:04:40 +0800 Subject: [PATCH] feat:add view tag and findViewByTag --- doric-demo/src/Counter.ts | 12 ++++++++---- doric-js/bundle/doric-lib.js | 21 +++++++++++++++++++++ doric-js/bundle/doric-sandbox.es5.js | 18 ++++++------------ doric-js/bundle/doric-vm.js | 21 +++++++++++++++++++++ doric-js/index.d.ts | 3 +++ doric-js/lib/src/ui/view.d.ts | 4 ++++ doric-js/lib/src/ui/view.js | 21 +++++++++++++++++++++ doric-js/src/ui/view.ts | 26 ++++++++++++++++++++++++++ doric-web/dist/index.js | 21 +++++++++++++++++++++ 9 files changed, 131 insertions(+), 16 deletions(-) diff --git a/doric-demo/src/Counter.ts b/doric-demo/src/Counter.ts index 010ca4ec..dc3633bc 100644 --- a/doric-demo/src/Counter.ts +++ b/doric-demo/src/Counter.ts @@ -9,13 +9,15 @@ class CounterView extends ViewHolder { build(root: Group) { vlayout( [ - this.number = text({ - textSize: 40, + text({ + textSize: 40, + tag:"tvNumber" }), - this.counter = text({ - text: "Click To Count", + text({ + text: "Click To Count 1", textSize: 20, + tag:"tvCounter" }), ], { @@ -24,6 +26,8 @@ class CounterView extends ViewHolder { space: 20, } ).in(root) + this.number= root.findViewByTag("tvNumber")! + this.counter= root.findViewByTag("tvCounter")! } } diff --git a/doric-js/bundle/doric-lib.js b/doric-js/bundle/doric-lib.js index 34fa5d96..5b440eb1 100644 --- a/doric-js/bundle/doric-lib.js +++ b/doric-js/bundle/doric-lib.js @@ -169,6 +169,12 @@ class View { } return f; } + findViewByTag(tag) { + if (tag === this.tag) { + return this; + } + return undefined; + } /** Anchor start*/ get left() { return this.x; @@ -405,6 +411,21 @@ class Superview extends View { } } } + findViewByTag(tag) { + if (tag === this.tag) { + return this; + } + return this.findViewTraversal(this, tag); + } + findViewTraversal(view, tag) { + for (let v of view.allSubviews()) { + let find = v.findViewByTag(tag); + if (find) { + return find; + } + } + return undefined; + } isDirty() { if (super.isDirty()) { return true; diff --git a/doric-js/bundle/doric-sandbox.es5.js b/doric-js/bundle/doric-sandbox.es5.js index ff0aabda..66ad4758 100644 --- a/doric-js/bundle/doric-sandbox.es5.js +++ b/doric-js/bundle/doric-sandbox.es5.js @@ -59,18 +59,8 @@ var doric = (function (exports) { var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; - function createCommonjsModule(fn, basedir, module) { - return module = { - path: basedir, - exports: {}, - require: function (path, base) { - return commonjsRequire(path, (base === undefined || base === null) ? module.path : base); - } - }, fn(module, module.exports), module.exports; - } - - function commonjsRequire () { - throw new Error('Dynamic requires are not currently supported by @rollup/plugin-commonjs'); + function createCommonjsModule(fn, module) { + return module = { exports: {} }, fn(module, module.exports), module.exports; } /*! ***************************************************************************** @@ -2855,6 +2845,10 @@ var doric = (function (exports) { hiddenKeys[METADATA] = true; }); + var internalMetadata_1 = internalMetadata.REQUIRED; + var internalMetadata_2 = internalMetadata.fastKey; + var internalMetadata_3 = internalMetadata.getWeakData; + var internalMetadata_4 = internalMetadata.onFreeze; var onFreeze = internalMetadata.onFreeze; diff --git a/doric-js/bundle/doric-vm.js b/doric-js/bundle/doric-vm.js index abc54808..91edeaad 100644 --- a/doric-js/bundle/doric-vm.js +++ b/doric-js/bundle/doric-vm.js @@ -1628,6 +1628,12 @@ class View { } return f; } + findViewByTag(tag) { + if (tag === this.tag) { + return this; + } + return undefined; + } /** Anchor start*/ get left() { return this.x; @@ -1864,6 +1870,21 @@ class Superview extends View { } } } + findViewByTag(tag) { + if (tag === this.tag) { + return this; + } + return this.findViewTraversal(this, tag); + } + findViewTraversal(view, tag) { + for (let v of view.allSubviews()) { + let find = v.findViewByTag(tag); + if (find) { + return find; + } + } + return undefined; + } isDirty() { if (super.isDirty()) { return true; diff --git a/doric-js/index.d.ts b/doric-js/index.d.ts index cfa0e361..9082763e 100644 --- a/doric-js/index.d.ts +++ b/doric-js/index.d.ts @@ -162,6 +162,7 @@ declare module 'doric/lib/src/ui/view' { alpha?: number; hidden?: boolean; viewId: string; + tag?: string; padding?: { left?: number; right?: number; @@ -172,6 +173,7 @@ declare module 'doric/lib/src/ui/view' { onClick?: Function; superview?: Superview; callbacks: Map; + findViewByTag(tag: string): View | undefined; constructor(); /** Anchor start*/ get left(): number; @@ -246,6 +248,7 @@ declare module 'doric/lib/src/ui/view' { } export abstract class Superview extends View { subviewById(id: string): View | undefined; + findViewByTag(tag: string): View | undefined; abstract allSubviews(): Iterable; isDirty(): boolean; clean(): void; diff --git a/doric-js/lib/src/ui/view.d.ts b/doric-js/lib/src/ui/view.d.ts index cadfbe2f..6a19eccf 100644 --- a/doric-js/lib/src/ui/view.d.ts +++ b/doric-js/lib/src/ui/view.d.ts @@ -41,6 +41,7 @@ export declare abstract class View implements Modeling { alpha?: number; hidden?: boolean; viewId: string; + tag?: string; padding?: { left?: number; right?: number; @@ -53,6 +54,7 @@ export declare abstract class View implements Modeling { callbacks: Map; private callback2Id; private id2Callback; + findViewByTag(tag: string): View | undefined; constructor(); /** Anchor start*/ get left(): number; @@ -130,6 +132,8 @@ export declare abstract class View implements Modeling { } export declare abstract class Superview extends View { subviewById(id: string): View | undefined; + findViewByTag(tag: string): View | undefined; + private findViewTraversal; abstract allSubviews(): Iterable; isDirty(): boolean; clean(): void; diff --git a/doric-js/lib/src/ui/view.js b/doric-js/lib/src/ui/view.js index 7feb7fe4..09791e25 100644 --- a/doric-js/lib/src/ui/view.js +++ b/doric-js/lib/src/ui/view.js @@ -54,6 +54,12 @@ export class View { } return f; } + findViewByTag(tag) { + if (tag === this.tag) { + return this; + } + return undefined; + } /** Anchor start*/ get left() { return this.x; @@ -290,6 +296,21 @@ export class Superview extends View { } } } + findViewByTag(tag) { + if (tag === this.tag) { + return this; + } + return this.findViewTraversal(this, tag); + } + findViewTraversal(view, tag) { + for (let v of view.allSubviews()) { + let find = v.findViewByTag(tag); + if (find) { + return find; + } + } + return undefined; + } isDirty() { if (super.isDirty()) { return true; diff --git a/doric-js/src/ui/view.ts b/doric-js/src/ui/view.ts index 8a1d50ed..8f1798e6 100644 --- a/doric-js/src/ui/view.ts +++ b/doric-js/src/ui/view.ts @@ -69,6 +69,8 @@ export abstract class View implements Modeling { viewId = uniqueId('ViewId') + tag?: string + @Property padding?: { left?: number, @@ -101,6 +103,13 @@ export abstract class View implements Modeling { return f } + findViewByTag(tag: string): View | undefined { + if (tag === this.tag) { + return this; + } + return undefined; + } + constructor() { return new Proxy(this, { get: (target, p, receiver) => { @@ -343,6 +352,23 @@ export abstract class Superview extends View { } } } + + findViewByTag(tag: string): View | undefined { + if (tag === this.tag) { + return this + } + return this.findViewTraversal(this, tag) + } + + private findViewTraversal(view: Superview, tag: string): View | undefined { + for (let v of view.allSubviews()) { + let find = v.findViewByTag(tag); + if (find) { + return find; + } + } + return undefined; + } abstract allSubviews(): Iterable isDirty() { diff --git a/doric-web/dist/index.js b/doric-web/dist/index.js index 7892897e..e394e7f2 100644 --- a/doric-web/dist/index.js +++ b/doric-web/dist/index.js @@ -1686,6 +1686,12 @@ class View { } return f; } + findViewByTag(tag) { + if (tag === this.tag) { + return this; + } + return undefined; + } /** Anchor start*/ get left() { return this.x; @@ -1922,6 +1928,21 @@ class Superview extends View { } } } + findViewByTag(tag) { + if (tag === this.tag) { + return this; + } + return this.findViewTraversal(this, tag); + } + findViewTraversal(view, tag) { + for (let v of view.allSubviews()) { + let find = v.findViewByTag(tag); + if (find) { + return find; + } + } + return undefined; + } isDirty() { if (super.isDirty()) { return true;