Skip to content

Commit

Permalink
Merge branch 'release/v1.1.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
freder committed Mar 24, 2023
2 parents ff1c09d + e845971 commit bef6496
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "logseq-plugin-jump-to-block",
"version": "1.1.1",
"version": "1.1.2",
"main": "dist/index.html",
"logseq": {
"id": "logseq-plugin-jump-to-block"
Expand Down
36 changes: 32 additions & 4 deletions src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,19 @@ type PathItem = {


const scrollTo = async (blockUuid: string) => {
const page = await logseq.Editor.getCurrentPage();
const pageOrBlock = (await logseq.Editor.getCurrentPage());
const isBlock = ('content' in (pageOrBlock || {}));
if (!pageOrBlock) {
return console.error('failed to get page or block');
}
const page = isBlock
? await logseq.Editor.getPage(pageOrBlock.page.id)
: pageOrBlock;
if (!page) {
return console.error('failed to get current page');
return console.error('failed to get page');
}
// TODO: how to scroll to block in sub-tree without leaving the sub-tree?
// `scrollToBlockInPage()` will open the entire page
logseq.Editor.scrollToBlockInPage(page.name, blockUuid);
};

Expand All @@ -31,7 +40,7 @@ const selectionHandler = async (
expand: boolean,
) => {
if (!item) {
return console.info('nothing selected');
return;
}
if (expand) {
await Promise.all(
Expand Down Expand Up @@ -114,10 +123,29 @@ function App() {
() => {
const visibilityHandler = async ({ visible }: { visible: boolean }) => {
if (visible) {
const blocks = await logseq.Editor.getCurrentPageBlocksTree();
const pageOrBlock = await logseq.Editor.getCurrentPage();
if (!pageOrBlock) {
return closeHandler();
}

// NOTE: `logseq.Editor.getCurrentPageBlocksTree()` won't return anything if
// we're in a sub-tree rather than a full page.
let blocks: BlockEntity[] = [];
if ('content' in pageOrBlock) {
const block = await logseq.Editor.getBlock(
(pageOrBlock as BlockEntity).uuid,
{ includeChildren: true }
);
if (block) {
blocks = [block];
}
} else {
blocks = await logseq.Editor.getCurrentPageBlocksTree();
}
if ((blocks || []).length === 0) {
return closeHandler();
}

const maxDepth = 3; // TODO: make this configurable
const items = makeCommands(blocks, maxDepth);
setItems(items);
Expand Down

0 comments on commit bef6496

Please sign in to comment.