forked from wongsingfo/86wubi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
121 lines (103 loc) · 2.65 KB
/
Copy pathscript.js
File metadata and controls
121 lines (103 loc) · 2.65 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
function WubiInput() {
const whitespaceCharacters = [' ', ' ',
'\b', '\t', '\n', '\v', '\f', '\r', `\"`, `\'`, `\\`,
'\u0008', '\u0009', '\u000A', '\u000B', '\u000C',
'\u000D', '\u0020', '\u0022', '\u0027', '\u005C',
'\u00A0', '\u2028', '\u2029', '\uFEFF'];
const stop_words = '\'"()[]{}<>.,:;!?*&^%$#@~`-_=+/\\|' +
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' +
'0123456789' +
',。“”《》——!?:、‘’';
function isSpace(str) {
for (const space of whitespaceCharacters) {
if (str.indexOf(space) > -1) {
return true;
}
}
return false;
}
function isStopWord(str) {
return stop_words.indexOf(str) > -1;
}
function moveInputBox() {
const cursor = document.getElementById('cursor');
const {left, top} = cursor.getBoundingClientRect();
const {offsetLeft, offsetTop, offsetHeight} = cursor;
const inputbox = document.getElementById('inputbox');
inputbox.style.top = offsetTop + offsetHeight;
inputbox.style.left = offsetLeft;
const inputtext = document.getElementById('inputtext');
inputtext.focus();
}
return {
target: "Loading...",
progress: Number(localStorage.getItem("progress")) || 0,
inputtext: '',
showTextarea: false,
isStopWord,
getText() {
let target = localStorage.getItem("target");
if (target) {
this.target = target;
setTimeout(() => moveInputBox(), 0);
return;
} else {
fetch('text.txt')
.then(response => response.text())
.then(text => {
this.target = text;
setTimeout(() => moveInputBox(), 0);
});
}
},
getTargetDone() {
let {target, progress} = this;
return target.substring(0, progress);
},
getTargetTodo() {
let {target, progress} = this;
return target.substring(progress);
},
getCharacter() {
return this.target[this.progress];
},
onTextChanged() {
let {target, progress, inputtext} = this;
if (progress >= target.length) {
return;
}
let hasEnter = false;
let hasSpace = false;
let newInputtext = '';
for (let i = 0; i < inputtext.length; i++) {
const ch = inputtext[i];
if (target[progress] === ch) {
progress += 1;
} else {
newInputtext += ch;
}
if ('\n' === ch) {
hasEnter = true;
}
if (isSpace(ch)) {
hasSpace = true;
}
}
while (progress < target.length && isSpace(target[progress])) {
progress += 1;
}
if (hasEnter) {
newInputtext = '';
}
this.progress = progress;
this.inputtext = newInputtext;
moveInputBox();
localStorage.setItem("progress", progress);
localStorage.setItem("target", target);
},
resetProgress() {
this.progress = 0;
this.onTextChanged();
},
};
}