Wrap markdown rendering in iframe (#837)

Signed-off-by: Tilman Vatteroth <tilman.vatteroth@tu-dortmund.de>
This commit is contained in:
Tilman Vatteroth 2021-01-24 20:50:51 +01:00 committed by GitHub
parent bd31076928
commit 586969f368
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
45 changed files with 1014 additions and 287 deletions

View file

@ -0,0 +1,43 @@
/*
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
export abstract class IframeCommunicator<SEND, RECEIVE> {
protected otherSide?: Window
protected otherOrigin?: string
constructor () {
window.addEventListener("message", this.handleEvent.bind(this))
}
public unregisterEventListener (): void {
window.removeEventListener("message", this.handleEvent.bind(this))
}
public setOtherSide (otherSide: Window, otherOrigin: string): void {
this.otherSide = otherSide
this.otherOrigin = otherOrigin
}
public unsetOtherSide (): void {
this.otherSide = undefined
this.otherOrigin = undefined
}
public getOtherSide (): Window | undefined {
return this.otherSide
}
protected sendMessageToOtherSide (message: SEND): void {
if (this.otherSide === undefined || this.otherOrigin === undefined) {
console.error("Can't send message because otherSide is null", message)
return
}
this.otherSide.postMessage(message, this.otherOrigin)
}
protected abstract handleEvent (event: MessageEvent<RECEIVE>): void;
}