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

[editor] Refactor cloze parser

parent ce392fca
No related branches found
No related tags found
No related merge requests found
......@@ -256,7 +256,7 @@ export class UnitService {
this.idService.removeId(element.id);
this.idService.addID(<string>value);
} else if (property === 'text' && element.type === 'cloze') {
element.setProperty('parts', ClozeParser.parse(value as string, this.idService));
element.setProperty('parts', ClozeParser.createClozeParts(value as string, this.idService));
} else {
element.setProperty(property, value);
}
......
......@@ -9,12 +9,8 @@ import { ToggleButtonElement } from '../../../../common/ui-elements/toggle-butto
import { IdService } from '../services/id.service';
export abstract class ClozeParser {
static parse(text: string, idService: IdService): ClozePart[][] {
return ClozeParser.createParts(text, idService);
}
private static createParts(htmlText: string, idService: IdService): ClozePart[][] {
const elementList = ClozeParser.readElementArray(htmlText);
static createClozeParts(htmlText: string, idService: IdService): ClozePart[][] {
const elementList = ClozeParser.createElementList(htmlText);
const parts: ClozePart[][] = [];
elementList.forEach((element: HTMLParagraphElement | HTMLHeadingElement, i: number) => {
......@@ -23,7 +19,7 @@ export abstract class ClozeParser {
return parts;
}
private static readElementArray(htmlText: string): (HTMLParagraphElement | HTMLHeadingElement)[] {
private static createElementList(htmlText: string): (HTMLParagraphElement | HTMLHeadingElement)[] {
const el = document.createElement('html');
el.innerHTML = htmlText;
return Array.from(el.children[1].children) as (HTMLParagraphElement | HTMLHeadingElement)[];
......@@ -34,7 +30,7 @@ export abstract class ClozeParser {
element: HTMLParagraphElement | HTMLHeadingElement, partIndex: number, parts: ClozePart[][], idService: IdService
): ClozePart[][] {
parts[partIndex] = [];
let [nextSpecialElementIndex, nextElementType] = ClozeParser.getNextSpecialElement(element.innerHTML);
let [nextSpecialElementIndex, nextElementType] = ClozeParser.getNextElementMarker(element.innerHTML);
let indexOffset = 0;
while (nextSpecialElementIndex !== -1) {
......@@ -50,7 +46,7 @@ export abstract class ClozeParser {
indexOffset = nextSpecialElementIndex + 2; // + 2 to get rid of the marker, i.e. '\b'
[nextSpecialElementIndex, nextElementType] =
ClozeParser.getNextSpecialElement(element.innerHTML.substring(indexOffset));
ClozeParser.getNextElementMarker(element.innerHTML.substring(indexOffset));
}
parts[partIndex].push({
type: element.localName,
......@@ -60,7 +56,7 @@ export abstract class ClozeParser {
return parts;
}
private static getNextSpecialElement(p: string): [number, string] {
private static getNextElementMarker(p: string): [number, string] {
const x = [];
if (p.indexOf('\\d') > 0) {
x.push(p.indexOf('\\d'));
......@@ -106,7 +102,7 @@ export abstract class ClozeParser {
break;
case 'drop-list':
newElement = new DropListSimpleElement(elementModel);
newElement.height = 25;
newElement.height = 25; // TODO weg?
newElement.width = 100;
break;
case 'toggle-button':
......
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