From 6adf762569282320dbed52748182d9b2811674f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Mon, 27 Sep 2021 13:04:00 +0200 Subject: [PATCH] Convert Skinner to TS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- src/@types/global.d.ts | 2 ++ src/{Skinner.js => Skinner.ts} | 33 +++++++++++++++++++++------------ 2 files changed, 23 insertions(+), 12 deletions(-) rename src/{Skinner.js => Skinner.ts} (84%) diff --git a/src/@types/global.d.ts b/src/@types/global.d.ts index d5856a5702..6309056bc2 100644 --- a/src/@types/global.d.ts +++ b/src/@types/global.d.ts @@ -51,6 +51,7 @@ import { SetupEncryptionStore } from "../stores/SetupEncryptionStore"; import { RoomScrollStateStore } from "../stores/RoomScrollStateStore"; import { ConsoleLogger, IndexedDBLogStore } from "../rageshake/rageshake"; import ActiveWidgetStore from "../stores/ActiveWidgetStore"; +import { Skinner } from "../Skinner"; /* eslint-disable @typescript-eslint/naming-convention */ @@ -95,6 +96,7 @@ declare global { mxSetupEncryptionStore?: SetupEncryptionStore; mxRoomScrollStateStore?: RoomScrollStateStore; mxActiveWidgetStore?: ActiveWidgetStore; + mxSkinner?: Skinner; mxOnRecaptchaLoaded?: () => void; electron?: Electron; } diff --git a/src/Skinner.js b/src/Skinner.ts similarity index 84% rename from src/Skinner.js rename to src/Skinner.ts index ef340e4052..6b20781b59 100644 --- a/src/Skinner.js +++ b/src/Skinner.ts @@ -14,12 +14,20 @@ See the License for the specific language governing permissions and limitations under the License. */ -class Skinner { - constructor() { - this.components = null; - } +import React from "react"; - getComponent(name) { +export interface IComponents { + [key: string]: React.Component; +} + +export interface ISkinObject { + components: IComponents; +} + +export class Skinner { + public components: IComponents = null; + + public getComponent(name: string): React.Component { if (!name) throw new Error(`Invalid component name: ${name}`); if (this.components === null) { throw new Error( @@ -30,7 +38,7 @@ class Skinner { ); } - const doLookup = (components) => { + const doLookup = (components: IComponents): React.Component => { if (!components) return null; let comp = components[name]; // XXX: Temporarily also try 'views.' as we're currently @@ -58,7 +66,7 @@ class Skinner { return comp; } - load(skinObject) { + public load(skinObject: ISkinObject): void { if (this.components !== null) { throw new Error( "Attempted to load a skin while a skin is already loaded"+ @@ -72,6 +80,7 @@ class Skinner { } // Now that we have a skin, load our components too + // eslint-disable-next-line @typescript-eslint/no-var-requires const idx = require("./component-index"); if (!idx || !idx.components) throw new Error("Invalid react-sdk component index"); for (const c in idx.components) { @@ -79,7 +88,7 @@ class Skinner { } } - addComponent(name, comp) { + public addComponent(name: string, comp: any) { let slot = name; if (comp.replaces !== undefined) { if (comp.replaces.indexOf('.') > -1) { @@ -91,7 +100,7 @@ class Skinner { this.components[slot] = comp; } - reset() { + public reset(): void { this.components = null; } } @@ -105,8 +114,8 @@ class Skinner { // See https://derickbailey.com/2016/03/09/creating-a-true-singleton-in-node-js-with-es6-symbols/ // or https://nodejs.org/api/modules.html#modules_module_caching_caveats // ("Modules are cached based on their resolved filename") -if (global.mxSkinner === undefined) { - global.mxSkinner = new Skinner(); +if (window.mxSkinner === undefined) { + window.mxSkinner = new Skinner(); } -export default global.mxSkinner; +export default window.mxSkinner;