Skip to content

Commit

Permalink
fix: fix queryElementRect issue and add fallback position, #64
Browse files Browse the repository at this point in the history
  • Loading branch information
ahonn committed Dec 24, 2023
1 parent 1e3d38b commit bb3a551
Showing 1 changed file with 40 additions and 12 deletions.
52 changes: 40 additions & 12 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,49 @@ import { logseq as plugin } from '../package.json';
import App from './App';
import settings from './settings';

async function openTaskPanel(e: any) {
const { rect } = e;
const taskPanel = document.querySelector("#" + plugin.id)!;
type Rect = {
top: number;
right: number;
left: number;
bottom: number;
};

let cachedRect: Rect | undefined = undefined;

async function openTaskPanel(e?: { rect: Rect }) {
const taskPanel = document.querySelector('#' + plugin.id)!;
let rect = e?.rect ?? cachedRect;

if (!rect) {
try {
const elRect = await logseq.App.queryElementRect('#' + plugin.id);
if (elRect) {
rect = elRect;
cachedRect = elRect;
}
} catch (e) {
console.error(e);
}
}

if (rect) {
cachedRect = rect;
}

const position = rect
? {
top: `${rect.top + 40}px`,
left: rect.left + 'px',
}
: {
top: '40px',
right: '200px',
};

// @ts-ignore
Object.assign(taskPanel.style, {
position: 'fixed',
// @ts-ignore
top: `${rect.top + 40}px`,
// @ts-ignore
left: rect.left + 'px',
...position,
});

logseq.showMainUI();
Expand Down Expand Up @@ -80,8 +112,4 @@ function main() {
);
}

logseq
.useSettingsSchema(settings)
.ready(createModel())
.then(main)
.catch(console.error);
logseq.useSettingsSchema(settings).ready(createModel()).then(main).catch(console.error);

0 comments on commit bb3a551

Please sign in to comment.