λͺ¨λ‹¬μ€ 정보λ₯Ό ν‘œμ‹œν•˜κ³  μ‚¬μš©μžλ‘œλΆ€ν„° μž…λ ₯을 λ°›μŠ΅λ‹ˆλ‹€. λͺ¨λ‹¬μ„ μƒμ„±ν•˜λ €λ©΄ 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}`);
  }
}