|
10 | 10 | * |
11 | 11 | */ |
12 | 12 |
|
13 | | -import { constants } from '@opentiny/tiny-engine-utils' |
| 13 | +import { useThrottleFn } from '@vueuse/core' |
| 14 | +import { |
| 15 | + useMaterial, |
| 16 | + useResource, |
| 17 | + useMessage, |
| 18 | + useCanvas, |
| 19 | + usePage, |
| 20 | + useBlock, |
| 21 | + getMetaApi, |
| 22 | + META_SERVICE, |
| 23 | + getMergeMeta |
| 24 | +} from '@opentiny/tiny-engine-meta-register' |
| 25 | +import { utils } from '@opentiny/tiny-engine-utils' |
14 | 26 | import { isDevelopEnv } from './environments' |
15 | | -import { useMaterial, useResource } from '@opentiny/tiny-engine-meta-register' |
16 | | -// prefer old unicode hacks for backward compatibility |
17 | 27 |
|
18 | | -const { COMPONENT_NAME } = constants |
| 28 | +const { deepClone } = utils |
19 | 29 |
|
20 | | -export const utoa = (string) => btoa(unescape(encodeURIComponent(string))) |
21 | | - |
22 | | -export const atou = (base64) => decodeURIComponent(escape(atob(base64))) |
23 | | - |
24 | | -const open = (params = {}) => { |
25 | | - const paramsMap = new URLSearchParams(location.search) |
26 | | - params.app = paramsMap.get('id') |
27 | | - params.tenant = paramsMap.get('tenant') |
| 30 | +// 保存预览窗口引用 |
| 31 | +let previewWindow = null |
28 | 32 |
|
| 33 | +const getScriptAndStyleDeps = () => { |
29 | 34 | const { scripts, styles } = useMaterial().getCanvasDeps() |
30 | 35 | const utilsDeps = useResource().getUtilsDeps() |
31 | 36 |
|
32 | | - params.scripts = [...scripts, ...utilsDeps].reduce((res, item) => { |
| 37 | + const scriptsDeps = [...scripts, ...utilsDeps].reduce((res, item) => { |
33 | 38 | res[item.package] = item.script |
34 | 39 |
|
35 | 40 | return res |
36 | 41 | }, {}) |
37 | | - params.styles = [...styles] |
| 42 | + const stylesDeps = [...styles] |
| 43 | + |
| 44 | + return { |
| 45 | + scripts: scriptsDeps, |
| 46 | + styles: stylesDeps |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +const getSchemaParams = async () => { |
| 51 | + const { isBlock, getPageSchema, getCurrentPage, getSchema } = useCanvas() |
| 52 | + const isBlockPreview = isBlock() |
| 53 | + const { scripts, styles } = getScriptAndStyleDeps() |
| 54 | + |
| 55 | + if (isBlockPreview) { |
| 56 | + const { getCurrentBlock } = useBlock() |
| 57 | + const block = getCurrentBlock() |
| 58 | + |
| 59 | + const latestPage = { |
| 60 | + ...block, |
| 61 | + page_content: getSchema() |
| 62 | + } |
| 63 | + |
| 64 | + return deepClone({ |
| 65 | + currentPage: latestPage, |
| 66 | + ancestors: [], |
| 67 | + scripts, |
| 68 | + styles |
| 69 | + }) |
| 70 | + } |
| 71 | + |
| 72 | + const pageSchema = getPageSchema() |
| 73 | + const currentPage = getCurrentPage() |
| 74 | + const { getFamily } = usePage() |
| 75 | + const latestPage = { |
| 76 | + ...currentPage, |
| 77 | + page_content: pageSchema |
| 78 | + } |
| 79 | + |
| 80 | + const ancestors = await getFamily(latestPage) |
| 81 | + |
| 82 | + return deepClone({ |
| 83 | + currentPage: latestPage, |
| 84 | + ancestors, |
| 85 | + scripts, |
| 86 | + styles |
| 87 | + }) |
| 88 | +} |
| 89 | + |
| 90 | +// 当 schema 变化时发送更新 |
| 91 | +const sendSchemaUpdate = (data) => { |
| 92 | + previewWindow.postMessage( |
| 93 | + { |
| 94 | + source: 'designer', |
| 95 | + type: 'schema', |
| 96 | + data |
| 97 | + }, |
| 98 | + '*' |
| 99 | + ) |
| 100 | +} |
| 101 | + |
| 102 | +// 监听来自预览页面的消息 |
| 103 | +const setupMessageListener = () => { |
| 104 | + window.addEventListener('message', async (event) => { |
| 105 | + // 确保消息来源安全 |
| 106 | + if (event.origin === window.location.origin || event.origin.includes(window.location.hostname)) { |
| 107 | + const { event: eventType, source } = event.data || {} |
| 108 | + // 通过 heartbeat 消息来重新建立连接,避免刷新页面后 previewWindow 为 null |
| 109 | + if (source === 'preview' && eventType === 'heartbeat' && !previewWindow) { |
| 110 | + previewWindow = event.source |
| 111 | + } |
| 112 | + |
| 113 | + if (source === 'preview' && eventType === 'onMounted' && previewWindow) { |
| 114 | + const params = await getSchemaParams() |
| 115 | + sendSchemaUpdate(params) |
| 116 | + } |
| 117 | + } |
| 118 | + }) |
| 119 | +} |
| 120 | + |
| 121 | +// 初始化消息监听 |
| 122 | +setupMessageListener() |
| 123 | + |
| 124 | +let schemaChangeListener = null |
| 125 | +const handleSchemaChange = async () => { |
| 126 | + const { unsubscribe } = useMessage() |
| 127 | + // 如果预览窗口不存在或已关闭,则取消订阅 |
| 128 | + if (!previewWindow || previewWindow.closed) { |
| 129 | + unsubscribe({ |
| 130 | + topic: 'schemaChange', |
| 131 | + subscriber: 'preview-communication' |
| 132 | + }) |
| 133 | + unsubscribe({ |
| 134 | + topic: 'schemaImport', |
| 135 | + subscriber: 'preview-communication' |
| 136 | + }) |
| 137 | + unsubscribe({ |
| 138 | + topic: 'pageOrBlockInit', |
| 139 | + subscriber: 'preview-communication' |
| 140 | + }) |
| 141 | + schemaChangeListener = null |
| 142 | + return |
| 143 | + } |
| 144 | + |
| 145 | + const params = await getSchemaParams() |
| 146 | + sendSchemaUpdate(params) |
| 147 | +} |
| 148 | + |
| 149 | +// 设置监听 schemaChange 事件,自动发送更新到预览页面 |
| 150 | +export const setupSchemaChangeListener = () => { |
| 151 | + // 如果已经存在监听,则取消之前的监听 |
| 152 | + if (schemaChangeListener) { |
| 153 | + return |
| 154 | + } |
38 | 155 |
|
| 156 | + const { subscribe } = useMessage() |
| 157 | + |
| 158 | + schemaChangeListener = subscribe({ |
| 159 | + topic: 'schemaChange', |
| 160 | + subscriber: 'preview-communication', |
| 161 | + // 防抖更新,防止因为属性变化频繁触发 |
| 162 | + callback: useThrottleFn(handleSchemaChange, 1000, true) |
| 163 | + }) |
| 164 | + |
| 165 | + subscribe({ |
| 166 | + topic: 'schemaImport', |
| 167 | + subscriber: 'preview-communication', |
| 168 | + callback: useThrottleFn(handleSchemaChange, 1000, true) |
| 169 | + }) |
| 170 | + |
| 171 | + subscribe({ |
| 172 | + topic: 'pageOrBlockInit', |
| 173 | + subscriber: 'preview-communication', |
| 174 | + callback: handleSchemaChange |
| 175 | + }) |
| 176 | +} |
| 177 | + |
| 178 | +const handleHistoryPreview = (params, url) => { |
| 179 | + let historyPreviewWindow = null |
| 180 | + const handlePreviewReady = (event) => { |
| 181 | + if (event.origin === window.location.origin || event.origin.includes(window.location.hostname)) { |
| 182 | + const { event: eventType, source } = event.data || {} |
| 183 | + if (source === 'preview' && eventType === 'onMounted' && historyPreviewWindow) { |
| 184 | + const { scripts, styles, ancestors = [], ...rest } = params |
| 185 | + |
| 186 | + historyPreviewWindow.postMessage( |
| 187 | + { |
| 188 | + source: 'designer', |
| 189 | + type: 'schema', |
| 190 | + data: deepClone({ |
| 191 | + currentPage: rest, |
| 192 | + ancestors, |
| 193 | + scripts, |
| 194 | + styles |
| 195 | + }) |
| 196 | + }, |
| 197 | + '*' |
| 198 | + ) |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + window.addEventListener('message', handlePreviewReady) |
| 204 | + |
| 205 | + historyPreviewWindow = window.open(url, '_blank') |
| 206 | +} |
| 207 | + |
| 208 | +const getQueryParams = (params = {}, isHistory = false) => { |
| 209 | + const paramsMap = new URLSearchParams(location.search) |
| 210 | + const tenant = paramsMap.get('tenant') || '' |
| 211 | + const pageId = paramsMap.get('pageid') |
| 212 | + const blockId = paramsMap.get('blockid') |
| 213 | + const theme = getMetaApi(META_SERVICE.ThemeSwitch)?.getThemeState()?.theme |
| 214 | + const framework = getMergeMeta('engine.config')?.dslMode |
| 215 | + const platform = getMergeMeta('engine.config')?.platformId |
| 216 | + const { scripts, styles } = getScriptAndStyleDeps() |
| 217 | + |
| 218 | + let query = `tenant=${tenant}&id=${paramsMap.get('id')}&theme=${theme}&framework=${framework}` |
| 219 | + |
| 220 | + query += `&platform=${platform}&scripts=${JSON.stringify(scripts)}&styles=${JSON.stringify(styles)}` |
| 221 | + |
| 222 | + if (pageId) { |
| 223 | + query += `&pageid=${pageId}` |
| 224 | + } |
| 225 | + |
| 226 | + if (blockId) { |
| 227 | + query += `&blockid=${blockId}` |
| 228 | + } |
| 229 | + |
| 230 | + if (isHistory) { |
| 231 | + query += `&history=${params.history}` |
| 232 | + } |
| 233 | + |
| 234 | + return query |
| 235 | +} |
| 236 | + |
| 237 | +const open = (params = {}, isHistory = false) => { |
39 | 238 | const href = window.location.href.split('?')[0] || './' |
40 | | - const tenant = new URLSearchParams(location.search).get('tenant') || '' |
| 239 | + const { scripts, styles } = getScriptAndStyleDeps() |
| 240 | + const query = getQueryParams(params, isHistory) |
| 241 | + |
41 | 242 | let openUrl = '' |
42 | | - const hashString = utoa(JSON.stringify(params)) |
43 | 243 |
|
44 | | - openUrl = isDevelopEnv |
45 | | - ? `./preview.html?tenant=${tenant}#${hashString}` |
46 | | - : `${href.endsWith('/') ? href : `${href}/`}preview?tenant=${tenant}#${hashString}` |
| 244 | + openUrl = isDevelopEnv ? `./preview.html?${query}` : `${href.endsWith('/') ? href : `${href}/`}preview?${query}` |
47 | 245 |
|
48 | | - const aTag = document.createElement('a') |
49 | | - aTag.href = openUrl |
50 | | - aTag.target = '_blank' |
51 | | - aTag.click() |
52 | | -} |
| 246 | + if (isHistory) { |
| 247 | + handleHistoryPreview({ ...params, scripts, styles }, openUrl) |
| 248 | + return |
| 249 | + } |
| 250 | + |
| 251 | + if (previewWindow && !previewWindow.closed) { |
| 252 | + // 如果预览窗口存在,则聚焦预览窗口 |
| 253 | + previewWindow.focus() |
| 254 | + return |
| 255 | + } |
| 256 | + |
| 257 | + // 打开新窗口并保存引用 |
| 258 | + previewWindow = window.open(openUrl, '_blank') |
53 | 259 |
|
54 | | -export const previewPage = (params = {}) => { |
55 | | - params.type = COMPONENT_NAME.Page |
56 | | - open(params) |
| 260 | + // 设置 schemaChange 事件监听 |
| 261 | + setupSchemaChangeListener() |
57 | 262 | } |
58 | 263 |
|
59 | | -export const previewBlock = (params = {}) => { |
60 | | - params.type = COMPONENT_NAME.Block |
61 | | - open(params) |
| 264 | +export const previewPage = (params = {}, isHistory = false) => { |
| 265 | + open(params, isHistory) |
62 | 266 | } |
0 commit comments