forked from mProjectsCode/obsidian-meta-bind-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathVF.ts
More file actions
131 lines (110 loc) · 3.45 KB
/
MathVF.ts
File metadata and controls
131 lines (110 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import type { EvalFunction } from 'mathjs';
import { AbstractViewField } from 'packages/core/src/fields/viewFields/AbstractViewField';
import type { ViewFieldMountable } from 'packages/core/src/fields/viewFields/ViewFieldMountable';
import type { ViewFieldVariable } from 'packages/core/src/fields/viewFields/ViewFieldVariable';
import { ErrorLevel, MetaBindExpressionError } from 'packages/core/src/utils/errors/MetaBindErrors';
import { parseLiteral, stringifyUnknown } from 'packages/core/src/utils/Literal';
import { Signal } from 'packages/core/src/utils/Signal';
import { DomHelpers, getUUID } from 'packages/core/src/utils/Utils';
interface MathVFResult {
value: unknown;
error: boolean;
}
export class MathVF extends AbstractViewField<MathVFResult> {
container?: HTMLElement;
expression?: EvalFunction;
expressionStr?: string;
hidden: boolean;
constructor(mountable: ViewFieldMountable) {
super(mountable);
this.hidden = false;
}
protected buildVariables(): void {
let varCounter = 0;
this.expressionStr = '';
this.variables = [];
for (const entry of this.mountable.getDeclaration().templateDeclaration ?? []) {
if (typeof entry !== 'string') {
const variable: ViewFieldVariable = {
bindTargetDeclaration: entry,
metadataSignal: new Signal<unknown>(undefined),
uuid: getUUID(),
contextName: `MB_VAR_${varCounter}`,
};
this.variables.push(variable);
this.expressionStr += variable.contextName;
varCounter += 1;
} else {
this.expressionStr += entry;
}
}
this.expression = this.plugin.internal.math.compile(this.expressionStr);
}
private buildMathJSContext(): Record<string, unknown> {
const context: Record<string, unknown> = {};
for (const variable of this.variables ?? []) {
if (!variable.contextName || !variable.metadataSignal) {
continue;
}
context[variable.contextName] = variable.metadataSignal.get() ?? '';
}
return context;
}
protected computeValue(): MathVFResult {
if (!this.expression) {
return this.handleComputeError(
new MetaBindExpressionError({
errorLevel: ErrorLevel.ERROR,
effect: 'failed to evaluate expression',
cause: 'expression is undefined',
}),
);
}
const context = this.buildMathJSContext();
try {
const value: string = `${this.expression.evaluate(context)}`;
return {
value: parseLiteral(value),
error: false,
};
} catch (e) {
if (e instanceof Error) {
return this.handleComputeError(
new MetaBindExpressionError({
errorLevel: ErrorLevel.ERROR,
effect: `failed to evaluate expression`,
cause: e,
context: {
expression: this.expressionStr,
context: context,
},
}),
);
} else {
return this.handleComputeError(
new Error('failed to evaluate js expression because of unexpected thrown value'),
);
}
}
}
protected mapValue(value: MathVFResult): unknown {
return value.value;
}
protected onInitialRender(_container: HTMLElement): void {}
protected onRerender(container: HTMLElement, value: MathVFResult | undefined): void {
const text = stringifyUnknown(value?.value, this.mountable.plugin.settings.viewFieldDisplayNullAsEmpty) ?? '';
if (value?.error) {
DomHelpers.addClass(container, 'mb-error');
} else {
DomHelpers.removeClass(container, 'mb-error');
}
container.innerText = text;
}
private handleComputeError(e: Error): MathVFResult {
console.warn(e);
return {
error: true,
value: e.message,
};
}
}