Skip to content

Commit

Permalink
feat: command to directly edit a form
Browse files Browse the repository at this point in the history
  • Loading branch information
danielo515 committed Jan 6, 2024
1 parent 91494c8 commit 47d4d8c
Showing 1 changed file with 34 additions and 39 deletions.
73 changes: 34 additions & 39 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { O } from "@std";
import { executeTemplate } from "./core/template/templateParser";
import { NewNoteModal } from "./suggesters/NewNoteModal";
import { file_exists } from "./utils/files";
import { FormPickerModal } from "./suggesters/FormPickerModal";

type ViewType = typeof EDIT_FORM_VIEW | typeof MANAGE_FORMS_VIEW;

Expand Down Expand Up @@ -61,7 +62,7 @@ function notifyMigrationErrors(errors: MigrationError[]) {
// This is the plugin entrypoint
export default class ModalFormPlugin extends Plugin {
public settings: ModalFormSettings | undefined;
private unsubscribeSettingsStore: () => void = () => { };
private unsubscribeSettingsStore: () => void = () => {};
// This things will be setup in the onload function rather than constructor
public api!: PublicAPI;

Expand All @@ -84,9 +85,7 @@ export default class ModalFormPlugin extends Plugin {
// then if you save another form you will unexpectedly save the mutated form too.
// Maybe we could instead do a deep copy instead, but until this proven to be a bottleneck I will leave it like this.
const savedSettings = await this.getSettings();
const formDefinition = savedSettings.formDefinitions.find(
(form) => form.name === formName,
);
const formDefinition = savedSettings.formDefinitions.find((form) => form.name === formName);
if (!formDefinition) {
throw new ModalFormError(`Form ${formName} not found`);
}
Expand All @@ -107,14 +106,10 @@ export default class ModalFormPlugin extends Plugin {

async activateView(viewType: ViewType, state?: FormDefinition) {
const { workspace } = this.app;
let leaf: WorkspaceLeaf | undefined =
workspace.getLeavesOfType(viewType)[0];
let leaf: WorkspaceLeaf | undefined = workspace.getLeavesOfType(viewType)[0];
if (leaf) {
console.info("found leaf, no reason to create a new one");
} else if (
Platform.isMobile ||
this.settings?.editorPosition === "mainView"
) {
} else if (Platform.isMobile || this.settings?.editorPosition === "mainView") {
leaf = this.app.workspace.getLeaf("tab");
} else if (this.settings?.editorPosition === "right") {
leaf = this.app.workspace.getRightLeaf(false);
Expand Down Expand Up @@ -142,11 +137,10 @@ export default class ModalFormPlugin extends Plugin {
const [migrationIsNeeded, settings] = pipe(
parseSettings(data),
E.map((settings): [boolean, ModalFormSettings] => {
const migrationIsNeeded =
settings.formDefinitions.some(formNeedsMigration);
const { right: formDefinitions, left: errors } = A.partitionMap(
migrateToLatest,
)(settings.formDefinitions);
const migrationIsNeeded = settings.formDefinitions.some(formNeedsMigration);
const { right: formDefinitions, left: errors } = A.partitionMap(migrateToLatest)(
settings.formDefinitions,
);
notifyParsingErrors(errors);
const validSettings: ModalFormSettings = {
...settings,
Expand Down Expand Up @@ -175,8 +169,8 @@ export default class ModalFormPlugin extends Plugin {

attachShortcutToGlobalWindow() {
if (!this.settings) {
log_error(new ModalFormError("Settings not loaded yet"))
return
log_error(new ModalFormError("Settings not loaded yet"));
return;
}
const globalNamespace = this.settings.globalNamespace;
if (this.settings?.attachShortcutToGlobalWindow) {
Expand All @@ -190,6 +184,13 @@ export default class ModalFormPlugin extends Plugin {
await this.saveSettings();
}

get validFormDefinitions(): FormDefinition[] {
return pipe(
this.settings!.formDefinitions,
A.filterMap((form) => (form instanceof MigrationError ? O.none : O.some(form))),
);
}

async onload() {
const settings = await this.getSettings();
if (settings.formDefinitions.length === 0) {
Expand All @@ -203,14 +204,8 @@ export default class ModalFormPlugin extends Plugin {
});
this.api = new API(this.app, this);
this.attachShortcutToGlobalWindow();
this.registerView(
EDIT_FORM_VIEW,
(leaf) => new EditFormView(leaf, this),
);
this.registerView(
MANAGE_FORMS_VIEW,
(leaf) => new ManageFormsView(leaf, this),
);
this.registerView(EDIT_FORM_VIEW, (leaf) => new EditFormView(leaf, this));
this.registerView(MANAGE_FORMS_VIEW, (leaf) => new ManageFormsView(leaf, this));

// This creates an icon in the left ribbon.
this.addRibbonIcon("documents", "Edit forms", (evt: MouseEvent) => {
Expand Down Expand Up @@ -239,6 +234,16 @@ export default class ModalFormPlugin extends Plugin {
},
});

this.addCommand({
id: "edit-form",
name: "Edit form",
callback: async () => {
new FormPickerModal(this.app, this.validFormDefinitions, (formToEdit) => {
this.activateView(EDIT_FORM_VIEW, formToEdit);
}).open();
},
});

// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new ModalFormSettingTab(this.app, this));
}
Expand All @@ -250,13 +255,9 @@ export default class ModalFormPlugin extends Plugin {
* @returns a unique name for the note, full path including the extension
*/
getUniqueNoteName(name: string, destinationFolder?: string): string {
const defaultNotesFolder = this.app.fileManager.getNewFileParent(
"",
"note.md",
);
const defaultNotesFolder = this.app.fileManager.getNewFileParent("", "note.md");
function makePath(name: string, folder?: string, suffix?: number) {
return `${folder || defaultNotesFolder.path}/${name}${suffix ? "-" + suffix : ""
}.md`;
return `${folder || defaultNotesFolder.path}/${name}${suffix ? "-" + suffix : ""}.md`;
}
let destinationPath = makePath(name, destinationFolder);
let i = 1;
Expand Down Expand Up @@ -291,14 +292,8 @@ export default class ModalFormPlugin extends Plugin {
destinationFolder: string,
) => {
const formData = await this.api.openForm(form);
const newNoteFullPath = this.getUniqueNoteName(
noteName,
destinationFolder,
);
const noteContent = executeTemplate(
form.template.parsedTemplate,
formData.getData(),
);
const newNoteFullPath = this.getUniqueNoteName(noteName, destinationFolder);
const noteContent = executeTemplate(form.template.parsedTemplate, formData.getData());
console.log("new note content", noteContent);
this.app.vault.create(newNoteFullPath, noteContent);
};
Expand Down

0 comments on commit 47d4d8c

Please sign in to comment.