Typescript init

This commit is contained in:
Sidharth Vinod
2022-08-20 23:40:52 +05:30
parent a89b6fd054
commit 88e17bf1b4
7 changed files with 148 additions and 17 deletions

View File

@@ -1,4 +1,3 @@
import utils from './utils';
import * as configApi from './config';
import { log } from './logger';
import { getDiagrams } from './diagram-api/diagramAPI';
@@ -8,7 +7,7 @@ class Diagram {
parser;
renderer;
db;
constructor(txt) {
constructor(public txt: string) {
const diagrams = getDiagrams();
const cnf = configApi.getConfig();
this.txt = txt;
@@ -17,26 +16,32 @@ class Diagram {
// console.log('this.type', this.type, diagrams[this.type]);
// Setup diagram
// @ts-ignore
this.db = diagrams[this.type].db;
this.db.clear?.();
// @ts-ignore
this.renderer = diagrams[this.type].renderer;
// @ts-ignore
this.parser = diagrams[this.type].parser;
// @ts-ignore
this.parser.parser.yy = this.db;
// @ts-ignore
if (typeof diagrams[this.type].init === 'function') {
// @ts-ignore
diagrams[this.type].init(cnf);
log.debug('Initialized diagram ' + this.type, cnf);
}
this.txt = this.txt + '\n';
this.txt += '\n';
this.parser.parser.yy.graphType = this.type;
this.parser.parser.yy.parseError = (str, hash) => {
this.parser.parser.yy.parseError = (str: string, hash: string) => {
const error = { str, hash };
throw error;
};
this.parser.parse(this.txt);
}
parse(text) {
parse(text: string) {
var parseEncounteredException = false;
try {
text = text + '\n';
@@ -47,13 +52,17 @@ class Diagram {
parseEncounteredException = true;
// Is this the correct way to access mermiad's parseError()
// method ? (or global.mermaid.parseError()) ?
// @ts-ignore
if (global.mermaid.parseError) {
// @ts-ignore
if (error.str != undefined) {
// handle case where error string and hash were
// wrapped in object like`const error = { str, hash };`
// @ts-ignore
global.mermaid.parseError(error.str, error.hash);
} else {
// assume it is just error string and pass it on
// @ts-ignore
global.mermaid.parseError(error);
}
} else {
@@ -63,9 +72,11 @@ class Diagram {
}
return !parseEncounteredException;
}
getParser() {
return this.parser;
}
getType() {
return this.type;
}

View File

@@ -12,11 +12,11 @@ export const LEVELS = {
};
export const log = {
debug: () => {},
info: () => {},
warn: () => {},
error: () => {},
fatal: () => {},
debug: (...args) => {},
info: (...args) => {},
warn: (...args) => {},
error: (...args) => {},
fatal: (...args) => {},
};
/**