support addon to props

This commit is contained in:
kangwei 2020-07-12 20:39:41 +08:00
parent c0ecaa6d60
commit e666853b1e

View File

@ -101,7 +101,7 @@ export class Node<Schema extends NodeSchema = NodeSchema> {
/** /**
* @deprecated * @deprecated
*/ */
private _addons: { [key: string]: any } = {}; private _addons: { [key: string]: { exportData: () => any; isProp: boolean; } } = {};
@obx.ref private _parent: ParentalNode | null = null; @obx.ref private _parent: ParentalNode | null = null;
/** /**
* *
@ -544,7 +544,11 @@ export class Node<Schema extends NodeSchema = NodeSchema> {
Object.keys(this._addons).forEach((key) => { Object.keys(this._addons).forEach((key) => {
const addon = this._addons[key]; const addon = this._addons[key];
if (addon) { if (addon) {
_extras_[key] = addon(); if (addon.isProp) {
(props as any)[key] = addon.exportData();
} else {
_extras_[key] = addon.exportData();
}
} }
}); });
@ -752,19 +756,19 @@ export class Node<Schema extends NodeSchema = NodeSchema> {
getAddonData(key: string) { getAddonData(key: string) {
const addon = this._addons[key]; const addon = this._addons[key];
if (addon) { if (addon) {
return addon(); return addon.exportData();
} }
return this.getExtraProp(key)?.value; return this.getExtraProp(key)?.value;
} }
/** /**
* @deprecated * @deprecated
*/ */
registerAddon(key: string, exportData: any) { registerAddon(key: string, exportData: () => any, isProp: boolean = false) {
if (this._addons[key]) { if (this._addons[key]) {
throw new Error(`node addon ${key} exist`); throw new Error(`node addon ${key} exist`);
} }
this._addons[key] = exportData; this._addons[key] = { exportData, isProp };
} }
getRect(): DOMRect | null { getRect(): DOMRect | null {