-
Notifications
You must be signed in to change notification settings - Fork 86
/
index.ts
80 lines (68 loc) · 2.13 KB
/
index.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import '@logseq/libs'
import { EmojiButton } from '@joeattardi/emoji-button'
/**
* main entry
*/
async function main () {
const appUserConfig = await logseq.App.getUserConfigs()
const emojiPickerEl = document.createElement('div')
emojiPickerEl.classList.add('emoji-picker-trigger')
document.getElementById('app').appendChild(emojiPickerEl)
let picker = null
let makePicker = () => {
if (picker) return picker
picker = new EmojiButton({
position: 'bottom-start',
theme: appUserConfig.preferredThemeMode,
})
picker.on('emoji', async (selection) => {
logseq.hideMainUI()
await logseq.Editor.insertAtEditingCursor(selection.emoji)
})
// Hack search key arrow down
const searchInput = document.querySelector('.emoji-picker__search')! as HTMLInputElement
searchInput.addEventListener('keydown', (e) => {
const isDown = (e as KeyboardEvent).which === 40
if (!isDown) return
const emojiCnt = document.querySelector('.emoji-picker__container')! as HTMLDivElement
(emojiCnt.querySelector('.emoji-picker__emoji') as HTMLElement).focus()
e.preventDefault()
})
//ESC
document.addEventListener('keydown', function (e) {
if (e.keyCode === 27) {
logseq.hideMainUI({ restoreEditingCursor: true })
}
e.stopPropagation()
}, false)
document.addEventListener('click', (e) => {
if (!(e.target as HTMLElement).closest('.emoji-picker__wrapper')) {
logseq.hideMainUI({ restoreEditingCursor: true })
}
})
logseq.App.onThemeModeChanged(({ mode }) => {
picker.setTheme(mode)
})
return picker
}
// Emoji picker
logseq.Editor.registerSlashCommand(
'😀 Emoji picker', async () => {
const {
left,
top,
rect,
} = await logseq.Editor.getEditingCursorPosition()
Object.assign(emojiPickerEl.style, {
top: top + rect.top + 'px',
left: left + rect.left + 'px',
})
logseq.showMainUI()
setTimeout(() => {
makePicker().showPicker(emojiPickerEl)
}, 100)
},
)
}
// bootstrap
logseq.ready(main).catch(console.error)