From 83ebf7a383b2ceff4dca48992561028a73647f88 Mon Sep 17 00:00:00 2001 From: Allan Chain <36528777+AllanChain@users.noreply.github.com> Date: Thu, 31 Aug 2023 11:12:55 +0800 Subject: [PATCH] fix: multiline display math --- src/index.ts | 2 +- src/popup.ts | 25 ++++++++++++------------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/index.ts b/src/index.ts index 21ca97d..33a740e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -20,7 +20,7 @@ function main() { if (logseq.settings?.selectEdit) { logseq.Editor.onInputSelectionEnd(async (event) => { if (event.text.length <= 2) return - if (!event.text.match(/^(\$+)([^$]+)\1$/)) return + if (!event.text.match(/^(\$+)([^$]+)\1$/m)) return const block = await logseq.Editor.getCurrentBlock() if (block === null) return openPopup(block.uuid, { diff --git a/src/popup.ts b/src/popup.ts index 49def28..80bb3f8 100644 --- a/src/popup.ts +++ b/src/popup.ts @@ -53,11 +53,13 @@ export async function openPopup( let dollarStart = opts?.selectionStart ?? textarea.selectionStart const originalContent = textarea.value.substring(dollarStart, dollarEnd) let delim = logseq.settings?.preferDisplay ? '$$' : '$' + let newline = '' if (originalContent) { - const match = originalContent.match(/^(?\$+)(?.*)\1$/) + const match = originalContent.match(/^(?\$+)(?\n*)(?.*)\2\1$/ms) if (match !== null && match.groups !== undefined) { mfe.value = match.groups.content delim = match.groups.delim + newline = match.groups.newline } } delimSwitch.innerText = delim === '$' ? 'Inline Math' : 'Display Math' @@ -70,23 +72,23 @@ export async function openPopup( const contentAfter = blockContent.substring(dollarEnd, blockContent.length) let done = false + const updateLaTeX = async () => { + const insertedText = delim + newline + mfe.value + newline + delim + const contentBeforeCaret = mfe.value ? contentBefore + insertedText : contentBefore + await logseq.Editor.updateBlock(uuid, contentBeforeCaret + contentAfter) + return contentBeforeCaret + } parent.addEventListener('resize', applyAlign) popupContent.querySelector('.draggable-handle')?.addEventListener('mouseup', applyAlign) delimSwitch.addEventListener('click', async () => { delim = delim === '$' ? '$$' : '$' delimSwitch.innerText = delim === '$' ? 'Inline Math' : 'Display Math' - const contentBeforeCaret = mfe.value - ? contentBefore + `${delim}${mfe.value}${delim}` - : contentBefore - await logseq.Editor.updateBlock(uuid, contentBeforeCaret + contentAfter) + await updateLaTeX() }) mfe.addEventListener('input', async () => { await applyAlign() if (mfe.value.includes('placeholder')) return // not a complete formula - const contentBeforeCaret = mfe.value - ? contentBefore + `${delim}${mfe.value}${delim}` - : contentBefore - await logseq.Editor.updateBlock(uuid, contentBeforeCaret + contentAfter) + await updateLaTeX() }) mfe.addEventListener('unmount', async () => { parent.removeEventListener('resize', applyAlign) @@ -97,10 +99,7 @@ export async function openPopup( if (done) return // avoid insert twice done = true logseq.provideUI({ key: 'popup', template: '' }) // close popup - const contentBeforeCaret = mfe.value - ? contentBefore + `${delim}${mfe.value}${delim}` - : contentBefore - await logseq.Editor.updateBlock(uuid, contentBeforeCaret + contentAfter) + const contentBeforeCaret = await updateLaTeX() // HACK: `Editor.editBlock` does nothing, focusing using DOM ops textarea.focus() textarea.selectionStart = contentBeforeCaret.length