Skip to content

Commit

Permalink
feat!: Dataview field lookups are now based on the **current active f…
Browse files Browse the repository at this point in the history
…ile**, not a cached set of fields.
  • Loading branch information
valentine195 committed Apr 22, 2024
1 parent 5b6210f commit d424df9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 53 deletions.
57 changes: 9 additions & 48 deletions src/api/api.dataview.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, type App } from "obsidian";
import { Component, TFile, type App } from "obsidian";
import { getAPI } from "obsidian-dataview";
import type { DvAPIInterface } from "obsidian-dataview/lib/typings/api";
import { Lexer } from "src/lexer/lexer";
Expand All @@ -16,61 +16,22 @@ class DVManager extends Component {
api: DvAPIInterface;

inline: Map<string, number> = new Map();
async registerDataviewInlineFields() {
if (!this.canUseDataview) return;

await this.dataviewReady();
ready: boolean = false;

const pages = this.api.index.pages;

pages.forEach(({ fields }) => {
for (const [key, value] of fields) {
if (
typeof value !== "number" ||
Number.isNaN(value) ||
value == undefined
)
continue;
this.inline.set(key, value);
}
});

Lexer.setInlineFields(this.inline);
this.registerEvent(
this.app.metadataCache.on(
"dataview:metadata-change",
(type, file) => {
if (type === "update") {
const page = this.api.page(file.path);

if (!page) return;

for (let key in page) {
let value = page[key];
if (
typeof value !== "number" ||
Number.isNaN(value) ||
value == undefined
)
continue;
this.inline.set(key, value);
}
Lexer.setInlineFields(this.inline);
}
}
)
);
}
initialize(app: App) {
this.app = app;
this.api = getAPI();

this.app.workspace.onLayoutReady(async () => {
await this.registerDataviewInlineFields();
});
this.dataviewReady().then(() => (this.ready = true));
return this;
}

getFieldValueFromFile(field: string, file: TFile): string | null {
if (!this.canUseDataview || !this.ready) return null;

return this.api.index.pages.get(file.path)?.fields.get(field) ?? null;
}

get canUseDataview() {
return this.app.plugins.getPlugin("dataview") != null;
}
Expand Down
19 changes: 15 additions & 4 deletions src/lexer/lexer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/* import lexer from "lex"; */

import * as moo from "moo";
import type { App } from "obsidian";
import { API } from "src/api/api";
import { DataviewManager } from "src/api/api.dataview";
import type { Conditional } from "src/roller";

export const TAG_REGEX =
Expand Down Expand Up @@ -108,6 +111,7 @@ export interface LexicalToken extends Partial<moo.Token> {
}

class LexerClass {
app: App;
constructor() {
this.parser = new Parser({
"+": {
Expand All @@ -132,6 +136,10 @@ class LexerClass {
}
});
}
initialize(app: App): LexerClass {
this.app = app;
return this;
}
lexer = moo.compile({
WS: [{ match: /[ \t]+/u }, { match: /[{}]+/u }],
table: TABLE_REGEX,
Expand Down Expand Up @@ -181,10 +189,13 @@ class LexerClass {
{
match: /\b[A-Za-z][A-Za-z0-9_]+\b/u,
value: (match) => {
if (this.inline.has(match)) {
return `${this.inline.get(match)}`;
}
return match;
const file = this.app.workspace.getActiveFile();
if (!file) return match;

return (
DataviewManager.getFieldValueFromFile(match, file) ??
match
);
}
}
],
Expand Down
5 changes: 4 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ export default class DiceRollerPlugin extends Plugin {
this.register(() => delete window["DiceRoller"]);
this.addChild(DataviewManager.initialize(this.app));

Lexer.setDefaults(this.data.defaultFace, this.data.defaultRoll);
Lexer.initialize(this.app).setDefaults(
this.data.defaultFace,
this.data.defaultRoll
);

this.addSettingTab(new SettingTab(this.app, this));

Expand Down

0 comments on commit d424df9

Please sign in to comment.