Skip to content
Snippets Groups Projects
Verified Commit a337bd3c authored by Jan Loewe's avatar Jan Loewe :speech_balloon:
Browse files

feat(text block): add advanced behaviour for content

parent c3beba1e
No related branches found
No related tags found
No related merge requests found
...@@ -11,9 +11,30 @@ export class TextBlock extends XMLElement { ...@@ -11,9 +11,30 @@ export class TextBlock extends XMLElement {
} }
getContent(lang?: string): XMLElement | null { getContent(lang?: string): XMLElement | null {
// item with undefined as lang is default
let def = this.content.find(x => x._attr == null || x._attr.lang == null); let def = this.content.find(x => x._attr == null || x._attr.lang == null);
let items = this.content.filter(x => x._attr != null && x._attr.lang === lang); let items = this.content.filter(x => x._attr != null && x._attr.lang === lang);
return items.length ? items[0] : def; // if there is more than one content element
// concat the text of all content elements with same lang
if (items.length > 1) {
items.forEach((item) => {
this.content.splice(this.content.indexOf(item), 1);
});
let grouped = items.reduce((prev, curr) => {
prev._text += `\n${curr._text}`;
return prev;
});
console.log(grouped);
this.content.push(grouped);
return grouped;
} else {
return items.length ? items[0] : def;
}
} }
} }
...@@ -5,6 +5,7 @@ function getOptions() { ...@@ -5,6 +5,7 @@ function getOptions() {
return { return {
content: [ content: [
new ElementStringWithLang("Hallo", "de"), new ElementStringWithLang("Hallo", "de"),
new ElementStringWithLang("Jan", "de"),
new ElementStringWithLang("Hello", "en"), new ElementStringWithLang("Hello", "en"),
], ],
}; };
...@@ -42,7 +43,7 @@ describe("TextBlock", function() { ...@@ -42,7 +43,7 @@ describe("TextBlock", function() {
it("should return correct content with lang flag", function() { it("should return correct content with lang flag", function() {
let textBlock = new TextBlock(getOptionsWithDefault()); let textBlock = new TextBlock(getOptionsWithDefault());
expect(textBlock.getContent("de")._text).to.be.equal("Hallo"); expect(textBlock.getContent("de")._text).to.be.equal("Hallo\nJan");
}); });
}); });
}); });
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment