generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
42 lines (37 loc) · 1.08 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
import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Scope, Setting } from 'obsidian';
let lastKeyupTime = 0;
let lastKeyWasShifted:boolean
function openSearchWhenDoubleShift(event:KeyboardEvent,app:App){
let key = event.key
if (key !== "Shift") {
lastKeyupTime = 0;
return;
}
if(lastKeyWasShifted){
lastKeyWasShifted = false;
return;
}
if (Date.now() - lastKeyupTime < 500) {
lastKeyupTime = 0;
simulateSearchHotkey(app)
return;
}
lastKeyupTime = Date.now();
}
function clearTimerWhenShifted(event:KeyboardEvent){
let key = event.key
let shiftKey = event.shiftKey;
if(key !== "Shift" && shiftKey===true){
lastKeyWasShifted = true
}
}
function simulateSearchHotkey(app:App){
// @ts-ignore
app.commands.executeCommandById('global-search:open')
}
export default class SearchEverywherePlugin extends Plugin {
async onload() {
this.registerDomEvent(window, 'keyup', (event) => openSearchWhenDoubleShift(event,this.app))
this.registerDomEvent(window, 'keydown', (event) => clearTimerWhenShifted(event))
}
}