Skip to content

Commit

Permalink
refactor: extract URL parameter related logic to defaultGlobalService (
Browse files Browse the repository at this point in the history
  • Loading branch information
gene9831 authored Feb 10, 2025
1 parent b3c4771 commit bdbae55
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 53 deletions.
22 changes: 2 additions & 20 deletions packages/canvas/DesignCanvas/src/DesignCanvas.vue
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ export default {
return key
}
const postUrlChanged = () => {
usePage().postLocationHistoryChanged(
getMetaApi(META_SERVICE.GlobalService).postLocationHistoryChanged(
Object.fromEntries(
Array.from(new URLSearchParams(window.location.search)).map(([key, value]) => [replaceKey(key), value])
)
Expand All @@ -247,24 +247,6 @@ export default {
})
})
function updatePreviewId(previewId, replace = false) {
const url = new URL(window.location.href)
if (previewId) {
if (previewId === url.searchParams.get('previewid')) {
return
}
url.searchParams.set('previewid', previewId)
} else {
url.searchParams.delete('previewid')
}
if (replace) {
window.history.replaceState({}, '', url)
} else {
window.history.pushState({}, '', url)
}
usePage().postLocationHistoryChanged({ previewId })
}
return {
removeNode,
canvasSrc,
Expand All @@ -282,7 +264,7 @@ export default {
getPageAncestors: usePage().getAncestors,
getBaseInfo: () => getMetaApi(META_SERVICE.GlobalService).getBaseInfo(),
addHistoryDataChangedCallback,
updatePreviewId,
updatePreviewId: getMetaApi(META_SERVICE.GlobalService).updatePreviewId,
ast,
getBlockByName: useMaterial().getBlockByName,
useModal,
Expand Down
42 changes: 41 additions & 1 deletion packages/common/composable/defaultGlobalService.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,42 @@ const fetchAppList = (platformId) => getMetaApi(META_SERVICE.Http).get(`/app-cen

const { subscribe, publish } = useMessage()

const postLocationHistoryChanged = (data) => publish({ topic: 'locationHistoryChanged', data })

const updatePageId = (pageId) => {
const url = new URL(window.location.href)
url.searchParams.delete('blockid')
url.searchParams.set('pageid', pageId)
window.history.pushState({}, '', url)
postLocationHistoryChanged({ pageId })
}

const updateBlockId = (blockId) => {
const url = new URL(window.location.href)
url.searchParams.delete('pageid')
url.searchParams.set('blockid', blockId)
window.history.pushState({}, '', url)
postLocationHistoryChanged({ blockId })
}

const updatePreviewId = (previewId, replace = false) => {
const url = new URL(window.location.href)
if (previewId) {
if (previewId === url.searchParams.get('previewid')) {
return
}
url.searchParams.set('previewid', previewId)
} else {
url.searchParams.delete('previewid')
}
if (replace) {
window.history.replaceState({}, '', url)
} else {
window.history.pushState({}, '', url)
}
postLocationHistoryChanged({ previewId })
}

export default defineService({
id: META_SERVICE.GlobalService,
type: 'MetaService',
Expand Down Expand Up @@ -121,6 +157,10 @@ export default defineService({
},
apis: ({ state }) => ({
getBaseInfo,
isAdmin: () => state.userInfo.resetPasswordToken === 'p_webcenter'
isAdmin: () => state.userInfo.resetPasswordToken === 'p_webcenter',
postLocationHistoryChanged,
updatePageId,
updateBlockId,
updatePreviewId
})
})
16 changes: 10 additions & 6 deletions packages/plugins/block/src/Main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,15 @@ import {
} from '@opentiny/vue'
import { IconSearch } from '@opentiny/vue-icon'
import { PluginPanel, PluginBlockList, SvgButton } from '@opentiny/tiny-engine-common'
import { useBlock, useModal, useLayout, useCanvas, useHelp } from '@opentiny/tiny-engine-meta-register'
import {
useBlock,
useModal,
useLayout,
useCanvas,
useHelp,
getMetaApi,
META_SERVICE
} from '@opentiny/tiny-engine-meta-register'
import { constants } from '@opentiny/tiny-engine-utils'
import BlockSetting, { openPanel, closePanel } from './BlockSetting.vue'
import BlockGroupArrange from './BlockGroupArrange.vue'
Expand Down Expand Up @@ -303,11 +311,7 @@ export default {
useBlock().initBlock(block, {}, isEdit)
useLayout().closePlugin()
closePanel()
const url = new URL(window.location)
url.searchParams.delete('pageid')
url.searchParams.set('blockid', block.id)
window.history.pushState({}, '', url)
useBlock().postLocationHistoryChanged({ blockId: block.id })
getMetaApi(META_SERVICE.GlobalService).updateBlockId(block.id)
} else {
confirm({
message: '当前画布内容尚未保存,是否要继续切换?',
Expand Down
4 changes: 0 additions & 4 deletions packages/plugins/block/src/composable/useBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ import {
getOptions,
META_SERVICE
} from '@opentiny/tiny-engine-meta-register'
import { useMessage } from '@opentiny/tiny-engine-meta-register'
const { publish } = useMessage()
const postLocationHistoryChanged = (data) => publish({ topic: 'locationHistoryChanged', data })
import meta from '../../meta'

const { SORT_TYPE, SCHEMA_DATA_TYPE, BLOCK_OPENNESS } = constants
Expand Down Expand Up @@ -738,7 +735,6 @@ export default function () {
NODE_TYPE_PAGE,
DEFAULT_GROUP_ID,
DEFAULT_GROUP_NAME,
postLocationHistoryChanged,
selectedGroup,
selectedBlock,
selectedBlockArray,
Expand Down
9 changes: 2 additions & 7 deletions packages/plugins/materials/src/composable/useResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ import {
getMetaApi,
META_APP,
useMessage,
META_SERVICE,
usePage
META_SERVICE
} from '@opentiny/tiny-engine-meta-register'

const { COMPONENT_NAME, DEFAULT_INTERCEPTOR } = constants
Expand All @@ -43,11 +42,7 @@ function goPage(pageId) {
return
}

const url = new URL(window.location)

url.searchParams.set('pageid', pageId)
window.history.pushState({}, '', url)
usePage().postLocationHistoryChanged({ pageId })
getMetaApi(META_SERVICE.GlobalService).updatePageId(pageId)
}

const initPage = (pageInfo) => {
Expand Down
17 changes: 2 additions & 15 deletions packages/plugins/page/src/composable/usePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ import http from '../http'

const { ELEMENT_TAG, COMPONENT_NAME } = constants

import { useMessage } from '@opentiny/tiny-engine-meta-register'
const { publish } = useMessage()
const postLocationHistoryChanged = (data) => publish({ topic: 'locationHistoryChanged', data })
import { getOptions } from '@opentiny/tiny-engine-meta-register'

const DEFAULT_PAGE = {
Expand Down Expand Up @@ -319,22 +316,13 @@ const clearCurrentState = () => {
pageState.pageSchema = null
}

const updateUrlPageId = (id) => {
const url = new URL(window.location)

url.searchParams.delete('blockid')
url.searchParams.set('pageid', id)
window.history.pushState({}, '', url)
postLocationHistoryChanged({ pageId: id })
}

const switchPage = (pageId) => {
// 切换页面时清空 选中节点信息状态
clearCurrentState()

// pageId !== 0 防止 pageId 为 0 的时候判断不出来
if (pageId !== 0 && !pageId) {
updateUrlPageId('')
getMetaApi(META_SERVICE.GlobalService).updatePageId('')
useCanvas().initData({ componentName: COMPONENT_NAME.Page }, {})
useLayout().layoutState.pageStatus = {
state: 'empty',
Expand All @@ -352,7 +340,7 @@ const switchPage = (pageId) => {
useBreadcrumb().setBreadcrumbPage([data.name])
}

updateUrlPageId(pageId)
getMetaApi(META_SERVICE.GlobalService).updatePageId(pageId)
useLayout().closePlugin()
useLayout().layoutState.pageStatus = getCanvasStatus(data.occupier)
useCanvas().initData(data['page_content'], data)
Expand Down Expand Up @@ -428,7 +416,6 @@ const getFamily = async (id) => {

export default () => {
return {
postLocationHistoryChanged,
getDefaultPage,
selectedTemplateCard,
pageSettingState,
Expand Down

0 comments on commit bdbae55

Please sign in to comment.