Skip to content

Commit

Permalink
chore: type safety
Browse files Browse the repository at this point in the history
  • Loading branch information
danielo515 committed Oct 2, 2023
1 parent 8ae7d5b commit 680ba82
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 124 deletions.
20 changes: 10 additions & 10 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
26 changes: 12 additions & 14 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -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-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"
}
}
49 changes: 25 additions & 24 deletions src/utils/Error.ts
Original file line number Diff line number Diff line change
@@ -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<T>(
fn: () => Promise<T>,
msg: string
fn: () => Promise<T>,
msg: string
): Promise<T | null> {
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;
}
}

/**
Expand All @@ -34,11 +35,11 @@ export async function errorWrapper<T>(
* @return {*} {(T | null)}
*/
export function tryCatch<T>(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;
}
}
28 changes: 14 additions & 14 deletions src/utils/Log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
126 changes: 64 additions & 62 deletions src/views/EditFormView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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<void> {
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<void> {
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;
}
}


0 comments on commit 680ba82

Please sign in to comment.