File tree Expand file tree Collapse file tree 4 files changed +51
-3
lines changed
exampleVault/Advanced Examples Expand file tree Collapse file tree 4 files changed +51
-3
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ num : 6
3+ ---
4+
5+ ``` js-engine
6+ const mb = engine.getPlugin('obsidian-meta-bind-plugin').api;
7+ const math = mb.getMathjs();
8+
9+ math.import({
10+ clamp: function (val, min, max) {
11+ return Math.min(Math.max(min, val), max);
12+ }
13+ }, {silent:true});
14+ ```
15+
16+ This is a clamped value: ` VIEW[clamp({num}, 0, 10)][math] `
17+ Test it with the slider
18+
19+ ``` meta-bind
20+ INPUT[slider(minValue(-5),maxValue(15)):num]
21+ ```
22+
23+
24+ > [ !Note]
25+ > Please note the second parameter for ` math.import ` .
26+ > Passing ` silent:true ` leads to the function <u >not</u > beeing updated on edit! (only on reload of meta-bind)
27+ > Pass ` override:true ` to override the function everytime you switch between edit and viewing mode.
Original file line number Diff line number Diff line change 1+ import type { MathJsInstance } from 'mathjs' ;
12import { SyntaxHighlightingAPI } from 'packages/core/src/api/SyntaxHighlightingAPI' ;
23import type {
34 ButtonGroupOptions ,
@@ -63,6 +64,7 @@ import { Signal } from 'packages/core/src/utils/Signal';
6364import { expectType , getUUID } from 'packages/core/src/utils/Utils' ;
6465import { validateAPIArgs } from 'packages/core/src/utils/ZodUtils' ;
6566import { z } from 'zod' ;
67+ import { getMathjsSingleton } from 'packages/core/src/utils/Mathjs' ;
6668
6769export interface LifecycleHook {
6870 register ( cb : ( ) => void ) : void ;
@@ -827,4 +829,12 @@ export abstract class API<Plugin extends IPlugin> {
827829 lineEnd : lineEnd ,
828830 } ) ;
829831 }
832+
833+ /**
834+ * get the mathjs instance used in math-views. Useful to modify the config and scope.
835+ *
836+ */
837+ public getMathjs ( ) : MathJsInstance {
838+ return getMathjsSingleton ( ) ;
839+ }
830840}
Original file line number Diff line number Diff line change 1- import type { EvalFunction } from 'mathjs' ;
2- import { compile as MathJsCompile } from 'mathjs' ;
1+ import type { EvalFunction , MathJsInstance } from 'mathjs' ;
32import { AbstractViewField } from 'packages/core/src/fields/viewFields/AbstractViewField' ;
43import type { ViewFieldMountable } from 'packages/core/src/fields/viewFields/ViewFieldMountable' ;
54import type { ViewFieldVariable } from 'packages/core/src/fields/viewFields/ViewFieldVariable' ;
65import { ErrorLevel , MetaBindExpressionError } from 'packages/core/src/utils/errors/MetaBindErrors' ;
76import { parseLiteral , stringifyUnknown } from 'packages/core/src/utils/Literal' ;
7+ import { getMathjsSingleton } from 'packages/core/src/utils/Mathjs' ;
88import { Signal } from 'packages/core/src/utils/Signal' ;
99import { DomHelpers , getUUID } from 'packages/core/src/utils/Utils' ;
1010
@@ -13,12 +13,15 @@ export class MathVF extends AbstractViewField<unknown> {
1313 expression ?: EvalFunction ;
1414 expressionStr ?: string ;
1515 hasError : boolean ;
16+ math : MathJsInstance ;
1617
1718 hidden : boolean ;
1819
1920 constructor ( mountable : ViewFieldMountable ) {
2021 super ( mountable ) ;
2122
23+ this . math = getMathjsSingleton ( ) ;
24+
2225 this . hidden = false ;
2326
2427 this . hasError = false ;
@@ -48,7 +51,7 @@ export class MathVF extends AbstractViewField<unknown> {
4851 }
4952 }
5053
51- this . expression = MathJsCompile ( this . expressionStr ) ;
54+ this . expression = this . math . compile ( this . expressionStr ) ;
5255 }
5356
5457 private buildMathJSContext ( ) : Record < string , unknown > {
Original file line number Diff line number Diff line change 1+ import type { MathJsInstance } from 'mathjs' ;
2+ import { create as MathjsCreate , all } from 'mathjs' ;
3+
4+ const math = MathjsCreate ( all ) ;
5+
6+ export function getMathjsSingleton ( ) : MathJsInstance {
7+ return math ;
8+ }
You can’t perform that action at this time.
0 commit comments