Skip to content

Commit

Permalink
fix: prompt default_value and placeholder order (#802)
Browse files Browse the repository at this point in the history
  • Loading branch information
shabegom authored Sep 5, 2022
1 parent fb5619c commit fdd1060
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/core/functions/internal_functions/system/PromptModal.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { TemplaterError } from "utils/Error";
import { App, ButtonComponent, Modal, Platform, TextAreaComponent, TextComponent } from "obsidian";
import {
App,
ButtonComponent,
Modal,
Platform,
TextAreaComponent,
TextComponent,
} from "obsidian";

export class PromptModal extends Modal {
private resolve: (value: string) => void;
Expand Down Expand Up @@ -29,7 +36,7 @@ export class PromptModal extends Modal {
this.reject();
}
}

createForm(): void {
const div = this.contentEl.createDiv();
div.addClass("templater-prompt-div");
Expand All @@ -42,28 +49,28 @@ export class PromptModal extends Modal {
buttonDiv.addClass("templater-button-div");
const submitButton = new ButtonComponent(buttonDiv);
submitButton.buttonEl.addClass("mod-cta");
submitButton
.setButtonText("Submit")
.onClick((evt: Event) => {
this.resolveAndClose(evt)
});
submitButton.setButtonText("Submit").onClick((evt: Event) => {
this.resolveAndClose(evt);
});
} else {
textInput = new TextComponent(div);
}

textInput.inputEl.addClass("templater-prompt-input");
textInput.setValue(this.default_value ?? "");
textInput.setPlaceholder("Type text here");
textInput.onChange(value => this.value = value)
textInput.inputEl.addEventListener('keydown', (evt: KeyboardEvent) => this.enterCallback(evt));
textInput.setValue(this.default_value ?? "");
textInput.onChange((value) => (this.value = value));
textInput.inputEl.addEventListener("keydown", (evt: KeyboardEvent) =>
this.enterCallback(evt)
);
}

private enterCallback(evt: KeyboardEvent) {
if (this.multi_line) {
if (Platform.isDesktop) {
if (evt.shiftKey && evt.key === "Enter") {
} else if (evt.key === "Enter") {
this.resolveAndClose(evt)
this.resolveAndClose(evt);
}
} else {
// allow pressing enter on mobile for multi-line input
Expand All @@ -78,7 +85,7 @@ export class PromptModal extends Modal {
}
}

private resolveAndClose(evt: Event|KeyboardEvent) {
private resolveAndClose(evt: Event | KeyboardEvent) {
this.submitted = true;
evt.preventDefault();
this.resolve(this.value);
Expand Down

0 comments on commit fdd1060

Please sign in to comment.