-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
90 lines (74 loc) · 2.41 KB
/
index.js
File metadata and controls
90 lines (74 loc) · 2.41 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
document.addEventListener('DOMContentLoaded', () => {
const editor = document.querySelector('#editor');
const iframe = document.querySelector('#preview');
// Preview
function setPreviewContent() {
iframe.contentWindow.document.open();
iframe.contentWindow.document.write(editor.value);
iframe.contentWindow.document.close();
}
editor.on('input', setPreviewContent);
editor.on('load', setPreviewContent);
// Typing interaction
let typingInteractionIsRunning = true;
async function startTypingInteraction(cycleThroughText) {
await wait(1000);
const textToReplace = cycleThroughText[0];
const start = editor.textarea.value.indexOf(textToReplace);
const end = start + textToReplace.length;
editor.textarea.setSelectionRange(start, end);
await wait(500);
const replacement = cycleThroughText[1];
editor.textarea.setRangeText('', start, end);
for (let index = 0; index < replacement.length; index++) {
editor.textarea.setRangeText(
replacement.slice(0, index + 1),
start,
start + index,
'select'
);
editor.textarea.dispatchEvent(new Event('input'));
editor.dispatchEvent(new Event('input'));
await wait(50);
}
await wait(1000);
window.requestAnimationFrame(() =>
startTypingInteraction([
replacement,
...cycleThroughText.slice(2),
textToReplace,
]).catch(console.error)
);
}
function stopTypingInteraction() {
typingInteractionIsRunning = false;
}
function wait(time) {
return new Promise((resolve, reject) => {
setTimeout(
() => (typingInteractionIsRunning ? resolve() : reject()),
time
);
});
}
editor.on('load', function () {
const initialText = 'World 🤗';
const start = editor.textarea.value.indexOf(initialText);
if (start < 0) return;
editor.textarea.focus({ preventScroll: true });
editor.textarea.setSelectionRange(start, start);
editor.textarea.addEventListener('click', stopTypingInteraction);
editor.textarea.addEventListener('keypress', stopTypingInteraction);
window.addEventListener('scroll', () => {
if (document.body.scrollTop > document.body.clientHeight) {
editor.textarea.blur();
}
});
startTypingInteraction([
initialText,
'Beautiful 😍',
'Gorgeous 😘',
'CodeBedder 💻',
]).catch(console.error);
});
});