2020-03-25 13:53:28 +00:00
|
|
|
/*
|
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
|
|
|
Copyright 2017 Vector Creations Ltd
|
2020-03-25 14:28:07 +00:00
|
|
|
Copyright 2018, 2019, 2020 New Vector Ltd
|
2020-03-25 13:53:28 +00:00
|
|
|
Copyright 2019 Michael Telatynski <7t3chguy@gmail.com>
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
import olmWasmPath from "olm/olm.wasm";
|
|
|
|
import Olm from 'olm';
|
|
|
|
|
2020-03-25 13:55:25 +00:00
|
|
|
import * as languageHandler from 'matrix-react-sdk/src/languageHandler';
|
|
|
|
import SettingsStore from "matrix-react-sdk/src/settings/SettingsStore";
|
2020-03-25 14:32:37 +00:00
|
|
|
import ElectronPlatform from "./platform/ElectronPlatform";
|
|
|
|
import WebPlatform from "./platform/WebPlatform";
|
|
|
|
import PlatformPeg from 'matrix-react-sdk/src/PlatformPeg';
|
|
|
|
import SdkConfig from "matrix-react-sdk/src/SdkConfig";
|
2020-03-25 13:55:25 +00:00
|
|
|
|
|
|
|
|
2020-03-25 14:32:37 +00:00
|
|
|
export function preparePlatform() {
|
|
|
|
if ((<any>window).ipcRenderer) {
|
|
|
|
console.log("Using Electron platform");
|
|
|
|
const plaf = new ElectronPlatform();
|
|
|
|
PlatformPeg.set(plaf);
|
|
|
|
} else {
|
|
|
|
console.log("Using Web platform");
|
|
|
|
PlatformPeg.set(new WebPlatform());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-25 14:37:28 +00:00
|
|
|
export async function loadConfig(): Promise<Error | void> {
|
2020-03-25 14:32:37 +00:00
|
|
|
const platform = PlatformPeg.get();
|
|
|
|
|
|
|
|
let configJson;
|
|
|
|
try {
|
|
|
|
configJson = await platform.getConfig();
|
|
|
|
} catch (e) {
|
2020-03-25 14:37:28 +00:00
|
|
|
return e;
|
|
|
|
} finally {
|
|
|
|
// XXX: We call this twice, once here and once in MatrixChat as a prop. We call it here to ensure
|
|
|
|
// granular settings are loaded correctly and to avoid duplicating the override logic for the theme.
|
|
|
|
//
|
|
|
|
// Note: this isn't called twice for some wrappers, like the Jitsi wrapper.
|
|
|
|
SdkConfig.put(configJson || {});
|
2020-03-25 14:32:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-25 14:07:22 +00:00
|
|
|
export function loadOlm(): Promise<void> {
|
2020-03-25 13:53:28 +00:00
|
|
|
/* Load Olm. We try the WebAssembly version first, and then the legacy,
|
|
|
|
* asm.js version if that fails. For this reason we need to wait for this
|
|
|
|
* to finish before continuing to load the rest of the app. In future
|
|
|
|
* we could somehow pass a promise down to react-sdk and have it wait on
|
|
|
|
* that so olm can be loading in parallel with the rest of the app.
|
|
|
|
*
|
|
|
|
* We also need to tell the Olm js to look for its wasm file at the same
|
|
|
|
* level as index.html. It really should be in the same place as the js,
|
|
|
|
* ie. in the bundle directory, but as far as I can tell this is
|
|
|
|
* completely impossible with webpack. We do, however, use a hashed
|
|
|
|
* filename to avoid caching issues.
|
|
|
|
*/
|
|
|
|
return Olm.init({
|
|
|
|
locateFile: () => olmWasmPath,
|
|
|
|
}).then(() => {
|
|
|
|
console.log("Using WebAssembly Olm");
|
|
|
|
}).catch((e) => {
|
|
|
|
console.log("Failed to load Olm: trying legacy version", e);
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const s = document.createElement('script');
|
|
|
|
s.src = 'olm_legacy.js'; // XXX: This should be cache-busted too
|
|
|
|
s.onload = resolve;
|
|
|
|
s.onerror = reject;
|
|
|
|
document.body.appendChild(s);
|
|
|
|
}).then(() => {
|
|
|
|
// Init window.Olm, ie. the one just loaded by the script tag,
|
|
|
|
// not 'Olm' which is still the failed wasm version.
|
|
|
|
return window.Olm.init();
|
|
|
|
}).then(() => {
|
|
|
|
console.log("Using legacy Olm");
|
|
|
|
}).catch((e) => {
|
|
|
|
console.log("Both WebAssembly and asm.js Olm failed!", e);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2020-03-25 13:55:25 +00:00
|
|
|
|
|
|
|
export async function loadLanguage() {
|
|
|
|
const prefLang = SettingsStore.getValue("language", null, /*excludeDefault=*/true);
|
|
|
|
let langs = [];
|
|
|
|
|
|
|
|
if (!prefLang) {
|
|
|
|
languageHandler.getLanguagesFromBrowser().forEach((l) => {
|
|
|
|
langs.push(...languageHandler.getNormalizedLanguageKeys(l));
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
langs = [prefLang];
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await languageHandler.setLanguage(langs);
|
|
|
|
document.documentElement.setAttribute("lang", languageHandler.getCurrentLanguage());
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Unable to set language", e);
|
|
|
|
}
|
|
|
|
}
|
2020-04-04 17:21:59 +01:00
|
|
|
|
|
|
|
export async function loadSkin() {
|
|
|
|
// Ensure the skin is the very first thing to load for the react-sdk. We don't even want to reference
|
|
|
|
// the SDK until we have to in imports.
|
|
|
|
console.log("Loading skin...");
|
|
|
|
const [sdk, skin] = await Promise.all([
|
|
|
|
import(
|
|
|
|
/* webpackChunkName: "matrix-react-sdk" */
|
|
|
|
/* webpackPreload: true */
|
|
|
|
"matrix-react-sdk"),
|
|
|
|
import(
|
|
|
|
/* webpackChunkName: "riot-web-component-index" */
|
|
|
|
/* webpackPreload: true */
|
|
|
|
"../component-index"),
|
|
|
|
]);
|
|
|
|
sdk.loadSkin(skin);
|
|
|
|
console.log("Skin loaded!");
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function loadApp() {
|
|
|
|
// Finally, load the app. All of the other react-sdk imports are in this file which causes the skinner to
|
|
|
|
// run on the components. We use `require` here to make sure webpack doesn't optimize this into an async
|
|
|
|
// import and thus running before the skin can load.
|
|
|
|
const module = await import(
|
|
|
|
/* webpackChunkName: "riot-web-app" */
|
|
|
|
/* webpackPreload: true */
|
|
|
|
"./app");
|
|
|
|
await module.loadApp();
|
|
|
|
}
|
|
|
|
|
|
|
|
// throw new Error("foobar");
|
|
|
|
window.Map = undefined;
|