|
| 1 | +import { claimsTooltipElement } from '../dom-elements.js'; |
| 2 | +import { timeClaims } from './time-tooltip.js'; |
| 3 | +import { stringifyIndentSpaces } from './utils.js'; |
| 4 | +import strings from '../strings.js'; |
| 5 | + |
| 6 | +function hideTooltip() { |
| 7 | + claimsTooltipElement.style.display = 'none'; |
| 8 | +} |
| 9 | + |
| 10 | +function showTooltip(x, y, text) { |
| 11 | + claimsTooltipElement.firstChild.textContent = text; |
| 12 | + claimsTooltipElement.style.left = x + 'px'; |
| 13 | + claimsTooltipElement.style.top = y + 'px'; |
| 14 | + claimsTooltipElement.style.display = 'block'; |
| 15 | +} |
| 16 | + |
| 17 | +export function claimsTooltipHandler(event) { |
| 18 | + const editor = event.currentTarget.querySelector('.CodeMirror').CodeMirror; |
| 19 | + //const editor = event.currentTarget.firstChild.CodeMirror; |
| 20 | + |
| 21 | + if(!editor) { |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + const result = editor.coordsChar({ |
| 26 | + left: event.pageX, |
| 27 | + top: event.pageY |
| 28 | + }, 'page'); |
| 29 | + |
| 30 | + const line = editor.getLine(result.line); |
| 31 | + const matches = /"(.*)":\s*"?(.*)"?/.exec(line); |
| 32 | + |
| 33 | + if(!matches) { |
| 34 | + hideTooltip(); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + const claim = matches[1]; |
| 39 | + |
| 40 | + // If this is a time claim and the mouse cursor is on top of the time, |
| 41 | + // let the time tooltip handle this, do nothing. |
| 42 | + // TODO: merge both tooltip handlers? |
| 43 | + const claimEnd = line.indexOf(':'); |
| 44 | + if(result.ch >= claimEnd && timeClaims.includes(claim)) { |
| 45 | + hideTooltip(); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + const claimText = strings.common.claims[claim]; |
| 50 | + if(!claimText) { |
| 51 | + hideTooltip(); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + showTooltip(event.pageX, event.pageY, claimText); |
| 56 | +} |
0 commit comments