|
| 1 | +export const Selector = { |
| 2 | + TITLE: '.yfm .yfm-term_title', |
| 3 | + CONTENT: '.yfm .yfm-term_dfn', |
| 4 | +}; |
| 5 | +export const openClass = 'open'; |
| 6 | +export const openDefinitionClass = Selector.CONTENT.replace(/\./g, '') + ' ' + openClass; |
| 7 | +let isListenerNeeded = true; |
| 8 | + |
| 9 | +export function createDefinitionElement(termElement: HTMLElement) { |
| 10 | + const termKey = termElement.getAttribute('term-key'); |
| 11 | + const definitionTemplate = document.getElementById( |
| 12 | + `${termKey}_template`, |
| 13 | + ) as HTMLTemplateElement; |
| 14 | + const definitionElement = definitionTemplate?.content.cloneNode(true).firstChild as HTMLElement; |
| 15 | + |
| 16 | + definitionTemplate?.parentElement?.appendChild(definitionElement); |
| 17 | + definitionTemplate.remove(); |
| 18 | + |
| 19 | + return definitionElement; |
| 20 | +} |
| 21 | + |
| 22 | +export function setDefinitionId(definitionElement: HTMLElement, termElement: HTMLElement): void { |
| 23 | + const termId = termElement.getAttribute('id') || Math.random().toString(36).substr(2, 8); |
| 24 | + definitionElement?.setAttribute('term-id', termId); |
| 25 | +} |
| 26 | + |
| 27 | +export function setDefinitionPosition( |
| 28 | + definitionElement: HTMLElement, |
| 29 | + termElement: HTMLElement, |
| 30 | +): void { |
| 31 | + const { |
| 32 | + x: termX, |
| 33 | + y: termY, |
| 34 | + right: termRight, |
| 35 | + left: termLeft, |
| 36 | + width: termWidth, |
| 37 | + } = termElement.getBoundingClientRect(); |
| 38 | + |
| 39 | + const termParent = termParentElement(termElement); |
| 40 | + |
| 41 | + if (!termParent) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + const {right: termParentRight, left: termParentLeft} = termParent.getBoundingClientRect(); |
| 46 | + |
| 47 | + if ((termParentRight < termLeft || termParentLeft > termRight) && !isListenerNeeded) { |
| 48 | + closeDefinition(definitionElement); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + if (isListenerNeeded && termParent) { |
| 53 | + termParent.addEventListener('scroll', termOnResize); |
| 54 | + isListenerNeeded = false; |
| 55 | + } |
| 56 | + |
| 57 | + const relativeX = Number(definitionElement.getAttribute('relativeX')); |
| 58 | + const relativeY = Number(definitionElement.getAttribute('relativeY')); |
| 59 | + |
| 60 | + if (relativeX === termX && relativeY === termY) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + definitionElement.setAttribute('relativeX', String(termX)); |
| 65 | + definitionElement.setAttribute('relativeY', String(termY)); |
| 66 | + |
| 67 | + const offsetTop = 25; |
| 68 | + const definitionParent = definitionElement.parentElement; |
| 69 | + |
| 70 | + if (!definitionParent) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + const {width: definitionWidth} = definitionElement.getBoundingClientRect(); |
| 75 | + const {left: definitionParentLeft} = definitionParent.getBoundingClientRect(); |
| 76 | + |
| 77 | + // If definition not fit document change base alignment |
| 78 | + const definitionRightCoordinate = definitionWidth + Number(getCoords(termElement).left); |
| 79 | + const fitDefinitionDocument = |
| 80 | + document.body.clientWidth > definitionRightCoordinate ? 0 : definitionWidth - termWidth; |
| 81 | + |
| 82 | + definitionElement.style.top = Number(getCoords(termElement).top + offsetTop) + 'px'; |
| 83 | + definitionElement.style.left = |
| 84 | + Number( |
| 85 | + getCoords(termElement).left - |
| 86 | + definitionParentLeft + |
| 87 | + definitionParent.offsetLeft - |
| 88 | + fitDefinitionDocument, |
| 89 | + ) + 'px'; |
| 90 | +} |
| 91 | + |
| 92 | +function termOnResize() { |
| 93 | + const openDefinition = document.getElementsByClassName(openDefinitionClass)[0] as HTMLElement; |
| 94 | + |
| 95 | + if (!openDefinition) { |
| 96 | + return; |
| 97 | + } |
| 98 | + const termId = openDefinition.getAttribute('term-id') || ''; |
| 99 | + const termElement = document.getElementById(termId); |
| 100 | + |
| 101 | + if (!termElement) { |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + setDefinitionPosition(openDefinition, termElement); |
| 106 | +} |
| 107 | + |
| 108 | +function termParentElement(term: HTMLElement | null) { |
| 109 | + if (!term) { |
| 110 | + return null; |
| 111 | + } |
| 112 | + |
| 113 | + const closestScrollableParent = term.closest('table') || term.closest('code'); |
| 114 | + |
| 115 | + return closestScrollableParent || term.parentElement; |
| 116 | +} |
| 117 | + |
| 118 | +export function closeDefinition(definition: HTMLElement) { |
| 119 | + definition.classList.remove(openClass); |
| 120 | + const termId = definition.getAttribute('term-id') || ''; |
| 121 | + const termParent = termParentElement(document.getElementById(termId)); |
| 122 | + |
| 123 | + if (!termParent) { |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + termParent.removeEventListener('scroll', termOnResize); |
| 128 | + isListenerNeeded = true; |
| 129 | +} |
| 130 | + |
| 131 | +function getCoords(elem: HTMLElement) { |
| 132 | + const box = elem.getBoundingClientRect(); |
| 133 | + |
| 134 | + const body = document.body; |
| 135 | + const docEl = document.documentElement; |
| 136 | + |
| 137 | + const scrollTop = window.pageYOffset || docEl.scrollTop || body.scrollTop; |
| 138 | + const scrollLeft = window.pageXOffset || docEl.scrollLeft || body.scrollLeft; |
| 139 | + |
| 140 | + const clientTop = docEl.clientTop || body.clientTop || 0; |
| 141 | + const clientLeft = docEl.clientLeft || body.clientLeft || 0; |
| 142 | + |
| 143 | + const top = box.top + scrollTop - clientTop; |
| 144 | + const left = box.left + scrollLeft - clientLeft; |
| 145 | + |
| 146 | + return {top: Math.round(top), left: Math.round(left)}; |
| 147 | +} |
0 commit comments