Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/import-forms #365

Merged
merged 2 commits into from
Dec 20, 2024
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
1 change: 0 additions & 1 deletion src/core/template/templateParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ const open = S.fold([S.string("{{"), S.spaces]);
const close = P.expected(S.fold([S.spaces, S.string("}}")]), 'closing variable tag: "}}"');
const identifier = S.many1(C.alphanum);
const transformation = pipe(
// dam prettier
S.fold([S.spaces, S.string("|"), S.spaces]),
P.apSecond(identifier),
P.map((x) => {
Expand Down
40 changes: 24 additions & 16 deletions src/store/SettngsStore.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
import { A, pipe } from '@std';
import { FormDefinition, duplicateForm } from 'src/core/formDefinition';
import { MigrationError } from 'src/core/formDefinitionSchema';
import { ModalFormSettings, getDefaultSettings } from 'src/core/settings';
import { writable, derived } from 'svelte/store';
import { A, pipe } from "@std";
import { FormDefinition, duplicateForm } from "src/core/formDefinition";
import { MigrationError } from "src/core/formDefinitionSchema";
import { ModalFormSettings, getDefaultSettings } from "src/core/settings";
import { derived, writable } from "svelte/store";

const settings = writable({ ...getDefaultSettings() });
export const formsStore = derived(settings, ($settings) => pipe(
$settings.formDefinitions,
A.filter((form): form is FormDefinition => !(form instanceof MigrationError))));
const { subscribe, update, set } = settings
export const formsStore = derived(settings, ($settings) =>
pipe(
$settings.formDefinitions,
A.filter((form): form is FormDefinition => !(form instanceof MigrationError)),
),
);
const { subscribe, update, set } = settings;

export const invalidFormsStore = derived(settings, ($settings) => {
return pipe(
$settings.formDefinitions,
A.filter((form): form is MigrationError => form instanceof MigrationError));
})
A.filter((form): form is MigrationError => form instanceof MigrationError),
);
});

export const settingsStore = {
subscribe,
set,
updateForm(name: string, form: FormDefinition) {
let edited = false;
update((s): ModalFormSettings => {
const forms = s.formDefinitions.map((f) => {
if (f.name === name) return form;
if (f.name === name) {
edited = true;
return form;
}
return f;
});
return { ...s, formDefinitions: forms };
return { ...s, formDefinitions: edited ? forms : [...s.formDefinitions, form] };
});
},
addNewForm(form: FormDefinition) {
Expand All @@ -42,7 +50,7 @@ export const settingsStore = {
},
duplicateForm(formName: string) {
update((s): ModalFormSettings => {
return { ...s, formDefinitions: duplicateForm(formName, s.formDefinitions) }
return { ...s, formDefinitions: duplicateForm(formName, s.formDefinitions) };
});
}
}
},
};
58 changes: 30 additions & 28 deletions src/views/EditFormView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ import { settingsStore } from "src/store/SettngsStore";
import { log_notice } from "src/utils/Log";
import type { EditableFormDefinition, FormDefinition } from "../core/formDefinition";
import ModalFormPlugin from "../main";
import FormEditor from './FormBuilder.svelte';
import FormEditor from "./FormBuilder.svelte";

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

function parseState(maybeState: unknown): maybeState is EditableFormDefinition {
if (maybeState === null) {
return false
return false;
}
if (typeof maybeState !== 'object') {
return false
if (typeof maybeState !== "object") {
return false;
}
if ('title' in maybeState && 'name' in maybeState && 'fields' in maybeState) {
return true
if ("title" in maybeState && "name" in maybeState && "fields" in maybeState) {
return true;
}
return false;
}
Expand All @@ -26,12 +26,15 @@ function parseState(maybeState: unknown): maybeState is EditableFormDefinition {
* Simple, right?
*/
export class EditFormView extends ItemView {
formState: EditableFormDefinition = { title: '', name: '', version: '1', fields: [] };
formState: EditableFormDefinition = { title: "", name: "", version: "1", fields: [] };
originalFormName?: string;
formEditor!: FormEditor;
constructor(readonly leaf: WorkspaceLeaf, readonly plugin: ModalFormPlugin) {
constructor(
readonly leaf: WorkspaceLeaf,
readonly plugin: ModalFormPlugin,
) {
super(leaf);
this.icon = 'note-glyph'
this.icon = "note-glyph";
}

getViewType() {
Expand All @@ -43,55 +46,54 @@ export class EditFormView extends ItemView {
}

async onOpen() {
this.containerEl.empty()
this.containerEl.empty();
this.formEditor = new FormEditor({
target: this.containerEl,
props: {
definition: this.formState,
app: this.app,
onChange: () => {
console.log('Save form state', this.formState)
this.app.workspace.requestSaveLayout()
console.log("Save form state", this.formState);
this.app.workspace.requestSaveLayout();
},
onSubmit: (formDefinition: FormDefinition) => {
console.log('Submitting form', { formDefinition });
if (this.originalFormName && this.originalFormName !== '') {
settingsStore.updateForm(this.originalFormName, formDefinition)
if (this.originalFormName && this.originalFormName !== "") {
console.log("Updating form", { formDefinition });
settingsStore.updateForm(this.originalFormName, formDefinition);
} else {
settingsStore.addNewForm(formDefinition)
console.log("Adding new form", { formDefinition });
settingsStore.addNewForm(formDefinition);
}
this.plugin.closeEditForm()
this.plugin.closeEditForm();
},
onCancel: () => {
this.plugin.closeEditForm()
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)
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')
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)
console.log("setState of edit form called", state);
if (parseState(state)) {
this.formState = state;
this.originalFormName = state.name;
this.formEditor.$set({ definition: this.formState })
this.formEditor.$set({ definition: this.formState });
}
return super.setState(state, result);
}
getState() {
return this.formState;
}
}


6 changes: 3 additions & 3 deletions src/views/FormImport.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as J from "fp-ts/Json";
import { O, type Option, ensureError, pipe } from "@std";
import * as E from "fp-ts/Either";
import * as J from "fp-ts/Json";
import { FormDefinition } from "src/core/formDefinition";
import { InvalidData, MigrationError, migrateToLatest } from "src/core/formDefinitionSchema";
import { Readable, writable } from "svelte/store";
import { FormDefinition } from "src/core/formDefinition";

type State = E.Either<string[], Option<FormDefinition>>;
type UiState = {
Expand Down Expand Up @@ -44,7 +44,7 @@ function matchState<T>(state: State, matchers: Matchers<T>): T {

function noop() {}

export function makeFormInputModel({ createForm }: FormImportDeps): FormImportModel {
export function makeFormImportModel({ createForm }: FormImportDeps): FormImportModel {
const state = writable<State>(E.of(O.none));
const setErrors = (errors: string[]) => state.set(E.left(errors));
const resetState = () => state.set(E.of(O.none));
Expand Down
4 changes: 2 additions & 2 deletions src/views/FormImportView.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { App, Modal } from "obsidian";
import { FormImportDeps, makeFormImportModel } from "./FormImport";
import FormImport from "./FormImport.svelte";
import { FormImportDeps, makeFormInputModel } from "./FormImport";
/**
* This class is just the minimum glue code to bind our core logic
* with the svelte UI and obsidian API modal.
Expand All @@ -23,7 +23,7 @@ export class FormImportModal extends Modal {
const { contentEl } = this;
this._component = new FormImport({
target: contentEl,
props: { model: makeFormInputModel(this.deps) },
props: { model: makeFormImportModel(this.deps) },
});
}
}
Loading