-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
64 lines (56 loc) · 1.68 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
import '@logseq/libs'
import { SettingSchemaDesc } from '@logseq/libs/dist/LSPlugin';
import { evalExpression } from '@hkh12/node-calc'
const settingsSchema: SettingSchemaDesc[] = [
{
key: "calculatorShortcut",
type: "string",
default: "ctrl+enter",
title: "Shortcut",
description: "Keyboard shortcut to call Calculator",
},
]
async function settings_are_valid() {
const shortcut = logseq.settings!["calculatorShortcut"]
if (!shortcut) {
console.error("Keyboard shortcut not configured for Calculator.")
logseq.UI.showMsg(
"Please configure a keyboard shortcut for Calculator.",
"error"
)
return false
}
return true
}
async function main() {
logseq.useSettingsSchema(settingsSchema)
if (!await settings_are_valid()) {
console.error("Calculator settings are invalid, exiting.")
return
}
const shortcut = logseq.settings!["calculatorShortcut"]
logseq.App.registerCommandShortcut({
binding: shortcut,
mode: "global"
},
async () => {
const current_block = await logseq.Editor.getCurrentBlock()
const input = await logseq.Editor.getEditingBlockContent()
if (input) {
// console.log("Evaluating input: ", input)
try {
const result = evalExpression(input)
logseq.Editor.insertBlock(current_block!.uuid, `${result}`, { sibling: false })
logseq.Editor.insertBlock(current_block!.uuid, "", { sibling: true })
} catch (e) {
logseq.UI.showMsg(`Error: ${e}`, "error")
}
}
else {
console.log("Calculator: No input, skipping...")
}
}
)
console.log('Calculator: ready.')
}
logseq.ready(main).catch(console.error)