-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
60 lines (57 loc) · 2.59 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
<title>UndoRedo.js - Textarea Example</title>
</head>
<body>
<div style="max-width: 800px;" class="container p-3">
<textarea class="form-control" id="input" style="height: 341px; width: 100%;"></textarea>
<br>
<div class="row">
<button id="undo" style="margin: 5px;" class="btn btn-danger btn-lg col">undo</button>
<button id="redo" style="margin: 5px;" class="btn btn-warning btn-lg col">redo</button>
</div>
</div>
<script src="./src/UndoRedo.js"></script>
<script>
// Require the library function
const txtHistory = new window.UndoRedojs(5)
// Get the textarea
const textarea = document.querySelector("#input")
// Add event listener for inputs on the textarea
textarea.addEventListener('input', () => {
// Check if the new textarea value is different
if (txtHistory.current() !== textarea.value) {
// Check for pastes, auto corrects..
if ((textarea.value.length - txtHistory.current().length) > 1 || (textarea.value.length - txtHistory.current().length) < -1 || (textarea.value.length - txtHistory.current().length) === 0) {
// Record the textarea value and force to bypass cooldown
txtHistory.record(textarea.value, true)
// Check for single key press, single chacacter paste..
} else {
// Record the textarea value
txtHistory.record(textarea.value)
}
}
})
// Some browsers will auto-fill the textarea again after reloading, this will deal with that
setTimeout(() => {
if (textarea.value) txtHistory.record(textarea.value, true)
}, 100)
// The undo button
document.querySelector("#undo").addEventListener('click', () => {
if (txtHistory.undo(true) !== undefined) {
textarea.value = txtHistory.undo()
}
})
// The redo button
document.querySelector("#redo").addEventListener('click', () => {
if (txtHistory.redo(true) !== undefined) {
textarea.value = txtHistory.redo()
}
})
</script>
</body>
</html>