refactor: move mover and munder tag to single class
This commit is contained in:
parent
ddaf33fef5
commit
5d84976977
|
@ -17,8 +17,7 @@ import {
|
|||
MSub,
|
||||
MSubsup,
|
||||
MText,
|
||||
MOver,
|
||||
MUnder,
|
||||
UnderOverTag,
|
||||
} from './mathml-tags';
|
||||
|
||||
export class Dispatcher {
|
||||
|
@ -69,9 +68,8 @@ export class Dispatcher {
|
|||
case 'mtext':
|
||||
return new MText(this._value, this._attributes, this._children);
|
||||
case 'mover':
|
||||
return new MOver(this._value, this._attributes, this._children);
|
||||
case 'munder':
|
||||
return new MUnder(this._value, this._attributes, this._children);
|
||||
return new UnderOverTag(this._name, this._value, this._attributes, this._children);
|
||||
case 'mrow':
|
||||
case 'mpadded':
|
||||
return new GenericContentWrapperTag(this._name, this._value, this._attributes, this._children);
|
||||
|
|
|
@ -1,22 +0,0 @@
|
|||
import { MathMLTag } from './MathMLTag';
|
||||
import { InvalidNumberOfChild } from '../../../../errors';
|
||||
import { latexAccents } from '../../../../syntax/latexAccents';
|
||||
|
||||
export class MOver extends MathMLTag {
|
||||
constructor(value: string, attributes: Record<string, string>, children: MathMLTag[]) {
|
||||
super('mover', value, attributes, children);
|
||||
}
|
||||
|
||||
convert(): string {
|
||||
if (this._children.length !== 2) throw new InvalidNumberOfChild(this._name, 2, this._children.length);
|
||||
|
||||
const content = this._children[0].convert();
|
||||
const accent = this._children[1].convert();
|
||||
|
||||
return this._overset(content, accent);
|
||||
}
|
||||
|
||||
private _overset(content: string, accent: string): string {
|
||||
return latexAccents.includes(accent) ? `${accent}{${content}}` : `\\overset{${accent}}{${content}}`;
|
||||
}
|
||||
}
|
|
@ -1,22 +0,0 @@
|
|||
import { MathMLTag } from './MathMLTag';
|
||||
import { InvalidNumberOfChild } from '../../../../errors';
|
||||
import { latexAccents } from '../../../../syntax/latexAccents';
|
||||
|
||||
export class MUnder extends MathMLTag {
|
||||
constructor(value: string, attributes: Record<string, string>, children: MathMLTag[]) {
|
||||
super('munder', value, attributes, children);
|
||||
}
|
||||
|
||||
convert(): string {
|
||||
if (this._children.length !== 2) throw new InvalidNumberOfChild(this._name, 2, this._children.length);
|
||||
|
||||
const content = this._children[0].convert();
|
||||
const accent = this._children[1].convert();
|
||||
|
||||
return this._underset(content, accent);
|
||||
}
|
||||
|
||||
private _underset(content: string, accent: string): string {
|
||||
return latexAccents.includes(accent) ? `${accent}{${content}}` : `\\overset{${accent}}{${content}}`;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
import { MathMLTag } from './MathMLTag';
|
||||
import { InvalidNumberOfChild } from '../../../../errors';
|
||||
import { latexAccents } from '../../../../syntax/latexAccents';
|
||||
|
||||
export class UnderOverTag extends MathMLTag {
|
||||
convert(): string {
|
||||
if (this._children.length !== 2) throw new InvalidNumberOfChild(this._name, 2, this._children.length);
|
||||
|
||||
const content = this._children[0].convert();
|
||||
const accent = this._children[1].convert();
|
||||
|
||||
return this._applyCommand(content, accent);
|
||||
}
|
||||
|
||||
private _applyCommand(content: string, accent: string): string {
|
||||
const type = this._name.match(/under/) ? TagTypes.Under : TagTypes.Over;
|
||||
return new UnderOverSetter(type).apply(content, accent);
|
||||
}
|
||||
}
|
||||
|
||||
class UnderOverSetter {
|
||||
private readonly _type;
|
||||
|
||||
constructor(type: TagTypes) {
|
||||
this._type = type;
|
||||
}
|
||||
|
||||
apply(content: string, accent: string) {
|
||||
return latexAccents.includes(accent) ? `${accent}{${content}}` : `${this._defaultCommand}{${accent}}{${content}}`;
|
||||
}
|
||||
|
||||
private get _defaultCommand(): string {
|
||||
if (this._type === TagTypes.Under) return '\\underset';
|
||||
return '\\overset';
|
||||
}
|
||||
}
|
||||
|
||||
enum TagTypes {
|
||||
Under,
|
||||
Over,
|
||||
}
|
|
@ -16,5 +16,4 @@ export { MSup } from './MSup';
|
|||
export { MSub } from './MSub';
|
||||
export { MSubsup } from './MSubsup';
|
||||
export { MText } from './MText';
|
||||
export { MOver } from './MOver';
|
||||
export { MUnder } from './MUnder';
|
||||
export { UnderOverTag } from './UnderOverTag';
|
||||
|
|
Loading…
Reference in New Issue