-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhover-macro.js
156 lines (123 loc) · 3.99 KB
/
hover-macro.js
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
/* Mali's <<hover>> macro */
(() => {
let curScroll = 0, tipVis = false;
const $tip = $('<div>').attr({ id: 'macro-hover-tip', 'aria-live': 'polite' });
const summonTip = (txt, $cont, dir) => {
tipVis = true;
curScroll = $(document).scrollTop();
$cont.append($tip);
$tip.removeClass().wiki(txt);
const { height: h, width: w } = $tip[0].getBoundingClientRect(),
mrg = 4,
contRect = $cont[0].getBoundingClientRect();
contRect.center = { y: contRect.top + contRect.height / 2, x: contRect.left + contRect.width / 2 };
const positions = {
up: {
at: [contRect.center.x, contRect.top],
stickOut: { main: -(h + mrg), sec: w / 2 }
},
down: {
at: [contRect.center.x, contRect.bottom],
stickOut: { main: h + mrg, sec: w / 2 }
},
left: {
at: [contRect.left, contRect.center.y],
stickOut: { main: -(w + mrg), sec: h / 2 }
},
right:
{
at: [contRect.right, contRect.center.y],
stickOut: { main: w + mrg, sec: h / 2 }
},
over: {
at: [contRect.center.x, contRect.center.y],
stickOut: { main: 0, sec: 0 }
}
};
const checkPos = dir => {
const pos = positions[dir], checked = [];
switch (dir) {
case 'up': case 'down':
const vert = pos.at[1] + pos.stickOut.main;
checked.push(
(vert > 0 && vert < window.innerHeight),
(pos.at[0] - pos.stickOut.sec > 0),
(pos.at[0] + pos.stickOut.sec < window.innerWidth)
);
break;
case 'left': case 'right':
const hori = pos.at[0] + pos.stickOut.main;
checked.push(
(hori > 0 && hori < window.innerWidth),
(pos.at[1] - pos.stickOut.sec > 0),
(pos.at[1] + pos.stickOut.sec < window.innerHeight)
);
break;
default: checked.push(true);
}
return checked.every(e => e);
};
dir = dir.find(d => checkPos(d));
if (!dir) { dir = ['up', 'right', 'down', 'left', 'over'].find(d => checkPos(d)) }
let [x, y] = positions[dir].at, { main, sec } = positions[dir].stickOut;
switch (dir) {
case 'up': y += main; x -= sec; break;
case 'down': y += mrg; x -= sec; break;
case 'left': y -= sec; x += main; break;
case 'right': y -= sec; x += mrg; break;
default: x -= w / 2; y -= h / 2;
}
$tip.attr({ 'aria-hidden': false }).css({ top: y, left: x }).addClass('visible ' + dir);
};
const hideTip = () => {
tipVis = false;
$tip.attr({ 'aria-hidden': true }).removeClass().empty();
};
$(document).on('scroll', e => {
if (!tipVis) return;
const scr = $(document).scrollTop(), off = scr - curScroll;
const pos = Number($tip.css('top').slice(0, -2));
$tip.css({ top: pos - off });
curScroll = scr;
}).on(':passageinit', hideTip);
Macro.add('hover', {
tags: ['swap', 'tip'],
handler() {
const innerText = this.payload[0].contents;
let active = false, tipDirection = [];
// wrapper
const $wrp = $('<span>')
.attr(this.args.find(a => typeof a === 'object') ?? {})
.attr('tabindex', '0')
.addClass(`macro-${this.name}`)
.wiki(innerText);
// convenient payload object
const pay = {};
this.payload.slice(1).forEach(p => {
pay[p.name] = p;
$wrp.addClass(p.name);
});
if (pay.tip) {
tipDirection = pay.tip.args.map(a => a.split(' ')).flat();
$wrp.attr('role', 'tooltip');
}
const inFunction = () => {
if (active) return;
active = true;
if (pay.swap) $wrp.empty().wiki(pay.swap.contents);
if (pay.tip) summonTip(pay.tip.contents, $wrp, tipDirection);
};
const outFunction = () => {
active = false;
if (pay.swap) $wrp.empty().wiki(innerText);
if (pay.tip) hideTip();
};
const shadowWrapper = this.shadowHandler ?? this.createShadowWrapper;
$wrp
.on('mouseenter focus', shadowWrapper.call(this, inFunction))
.on('mouseleave focusout', shadowWrapper.call(this, outFunction))
.appendTo($(this.output));
}
});
})();
/* End of <<hover>> macro */