generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
26 lines (20 loc) · 789 Bytes
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { Plugin, MarkdownPostProcessorContext, Notice } from 'obsidian';
const COPY_SUCCESS_MESSAGE = "Code copied";
export default class AltClickToCopy extends Plugin {
onload(): void {
this.registerMarkdownPostProcessor((el: HTMLElement, ctx: MarkdownPostProcessorContext) => this.processMarkdown(el, ctx));
}
processMarkdown(el: HTMLElement, ctx: MarkdownPostProcessorContext) {
const codeBlocks = el.querySelectorAll('code');
codeBlocks.forEach(block => {
block.addEventListener('click', this.handleCodeBlockClick.bind(this));
});
}
handleCodeBlockClick(event: MouseEvent) {
if (event.altKey) {
const blockContent = (event.target as HTMLElement).textContent;
navigator.clipboard.writeText(blockContent || '');
new Notice(COPY_SUCCESS_MESSAGE);
}
}
}