From da8b53960f6cc7b3786cf4907db57371cfe5b6fb Mon Sep 17 00:00:00 2001 From: hexqi Date: Tue, 28 May 2024 17:48:50 +0800 Subject: [PATCH] feat: add app lifecycle --- designer-demo/src/App.vue | 15 ------- designer-demo/src/canvas.js | 12 ++++++ designer-demo/src/defineEntry.js | 15 ------- designer-demo/src/main.js | 13 +++--- package.json | 2 +- packages/design-core/src/init.js | 69 +++++++++++++++----------------- scripts/setup.js | 2 +- 7 files changed, 54 insertions(+), 74 deletions(-) delete mode 100644 designer-demo/src/App.vue delete mode 100644 designer-demo/src/defineEntry.js diff --git a/designer-demo/src/App.vue b/designer-demo/src/App.vue deleted file mode 100644 index a4fa20ff4..000000000 --- a/designer-demo/src/App.vue +++ /dev/null @@ -1,15 +0,0 @@ - - - - - diff --git a/designer-demo/src/canvas.js b/designer-demo/src/canvas.js index 118254a0d..b867bf53f 100644 --- a/designer-demo/src/canvas.js +++ b/designer-demo/src/canvas.js @@ -1,3 +1,15 @@ +/** + * Copyright (c) 2023 - present TinyEngine Authors. + * Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd. + * + * Use of this source code is governed by an MIT-style license. + * + * THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, + * BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR + * A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS. + * + */ + import { createRender } from '@opentiny/tiny-engine-canvas' createRender(window.parent.TinyGlobalConfig) diff --git a/designer-demo/src/defineEntry.js b/designer-demo/src/defineEntry.js deleted file mode 100644 index 2d7ea2064..000000000 --- a/designer-demo/src/defineEntry.js +++ /dev/null @@ -1,15 +0,0 @@ -/** - * Copyright (c) 2024 - present TinyEngine Authors. - * Copyright (c) 2024 - present Huawei Cloud Computing Technologies Co., Ltd. - * - * Use of this source code is governed by an MIT-style license. - * - * THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, - * BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR - * A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS. - * - */ - -import registry from '../registry.js' -import { defineEntry } from '@opentiny/tiny-engine-entry' -defineEntry(registry) diff --git a/designer-demo/src/main.js b/designer-demo/src/main.js index ac35c0ac7..b7eec3b25 100644 --- a/designer-demo/src/main.js +++ b/designer-demo/src/main.js @@ -1,6 +1,6 @@ /** - * Copyright (c) 2024 - present TinyEngine Authors. - * Copyright (c) 2024 - present Huawei Cloud Computing Technologies Co., Ltd. + * Copyright (c) 2023 - present TinyEngine Authors. + * Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd. * * Use of this source code is governed by an MIT-style license. * @@ -10,8 +10,9 @@ * */ -import './defineEntry.js' -import { createApp } from 'vue' -import App from './App.vue' +import registry from '../registry.js' +import { defineEntry } from '@opentiny/tiny-engine-entry' +import { init } from '@opentiny/tiny-engine' -createApp(App).mount('#app') +defineEntry(registry) +init() diff --git a/package.json b/package.json index 285319f45..24c3f77df 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "preinstall": "npx only-allow pnpm", "dev": "pnpm run setup && concurrently 'pnpm:serve:backend' 'pnpm:serve:frontend'", "dev:mock": "pnpm --filter @opentiny/tiny-engine dev", - "dev:demo": "concurrently 'pnpm:serve:backend' 'pnpm:dev:demo:frontend'", + "dev:demo": "pnpm run setup && concurrently 'pnpm:serve:backend' 'pnpm:dev:demo:frontend'", "serve:frontend": "pnpm --filter @opentiny/tiny-engine serve", "dev:demo:frontend": "pnpm --filter designer-demo dev", "serve:backend": "pnpm --filter @opentiny/tiny-engine-mock dev", diff --git a/packages/design-core/src/init.js b/packages/design-core/src/init.js index 9ce02a8f5..8a337e0f6 100644 --- a/packages/design-core/src/init.js +++ b/packages/design-core/src/init.js @@ -52,45 +52,42 @@ const mergeRegistry = (registry) => { return registry } -const beforeAppCreate = ({ selector, registry }) => { - // 合并用户自定义注册表 - const newRegistry = mergeRegistry(registry) - - // 在common层注入合并后的注册表 - defineEntry(newRegistry) - - initHttp({ env: import.meta.env }) - - // eslint-disable-next-line no-new - new TinyThemeTool(tinySmbTheme, 'smbtheme') // 初始化主题 - - if (import.meta.env.VITE_ERROR_MONITOR === 'true' && import.meta.env.VITE_ERROR_MONITOR_URL) { - initMonitor(import.meta.env.VITE_ERROR_MONITOR_URL) - } - - window.TinyGlobalConfig = globalConfig - setGlobalConfig(globalConfig) -} - -const appCreated = (app) => { - initSvgs(app) - window.lowcodeI18n = i18n - app.use(i18n).use(injectGlobalComponents) -} - -const beforeAppMounted = (app) => { - +const defaultLifeCycles = { + beforeAppCreate: ({ selector, registry }) => { + // 合并用户自定义注册表 + const newRegistry = mergeRegistry(registry) + + // 在common层注入合并后的注册表 + defineEntry(newRegistry) + + initHttp({ env: import.meta.env }) + + // eslint-disable-next-line no-new + new TinyThemeTool(tinySmbTheme, 'smbtheme') // 初始化主题 + + if (import.meta.env.VITE_ERROR_MONITOR === 'true' && import.meta.env.VITE_ERROR_MONITOR_URL) { + initMonitor(import.meta.env.VITE_ERROR_MONITOR_URL) + } + + window.TinyGlobalConfig = globalConfig + setGlobalConfig(globalConfig) + }, + appCreated: (app) => { + initSvgs(app) + window.lowcodeI18n = i18n + app.use(i18n).use(injectGlobalComponents) + } } -const appMounted = (app) => { +export const init = (selector = '#app', registry = defaultRegistry, lifeCycles = {}) => { + const { beforeAppCreate, appCreated, appMounted } = lifeCycles -} - -export const init = (selector = '#app', registry = defaultRegistry) => { - beforeAppCreate({ selector, registry }) + defaultLifeCycles.beforeAppCreate({ selector, registry }) + beforeAppCreate?.({ selector, registry }) const app = createApp(App) - appCreated(app) - beforeAppMounted(app) + defaultLifeCycles.appCreated(app) + appCreated?.(app) + app.mount(selector) - appMounted(app) + appMounted?.(app) } diff --git a/scripts/setup.js b/scripts/setup.js index e68188dac..e72a8d30f 100644 --- a/scripts/setup.js +++ b/scripts/setup.js @@ -1,3 +1,3 @@ const { exec } = require('child_process') -exec('pnpm -F @opentiny/tiny-engine-controller -F @opentiny/tiny-engine-dsl-vue build') +exec('pnpm -F @opentiny/vite-plugin-generate-comments -F @opentiny/tiny-engine-controller -F @opentiny/tiny-engine-dsl-vue build')