์ปจํ ์คํธ ๋ฉ๋ด๋ฅผ ์ด๊ณ ์ถ๋ค๋ฉด Menu๋ฅผ ์ฌ์ฉํ์ธ์:
import { Menu, Notice, Plugin } from 'obsidian';
export default class ExamplePlugin extends Plugin {
async onload() {
this.addRibbonIcon('dice', 'Open menu', (event) => {
const menu = new Menu();
menu.addItem((item) =>
item
.setTitle('Copy')
.setIcon('documents')
.onClick(() => {
new Notice('Copied');
})
);
menu.addItem((item) =>
item
.setTitle('Paste')
.setIcon('paste')
.onClick(() => {
new Notice('Pasted');
})
);
menu.showAtMouseEvent(event);
});
}
}
showAtMouseEvent()๋ ๋ง์ฐ์ค๋ก ํด๋ฆญํ ์์น์ ๋ฉ๋ด๋ฅผ ์ฝ๋๋ค.
Tip
๋ฉ๋ด๊ฐ ๋ํ๋๋ ์์น๋ฅผ ๋ ์ธ๋ฐํ๊ฒ ์ ์ดํด์ผ ํ๋ ๊ฒฝ์ฐ,
menu.showAtPosition({ x: 20, y: 20 })
์ ์ฌ์ฉํ์ฌ Obsidian ์ฐฝ์ ์ผ์ชฝ ์๋จ ๋ชจ์๋ฆฌ๋ฅผ ๊ธฐ์ค์ผ๋ก ํ ์์น์ ๋ฉ๋ด๋ฅผ ์ด ์ ์์ต๋๋ค.
์ฌ์ฉํ ์ ์๋ ์์ด์ฝ์ ๋ํ ์์ธํ ๋ด์ฉ์ Icons๋ฅผ ์ฐธ์กฐํ์ธ์.
file-menu
๋ฐ editor-menu
์์
๊ณต๊ฐ ์ด๋ฒคํธ๋ฅผ ๊ตฌ๋
ํ์ฌ ํ์ผ ๋ฉ๋ด ๋๋ ์๋ํฐ ๋ฉ๋ด์ ํญ๋ชฉ์ ์ถ๊ฐํ ์๋ ์์ต๋๋ค:
import { Notice, Plugin } from 'obsidian';
export default class ExamplePlugin extends Plugin {
async onload() {
this.registerEvent(
this.app.workspace.on('file-menu', (menu, file) => {
menu.addItem((item) => {
item
.setTitle('Print file path ๐')
.setIcon('document')
.onClick(async () => {
new Notice(file.path);
});
});
})
);
this.registerEvent(
this.app.workspace.on("editor-menu", (menu, editor, view) => {
menu.addItem((item) => {
item
.setTitle('Print file path ๐')
.setIcon('document')
.onClick(async () => {
new Notice(view.file.path);
});
});
})
);
}
}
์ด๋ฒคํธ ์ฒ๋ฆฌ์ ๋ํ ์์ธํ ๋ด์ฉ์ Events๋ฅผ ์ฐธ์กฐํ์ธ์.