λͺ…λ Ήμ–΄λŠ” μ‚¬μš©μžκ°€ λͺ…λ Ήμ–΄ νŒ”λ ˆνŠΈ(Command Palette)μ—μ„œ λ˜λŠ” 단좕킀λ₯Ό μ‚¬μš©ν•˜μ—¬ μˆ˜ν–‰ν•  수 μžˆλŠ” μž‘μ—…μž…λ‹ˆλ‹€.

ν”ŒλŸ¬κ·ΈμΈμ— μƒˆ λͺ…λ Ήμ–΄λ₯Ό λ“±λ‘ν•˜λ €λ©΄ onload() λ©”μ†Œλ“œ λ‚΄μ—μ„œ addCommand() λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•©λ‹ˆλ‹€:

import { Plugin } from 'obsidian';
 
export default class ExamplePlugin extends Plugin {
  async onload() {
    this.addCommand({
      id: 'print-greeting-to-console',
      name: 'Print greeting to console',
      callback: () => {
        console.log('Hey, you!');
      },
    });
  }
}

쑰건뢀 λͺ…λ Ήμ–΄

λͺ…λ Ήμ–΄κ°€ νŠΉμ • μ‘°κ±΄μ—μ„œλ§Œ 싀행될 수 μžˆλŠ” 경우, λŒ€μ‹  checkCallback() μ‚¬μš©μ„ κ³ λ €ν•˜μ„Έμš”.

checkCallback은 두 번 μ‹€ν–‰λ©λ‹ˆλ‹€. 첫째, λͺ…λ Ήμ–΄κ°€ 싀행될 수 μžˆλŠ”μ§€ μ˜ˆλΉ„ 확인을 μˆ˜ν–‰ν•©λ‹ˆλ‹€. λ‘˜μ§Έ, μž‘μ—…μ„ μˆ˜ν–‰ν•©λ‹ˆλ‹€.

두 μ‹€ν–‰ 사이에 μ‹œκ°„μ΄ κ²½κ³Όν•  수 μžˆμœΌλ―€λ‘œ, 두 호좜 λͺ¨λ‘μ—μ„œ 확인을 μˆ˜ν–‰ν•΄μ•Ό ν•©λ‹ˆλ‹€.

콜백이 μ˜ˆλΉ„ 확인을 μˆ˜ν–‰ν•΄μ•Ό ν•˜λŠ”μ§€ λ˜λŠ” μž‘μ—…μ„ μˆ˜ν–‰ν•΄μ•Ό ν•˜λŠ”μ§€ κ²°μ •ν•˜κΈ° μœ„ν•΄ checking μΈμˆ˜κ°€ μ½œλ°±μ— μ „λ‹¬λ©λ‹ˆλ‹€.

  • checking이 true둜 μ„€μ •λœ 경우, μ˜ˆλΉ„ 확인을 μˆ˜ν–‰ν•©λ‹ˆλ‹€.
  • checking이 false둜 μ„€μ •λœ 경우, μž‘μ—…μ„ μˆ˜ν–‰ν•©λ‹ˆλ‹€.

λ‹€μŒ 예제의 λͺ…λ Ήμ–΄λŠ” ν•„μˆ˜ 값에 따라 λ‹¬λΌμ§‘λ‹ˆλ‹€. 두 μ‹€ν–‰ λͺ¨λ‘μ—μ„œ μ½œλ°±μ€ 값이 μžˆλŠ”μ§€ ν™•μΈν•˜μ§€λ§Œ checking이 false인 κ²½μš°μ—λ§Œ μž‘μ—…μ„ μˆ˜ν–‰ν•©λ‹ˆλ‹€.

this.addCommand({
  id: 'example-command',
  name: 'Example command',
  // highlight-next-line
  checkCallback: (checking: boolean) => {
    const value = getRequiredValue();
 
    if (value) {
      if (!checking) {
        doCommand(value);
      }
 
      return true
    }
 
    return false;
  },
});

에디터 λͺ…λ Ήμ–΄

λͺ…λ Ήμ–΄κ°€ 에디터에 μ ‘κ·Όν•΄μ•Ό ν•˜λŠ” 경우, ν™œμ„± 에디터와 ν•΄λ‹Ή λ·°λ₯Ό 인수둜 μ œκ³΅ν•˜λŠ” editorCallback()을 μ‚¬μš©ν•  μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.

this.addCommand({
  id: 'example-command',
  name: 'Example command',
  editorCallback: (editor: Editor, view: MarkdownView) => {
    const sel = editor.getSelection()
 
    console.log(`You have selected: ${sel}`);
  },
}

Note

에디터 λͺ…λ Ήμ–΄λŠ” ν™œμ„± 에디터λ₯Ό μ‚¬μš©ν•  수 μžˆμ„ λ•Œλ§Œ λͺ…λ Ήμ–΄ νŒ”λ ˆνŠΈμ— λ‚˜νƒ€λ‚©λ‹ˆλ‹€.

에디터 콜백이 νŠΉμ • μ‘°κ±΄μ—μ„œλ§Œ 싀행될 수 μžˆλŠ” 경우, λŒ€μ‹  editorCheckCallback() μ‚¬μš©μ„ κ³ λ €ν•˜μ„Έμš”. μžμ„Έν•œ λ‚΄μš©μ€ Conditional commandsλ₯Ό μ°Έμ‘°ν•˜μ„Έμš”.

this.addCommand({
  id: 'example-command',
  name: 'Example command',
  editorCheckCallback: (checking: boolean, editor: Editor, view: MarkdownView) => {
    const value = getRequiredValue();
 
    if (value) {
      if (!checking) {
        doCommand(value);
      }
 
      return true
    }
 
    return false;
  },
});

단좕킀(Hot keys)

μ‚¬μš©μžλŠ” ν‚€λ³΄λ“œ 단좕킀 λ˜λŠ” 단좕킀(hot key) λ₯Ό μ‚¬μš©ν•˜μ—¬ λͺ…λ Ήμ–΄λ₯Ό μ‹€ν–‰ν•  수 μžˆμŠ΅λ‹ˆλ‹€. μ‚¬μš©μžκ°€ 직접 ꡬ성할 μˆ˜λ„ μžˆμ§€λ§Œ, κΈ°λ³Έ 단좕킀λ₯Ό μ œκ³΅ν•  μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.

Warning

λ‹€λ₯Έ μ‚¬λžŒμ΄ μ‚¬μš©ν•˜λ„λ‘ μ˜λ„λœ ν”ŒλŸ¬κ·ΈμΈμ— κΈ°λ³Έ 단좕킀λ₯Ό μ„€μ •ν•˜μ§€ λ§ˆμ„Έμš”. λ‹¨μΆ•ν‚€λŠ” λ‹€λ₯Έ ν”ŒλŸ¬κ·ΈμΈμ΄λ‚˜ μ‚¬μš©μž μžμ‹ μ΄ μ •μ˜ν•œ 단좕킀와 μΆ©λŒν•  κ°€λŠ₯성이 λ†’μŠ΅λ‹ˆλ‹€.

이 μ˜ˆμ œμ—μ„œ μ‚¬μš©μžλŠ” Ctrl(Macμ—μ„œλŠ” Cmd)κ³Ό Shiftλ₯Ό ν•¨κ»˜ λˆ„λ₯Έ λ‹€μŒ ν‚€λ³΄λ“œμ—μ„œ a ν‚€λ₯Ό 눌러 λͺ…λ Ήμ–΄λ₯Ό μ‹€ν–‰ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

this.addCommand({
  id: 'example-command',
  name: 'Example command',
  hotkeys: [{ modifiers: ['Mod', 'Shift'], key: 'a' }],
  callback: () => {
    console.log('Hey, you!');
  },
});

Note

Mod ν‚€λŠ” Windows 및 Linuxμ—μ„œλŠ” Ctrl이 되고 macOSμ—μ„œλŠ” Cmdκ°€ λ˜λŠ” 특수 μˆ˜μ •μž ν‚€μž…λ‹ˆλ‹€.