mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
Merge pull request #19617 from overleaf/dp-tooltip-alignment
Always render math preview tooltip at start of math content GitOrigin-RevId: b0af7aa3c0920b2a9b9930f27a580018de1d2f52
This commit is contained in:
parent
afd965c04b
commit
72c7b946ed
2 changed files with 26 additions and 12 deletions
|
@ -14,6 +14,7 @@ import {
|
||||||
import { loadMathJax } from '../../mathjax/load-mathjax'
|
import { loadMathJax } from '../../mathjax/load-mathjax'
|
||||||
import { descendantsOfNodeWithType } from '../utils/tree-query'
|
import { descendantsOfNodeWithType } from '../utils/tree-query'
|
||||||
import {
|
import {
|
||||||
|
MathContainer,
|
||||||
mathAncestorNode,
|
mathAncestorNode,
|
||||||
parseMathContainer,
|
parseMathContainer,
|
||||||
} from '../utils/tree-operations/math'
|
} from '../utils/tree-operations/math'
|
||||||
|
@ -92,12 +93,13 @@ function buildTooltips(state: EditorState): readonly Tooltip[] {
|
||||||
|
|
||||||
for (const range of state.selection.ranges) {
|
for (const range of state.selection.ranges) {
|
||||||
if (range.empty) {
|
if (range.empty) {
|
||||||
const pos = range.from
|
const mathContainer = getMathContainer(state, range.from)
|
||||||
const content = buildTooltipContent(state, pos)
|
const content = buildTooltipContent(state, mathContainer)
|
||||||
if (content) {
|
if (content && mathContainer) {
|
||||||
const tooltip: Tooltip = {
|
const tooltip: Tooltip = {
|
||||||
pos,
|
pos: mathContainer.pos,
|
||||||
above: true,
|
above: true,
|
||||||
|
strictSide: true,
|
||||||
arrow: false,
|
arrow: false,
|
||||||
create() {
|
create() {
|
||||||
const dom = document.createElement('div')
|
const dom = document.createElement('div')
|
||||||
|
@ -116,18 +118,21 @@ function buildTooltips(state: EditorState): readonly Tooltip[] {
|
||||||
return tooltips
|
return tooltips
|
||||||
}
|
}
|
||||||
|
|
||||||
const buildTooltipContent = (
|
const getMathContainer = (state: EditorState, pos: number) => {
|
||||||
state: EditorState,
|
// if anywhere inside Math, find the whole Math node
|
||||||
pos: number
|
|
||||||
): HTMLDivElement | null => {
|
|
||||||
// if anywhere inside Math, render the whole Math content
|
|
||||||
const ancestorNode = mathAncestorNode(state, pos)
|
const ancestorNode = mathAncestorNode(state, pos)
|
||||||
if (!ancestorNode) return null
|
if (!ancestorNode) return null
|
||||||
|
|
||||||
const [node] = descendantsOfNodeWithType(ancestorNode, 'Math', 'Math')
|
const [node] = descendantsOfNodeWithType(ancestorNode, 'Math', 'Math')
|
||||||
if (!node) return null
|
if (!node) return null
|
||||||
|
|
||||||
const math = parseMathContainer(state, node, ancestorNode)
|
return parseMathContainer(state, node, ancestorNode)
|
||||||
|
}
|
||||||
|
|
||||||
|
const buildTooltipContent = (
|
||||||
|
state: EditorState,
|
||||||
|
math: MathContainer | null
|
||||||
|
): HTMLDivElement | null => {
|
||||||
if (!math || !math.content.length) return null
|
if (!math || !math.content.length) return null
|
||||||
|
|
||||||
const element = document.createElement('div')
|
const element = document.createElement('div')
|
||||||
|
|
|
@ -3,6 +3,13 @@ import { EditorState } from '@codemirror/state'
|
||||||
import { SyntaxNode, SyntaxNodeRef } from '@lezer/common'
|
import { SyntaxNode, SyntaxNodeRef } from '@lezer/common'
|
||||||
import { ancestorNodeOfType } from './ancestors'
|
import { ancestorNodeOfType } from './ancestors'
|
||||||
|
|
||||||
|
export type MathContainer = {
|
||||||
|
content: string
|
||||||
|
displayMode: boolean
|
||||||
|
passToMathJax: boolean
|
||||||
|
pos: number
|
||||||
|
}
|
||||||
|
|
||||||
export const mathAncestorNode = (state: EditorState, pos: number) =>
|
export const mathAncestorNode = (state: EditorState, pos: number) =>
|
||||||
ancestorNodeOfType(state, pos, '$MathContainer') ||
|
ancestorNodeOfType(state, pos, '$MathContainer') ||
|
||||||
ancestorNodeOfType(state, pos, 'EquationEnvironment') ||
|
ancestorNodeOfType(state, pos, 'EquationEnvironment') ||
|
||||||
|
@ -13,7 +20,7 @@ export const parseMathContainer = (
|
||||||
state: EditorState,
|
state: EditorState,
|
||||||
nodeRef: SyntaxNodeRef,
|
nodeRef: SyntaxNodeRef,
|
||||||
ancestorNode: SyntaxNode
|
ancestorNode: SyntaxNode
|
||||||
) => {
|
): MathContainer | null => {
|
||||||
// the content of the Math element, without braces
|
// the content of the Math element, without braces
|
||||||
const innerContent = state.doc.sliceString(nodeRef.from, nodeRef.to).trim()
|
const innerContent = state.doc.sliceString(nodeRef.from, nodeRef.to).trim()
|
||||||
|
|
||||||
|
@ -24,6 +31,7 @@ export const parseMathContainer = (
|
||||||
let content = innerContent
|
let content = innerContent
|
||||||
let displayMode = false
|
let displayMode = false
|
||||||
let passToMathJax = true
|
let passToMathJax = true
|
||||||
|
let pos = nodeRef.from
|
||||||
|
|
||||||
if (ancestorNode.type.is('$Environment')) {
|
if (ancestorNode.type.is('$Environment')) {
|
||||||
const environmentName = getEnvironmentName(ancestorNode, state)
|
const environmentName = getEnvironmentName(ancestorNode, state)
|
||||||
|
@ -37,6 +45,7 @@ export const parseMathContainer = (
|
||||||
content = state.doc
|
content = state.doc
|
||||||
.sliceString(ancestorNode.from, ancestorNode.to)
|
.sliceString(ancestorNode.from, ancestorNode.to)
|
||||||
.trim()
|
.trim()
|
||||||
|
pos = ancestorNode.from
|
||||||
}
|
}
|
||||||
|
|
||||||
if (environmentName !== 'math') {
|
if (environmentName !== 'math') {
|
||||||
|
@ -52,5 +61,5 @@ export const parseMathContainer = (
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return { content, displayMode, passToMathJax }
|
return { content, displayMode, passToMathJax, pos }
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue