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

Commit d2f5342

Browse files
committed
Merge branch '48-fr-create-new-filerow-from-template'
2 parents 5d1d19d + 52118a5 commit d2f5342

30 files changed

+587
-52
lines changed

docs/docs/changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## 1.8.0
22
### Shiny new things
33
- New source option: query. Use your own dataview query as source of the database. Start the query with the `FROM` term, since the plugin will autocomplete the beginning with `TABLE` and the column fields[ISSUE#156](https://github.com/RafaelGB/obsidian-db-folder/issues/156)
4+
- Templates for new rows added! now you can choose a template folder on settings menu, then you can choose your template file easily near of the add row button[ISSUE#48](https://github.com/RafaelGB/obsidian-db-folder/issues/48)
45
### Improved
56
- Inline fields now supports render embed images with ![[note]] syntax. [ISSUE#136](https://github.com/RafaelGB/obsidian-db-folder/issues/136)
67
### No longer broken

src/Settings.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { media_settings_section } from "settings/MediaSection";
1212
import { source_settings_section } from "settings/SourceSection";
1313
import { DatabaseSettings } from "cdm/SettingsModel";
1414
import editing_engine_settings_section from "settings/EditingEngineSection";
15+
import rows_settings_section from "settings/RowsSection";
1516

1617
export type SettingRetriever = <K extends keyof DatabaseSettings>(
1718
key: K,
@@ -81,6 +82,8 @@ export class SettingsManager {
8182
}
8283
/** Columns section */
8384
columns_settings_section.run(settingHandlerResponse);
85+
/** Rows section */
86+
rows_settings_section.run(settingHandlerResponse);
8487
/** Editing engine section */
8588
editing_engine_settings_section.run(settingHandlerResponse);
8689
/** Media section */

src/cdm/FolderModel.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ export type RelationshipProps = {
138138
backgroundColor: string
139139
}
140140

141+
export type RowTemplateOption = {
142+
label: string,
143+
value: string
144+
}
145+
141146
export type NoteContentAction = {
142147
file: TFile,
143148
action: string,

src/cdm/SettingsModel.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export interface LocalSettings {
2323
source_data: string;
2424
source_form_result: string;
2525
frontmatter_quote_wrap: boolean;
26+
row_templates_folder: string;
27+
current_row_template: string;
2628
}
2729

2830
export interface DatabaseSettings {

src/components/Table.tsx

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ import {
1010
Column,
1111
} from "react-table";
1212
import { DragDropContext, Droppable, Draggable } from "react-beautiful-dnd";
13-
import { TableDataType, RowDataType, TableColumn } from "cdm/FolderModel";
13+
import {
14+
TableDataType,
15+
RowDataType,
16+
TableColumn,
17+
RowTemplateOption,
18+
} from "cdm/FolderModel";
1419
import { DatabaseView } from "DatabaseView";
1520
import StateManager from "StateManager";
1621
import { getNormalizedPath } from "helpers/VaultManagement";
@@ -28,7 +33,10 @@ import { c, getTotalWidth } from "helpers/StylesHelper";
2833
import { HeaderNavBar } from "components/NavBar";
2934
import { getColumnsWidthStyle } from "components/styles/ColumnWidthStyle";
3035
import { HeaderContext } from "components/contexts/HeaderContext";
31-
import { getDndListStyle, getDndItemStyle } from "./styles/DnDStyle";
36+
import { getDndListStyle, getDndItemStyle } from "components/styles/DnDStyle";
37+
import CustomTemplateSelectorStyles from "components/styles/RowTemplateStyles";
38+
import Select, { ActionMeta, OnChangeValue } from "react-select";
39+
import { get_tfiles_from_folder } from "helpers/FileManagement";
3240

3341
const defaultColumn = {
3442
minWidth: 25,
@@ -229,7 +237,32 @@ export function Table(tableData: TableDataType) {
229237
globalFilter: (state as any).globalFilter,
230238
setGlobalFilter: setGlobalFilter,
231239
};
240+
// Manage Templates
241+
const [rowTemplateState, setRowTemplateState] = React.useState(
242+
view.diskConfig.yaml.config.current_row_template
243+
);
244+
const rowTemplatesOptions = get_tfiles_from_folder(
245+
view.diskConfig.yaml.config.row_templates_folder
246+
).map((tfile) => {
247+
return {
248+
value: tfile.path,
249+
label: tfile.path,
250+
};
251+
});
252+
253+
function handleChangeRowTemplate(
254+
newValue: OnChangeValue<RowTemplateOption, false>,
255+
actionMeta: ActionMeta<RowTemplateOption>
256+
) {
257+
const settingsValue = !!newValue ? newValue.value : "";
258+
dataDispatch({
259+
type: ActionTypes.CHANGE_ROW_TEMPLATE,
260+
template: settingsValue,
261+
});
262+
setRowTemplateState(settingsValue);
263+
}
232264
LOGGER.debug(`<= Table`);
265+
233266
return (
234267
<>
235268
<div
@@ -445,6 +478,23 @@ export function Table(tableData: TableDataType) {
445478
</span>
446479
New
447480
</div>
481+
<div className={`${c("padding-left")}`}>
482+
<Select
483+
styles={CustomTemplateSelectorStyles}
484+
options={rowTemplatesOptions}
485+
value={{
486+
label: rowTemplateState,
487+
value: rowTemplateState,
488+
}}
489+
isClearable={true}
490+
isMulti={false}
491+
onChange={handleChangeRowTemplate}
492+
defaultValue={{ label: "Choose a Template", value: "None" }}
493+
menuPortalTarget={document.body}
494+
menuShouldBlockScroll={true}
495+
isSearchable
496+
/>
497+
</div>
448498
</div>
449499
</div>
450500
</div>

src/components/portals/TagsPortal.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { RowSelectOption, TagsProps } from "cdm/ComponentsModel";
22
import Relationship from "components/RelationShip";
33
import CustomTagsStyles from "components/styles/TagsStyles";
44
import CreatableSelect from "react-select/creatable";
5-
import { grey, randomColor } from "helpers/Colors";
5+
import { randomColor } from "helpers/Colors";
66
import React, { useState } from "react";
77
import { ActionMeta, OnChangeValue } from "react-select";
88
import { c } from "helpers/StylesHelper";

src/components/reducers/DatabaseDispatch.tsx

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { obtainUniqueOptionValues } from "helpers/SelectHelper";
2626
import { Literal } from "obsidian-dataview/lib/data-model/value";
2727
import { DateTime } from "luxon";
2828
import { RowSelectOption } from "cdm/ComponentsModel";
29+
import { viewport } from "@popperjs/core";
2930

3031
export function databaseReducer(state: TableDataType, action: ActionType) {
3132
LOGGER.debug(`<=>databaseReducer action: ${action.type}`, action);
@@ -80,7 +81,8 @@ export function databaseReducer(state: TableDataType, action: ActionType) {
8081
VaultManagerDB.create_markdown_file(
8182
state.view.file.parent,
8283
action.filename,
83-
rowRecord
84+
rowRecord,
85+
state.view.diskConfig.yaml.config
8486
);
8587
const metadata: Record<string, Literal> = {};
8688
metadata[MetadataColumns.CREATED] = DateTime.now();
@@ -106,6 +108,27 @@ export function databaseReducer(state: TableDataType, action: ActionType) {
106108
rows: { $push: [row] },
107109
},
108110
});
111+
/**
112+
* Add new row into table
113+
*/
114+
case ActionTypes.CHANGE_ROW_TEMPLATE:
115+
state.view.diskConfig.updateConfig(
116+
"current_row_template",
117+
action.template
118+
);
119+
return update(state, {
120+
view: {
121+
diskConfig: {
122+
yaml: {
123+
config: {
124+
current_row_template: {
125+
$set: action.template,
126+
},
127+
},
128+
},
129+
},
130+
},
131+
});
109132
/**
110133
* Modify column label of its header.
111134
* Update key of column in all rows
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { GroupBase, StylesConfig } from "react-select";
2+
3+
const CustomTemplateSelectorStyles: StylesConfig<any, true, GroupBase<any>> = {
4+
option: (styles, { data, isDisabled, isFocused, isSelected }) => {
5+
return {
6+
...styles,
7+
fontSize: '12px',
8+
textAlign: 'left',
9+
width: 'auto',
10+
}
11+
},
12+
}
13+
14+
export default CustomTemplateSelectorStyles;

src/errors/HelperError.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { DbFolderError } from 'errors/AbstractError';
2+
3+
/**
4+
* Custom error for parser yaml of dbfolder
5+
*/
6+
export class HelperError extends DbFolderError {
7+
constructor(message: string) {
8+
super(message, {});
9+
}
10+
}

src/helpers/Constants.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { DatabaseSettings } from "cdm/SettingsModel";
66
export const ActionTypes = Object.freeze({
77
ADD_OPTION_TO_COLUMN: 'add_option_to_column',
88
ADD_ROW: 'add_row',
9+
CHANGE_ROW_TEMPLATE: 'change_row_template',
910
UPDATE_COLUMN_TYPE: 'update_column_type',
1011
UPDATE_COLUMN_LABEL: 'update_column_label',
1112
UPDATE_CELL: 'update_cell',
@@ -137,7 +138,8 @@ export const TableColumnsTemplate: Partial<TableColumn> =
137138
}
138139

139140
export const DatabaseCore = Object.freeze({
140-
FRONTMATTER_KEY: 'database-plugin'
141+
FRONTMATTER_KEY: 'database-plugin',
142+
DATAVIEW_FILE: 'file',
141143
});
142144

143145
export const DatabaseFrontmatterOptions = Object.freeze({
@@ -277,6 +279,8 @@ export const DEFAULT_SETTINGS: DatabaseSettings = {
277279
show_metadata_tasks: false,
278280
source_data: SourceDataTypes.CURRENT_FOLDER,
279281
source_form_result: 'root',
280-
frontmatter_quote_wrap: false
282+
frontmatter_quote_wrap: false,
283+
row_templates_folder: '/',
284+
current_row_template: '',
281285
}
282286
};

0 commit comments

Comments
 (0)