Skip to content

Commit

Permalink
feat: ctrl+$ shortcut to switch math mode
Browse files Browse the repository at this point in the history
See #7
  • Loading branch information
AllanChain committed Oct 7, 2023
1 parent 2a953c0 commit c85716b
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,17 @@ export async function openPopup(
let done = false
const wrapLaTeX = (latex: string) =>
delim === '$' ? delim + latex + delim : delim + newline + latex + newline + delim

const updateLaTeX = async () => {
const insertedText = wrapLaTeX(mfe.getValue('latex-expanded'))
const contentBeforeCaret = mfe.value ? contentBefore + insertedText : contentBefore
await logseq.Editor.updateBlock(uuid, contentBeforeCaret + contentAfter)
return contentBeforeCaret
}
const switchMode = async () => {
delim = delim === '$' ? '$$' : '$'
delimSwitch.innerText = delim === '$' ? 'Inline Math' : 'Display Math'
await updateLaTeX()
}
let mouseMovement = ''
// Avoid firing click after dragging
delimSwitch.addEventListener('mousedown', (event) => {
Expand All @@ -153,10 +157,12 @@ export async function openPopup(
delimSwitch.addEventListener('mouseup', async (event) => {
if (mouseMovement !== `${event.x},${event.y}`) return
mouseMovement = ''
console.log(event)
delim = delim === '$' ? '$$' : '$'
delimSwitch.innerText = delim === '$' ? 'Inline Math' : 'Display Math'
await updateLaTeX()
await switchMode()
})
mfe.addEventListener('keydown', async (event) => {
if (event.key === '$' && event.ctrlKey) {
await switchMode()
}
})
clearButton.addEventListener('click', async () => {
mfe.value = ''
Expand Down

0 comments on commit c85716b

Please sign in to comment.