diff --git a/.editorconfig b/.editorconfig index 84b8a66d..5663ab07 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,10 +1,10 @@ -# top-most EditorConfig file -root = true - -[*] -charset = utf-8 -end_of_line = lf -insert_final_newline = true -indent_style = tab -indent_size = 4 -tab_width = 4 +# top-most EditorConfig file +root = true + +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +indent_style = space +indent_size = 4 +tab_width = 4 diff --git a/.eslintrc b/.eslintrc index 08072905..b16e0a5b 100644 --- a/.eslintrc +++ b/.eslintrc @@ -2,22 +2,20 @@ "root": true, "parser": "@typescript-eslint/parser", "env": { "node": true }, - "plugins": [ - "@typescript-eslint" - ], + "plugins": ["@typescript-eslint"], "extends": [ - "eslint:recommended", - "plugin:@typescript-eslint/eslint-recommended", - "plugin:@typescript-eslint/recommended" - ], + "eslint:recommended", + "plugin:@typescript-eslint/eslint-recommended", + "plugin:@typescript-eslint/recommended" + ], "parserOptions": { "sourceType": "module" }, "rules": { - "no-unused-vars": "off", - "@typescript-eslint/no-unused-vars": ["error", { "args": "none" }], - "@typescript-eslint/ban-ts-comment": "off", - "no-prototype-builtins": "off", - "@typescript-eslint/no-empty-function": "off" - } - } \ No newline at end of file + "no-unused-vars": "off", + "@typescript-eslint/no-unused-vars": ["error", { "args": "none" }], + "@typescript-eslint/ban-ts-comment": "off", + "no-prototype-builtins": "off", + "@typescript-eslint/no-empty-function": "off" + } +} diff --git a/src/utils/Error.ts b/src/utils/Error.ts index 4de523c0..e98b84d8 100644 --- a/src/utils/Error.ts +++ b/src/utils/Error.ts @@ -1,27 +1,28 @@ import { log_error } from "./Log"; export class ModalFormError extends Error { - constructor(msg: string, public console_msg?: string) { - super(msg); - this.name = this.constructor.name; - Error.captureStackTrace(this, this.constructor); - } + constructor(msg: string, public console_msg?: string) { + super(msg); + this.name = this.constructor.name; + Error.captureStackTrace(this, this.constructor); + } } export async function errorWrapper( - fn: () => Promise, - msg: string + fn: () => Promise, + msg: string ): Promise { - try { - return await fn(); - } catch (e: any) { - if (!(e instanceof ModalFormError)) { - log_error(new ModalFormError(msg, e.message)); - } else { - log_error(e); - } - return null; - } + try { + return await fn(); + } catch (e) { + const err = e instanceof Error ? e : new Error(String(e)); + if (!(err instanceof ModalFormError)) { + log_error(new ModalFormError(msg, err.message)); + } else { + log_error(err); + } + return null; + } } /** @@ -34,11 +35,11 @@ export async function errorWrapper( * @return {*} {(T | null)} */ export function tryCatch(fn: () => T, msg: string): T | null { - try { - return fn(); - } catch (e) { - if (e instanceof Error) - log_error(new ModalFormError(msg, e.message)); - return null; - } + try { + return fn(); + } catch (e) { + if (e instanceof Error) + log_error(new ModalFormError(msg, e.message)); + return null; + } } diff --git a/src/utils/Log.ts b/src/utils/Log.ts index ff1905d3..f5a3d4b9 100644 --- a/src/utils/Log.ts +++ b/src/utils/Log.ts @@ -2,24 +2,24 @@ import { Notice } from "obsidian"; import { ModalFormError } from "./Error"; -export function log_notice(title: string, msg: string): void { - const notice = new Notice("", 15000); - const el = notice.noticeEl; - el.empty(); - const head = el.createEl('h6', { text: title }) - const body = el.createEl('div', { text: title }) - el.append(head, body); +export function log_notice(title: string, msg: string | DocumentFragment): void { + const notice = new Notice("", 15000); + const el = notice.noticeEl; + el.empty(); + const head = el.createEl('h6', { text: title }) + const body = el.createEl('div', { text: msg }) + el.append(head, body); } export function log_update(msg: string): void { - log_notice('Modal form update', msg) + log_notice('Modal form update', msg) } export function log_error(e: Error | ModalFormError): void { - if (e instanceof ModalFormError && e.console_msg) { - log_notice('Modal from error', e.message) - console.error(`Modal form Error:`, e.message, "\n", e.console_msg); - } else { - log_notice('Modal from error', e.message) - } + if (e instanceof ModalFormError && e.console_msg) { + log_notice('Modal from error', e.message) + console.error(`Modal form Error:`, e.message, "\n", e.console_msg); + } else { + log_notice('Modal from error', e.message) + } } diff --git a/src/views/EditFormView.ts b/src/views/EditFormView.ts index 94aaa193..2b179559 100644 --- a/src/views/EditFormView.ts +++ b/src/views/EditFormView.ts @@ -7,16 +7,16 @@ import { log_notice } from "src/utils/Log"; export const EDIT_FORM_VIEW = "modal-form-edit-form-view"; function parseState(maybeState: unknown): maybeState is EditableFormDefinition { - if (maybeState === null) { - return false - } - if (typeof maybeState !== 'object') { - return false - } - if ('title' in maybeState && 'name' in maybeState && 'fields' in maybeState) { - return true - } - return false; + if (maybeState === null) { + return false + } + if (typeof maybeState !== 'object') { + return false + } + if ('title' in maybeState && 'name' in maybeState && 'fields' in maybeState) { + return true + } + return false; } /** @@ -25,64 +25,66 @@ function parseState(maybeState: unknown): maybeState is EditableFormDefinition { * Simple, right? */ export class EditFormView extends ItemView { - formState: EditableFormDefinition = { title: '', name: '', fields: [] }; - formEditor!: FormEditor; - constructor(readonly leaf: WorkspaceLeaf, readonly plugin: ModalFormPlugin) { - super(leaf); - this.icon = 'note-glyph' - } + formState: EditableFormDefinition = { title: '', name: '', fields: [] }; + formEditor!: FormEditor; + constructor(readonly leaf: WorkspaceLeaf, readonly plugin: ModalFormPlugin) { + super(leaf); + this.icon = 'note-glyph' + } - getViewType() { - return EDIT_FORM_VIEW; - } + getViewType() { + return EDIT_FORM_VIEW; + } - getDisplayText() { - return "Edit form"; - } + getDisplayText() { + return "Edit form"; + } - async onOpen() { - this.containerEl.empty() - this.formEditor = new FormEditor({ - target: this.containerEl, - props: { - definition: this.formState, - app: this.app, - onChange: () => { - console.log(this.formState) - this.app.workspace.requestSaveLayout() - }, - onSubmit: (formDefinition: FormDefinition) => { - console.log({ formDefinition }); - this.plugin.saveForm(formDefinition); - this.plugin.closeEditForm() - }, - onCancel: () => { - this.plugin.closeEditForm() - }, - onPreview: async (formDefinition: FormDefinition) => { - const result = await this.plugin.api.openForm(formDefinition) - log_notice('Form result', JSON.stringify(result, null, 2)) - }, - } - }); - } + async onOpen() { + this.containerEl.empty() + this.formEditor = new FormEditor({ + target: this.containerEl, + props: { + definition: this.formState, + app: this.app, + onChange: () => { + console.log(this.formState) + this.app.workspace.requestSaveLayout() + }, + onSubmit: (formDefinition: FormDefinition) => { + console.log({ formDefinition }); + this.plugin.saveForm(formDefinition); + this.plugin.closeEditForm() + }, + onCancel: () => { + this.plugin.closeEditForm() + }, + onPreview: async (formDefinition: FormDefinition) => { + const result = await this.plugin.api.openForm(formDefinition) + const result_str = JSON.stringify(result, null, 2) + log_notice('Form result', result_str) + console.log(result_str) + }, + } + }); + } - async onClose() { - console.log('onClose of edit form called') - this.formEditor.$destroy(); - } + async onClose() { + console.log('onClose of edit form called') + this.formEditor.$destroy(); + } - async setState(state: unknown, result: ViewStateResult): Promise { - console.log('setState of edit form called', state) - if (parseState(state)) { - this.formState = state; - this.formEditor.$set({ definition: this.formState }) + async setState(state: unknown, result: ViewStateResult): Promise { + console.log('setState of edit form called', state) + if (parseState(state)) { + this.formState = state; + this.formEditor.$set({ definition: this.formState }) + } + return super.setState(state, result); + } + getState() { + return this.formState; } - return super.setState(state, result); - } - getState() { - return this.formState; - } }