-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
86 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* 同步路由到主窗口 | ||
* @param iframe | ||
* @param to | ||
*/ | ||
export const syncPathToParent = (iframe: HTMLIFrameElement, to: string) => { | ||
iframe?.contentWindow?.postMessage({ | ||
value: to | ||
}, '*') | ||
} | ||
|
||
const cache = { | ||
prevPath: '' | ||
} | ||
/** | ||
* 监听子路由的变化 | ||
* @param callback | ||
* @returns | ||
*/ | ||
export const onChildPathchange = (callback?: (path: string) => void): () => void => { | ||
const handleMessage = (e: { data: { value: string } }) => { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
const { value } = (typeof e.data === 'object' ? e.data || {} : {}) | ||
if (value !== undefined && value !== cache.prevPath) { | ||
callback?.(value) | ||
cache.prevPath = value | ||
} | ||
} | ||
window.addEventListener('message', handleMessage) | ||
return () => { | ||
window.removeEventListener('message', handleMessage) | ||
} | ||
} | ||
|
||
/** | ||
* 同步路由到子窗口 | ||
* @param to | ||
*/ | ||
export const syncPathToChild = (to: string) => { | ||
window.parent.postMessage({ | ||
value: to | ||
}, '*') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const path = require('path') | ||
const ts = require('rollup-plugin-typescript2') | ||
|
||
const generateConfig = (input, output, external = [], plugins = []) => { | ||
return { | ||
input, | ||
output, | ||
external, | ||
plugins: [ | ||
ts({ | ||
tsconfig: path.resolve(__dirname, './tsconfig.json') | ||
}), | ||
...plugins, | ||
], | ||
} | ||
} | ||
|
||
module.exports = [ | ||
generateConfig(path.resolve(__dirname, './lib/iframe-sync.ts'), { | ||
file: path.resolve(__dirname, './dist/iframe-sync.js'), | ||
format: 'es', | ||
name: 'websiteIframeSync' | ||
}) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters