Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion manifest-beta.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"id": "obsidian-meta-bind-plugin",
"name": "Meta Bind",
"version": "1.4.10",
"minAppVersion": "1.10.0",
"minAppVersion": "1.13.0",
"description": "Make your notes interactive with inline input fields, metadata displays, and buttons.",
"author": "Moritz Jung",
"authorUrl": "https://www.moritzjung.dev/",
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"id": "obsidian-meta-bind-plugin",
"name": "Meta Bind",
"version": "1.4.10",
"minAppVersion": "1.10.0",
"minAppVersion": "1.13.0",
"description": "Make your notes interactive with inline input fields, metadata displays, and buttons.",
"author": "Moritz Jung",
"authorUrl": "https://www.moritzjung.dev/",
Expand Down
27 changes: 25 additions & 2 deletions packages/core/src/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,34 @@ export const weekdays: Weekday[] = [
},
];

export function getWeekdayByName(name: string): Weekday {
return weekdays.find(weekday => weekday.name === name) ?? weekdays[1];
}

export function normalizeFirstWeekday(firstWeekday: unknown): string {
if (typeof firstWeekday === 'string') {
return getWeekdayByName(firstWeekday).name;
}

if (typeof firstWeekday === 'number') {
return (weekdays.find(weekday => weekday.index === firstWeekday) ?? weekdays[1]).name;
}

if (typeof firstWeekday === 'object' && firstWeekday !== null && 'name' in firstWeekday) {
const name = firstWeekday.name;
if (typeof name === 'string') {
return getWeekdayByName(name).name;
}
}

return weekdays[1].name;
}

export interface MetaBindPluginSettings {
devMode: boolean;
ignoreCodeBlockRestrictions: boolean;
preferredDateFormat: string;
firstWeekday: Weekday;
firstWeekday: string;
syncInterval: number;
enableJs: boolean;
viewFieldDisplayNullAsEmpty: boolean;
Expand All @@ -84,7 +107,7 @@ export const DEFAULT_SETTINGS: MetaBindPluginSettings = {
devMode: false,
ignoreCodeBlockRestrictions: false,
preferredDateFormat: 'YYYY-MM-DD',
firstWeekday: weekdays[1],
firstWeekday: weekdays[1].name,
syncInterval: 200,
enableJs: false,
viewFieldDisplayNullAsEmpty: false,
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/utils/DatePickerUtils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { Weekday } from 'meta-bind-core/src/Settings';
import { monthNames, weekdays } from 'meta-bind-core/src/Settings';
import { getWeekdayByName, monthNames, weekdays } from 'meta-bind-core/src/Settings';
import { mod } from 'meta-bind-core/src/utils/Utils';
import Moment from 'moment/moment';

export let firstWeekday: Weekday = weekdays[1];

export function setFirstWeekday(w: Weekday): void {
firstWeekday = w;
export function setFirstWeekday(weekdayName: string): void {
firstWeekday = getWeekdayByName(weekdayName);
}

export function getMonthName(index: number): string {
Expand Down
25 changes: 25 additions & 0 deletions packages/core/tests/utils/Settings.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { describe, expect, test } from 'bun:test';
import { normalizeFirstWeekday } from 'meta-bind-core/src/Settings';

describe('settings', () => {
describe('normalizeFirstWeekday', () => {
test('keeps valid weekday names', () => {
expect(normalizeFirstWeekday('Sunday')).toBe('Sunday');
expect(normalizeFirstWeekday('Wednesday')).toBe('Wednesday');
});

test('migrates legacy weekday objects', () => {
expect(normalizeFirstWeekday({ index: 5, name: 'Friday', shortName: 'Fr' })).toBe('Friday');
});

test('accepts numeric weekday indexes defensively', () => {
expect(normalizeFirstWeekday(6)).toBe('Saturday');
});

test('falls back to Monday for invalid values', () => {
expect(normalizeFirstWeekday('Funday')).toBe('Monday');
expect(normalizeFirstWeekday({ name: 1 })).toBe('Monday');
expect(normalizeFirstWeekday(undefined)).toBe('Monday');
});
});
});
2 changes: 1 addition & 1 deletion packages/obsidian/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
"@codemirror/lint": "^6.9.5",
"@codemirror/state": "^6.6.0",
"@codemirror/view": "^6.41.1",
"obsidian": "latest"
"obsidian": "obsidianmd/obsidian-api#2d878c6a67294b13a07908b27003246a8593d469"
}
}
6 changes: 4 additions & 2 deletions packages/obsidian/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { MetaBindPluginSettings } from 'meta-bind-core/src/Settings';
import { DEFAULT_SETTINGS } from 'meta-bind-core/src/Settings';
import { DEFAULT_SETTINGS, normalizeFirstWeekday } from 'meta-bind-core/src/Settings';
import { areObjectsEqual } from 'meta-bind-core/src/utils/Utils';
import type { ObsAPI } from 'meta-bind-obsidian/src/ObsAPI';
import { ObsMetaBind } from 'meta-bind-obsidian/src/ObsMB';
Expand Down Expand Up @@ -40,13 +40,15 @@ export default class ObsMetaBindPlugin extends Plugin {
async loadSettings(): Promise<void> {
MB_DEBUG && console.log(`meta-bind | Main >> loading settings`);

const loadedSettings = ((await this.loadData()) ?? {}) as MetaBindPluginSettings;
const loadedSettings = ((await this.loadData()) ?? {}) as Partial<MetaBindPluginSettings>;

if (typeof loadedSettings === 'object' && loadedSettings != null) {
// @ts-expect-error TS2339 remove old config field
delete loadedSettings.inputTemplates;
// @ts-expect-error TS2339 remove old config field
delete loadedSettings.useUsDateInputOrder;

loadedSettings.firstWeekday = normalizeFirstWeekday(loadedSettings.firstWeekday);
}

this.settings = Object.assign({}, DEFAULT_SETTINGS, loadedSettings);
Expand Down
61 changes: 61 additions & 0 deletions packages/obsidian/src/settings/ListSettingGroup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import type { MetaBindSettingKey } from 'meta-bind-obsidian/src/settings/SettingsTypes';
import type { SettingDefinitionItem, SettingGroupItem } from 'obsidian';

export interface ListSettingGroupOptions<T> {
heading: string;
emptyState: string;
items: T[];
renderItem: (item: T, index: number) => SettingGroupItem<MetaBindSettingKey>;
applyItems: (items: T[]) => boolean;
onUpdate: () => void;
}

export function getListSettingGroup<T>(options: ListSettingGroupOptions<T>): SettingDefinitionItem<MetaBindSettingKey> {
return {
type: 'group',
heading: options.heading,
cls: 'mod-list',
emptyState: options.emptyState,
items: options.items.map((item, index) => options.renderItem(item, index)),
onDelete: (index: number): void => {
updateListItems(
options.items,
items => {
items.splice(index, 1);
},
options.applyItems,
options.onUpdate,
);
},
onReorder: (oldIndex: number, newIndex: number): void => {
updateListItems(
options.items,
items => {
moveListItem(items, oldIndex, newIndex);
},
options.applyItems,
options.onUpdate,
);
},
};
}

export function updateListItems<T>(
items: T[],
updateItems: (items: T[]) => void,
applyItems: (items: T[]) => boolean,
onUpdate: () => void,
): boolean {
const nextItems = structuredClone(items);
updateItems(nextItems);
if (applyItems(nextItems)) {
onUpdate();
return true;
}
return false;
}

function moveListItem<T>(items: T[], oldIndex: number, newIndex: number): void {
const [item] = items.splice(oldIndex, 1);
items.splice(newIndex, 0, item);
}
Loading
Loading