Skip to content
This repository was archived by the owner on Jul 28, 2025. It is now read-only.

Commit dee256a

Browse files
committed
first steps with preview table
1 parent 8244948 commit dee256a

File tree

1 file changed

+54
-16
lines changed

1 file changed

+54
-16
lines changed

src/services/MarkdownPostProcessorService.ts

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import DBFolderPlugin from "main";
33
import { MarkdownPostProcessorContext } from "obsidian";
44
import DatabaseInfo from "services/DatabaseInfo";
55
import { VaultManagerDB } from "services/FileManagerService";
6-
6+
import { DataviewService } from "services/DataviewService";
7+
import { obtainColumnsFromFolder, obtainMetadataColumns } from "components/Columns";
8+
import { adapterTFilesToRows } from "helpers/VaultManagement";
9+
import { DatabaseColumn } from "cdm/DatabaseModel";
710
/**
811
* Keep info about a note and offer methods to manipulate it
912
*/
@@ -70,18 +73,9 @@ export class PreviewDatabaseModeService {
7073
return;
7174
}
7275
el.empty();
73-
74-
const dbFile = VaultManagerDB.obtainTfileFromFilePath(ctx.sourcePath);
75-
const databaseDisk = new DatabaseInfo(dbFile);
76-
await databaseDisk.initDatabaseconfigYaml(
77-
this.plugin.settings.local_settings
78-
);
79-
const div = createDiv();
80-
div.textContent = `${databaseDisk
81-
.yaml.description}`;
82-
el.appendChild(div);
76+
const previewContainer = await this.renderPreview(el, ctx);
8377
setTimeout(async () => {
84-
let internalEmbedDiv: HTMLElement = div;
78+
let internalEmbedDiv: HTMLElement = el;
8579
while (
8680
!internalEmbedDiv.hasClass("internal-embed") &&
8781
internalEmbedDiv.parentElement
@@ -91,12 +85,13 @@ export class PreviewDatabaseModeService {
9185

9286
if (!internalEmbedDiv.hasClass("internal-embed")) {
9387
el.empty();
94-
el.appendChild(div);
88+
el.appendChild(previewContainer);
9589
return;
9690
}
9791

9892
internalEmbedDiv.empty();
99-
93+
const previewTimeoutContainer = await this.renderPreview(internalEmbedDiv, ctx);
94+
console.log("internalEmbedDiv");
10095
//timer to avoid the image flickering when the user is typing
10196
let timer: NodeJS.Timeout = null;
10297
const observer = new MutationObserver((m) => {
@@ -108,15 +103,58 @@ export class PreviewDatabaseModeService {
108103
}
109104
timer = setTimeout(() => {
110105
timer = null;
111-
internalEmbedDiv.empty();
106+
previewTimeoutContainer.empty();
112107
}, 500);
113108
});
114-
observer.observe(internalEmbedDiv, {
109+
observer.observe(previewTimeoutContainer, {
115110
attributes: true, //configure it to listen to attribute changes
116111
});
117112
}, 300);
118113
};
119114

115+
private renderPreview = async (
116+
el: HTMLElement,
117+
ctx: MarkdownPostProcessorContext,
118+
): Promise<HTMLElement> => {
119+
const dbFile = VaultManagerDB.obtainTfileFromFilePath(ctx.sourcePath);
120+
const databaseDisk = new DatabaseInfo(dbFile);
121+
await databaseDisk.initDatabaseconfigYaml(
122+
this.plugin.settings.local_settings
123+
);
124+
125+
const div = createDiv();
126+
div.textContent = `${databaseDisk
127+
.yaml.description}`;
128+
el.appendChild(div);
129+
let yamlColumns: Record<string, DatabaseColumn> =
130+
databaseDisk.yaml.columns;
131+
// Complete the columns with the metadata columns
132+
yamlColumns = await obtainMetadataColumns(
133+
yamlColumns,
134+
databaseDisk.yaml.config
135+
);
136+
// Obtain base information about columns
137+
const columns = await obtainColumnsFromFolder(yamlColumns);
138+
const rows = await adapterTFilesToRows(
139+
dbFile.parent.path,
140+
columns,
141+
databaseDisk.yaml.filters
142+
);
143+
const dataviewCols: string[] = columns.map((c) => c.key);
144+
const dataviewMatrixRow: any[][] = rows.map((r) =>
145+
dataviewCols.map((c) => r[c])
146+
);
147+
148+
DataviewService.getDataviewAPI().table(
149+
dataviewCols,
150+
dataviewMatrixRow,
151+
div,
152+
null,
153+
ctx.sourcePath
154+
)
155+
return div;
156+
}
157+
120158
public static getInstance(plugin?: DBFolderPlugin): PreviewDatabaseModeService {
121159
if (!this.instance) {
122160
this.instance = new PreviewDatabaseModeService(plugin);

0 commit comments

Comments
 (0)