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' ;
1
2
import { SyntaxHighlightingAPI } from 'packages/core/src/api/SyntaxHighlightingAPI' ;
2
3
import type {
3
4
ButtonGroupOptions ,
@@ -63,6 +64,7 @@ import { Signal } from 'packages/core/src/utils/Signal';
63
64
import { expectType , getUUID } from 'packages/core/src/utils/Utils' ;
64
65
import { validateAPIArgs } from 'packages/core/src/utils/ZodUtils' ;
65
66
import { z } from 'zod' ;
67
+ import { getMathjsSingleton } from 'packages/core/src/utils/Mathjs' ;
66
68
67
69
export interface LifecycleHook {
68
70
register ( cb : ( ) => void ) : void ;
@@ -827,4 +829,12 @@ export abstract class API<Plugin extends IPlugin> {
827
829
lineEnd : lineEnd ,
828
830
} ) ;
829
831
}
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
+ }
830
840
}
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' ;
3
2
import { AbstractViewField } from 'packages/core/src/fields/viewFields/AbstractViewField' ;
4
3
import type { ViewFieldMountable } from 'packages/core/src/fields/viewFields/ViewFieldMountable' ;
5
4
import type { ViewFieldVariable } from 'packages/core/src/fields/viewFields/ViewFieldVariable' ;
6
5
import { ErrorLevel , MetaBindExpressionError } from 'packages/core/src/utils/errors/MetaBindErrors' ;
7
6
import { parseLiteral , stringifyUnknown } from 'packages/core/src/utils/Literal' ;
7
+ import { getMathjsSingleton } from 'packages/core/src/utils/Mathjs' ;
8
8
import { Signal } from 'packages/core/src/utils/Signal' ;
9
9
import { DomHelpers , getUUID } from 'packages/core/src/utils/Utils' ;
10
10
@@ -13,12 +13,15 @@ export class MathVF extends AbstractViewField<unknown> {
13
13
expression ?: EvalFunction ;
14
14
expressionStr ?: string ;
15
15
hasError : boolean ;
16
+ math : MathJsInstance ;
16
17
17
18
hidden : boolean ;
18
19
19
20
constructor ( mountable : ViewFieldMountable ) {
20
21
super ( mountable ) ;
21
22
23
+ this . math = getMathjsSingleton ( ) ;
24
+
22
25
this . hidden = false ;
23
26
24
27
this . hasError = false ;
@@ -48,7 +51,7 @@ export class MathVF extends AbstractViewField<unknown> {
48
51
}
49
52
}
50
53
51
- this . expression = MathJsCompile ( this . expressionStr ) ;
54
+ this . expression = this . math . compile ( this . expressionStr ) ;
52
55
}
53
56
54
57
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