import type { Tooltip } from "@codemirror/view"; import type { ViewUpdate } from "@codemirror/view"; import { StateEffect, StateField } from "@codemirror/state"; import { ViewPlugin } from "@codemirror/view"; import { showTooltip } from "@codemirror/view"; import { replacedFrameAt, tooltipsStateEffect, tooltipsStateField, typstRecompileEffect, } from "./widgets"; const tooltipStateEffect = StateEffect.define(); export const tooltipStateField = StateField.define({ create() { return null; }, update(tooltip, transaction) { const effect = transaction.effects.find((e) => e.is(tooltipStateEffect)); if (effect) return effect.value; return tooltip; }, provide: (field) => showTooltip.from(field), }); export const tooltipViewPlugin = () => ViewPlugin.define((view) => ({ update(update: ViewUpdate) { const tooltipsChanged = update.transactions.some((transaction) => transaction.effects.some((effect) => effect.is(tooltipsStateEffect)), ); if (!update.selectionSet && !update.docChanged && !update.focusChanged && !tooltipsChanged) { return; } queueMicrotask(() => { if (!view.hasFocus) { if (view.state.field(tooltipStateField)) { view.dispatch({ effects: tooltipStateEffect.of(null) }); } return; } const state = view.state; const pos = state.selection.main.from; // A replaced frame has no source text to anchor to. The equation is // not visible, so a popup would only land at the frame's right edge. if (replacedFrameAt(state, pos)) { if (state.field(tooltipStateField)) { view.dispatch({ effects: tooltipStateEffect.of(null) }); } return; } const overlays = state.field(tooltipsStateField); const { tooltips, ranges, focus } = overlays; if (!tooltips.some(({ range }) => pos >= range.start && pos <= range.end)) { const effects = []; if (state.field(tooltipStateField)) effects.push(tooltipStateEffect.of(null)); const inMath = ranges.some(([start, end]) => pos >= start && pos <= end); if (inMath && focus !== pos) { effects.push(typstRecompileEffect.of(null)); } if (effects.length) view.dispatch({ effects }); return; } let tooltip: Tooltip | null = null; // Find which frame contains the cursor for (const { render, range } of tooltips) { const { start, end } = range; if (pos >= start && pos <= end && render) { const container = document.createElement("div"); container.classList.add("typst-popup-render"); const svg = document.createElement("div"); svg.style.width = render.width + "px"; svg.style.height = render.height + "px"; svg.style.transform = `translateX(-${render.xOffset}px)`; svg.setHTMLUnsafe(render.svg); container.append(svg); tooltip = { pos: Math.min(start, state.doc.length), end: Math.min(end, state.doc.length), create(tooltipView) { return { dom: container, offset: { x: 0, y: 4 }, getCoords(_pos) { const docLength = tooltipView.state.doc.length; const content = tooltipView.contentDOM.getBoundingClientRect(); const startCoords = tooltipView.coordsAtPos(Math.min(start, docLength)); const endCoords = tooltipView.coordsAtPos(Math.min(end, docLength)); const top = Math.max( content.top, Math.min(content.bottom, startCoords?.top ?? content.top), ); const bottom = Math.max( top, Math.min(content.bottom, endCoords?.bottom ?? startCoords?.bottom ?? top), ); const left = Math.max( content.left, Math.min(content.right, startCoords?.left ?? content.left), ); const right = Math.max(left, Math.min(content.right, endCoords?.right ?? left)); return { left, right, top, bottom }; }, }; }, }; break; } } view.dispatch({ effects: tooltipStateEffect.of(tooltip) }); }); }, }));