-
Notifications
You must be signed in to change notification settings - Fork 77
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
148 additions
and
2 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
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,101 @@ | ||
export class ExternalWindow { | ||
constructor( | ||
private readonly id: string, | ||
private readonly className: string, | ||
private readonly options: { | ||
url: string; | ||
top: number; | ||
left: number; | ||
width: number; | ||
height: number; | ||
} | ||
) { | ||
// | ||
} | ||
|
||
open(content: HTMLElement): void { | ||
const url = `${this.options.url}`; | ||
const externalWindow = window.open( | ||
'popout.html', | ||
this.id, | ||
`top=${this.options.top},left=${this.options.left},width=${this.options.width},height=${this.options.height}` | ||
); | ||
|
||
if (!externalWindow) { | ||
return; | ||
} | ||
|
||
const grievingParent = content.parentElement; | ||
|
||
if (!grievingParent) { | ||
return; | ||
} | ||
|
||
const cleanUp = () => { | ||
grievingParent.appendChild(content); | ||
}; | ||
|
||
// prevent any default content from loading | ||
externalWindow.document.body.replaceWith(document.createElement('div')); | ||
|
||
window.addEventListener('beforeunload', () => { | ||
cleanUp(); | ||
externalWindow.close(); | ||
}); | ||
|
||
externalWindow.addEventListener('load', () => { | ||
const externalDocument = externalWindow.document; | ||
externalDocument.title = 'test'; | ||
|
||
const div = document.createElement('div'); | ||
div.style.position = 'absolute'; | ||
div.style.width = '100%'; | ||
div.style.height = '100%'; | ||
div.style.top = '0px'; | ||
div.style.left = '0px'; | ||
div.appendChild(content); | ||
|
||
externalDocument.body.replaceChildren(div); | ||
externalDocument.body.classList.add(this.className); | ||
|
||
addStyles(externalDocument, window.document.styleSheets); | ||
|
||
externalWindow.addEventListener('beforeunload', () => { | ||
// TODO: indicate external window is closing | ||
cleanUp(); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
function addStyles(document: Document, styleSheetList: StyleSheetList) { | ||
const styleSheets = Array.from(styleSheetList); | ||
|
||
for (const styleSheet of styleSheets) { | ||
if (styleSheet.href) { | ||
const link = document.createElement('link'); | ||
link.href = styleSheet.href; | ||
link.type = styleSheet.type; | ||
link.rel = 'stylesheet'; | ||
document.head.appendChild(link); | ||
} | ||
|
||
let cssTexts: string[] = []; | ||
|
||
try { | ||
if (styleSheet.cssRules) { | ||
cssTexts = Array.from(styleSheet.cssRules).map( | ||
(rule) => rule.cssText | ||
); | ||
} | ||
} catch (er) { | ||
// | ||
} | ||
|
||
for (const rule of cssTexts) { | ||
const style = document.createElement('style'); | ||
style.appendChild(document.createTextNode(rule)); | ||
document.head.appendChild(style); | ||
} | ||
} | ||
} |
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