Skip to content

Commit ee4398d

Browse files
committed
better errors second part; fix #311
1 parent aff032a commit ee4398d

File tree

5 files changed

+90
-31
lines changed

5 files changed

+90
-31
lines changed

packages/core/src/Settings.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,6 @@ export interface MetaBindPluginSettings {
7575
inputFieldTemplates: InputFieldTemplate[];
7676
buttonTemplates: ButtonConfig[];
7777
excludedFolders: string[];
78-
79-
// @deprecated
80-
inputTemplates: string | undefined;
8178
}
8279

8380
export interface InputFieldTemplate {
@@ -101,7 +98,4 @@ export const DEFAULT_SETTINGS: MetaBindPluginSettings = {
10198
inputFieldTemplates: [],
10299
buttonTemplates: [],
103100
excludedFolders: ['templates'],
104-
105-
// @deprecated
106-
inputTemplates: undefined,
107101
};

packages/core/src/config/ButtonConfigValidators.ts

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,23 @@ import {
2020
import { oneOf, schemaForType } from 'packages/core/src/utils/ZodUtils';
2121
import { z } from 'zod';
2222

23+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
2324
function numberValidator(action: string, name: string, description: string) {
2425
return z.number({
2526
required_error: `The ${action} action requires a specified ${description} with the '${name}' field.`,
2627
invalid_type_error: `The ${action} action requires the value of the '${name}' fields to be a number.`,
2728
});
2829
}
2930

31+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
3032
function stringValidator(action: string, name: string, description: string) {
3133
return z.string({
3234
required_error: `The ${action} action requires a specified ${description} with the '${name}' field.`,
3335
invalid_type_error: `The ${action} action requires the value of the '${name}' fields to be a string.`,
3436
});
3537
}
3638

39+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
3740
function booleanValidator(action: string, name: string, description: string) {
3841
return z.boolean({
3942
required_error: `The ${action} action requires a specified ${description} with the '${name}' field.`,
@@ -82,69 +85,76 @@ export const V_SleepButtonAction = schemaForType<SleepButtonAction>()(
8285
export const V_TemplaterCreateNoteButtonAction = schemaForType<TemplaterCreateNoteButtonAction>()(
8386
z.object({
8487
type: z.literal(ButtonActionType.TEMPLATER_CREATE_NOTE),
85-
templateFile: z.string(),
86-
folderPath: z.string().optional(),
87-
fileName: z.string().optional(),
88-
openNote: z.boolean().optional(),
88+
templateFile: stringValidator('templaterCreateNote', 'templateFile', 'template file path'),
89+
folderPath: stringValidator('templaterCreateNote', 'folderPath', 'folder path').optional(),
90+
fileName: stringValidator('templaterCreateNote', 'fileName', 'file name').optional(),
91+
openNote: booleanValidator('templaterCreateNote', 'openNote', 'value for whether to open the note').optional(),
8992
}),
9093
);
9194
export const V_UpdateMetadataButtonAction = schemaForType<UpdateMetadataButtonAction>()(
9295
z.object({
9396
type: z.literal(ButtonActionType.UPDATE_METADATA),
94-
bindTarget: z.string(),
95-
evaluate: z.boolean(),
96-
value: z.coerce.string(),
97+
bindTarget: stringValidator('updateMetadata', 'bindTarget', 'bind target to the metadata to update'),
98+
evaluate: booleanValidator(
99+
'updateMetadata',
100+
'evaluate',
101+
'value for whether to evaluate the value as a JavaScript expression',
102+
),
103+
value: z.coerce.string({
104+
required_error: `The updateMetadata action requires a specified value for the update with the 'value' field.`,
105+
invalid_type_error: `The updateMetadata action requires the value of the 'value' fields to be a string.`,
106+
}),
97107
}),
98108
);
99109

100110
export const V_CreateNoteButtonAction = schemaForType<CreateNoteButtonAction>()(
101111
z.object({
102112
type: z.literal(ButtonActionType.CREATE_NOTE),
103-
folderPath: z.string().optional(),
104-
fileName: z.string(),
105-
openNote: z.boolean().optional(),
113+
folderPath: stringValidator('createNote', 'folderPath', 'folder path').optional(),
114+
fileName: stringValidator('createNote', 'fileName', 'file name'),
115+
openNote: booleanValidator('createNote', 'openNote', 'value for whether to open the note').optional(),
106116
}),
107117
);
108118

109119
export const V_ReplaceInNoteButtonAction = schemaForType<ReplaceInNoteButtonAction>()(
110120
z.object({
111121
type: z.literal(ButtonActionType.REPLACE_IN_NOTE),
112-
fromLine: z.number(),
113-
toLine: z.number(),
114-
replacement: z.string(),
115-
templater: z.boolean().optional(),
122+
fromLine: numberValidator('replaceInNote', 'fromLine', 'line to replace from'),
123+
toLine: numberValidator('replaceInNote', 'toLine', 'line to replace to'),
124+
replacement: stringValidator('replaceInNote', 'replacement', 'replacement string'),
125+
templater: booleanValidator('replaceInNote', 'templater', 'value for whether to use Templater').optional(),
116126
}),
117127
);
118128

119129
export const V_ReplaceSelfButtonAction = schemaForType<ReplaceSelfButtonAction>()(
120130
z.object({
121131
type: z.literal(ButtonActionType.REPLACE_SELF),
122-
replacement: z.string(),
123-
templater: z.boolean().optional(),
132+
replacement: stringValidator('replaceSelf', 'replacement', 'replacement string'),
133+
templater: booleanValidator('replaceSelf', 'templater', 'value for whether to use Templater').optional(),
124134
}),
125135
);
126136

127137
export const V_RegexpReplaceInNoteButtonAction = schemaForType<RegexpReplaceInNoteButtonAction>()(
128138
z.object({
129139
type: z.literal(ButtonActionType.REGEXP_REPLACE_IN_NOTE),
130-
regexp: z.string(),
131-
replacement: z.string(),
140+
regexp: stringValidator('regexpReplaceInNote', 'regexp', 'search regular expression'),
141+
replacement: stringValidator('regexpReplaceInNote', 'replacement', 'replacement string'),
132142
}),
133143
);
134144

135145
export const V_InsertIntoNoteButtonAction = schemaForType<InsertIntoNoteButtonAction>()(
136146
z.object({
137147
type: z.literal(ButtonActionType.INSERT_INTO_NOTE),
138-
line: z.number(),
139-
value: z.string(),
140-
templater: z.boolean().optional(),
148+
line: numberValidator('insertIntoNote', 'line', 'line to insert at'),
149+
value: stringValidator('insertIntoNote', 'value', 'string to insert'),
150+
templater: booleanValidator('insertIntoNote', 'templater', 'value for whether to use Templater').optional(),
141151
}),
142152
);
143153

144154
export const V_InlineJsButtonAction = schemaForType<InlineJsButtonAction>()(
145155
z.object({
146156
type: z.literal(ButtonActionType.INLINE_JS),
147-
code: z.string(),
157+
code: stringValidator('inlineJS', 'code', 'code string to run'),
148158
}),
149159
);
150160

packages/core/src/utils/Utils.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,53 @@ export function areArraysEqual<T>(arr1: T[] | undefined, arr2: T[] | undefined):
6767
return true;
6868
}
6969

70+
export function areObjectsEqual(obj1: unknown, obj2: unknown): boolean {
71+
if (obj1 == null && obj2 == null) {
72+
return true;
73+
}
74+
if (obj1 == null || obj2 == null) {
75+
return false;
76+
}
77+
78+
if (typeof obj1 !== typeof obj2) {
79+
return false;
80+
}
81+
82+
if (typeof obj1 === 'object' && typeof obj2 === 'object') {
83+
if (Array.isArray(obj1) && Array.isArray(obj2)) {
84+
if (obj1.length !== obj2.length) {
85+
return false;
86+
}
87+
88+
for (let i = 0; i < obj1.length; i++) {
89+
if (!areObjectsEqual(obj1[i], obj2[i])) {
90+
return false;
91+
}
92+
}
93+
94+
return true;
95+
}
96+
97+
const keys1 = Object.keys(obj1);
98+
const keys2 = Object.keys(obj2);
99+
100+
if (keys1.length !== keys2.length) {
101+
return false;
102+
}
103+
104+
for (const key of keys1) {
105+
// @ts-ignore
106+
if (!areObjectsEqual(obj1[key], obj2[key])) {
107+
return false;
108+
}
109+
}
110+
111+
return true;
112+
}
113+
114+
return obj1 === obj2;
115+
}
116+
70117
/**
71118
* Checks if arr starts with base.
72119
*

packages/obsidian/src/cm6/Cm5_Modes.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import { javascript } from '@codemirror/legacy-modes/mode/javascript';
33
import { type Mode, type StringStream } from 'codemirror';
44
import { SyntaxHighlighting } from 'packages/core/src/parsers/syntaxHighlighting/SyntaxHighlighting';
55
import type MetaBindPlugin from 'packages/obsidian/src/main';
6-
7-
import { type FieldType, type InlineFieldType } from 'packages/core/src/config/APIConfigs';
6+
import { type InlineFieldType } from 'packages/core/src/config/APIConfigs';
87

98
export function registerCm5HLModes(plugin: MetaBindPlugin): void {
109
/* eslint-disable */

packages/obsidian/src/main.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { PlaygroundView, MB_PLAYGROUND_VIEW_TYPE } from 'packages/obsidian/src/p
2424
import { MetaBindSettingTab } from 'packages/obsidian/src/settings/SettingsTab';
2525
import { ObsidianNotePosition } from 'packages/obsidian/src/ObsidianNotePosition';
2626
import { RenderChildType } from 'packages/core/src/config/APIConfigs';
27+
import { areObjectsEqual } from 'packages/core/src/utils/Utils';
2728

2829
export enum MetaBindBuild {
2930
DEV = 'dev',
@@ -341,9 +342,17 @@ export default class MetaBindPlugin extends Plugin implements IPlugin {
341342

342343
const loadedSettings = (await this.loadData()) as MetaBindPluginSettings;
343344

345+
// @ts-expect-error TS2339 remove old config field
346+
delete loadedSettings.inputTemplates;
347+
// @ts-expect-error TS2339 remove old config field
348+
delete loadedSettings.useUsDateInputOrder;
349+
344350
this.settings = Object.assign({}, DEFAULT_SETTINGS, loadedSettings);
345351

346-
await this.saveSettings();
352+
if (!areObjectsEqual(loadedSettings, this.settings)) {
353+
// console.log(JSON.stringify(loadedSettings, null, '\t'), JSON.stringify(this.settings, null, '\t'));
354+
await this.saveSettings();
355+
}
347356
}
348357

349358
async saveSettings(): Promise<void> {

0 commit comments

Comments
 (0)