Skip to content
Snippets Groups Projects
Commit b361c942 authored by rhenck's avatar rhenck
Browse files

Fix cloze element to find child elements in lists and blockquotes

parent 0a4a9bf8
No related branches found
No related tags found
No related merge requests found
...@@ -46,12 +46,32 @@ export class ClozeElement extends CompoundElement implements PositionedElement, ...@@ -46,12 +46,32 @@ export class ClozeElement extends CompoundElement implements PositionedElement,
} }
getChildElements(): InputElement[] { getChildElements(): InputElement[] {
return this.document.content const elementList: InputElement[] = [];
.filter((paragraph: ClozeDocumentParagraph) => paragraph.content) // filter empty paragraphs this.document.content.forEach((documentPart: any) => {
.map((paragraph: ClozeDocumentParagraph) => paragraph.content // get custom paragraph parts if (documentPart.type === 'paragraph') {
.filter((word: ClozeDocumentPart) => ['TextField', 'DropList', 'ToggleButton'].includes(word.type))) elementList.push(...ClozeElement.getParagraphCustomElements(documentPart));
.reduce((accumulator: any[], currentValue: any) => accumulator // put all collected paragraph parts into one list } else if (documentPart.type === 'bulletList' || documentPart.type === 'orderedList') {
.concat(currentValue.map((node: ClozeDocumentPart) => node.attrs?.model)), []); // model is in node.attrs.model documentPart.content.forEach((listItem: any) => {
listItem.content.forEach((listItemParagraph: any) => {
elementList.push(...ClozeElement.getParagraphCustomElements(listItemParagraph));
});
});
} else if (documentPart.type === 'blockquote') {
documentPart.content.forEach((blockQuoteItem: any) => {
elementList.push(...ClozeElement.getParagraphCustomElements(blockQuoteItem));
});
}
});
return elementList;
}
private static getParagraphCustomElements(documentPart: any): InputElement[] {
console.log('fff', documentPart);
return documentPart.content
.filter((word: ClozeDocumentPart) => ['TextField', 'DropList', 'ToggleButton'].includes(word.type))
.reduce((accumulator: any[], currentValue: any) => {
return accumulator.concat(currentValue.attrs.model);
}, []);
} }
private handleBackwardsCompatibility(serializedElement: Partial<UIElement>): void { private handleBackwardsCompatibility(serializedElement: Partial<UIElement>): void {
......
...@@ -6,19 +6,35 @@ import { ToggleButtonElement } from '../../../../common/ui-elements/toggle-butto ...@@ -6,19 +6,35 @@ import { ToggleButtonElement } from '../../../../common/ui-elements/toggle-butto
export abstract class ClozeParser { export abstract class ClozeParser {
static setMissingIDs(clozeJSON: ClozeDocument, idService: IdService): ClozeDocument { static setMissingIDs(clozeJSON: ClozeDocument, idService: IdService): ClozeDocument {
clozeJSON.content.forEach((paragraph: any) => { clozeJSON.content.forEach((node: any) => {
paragraph.content?.forEach((node: any) => { if (node.type === 'paragraph') {
if (['ToggleButton', 'DropList', 'TextField'].includes(node.type) && ClozeParser.createSubNodeElements(node, idService);
node.attrs.model.id === 'id_placeholder') { } else if (node.type === 'bulletList' || node.type === 'orderedList') {
// create element anew because the TextEditor can't create multiple element instances node.content.forEach((listItem: any) => {
node.attrs.model = ClozeParser.createElement(node.attrs.model); listItem.content.forEach((listItemParagraph: any) => {
node.attrs.model.id = idService.getNewID(node.attrs.model.type); ClozeParser.createSubNodeElements(listItemParagraph, idService);
} });
}); });
} else if (node.type === 'blockquote') {
node.content.forEach((blockQuoteItem: any) => {
ClozeParser.createSubNodeElements(blockQuoteItem, idService);
});
}
}); });
return clozeJSON; return clozeJSON;
} }
// create element anew because the TextEditor can't create multiple element instances
private static createSubNodeElements(node: any, idService: IdService) {
node.content?.forEach((subNode: any) => {
if (['ToggleButton', 'DropList', 'TextField'].includes(subNode.type) &&
subNode.attrs.model.id === 'id_placeholder') {
subNode.attrs.model = ClozeParser.createElement(subNode.attrs.model);
subNode.attrs.model.id = idService.getNewID(subNode.attrs.model.type);
}
});
}
private static createElement(elementModel: Partial<UIElement>): InputElement { private static createElement(elementModel: Partial<UIElement>): InputElement {
let newElement: InputElement; let newElement: InputElement;
switch (elementModel.type) { switch (elementModel.type) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment