forked from MapComplete/MapComplete
Performance improvements
This commit is contained in:
parent
49f78d5604
commit
8f8ef690a4
11 changed files with 125 additions and 101 deletions
|
@ -5,9 +5,7 @@ import LiveQueryHandler from "../Logic/Web/LiveQueryHandler";
|
|||
import {ImageCarousel} from "./Image/ImageCarousel";
|
||||
import Combine from "./Base/Combine";
|
||||
import {FixedUiElement} from "./Base/FixedUiElement";
|
||||
import Locale from "../UI/i18n/Locale";
|
||||
import {ImageUploadFlow} from "./Image/ImageUploadFlow";
|
||||
import {Translation} from "./i18n/Translation";
|
||||
|
||||
import ShareButton from "./BigComponents/ShareButton";
|
||||
import Svg from "../Svg";
|
||||
|
@ -19,93 +17,6 @@ import OpeningHoursVisualization from "./OpeningHours/OhVisualization";
|
|||
|
||||
import State from "../State";
|
||||
|
||||
export class SubstitutedTranslation extends UIElement {
|
||||
private readonly tags: UIEventSource<any>;
|
||||
private readonly translation: Translation;
|
||||
private content: UIElement[];
|
||||
|
||||
constructor(
|
||||
translation: Translation,
|
||||
tags: UIEventSource<any>) {
|
||||
super(tags);
|
||||
this.translation = translation;
|
||||
this.tags = tags;
|
||||
const self = this;
|
||||
tags.addCallbackAndRun(() => {
|
||||
self.content = self.CreateContent();
|
||||
self.Update();
|
||||
});
|
||||
|
||||
Locale.language.addCallback(() => {
|
||||
self.content = self.CreateContent();
|
||||
self.Update();
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
InnerRender(): string {
|
||||
return new Combine(this.content).Render();
|
||||
}
|
||||
|
||||
private CreateContent(): UIElement[] {
|
||||
let txt = this.translation?.txt;
|
||||
if (txt === undefined) {
|
||||
return []
|
||||
}
|
||||
const tags = this.tags.data;
|
||||
txt = SubstitutedTranslation.SubstituteKeys(txt, tags);
|
||||
return this.EvaluateSpecialComponents(txt);
|
||||
}
|
||||
|
||||
public static SubstituteKeys(txt: string, tags: any) {
|
||||
for (const key in tags) {
|
||||
// Poor mans replace all
|
||||
txt = txt.split("{" + key + "}").join(tags[key]);
|
||||
}
|
||||
return txt;
|
||||
}
|
||||
|
||||
private EvaluateSpecialComponents(template: string): UIElement[] {
|
||||
|
||||
for (const knownSpecial of SpecialVisualizations.specialVisualizations) {
|
||||
|
||||
// Note: the '.*?' in the regex reads as 'any character, but in a non-greedy way'
|
||||
const matched = template.match(`(.*){${knownSpecial.funcName}\\((.*?)\\)}(.*)`);
|
||||
if (matched != null) {
|
||||
|
||||
// We found a special component that should be brought to live
|
||||
const partBefore = this.EvaluateSpecialComponents(matched[1]);
|
||||
const argument = matched[2].trim();
|
||||
const partAfter = this.EvaluateSpecialComponents(matched[3]);
|
||||
try {
|
||||
const args = knownSpecial.args.map(arg => arg.defaultValue ?? "");
|
||||
if (argument.length > 0) {
|
||||
const realArgs = argument.split(",").map(str => str.trim());
|
||||
for (let i = 0; i < realArgs.length; i++) {
|
||||
if (args.length <= i) {
|
||||
args.push(realArgs[i]);
|
||||
} else {
|
||||
args[i] = realArgs[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const element = knownSpecial.constr(State.state, this.tags, args);
|
||||
return [...partBefore, element, ...partAfter]
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
return [...partBefore, new FixedUiElement(`Failed loading ${knownSpecial.funcName}(${matched[2]}): ${e}`), ...partAfter]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// IF we end up here, no changes have to be made
|
||||
return [new FixedUiElement(template)];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default class SpecialVisualizations {
|
||||
|
||||
public static specialVisualizations: {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue