From 1d947bcf7e26ace6eac106598705d6190eb3d53a Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Mon, 5 Aug 2024 11:29:21 +0200 Subject: [PATCH] Export hover tooltip state field from object returned by hoverTooltip 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 --- src/tooltip.ts | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/tooltip.ts b/src/tooltip.ts index d10cb36..e679eea 100644 --- a/src/tooltip.ts +++ b/src/tooltip.ts @@ -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: { @@ -760,7 +765,7 @@ export function hoverTooltip( /// milliseconds. Defaults to 300ms. hoverTime?: number } = {} -): Extension { +): Extension & {active: StateField} { let setHover = StateEffect.define() let hoverState = StateField.define({ create() { return [] }, @@ -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.