generated from obsidianmd/obsidian-sample-plugin
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a "select with custom alias" pop-up command #282
- Loading branch information
1 parent
17a37dc
commit e809aa5
Showing
4 changed files
with
147 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { Modal } from "obsidian"; | ||
|
||
export class InputDialog extends Modal { | ||
inputEl!: HTMLInputElement; | ||
promise!: Promise<string | null>; | ||
submitted = false; | ||
|
||
constructor( | ||
public args: { | ||
title: string; | ||
placeholder?: string; | ||
defaultValue?: string; | ||
}, | ||
) { | ||
super(app); | ||
} | ||
|
||
onOpen(): void { | ||
this.titleEl.setText(this.args.title); | ||
|
||
this.inputEl = this.contentEl.createEl("input", { | ||
type: "text", | ||
placeholder: this.args.placeholder ?? "", | ||
cls: "carnelian-input-dialog-input", | ||
value: this.args.defaultValue, | ||
}); | ||
} | ||
|
||
/** | ||
* This function returns | ||
* - Promise<string> if submitted not empty string | ||
* - Promise<""> if submitted empty string | ||
* - Promise<null> if canceled | ||
*/ | ||
open(args?: { initialSelect: boolean }): Promise<string | null> { | ||
super.open(); | ||
|
||
this.promise = new Promise<string | null>((resolve) => { | ||
const listener = (ev: KeyboardEvent) => { | ||
if (ev.isComposing) { | ||
return; | ||
} | ||
if (ev.code === "Enter") { | ||
ev.preventDefault(); | ||
resolve(this.inputEl.value); | ||
this.submitted = true; | ||
this.close(); | ||
} | ||
}; | ||
|
||
this.inputEl.addEventListener("keydown", listener); | ||
|
||
this.onClose = () => { | ||
super.onClose(); | ||
this.inputEl.removeEventListener("keydown", listener); | ||
if (!this.submitted) { | ||
resolve(null); | ||
} | ||
}; | ||
|
||
if (args?.initialSelect) { | ||
this.inputEl.select(); | ||
} | ||
}); | ||
|
||
return this.promise; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters