forked from MapComplete/MapComplete
Fix loading of themes from external sources
This commit is contained in:
parent
d5f4572e9a
commit
bec7ed6da6
3 changed files with 8 additions and 13 deletions
|
@ -18,7 +18,6 @@ export default class DetermineLayout {
|
||||||
*/
|
*/
|
||||||
public static async GetLayout(): Promise<[LayoutConfig, string]> {
|
public static async GetLayout(): Promise<[LayoutConfig, string]> {
|
||||||
|
|
||||||
|
|
||||||
const loadCustomThemeParam = QueryParameters.GetQueryParameter("userlayout", "false", "If not 'false', a custom (non-official) theme is loaded. This custom layout can be done in multiple ways: \n\n- The hash of the URL contains a base64-encoded .json-file containing the theme definition\n- The hash of the URL contains a lz-compressed .json-file, as generated by the custom theme generator\n- The parameter itself is an URL, in which case that URL will be downloaded. It should point to a .json of a theme")
|
const loadCustomThemeParam = QueryParameters.GetQueryParameter("userlayout", "false", "If not 'false', a custom (non-official) theme is loaded. This custom layout can be done in multiple ways: \n\n- The hash of the URL contains a base64-encoded .json-file containing the theme definition\n- The hash of the URL contains a lz-compressed .json-file, as generated by the custom theme generator\n- The parameter itself is an URL, in which case that URL will be downloaded. It should point to a .json of a theme")
|
||||||
const layoutFromBase64 = decodeURIComponent(loadCustomThemeParam.data);
|
const layoutFromBase64 = decodeURIComponent(loadCustomThemeParam.data);
|
||||||
|
|
||||||
|
@ -73,17 +72,13 @@ export default class DetermineLayout {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
const data = await Utils.downloadJson(link)
|
const parsed = await Utils.downloadJson(link)
|
||||||
|
console.log("Got ", parsed)
|
||||||
try {
|
try {
|
||||||
let parsed = data;
|
|
||||||
if (typeof parsed == "string") {
|
|
||||||
parsed = JSON.parse(parsed);
|
|
||||||
}
|
|
||||||
// Overwrite the id to the url
|
|
||||||
parsed.id = link;
|
parsed.id = link;
|
||||||
return new LayoutConfig(parsed, false).patchImages(link, data);
|
return new LayoutConfig(parsed, false).patchImages(link, JSON.stringify(parsed));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
console.error(e)
|
||||||
DetermineLayout.ShowErrorOnCustomTheme(
|
DetermineLayout.ShowErrorOnCustomTheme(
|
||||||
`<a href="${link}">${link}</a> is invalid:`,
|
`<a href="${link}">${link}</a> is invalid:`,
|
||||||
new FixedUiElement(e)
|
new FixedUiElement(e)
|
||||||
|
@ -92,6 +87,7 @@ export default class DetermineLayout {
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
console.erorr(e)
|
||||||
DetermineLayout.ShowErrorOnCustomTheme(
|
DetermineLayout.ShowErrorOnCustomTheme(
|
||||||
`<a href="${link}">${link}</a> is invalid - probably not found or invalid JSON:`,
|
`<a href="${link}">${link}</a> is invalid - probably not found or invalid JSON:`,
|
||||||
new FixedUiElement(e)
|
new FixedUiElement(e)
|
||||||
|
@ -107,7 +103,7 @@ export default class DetermineLayout {
|
||||||
try {
|
try {
|
||||||
// layoutFromBase64 contains the name of the theme. This is partly to do tracking with goat counter
|
// layoutFromBase64 contains the name of the theme. This is partly to do tracking with goat counter
|
||||||
const dedicatedHashFromLocalStorage = LocalStorageSource.Get(
|
const dedicatedHashFromLocalStorage = LocalStorageSource.Get(
|
||||||
"user-layout-" + userLayoutParam.data.replace(" ", "_")
|
"user-layout-" + userLayoutParam.data?.replace(" ", "_")
|
||||||
);
|
);
|
||||||
if (dedicatedHashFromLocalStorage.data?.length < 10) {
|
if (dedicatedHashFromLocalStorage.data?.length < 10) {
|
||||||
dedicatedHashFromLocalStorage.setData(undefined);
|
dedicatedHashFromLocalStorage.setData(undefined);
|
||||||
|
@ -134,6 +130,7 @@ export default class DetermineLayout {
|
||||||
try {
|
try {
|
||||||
json = JSON.parse(Utils.UnMinify(LZString.decompressFromBase64(hash)))
|
json = JSON.parse(Utils.UnMinify(LZString.decompressFromBase64(hash)))
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
console.error(e)
|
||||||
DetermineLayout.ShowErrorOnCustomTheme("Could not decode the hash", new FixedUiElement("Not a valid (LZ-compressed) JSON"))
|
DetermineLayout.ShowErrorOnCustomTheme("Could not decode the hash", new FixedUiElement("Not a valid (LZ-compressed) JSON"))
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -143,6 +140,7 @@ export default class DetermineLayout {
|
||||||
userLayoutParam.setData(layoutToUse.id);
|
userLayoutParam.setData(layoutToUse.id);
|
||||||
return [layoutToUse, btoa(Utils.MinifyJSON(JSON.stringify(json)))];
|
return [layoutToUse, btoa(Utils.MinifyJSON(JSON.stringify(json)))];
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
console.error(e)
|
||||||
if (hash === undefined || hash.length < 10) {
|
if (hash === undefined || hash.length < 10) {
|
||||||
DetermineLayout.ShowErrorOnCustomTheme("Could not load a theme from the hash", new FixedUiElement("Hash does not contain data"))
|
DetermineLayout.ShowErrorOnCustomTheme("Could not load a theme from the hash", new FixedUiElement("Hash does not contain data"))
|
||||||
}
|
}
|
||||||
|
|
|
@ -304,7 +304,6 @@ export default class LayoutConfig {
|
||||||
}
|
}
|
||||||
rewriting.forEach((value, key) => {
|
rewriting.forEach((value, key) => {
|
||||||
console.log("Rewriting", key, "==>", value)
|
console.log("Rewriting", key, "==>", value)
|
||||||
|
|
||||||
originalJson = originalJson.replace(new RegExp(key, "g"), value)
|
originalJson = originalJson.replace(new RegExp(key, "g"), value)
|
||||||
})
|
})
|
||||||
return new LayoutConfig(JSON.parse(originalJson), false, "Layout rewriting")
|
return new LayoutConfig(JSON.parse(originalJson), false, "Layout rewriting")
|
||||||
|
|
2
index.ts
2
index.ts
|
@ -33,8 +33,6 @@ if (location.href.startsWith("http://buurtnatuur.be")) {
|
||||||
|
|
||||||
|
|
||||||
class Init {
|
class Init {
|
||||||
|
|
||||||
|
|
||||||
public static Init(layoutToUse: LayoutConfig, encoded: string) {
|
public static Init(layoutToUse: LayoutConfig, encoded: string) {
|
||||||
|
|
||||||
if(layoutToUse === null){
|
if(layoutToUse === null){
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue