generated from sawhney17/logseq-plugin-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
85 lines (82 loc) · 2.69 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
81
82
83
84
85
import "@logseq/libs";
import {
BlockEntity,
SettingSchemaDesc,
} from "@logseq/libs/dist/LSPlugin.user";
import "logseq-dateutils";
import { getDateForPageWithoutBrackets } from "logseq-dateutils";
import SimpleDateFormat from 'simple_dt.js';
const settings: SettingSchemaDesc[] = [
{
key: "keyboardShortcut",
title: "Keyboard shortcut",
description: "Keyboard shortcut to trigger going home",
type: "string",
default: "mod+g",
},
{
key: "enterEditingMode",
title: "Scroll to or enter editing mode for block?",
description:
"When you click the shortcut, should the ball enter editing mode or not?",
type: "enum",
default: "scroll",
enumChoices: ["scroll", "enter"],
enumPicker: "radio",
},
{
key: "customTimeout",
title: "Custom timeout",
description: "Custom timeout entering the editing mode, if you notice excess lag, reduce this number, if you notice entering edit mode isn't working properly, increase this number",
type: "number",
default: 300,
},
{
key: "useAlternateDateParser",
title: "Use alternate date parser?",
description: "Use the alternate date parser?",
type: "boolean",
default: false,
}
];
const triggerEnter = () => {
setTimeout(() => {
//Trigger the enter key on the keyboard
top?.window.dispatchEvent(
new KeyboardEvent("keydown", {
key: "enter",
})
);
console.log("enter key pressed");
}, logseq.settings?.customTimeout || 300);
};
const main = async () => {
logseq.useSettingsSchema(settings);
logseq.App.registerCommandPalette(
{
key: "Go home now!",
label: "Go to todays journal, append a new block and scroll to it",
keybinding: { binding: logseq.settings?.keyboardShortcut },
},
async () => {
const dateFormat = (await logseq.App.getUserConfigs()).preferredDateFormat;
const language = (await logseq.App.getUserConfigs()).preferredLanguage;
const date = logseq.settings?.useAlternateDateParser ? SimpleDateFormat.get(language).format('#'+dateFormat, new Date()): getDateForPageWithoutBrackets(new Date(), dateFormat);
const homepage: BlockEntity[] = await logseq.Editor.getPageBlocksTree(
date
);
const lastItem: BlockEntity = homepage[homepage.length - 1];
if (lastItem.content == "") {
logseq.Editor.scrollToBlockInPage(date, lastItem.uuid);
triggerEnter();
} else {
const block = await logseq.Editor.appendBlockInPage(date, "");
logseq.Editor.scrollToBlockInPage(date, block!.uuid);
if (logseq.settings?.enterEditingMode == "enter") {
triggerEnter();
}
}
}
);
};
logseq.ready(main).catch(console.error);