Skip to content

Commit

Permalink
feat: smart inline or display mode
Browse files Browse the repository at this point in the history
See #7
  • Loading branch information
AllanChain committed Oct 7, 2023
1 parent c40097f commit d445365
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
14 changes: 9 additions & 5 deletions src/popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ export async function openPopup(
let originalValue = ''
let delim = logseq.settings?.preferDisplay ? '$$' : '$'
let newline = logseq.settings?.preferMultiline ? '\n' : ''

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

if (originalContent) {
const match = originalContent.match(/^(?<delim>\$+)(?<newline>\n*)(?<content>.*)\2\1$/s)
if (match !== null && match.groups !== undefined) {
Expand All @@ -33,6 +39,9 @@ export async function openPopup(
// Only `$$` has multiline preference
if (delim !== '$') newline = match.groups.newline
}
} else if (logseq.settings?.smartFormat && contentBefore.length > 0) {
if (contentBefore[contentBefore.length - 1] === '\n') delim = '$$'
else delim = '$'
}

// Insert a background screen to capture clicks
Expand Down Expand Up @@ -121,11 +130,6 @@ export async function openPopup(

applyAlign() // Resize box based on originalContent

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

parent.addEventListener('resize', applyAlign)
// keep block in editing mode after mousedown
popupContent.addEventListener('mousedown', (event) => event.stopPropagation())
Expand Down
14 changes: 12 additions & 2 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,16 @@ export const settingsConfig: SettingSchemaDesc[] = [
type: 'boolean',
default: false,
},
{ title: 'MathLive Settings', description: '', key: 'mathliveSettings', type: 'heading', default: null },
{
title: 'Smart format',
description:
'Intelligently determines whether to use inline or display mode, ' +
'honoring the preferDisplay option in case of ambiguity.',
key: 'smartFormat',
type: 'boolean',
default: true,
},
{ title: 'MathLive', description: '', key: 'mathliveSettings', type: 'heading', default: null },
{
title: 'Smart Fence',
description: 'Automatically convert parentheses to `\\left...\\right` markup.',
Expand All @@ -51,7 +60,8 @@ export const settingsConfig: SettingSchemaDesc[] = [
},
{
title: 'Smart Mode',
description: 'Switch to text mode when text input is detected, for example when typing `if x > 0`.',
description:
'Switch to text mode when text input is detected, for example when typing `if x > 0`.',
key: 'smartMode',
type: 'boolean',
default: false,
Expand Down

0 comments on commit d445365

Please sign in to comment.