2019-12-19 13:34:56 +08:00
|
|
|
import { jsObtainContext, jsCallEntityMethod } 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"
|
2019-12-26 17:33:22 +08:00
|
|
|
import { createContext, destroyContext } from "./DoricDriver"
|
2019-12-20 17:28:24 +08:00
|
|
|
import { DoricStackNode } from './shader/DoricStackNode'
|
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
|
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
|
|
|
|
|
|
|
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])
|
|
|
|
}
|
|
|
|
Reflect.apply(jsCallEntityMethod, this.panel, argumentsList)
|
2019-12-19 13:07:33 +08:00
|
|
|
}
|
|
|
|
|
2019-12-19 13:34:56 +08:00
|
|
|
init(frame: {
|
|
|
|
width: number,
|
|
|
|
height: number,
|
|
|
|
}, extra?: object) {
|
|
|
|
this.invokeEntityMethod("__init__", frame, extra ? JSON.stringify(extra) : undefined)
|
2019-12-19 13:07:33 +08:00
|
|
|
}
|
|
|
|
|
2019-12-26 17:33:22 +08:00
|
|
|
teardown() {
|
|
|
|
destroyContext(this.contextId)
|
|
|
|
}
|
2019-12-19 11:52:15 +08:00
|
|
|
}
|