diff --git a/packages/core/src/App.ts b/packages/core/src/App.ts index 99a4394f..8b76ad37 100644 --- a/packages/core/src/App.ts +++ b/packages/core/src/App.ts @@ -49,6 +49,12 @@ export interface AppOptionsConfig { class App extends EventEmitter { [x: string]: any; + static nodeClassMap = new Map(); + + public static registerNode(type: string, NodeClass: T) { + App.nodeClassMap.set(type, NodeClass); + } + public env: Env = new Env(); public dsl?: MApp; public codeDsl?: CodeBlockDSL; diff --git a/packages/core/src/Env.ts b/packages/core/src/Env.ts index 2b2dea08..bbacc7a1 100644 --- a/packages/core/src/Env.ts +++ b/packages/core/src/Env.ts @@ -30,7 +30,11 @@ class Env { isWeb = false; isOpenHarmony = false; - constructor(ua = globalThis.navigator.userAgent, options: Record = {}) { + constructor(ua = globalThis.navigator?.userAgent ?? '', options: Record = {}) { + if (!ua) { + return; + } + this.isIphone = ua.indexOf('iPhone') >= 0; this.isIpad = /(iPad).*OS\s([\d_]+)/.test(ua); diff --git a/packages/core/src/Page.ts b/packages/core/src/Page.ts index ab657c7c..51312ad9 100644 --- a/packages/core/src/Page.ts +++ b/packages/core/src/Page.ts @@ -18,7 +18,7 @@ import type { Id, MComponent, MContainer, MPage, MPageFragment } from '@tmagic/schema'; -import type App from './App'; +import App from './App'; import IteratorContainer from './IteratorContainer'; import type { default as TMagicNode } from './Node'; import Node from './Node'; @@ -53,7 +53,7 @@ class Page extends Node { return; } - const node = new Node({ + const node = new ((config.type && App.nodeClassMap.get(config.type)) || Node)({ config, parent, page: this,