|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
16 | 16 |
|
17 |
| -import { |
18 |
| - EditorView, |
19 |
| - Extension, |
20 |
| - showTooltip, |
21 |
| - StateEffect, |
22 |
| - StateField, |
23 |
| - Tooltip, |
24 |
| - ViewPlugin, |
25 |
| - ViewUpdate, |
26 |
| -} from '@uiw/react-codemirror' |
| 17 | +import { EditorView, Extension, showTooltip, StateEffect, StateField, Tooltip, ViewPlugin } from '@uiw/react-codemirror' |
27 | 18 |
|
28 | 19 | import { getReadOnlyElement } from '../utils'
|
| 20 | +import { READ_ONLY_TOOLTIP_TIMEOUT } from '../CodeEditor.constants' |
29 | 21 |
|
30 | 22 | // Effect to update the tooltip in the editor state
|
31 | 23 | const updateTooltipEffect = StateEffect.define<Tooltip | null>()
|
@@ -76,43 +68,46 @@ const createTooltip = (view: EditorView): Tooltip => {
|
76 | 68 | }
|
77 | 69 | }
|
78 | 70 |
|
79 |
| -// Define a plugin to manage tooltip updates |
80 |
| -const focusTooltipPlugin = ViewPlugin.fromClass( |
| 71 | +// Plugin to show and remove tooltip on keypress |
| 72 | +const keypressTooltipPlugin = ViewPlugin.fromClass( |
81 | 73 | class {
|
82 |
| - // Tracks whether an update is scheduled |
83 |
| - public scheduled: boolean = false |
| 74 | + private timeoutId: number | null = null |
84 | 75 |
|
85 | 76 | constructor(public view: EditorView) {
|
86 |
| - this.scheduleUpdate() |
| 77 | + this.view.dom.addEventListener('keydown', this.handleKeyPress) |
87 | 78 | }
|
88 | 79 |
|
89 |
| - // Called when the editor state changes |
90 |
| - update(update: ViewUpdate) { |
91 |
| - if (update.focusChanged || update.selectionSet) { |
92 |
| - this.scheduleUpdate() |
| 80 | + destroy() { |
| 81 | + this.view.dom.removeEventListener('keydown', this.handleKeyPress) |
| 82 | + if (this.timeoutId) { |
| 83 | + clearTimeout(this.timeoutId) |
93 | 84 | }
|
94 | 85 | }
|
95 | 86 |
|
96 |
| - // Schedules a tooltip update in the next microtask |
97 |
| - scheduleUpdate() { |
98 |
| - if (this.scheduled) return |
99 |
| - this.scheduled = true |
100 |
| - |
101 |
| - // Update the tooltip asynchronously |
102 |
| - // eslint-disable-next-line @typescript-eslint/no-floating-promises |
103 |
| - Promise.resolve().then(() => { |
104 |
| - this.scheduled = false |
105 |
| - const tooltip = this.view.state.readOnly && this.view.hasFocus ? createTooltip(this.view) : null |
106 |
| - this.view.dispatch({ |
107 |
| - effects: updateTooltipEffect.of(tooltip), |
108 |
| - }) |
109 |
| - }) |
| 87 | + handleKeyPress = () => { |
| 88 | + if (!this.view.state.readOnly) { |
| 89 | + return |
| 90 | + } |
| 91 | + |
| 92 | + // Show tooltip |
| 93 | + const tooltip = createTooltip(this.view) |
| 94 | + this.view.dispatch({ effects: updateTooltipEffect.of(tooltip) }) |
| 95 | + |
| 96 | + // Reset the timer after every key press |
| 97 | + if (this.timeoutId) { |
| 98 | + clearTimeout(this.timeoutId) |
| 99 | + } |
| 100 | + |
| 101 | + // Remove tooltip after timeout time |
| 102 | + this.timeoutId = setTimeout(() => { |
| 103 | + this.view.dispatch({ effects: updateTooltipEffect.of(null) }) |
| 104 | + }, READ_ONLY_TOOLTIP_TIMEOUT) |
110 | 105 | }
|
111 | 106 | },
|
112 | 107 | )
|
113 | 108 |
|
114 | 109 | /**
|
115 | 110 | * The read-only tooltip extension for CodeMirror. \
|
116 |
| - * Displays a tooltip at the cursor position when the editor is read-only and focused. |
| 111 | + * Displays a tooltip at the cursor position when the editor is read-only and key is pressed. |
117 | 112 | */
|
118 |
| -export const readOnlyTooltip: Extension = [tooltipField, focusTooltipPlugin] |
| 113 | +export const readOnlyTooltip: Extension = [tooltipField, keypressTooltipPlugin] |
0 commit comments