Skip to content

Commit

Permalink
fix: forms can not be renamed
Browse files Browse the repository at this point in the history
fixes #108
  • Loading branch information
danielo515 committed Nov 2, 2023
1 parent 040f8f5 commit ab2e20d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
4 changes: 0 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ export default class ModalFormPlugin extends Plugin {
return this.activateView(EDIT_FORM_VIEW);
}

formExists(formName: string): boolean {
return this.settings?.formDefinitions.some((form) => form.name === formName) ?? false;
}

/**
* Opens the form in the editor.
* @returns
Expand Down
6 changes: 6 additions & 0 deletions src/store/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ export const settingsStore = {
return { ...s, formDefinitions: forms };
});
},
addNewForm(form: FormDefinition) {
update((s): ModalFormSettings => {
const forms = [...s.formDefinitions, form];
return { ...s, formDefinitions: forms };
});
},
removeForm(name: string) {
update((s): ModalFormSettings => {
const forms = s.formDefinitions.filter((f) => f.name !== name);
Expand Down
13 changes: 10 additions & 3 deletions src/views/EditFormView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ItemView, type ViewStateResult, WorkspaceLeaf } from "obsidian";
import type { FormDefinition, EditableFormDefinition } from "../core/formDefinition";
import FormEditor from './FormBuilder.svelte'
import { log_notice } from "src/utils/Log";
import { settingsStore } from "src/store/store";

export const EDIT_FORM_VIEW = "modal-form-edit-form-view";

Expand All @@ -26,6 +27,7 @@ function parseState(maybeState: unknown): maybeState is EditableFormDefinition {
*/
export class EditFormView extends ItemView {
formState: EditableFormDefinition = { title: '', name: '', fields: [] };
originalFormName?: string;
formEditor!: FormEditor;
constructor(readonly leaf: WorkspaceLeaf, readonly plugin: ModalFormPlugin) {
super(leaf);
Expand All @@ -47,12 +49,16 @@ export class EditFormView extends ItemView {
props: {
definition: this.formState,
onChange: () => {
console.log(this.formState)
console.log('Save form state', this.formState)
this.app.workspace.requestSaveLayout()
},
onSubmit: (formDefinition: FormDefinition) => {
console.log({ formDefinition });
this.plugin.saveForm(formDefinition);
console.log('Submitting form', { formDefinition });
if (this.originalFormName && this.originalFormName !== '') {
settingsStore.updateForm(this.originalFormName, formDefinition)
} else {
settingsStore.addNewForm(formDefinition)
}
this.plugin.closeEditForm()
},
onCancel: () => {
Expand All @@ -77,6 +83,7 @@ export class EditFormView extends ItemView {
console.log('setState of edit form called', state)
if (parseState(state)) {
this.formState = state;
this.originalFormName = state.name;
this.formEditor.$set({ definition: this.formState })
}
return super.setState(state, result);
Expand Down

0 comments on commit ab2e20d

Please sign in to comment.