generated from diplodoc-platform/package-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add inheritance to the IFrameController
- Loading branch information
Showing
5 changed files
with
95 additions
and
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import {Disposable, updateClassNames, updateStyles} from '../utils'; | ||
|
||
export abstract class BaseIFrameController extends Disposable { | ||
protected readonly iframe: HTMLIFrameElement | Window; | ||
protected readonly domContainer: HTMLElement; | ||
private classNames: string[] = []; | ||
private styles: Record<string, string> = {}; | ||
|
||
constructor(iframe: HTMLIFrameElement | Window) { | ||
super(); | ||
|
||
this.iframe = iframe; | ||
this.domContainer = | ||
'contentWindow' in iframe | ||
? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
iframe.contentWindow!.document.body | ||
: iframe.document.body; | ||
} | ||
|
||
setClassNames = (classNames: string[] | undefined = []) => { | ||
updateClassNames(this.domContainer, classNames, this.classNames); | ||
|
||
// update this._classNames to the new classNames | ||
this.classNames = classNames; | ||
}; | ||
|
||
setStyles = (styles: Record<string, string> | undefined = {}) => { | ||
updateStyles(this.domContainer, styles, this.styles); | ||
|
||
// update this._styles to the new styles | ||
this.styles = styles; | ||
}; | ||
|
||
replaceHTML = (htmlString: string) => { | ||
const fragment = globalThis.document.createRange().createContextualFragment(htmlString); | ||
|
||
this.domContainer.replaceChildren(fragment); | ||
|
||
// TODO: should we reinitialize Observer? | ||
}; | ||
|
||
setBaseTarget = (value: string) => { | ||
const baseElement = this.getBaseElement(); | ||
|
||
baseElement.setAttribute('target', value); | ||
}; | ||
|
||
private getBaseElement() { | ||
const head = globalThis.document.head; | ||
|
||
const maybeExistingBase = head.querySelector('base'); | ||
|
||
if (!maybeExistingBase) { | ||
const newBase = globalThis.document.createElement('base'); | ||
|
||
head.appendChild(newBase); | ||
|
||
return newBase; | ||
} | ||
|
||
return maybeExistingBase; | ||
} | ||
} |
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