trigger le expensemanipulation depuis une commande et modifé expensmanipulation pour rééecrire un tableau sorted

This commit is contained in:
steeven 2024-04-21 18:15:16 +02:00
parent efee00c529
commit 36b1f17ae8
2 changed files with 49 additions and 30 deletions

View File

@ -1,31 +1,46 @@
import { App, TFile } from "obsidian"; import { App, MarkdownView } from "obsidian";
export async function playWithTable(app: App) { export async function playWithTable(app: App) {
const file = await this.app.workspace.getActiveFile(); const file = app.workspace.getActiveFile();
// trigger if something changed in the file (ex. new expense added) const fileContent = await this.app.vault.read(file);
app.vault.on("modify", async (changedFile: TFile) => { const lines = fileContent.split("\n");
if (changedFile.path === file.path) { let inTable = false;
if (file) { const tableLines = lines
const fileContent = await this.app.vault.read(file); .map((line: string) => {
const lines = fileContent.split("\n"); if (/^\|.*\|$/.test(line)) {
let inTable = false; if (line.includes("---")) {
lines.forEach((line) => { // This is the header line, start reading from next line
if (line.startsWith("|")) { inTable = true;
if (line.includes("---")) { } else if (inTable) {
// This is the header line, start reading from next line // This is a table line, read the cells
inTable = true; const cells = line
} else if (inTable) { .split("|")
// This is a table line, read the cells .map((cell: string) => cell.trim())
const cells = line .filter((cell: string) => cell !== "");
.split("|") return cells.reduce(
.map((cell) => cell.trim()); (
console.log(cells); acc: Record<string, string>,
} cell: string,
} else { index: number
inTable = false; ) => {
} 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");
} }

10
main.ts
View File

@ -32,6 +32,7 @@ export const DEFAULT_SETTINGS: BudgetSettings = {
export default class budgetPlugin extends Plugin { export default class budgetPlugin extends Plugin {
settings: BudgetSettings; settings: BudgetSettings;
file: TFile;
async onload() { async onload() {
await this.loadSettings(); 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 // 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.addCommand({
this.app.workspace.on("layout-ready", async () => { id: "play-with-table",
await playWithTable(this.app); name: "Play with table",
callback: async () => {
await playWithTable(this.app);
},
}); });
} }