Skip to content

Commit

Permalink
Export hover tooltip state field from object returned by hoverTooltip
Browse files Browse the repository at this point in the history
FEATURE: The value returned by `hoverTooltip` now has an `active` property
providing the state field used to store the open tooltips.

See https://discuss.codemirror.net/t/highlight-some-text-while-hovertooltip-is-activated/8506
  • Loading branch information
marijnh committed Aug 5, 2024
1 parent a4b41a8 commit 1d947bc
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,11 @@ function isOverRange(view: EditorView, from: number, to: number, x: number, y: n
/// Note that all hover tooltips are hosted within a single tooltip
/// container element. This allows multiple tooltips over the same
/// range to be "merged" together without overlapping.
///
/// The return value is a valid [editor extension](#state.Extension)
/// but also provides an `active` property holding a state field that
/// can be used to read the currently active tooltips produced by this
/// extension.
export function hoverTooltip(
source: HoverTooltipSource,
options: {
Expand All @@ -760,7 +765,7 @@ export function hoverTooltip(
/// milliseconds. Defaults to 300ms.
hoverTime?: number
} = {}
): Extension {
): Extension & {active: StateField<readonly Tooltip[]>} {
let setHover = StateEffect.define<readonly Tooltip[]>()
let hoverState = StateField.define<readonly Tooltip[]>({
create() { return [] },
Expand Down Expand Up @@ -793,11 +798,14 @@ export function hoverTooltip(
provide: f => showHoverTooltip.from(f)
})

return [
hoverState,
ViewPlugin.define(view => new HoverPlugin(view, source, hoverState, setHover, options.hoverTime || Hover.Time)),
showHoverTooltipHost
]
return {
active: hoverState,
extension: [
hoverState,
ViewPlugin.define(view => new HoverPlugin(view, source, hoverState, setHover, options.hoverTime || Hover.Time)),
showHoverTooltipHost
]
}
}

/// Get the active tooltip view for a given tooltip, if available.
Expand Down

0 comments on commit 1d947bc

Please sign in to comment.