This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Doric/doric-web/src/DoricContext.ts

111 lines
3.0 KiB
TypeScript
Raw Normal View History

import { jsObtainContext, jsCallEntityMethod, pureCallEntityMethod } from 'doric/src/runtime/sandbox'
2019-12-19 13:07:33 +08:00
import { Panel } from 'doric'
2019-12-19 11:52:15 +08:00
import { DoricPlugin } from "./DoricPlugin"
2021-09-16 12:52:02 +08:00
import { createContext, destroyContext, markNeedHook } from "./DoricDriver"
2019-12-20 17:28:24 +08:00
import { DoricStackNode } from './shader/DoricStackNode'
2019-12-26 19:01:28 +08:00
import { DoricViewNode } from './shader/DoricViewNode'
2019-12-19 11:52:15 +08:00
const doricContexts: Map<string, DoricContext> = new Map
2019-12-19 13:07:33 +08:00
let __contextId__ = 0
function getContextId() {
return `context_${__contextId__++}`
}
2019-12-19 11:52:15 +08:00
export function getDoricContext(contextId: string) {
return doricContexts.get(contextId)
}
export class DoricContext {
2019-12-19 13:07:33 +08:00
contextId = getContextId()
2019-12-19 11:52:15 +08:00
pluginInstances: Map<string, DoricPlugin> = new Map
2019-12-20 17:28:24 +08:00
rootNode: DoricStackNode
2020-01-09 19:31:50 +08:00
headNodes: Map<string, Map<string, DoricViewNode>> = new Map
2021-04-20 11:59:59 +08:00
animationSet?: {
viewNode: DoricViewNode,
keyFrame: Partial<CSSStyleDeclaration>,
}[]
2019-12-26 19:01:28 +08:00
2019-12-19 13:07:33 +08:00
constructor(content: string) {
createContext(this.contextId, content)
doricContexts.set(this.contextId, this)
2019-12-20 17:28:24 +08:00
this.rootNode = new DoricStackNode(this)
2019-12-19 11:52:15 +08:00
}
2019-12-19 13:07:33 +08:00
2021-04-15 15:51:24 +08:00
targetViewNode(viewId: string) {
if (this.rootNode.viewId === viewId) {
return this.rootNode
}
for (let nodes of this.headNodes.values()) {
if (nodes.has(viewId)) {
return nodes.get(viewId)
}
}
}
2019-12-19 13:07:33 +08:00
get panel() {
2019-12-19 13:34:56 +08:00
return jsObtainContext(this.contextId)?.entity as Panel
}
invokeEntityMethod(method: string, ...otherArgs: any) {
const argumentsList: any = [this.contextId]
for (let i = 0; i < arguments.length; i++) {
argumentsList.push(arguments[i])
}
2021-09-16 12:52:02 +08:00
const ret = Reflect.apply(jsCallEntityMethod, this.panel, argumentsList)
markNeedHook()
return ret
2019-12-19 13:07:33 +08:00
}
pureInvokeEntityMethod(method: string, ...otherArgs: any) {
const argumentsList: any = [this.contextId]
for (let i = 0; i < arguments.length; i++) {
argumentsList.push(arguments[i])
}
return Reflect.apply(pureCallEntityMethod, this.panel, argumentsList)
}
2021-04-13 16:22:15 +08:00
init(data?: string) {
this.invokeEntityMethod("__init__", data)
2020-03-30 15:18:35 +08:00
}
2021-04-13 16:22:15 +08:00
onCreate() {
this.invokeEntityMethod("__onCreate__")
}
onDestroy() {
this.invokeEntityMethod("__onDestroy__")
}
onShow() {
this.invokeEntityMethod("__onShow__")
}
onHidden() {
this.invokeEntityMethod("__onHidden__")
}
2020-03-30 15:18:35 +08:00
build(frame: {
2019-12-19 13:34:56 +08:00
width: number,
height: number,
2020-03-30 15:18:35 +08:00
}) {
this.invokeEntityMethod("__build__", frame)
2019-12-19 13:07:33 +08:00
}
2021-04-16 17:19:02 +08:00
inAnimation() {
return !!this.animationSet
}
addAnimation(viewNode: DoricViewNode, keyFrame: Partial<CSSStyleDeclaration>) {
this.animationSet?.push({
viewNode,
2021-04-20 11:59:59 +08:00
keyFrame,
2021-04-16 17:19:02 +08:00
})
}
2019-12-26 17:33:22 +08:00
teardown() {
2019-12-26 19:01:28 +08:00
for (let plugin of this.pluginInstances.values()) {
plugin.onTearDown()
}
2019-12-26 17:33:22 +08:00
destroyContext(this.contextId)
}
2019-12-19 11:52:15 +08:00
}