λͺ λ Ήμ΄λ μ¬μ©μκ° λͺ λ Ήμ΄ νλ νΈ(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κ° λλ νΉμ μμ μ ν€μ λλ€.