Skip to content

Commit 2bc2500

Browse files
committed
fix: preview page
Signed-off-by: Innei <[email protected]>
1 parent 653cac2 commit 2bc2500

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/app/(app)/preview/page.tsx

+14-3
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ const safeParse = (value: string) => {
6767
}
6868
const previewDataAtom = atom<PostModel | NoteModel | null>(null)
6969
export default function PreviewPage() {
70+
// handle preview by storage observer
7071
useIsomorphicLayoutEffect(() => {
7172
const search = location.search
7273
const searchParams = new URLSearchParams(search)
@@ -105,12 +106,20 @@ export default function PreviewPage() {
105106
}
106107
}, [])
107108

109+
// handle preview by postMessage
110+
108111
useIsomorphicLayoutEffect(() => {
109112
const search = location.search
110113
const searchParams = new URLSearchParams(search)
111114

112115
let targetOrigin = searchParams.get('origin')
113116

117+
// const isInIframe = window.self !== window.top
118+
119+
// if (isInIframe) {
120+
// return
121+
// }
122+
114123
if (!targetOrigin) {
115124
return
116125
}
@@ -121,6 +130,7 @@ export default function PreviewPage() {
121130

122131
if (!parsedData) return
123132
const PREVIEW_HASH = new URLSearchParams(location.search).get('key')
133+
124134
if (!PREVIEW_HASH) return
125135
if (parsedData.key !== PREVIEW_HASH) {
126136
return
@@ -132,16 +142,18 @@ export default function PreviewPage() {
132142
JSON.stringify(parsedData.data)
133143
)
134144
return
145+
135146
jotaiStore.set(previewDataAtom, simpleCamelcaseKeys(parsedData.data))
136147
}
137148
}
138149
window.addEventListener('message', handler)
139150

140151
console.info('preview page ready')
141-
window.opener.postMessage('ok', targetOrigin)
152+
const parentWindow = window.opener || window.parent
153+
parentWindow.postMessage('ok', targetOrigin)
142154

143155
const timer = setInterval(() => {
144-
window.opener.postMessage('ok', targetOrigin)
156+
parentWindow.postMessage('ok', targetOrigin)
145157
}, 3000)
146158
return () => {
147159
window.removeEventListener('message', handler)
@@ -216,7 +228,6 @@ const NotePreview = () => {
216228
next: undefined,
217229
data: {
218230
...data,
219-
220231
created: new Date().toISOString(),
221232
images: data.images ?? [],
222233
count: data.count ?? {

0 commit comments

Comments
 (0)