Skip to content

Commit b4acb55

Browse files
committed
reenable mathjs; remove dataview dep in build
1 parent 75dfd5e commit b4acb55

File tree

7 files changed

+87
-47
lines changed

7 files changed

+87
-47
lines changed

esbuild.config.mjs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ if you want to view the source, please visit the github repository of this plugi
1818
*/
1919
`;
2020

21-
const context = await esbuild.context({
21+
const build = await esbuild.build({
2222
banner: {
2323
js: banner,
2424
},
@@ -63,9 +63,6 @@ const context = await esbuild.context({
6363
],
6464
});
6565

66-
const build = await context.rebuild();
67-
console.log(await analyzeMetafile(build.metafile));
68-
6966
const file = Bun.file('meta.txt');
7067
await Bun.write(file, JSON.stringify(build.metafile, null, '\t'));
7168
process.exit(0);

exampleVault/Examples.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
slider1: 3
2+
slider1: 8
33
suggest: test
44
toggle1: false
55
Domestic_tasks:

exampleVault/View Fields/View Field.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
number1: 13
2+
number1: 12
33
number2: 200
44
unit: km
55
distance: 12

src/fields/inputFields/fields/Suggester/SuggesterModalHelper.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { type MBLiteral } from '../../../../utils/Literal';
2-
import { type DataArray, type DataviewApi, getAPI, type Literal } from 'obsidian-dataview';
2+
import { type DataArray, type DataviewApi, type Literal } from 'obsidian-dataview';
33
import { InputFieldArgumentType } from '../../../../config/FieldConfigs';
44
import { type OptionInputFieldArgument } from '../../../fieldArguments/inputFieldArguments/arguments/OptionInputFieldArgument';
55
import { type OptionQueryInputFieldArgument } from '../../../fieldArguments/inputFieldArguments/arguments/OptionQueryInputFieldArgument';
@@ -58,7 +58,8 @@ export function getSuggesterOptionsForInputField(
5858
plugin: MetaBindPlugin,
5959
): SuggesterOption<MBLiteral>[] {
6060
const app = plugin.app;
61-
const dv = getAPI(app);
61+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
62+
const dv = (app.plugins.getPlugin('obsidian-dataview') as any).api as DataviewApi | undefined;
6263
const optionArgs = inputField.base.getArguments(InputFieldArgumentType.OPTION);
6364
const optionQueryArgs = inputField.base.getArguments(InputFieldArgumentType.OPTION_QUERY);
6465
const useLinksArgs = inputField.base.getArgument(InputFieldArgumentType.USE_LINKS);

src/fields/viewFields/AbstractViewField.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { type ViewFieldDeclaration } from '../../parsers/viewFieldParser/ViewFie
22
import { type ViewFieldMDRC, type ViewFieldVariable } from '../../renderChildren/ViewFieldMDRC';
33

44
import { ViewFieldArgumentType } from '../../config/FieldConfigs';
5+
import { stringifyUnknown } from '../../utils/Literal';
56

67
export abstract class AbstractViewField {
78
protected renderChild: ViewFieldMDRC;
@@ -42,20 +43,10 @@ export abstract class AbstractViewField {
4243
return;
4344
}
4445

45-
try {
46-
const text = value?.toString() ?? '';
47-
48-
if (!this.hidden) {
49-
this.container.empty();
50-
await this._update(this.container, text);
51-
}
52-
this.container.removeClass('mb-error');
53-
} catch (e) {
54-
if (e instanceof Error) {
55-
console.error(e);
56-
this.container.innerText = e.message;
57-
this.container.addClass('mb-error');
58-
}
46+
if (!this.hidden) {
47+
const text = stringifyUnknown(value, this.renderChild.plugin.settings.viewFieldDisplayNullAsEmpty) ?? '';
48+
this.container.empty();
49+
await this._update(this.container, text);
5950
}
6051
}
6152

src/fields/viewFields/fields/MathVF.ts

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,24 @@ import { AbstractViewField } from '../AbstractViewField';
22
import { type ViewFieldDeclaration } from '../../../parsers/viewFieldParser/ViewFieldDeclaration';
33
import { type ViewFieldMDRC, type ViewFieldVariable } from '../../../renderChildren/ViewFieldMDRC';
44
import { ErrorLevel, MetaBindExpressionError } from '../../../utils/errors/MetaBindErrors';
5-
// import { type EvalFunction, compile as MathJsCompile } from 'mathjs';
65
import { Signal } from '../../../utils/Signal';
76
import { getUUID } from '../../../utils/Utils';
7+
import { compile as MathJsCompile, type EvalFunction } from 'mathjs';
88

99
export class MathVF extends AbstractViewField {
1010
container?: HTMLElement;
11-
// expression?: EvalFunction;
11+
expression?: EvalFunction;
1212
expressionStr?: string;
13+
hasError: boolean;
1314

1415
hidden: boolean;
1516

1617
constructor(renderChild: ViewFieldMDRC) {
1718
super(renderChild);
1819

1920
this.hidden = false;
21+
22+
this.hasError = false;
2023
}
2124

2225
public buildVariables(declaration: ViewFieldDeclaration): ViewFieldVariable[] {
@@ -43,15 +46,20 @@ export class MathVF extends AbstractViewField {
4346
}
4447
}
4548

46-
// this.expression = MathJsCompile(this.expressionStr);
49+
this.expression = MathJsCompile(this.expressionStr);
4750

4851
return variables;
4952
}
5053

51-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
52-
protected _render(container: HTMLElement): void {}
54+
protected _render(_container: HTMLElement): void {}
5355

5456
protected _update(container: HTMLElement, text: string): void {
57+
// console.log('hasError', this.hasError);
58+
if (this.hasError) {
59+
container.addClass('mb-error');
60+
} else {
61+
container.removeClass('mb-error');
62+
}
5563
container.innerText = text;
5664
}
5765

@@ -70,37 +78,51 @@ export class MathVF extends AbstractViewField {
7078
return context;
7179
}
7280

73-
async computeValue(variables: ViewFieldVariable[]): Promise<string> {
74-
// if (!this.expression) {
75-
// throw new MetaBindExpressionError({
76-
// errorLevel: ErrorLevel.ERROR,
77-
// effect: 'failed to evaluate expression',
78-
// cause: 'expression is undefined',
79-
// });
80-
// }
81+
computeValue(variables: ViewFieldVariable[]): string {
82+
this.hasError = false;
83+
84+
if (!this.expression) {
85+
return this.handleComputeError(
86+
new MetaBindExpressionError({
87+
errorLevel: ErrorLevel.ERROR,
88+
effect: 'failed to evaluate expression',
89+
cause: 'expression is undefined',
90+
}),
91+
);
92+
}
8193

8294
const context = this.buildContext(variables);
8395
try {
84-
// return this.expression.evaluate(context) as Promise<string>;
85-
return 'mathjs is disabled';
96+
// eslint-disable-next-line
97+
return this.expression.evaluate(context).toString();
8698
} catch (e) {
8799
if (e instanceof Error) {
88-
throw new MetaBindExpressionError({
89-
errorLevel: ErrorLevel.ERROR,
90-
effect: `failed to evaluate expression`,
91-
cause: e,
92-
context: {
93-
expression: this.expressionStr,
94-
context: context,
95-
},
96-
});
100+
return this.handleComputeError(
101+
new MetaBindExpressionError({
102+
errorLevel: ErrorLevel.ERROR,
103+
effect: `failed to evaluate expression`,
104+
cause: e,
105+
context: {
106+
expression: this.expressionStr,
107+
context: context,
108+
},
109+
}),
110+
);
97111
} else {
98-
throw new Error('failed to evaluate js expression because of: unexpected thrown value');
112+
return this.handleComputeError(
113+
new Error('failed to evaluate js expression because of: unexpected thrown value'),
114+
);
99115
}
100116
}
101117
}
102118

103119
public getDefaultDisplayValue(): string {
104120
return '';
105121
}
122+
123+
private handleComputeError(e: Error): string {
124+
this.hasError = true;
125+
console.warn(e);
126+
return e.message;
127+
}
106128
}

src/utils/Utils.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,32 @@ export function isUrl(str: string): boolean {
124124
return false;
125125
}
126126
}
127+
128+
// inspired by https://stackoverflow.com/a/48764436
129+
export class DecimalPrecision {
130+
static round(value: number, decimalPlaces: number = 0): number {
131+
const p = Math.pow(10, decimalPlaces || 0);
132+
const n = value * p * (1 + Number.EPSILON);
133+
return Math.round(n) / p;
134+
}
135+
136+
static ceil(value: number, decimalPlaces: number = 0): number {
137+
const p = Math.pow(10, decimalPlaces || 0);
138+
const n = value * p * (1 - Math.sign(value) * Number.EPSILON);
139+
return Math.ceil(n) / p;
140+
}
141+
142+
static floor(value: number, decimalPlaces: number = 0): number {
143+
const p = Math.pow(10, decimalPlaces || 0);
144+
const n = value * p * (1 + Math.sign(value) * Number.EPSILON);
145+
return Math.floor(n) / p;
146+
}
147+
148+
static trunc(value: number, decimalPlaces: number = 0): number {
149+
return value < 0 ? DecimalPrecision.ceil(value, decimalPlaces) : DecimalPrecision.floor(value, decimalPlaces);
150+
}
151+
152+
static toFixed(value: number, decimalPlaces: number = 0): string {
153+
return DecimalPrecision.round(value, decimalPlaces).toFixed(decimalPlaces || 0);
154+
}
155+
}

0 commit comments

Comments
 (0)