A world-class math input for the web
1import { CaretNode, h, t, VNode } from "@caret-js/core";
2import { CommaListNode } from "./commaList";
3import { MathExpressionTag } from "../tags/mathExpression";
4
5export class FunctionCallNode extends CaretNode {
6 constructor(public name: string, public args: CaretNode[] = []) {
7 super();
8 this.addTag(new MathExpressionTag());
9 }
10
11 static from(name: string, args: CaretNode[] = []): FunctionCallNode {
12 args = args.flatMap((node) =>
13 node instanceof CommaListNode ? node.expressions : [node]
14 );
15 return new FunctionCallNode(name, args).mergeTagsFromNodes(
16 args.filter((node) => node instanceof CommaListNode)
17 );
18 }
19
20 toDebugMathML(): VNode {
21 return h(
22 "mrow",
23 {},
24 h("mi", {}, t(this.name)),
25 h("mo", { stretchy: "true" }, t("(")),
26 ...this.args.flatMap((arg, index) => [
27 arg.toDebugMathML(),
28 index < this.args.length - 1 ? h("mo", {}, t(",")) : null,
29 ]),
30 h("mo", { stretchy: "true" }, t(")"))
31 );
32 }
33}