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