A world-class math input for the web
at main 30 lines 1.0 kB view raw
1import { CaretNode, h, t, VNode } from "@caret-js/core"; 2import { DecimalNode } from "./decimal"; 3import { MathExpressionTag } from "../tags/mathExpression"; 4import { CanBeAdjacentMultRightSideTag } from "../parselets/adjacentMultiplication"; 5import { ParenthesesChildTag } from "../parselets/parentheses"; 6 7export class NegativeNode extends CaretNode { 8 constructor(public child: CaretNode) { 9 super(); 10 this.addTag(new MathExpressionTag()).addTag( 11 new CanBeAdjacentMultRightSideTag(false) // Negative nodes cannot be on the right side of adjacent multiplication (that would just be subtraction) 12 ); 13 } 14 15 static from(child: CaretNode): NegativeNode | DecimalNode { 16 if ( 17 child instanceof DecimalNode && 18 !child.value.startsWith("-") && 19 !child.hasTag(ParenthesesChildTag) 20 ) { 21 return new DecimalNode(`-${child.value}`); 22 } 23 24 return new NegativeNode(child); 25 } 26 27 toDebugMathML(): VNode { 28 return h("mrow", {}, h("mo", {}, t("-")), this.child.toDebugMathML()); 29 } 30}