Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 钩子appendOrInsertElementHook添加原生元素参数 #220

Merged
merged 1 commit into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/guide/plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,9 @@ const plugins = [
```javascript
const plugins = [
{
// element 为插入的元素,window 为子应用的 window
appendOrInsertElementHook(element, iframeWindow) {
console.log(element, iframeWindow)
// element 为真正插入的元素,window 为子应用的 window, rawElement为原始插入元素
appendOrInsertElementHook(element, iframeWindow, rawElement) {
console.log(element, iframeWindow, rawElement)
}
},
];
Expand Down
6 changes: 3 additions & 3 deletions packages/wujie-core/src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ function rewriteAppendOrInsertChild(opts: {
manualInvokeElementEvent(element, "load");
element = null;
};
insertScriptToIframe({ ...scriptResult, onload }, sandbox.iframe.contentWindow);
insertScriptToIframe({ ...scriptResult, onload }, sandbox.iframe.contentWindow, element);
};
const scriptOptions = {
src,
Expand Down Expand Up @@ -272,9 +272,9 @@ function rewriteAppendOrInsertChild(opts: {
sandbox.execQueue.push(() =>
fiber
? requestIdleCallback(() => {
insertScriptToIframe({ src: null, content: text }, sandbox.iframe.contentWindow);
insertScriptToIframe({ src: null, content: text }, sandbox.iframe.contentWindow, element);
})
: insertScriptToIframe({ src: null, content: text }, sandbox.iframe.contentWindow)
: insertScriptToIframe({ src: null, content: text }, sandbox.iframe.contentWindow, element)
);
if (!execQueueLength) sandbox.execQueue.shift()();
}
Expand Down
14 changes: 10 additions & 4 deletions packages/wujie-core/src/iframe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -648,8 +648,13 @@ export function syncIframeUrlToWindow(iframeWindow: Window): void {
* iframe插入脚本
* @param scriptResult script请求结果
* @param iframeWindow
* @param rawElement 原始的脚本
*/
export function insertScriptToIframe(scriptResult: ScriptObject | ScriptObjectLoader, iframeWindow: Window) {
export function insertScriptToIframe(
scriptResult: ScriptObject | ScriptObjectLoader,
iframeWindow: Window,
rawElement?: HTMLScriptElement
) {
const { src, module, content, crossorigin, crossoriginType, async, callback, onload } =
scriptResult as ScriptObjectLoader;
const scriptElement = iframeWindow.document.createElement("script");
Expand Down Expand Up @@ -693,12 +698,13 @@ export function insertScriptToIframe(scriptResult: ScriptObject | ScriptObjectLo
}
container.appendChild(scriptElement);

// 外联转内联调用手动触发onload
content && onload?.();
// 调用回调
callback?.(iframeWindow);
// 执行 hooks
execHooks(plugins, "appendOrInsertElementHook", scriptElement, iframeWindow);
execHooks(plugins, "appendOrInsertElementHook", scriptElement, iframeWindow, rawElement);
// 外联转内联调用手动触发onload
content && onload?.();

// async脚本不在执行队列,无需next操作
!async && container.appendChild(nextScriptElement);
}
Expand Down