Skip to content

Commit

Permalink
refactor: add canvas renderer state
Browse files Browse the repository at this point in the history
  • Loading branch information
hexqi committed Feb 27, 2024
1 parent e390ea7 commit 4838c89
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export default {
iframeMonitoring()
initCanvas({ emit, renderer: detail, iframe: iframe.value, controller: props.controller })
useCanvas().renderer = detail
const doc = iframe.value.contentDocument
const win = iframe.value.contentWindow
Expand Down
12 changes: 11 additions & 1 deletion packages/canvas/src/components/render/RenderMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { generateFunction } from '@opentiny/tiny-engine-controller/utils'
import renderer, { parseData, setConfigure, setController, globalNotify, isStateAccessor } from './render'
import { getNode as getNodeById, clearNodes, getRoot, setContext, getContext, setCondition, context } from './context'
import CanvasEmpty from './CanvasEmpty.vue'
import { getCurrent, setLocales, updateRect, addStyle, addScript, canvasDispatch } from '../container/container'
import Builtin from '../builtin/builtin.json'

const { BROADCAST_CHANNEL } = constants

Expand Down Expand Up @@ -422,7 +424,15 @@ export const api = {
getGlobalState,
getDataSourceMap,
setDataSourceMap,
setGlobalState
setGlobalState,
getCurrent,
setLocales,
getNodeById: getNode,
updateRect,
addStyle,
addScript,
canvasDispatch
}

window.api = api
window.Builtin = Builtin
1 change: 0 additions & 1 deletion packages/controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
"@babel/parser": "7.18.13",
"@babel/traverse": "7.18.13",
"@opentiny/tiny-engine-builtin-component": "workspace:*",
"@opentiny/tiny-engine-canvas": "workspace:*",
"@opentiny/tiny-engine-http": "workspace:*",
"@opentiny/tiny-engine-utils": "workspace:*",
"@opentiny/vue": "~3.10.0",
Expand Down
5 changes: 2 additions & 3 deletions packages/controller/src/useBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import useEditorInfo from './useEditorInfo'
import useBreadcrumb from './useBreadcrumb'
import useLayout from './useLayout'
import { getGlobalConfig } from './globalConfig'
import { getSchema, getCurrent } from '@opentiny/tiny-engine-canvas'

const { SORT_TYPE, SCHEMA_DATA_TYPE, BLOCK_OPENNESS } = constants

Expand Down Expand Up @@ -279,7 +278,7 @@ const initBlock = async (block = {}, _langs = {}, isEdit) => {
// 把区块的schema传递给画布
await resetBlockCanvasState({ pageSchema: getBlockPageSchema(block) })
// 这一步操作很重要,让区块管理面板和画布共同维护同一份区块schema
block.content = getSchema()
block.content = useCanvas().renderer.value?.getSchema()

setCurrentBlock(block)
setBreadcrumbBlock([block[nameCn] || block.label], block.histories)
Expand Down Expand Up @@ -362,7 +361,7 @@ const createEmptyBlock = ({ name_cn, label, path, categories }) => {
}

const setComponentLinkedValue = ({ propertyName, value }) => {
const { schema } = getCurrent()
const { schema } = useCanvas().renderer.value?.getCurrent()

Check failure on line 364 in packages/controller/src/useBlock.js

View workflow job for this annotation

GitHub Actions / push-check

Unsafe usage of optional chaining. If it short-circuits with 'undefined' the evaluation will throw TypeError

if (!propertyName || !schema) {
return
Expand Down
12 changes: 7 additions & 5 deletions packages/controller/src/useCanvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
*/

/* eslint-disable no-new-func */
import { reactive } from 'vue'
import { setSchema, renderApi } from '@opentiny/tiny-engine-canvas'
import { reactive, ref } from 'vue'
import { constants } from '@opentiny/tiny-engine-utils'
import useHistory from './useHistory'

Expand Down Expand Up @@ -52,12 +51,14 @@ const defaultSchema = {
outputs: []
}

const renderer = ref(null)

const pageState = reactive({ ...defaultPageState, loading: true })
// 重置画布数据
const resetCanvasState = async (state = {}) => {
Object.assign(pageState, defaultPageState, state)

await setSchema(pageState.pageSchema)
await renderer.value?.setSchema(pageState.pageSchema)
}

// 页面重置画布数据
Expand Down Expand Up @@ -141,6 +142,7 @@ const getCurrentPage = () => pageState.currentPage
export default function () {
return {
pageState,
renderer,
isBlock,
isSaved,
isLoading,
Expand All @@ -151,8 +153,8 @@ export default function () {
resetPageCanvasState,
resetBlockCanvasState,
clearCurrentState,
getDataSourceMap: renderApi.getDataSourceMap,
setDataSourceMap: renderApi.setDataSourceMap,
getDataSourceMap: renderer.value?.getDataSourceMap,
setDataSourceMap: renderer.value?.setDataSourceMap,
getCurrentSchema,
setCurrentSchema,
getCurrentPage
Expand Down
5 changes: 2 additions & 3 deletions packages/controller/src/useHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import { reactive, isProxy, toRaw, watch } from 'vue'
import useCanvas from './useCanvas'
import { setSchema, getSchema } from '@opentiny/tiny-engine-canvas'

const schema2String = (schema) => {
if (isProxy(schema)) {
Expand Down Expand Up @@ -62,7 +61,7 @@ const push = (schema) => {

const go = (addend, valid) => {
historyState.index = historyState.index + addend
setSchema(string2Schema(list[historyState.index]))
useCanvas().renderer.value?.setSchema(string2Schema(list[historyState.index]))

// 不是锁定状态,撤销操作后,传递第二个标识位,将 list 的长度减一,置灰 undoredo 操作按钮
if (typeof valid === 'boolean') {
Expand All @@ -87,7 +86,7 @@ const forward = () => {
const addHistory = (schema) => {
if (!schema) {
useCanvas().setSaved(false)
push(getSchema())
push(useCanvas().renderer.value?.getSchema())
} else {
clear()

Check failure on line 91 in packages/controller/src/useHistory.js

View workflow job for this annotation

GitHub Actions / push-check

'clear' was used before it was defined
// 初始 schema 需要设置为第一条历史记录
Expand Down
2 changes: 1 addition & 1 deletion packages/controller/src/useProperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/

import { toRaw, nextTick, shallowReactive, ref } from 'vue'
import { getNode, setState, updateRect } from '@opentiny/tiny-engine-canvas'
import { constants } from '@opentiny/tiny-engine-utils'
import useCanvas from './useCanvas'
import useResource from './useResource'
Expand Down Expand Up @@ -192,6 +191,7 @@ const setProp = (name, value, type) => {
}

// 没有父级,或者不在节点上面,要更新内容。就用setState
const { getNode, setState, updateRect } = useCanvas().renderer.value || {}
getNode(properties.schema.id, true)?.parent || setState(useCanvas().getPageSchema().state)
propsUpdateKey.value++

Expand Down
4 changes: 2 additions & 2 deletions packages/controller/src/useResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { reactive } from 'vue'
import { getGlobalConfig } from './globalConfig'
import { useHttp } from '@opentiny/tiny-engine-http'
import { utils, constants } from '@opentiny/tiny-engine-utils'
import { Builtin, addScript, addStyle, canvasDispatch } from '@opentiny/tiny-engine-canvas'
import { getCanvasStatus } from '@opentiny/tiny-engine-controller/js/index'
import { meta as BuiltinComponentMaterials } from '@opentiny/tiny-engine-builtin-component'
import useApp from './useApp'
Expand Down Expand Up @@ -138,6 +137,7 @@ const registerBlock = async (data, notFetchResouce) => {
return block
} else {
if (!blockResource.get(label)) {
const { addScript, addStyle } = useCanvas().renderer.value
const promises = scripts
.filter((item) => item.includes('umd.js'))
.map(addScript)
Expand Down Expand Up @@ -451,7 +451,7 @@ const updateCanvasDependencies = (blocks) => {
getBlockDeps(block.content.dependencies)
})

canvasDispatch('updateDependencies', { detail: resState.thirdPartyDeps })
useCanvas().renderer.value?.canvasDispatch('updateDependencies', { detail: resState.thirdPartyDeps })
}

export default function () {
Expand Down
9 changes: 4 additions & 5 deletions packages/controller/src/useTranslate.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import { reactive, ref } from 'vue'
import { useHttp } from '@opentiny/tiny-engine-http'
import { utils } from '@opentiny/tiny-engine-utils'
import { setLocales } from '@opentiny/tiny-engine-canvas'
import { isVsCodeEnv } from '@opentiny/tiny-engine-controller/js/environments'
import { constants } from '@opentiny/tiny-engine-utils'
import { generateI18n } from '@opentiny/tiny-engine-controller/js/vscodeGenerateFile'
Expand Down Expand Up @@ -101,7 +100,7 @@ const ensureI18n = (obj, send) => {
}
})

setLocales(messages, true)
useCanvas().renderer.value?.setLocales(messages, true)

Check failure on line 103 in packages/controller/src/useTranslate.js

View workflow job for this annotation

GitHub Actions / push-check

'useCanvas' is not defined
} catch (e) {
// 不需要处理,有报错的词条会在画布初始化的时候统一调setLocales这个方法
}
Expand Down Expand Up @@ -171,7 +170,7 @@ const initAppI18n = async (appId) => {
host: appId,
hostType: HOST_TYPE.App
})
setLocales(i18nResource.messages)
useCanvas().renderer.value?.setLocales(i18nResource.messages)

Check failure on line 173 in packages/controller/src/useTranslate.js

View workflow job for this annotation

GitHub Actions / push-check

'useCanvas' is not defined
}
}

Expand All @@ -181,7 +180,7 @@ const initBlockI18n = async (blockId) => {
host: blockId,
hostType: HOST_TYPE.Block
})
setLocales(i18nResource.messages)
useCanvas().renderer.value?.setLocales(i18nResource.messages)

Check failure on line 183 in packages/controller/src/useTranslate.js

View workflow job for this annotation

GitHub Actions / push-check

'useCanvas' is not defined
}
}

Expand All @@ -192,7 +191,7 @@ const initBlockLocalI18n = async (langs = {}) => {
hostType: HOST_TYPE.Block,
local: true
})
setLocales(i18nResource.messages)
useCanvas().renderer.value?.setLocales(i18nResource.messages)

Check failure on line 194 in packages/controller/src/useTranslate.js

View workflow job for this annotation

GitHub Actions / push-check

'useCanvas' is not defined
}

const format = (str = '', params = {}) => str.replace(/\$\{(.+?)\}/g, (substr, key) => params[key] || '')
Expand Down
1 change: 1 addition & 0 deletions packages/settings/design/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"homepage": "https://opentiny.design/tiny-engine",
"dependencies": {
"@opentiny/tiny-engine-common": "workspace:*",
"@opentiny/tiny-engine-controller": "workspace:*",
"@opentiny/vue": "~3.10.0",
"sortablejs": "^1.14.0"
},
Expand Down

0 comments on commit 4838c89

Please sign in to comment.