Skip to content
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
4 changes: 2 additions & 2 deletions mockServer/src/routes/main-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ router.get('/material-center/api/blocks', async (ctx) => {
})

router.post('/material-center/api/block/create', async (ctx) => {
const result = mockService.blockService.create(ctx.request.body)
const categoriesId = ctx.request.body.categories[0]
const result = await mockService.blockService.create(ctx.request.body)
const categoriesId = ctx.request.body.categories?.[0] || ctx.request.body.groups?.[0]
const _id = result.id
await mockService.blockCategoryService.update(categoriesId, { _id })
ctx.body = getResponseData(result)
Expand Down
8 changes: 5 additions & 3 deletions mockServer/src/services/blockCategory.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ export default class BlockCategoryService {
async update(id, params) {
if (params?._id) {
const categories = await this.db.findOneAsync({ _id: id })
categories.blocks.push(params._id)
await this.db.updateAsync({ _id: id }, { $set: categories })
return getResponseData(categories)
if (categories) {
categories.blocks.push(params._id)
await this.db.updateAsync({ _id: id }, { $set: categories })
return getResponseData(categories)
}
}
params.app = appinfo.app
await this.db.updateAsync({ _id: id }, { $set: params })
Expand Down
16 changes: 8 additions & 8 deletions packages/plugins/block/src/SaveNewBlock.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<script lang="ts">
import { reactive, computed, ref } from 'vue'
import { Input, Form, FormItem, Button, DialogBox, Select } from '@opentiny/vue'
import { useBlock, useLayout, useCanvas, useModal } from '@opentiny/tiny-engine-meta-register'
import { useBlock, useLayout, useCanvas, useModal, getMetaApi, META_SERVICE } from '@opentiny/tiny-engine-meta-register'
import { REGEXP_BLOCK_NAME } from '@opentiny/tiny-engine-common/js/verification'

export default {
Expand Down Expand Up @@ -88,13 +88,13 @@ export default {
}

const handleAddBlock = () => {
if (props.fromCanvas) {
createBlock(formData)
} else {
createEmptyBlock(formData)
}
activePlugin(PLUGIN_NAME.Materials) // ?? 疑问:新建区块后,这里为啥要激活物料
cancel()
const promise = props.fromCanvas ? createBlock(formData) : createEmptyBlock(formData)

promise.then((block) => {
getMetaApi(META_SERVICE.GlobalService).updateBlockId(block.id)
activePlugin(PLUGIN_NAME.Materials)
cancel()
})
}
const addBlock = () => {
formRef.value.validate((valid) => {
Expand Down
10 changes: 7 additions & 3 deletions packages/plugins/block/src/composable/useBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ const createBlock = ({ name_cn, label, path, categories }: CreateBlockOptions) =
// 过滤只有新区块内使用到的数据
const { getLangs } = useTranslate()
const filterData = filterDataFn(parseChildProps)
const { langs, methods, state, classNameList, contentList } = extend(
const { methods, state, classNameList, contentList } = extend(
true,
{},
filterData({
Expand Down Expand Up @@ -370,7 +370,9 @@ const createBlock = ({ name_cn, label, path, categories }: CreateBlockOptions) =
}
}

initBlock(block, langs)
const api = getMetaApi(META_APP.BlockManage)
// saveBlock 内部会调用 blockSettings.tsx 中的 createBlock 接口,成功后会自动初始化区块
return api.saveBlock?.(block)
}

const createEmptyBlock = ({ name_cn, label, path, categories }: CreateEmptyBlockOptions) => {
Expand All @@ -387,7 +389,9 @@ const createEmptyBlock = ({ name_cn, label, path, categories }: CreateEmptyBlock
}
}

initBlock(block)
const api = getMetaApi(META_APP.BlockManage)
// saveBlock 内部会调用 blockSettings.tsx 中的 createBlock 接口,成功后会自动初始化区块
return api.saveBlock?.(block)
}

const setComponentLinkedValue = ({ propertyName, value }: { propertyName: string; value: any }) => {
Expand Down
16 changes: 10 additions & 6 deletions packages/plugins/block/src/js/blockSetting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -577,22 +577,24 @@ const createBlock = (block = {}) => {
}
}

requestCreateBlock(params)
return requestCreateBlock(params)
.then((data) => {
// 后台获取区块id后保存id信息
block.id = data.id
useTranslate().batchCreateI18n({ host: block.id, hostType: HOST_TYPE.Block })
useCanvas().setSaved(true)
// 新建区块成功后需要同步更新画布上的区块数据ctx上下文环境
useBlock().initBlock(data, {}, true)
message({ message: '新建区块成功!', status: 'success' })
useNotify({ message: '新建区块成功!!', type: 'success' })
// 本地生成区块服务
if (isVsCodeEnv) {
generateBlock({ schema: data.content, blockPath: data.path })
}
updateBlockList()
// 更新区块分类数据,分类下区块不为空的不能删除
getCategories()

return data
})
.catch((error) => {
message({ message: error.message, status: 'error' })
Expand Down Expand Up @@ -622,7 +624,7 @@ const updateBlock = (block = {}) => {
extraParams.categories = categories
}

requestUpdateBlock(
return requestUpdateBlock(
id,
{
[nameCn]: block[nameCn],
Expand Down Expand Up @@ -651,7 +653,7 @@ const updateBlock = (block = {}) => {
}

// 弹出保存区块成功
useModal().message({ message: '保存区块成功!', status: 'success' })
useNotify({ message: '保存区块成功!', type: 'success' })
// 本地生成区块服务
if (isVsCodeEnv) {
generateBlock({ schema: data.content, blockPath: data.path })
Expand All @@ -660,6 +662,8 @@ const updateBlock = (block = {}) => {
// 更新区块分类数据,分类下区块不为空的不能删除
getCategories()
useBlock().isRefresh.value = true

return data
})
.catch((error) => {
useModal().message({ message: error.message, status: 'error' })
Expand Down Expand Up @@ -716,14 +720,14 @@ const generateBlockDeps = (children, deps = { scripts: [], styles: new Set() })
return deps
}

export const saveBlock = async (block) => {
export const saveBlock = (block) => {
if (block && validBlockSlotsName(block)) {
const { scripts, styles } = generateBlockDeps(block.content.children)

block.content.dependencies = { scripts, styles: [...styles] }

const actionPromise = block.id ? updateBlock(block) : createBlock(block)
Comment thread
hexqi marked this conversation as resolved.
await actionPromise
return actionPromise
Comment thread
hexqi marked this conversation as resolved.
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/materials/src/composable/useResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ const initBlock = async (blockId: string) => {
const blockApi = getMetaApi(META_APP.BlockManage)
const blockContent = await blockApi.getBlockById(blockId)

if (blockContent.public_scope_tenants.length) {
if (blockContent.public_scope_tenants?.length) {
blockContent.public_scope_tenants = blockContent.public_scope_tenants.map((e: { id: string }) => e.id)
}

Expand Down