A world-class math input for the web
at main 43 lines 1.2 kB view raw
1import { CaretNode, h, t, VNode } from "@caret-js/core"; 2import { MathExpressionTag } from "../tags/mathExpression"; 3import { MultiplicationStyleTag } from "../parselets/adjacentMultiplication"; 4 5export class MultiplyNode extends CaretNode { 6 constructor(public factors: CaretNode[]) { 7 super(); 8 this.addTag(new MathExpressionTag()); 9 } 10 11 static from(factors: CaretNode[]): MultiplyNode { 12 return new MultiplyNode( 13 factors.flatMap((node) => 14 node instanceof MultiplyNode ? node.factors : [node] 15 ) 16 ).mergeTagsFromNodes( 17 factors.filter((node) => node instanceof MultiplyNode) 18 ); 19 } 20 21 toDebugMathML(): VNode { 22 const separator = () => { 23 const tag = this.getTag(MultiplicationStyleTag); 24 if (tag?.style === "dot") { 25 return h("mo", {}, t("·")); 26 } else if (tag?.style === "cross") { 27 return h("mo", {}, t("×")); 28 } else if (tag?.style === "implicit") { 29 return null; 30 } 31 return null; 32 }; 33 34 return h( 35 "mrow", 36 {}, 37 ...this.factors.flatMap((factor, index) => [ 38 factor.toDebugMathML(), 39 index < this.factors.length - 1 ? separator() : null, 40 ]) 41 ); 42 } 43}