Skip to content

Commit

Permalink
fix(renderer-app): preview file style loading failed (#1298)
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackHole1 committed Jan 14, 2022
1 parent ad71ae8 commit 6cad8cf
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions desktop/renderer-app/src/utils/portal-window-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,23 @@ class PortalWindowManager {

portalWindow.document.title = title;

// if we don’t do this, the image resource will fail to load
const base = document.createElement("base");
base.href = PortalWindowManager.originPath();
portalWindow.document.head.appendChild(base);

// avoid being unable to use defined style
const styles = document.querySelectorAll("style");
const links = document.querySelectorAll("link");
const links = document.querySelectorAll("link[rel='stylesheet']");

styles.forEach((ele: HTMLStyleElement) => {
portalWindow.document.head.appendChild(ele.cloneNode(true));
});

links.forEach((ele: HTMLLinkElement) => {
links.forEach((ele: Element) => {
portalWindow.document.head.appendChild(ele.cloneNode(true));
});

// if we don’t do this, the image resource will fail to load
const base = document.createElement("base");
base.href = window.location.origin;
portalWindow.document.head.appendChild(base);

portalWindow.document.body.appendChild(containerElement);

/**
Expand Down Expand Up @@ -119,6 +119,25 @@ class PortalWindowManager {
}),
);
}

/**
* get current origin
* development env: http://localhost:3000/
* production env: file:///Applications/Flat.app/Contents/Resources/static/render/
*/
private static originPath(): string {
if (process.env.NODE_ENV === "development") {
return `${window.location.origin}/`;
}

/**
* /Applications/Flat.app/Contents/Resources/static/render/index.html
* to
* file:///Applications/Flat.app/Contents/Resources/static/render/
*/
const pathname = window.location.pathname;
return `file://${pathname.slice(0, pathname.lastIndexOf("/") + 1)}`;
}
}

export const portalWindowManager = new PortalWindowManager();

0 comments on commit 6cad8cf

Please sign in to comment.