Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

option to select binary checkbox states for integration with Tracker plugin #906

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/cdm/SettingsModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface LocalSettings {
show_metadata_inlinks: boolean;
show_metadata_outlinks: boolean;
show_metadata_tags: boolean;
binary_checkbox_type: boolean;
source_form_result: string;
source_destination_path: string;
source_data: string;
Expand Down
16 changes: 9 additions & 7 deletions src/components/cellTypes/CheckboxCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { TableColumn } from "cdm/FolderModel";
import { c } from "helpers/StylesHelper";
import { CellComponentProps } from "cdm/ComponentsModel";
import { ParseService } from "services/ParseService";
import { InputType } from "helpers/Constants";
import {DEFAULT_SETTINGS, InputType} from "helpers/Constants";
import {Notice} from "obsidian";

function CheckboxCell(props: CellComponentProps) {
const { defaultCell } = props;
Expand All @@ -15,22 +16,23 @@ function CheckboxCell(props: CellComponentProps) {
const checkboxRow = tableState.data((state) => state.rows[row.index]);
const columnsInfo = tableState.columns((state) => state.info);
const configInfo = tableState.configState((state) => state.info);
const isBinaryCellType = DEFAULT_SETTINGS.local_settings.binary_checkbox_type;
const checkboxCell = tableState.data(
(state) =>
ParseService.parseRowToCell(
state.rows[row.index],
tableColumn,
InputType.CHECKBOX,
isBinaryCellType ? InputType.NUMBER : InputType.CHECKBOX,
configInfo.getLocalSettings()
) as boolean
) as boolean | number
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can create an specific type for this

type Checkbox = number | boolean

);

const handleChange = async (event: React.ChangeEvent<HTMLInputElement>) => {
const newValue = event.target.checked;
editCheckbox(newValue);
editCheckbox( isBinaryCellType ? (newValue ? 1 : 0) : newValue );
};

const editCheckbox = async (newValue: boolean) => {
const editCheckbox = async (newValue: boolean | number) => {
const newCell = ParseService.parseRowToLiteral(
checkboxRow,
tableColumn,
Expand All @@ -55,13 +57,13 @@ function CheckboxCell(props: CellComponentProps) {
onKeyDown={(e) => {
if (e.key === "Enter") {
e.preventDefault();
editCheckbox(!checkboxCell);
editCheckbox(isBinaryCellType ? (checkboxCell ? 1 : 0) : !checkboxCell);
}
}}
>
<input
type="checkbox"
checked={checkboxCell}
checked={!!checkboxCell}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can use the service function of isTruthy avaliable in dataview api or dataviewService

key={`checkbox-input-${row.index}`}
onChange={handleChange}
/>
Expand Down
1 change: 1 addition & 0 deletions src/components/modals/columnSettings/ColumnSections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class BehaviorSetttingsSection extends AbstractChain<ColumnSettingsHandlerRespon
case InputType.INLINKS:
case InputType.OUTLINKS:
case InputType.METADATA_TAGS:
case InputType.CHECKBOX_TYPE:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the configuration as metadata info of the checkbox type I think it could be moved to column configuration, the idea of the flag to control the behavior its cool but I think it should be managed on columnConfig

// do nothing
break;
default:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AddColumnModalHandlerResponse } from "cdm/ModalsModel";
import { ColumnSettingsModal } from "components/modals/columnSettings/ColumnSettingsModal";
import { MetadataColumns } from "helpers/Constants";
import {DEFAULT_SETTINGS, MetadataColumns} from "helpers/Constants";
import { t } from "lang/helpers";
import { Setting } from "obsidian";
import { AbstractHandlerClass } from "patterns/chain/AbstractHandler";
Expand Down Expand Up @@ -133,6 +133,24 @@ export class MetadataToggleGroupHandler extends AbstractHandlerClass<AddColumnMo
.onChange(metadata_tags_toggle_promise)
);

/************************
* CHECKBOX TYPE COLUMN
************************/
const metadata_checkbox_type_toggle_promise = async (value: boolean): Promise<void> => {
// Persist value
view.diskConfig.updateConfig({ binary_checkbox_type: value });
addColumnModalManager.addColumnModal.enableReset = true;
DEFAULT_SETTINGS.local_settings.binary_checkbox_type = value;
}

new Setting(metadata_section)
.setName(t("settings_metatata_checkbox_type_toggle_title"))
.setDesc(t("settings_metatata_checkbox_type_toggle_desc"))
.addToggle(toggle =>
toggle.setValue(view.diskConfig.yaml.config.binary_checkbox_type)
.onChange(metadata_checkbox_type_toggle_promise)
);

return this.goNext(response);
}
}
16 changes: 16 additions & 0 deletions src/helpers/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const StaticInputType = Object.freeze({
INLINKS: 'inlinks',
OUTLINKS: 'outlinks',
METADATA_TAGS: 'metadata_tags',
CHECKBOX_TYPE: 'checkbox_type',
NEW_COLUMN: 'new_column',
});

Expand All @@ -54,6 +55,7 @@ export const MetadataColumns = Object.freeze({
INLINKS: `__inlinks__`,
ROW_CONTEXT_MENU: "__rowContextMenu__",
TAGS: `__tags__`,
CHECKBOX_TYPE: `__checkbox_type__`,
});

export const MetadataLabels = Object.freeze({
Expand All @@ -65,6 +67,7 @@ export const MetadataLabels = Object.freeze({
OUTLINKS: 'Outlinks',
INLINKS: 'Inlinks',
TAGS: 'File Tags',
CHECKBOX_TYPE: 'Checkbox Type',
});

export const PaginationRenderOptions = Object.freeze({
Expand Down Expand Up @@ -229,6 +232,18 @@ export const MetadataDatabaseColumns: MetadataColumnsModel = Object.freeze({
csvCandidate: false,
config: DEFAULT_COLUMN_CONFIG
},
CHECKBOX_TYPE: {
key: MetadataColumns.CHECKBOX_TYPE,
id: MetadataColumns.CHECKBOX_TYPE,
input: InputType.CHECKBOX_TYPE,
label: MetadataLabels.CHECKBOX_TYPE,
accessorKey: MetadataColumns.CHECKBOX_TYPE,
isMetadata: true,
isDragDisabled: false,
skipPersist: false,
csvCandidate: false,
config: DEFAULT_COLUMN_CONFIG
},
ROW_CONTEXT_MENU: {
id: MetadataColumns.ROW_CONTEXT_MENU,
key: MetadataColumns.ROW_CONTEXT_MENU,
Expand Down Expand Up @@ -429,6 +444,7 @@ export const DEFAULT_SETTINGS: DatabaseSettings = {
show_metadata_inlinks: false,
show_metadata_outlinks: false,
show_metadata_tags: false,
binary_checkbox_type: false,
source_data: SourceDataTypes.CURRENT_FOLDER,
source_form_result: '',
source_destination_path: '/',
Expand Down
2 changes: 2 additions & 0 deletions src/lang/locale/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ export default {
"settings_metatata_outlinks_toggle_desc": "Enable/disable File Outlinks Column",
"settings_metatata_tags_toggle_title": "File Tags",
"settings_metatata_tags_toggle_desc": "Enable/disable File Tags Column",
"settings_metatata_checkbox_type_toggle_title": "Checkbox Type",
"settings_metatata_checkbox_type_toggle_desc": "Select whether the cell state should be a boolean (true/false) or binary (1/0) in the frontmatter",
"settings_remove_fields_title": "Remove fields",
"settings_remove_fields_desc": "Enable/disable remove fields when a column is deleted",
"settings_template_title": "Header templates",
Expand Down
23 changes: 23 additions & 0 deletions src/settings/handlers/columns/DefaultMetadataToggleGroupHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { t } from "lang/helpers";
import { Setting } from "obsidian";
import { AbstractSettingsHandler, SettingHandlerResponse } from "settings/handlers/AbstractSettingHandler";
import { add_setting_header } from "settings/SettingsComponents";
import {DEFAULT_SETTINGS} from "../../../helpers/Constants";

export class DefaultMetadataToggleGroupHandler extends AbstractSettingsHandler {
settingTitle: string = t("settings_metatata_title");
Expand Down Expand Up @@ -137,6 +138,28 @@ export class DefaultMetadataToggleGroupHandler extends AbstractSettingsHandler {
toggle.setValue(settingsManager.plugin.settings.local_settings.show_metadata_tags)
.onChange(metadata_tags_toggle_promise)
);

/*************************
* CHECKBOX TYPE COLUMN
************************/
const metadata_checkbox_type_toggle_promise = async (value: boolean): Promise<void> => {
// switch show tags on/off
const update_local_settings = settingsManager.plugin.settings.local_settings;
update_local_settings.binary_checkbox_type = value;
// update settings
await settingsManager.plugin.updateSettings({
local_settings: update_local_settings
});
DEFAULT_SETTINGS.local_settings.binary_checkbox_type = value;
}

new Setting(metadata_section)
.setName(t("settings_metatata_checkbox_type_toggle_title"))
.setDesc(t("settings_metatata_checkbox_type_toggle_desc"))
.addToggle(toggle =>
toggle.setValue(settingsManager.plugin.settings.local_settings.binary_checkbox_type)
.onChange(metadata_checkbox_type_toggle_promise)
);
}
return this.goNext(settingHandlerResponse);
}
Expand Down