λͺ¨λ¬μ μ 보λ₯Ό νμνκ³ μ¬μ©μλ‘λΆν° μ λ ₯μ λ°μ΅λλ€. λͺ¨λ¬μ μμ±νλ €λ©΄ Modalμ νμ₯νλ ν΄λμ€λ₯Ό λ§λλλ€:
import { App, Modal } from 'obsidian';
export class ExampleModal extends Modal {
constructor(app: App) {
super(app);
this.setContent('Look at me, I\'m a modal! π')
}
}
λͺ¨λ¬μ μ΄λ €λ©΄ ExampleModal
μ μ μΈμ€ν΄μ€λ₯Ό λ§λ€κ³ open()μ νΈμΆν©λλ€:
import { Plugin } from 'obsidian';
import { ExampleModal } from './modal';
export default class ExamplePlugin extends Plugin {
async onload() {
this.addCommand({
id: 'display-modal',
name: 'Display modal',
callback: () => {
new ExampleModal(this.app).open();
},
});
}
}
μ¬μ©μ μ λ ₯ λ°κΈ°
μ΄μ μμ μ λͺ¨λ¬μ ν μ€νΈλ§ νμνμ΅λλ€. μ¬μ©μ μ λ ₯μ μ²λ¦¬νλ μ’ λ 볡μ‘ν μμ λ₯Ό μ΄ν΄λ³΄κ² μ΅λλ€.
import { App, Modal, Setting } from 'obsidian';
export class ExampleModal extends Modal {
constructor(app: App, onSubmit: (result: string) => void) {
super(app);
this.setTitle('What\'s your name?');
let name = '';
new Setting(this.contentEl)
.setName('Name')
.addText((text) =>
text.onChange((value) => {
name = value;
}));
new Setting(this.contentEl)
.addButton((btn) =>
btn
.setButtonText('Submit')
.setCta()
.onClick(() => {
this.close();
onSubmit(name);
}));
}
}
μ¬μ©μκ° Submitμ ν΄λ¦νλ©΄ onSubmit
μ½λ°±μμ κ²°κ³Όκ° λ°νλ©λλ€:
new ExampleModal(this.app, (result) => {
new Notice(`Hello, ${result}!`);
}).open();
μ μ λͺ©λ‘μμ μ ννκΈ°
SuggestModalμ μ¬μ©μμκ² μ μ λͺ©λ‘μ νμν μ μλ νΉμ λͺ¨λ¬μ λλ€.
import { App, Notice, SuggestModal } from 'obsidian';
interface Book {
title: string;
author: string;
}
const ALL_BOOKS = [
{
title: 'How to Take Smart Notes',
author: 'SΓΆnke Ahrens',
},
{
title: 'Thinking, Fast and Slow',
author: 'Daniel Kahneman',
},
{
title: 'Deep Work',
author: 'Cal Newport',
},
];
export class ExampleModal extends SuggestModal<Book> {
// μ¬μ© κ°λ₯ν λͺ¨λ μ μμ λ°νν©λλ€.
getSuggestions(query: string): Book[] {
return ALL_BOOKS.filter((book) =>
book.title.toLowerCase().includes(query.toLowerCase())
);
}
// κ° μ μ νλͺ©μ λ λλ§ν©λλ€.
renderSuggestion(book: Book, el: HTMLElement) {
el.createEl('div', { text: book.title });
el.createEl('small', { text: book.author });
}
// μ νλ μ μμ λν μμ
μ μνν©λλ€.
onChooseSuggestion(book: Book, evt: MouseEvent | KeyboardEvent) {
new Notice(`Selected ${book.title}`);
}
}
Obsidian APIλ SuggestModal
μΈμλ μ μμ μν ν¨μ¬ λ μ λ¬Ένλ μ νμ λͺ¨λ¬μΈ FuzzySuggestModalμ μ 곡ν©λλ€. κ° νλͺ©μ΄ λ λλ§λλ λ°©μμ λν λμΌν μ μ΄κΆμ μ 곡νμ§λ μμ§λ§, κΈ°λ³Έμ μΌλ‘ μ μ¬ λ¬Έμμ΄ κ²μ(fuzzy string search)μ μ¬μ©ν μ μμ΅λλ€.
export class ExampleModal extends FuzzySuggestModal<Book> {
getItems(): Book[] {
return ALL_BOOKS;
}
getItemText(book: Book): string {
return book.title;
}
onChooseItem(book: Book, evt: MouseEvent | KeyboardEvent) {
new Notice(`Selected ${book.title}`);
}
}