Skip to content

Commit

Permalink
Merge pull request #46 from ice-lab/release/1.2.1
Browse files Browse the repository at this point in the history
Release/1.2.1
  • Loading branch information
imsobear authored Dec 13, 2019
2 parents 667cb06 + cb3ee35 commit 965e0c2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ice/stark",
"version": "1.2.0",
"version": "1.2.1",
"description": "Icestark is a JavaScript library for multiple projects, Ice workbench solution.",
"scripts": {
"build": "rm -rf lib && tsc",
Expand Down
29 changes: 14 additions & 15 deletions src/AppRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ export interface AppRouteProps extends AppConfig {
onAppLeave?: (appConfig: AppConfig) => void;
triggerLoading?: (loading: boolean) => void;
triggerError?: (err: string) => void;
shouldAssetsRemove?: (assetUrl?: string) => boolean;
shouldAssetsRemove?: (
assetUrl?: string,
element?: HTMLElement | HTMLLinkElement | HTMLStyleElement | HTMLScriptElement,
) => boolean;
}

export function converArray2String(list: string | string[]) {
Expand Down Expand Up @@ -155,10 +158,9 @@ export default class AppRoute extends React.Component<AppRouteProps, AppRouteSta

this.triggerPrevAppLeave();

// reCreate rootElement to remove React Component instance,
// reCreate rootElement to remove Child App instance,
// rootElement is created for render Child App
this.removeElementFromBase(rootId);
let rootElement: any = this.appendElementToBase(rootId);
let rootElement: any = this.reCreateElementInBase(rootId);

// prevent duplicate creation of shadowRoot
if (useShadow && !rootElement.shadowRoot) {
Expand Down Expand Up @@ -201,6 +203,9 @@ export default class AppRoute extends React.Component<AppRouteProps, AppRouteSta
};

const handleError = (errMessage: string): void => {
// if AppRoute is unmountd, cancel all operations
if (this.unmounted) return;

handleLoading(false);
typeof triggerError === 'function' && triggerError(errMessage);
};
Expand Down Expand Up @@ -232,26 +237,20 @@ export default class AppRoute extends React.Component<AppRouteProps, AppRouteSta
}
};

appendElementToBase = (elementId: string): HTMLElement => {
reCreateElementInBase = (elementId: string): HTMLElement => {
const myBase = this.myRefBase;
if (!myBase) return;

// remove all elements in base
myBase.innerHTML = '';

// create new rootElement
const element = document.createElement('div');
element.id = elementId;
myBase.appendChild(element);
return element;
};

removeElementFromBase = (elementId: string): void => {
const myBase = this.myRefBase;
if (!myBase) return;

const element = myBase.querySelector(`#${elementId}`);
if (element) {
myBase.removeChild(element);
}
};

triggerPrevAppLeave = (): void => {
const { onAppLeave } = this.props;

Expand Down
7 changes: 6 additions & 1 deletion src/AppRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ export interface AppRouterProps {
NotFoundComponent?: any;
onAppEnter?: (appConfig: AppConfig) => void;
onAppLeave?: (appConfig: AppConfig) => void;
shouldAssetsRemove?: (assetUrl?: string) => boolean;
shouldAssetsRemove?: (
assetUrl?: string,
element?: HTMLElement | HTMLLinkElement | HTMLStyleElement | HTMLScriptElement,
) => boolean;
}

interface AppRouterState {
Expand Down Expand Up @@ -125,6 +128,8 @@ export default class AppRouter extends React.Component<AppRouterProps, AppRouter
triggerLoading = (newShowLoading: boolean): void => {
// if AppRouter is unmountd, cancel all operations
if (this.unmounted) return;
// if no LoadingComponent, showLoading will never be true
if (newShowLoading && !this.props.LoadingComponent) return;

const { showLoading } = this.state;
if (showLoading !== newShowLoading) {
Expand Down
17 changes: 13 additions & 4 deletions src/util/handleAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,18 +347,27 @@ export function setStaticAttribute(tag: HTMLStyleElement | HTMLScriptElement): v
/**
* Empty useless assets
*/
export function emptyAssets(shouldRemove: (assetUrl: string) => boolean): void {
export function emptyAssets(
shouldRemove: (
assetUrl: string,
element?: HTMLElement | HTMLLinkElement | HTMLStyleElement | HTMLScriptElement,
) => boolean,
): void {
// remove extra assets
const styleList: NodeListOf<HTMLElement> = document.querySelectorAll(
`style:not([${PREFIX}=${STATIC}])`,
);
styleList.forEach(style => style.parentNode.removeChild(style));
styleList.forEach(style => {
if (shouldRemove(null, style)) {
style.parentNode.removeChild(style);
}
});

const linkList: NodeListOf<HTMLElement> = document.querySelectorAll(
`link:not([${PREFIX}=${STATIC}])`,
);
linkList.forEach(link => {
if (shouldRemove(link.getAttribute('href'))) {
if (shouldRemove(link.getAttribute('href'), link)) {
link.parentNode.removeChild(link);
}
});
Expand All @@ -367,7 +376,7 @@ export function emptyAssets(shouldRemove: (assetUrl: string) => boolean): void {
`script:not([${PREFIX}=${STATIC}])`,
);
jsExtraList.forEach(js => {
if (shouldRemove(js.getAttribute('src'))) {
if (shouldRemove(js.getAttribute('src'), js)) {
js.parentNode.removeChild(js);
}
});
Expand Down

0 comments on commit 965e0c2

Please sign in to comment.