refactor: extract URL parameter related logic to defaultGlobalService (#1103)
This commit is contained in:
parent
b3c4771f2c
commit
bdbae55e51
|
|
@ -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])
|
||||
)
|
||||
|
|
@ -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,
|
||||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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',
|
||||
|
|
@ -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
|
||||
})
|
||||
})
|
||||
|
|
|
|||
|
|
@ -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'
|
||||
|
|
@ -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: '当前画布内容尚未保存,是否要继续切换?',
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -738,7 +735,6 @@ export default function () {
|
|||
NODE_TYPE_PAGE,
|
||||
DEFAULT_GROUP_ID,
|
||||
DEFAULT_GROUP_NAME,
|
||||
postLocationHistoryChanged,
|
||||
selectedGroup,
|
||||
selectedBlock,
|
||||
selectedBlockArray,
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -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) => {
|
||||
|
|
|
|||
|
|
@ -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 = {
|
||||
|
|
@ -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',
|
||||
|
|
@ -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)
|
||||
|
|
@ -428,7 +416,6 @@ const getFamily = async (id) => {
|
|||
|
||||
export default () => {
|
||||
return {
|
||||
postLocationHistoryChanged,
|
||||
getDefaultPage,
|
||||
selectedTemplateCard,
|
||||
pageSettingState,
|
||||
|
|
|
|||
Loading…
Reference in New Issue