-
Notifications
You must be signed in to change notification settings - Fork 0
/
content-script.js
178 lines (141 loc) · 4.09 KB
/
content-script.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
var spotlightPopup = document.querySelector(".spotlight-popup");
if (spotlightPopup) {
document.body.removeChild(spotlightPopup);
}
function TogglePopup({ $target, initialState, onToggle }) {
const $container = document.createElement("div");
$container.classList.add("spotlight-popup");
this.state = initialState;
this.setState = (nextState) => {
this.state = nextState;
this.render();
};
this.render = () => {
const { toggled, removed } = this.state;
if (removed) {
$container.style.display = "none";
return;
}
$container.innerHTML = `
<div class="spotlight-popup-message">
${
toggled
? `<span class="spotlight-popup-message-text guide">Shift</span>`
: `<span class="spotlight-popup-message-text">Spotlight</span>`
}
</div>
<div class="spotlight-popup-remove">
<span class="spotlight-popup-remove-text">✕</span>
</div>
<label class="toggle-container">
<input
class="toggle-input"
type="checkbox"
${toggled && "checked"} />
<div class="toggle-switch"></div>
</label>
`;
};
this.render();
$target.appendChild($container);
onToggle(this.state.toggled);
$container.addEventListener("click", (e) => {
const toggleInput = e.target.closest(".toggle-input");
if (!toggleInput) {
return;
}
this.setState({
...this.state,
toggled: !this.state.toggled,
});
onToggle && onToggle(this.state.toggled);
});
$container.addEventListener("click", (e) => {
const popupRemove = e.target.closest(".spotlight-popup-remove");
if (!popupRemove) {
return;
}
if (!confirm("spotlight를 종료하시겠습니까?")) {
return;
}
this.setState({
toggled: false,
removed: true,
});
onToggle && onToggle(this.state.toggled);
});
}
var togglePopup = new TogglePopup({
$target: document.body,
initialState: {
toggled: true,
removed: false,
},
onToggle: (toggled) => {
toggled ? activate() : deactivate();
},
});
var mask = document.createElement("div");
mask.setAttribute("class", "spotlight-mask");
var DEFAULT_SCALE = 150;
var scale = DEFAULT_SCALE;
function beam(event) {
const coord = `${event.clientX}px ${event.clientY}px`;
mask.style.backgroundImage = `radial-gradient(circle at ${coord}, transparent ${scale}px, rgba(0, 0, 0, 0.4) 0)`;
}
/**
* 조명의 필드 앵글(field angle)을 확대합니다.
*
* @param {*} event
*/
function zoom(event) {
const coord = `${event.clientX}px ${event.clientY}px`;
// Use deltaX due to shift key (shfit+ wheel means scrollX)
const delta = Math.sign(event.deltaX) * 6;
scale += delta;
const max = Math.min(window.innerHeight, window.innerWidth);
const min = 10;
scale = Math.min(Math.max(scale, min), max);
mask.style.backgroundImage = `radial-gradient(circle at ${coord}, transparent ${scale}px, rgba(0, 0, 0, 0.4) 0)`;
}
function activate() {
console.log("SPOTLIGHT ENABLED");
window.addEventListener("keydown", startSpotlight);
window.addEventListener("keyup", stopSpotlight);
}
function deactivate() {
console.log("SPOTLIGHT DISABLED");
window.removeEventListener("keydown", startSpotlight);
window.removeEventListener("keyup", stopSpotlight);
}
function startSpotlight(event) {
if (event.shiftKey) {
document.body.appendChild(mask);
window.addEventListener("mousemove", beam);
window.addEventListener("wheel", zoom);
}
}
function stopSpotlight(event) {
if (event.key === "Shift") {
try {
if (document.querySelector(".spotlight-mask")) {
document.body.removeChild(document.querySelector(".spotlight-mask"));
}
window.removeEventListener("mousemove", beam);
window.removeEventListener("wheel", zoom);
} catch (e) {
console.error(e);
}
}
}
/**
* innerHTML을 넣으면 DOM 요소로 만듭니다.
*
* @param {*} innerHTML
* @returns
*/
function fragment(innerHTML) {
const $template = document.createElement("template");
$template.innerHTML = innerHTML;
return $template.content;
}