implemented basic table reading and trigger for expenseManipulation

This commit is contained in:
steeven 2024-04-14 11:31:24 +02:00
parent 7b8b9309ef
commit efee00c529
2 changed files with 38 additions and 0 deletions

31
expenseManipulation.ts Normal file
View File

@ -0,0 +1,31 @@
import { App, TFile } from "obsidian";
export async function playWithTable(app: App) {
const file = await this.app.workspace.getActiveFile();
// trigger if something changed in the file (ex. new expense added)
app.vault.on("modify", async (changedFile: TFile) => {
if (changedFile.path === file.path) {
if (file) {
const fileContent = await this.app.vault.read(file);
const lines = fileContent.split("\n");
let inTable = false;
lines.forEach((line) => {
if (line.startsWith("|")) {
if (line.includes("---")) {
// This is the header line, start reading from next line
inTable = true;
} else if (inTable) {
// This is a table line, read the cells
const cells = line
.split("|")
.map((cell) => cell.trim());
console.log(cells);
}
} else {
inTable = false;
}
});
}
}
});
}

View File

@ -1,6 +1,7 @@
import test from "node:test"; import test from "node:test";
import { App, Notice, Plugin, PluginSettingTab, Setting } from "obsidian"; import { App, Notice, Plugin, PluginSettingTab, Setting } from "obsidian";
import { ExpenseModal } from "./expenseModal"; import { ExpenseModal } from "./expenseModal";
import { playWithTable } from "./expenseManipulation";
interface BudgetSettings { interface BudgetSettings {
expenseCategories: object; expenseCategories: object;
@ -56,8 +57,14 @@ export default class budgetPlugin extends Plugin {
// need to set active md file to budget // need to set active md file to budget
console.log("Debug: trigger new expense modal from ribbon"); console.log("Debug: trigger new expense modal from ribbon");
}); });
// Adds a setting tag so the user can configure the aspects of the plugin // Adds a setting tag so the user can configure the aspects of the plugin
this.addSettingTab(new ExpenseSettingTab(this.app, this)); this.addSettingTab(new ExpenseSettingTab(this.app, this));
// Adds a listener to the layout-ready event to trigger the playWithTable function
this.app.workspace.on("layout-ready", async () => {
await playWithTable(this.app);
});
} }
async loadSettings(): Promise<void> { async loadSettings(): Promise<void> {