Skip to content

Commit 35f25a3

Browse files
committed
fix: change by review
1 parent 17a9903 commit 35f25a3

File tree

4 files changed

+29
-12
lines changed

4 files changed

+29
-12
lines changed

packages/common/js/preview.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ const sendSchemaUpdate = (data) => {
9898
type: 'schema',
9999
data
100100
},
101-
'*'
101+
previewWindow.origin || window.location.origin
102102
)
103103
}
104104

@@ -166,8 +166,10 @@ export const setupSchemaChangeListener = () => {
166166
// 监听来自预览页面的消息
167167
const setupMessageListener = () => {
168168
window.addEventListener('message', async (event) => {
169+
const parsedOrigin = new URL(event.origin)
170+
const parsedHost = new URL(window.location.href)
169171
// 确保消息来源安全
170-
if (event.origin === window.location.origin || event.origin.includes(window.location.hostname)) {
172+
if (parsedOrigin.origin === parsedHost.origin || parsedOrigin.host === parsedHost.host) {
171173
const { event: eventType, source } = event.data || {}
172174
// 通过 heartbeat 消息来重新建立连接,避免刷新页面后 previewWindow 为 null
173175
if (source === 'preview' && eventType === 'connect' && !previewWindow) {
@@ -211,8 +213,11 @@ const handleHistoryPreview = (params, url) => {
211213
styles
212214
})
213215
},
214-
'*'
216+
previewWindow.origin || window.location.origin
215217
)
218+
219+
// 历史页面不需要实时更新预览,发送完消息后移除监听
220+
window.removeEventListener('message', handlePreviewReady)
216221
}
217222
}
218223
}

packages/design-core/src/preview/src/preview/usePreviewCommunication.ts

+17-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ let onSchemaReceivedAction: PreviewCommunicationOptions['onSchemaReceived'] | nu
88
let previewChannel: BroadcastChannel | null = null
99

1010
const handleMessage = async (event: MessageEvent) => {
11-
if (event.origin === window.location.origin || event.origin.includes(window.location.hostname)) {
11+
const parsedOrigin = new URL(event.origin)
12+
const parsedHost = new URL(window.location.href)
13+
14+
if (parsedOrigin.origin === parsedHost.origin || parsedOrigin.host === parsedHost.host) {
1215
const { type, data, source } = event.data || {}
1316

1417
if (source === 'designer' && type === 'schema' && data && onSchemaReceivedAction) {
@@ -21,7 +24,7 @@ const handleBroadcastMessage = async (event: MessageEvent) => {
2124
const { event: eventType, source } = event.data || {}
2225
// 初始化了,重新建立连接
2326
if (source === 'designer' && eventType === 'connect' && window.opener) {
24-
window.opener.postMessage({ event: 'connect', source: 'preview' }, '*')
27+
window.opener.postMessage({ event: 'connect', source: 'preview' }, window.opener.origin || window.location.origin)
2528
}
2629
}
2730

@@ -31,13 +34,22 @@ const sendReadyMessage = () => {
3134
// 尝试获取父窗口引用
3235
const opener = window.opener
3336

34-
if (opener) {
35-
opener.postMessage({ event: 'onMounted', source: 'preview' }, '*')
36-
} else {
37+
const fallbackHandler = () => {
3738
const logger = console
3839
logger.warn('无法获取主窗口引用,将使用 URL 参数初始化预览')
3940
loadInitialData?.()
4041
}
42+
43+
if (opener) {
44+
try {
45+
opener.postMessage({ event: 'onMounted', source: 'preview' }, opener.origin || window.location.origin)
46+
} catch (error) {
47+
fallbackHandler()
48+
}
49+
return
50+
}
51+
52+
fallbackHandler()
4153
}
4254

4355
const cleanupCommunication = () => {

packages/design-core/src/preview/src/preview/usePreviewData.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ const getPageOrBlockByApi = async (): Promise<{ currentPage: IPage | null; ances
102102
const history = searchParams.get('history')
103103

104104
if (pageId) {
105-
let ancestors = await getPageRecursively(pageId)
105+
let ancestors = (await getPageRecursively(pageId)).reverse()
106106
let currentPage = await getPageById(pageId)
107107
if (history) {
108108
const historyList: IPage[] = await fetchPageHistory(pageId)

packages/plugins/page/src/composable/usePage.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -502,12 +502,12 @@ const handlePageDetail = async (pages: any[]) => {
502502
}
503503
}
504504

505-
const getFamily = async (previewParams: { id: string }) => {
505+
const getFamily = async (currentPage: { id: string }) => {
506506
if (pageSettingState.pages.length === 0) {
507507
await getPageList()
508508
}
509509

510-
const familyPages = getAncestorsRecursively(previewParams.id)
510+
const familyPages = getAncestorsRecursively(currentPage.id)
511511
.filter((item) => item.isPage)
512512
.reverse()
513513
.map((item) => ({
@@ -526,7 +526,7 @@ const getFamily = async (previewParams: { id: string }) => {
526526

527527
await handlePageDetail(familyPages)
528528

529-
updatePageContent(familyPages, previewParams)
529+
updatePageContent(familyPages, currentPage)
530530

531531
return familyPages
532532
}

0 commit comments

Comments
 (0)