diff --git a/src/app/markup-text-component/markup-text-component.component.ts b/src/app/markup-text-component/markup-text-component.component.ts index eceb9b0..8fed6b3 100644 --- a/src/app/markup-text-component/markup-text-component.component.ts +++ b/src/app/markup-text-component/markup-text-component.component.ts @@ -1,115 +1,95 @@ import {Component, OnInit, Input, OnChanges} from '@angular/core'; @Component({ selector: 'app-markup-text-component', templateUrl: './markup-text-component.component.html', styleUrls: ['./markup-text-component.component.scss'] }) export class MarkupTextComponentComponent implements OnChanges { @Input() markupData; // ranges: Array; word:string = 'Schifffahrtskapitän'; ranges: Array> = [ [0, 3, "font-weight: bold;"], [5, 11, "font-style: italic;"], [5, 5, "text-decoration: underline;" ], [7, 7, "color: blue;"], [8, 10, "text-decoration: line-through;"], [10, 13, "font-weight: bold;"]]; //indexList: Array = [0, 3, 5, 7, 8, 10, 11, 13]; // distinct list of all indices indexList: Array = [0, 3, 5, 6, 7, 8, 10, 11, 13]; // distinct list of all indices singleEndings: Array = [3, 13]; // ending which are NOT a start of a new range! noRangeExceptions: Array = [ 5, 7 , 10]; // distinct list of segments with start === any end segments: Array = []; constructor() { } ngOnChanges() { this.createSegments(); } // this method is only creating segments which are styled differently private createSegments() { let c = 1; let lastIndex: number = null; // sometimes this has been already processed. for (let i in this.indexList) { if (c < this.indexList.length) { lastIndex = this.createStyleSegments(this.indexList[i[0]], this.indexList[c], lastIndex); c += 1; } } console.log('this.segments: ', this.segments); } private createStyleSegments(startIndex, nextIndex, lastEndindex? ) { // we get the styles for each startIndex and define the range/endIndex where a style is ending console.log('lastEndindex ', lastEndindex, 'startIndex: ', startIndex, 'nextIndex: ', nextIndex); + // if an index is a singleEnding (ending , where no other style is starting) or is already set as a endpoint in this.segments, // then we need to start at the following character by creating a new startindex = startIndex + 1 for this. if (lastEndindex && startIndex === lastEndindex ) { startIndex = lastEndindex + 1; } - if (this.noRangeExceptions.indexOf(nextIndex) > -1) { + if (this.noRangeExceptions.indexOf(nextIndex) > -1) { // if the nextIndex is part of norangeexceptions nextIndex = nextIndex - 1; } - console.log('new indices: ', startIndex, ', ', nextIndex); - const styles = this.getStyles(startIndex); - if (this.singleEndings.indexOf(nextIndex) > -1) { - // if the nextIndex is a single endIndex, the style from startIndex - // will end at nextIndex, as nextIndex equals the endIndex of the style. We can simply segment that and push the styles - // create the segment by pushing [the actual range with all styles] - this.segments.push([startIndex, nextIndex, styles ]); - return nextIndex; - } else { // if the nextIndex is also a start of other styles we check further... - // TODO remove this if for norangeexceptions once a norangeexception is present simply as doubled index in the distinct list of indices - if (this.noRangeExceptions.indexOf(startIndex) > -1) { - // if the startindex is in norangeExceptions, the startindex equals at least one endindex, - // so we have to define an own style range for this single charter - this.segments.push([startIndex, startIndex, styles ]); - return startIndex; - // as after a singleRangeindex another style (which may not be defined) must start, - // we define that now with a new starting point of startIndex + 1 - // if this exists already as an index within our indexlist, we just break and let it be handled by the next iteration, - // else we define that new startIndex ourselves and call this very method recursively. - if (startIndex + 1 < nextIndex) { - this.createStyleSegments(startIndex + 1, nextIndex ); - } - } else { - // if the startIndex is not defining a single character range, we just define the style until the nextIndex - 1 - // (at nextindex a new style is already starting) - this.segments.push([startIndex, nextIndex, styles]); - return nextIndex; - } + if (this.noRangeExceptions.indexOf(startIndex) > -1) { // if the startIndex is part of norangeexceptions + nextIndex = startIndex; - } } + console.log('new indices defined: ', startIndex, ', ', nextIndex); + const styles = this.getStyles(startIndex); + this.segments.push([startIndex, nextIndex, styles ]); + return nextIndex; + } + private getStyles(startIndex) { // HENCE the styles of a segment are unique for every character within that segment we only need to get the styles of the startindex // and the styles apply to the whole segment correctly. No endindex needed. const styles: Array = []; for (const i of this.ranges) { // console.log('i[0]: ', i[0], ' <= ', startIndex, '<= i[1]: ', i[1] ); if (i[0] <= startIndex && startIndex <= i[1] && styles.indexOf(i[2]) === -1) { // console.log(i[2], ' -> pushed to styles'); styles.push(i[2]); } } return styles; } } export interface Range { startindex: number; endindex: number; cssStyle: string; }