-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
277 lines (260 loc) · 7.64 KB
/
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
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
let capitalizeNext = true;
let spaceAfter = false;
let glue = false;
let currentWord = "";
let history = [""];
let numbers = /^\d+/g;
clear.onclick = () => {
textArea.value = "";
}
enter.onclick = () => {
if (S.value == "S") {
if (num.value == "#") {
currentWord += "1";
} else {
currentWord += "S";
}
}
if (T.value == "T") {
if (num.value == "#") {
currentWord += "2";
} else {
currentWord += "T";
}
}
if (K.value == "K") {
currentWord += "K";
}
if (P.value == "P") {
if (num.value == "#") {
currentWord += "3";
} else {
currentWord += "P";
}
}
if (W.value == "W") {
currentWord += "W";
}
if (H.value == "H") {
if (num.value == "#") {
currentWord += "4";
} else {
currentWord += "H";
}
}
if (R.value == "R") {
currentWord += "R";
}
if (A.value == "" && O.value == "" && star.value == "" && E.value == "" && U.value == "" && num.value == "") {
currentWord += "-";
}
if (A.value == "A") {
if (num.value == "#") {
currentWord += "5";
} else {
currentWord += "A";
}
}
if (O.value == "O") {
if (num.value == "#") {
currentWord += "0";
} else {
currentWord += "O";
}
}
if (star.value == "*") {
currentWord += "*";
}
if (E.value == "E") {
currentWord += "E";
}
if (U.value == "U") {
currentWord += "U";
}
if (f.value == "F") {
if (num.value == "#") {
currentWord += "6";
} else {
currentWord += "F";
}
}
if (r.value == "R") {
currentWord += "R";
}
if (p.value == "P") {
if (num.value == "#") {
currentWord += "7";
} else {
currentWord += "P";
}
}
if (b.value == "B") {
currentWord += "B";
}
if (l.value == "L") {
if (num.value == "#") {
currentWord += "8";
} else {
currentWord += "L";
}
}
if (g.value == "G") {
currentWord += "G";
}
if (t.value == "T") {
if (num.value == "#") {
currentWord += "9";
} else {
currentWord += "T";
}
}
if (s.value == "S") {
currentWord += "S";
}
if (d.value == "D") {
currentWord += "D";
}
if (z.value == "Z") {
currentWord += "Z";
}
if (currentWord.charAt(currentWord.length - 1) == "-") {
currentWord = currentWord.slice(0,-1);
}
////////////////////////////////
if (stroke.value == "/") {
currentWord += "/";
} else {
word = stenoData[currentWord];
hasSpace = false;
if (word == undefined) {
word = currentWord;
}
if (spaceAfter == true) {
hasSpace = true;
}
spaceAfter = true;
word = word.replace("{}", "");
if (word.includes("{-|}")) {
hasSpace = false;
capitalizeNext = true;
word = word.replace("{-|}", "");
}
if (word.includes("{>}")) {
hasSpace = false;
capitalizeNext = false;
word = word.replace("{>}", "");
}
console.log(numbers.test(word));
if (numbers.test(word) == true) {
if (glue == true) {
hasSpace = false;
}
glue = true;
} else if (word.charAt(0) == "{" && word.charAt(1) == "&" && word.charAt(word.length - 1) == "}") {
word = word.substring(2, word.length - 1);
if (glue == true) {
hasSpace = false;
}
glue = true;
} else {
glue = false;
}
if (word.charAt(0) == "{" && word.charAt(word.length - 1) == "}") {
word = word.substring(1, word.length - 1);
hasSpace = false;
}
if (word.includes("{^.^}")) {
word = word.replace("{^.^}", ".");
hasSpace = false;
}
if (word.charAt(0) == "^") {
hasSpace = false;
word = word.substring(1, word.length);
}
if (word.charAt(word.length - 1) == "^") {
spaceAfter = false;
word = word.substring(0, word.length - 1)
}
word = word.replace(">}{&", "");
if (capitalizeNext == true) {
word = word.charAt(0).toUpperCase() + word.slice(1)
}
capitalizeNext = false;
if (hasSpace == true) {
textArea.value += " ";
}
spaceAfter = true;
switch (word) {
case ".":
case "?":
case "!":
case ":":
capitalizeNext = true;
}
textArea.value += word;
currentWord = "";
}
}
changePos();
function changePos() {
/* t.focus();
t.setSelectionRange(t.value.length, t.value.length);
s.style.left = getCursorXY(t, t.selectionEnd).x - 100 + "px";
t.blur();
*/
}
post.onclick = () => {
const posting = document.createElement("span");
posting.style.display = "block";
posting.style.marginTop = "0.5em";
posting.style.fontSize = "1.3em";
posting.innerText = "Posting...";
document.body.appendChild(posting);
setTimeout(() => {
posting.innerText = "Posted!";
}, 1500);
}
/**
* FROM https://medium.com/@jh3y/how-to-where-s-the-caret-getting-the-xy-position-of-the-caret-a24ba372990a
*/
function getCursorXY(input, selectionPoint) {
const {
offsetLeft: inputX,
offsetTop: inputY,
} = input
// create a dummy element that will be a clone of our input
const div = document.createElement('div')
// get the computed style of the input and clone it onto the dummy element
const copyStyle = getComputedStyle(input)
for (const prop of copyStyle) {
div.style[prop] = copyStyle[prop]
}
// we need a character that will replace whitespace when filling our dummy element if it's a single line <input/>
const swap = '.'
const inputValue = input.tagName === 'INPUT' ? input.value.replace(/ /g, swap) : input.value
// set the div content to that of the textarea up until selection
const textContent = inputValue.substr(0, selectionPoint)
// set the text content of the dummy element div
div.textContent = textContent
if (input.tagName === 'TEXTAREA') div.style.height = 'auto'
// if a single line input then the div needs to be single line and not break out like a text area
if (input.tagName === 'INPUT') div.style.width = 'auto'
// create a marker element to obtain caret position
const span = document.createElement('span')
// give the span the textContent of remaining content so that the recreated dummy element is as close as possible
span.textContent = inputValue.substr(selectionPoint) || '.'
// append the span marker to the div
div.appendChild(span)
// append the dummy element to the body
document.body.appendChild(div)
// get the marker position, this is the caret position top and left relative to the input
const { offsetLeft: spanX, offsetTop: spanY } = span
// lastly, remove that dummy element
// NOTE:: can comment this out for debugging purposes if you want to see where that span is rendered
document.body.removeChild(div)
// return an object with the x and y of the caret. account for input positioning so that you don't need to wrap the input
return {
x: inputX + spanX,
y: inputY + spanY,
}
}