diff --git a/expenseManipulation.ts b/expenseManipulation.ts index 1a578c7..ad9839d 100644 --- a/expenseManipulation.ts +++ b/expenseManipulation.ts @@ -1,31 +1,46 @@ -import { App, TFile } from "obsidian"; +import { App, MarkdownView } 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; - } - }); + const file = app.workspace.getActiveFile(); + const fileContent = await this.app.vault.read(file); + const lines = fileContent.split("\n"); + let inTable = false; + const tableLines = lines + .map((line: string) => { + if (/^\|.*\|$/.test(line)) { + 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: string) => cell.trim()) + .filter((cell: string) => cell !== ""); + return cells.reduce( + ( + acc: Record, + cell: string, + index: number + ) => { + acc["cell" + (index + 1)] = cell; + return acc; + }, + { date: new Date(cells[0]), line } + ); + } } - } - }); + }) + .filter(Boolean); // Remove undefined values + + // Sort table lines by date + tableLines.sort((a, b) => a.date - b.date); + const sortedLines = tableLines.map((obj) => obj.line); + console.log(sortedLines); + // set the cursor at the end of the document + const editor = this.app.workspace.getActiveViewOfType(MarkdownView); + // write hello in the start of the document + editor.editor.setCursor(2, 0); + editor.editor.replaceSelection(sortedLines.join("\n")); + editor.editor.replaceSelection("\n\n"); } diff --git a/main.ts b/main.ts index bf6738c..274c689 100644 --- a/main.ts +++ b/main.ts @@ -32,6 +32,7 @@ export const DEFAULT_SETTINGS: BudgetSettings = { export default class budgetPlugin extends Plugin { settings: BudgetSettings; + file: TFile; async onload() { await this.loadSettings(); @@ -61,9 +62,12 @@ export default class budgetPlugin extends Plugin { // Adds a setting tag so the user can configure the aspects of the plugin 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); + this.addCommand({ + id: "play-with-table", + name: "Play with table", + callback: async () => { + await playWithTable(this.app); + }, }); }