Skip to content

Commit

Permalink
feat: C-M auto search, no selection needed
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanChain committed Dec 6, 2023
1 parent f0db8f9 commit c244214
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function main() {
async () => {
const block = await logseq.Editor.getCurrentBlock()
if (block === null) return
await openPopup(block.uuid)
await openPopup(block.uuid, { searchMath: true })
},
)
logseq.Editor.onInputSelectionEnd(async (event) => {
Expand Down
17 changes: 14 additions & 3 deletions src/popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ async function sleep(ms: number) {
export async function openPopup(
uuid: string,
opts?: {
selectionStart: number
selectionEnd: number
selectionStart?: number
selectionEnd?: number
searchMath?: boolean
},
) {
const textarea = parent.document.querySelector<HTMLTextAreaElement>(`textarea[id$="${uuid}"]`)
Expand All @@ -21,11 +22,21 @@ export async function openPopup(
const blockContent = textarea.value
let dollarEnd = opts?.selectionEnd ?? textarea.selectionEnd
let dollarStart = opts?.selectionStart ?? textarea.selectionStart
const originalContent = textarea.value.substring(dollarStart, dollarEnd)
let originalValue = ''
let delim = logseq.settings?.preferDisplay ? '$$' : '$'
let newline = logseq.settings?.preferMultiline ? '\n' : ''

if (opts?.searchMath && dollarStart === dollarEnd) {
for (const match of blockContent.matchAll(/(\$+)([^$]+)\1/g)) {
if (match.index! <= dollarStart && dollarStart <= match.index! + match[0].length) {
dollarStart = match.index!
dollarEnd = dollarStart + match[0].length
}
}
}

const originalContent = textarea.value.substring(dollarStart, dollarEnd)

while (blockContent.charAt(dollarEnd) === '$') dollarEnd++
while (blockContent.charAt(dollarStart - 1) === '$') dollarStart--
const contentBefore = blockContent.substring(0, dollarStart)
Expand Down

0 comments on commit c244214

Please sign in to comment.