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

Commit 92b9d62

Browse files
committed
autocreation of note
1 parent e1d7bd2 commit 92b9d62

File tree

3 files changed

+61
-42
lines changed

3 files changed

+61
-42
lines changed

src/api/obsidian-projects-api.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ import {
55
DatabaseView,
66
} from 'DatabaseView';
77
import { LOGGER } from "services/Logger";
8-
import { TFolder } from "obsidian";
98
import { DataQueryResult, ProjectView, ProjectViewProps } from "obsidian-projects-types";
10-
import { resolve_tfile } from "helpers/FileManagement";
9+
import { createDatabaseFile, resolve_tfile } from "helpers/FileManagement";
1110

1211
class ProjectAPI extends ProjectView {
1312
private plugin: DBFolderPlugin;
@@ -43,28 +42,26 @@ class ProjectAPI extends ProjectView {
4342
// `contentEl` HTML element where you can attach your view.
4443
// `config` JSON object with optional view configuration.
4544
// `saveConfig` Callback to save configuration changes.
46-
async onOpen({ contentEl, config, saveConfig, }: ProjectViewProps) {
47-
if (this.isConfigEmpty(config)) {
45+
async onOpen({ contentEl, config, saveConfig, project, viewId }: ProjectViewProps) {
46+
const { path } = project;
47+
let filePath = config.filepath;
48+
if (!filePath) {
4849
// If the config is empty, we need to create a Default
49-
const leaf = app.workspace.getLeaf();
50-
const file = resolve_tfile("pruebas.md");
51-
this.view = new DatabaseView(leaf, this.plugin, file);
52-
this.view.initRootContainer(file);
53-
this.view.initDatabase().then(() => {
54-
LOGGER.debug("Database initialized successfully from project view");
55-
this.dataEl = contentEl.createDiv().appendChild(this.view.containerEl);
56-
});
50+
filePath = await createDatabaseFile(path, `${viewId}_db`, this.plugin.settings.local_settings);
51+
saveConfig({ filepath: filePath });
5752
}
58-
53+
const leaf = app.workspace.getLeaf();
54+
const file = resolve_tfile(filePath);
55+
this.view = new DatabaseView(leaf, this.plugin, file);
56+
this.view.initRootContainer(file);
57+
await this.view.initDatabase();
58+
LOGGER.debug("Database initialized successfully from project view");
59+
this.dataEl = contentEl.createDiv().appendChild(this.view.containerEl);
5960
}
6061

6162
async onClose() {
6263
LOGGER.debug("Closing project view ", this.getDisplayName());
6364
}
64-
65-
private isConfigEmpty(config: Record<string, any>): boolean {
66-
return Object.keys(config).length === 0;
67-
}
6865
}
6966

7067
export default ProjectAPI;

src/helpers/FileManagement.ts

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,22 @@ import HelperException from "errors/HelperException";
44
import { normalizePath, TAbstractFile, TFile, TFolder, Vault } from "obsidian";
55
import { INLINE_POSITION, SourceDataTypes } from "helpers/Constants";
66
import { RowDataType } from "cdm/FolderModel";
7+
import { VaultManagerDB } from "services/FileManagerService";
78

8-
export function resolve_tfile(file_str: string): TFile {
9+
export function resolve_tfile(file_str: string, restrict: boolean = true): TFile {
910
file_str = normalizePath(file_str);
1011

1112
const file = app.vault.getAbstractFileByPath(file_str);
12-
if (!file) {
13+
if (!file && restrict) {
1314
throw new HelperException(`File "${file_str}" doesn't exist`);
1415
}
16+
1517
if (!(file instanceof TFile)) {
16-
throw new HelperException(`${file_str} is a folder, not a file`);
18+
if (restrict) {
19+
throw new HelperException(`${file_str} is a folder, not a file`);
20+
} else {
21+
return null;
22+
}
1723
}
1824

1925
return file;
@@ -152,3 +158,37 @@ export const resolveNewFilePath = ({
152158
.join("/");
153159
return `${folderPath}${subfolders ? `/${subfolders}` : ""}`;
154160
};
161+
162+
/**
163+
* Generate a new file with the structure of a database view
164+
* @param folderPath
165+
* @param filename
166+
* @param ddbbConfig
167+
* @returns
168+
*/
169+
export async function createDatabaseFile(
170+
folderPath: string,
171+
filename: string,
172+
ddbbConfig: LocalSettings
173+
): Promise<string> {
174+
let trimedFilename = filename.replace(/\.[^/.]+$/, "").trim();
175+
let filepath = `${folderPath}/${trimedFilename}.md`;
176+
// Validate possible duplicates
177+
let sufixOfDuplicate = 0;
178+
while (resolve_tfile(filepath, false)) {
179+
sufixOfDuplicate++;
180+
filepath = `${folderPath}/${trimedFilename}-${sufixOfDuplicate}.md`;
181+
}
182+
183+
if (sufixOfDuplicate > 0) {
184+
trimedFilename = `${trimedFilename}-${sufixOfDuplicate}`;
185+
filename = `${trimedFilename} copy(${sufixOfDuplicate})`;
186+
}
187+
// Add note to persist row
188+
await VaultManagerDB.create_markdown_file(
189+
resolve_tfolder(folderPath),
190+
trimedFilename,
191+
ddbbConfig
192+
);
193+
return filepath;
194+
}

src/stateManagement/data/handlers/AddRowHandlerAction.ts

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,18 @@
11
import { RowDataType, TableColumn } from "cdm/FolderModel";
22
import { LocalSettings } from "cdm/SettingsModel";
33
import { DataState, TableActionResponse } from "cdm/TableStateInterface";
4-
import { destination_folder, resolve_tfolder } from "helpers/FileManagement";
4+
import { createDatabaseFile, destination_folder } from "helpers/FileManagement";
55
import { DateTime } from "luxon";
66
import { Link } from "obsidian-dataview";
7-
import { VaultManagerDB } from "services/FileManagerService";
87
import NoteInfo from "services/NoteInfo";
98
import { AbstractTableAction } from "stateManagement/AbstractTableAction";
109

1110
export default class AddRowlHandlerAction extends AbstractTableAction<DataState> {
1211
handle(tableActionResponse: TableActionResponse<DataState>): TableActionResponse<DataState> {
13-
const { view, set, get, implementation } = tableActionResponse;
12+
const { view, set, implementation } = tableActionResponse;
1413
implementation.actions.addRow = async (filename: string, columns: TableColumn[], ddbbConfig: LocalSettings) => {
1514
const folderPath = destination_folder(view, ddbbConfig);
16-
let trimedFilename = filename.replace(/\.[^/.]+$/, "").trim();
17-
let filepath = `${folderPath}/${trimedFilename}.md`;
18-
// Validate possible duplicates
19-
let sufixOfDuplicate = 0;
20-
while (get().rows.find((row) => row.__note__.filepath === filepath)) {
21-
sufixOfDuplicate++;
22-
filepath = `${folderPath}/${trimedFilename}-${sufixOfDuplicate}.md`;
23-
}
24-
if (sufixOfDuplicate > 0) {
25-
trimedFilename = `${trimedFilename}-${sufixOfDuplicate}`;
26-
filename = `${trimedFilename} copy(${sufixOfDuplicate})`;
27-
}
28-
// Add note to persist row
29-
await VaultManagerDB.create_markdown_file(
30-
resolve_tfolder(folderPath),
31-
trimedFilename,
32-
ddbbConfig
33-
);
15+
const filepath = await createDatabaseFile(folderPath, filename, ddbbConfig);
3416

3517
const newNote = new NoteInfo({
3618
file: {
@@ -68,4 +50,4 @@ export default class AddRowlHandlerAction extends AbstractTableAction<DataState>
6850
tableActionResponse.implementation = implementation;
6951
return this.goNext(tableActionResponse);
7052
}
71-
}
53+
}

0 commit comments

Comments
 (0)