1+ import { DB_ICONS } from "helpers/Constants" ;
2+ import DBFolderPlugin from "main" ;
3+
4+ import {
5+ DatabaseView ,
6+ } from 'DatabaseView' ;
7+ import { LOGGER } from "services/Logger" ;
8+ import { DataQueryResult , ProjectView , ProjectViewProps } from "obsidian-projects-types" ;
9+ import { resolve_tfile , resolve_tfolder } from "helpers/FileManagement" ;
10+ import { generateDbConfiguration , generateNewDatabase } from "helpers/CommandsHelper" ;
11+
12+ class ProjectAPI extends ProjectView {
13+ private plugin : DBFolderPlugin ;
14+ private view : DatabaseView ;
15+
16+ constructor ( plugin : DBFolderPlugin ) {
17+ super ( ) ;
18+ this . plugin = plugin ;
19+ }
20+ dataEl ?: HTMLElement ;
21+
22+ getViewType ( ) : string {
23+ return "dbfolder-view" ;
24+ }
25+
26+ getDisplayName ( ) : string {
27+ return "Database Folder" ;
28+ }
29+
30+ getIcon ( ) : string {
31+ return DB_ICONS . NAME ;
32+ }
33+
34+ async onData ( { data } : DataQueryResult ) {
35+ // Do nothing here
36+ // Data is handled by the database itself and will be updated automatically every time the view is opened
37+
38+
39+ }
40+
41+ // onOpens is called whenever the user activates your view.
42+ //
43+ // `contentEl` HTML element where you can attach your view.
44+ // `config` JSON object with optional view configuration.
45+ // `saveConfig` Callback to save configuration changes.
46+ async onOpen ( { contentEl, config, saveConfig, project, viewId } : ProjectViewProps ) {
47+ const { path } = project ;
48+ let filePath = config . filepath ;
49+ if ( ! filePath ) {
50+ const folder = resolve_tfolder ( path ) ;
51+ // If the config is empty, we need to create a Default
52+ const dbConfig = generateDbConfiguration ( this . plugin . settings . local_settings ) ;
53+ await generateNewDatabase ( dbConfig , folder , `${ viewId } _db` , false ) ;
54+ saveConfig ( { filepath : `${ path } /${ viewId } _db.md` } ) ;
55+ }
56+ const leaf = app . workspace . getLeaf ( ) ;
57+ const file = resolve_tfile ( filePath ) ;
58+ this . view = new DatabaseView ( leaf , this . plugin , file ) ;
59+ this . view . initRootContainer ( file ) ;
60+ await this . view . initDatabase ( ) ;
61+ LOGGER . debug ( "Database initialized successfully from project view" ) ;
62+ this . dataEl = contentEl . createDiv ( ) . appendChild ( this . view . containerEl ) ;
63+ }
64+
65+ async onClose ( ) {
66+ LOGGER . debug ( "Closing project view " , this . getDisplayName ( ) ) ;
67+ }
68+ }
69+
70+ export default ProjectAPI ;
0 commit comments