Skip to content

Commit

Permalink
fix: re-place popup on new math
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanChain committed Aug 31, 2023
1 parent eeb6182 commit 3d13110
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,16 @@ export async function openPopup(
}
}

// Make sure the previous one is closed,
// so that Logseq will honor the style provided.
logseq.provideUI({ key: 'popup', template: '' })
logseq.provideUI({
key: 'popup',
template: '<span></span>',
style: await calcAlign(delim === '$$' && !!newline),
style: await calcAlign({
multiline: delim === '$$' && !!newline,
renew: true,
}),
attrs: {
title: 'Click title to switch display and inline style',
},
Expand Down Expand Up @@ -69,8 +75,7 @@ export async function openPopup(
await sleep(0)

async function applyAlign() {
console.log('newlien', newline)
const styles = await calcAlign(delim === '$$' && !!newline)
const styles = await calcAlign({ multiline: delim === '$$' && !!newline })
if (styles === undefined) return
if (popupContent === null) return

Expand Down Expand Up @@ -162,16 +167,19 @@ function parseStyle(s: string): number {
return parseInt(s.substring(0, s.length - 2), 10)
}

async function calcAlign(multiline = false): Promise<UIBaseOptions['style']> {
async function calcAlign(opt?: {
multiline?: boolean
renew?: boolean
}): Promise<UIBaseOptions['style']> {
const popupMinHeight = 100
const popupTopMargin = multiline ? 60 : 30
const popupTopMargin = opt?.multiline ?? false ? 60 : 30
const popupBottomMargin = 20
const popupDefaultWidth = 300
const clientWidth = parent.document.documentElement.clientWidth
const clientHeight = parent.document.documentElement.clientHeight
const popupContent = parent.document.getElementById('logseq-live-math--popup')
let popup: PopupAlign
if (popupContent === null) {
if (popupContent === null || opt?.renew) {
const caret = await logseq.Editor.getEditingCursorPosition()
if (caret === null) {
console.error('Error getting cursor pos')
Expand Down

0 comments on commit 3d13110

Please sign in to comment.