Skip to content

Commit

Permalink
fix: blockPublishLogic (#973)
Browse files Browse the repository at this point in the history
* fix: breadcrumb publish block cannot get correct version

* fix: block schema change sync

* fix: del useless comment code

* fix: hide publish block button when creating block
  • Loading branch information
chilingling committed Jan 3, 2025
1 parent 80359a1 commit af552d0
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 76 deletions.
65 changes: 41 additions & 24 deletions packages/common/component/BlockDeployDialog.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<tiny-dialog-box
:visible="$attrs.visible"
:visible="visible"
title="发布区块"
width="550px"
append-to-body
Expand Down Expand Up @@ -80,7 +80,7 @@ import {
Popover as TinyPopover,
FormItem as TinyFormItem
} from '@opentiny/vue'
import { useNotify, useCanvas, getMetaApi, META_APP, useBlock } from '@opentiny/tiny-engine-meta-register'
import { useNotify, getMetaApi, META_APP } from '@opentiny/tiny-engine-meta-register'
import { constants } from '@opentiny/tiny-engine-utils'
import VueMonaco from './VueMonaco.vue'
Expand All @@ -96,13 +96,17 @@ export default {
VueMonaco
},
props: {
nextVersion: {
type: String,
default: ''
block: {
type: Object,
default: () => ({})
},
visible: {
type: Boolean,
default: false
}
},
emits: ['update:visible'],
setup(props, { emit, attrs }) {
emits: ['update:visible', 'changeSchema'],
setup(props, { emit }) {
const { COMPONENT_NAME } = constants
const formState = reactive({
deployInfo: '',
Expand Down Expand Up @@ -137,23 +141,43 @@ export default {
}
}
const getNextVersion = (block) => {
const backupList = block.histories || []
let latestVersion = '1.0.0'
let latestTime = 0
backupList.forEach((v) => {
const vTime = new Date(v.created_at).getTime()
if (vTime > latestTime) {
latestTime = vTime
latestVersion = v.version
}
})
// version 符合X.Y.Z的字符结构
return latestVersion.replace(/\d+$/, (match) => Number(match) + 1)
}
watch(
() => attrs.visible,
() => props.visible,
(visible) => {
if (visible && !formState.version) {
formState.version = props.nextVersion
if (!visible) {
return
}
formState.version = getNextVersion(props.block)
}
)
const setVisible = (visible) => emit('update:visible', visible)
const deployBlock = async () => {
deployBlockRef.value.validate((valid) => {
const { getEditBlock, publishBlock } = getMetaApi(META_APP.BlockManage)
const { publishBlock } = getMetaApi(META_APP.BlockManage)
if (valid) {
const params = {
block: getEditBlock() || useBlock().getCurrentBlock(),
block: props.block,
is_compile: true,
deploy_info: formState.deployInfo,
version: formState.version,
Expand All @@ -170,21 +194,14 @@ export default {
const changeCompare = async (value) => {
const api = getMetaApi(META_APP.BlockManage)
if (value) {
const block = api.getEditBlock()
const block = props.block
const remote = await api.getBlockById(block?.id)
const originalObj = remote?.content || {}
state.originalCode = JSON.stringify(originalObj, null, 2)
const getSchema = useCanvas().getSchema
// 转为普通对象,和线上代码顺序保持一致
const pageSchema = getSchema?.() || {}
if (pageSchema.componentName === 'Block') {
state.code = JSON.stringify(pageSchema, null, 2)
} else {
// 非区块编辑
state.code = state.originalCode
}
state.code = JSON.stringify(block?.content || {}, null, 2)
state.compareVisible = true
}
}
Expand All @@ -207,8 +224,8 @@ export default {
return
}
try {
const pageSchema = JSON.parse(state.newCode)
useCanvas().importSchema({ ...pageSchema, componentName: COMPONENT_NAME.Block })
const newSchema = JSON.parse(state.newCode)
emit('changeSchema', { ...newSchema, componentName: COMPONENT_NAME.Block })
close()
} catch (err) {
useNotify({
Expand Down
49 changes: 26 additions & 23 deletions packages/plugins/block/src/BlockSetting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,17 @@
</tiny-collapse>
</template>
</plugin-setting>
<block-deploy-dialog v-model:visible="state.showDeployBlock" :nextVersion="nextVersion"></block-deploy-dialog>
<block-deploy-dialog
v-model:visible="state.showDeployBlock"
:block="publishBlock"
@changeSchema="handleChangeSchema"
></block-deploy-dialog>
</template>

<script lang="jsx">
import { reactive, ref, watch, watchEffect, computed } from 'vue'
import { Button as TinyButton, Collapse as TinyCollapse, CollapseItem as TinyCollapseItem } from '@opentiny/vue'
import { useModal } from '@opentiny/tiny-engine-meta-register'
import { getMergeMeta, useBlock } from '@opentiny/tiny-engine-meta-register'
import { useModal, getMergeMeta, useBlock } from '@opentiny/tiny-engine-meta-register'
import { BlockHistoryList, PluginSetting, CloseIcon, SvgButton, ButtonGroup } from '@opentiny/tiny-engine-common'
import { previewBlock } from '@opentiny/tiny-engine-common/js/preview'
import { LifeCycles } from '@opentiny/tiny-engine-common'
Expand Down Expand Up @@ -136,6 +139,16 @@ export default {
setup() {
const { confirm } = useModal()
const editBlock = computed(getEditBlock)
const publishBlock = computed(() => {
const currentBlock = useBlock().getCurrentBlock()
const currentEditBlock = getEditBlock()
if (currentBlock?.id === currentEditBlock?.id) {
return currentBlock
}
return currentEditBlock
})
const blockConfigForm = ref(null)
const state = reactive({
Expand All @@ -151,24 +164,6 @@ export default {
state.bindLifeCycles = getEditBlock()?.content?.lifeCycles || {}
})
// 按时间最新提交的版本的修订版本号+1, 提示输入的下一个版本
const nextVersion = computed(() => {
const backupList = state.backupList || []
let latestVersion = '1.0.0'
let latestTime = 0
backupList.forEach((v) => {
const vTime = new Date(v.created_at).getTime()
if (vTime > latestTime) {
latestTime = vTime
latestVersion = v.version
}
})
// version 符合X.Y.Z的字符结构
return latestVersion.replace(/\d+$/, (match) => Number(match) + 1)
})
watch(
() => {
const block = getEditBlock()
Expand Down Expand Up @@ -264,10 +259,16 @@ export default {
setConfigItemVisible(false)
}
const handleChangeSchema = (newSchema) => {
// 如果是当前正在画布编辑的区块,需要重新 importSchema
if (getEditBlock()?.id === useBlock().getCurrentBlock()?.id) {
useBlock().initBlock({ ...useBlock().getCurrentBlock(), content: newSchema })
}
}
return {
state,
isOpen,
nextVersion,
showDeployBlockDialog,
closePanel,
deleteBlock,
Expand All @@ -279,7 +280,9 @@ export default {
globalConfig: getMergeMeta('engine.config'),
onMouseLeave,
handleClick,
handleShowGuide
handleShowGuide,
handleChangeSchema,
publishBlock
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/block/src/composable/useBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ const initBlock = async (block = {}, _langs = {}, isEdit) => {
block.content = getSchema()

setCurrentBlock(block)
setBreadcrumbBlock([block[nameCn] || block.label], block.histories)
setBreadcrumbBlock([block[nameCn] || block.label])

// 如果是点击区块管理列表进来的则不需要执行以下操作
if (!isEdit) {
Expand Down
13 changes: 10 additions & 3 deletions packages/plugins/block/src/js/blockSetting.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -419,10 +419,17 @@ export const refreshBlockData = async (block = {}) => {
if (newBlock?.public_scope_tenants?.length) {
newBlock.public_scope_tenants = newBlock.public_scope_tenants.map((e) => e.id)
}
Object.assign(block, newBlock)
useLayout().layoutState.pageStatus = getCanvasStatus(newBlock?.occupier)

useHistory().addHistory(block.content)
// 与当前正在画布编辑态的区块相同,需要同步更新
if (useBlock().getCurrentBlock()?.id === block.id) {
useLayout().layoutState.pageStatus = getCanvasStatus(newBlock?.occupier)
useHistory().addHistory(block.content)
Object.assign(useBlock().getCurrentBlock(), newBlock)
}
// 与当前区块管理面板的区块相同,需要同步更新
if (getEditBlock()?.id === block.id) {
Object.assign(getEditBlock(), newBlock)
}
}
}
}
Expand Down
39 changes: 16 additions & 23 deletions packages/toolbars/breadcrumb/src/Main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,26 @@

<tiny-button
class="publish"
v-if="breadcrumbData[0] === CONSTANTS.BLOCKTEXT"
v-if="breadcrumbData[0] === CONSTANTS.BLOCKTEXT && currentBlock?.id"
@click="publishBlock()"
type="primary"
size="small"
>发布区块
</tiny-button>
</div>
<block-deploy-dialog v-model:visible="state.showDeployBlock" :nextVersion="nextVersion"></block-deploy-dialog>
<block-deploy-dialog
v-model:visible="state.showDeployBlock"
:block="currentBlock"
@changeSchema="handleChangeSchema"
></block-deploy-dialog>
</template>
</toolbar-base>
</template>

<script>
import { reactive, computed } from 'vue'
import { Breadcrumb, BreadcrumbItem, Button } from '@opentiny/vue'
import { useBreadcrumb, useLayout } from '@opentiny/tiny-engine-meta-register'
import { useBreadcrumb, useLayout, useBlock } from '@opentiny/tiny-engine-meta-register'
import { ToolbarBase } from '@opentiny/tiny-engine-common'
import { BlockDeployDialog } from '@opentiny/tiny-engine-common'
Expand Down Expand Up @@ -68,36 +72,25 @@ export default {
state.showDeployBlock = true
}
const nextVersion = computed(() => {
const backupList = getBreadcrumbData().value[2] || []
let latestVersion = '1.0.0'
let latestTime = 0
backupList.forEach((v) => {
const vTime = new Date(v.created_at).getTime()
if (vTime > latestTime) {
latestTime = vTime
latestVersion = v.version
}
})
// version 符合X.Y.Z的字符结构
return latestVersion.replace(/\d+$/, (match) => Number(match) + 1)
})
const open = () => {
if (!plugins) return
plugins.render = breadcrumbData.value[0] === CONSTANTS.PAGETEXT ? PLUGINS_ID.PAGEID : PLUGINS_ID.BLOCKID
}
const currentBlock = computed(useBlock().getCurrentBlock)
const handleChangeSchema = (newSchema) => {
useBlock().initBlock({ ...useBlock().getCurrentBlock(), content: newSchema })
}
return {
breadcrumbData,
publishBlock,
state,
nextVersion,
CONSTANTS,
open
open,
currentBlock,
handleChangeSchema
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/toolbars/breadcrumb/src/composable/useBreadcrumb.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ const setBreadcrumbPage = (value) => {
sessionStorage.setItem('pageInfo', value)
}

const setBreadcrumbBlock = (value, histories = []) => {
breadcrumbData.value = [CONSTANTS.BLOCKTEXT, ...value, histories]
const setBreadcrumbBlock = (value) => {
breadcrumbData.value = [CONSTANTS.BLOCKTEXT, ...value]
}

const getBreadcrumbData = () => breadcrumbData
Expand Down

0 comments on commit af552d0

Please sign in to comment.