generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.ts
60 lines (52 loc) · 1.66 KB
/
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { Menu, Notice, Plugin } from "obsidian";
import { SearchLeafView } from "./types";
declare module "obsidian" {
interface Workspace {
on(name: "search:results-menu", callback: (menu: Menu) => any): EventRef;
}
}
export default class CopySearchUrl extends Plugin {
async onload() {
this.app.workspace.onLayoutReady(() => {
if (this.isSearchDisabled()) {
new Notice(
"Core search plugin is disabled, can't set up Copy Search URL plugin! Please enable core Search plugin and reload.",
);
return;
}
this.registerEvent(
this.app.workspace.on(
"search:results-menu",
(menu: Menu) => {
menu.addItem((item) => {
item
.setTitle("Copy Obsidian search URL")
.setIcon("link")
.onClick(async () => {
if (this.getSearchQuery() === "") {
return;
}
await navigator.clipboard.writeText(this.getObsidianUrl());
new Notice("Obsidian search URL copied!");
});
});
},
),
);
});
}
private isSearchDisabled() {
return this.getSearchLeaf() === undefined;
}
private getSearchLeaf() {
return this.app.workspace.getLeavesOfType("search")[0];
}
private getSearchQuery() {
return (<SearchLeafView> this.getSearchLeaf().view)?.getQuery() || "";
}
private getObsidianUrl() {
const query = encodeURIComponent(this.getSearchQuery());
const vault = encodeURIComponent(this.app.vault.getName());
return `obsidian://search?vault=${vault}&query=${query}`;
}
}