question-text.service.ts 2.49 KB
Newer Older
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
1 2 3 4
import {EventEmitter, Injectable, Output} from '@angular/core';
import {parseGithubFlavoredMarkdown} from '../../lib/markdown/markdown';
import {HttpClient} from '@angular/common/http';
import {DefaultSettings} from '../../lib/default.settings';
5
import {IMathjaxResponse} from 'arsnova-click-v2-types/src/common';
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

@Injectable()
export class QuestionTextService {

  @Output() fire: EventEmitter<string | Array<string>> = new EventEmitter();
  private _inputCache = {};

  constructor(private http: HttpClient) {
  }

  parseMathjax(value: Array<string>) {
    return new Promise<Array<IMathjaxResponse>>(resolve => {
      this.http.post(`${DefaultSettings.httpLibEndpoint}/mathjax`, {
        mathjax: JSON.stringify(value),
        format: 'TeX',
        output: 'svg'
      }).subscribe((data: any) => {
        resolve(data);
      });
    });
  }

  parseInput(value: string) {
    if (this._inputCache[value]) {
      return new Promise((resolve => resolve(this._inputCache[value])));
    }
32 33

    const matchForDollar = value.match(/(\${1,2}.*\$)/g);
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
34
    const matchForBlock = value.match(/(\\(.)*\\.*)/g);
35
    let result = value;
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
36
    let mathjaxValues = [];
37

Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
38 39 40 41 42 43 44 45 46 47
    if (matchForDollar) {
      mathjaxValues = mathjaxValues.concat(matchForDollar);
    }
    if (matchForBlock) {
      mathjaxValues = mathjaxValues.concat(matchForBlock);
    }

    return new Promise((resolve) => {
      if (mathjaxValues.length) {
        this.parseMathjax(mathjaxValues).then((mathjaxRendered) => {
48 49 50

          result = parseGithubFlavoredMarkdown(result);

Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
51
          mathjaxValues.forEach((mathjaxValue: string, index: number) => {
52
            result = result.replace(mathjaxValue, mathjaxRendered[index].svg);
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
53
          });
54

Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
55 56
          this._inputCache[value] = result;
          resolve(result);
57

Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
58 59
        });
      } else {
60

61
        result = parseGithubFlavoredMarkdown(result);
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
62 63
        this._inputCache[value] = result;
        resolve(result);
64

Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
      }
    });
  }

  change(value: string): void {
    this.parseInput(value).then((result: string) => {
      this.fire.emit(result);
    });
  }

  changeMultiple(value: Array<string>): void {
    const allResults = [];
    value.forEach((singleValue, index) => {
      this.parseInput(singleValue).then((result: string) => {
        allResults[index] = result;
        if (allResults.filter(singleResult => !!singleResult).length === value.length) {
          this.fire.emit(allResults);
        }
      });
    });
  }

  getEmitter(): EventEmitter<string | Array<string>> {
    return this.fire;
  }

}