Skip to content

Commit

Permalink
fix: add new tab via MOD+ENTER in search menu
Browse files Browse the repository at this point in the history
fix #46
  • Loading branch information
pengx17 committed Jun 9, 2022
1 parent 04d7509 commit 01d2a4a
Showing 1 changed file with 51 additions and 24 deletions.
75 changes: 51 additions & 24 deletions src/PageTabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -255,43 +255,70 @@ function stop(e: Event) {
* Captures user CTRL Click a page link.
*/
function useCaptureAddTabAction(cb: (e: ITabInfo, open: boolean) => void) {
const handleAddTab = React.useCallback(
async (e: Event, target: HTMLElement) => {
let newTab: ITabInfo | null = null;
if (getPageRef(target)) {
stop(e);
const p = await getSourcePage(getPageRef(target));
if (p) {
newTab = p;
}
} else if (getBlockUUID(target)) {
stop(e);
const blockId = getBlockUUID(target);
if (blockId) {
const block = await logseq.Editor.getBlock(blockId);
if (block) {
const page = await logseq.Editor.getPage(block?.page.id);
if (page) {
newTab = { ...page, ...block };
}
}
}
}
if (newTab) {
cb(newTab, false);
}
},
[cb]
);

React.useEffect(() => {
const listener = async (e: MouseEvent) => {
const target = e.composedPath()[0] as HTMLElement;
// If CtrlKey is pressed, always open a new tab
const ctrlKey = isMac() ? e.metaKey : e.ctrlKey;

if (ctrlKey) {
let newTab: ITabInfo | null = null;
if (getPageRef(target)) {
stop(e);
const p = await getSourcePage(getPageRef(target));
if (p) {
newTab = p;
}
} else if (getBlockUUID(target)) {
stop(e);
const blockId = getBlockUUID(target);
if (blockId) {
const block = await logseq.Editor.getBlock(blockId);
if (block) {
const page = await logseq.Editor.getPage(block?.page.id);
if (page) {
newTab = { ...page, ...block };
}
}
}
}
if (newTab) {
cb(newTab, e.shiftKey);
}
handleAddTab(e, target);
}
};
top?.document.addEventListener("mousedown", listener, true);
return () => {
top?.document.removeEventListener("mousedown", listener, true);
};
}, [cb]);
}, [handleAddTab]);

// Capture MOD+ENTER on search results
React.useEffect(() => {
const listener = async (e: KeyboardEvent) => {
const ctrlKey = isMac() ? e.metaKey : e.ctrlKey;
if (e.key === "Enter" && ctrlKey) {
// Find out chosen search item
const chosenMenuItem = top?.document.querySelector<HTMLElement>(
".search-results-wrap .menu-link.chosen"
);
if (chosenMenuItem) {
handleAddTab(e, chosenMenuItem);
}
}
};
top?.document.addEventListener("keydown", listener, true);
return () => {
top?.document.removeEventListener("keydown", listener, true);
};
}, [handleAddTab]);
}

/**
Expand Down

0 comments on commit 01d2a4a

Please sign in to comment.