Skip to content
Merged
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
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Obsidian Meta Bind Changelog

# 1.4.11

Bug Fixes

- Fixed button templates loading invalid buttons in some cases [#664](https://github.com/mProjectsCode/obsidian-meta-bind-plugin/issues/664)

Misc

- Redid monorepo setup
- Fix more Obsidian review warnings

# 1.4.10

Misc
Expand Down
196 changes: 169 additions & 27 deletions bun.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions manifest-beta.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"id": "obsidian-meta-bind-plugin",
"name": "Meta Bind",
"version": "1.4.10",
"minAppVersion": "1.10.0",
"version": "1.4.11",
"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
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"id": "obsidian-meta-bind-plugin",
"name": "Meta Bind",
"version": "1.4.10",
"minAppVersion": "1.10.0",
"version": "1.4.11",
"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
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-meta-bind-plugin",
"version": "1.4.10",
"version": "1.4.11",
"description": "Make your notes interactive with inline input fields, metadata displays, and buttons.",
"main": "main.js",
"scripts": {
Expand Down Expand Up @@ -41,8 +41,8 @@
"svelte-check": "^4.4.8",
"tslib": "^2.8.1",
"typescript": "^6.0.3",
"typescript-eslint": "^8.59.3",
"vite": "^8.0.12",
"typescript-eslint": "^8.60.0",
"vite": "^8.0.14",
"vite-plugin-banner": "^0.8.1",
"vite-plugin-static-copy": "^4.1.0",
"yaml": "^2.9.0"
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
22 changes: 16 additions & 6 deletions packages/core/src/fields/button/ButtonManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,35 @@ export class ButtonManager {
this.buttonTemplates.clear();

for (const buttonTemplate of buttonTemplates) {
if (buttonTemplate.id === undefined || buttonTemplate.id === '') {
let validatedButtonTemplate: ButtonConfig;

try {
validatedButtonTemplate = this.mb.buttonParser.validateConfig(buttonTemplate);
Object.assign(buttonTemplate, validatedButtonTemplate);
} catch (e) {
errorCollection.add(e);
continue;
}

if (validatedButtonTemplate.id === undefined || validatedButtonTemplate.id === '') {
errorCollection.add(
new MetaBindButtonError({
errorLevel: ErrorLevel.ERROR,
cause: `Button with label "${buttonTemplate.label}" has no id, but button templates must have an id.`,
cause: `Button with label "${validatedButtonTemplate.label}" has no id, but button templates must have an id.`,
effect: 'Button templates could not be saved.',
}),
);
} else if (idSet.has(buttonTemplate.id)) {
} else if (idSet.has(validatedButtonTemplate.id)) {
errorCollection.add(
new MetaBindButtonError({
errorLevel: ErrorLevel.ERROR,
cause: `Button id "${buttonTemplate.id}" is not unique. The same id is used by multiple buttons.`,
cause: `Button id "${validatedButtonTemplate.id}" is not unique. The same id is used by multiple buttons.`,
effect: 'Button templates could not be saved.',
}),
);
} else {
idSet.add(buttonTemplate.id);
this.buttonTemplates.set(buttonTemplate.id, buttonTemplate);
idSet.add(validatedButtonTemplate.id);
this.buttonTemplates.set(validatedButtonTemplate.id, validatedButtonTemplate);
}
}

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
57 changes: 51 additions & 6 deletions packages/core/tests/fields/Button.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { beforeEach, describe, expect, test } from 'bun:test';
import {
type ButtonAction,
ButtonActionType,
ButtonClickContext,
ButtonClickType,
} from 'meta-bind-core/src/config/ButtonConfig';
import type { ButtonAction, ButtonConfig } from 'meta-bind-core/src/config/ButtonConfig';
import { ButtonActionType, ButtonClickContext, ButtonClickType } from 'meta-bind-core/src/config/ButtonConfig';
import { TestMetaBind } from '../__mocks__/TestPlugin';

let testPlugin: TestMetaBind;
Expand Down Expand Up @@ -433,4 +429,53 @@ describe('Button', () => {
});
}
});

describe('Button Templates', () => {
beforeEach(() => {
testPlugin = new TestMetaBind();
});

test('normalizes raw template configs before storing them', async () => {
const rawTemplate = {
label: 'Raw Update',
style: 'default',
id: 'raw-update',
actions: [
{
type: ButtonActionType.UPDATE_METADATA,
bindTarget: 'testProp',
evaluate: true,
value: 1,
},
],
} as unknown as ButtonConfig;

const errors = testPlugin.buttonManager.setButtonTemplates([rawTemplate]);

expect(errors.hasErrors()).toBe(false);

const template = testPlugin.buttonManager.getButton(testFilePath, 'raw-update');
const action = template?.actions?.[0];

expect(action).toMatchObject({ value: '1' });

testPlugin.internal.jsEngineExecuteCustom = async (code): Promise<unknown> => {
expect(code).toBe('1');
return 7;
};

await testPlugin.buttonActionRunner.runButtonActions(
template!,
testFilePath,
{
position: undefined,
isInline: false,
isInGroup: false,
},
defaultClick,
);

expect(testPlugin.getCacheMetadata(testFilePath)?.testProp).toBe(7);
});
});
});
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#cf9ef454f6e0fa0f1f275b3e51991dfc557cc7be"
}
}
2 changes: 1 addition & 1 deletion packages/obsidian/src/ObsMB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export class ObsMetaBind extends MetaBind<ObsComponents> {
);

this.plugin.registerInterval(
window.setInterval(() => this.metadataManager.cycle(), this.getSettings().syncInterval),
window.setInterval(() => void this.metadataManager.cycle(), this.getSettings().syncInterval),
);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/obsidian/src/dependencies/DependencyManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class DependencyManager {
private checkDependencyVersion(dependency: Dependency, version: Version): void {
if (Version.lessThan(version, dependency.minVersion)) {
this.throwDependencyError(
`Plugin ${dependency.pluginId} is outdated. Required version is at least ${dependency.minVersion}, installed version is ${version}. Please update the plugin.`,
`Plugin ${dependency.pluginId} is outdated. Required version is at least ${dependency.minVersion.toString()}, installed version is ${version.toString()}. Please update the plugin.`,
);
}

Expand All @@ -55,7 +55,7 @@ export class DependencyManager {
(Version.greaterThan(version, dependency.maxVersion) || Version.equals(version, dependency.maxVersion))
) {
this.throwDependencyError(
`Plugin ${dependency.pluginId} is too new. Required version is lower than ${dependency.maxVersion}, installed version is ${version}. Please downgrade the plugin.`,
`Plugin ${dependency.pluginId} is too new. Required version is lower than ${dependency.maxVersion.toString()}, installed version is ${version.toString()}. Please downgrade the plugin.`,
);
}
}
Expand Down
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
60 changes: 60 additions & 0 deletions packages/obsidian/src/settings/ListSettingGroup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
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: 'list',
heading: options.heading,
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