-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
309 lines (252 loc) · 9.14 KB
/
Copy pathscript.js
File metadata and controls
309 lines (252 loc) · 9.14 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// script.js
// String karakter QWERTY dan Alphabet biasa
const qwertyChars = "QWERTYUIOPASDFGHJKLZXCVBNM";
const abcChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// Membuat objek (setara dictionary di Python) otomatis
const encodeMap = {};
const decodeMap = {};
for (let i = 0; i < qwertyChars.length; i++) {
encodeMap[qwertyChars[i]] = abcChars[i];
decodeMap[abcChars[i]] = qwertyChars[i];
}
// Fungsi Encode
function encode(text) {
return text
.toUpperCase()
.split('') // Memecah string menjadi array karakter
.map(ch => encodeMap[ch] || ch) // Jika karakter ada di map, ganti. Jika tidak, biarkan (angka/spasi).
.join(''); // Gabungkan kembali menjadi string
}
// Fungsi Decode
function decode(code) {
return code
.toUpperCase()
.split('')
.map(ch => decodeMap[ch] || ch)
.join('');
}
// === PENGHUBUNG HTML & JAVASCRIPT (DOM MANIPULATION) ===
// Mengambil elemen-elemen dari HTML berdasarkan ID
const inputText = document.getElementById('inputText');
const outputText = document.getElementById('outputText');
const outputLabel = document.getElementById('outputLabel');
const btnEncode = document.getElementById('btnEncode');
const btnDecode = document.getElementById('btnDecode');
const btnProcess = document.getElementById('btnProcess');
const btnCopy = document.getElementById('btnCopy');
const btnExample = document.getElementById('btnExample');
const btnSwap = document.getElementById('btnSwap');
const btnClear = document.getElementById('btnClear');
const exampleText = "Sepertinya ekspresi dalam tulisan memang ditakdirkan untuk menafsirkan dirinya sendiri";
let activeMode = 'hide';
let copyFeedbackTimer;
let swapFeedbackTimer;
let outputPulseTimer;
const modeConfig = {
hide: {
buttonText: 'Sembunyikan',
labelText: 'Teks yang hanya dimengerti oleh teks itu sendiri.',
transform: encode
},
interpret: {
buttonText: 'Interpretasikan',
labelText: 'Teks yang bisa kamu interpretasikan.',
transform: decode
}
};
function setCopyButtonText(text) {
btnCopy.textContent = text;
}
function resetCopyButton(delay = 0) {
window.clearTimeout(copyFeedbackTimer);
if (delay > 0) {
copyFeedbackTimer = window.setTimeout(() => {
setCopyButtonText('Copy');
}, delay);
return;
}
setCopyButtonText('Copy');
}
function setSwapButtonText(text) {
btnSwap.textContent = text;
}
function resetSwapButton(delay = 0) {
window.clearTimeout(swapFeedbackTimer);
if (delay > 0) {
swapFeedbackTimer = window.setTimeout(() => {
setSwapButtonText('⇄ Tukar');
}, delay);
return;
}
setSwapButtonText('⇄ Tukar');
}
function updateUI() {
const isHideMode = activeMode === 'hide';
btnEncode.classList.toggle('is-active', isHideMode);
btnDecode.classList.toggle('is-active', !isHideMode);
btnEncode.setAttribute('aria-checked', String(isHideMode));
btnDecode.setAttribute('aria-checked', String(!isHideMode));
btnProcess.textContent = modeConfig[activeMode].buttonText;
}
function setMode(mode) {
activeMode = mode;
updateUI();
}
function setOutput(value, labelText = outputLabel.innerText) {
window.clearTimeout(outputPulseTimer);
outputText.value = value;
outputLabel.innerText = labelText;
outputText.classList.toggle('is-empty', !value);
outputText.classList.toggle('has-output', Boolean(value));
btnCopy.classList.toggle('is-ready', Boolean(value));
if (value) {
outputText.classList.add('is-fresh');
outputPulseTimer = window.setTimeout(() => {
outputText.classList.remove('is-fresh');
}, 650);
} else {
outputText.classList.remove('is-fresh');
}
}
function scrollOutputIntoView() {
if (!window.matchMedia('(max-width: 640px)').matches) {
return;
}
outputText.scrollIntoView({ behavior: 'smooth', block: 'center' });
}
function processText() {
const teksInput = inputText.value;
if (!teksInput.trim()) {
inputText.focus();
inputText.classList.add('needs-input');
window.setTimeout(() => {
inputText.classList.remove('needs-input');
}, 650);
return;
}
const config = modeConfig[activeMode];
setOutput(config.transform(teksInput), config.labelText);
scrollOutputIntoView();
}
btnEncode.addEventListener('click', () => setMode('hide'));
btnDecode.addEventListener('click', () => setMode('interpret'));
btnProcess.addEventListener('click', processText);
async function copyOutput() {
const teksOutput = outputText.value;
if (!teksOutput) {
setCopyButtonText('Kosong');
resetCopyButton(1200);
return;
}
try {
await navigator.clipboard.writeText(teksOutput);
} catch (error) {
outputText.focus();
outputText.select();
document.execCommand('copy');
outputText.setSelectionRange(outputText.value.length, outputText.value.length);
}
setCopyButtonText('Copied');
resetCopyButton(1200);
}
btnCopy.addEventListener('click', copyOutput);
btnExample.addEventListener('click', () => {
inputText.value = exampleText;
inputText.focus();
inputText.setSelectionRange(inputText.value.length, inputText.value.length);
});
btnSwap.addEventListener('click', () => {
if (!outputText.value) {
setSwapButtonText('Masih kosong');
resetSwapButton(1200);
return;
}
inputText.value = outputText.value;
inputText.focus();
inputText.setSelectionRange(inputText.value.length, inputText.value.length);
setSwapButtonText('Dipindah');
resetSwapButton(1200);
});
btnClear.addEventListener('click', () => {
inputText.value = '';
setOutput('', 'Teks yang hanya dimengerti oleh teks itu sendiri :)');
resetCopyButton();
resetSwapButton();
inputText.focus();
});
setOutput('');
updateUI();
function initMusCustomCursor() {
const mediaQuery = window.matchMedia('(hover: none), (pointer: coarse)');
const reducedMotionQuery = window.matchMedia('(prefers-reduced-motion: reduce)');
if (mediaQuery.matches || reducedMotionQuery.matches) {
return;
}
const root = document.documentElement;
const dot = document.createElement('div');
const ring = document.createElement('div');
const interactiveSelector = 'a, button, input, textarea, select, summary, [role="button"], [data-cursor="hover"]';
const textSelector = 'input, textarea, [contenteditable="true"], p, span, h1, h2, h3, h4, h5, h6, label, li, blockquote, code, pre';
let pointerX = window.innerWidth / 2;
let pointerY = window.innerHeight / 2;
let ringX = pointerX;
let ringY = pointerY;
let rafId = null;
dot.className = 'mus-cursor-dot';
ring.className = 'mus-cursor-ring';
document.body.append(dot, ring);
root.classList.add('mus-custom-cursor-enabled');
function render() {
ringX += (pointerX - ringX) * 0.18;
ringY += (pointerY - ringY) * 0.18;
dot.style.transform = `translate3d(${pointerX}px, ${pointerY}px, 0) translate(-50%, -50%) scale(var(--mus-cursor-dot-scale, 1))`;
ring.style.transform = `translate3d(${ringX}px, ${ringY}px, 0) translate(-50%, -50%) scale(var(--mus-cursor-ring-scale, 1))`;
rafId = window.requestAnimationFrame(render);
}
function setVisibility(isVisible) {
dot.classList.toggle('is-visible', isVisible);
ring.classList.toggle('is-visible', isVisible);
}
function updatePointer(event) {
pointerX = event.clientX;
pointerY = event.clientY;
if (!dot.classList.contains('is-visible')) {
ringX = pointerX;
ringY = pointerY;
setVisibility(true);
}
const target = event.target;
const isHoverTarget = target instanceof Element && target.closest(interactiveSelector);
const isTextTarget = target instanceof Element && target.closest(textSelector);
dot.classList.toggle('mus-cursor-hover', Boolean(isHoverTarget));
ring.classList.toggle('mus-cursor-hover', Boolean(isHoverTarget));
root.classList.toggle('mus-cursor-over-text', Boolean(isTextTarget));
}
function pressCursor() {
dot.classList.add('mus-cursor-click');
ring.classList.add('mus-cursor-click');
}
function releaseCursor() {
dot.classList.remove('mus-cursor-click');
ring.classList.remove('mus-cursor-click');
}
document.addEventListener('pointermove', updatePointer, { passive: true });
document.addEventListener('pointerdown', pressCursor, { passive: true });
document.addEventListener('pointerup', releaseCursor, { passive: true });
document.addEventListener('pointercancel', releaseCursor, { passive: true });
document.addEventListener('mouseleave', () => {
setVisibility(false);
root.classList.remove('mus-cursor-over-text');
});
window.addEventListener('blur', () => {
setVisibility(false);
releaseCursor();
});
render();
window.addEventListener('beforeunload', () => {
if (rafId) {
window.cancelAnimationFrame(rafId);
}
});
}
initMusCustomCursor();