Skip to content

Commit 2d93d6b

Browse files
committed
Initial
1 parent 8824f8f commit 2d93d6b

File tree

5 files changed

+137
-0
lines changed

5 files changed

+137
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.vscode
2+
*.bak
3+
*~

README.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Blocknav
2+
3+
## Synopsis
4+
5+
Super simple plugin to facilitate block navigation in [Logseq](https://www.logseq.com/).
6+
7+
## Installation
8+
9+
Not currently in the marketplace.
10+
11+
To load, must turn on developer mode in settings and load as an "unpacked plugin" from the Logseq plugins page (`t p`).
12+
13+
## Usage
14+
15+
Keybind|Action
16+
-|-
17+
`b p`|Prepend empty block and edit
18+
`b b`|Append empty block and edit
19+
`b e`|Edit last block in page
20+
`b 1`|Edit first block in page
21+
…|…
22+
`b 9`|Edit 9th block in page
23+
`b 0`|Edit 10th block in page
24+
25+
**Note 1**: `b p` and `b b` will only prepend/append a new block if the block in that position is not already empty. Entering edit mode will occur in either case.
26+
27+
**Note 2**: If the block to be edited is greater than the number of blocks (i.e. `b 5` when there are 4 blocks) the last block will be edited.

index.html

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport"
6+
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
7+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
8+
<title>Blocknav LSPlugin</title>
9+
</head>
10+
<body>
11+
<div id="app"></div>
12+
<script src="https://cdn.jsdelivr.net/npm/@logseq/libs"></script>
13+
<script src="./index.js"></script>
14+
</body>
15+
</html>

index.js

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
function delay(time) {
2+
return new Promise(resolve => setTimeout(resolve, time));
3+
}
4+
5+
6+
async function blockNav(idx, newBlock, onlySelect) {
7+
const ed = logseq.Editor, app = logseq.App;
8+
const before = idx >= 0;
9+
let page = await ed.getCurrentPage();
10+
11+
if (page === null) {
12+
const today = await app.getStateFromStore('today');
13+
page = await ed.getPage(today);
14+
if (page === null) return;
15+
}
16+
17+
let blocks = await ed.getPageBlocksTree(page.name);
18+
if (blocks === null || blocks.length < 1) return;
19+
20+
const bidx = idx < 0 ? Math.max(0, blocks.length - Math.abs(idx)) : Math.min(idx, blocks.length - 1);
21+
let block = blocks[bidx];
22+
23+
newBlock = newBlock && !(block.children.length < 1 && block.content === '');
24+
if (newBlock) {
25+
block = await ed.insertBlock(block.uuid, '', {
26+
before, isPageBlock: false, sibling: true
27+
});
28+
}
29+
30+
await ed.editBlock(block.uuid);
31+
32+
if (onlySelect) await delay(50).then(() => ed.exitEditingMode(true));
33+
}
34+
35+
function main () {
36+
logseq.App.registerCommandPalette({
37+
key: 'blocknav-prepend',
38+
keybinding: { binding: 'b p', mode: 'non-editing' },
39+
label: 'Blocknav: Prepend and edit',
40+
palette: true
41+
}, () => blockNav(0, true));
42+
43+
logseq.App.registerCommandPalette({
44+
key: 'blocknav-append',
45+
keybinding: { binding: 'b b', mode: 'non-editing' },
46+
label: 'Blocknav: Append and edit',
47+
palette: true
48+
}, () => blockNav(-1, true));
49+
50+
logseq.App.registerCommandPalette({
51+
key: 'blocknav-1',
52+
keybinding: { binding: 'b 1', mode: 'non-editing' },
53+
label: 'Blocknav: Edit first block',
54+
palette: true
55+
}, () => blockNav(0));
56+
57+
for (let bnum = 1; bnum < 10; bnum++) {
58+
const bs = (bnum + 1).toString();
59+
logseq.App.registerCommandPalette({
60+
key: 'blocknav-'.concat(bs),
61+
keybinding: { binding: 'b '.concat(bnum < 9 ? bs : '0'), mode: 'non-editing' },
62+
label: 'Blocknav: Edit block '.concat(bs),
63+
palette: true
64+
}, () => blockNav(bnum));
65+
}
66+
67+
logseq.App.registerCommandPalette({
68+
key: 'blocknav-last',
69+
keybinding: { binding: 'b e', mode: 'non-editing' },
70+
label: 'Blocknav: Edit last block',
71+
palette: true
72+
}, () => blockNav(-1));
73+
}
74+
75+
logseq.ready(main).catch(console.error);

package.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
{
3+
"name": "logseq-blocknav",
4+
"version": "0.0.1",
5+
"author": "KerfuffleV2",
6+
"main": "index.html",
7+
"description": "Block navigation shortcuts",
8+
"repository": {
9+
"type": "git",
10+
"url": "https://github.com/kerfufflev2/logseq-plugin-blocknav.git"
11+
},
12+
"license": "MIT",
13+
"logseq": {
14+
"title": "Block Navigation",
15+
"id": "b985d1a5-ed73-472e-8abd-1b51bc293c07"
16+
}
17+
}

0 commit comments

Comments
 (0)