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: add refresh life cycles #898

Merged
merged 6 commits into from
Nov 11, 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
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ const getDevAlias = (useSourceAlias) => {
'@opentiny/tiny-engine-theme-light': path.resolve(basePath, 'packages/theme/light/index.less'),
'@opentiny/tiny-engine-theme-base': path.resolve(basePath, 'packages/theme/base/src/index.js'),
'@opentiny/tiny-engine-svgs': path.resolve(basePath, 'packages/svgs/index.js'),
'@opentiny/tiny-engine-canvas': path.resolve(basePath, 'packages/canvas/index.js'),
'@opentiny/tiny-engine-canvas/render': path.resolve(basePath, 'packages/canvas/render/index.js'),
'@opentiny/tiny-engine-canvas': path.resolve(basePath, 'packages/canvas/index.js'),
yy-wow marked this conversation as resolved.
Show resolved Hide resolved
'@opentiny/tiny-engine-utils': path.resolve(basePath, 'packages/utils/src/index.js'),
'@opentiny/tiny-engine-webcomponent-core': path.resolve(basePath, 'packages/webcomponent/src/lib.js'),
'@opentiny/tiny-engine-i18n-host': path.resolve(basePath, 'packages/i18n/src/lib.js'),
Expand Down
5 changes: 5 additions & 0 deletions packages/canvas/common/src/constant.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
export const NODE_UID = 'data-uid'
export const NODE_TAG = 'data-tag'
export const NODE_LOOP = 'loop-id'

export const DESIGN_MODE = {
DESIGN: 'design', // 设计态
RUNTIME: 'runtime' // 运行态
}
36 changes: 25 additions & 11 deletions packages/canvas/container/src/CanvasContainer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import { onMounted, ref, computed, onUnmounted } from 'vue'
import { iframeMonitoring } from '@opentiny/tiny-engine-common/js/monitor'
import { useTranslate, useCanvas, useMaterial } from '@opentiny/tiny-engine-meta-register'
import { NODE_UID, NODE_LOOP } from '../../common'
import { NODE_UID, NODE_LOOP, DESIGN_MODE } from '../../common'
import { registerHostkeyEvent, removeHostkeyEvent } from './keyboard'
import CanvasMenu, { closeMenu, openMenu } from './components/CanvasMenu.vue'
import CanvasAction from './components/CanvasAction.vue'
Expand Down Expand Up @@ -117,6 +117,16 @@ export default {
}
}

const handleCanvasEvent = (handler) => {
const designMode = canvasApi.getDesignMode()

if (designMode !== DESIGN_MODE.DESIGN) {
return
}

return handler()
}
yy-wow marked this conversation as resolved.
Show resolved Hide resolved

const canvasReady = ({ detail }) => {
if (iframe.value) {
// 设置monitor报错埋点
Expand All @@ -131,15 +141,17 @@ export default {

// 以下是内部iframe监听的事件
win.addEventListener('mousedown', (event) => {
// html元素使用scroll和mouseup事件处理
if (event.target === doc.documentElement) {
isScrolling = false
return
}

insertPosition.value = false
setCurrentNode(event)
target.value = event.target
handleCanvasEvent(() => {
// html元素使用scroll和mouseup事件处理
if (event.target === doc.documentElement) {
isScrolling = false
return
}

insertPosition.value = false
setCurrentNode(event)
target.value = event.target
})
})

win.addEventListener('scroll', () => {
Expand Down Expand Up @@ -168,7 +180,9 @@ export default {
})

win.addEventListener('mousemove', (ev) => {
dragMove(ev, true)
handleCanvasEvent(() => {
dragMove(ev, true)
})
})

// 阻止浏览器默认的右键菜单功能
Expand Down
6 changes: 6 additions & 0 deletions packages/canvas/container/src/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ export const getCurrent = () => {
}
}

export const getDesignMode = () => getRenderer()?.getDesignMode()

export const setDesignMode = (mode) => getRenderer()?.setDesignMode(mode)

export const getGlobalState = () => getRenderer().getGlobalState()

export const getNode = (id, parent) => getRenderer()?.getNode(id, parent)
Expand Down Expand Up @@ -893,6 +897,8 @@ export const canvasApi = {
setProps,
setGlobalState,
getGlobalState,
getDesignMode,
setDesignMode,
getDocument,
canvasDispatch,
Builtin,
Expand Down
8 changes: 6 additions & 2 deletions packages/canvas/render/src/RenderMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ import {
getCondition,
getConditions,
context,
setNode
setNode,
getDesignMode,
setDesignMode
} from './context'
import CanvasEmpty from './CanvasEmpty.vue'

Expand Down Expand Up @@ -451,5 +453,7 @@ export const api = {
setGlobalState,
setNode,
getRenderer,
setRenderer
setRenderer,
getDesignMode,
setDesignMode
}
14 changes: 14 additions & 0 deletions packages/canvas/render/src/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,17 @@ export const setCondition = (id, visible = false) => {
export const getCondition = (id) => conditions[id] !== false

export const getConditions = () => conditions

export const DESIGN_MODE = {
DESIGN: 'design', // 设计态
RUNTIME: 'runtime' // 运行态
}

// 是否表现画布内特征的标志,用来控制是否允许拖拽、原生事件是否触发等
let designMode = DESIGN_MODE.DESIGN

export const getDesignMode = () => designMode

export const setDesignMode = (mode) => {
designMode = mode
}
14 changes: 9 additions & 5 deletions packages/canvas/render/src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import i18nHost from '@opentiny/tiny-engine-i18n-host'
import { CanvasRow, CanvasCol, CanvasRowColContainer } from '@opentiny/tiny-engine-builtin-component'

import { NODE_UID as DESIGN_UIDKEY, NODE_TAG as DESIGN_TAGKEY, NODE_LOOP as DESIGN_LOOPID } from '../../common'
import { context, conditions, setNode } from './context'
import { context, conditions, setNode, getDesignMode, DESIGN_MODE } from './context'
import {
CanvasBox,
CanvasCollection,
Expand Down Expand Up @@ -558,16 +558,20 @@ const getBindProps = (schema, scope) => {
const bindProps = {
...parseData(schema.props, scope),
[DESIGN_UIDKEY]: id,
[DESIGN_TAGKEY]: componentName,
onMouseover: stopEvent,
onFocus: stopEvent
[DESIGN_TAGKEY]: componentName
}

if (getDesignMode() === DESIGN_MODE.DESIGN) {
bindProps.onMouseover = stopEvent
bindProps.onFocus = stopEvent
}

if (scope) {
bindProps[DESIGN_LOOPID] = scope.index === undefined ? scope.idx : scope.index
}

// 在捕获阶段阻止事件的传播
if (clickCapture(componentName)) {
if (clickCapture(componentName) && getDesignMode() === DESIGN_MODE.DESIGN) {
bindProps.onClickCapture = stopEvent
}

Expand Down
39 changes: 34 additions & 5 deletions packages/toolbars/refresh/src/Main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,18 @@
</template>

<script>
import { useMaterial, useCanvas, useModal, useLayout, useBlock } from '@opentiny/tiny-engine-meta-register'
import {
useMaterial,
useCanvas,
useModal,
useLayout,
useBlock,
useNotify,
useMessage,
getOptions
} from '@opentiny/tiny-engine-meta-register'
import { ToolbarBase } from '@opentiny/tiny-engine-common'
import meta from '../meta'

export default {
components: {
Expand All @@ -22,12 +32,16 @@ export default {
const { isBlock, isSaved, pageState, initData } = useCanvas()
const { PLUGIN_NAME, activePlugin, isEmptyPage } = useLayout()
const { getCurrentBlock, initBlock } = useBlock()
const { beforeRefresh } = getOptions(meta.id)
const { publish } = useMessage()

const refreshResouce = () => {
const refreshResource = () => {
// 清空区块缓存(不能清空组件缓存),保证画布刷新后可以重新注册最新的区块资源
useMaterial().clearBlockResources()
// 因为webcomponents无法重复注册,所以需要刷新内部iframe
useCanvas().canvasApi.value.getDocument().location.reload()
// 通知画布更新完成
publish({ topic: 'canvas_refreshed' })
}

const refreshBlock = async () => {
Expand All @@ -36,7 +50,7 @@ export default {
const api = await activePlugin(PLUGIN_NAME.BlockManage, true)
await api.refreshBlockData(block)
await initBlock(block, {}, true)
refreshResouce()
refreshResource()
}

const refreshPage = async () => {
Expand All @@ -48,10 +62,25 @@ export default {
const api = await activePlugin(PLUGIN_NAME.AppManage, true)
const page = await api.getPageById(currentPage.id)
await initData(page['page_content'], page)
refreshResouce()
refreshResource()
}

const refresh = () => {
const refresh = async () => {
try {
if (typeof beforeRefresh === 'function') {
const stop = await beforeRefresh()

if (stop) {
return
yy-wow marked this conversation as resolved.
Show resolved Hide resolved
}
}
} catch (error) {
useNotify({
type: 'error',
message: `Error in beforeRefresh: ${error}`
})
}

if (isSaved()) {
isBlock() ? refreshBlock() : refreshPage()
} else {
Expand Down