improve random table styling

This commit is contained in:
isaprettycoolguy@protonmail.com 2026-03-16 18:55:06 -04:00
parent 2447c12568
commit 2ebee21308
2 changed files with 70 additions and 0 deletions

View file

@ -268,6 +268,31 @@ export class RandomTableView extends ItemView {
}),
);
// ── Auto-sync: note renamed/deleted in a linked folder → rebuild table entries ──
this.registerEvent(
this.app.vault.on("rename", async (file, oldPath) => {
if (!(file instanceof TFile)) return;
const oldDir = normalizeFolder(oldPath.slice(0, oldPath.lastIndexOf("/")));
const newDir = normalizeFolder(file.parent?.path ?? "");
for (const dir of new Set([oldDir, newDir])) {
const tableFilePath = this.linkedFolderMap.get(dir);
if (!tableFilePath) continue;
const tableFile = this.app.vault.getAbstractFileByPath(tableFilePath);
if (tableFile instanceof TFile) await this.autoSyncLinkedFolder(tableFile);
}
}),
);
this.registerEvent(
this.app.vault.on("delete", async (file) => {
if (!(file instanceof TFile)) return;
const dir = normalizeFolder(file.path.slice(0, file.path.lastIndexOf("/")));
const tableFilePath = this.linkedFolderMap.get(dir);
if (!tableFilePath) return;
const tableFile = this.app.vault.getAbstractFileByPath(tableFilePath);
if (tableFile instanceof TFile) await this.autoSyncLinkedFolder(tableFile);
}),
);
// ── Live sync: note created in a linked folder → add entry to table ──
this.registerEvent(
this.app.vault.on("create", async (createdFile) => {
@ -707,9 +732,46 @@ export class RandomTableView extends ItemView {
.forEach((el) => {
el.toggleClass("is-active", el.title === file.path);
});
await this.autoSyncLinkedFolder(file);
await this.renderDetail();
}
/**
* If tableFile has a linked-folder, rebuild its entries from the folder:
* keep existing entries (preserving weights), add new notes, remove stale ones.
* No-op when there's no linked folder or nothing has changed.
*/
private async autoSyncLinkedFolder(tableFile: TFile): Promise<void> {
const content = await this.app.vault.read(tableFile);
const table = parseRandomTable(content);
if (!table.linkedFolder) return;
const lf = normalizeFolder(table.linkedFolder);
const folderFiles = this.app.vault.getMarkdownFiles()
.filter(f => f.parent?.path === lf && !f.basename.startsWith("_"))
.sort((a, b) => a.basename.localeCompare(b.basename));
const folderBasenames = new Set(folderFiles.map(f => f.basename));
const currentNames = new Set(table.entries.map(e => e.result));
const hasNew = folderFiles.some(f => !currentNames.has(f.basename));
const hasStale = table.entries.some(e => !folderBasenames.has(e.result));
if (!hasNew && !hasStale) return;
const kept = table.entries.filter(e => folderBasenames.has(e.result));
const added = folderFiles
.filter(f => !currentNames.has(f.basename))
.map(f => ({ result: f.basename, weight: 1 }));
const newEntries = [...kept, ...added];
const rows = newEntries.map(e => `| ${e.result} | ${e.weight} |`).join("\n");
const updated = content.replace(
/(\| Result \| Weight \|\n\|[-| ]+\|\n)([\s\S]*)$/,
`$1${rows}\n`,
);
if (updated !== content) await this.app.vault.modify(tableFile, updated);
}
private renderDetailSeq = 0;
private async renderDetail(): Promise<void> {
if (!this.detailEl || !this.activeFile) return;
@ -779,6 +841,7 @@ export class RandomTableView extends ItemView {
cls: "duckmage-rt-collapse-btn",
});
descCollapseBtn.title = "Collapse description";
descHeader.createSpan({ text: "Description", cls: "duckmage-rt-section-label" });
const descBody = descSection.createDiv({ cls: "duckmage-rt-desc-body" });
MarkdownRenderer.render(this.app, table.description, descBody, file.path, this);
descCollapseBtn.addEventListener("click", () => {

View file

@ -1826,6 +1826,13 @@ div.duckmage-terrain-preview-icon {
align-items: center;
margin-bottom: 4px;
}
.duckmage-rt-section-label {
font-size: 0.8em;
color: var(--text-muted);
text-transform: uppercase;
letter-spacing: 0.05em;
user-select: none;
}
.duckmage-rt-desc-body {
font-size: 0.9em;
color: var(--text-muted);