-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
335 lines (302 loc) · 10.2 KB
/
index.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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*======================================================================*/
/*AngularJS*/
var SYMBOLS = '!"#$%&\'()*+,-./:;<=>?@[]^_`{|}~ \n\\';
//regex special meanings: \, ^, $, ., |, ?, *, +, (, ), [, {
var SYMBOLSARR = [" ", "\n", "\\\.", "!", '\\\?', '\\\(', '\\\)', "\\\,", "\\\|"];
//Emoji mappings
var emojiMappings;
var personalEmojiMappings;
//URL's for emoji mappings
emojiMappingsURL = "http://erikyangs.com/emojipastagenerator/emojiMapping.json";
personalEmojiMappingsURL = "http://erikyangs.com/emojipastagenerator/personalEmojiMapping.json";
//emoji load flag
var emojisReady = false;
// Define the app
var app = angular.module('mainApp', []);
app.controller('mainController', function mainController($scope, $http, $q) {
//gets data from both json files
//http://stackoverflow.com/questions/14545573/angular-accessing-data-of-multiple-http-calls-how-to-resolve-the-promises
$q.all([$http.get(emojiMappingsURL), $http.get(personalEmojiMappingsURL)])
.then(
function(data){
//data is returned as a list
for(var i in data){
mapping = data[i];
if(mapping["config"]["url"] == emojiMappingsURL){
emojiMappings = mapping["data"];
}
else if(mapping["config"]["url"] == personalEmojiMappingsURL){
personalEmojiMappings = wordArrayJSON(lowercaseJSON(mapping["data"]));
}
else{
console.log("Error when assigning emojiMappings.");
alert("Emojis may not be loading. Sorry about that. Contact me for fixes.");
}
}
emojisReady = true;
},
function(error){
console.log("Error when getting emojiMappings: " + error);
alert("Emojis may not be loading. Sorry about that. Contact me for fixes.");
}
);
//stores data from textarea from ng-model
$scope.input = "";
$scope.convertToEmojipasta = function(input) {
if (emojisReady) {
//var wordArray = createWordArray(addPersonalEmojis(input));
var wordArray = createWordArray(input);
return wordArrayToEmojipasta(wordArray);
}
else if (input){
return "Loading...";
}
else{
return "";
}
}
});
/*======================================================================*/
/*JQuery*/
//emojilib: https://raw.githubusercontent.com/muan/emojilib/master/emojis.json
var emojiRepeatArray;
$(document).ready(function() {
//textarea resizing with content
$("#textinput").on('input', function(event) {
$("#textinput").css("height", "1px");
var scrollHeight = $("#textinput").prop("scrollHeight");
var minHeight = $(window).height() * 0.30;
//if new line
if (event.keyCode == 13) {
scrollHeight += 30;
}
if (scrollHeight > minHeight) {
$("#textinput").css("height", scrollHeight);
} else {
$("#textinput").css("height", minHeight);
}
});
//copy button
$("#copybutton").click(function(event){
selectElementContents(document.getElementById("emojipasta"));
document.execCommand("copy");
clearSelection();
$("#copynotification").fadeIn(300, function() { $(this).delay(500).fadeOut(1000); });
});
//seed for which words will have 1,2,3 emojis
emojiRepeatArray = getRandomIntArray(50);
});
/*======================================================================*/
/*Copy text helper methods*/
function selectElementContents(el) {
if (window.getSelection && document.createRange) {
var sel = window.getSelection();
var range = document.createRange();
range.selectNodeContents(el);
sel.removeAllRanges();
sel.addRange(range);
} else if (document.selection && document.body.createTextRange) {
var textRange = document.body.createTextRange();
textRange.moveToElementText(el);
textRange.select();
}
}
function clearSelection() {
if (document.selection) {
document.selection.empty();
} else if (window.getSelection) {
window.getSelection().removeAllRanges();
}
}
/*======================================================================*/
/*Emojipasta helper methods*/
//takes input string and makes each word into string
function createWordArray(input) {
var output = [];
var currentWord = "";
for (var i = 0; i < input.length; i++) {
//if symbol present
if (SYMBOLS.indexOf(input[i]) > -1) {
if (currentWord != "") {
output.push(currentWord);
currentWord = "";
}
output.push(input[i]);
} else {
currentWord += input[i];
}
}
//push last word
if (currentWord != "") {
output.push(currentWord);
}
return output;
}
function wordArrayToEmojipasta(wordArray) {
var output = "";
var i = 0;
//console.log(wordArray);
while (i < wordArray.length) {
word = wordArray[i];
var personalEmoji = personalEmojiMappings[word.toLowerCase()];
var emoji = emojiMappings[word.toLowerCase()];
//personalEmojiMappings.json multi-word
if (personalEmoji && typeof(personalEmoji) === "object") {
var wordArraySliced = wordArray.slice(i, i + personalEmoji.length);
var wordArraySlicedLowercase = lowercaseArray(wordArraySliced);
if (arraysEqual(personalEmoji.wordArray, wordArraySlicedLowercase)) {
var totalWord = "";
for(var k = 0; k<wordArraySliced.length; k++){
totalWord += wordArraySliced[k];
}
output += totalWord + " " + personalEmoji.emoji;
i += personalEmoji.length - 1;
} else {
output += word;
}
} //personalEmojiMappings.json one word
else if (personalEmoji && typeof(personalEmoji) !== "object") {
output += word;
output += " " + personalEmoji;
} //emojiMappings.json
else {
output += word;
if (emoji) {
var repeat = emojiRepeatArray[i % emojiRepeatArray.length];
var totalEmoji = "";
for (var j = 0; j < repeat; j++) {
totalEmoji += emoji;
}
output += " " + totalEmoji;
}
}
i++;
}
return output;
}
/* Deprecated. Replaces every occurence of the word, even within words (e.g. w🍑ass🍑up)
function addPersonalEmojis(input) {
var result = new String(input);
var lowercaseInput = input.toLowerCase();
for (var key in personalEmojiMappings) {
if (lowercaseInput.includes(key.toLowerCase())) {
var emoji = personalEmojiMappings[key];
result = result.replace(new RegExp(key, 'ig'), function(match) {
return emoji + " " + match + " " + emoji;
});
}
}
return result;
}*/
/*======================================================================*/
/*General Helper Methods*/
//inclusive min, exclusive max
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
//random int array of x integers
function getRandomIntArray(x) {
var result = [];
for (var i = 0; i < x; i++) {
result.push(getRandomInt(1, 4));
}
return result;
}
//checks if values of array are equal
function arraysEqual(a, b) {
if (a === b) {
return true;
}
if (a == null || b == null) {
return false;
}
if (a.length != b.length) {
return false;
}
for (var i = 0; i < a.length; ++i) {
if (a[i] !== b[i]) return false;
}
return true;
}
/*======================================================================*/
/*Made for updating emojis and testing purposes*/
function parseJSON(data) {
emojiMappings = JSON && JSON.parse(data) || $.parseJSON(data);
}
//creates a map from words&keywords to emojis from emojis.json
function modifyEmojiLib(x) {
var output = {};
for (var key in x) {
var char = x[key].char;
//add the key
if (!(key in output)) {
output[key] = char;
}
var keywords = x[key].keywords;
for (var i = 0; i < keywords.length; i++) {
keyword = keywords[i];
//pairs emoji with first keyword occurrence (for the time being)
if (!(keyword in output)) {
output[keyword] = char;
}
}
}
var str = JSON.stringify(output, null, 4);
$("#json").text(str);
return output;
}
//alphabetize the object o
function alphabetizeJSON(o) {
var sorted = {},
key, a = [];
for (key in o) {
if (o.hasOwnProperty(key)) {
a.push(key);
}
}
a.sort();
for (key = 0; key < a.length; key++) {
sorted[a[key]] = o[a[key]];
}
var str = JSON.stringify(sorted, null, 4);
console.log(str)
$("#json").text(str);
return sorted;
}
//makes all keys in object lowercase
function lowercaseJSON(obj) {
var key, keys = Object.keys(obj);
var n = keys.length;
var newobj = {};
while (n--) {
key = keys[n];
newobj[key.toLowerCase()] = obj[key];
}
return newobj;
}
//makes all keys in array lowercase
function lowercaseArray(array) {
var result = [];
for (var i = 0; i < array.length; i++) {
result.push(array[i].toLowerCase());
}
return result;
}
//makes keys with more than one word have object values.
function wordArrayJSON(obj) {
var keys = Object.keys(obj);
var result = {};
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var val = obj[key];
//var keyWordArray = key.split(new RegExp(SYMBOLSARR.join("|"), 'g'));
var keyWordArray = createWordArray(key);
if (keyWordArray.length > 1) {
//keys will be the first word
result[keyWordArray[0]] = { "words": key, "wordArray": keyWordArray, "emoji": val, "length": keyWordArray.length };
} else {
result[key] = val;
}
}
return result;
}