Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(canvas): add the renderer extension capability #718

Merged
merged 6 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 44 additions & 22 deletions packages/canvas/render/src/RenderMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ import * as TinyVueIcon from '@opentiny/vue-icon'
import { useBroadcastChannel } from '@vueuse/core'
import { constants, utils as commonUtils } from '@opentiny/tiny-engine-utils'
import renderer, { parseData, setConfigure, setController, globalNotify, isStateAccessor } from './render'
import { getNode as getNodeById, clearNodes, getRoot, setContext, getContext, setCondition, context } from './context'
import {
getNode as getNodeById,
clearNodes,
getRoot,
setContext,
getContext,
setCondition,
context,
setNode
} from './context'
import CanvasEmpty from './CanvasEmpty.vue'

const { BROADCAST_CHANNEL } = constants
Expand All @@ -39,15 +48,13 @@ const globalState = ref([])
const stores = shallowReactive({})
const dataSourceMap = shallowReactive({})

const Func = Function

watchEffect(() => {
reset(stores)
globalState.value.forEach(({ id, state = {}, getters = {} }) => {
const computedGetters = Object.keys(getters).reduce(
(acc, key) => ({
...acc,
[key]: new Func('return ' + getters[key].value)().call(acc, state)
[key]: parseData(getters[key], state, acc)
}),
{}
)
Expand Down Expand Up @@ -347,6 +354,34 @@ const setSchema = async (data) => {

const getNode = (id, parent) => (id ? getNodeById(id, parent) : schema)

let canvasRenderer = null

const defaultRenderer = function () {
// 渲染画布增加根节点,与出码和预览保持一致
const rootChildrenSchema = {
componentName: 'div',
props: schema.props,
children: schema.children
}

return h(
'tiny-i18n-host',
{
locale: 'zh_CN',
key: refreshKey.value,
ref: 'page',
className: 'design-page'
},
schema.children?.length ? h(renderer, { schema: rootChildrenSchema, parent: schema }) : [h(CanvasEmpty)]
)
}
hexqi marked this conversation as resolved.
Show resolved Hide resolved

const getRenderer = () => canvasRenderer || defaultRenderer
hexqi marked this conversation as resolved.
Show resolved Hide resolved

const setRenderer = (fn) => {
canvasRenderer = fn
}
hexqi marked this conversation as resolved.
Show resolved Hide resolved

export default {
setup() {
provide('rootSchema', schema)
Expand Down Expand Up @@ -378,23 +413,7 @@ export default {
)
},
render() {
// 渲染画布增加根节点,与出码和预览保持一致
const rootChildrenSchema = {
componentName: 'div',
props: schema.props,
children: schema.children
}

return h(
'tiny-i18n-host',
{
locale: 'zh_CN',
key: refreshKey.value,
ref: 'page',
className: 'design-page'
},
schema.children?.length ? h(renderer, { schema: rootChildrenSchema, parent: schema }) : [h(CanvasEmpty)]
)
return getRenderer().call(this)
}
}

Expand Down Expand Up @@ -424,5 +443,8 @@ export const api = {
getGlobalState,
getDataSourceMap,
setDataSourceMap,
setGlobalState
setGlobalState,
setNode,
getRenderer,
setRenderer
}
15 changes: 12 additions & 3 deletions packages/canvas/render/src/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
*/

import { createApp } from 'vue'
import { createApp, h } from 'vue'
import { addScript, addStyle, dynamicImportComponents, updateDependencies } from '../../common'
import TinyI18nHost, { I18nInjectionKey } from '@opentiny/tiny-engine-common/js/i18n'
import Main, { api } from './RenderMain'
Expand Down Expand Up @@ -46,7 +46,11 @@ const renderer = {
...api
}

const create = () => {
const create = async (config) => {
const { beforeAppCreate, appCreated } = config.lifeCycles || {}
if (typeof beforeAppCreate === 'function') {
await beforeAppCreate({ Vue: { h }, canvasWin: window, api })
}
App && App.unmount()
App = null

Expand All @@ -59,6 +63,11 @@ const create = () => {
dispatch('canvasReady', { detail: renderer })

App = createApp(Main).use(TinyI18nHost).provide(I18nInjectionKey, TinyI18nHost)

if (typeof appCreated === 'function') {
await appCreated(App)
}

App.config.globalProperties.lowcodeConfig = window.parent.TinyGlobalConfig
App.mount(document.querySelector('#app'))

Expand All @@ -78,5 +87,5 @@ export const createRender = (config) => {
Promise.all([
...thirdScripts.map(dynamicImportComponents),
...scripts.map((src) => addScript(src)).concat([...thirdStyles, ...styles].map((src) => addStyle(src)))
]).finally(create)
]).finally(() => create(config))
}