-
Notifications
You must be signed in to change notification settings - Fork 1
/
typesmart-src.js
497 lines (455 loc) · 15.6 KB
/
typesmart-src.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
TypeSmart = {};
TypeSmart.smartDoubleQuote = function () {
var cursor = Cursor.new ();
// If the previous character is a whitespace,
// or opening parenthesize
// insert opening quote, else insert cloning quote.
if (/^\s*$/.test (cursor.getTextBefore (1)) || /^\(*$/.test (cursor.getTextBefore (1))) {
cursor.insert ("\u201C");
return false;
}
else {
cursor.insert ("\u201D");
return false;
}
return true;
};
TypeSmart.smartSingleQuote = function () {
var cursor = Cursor.new ();
// If the previous character is a whitespace or parens,
// insert opening quote, else insert cloning quote.
if (/^\s*$/.test (cursor.getTextBefore (1)) || /^\(*$/.test (cursor.getTextBefore (1))) {
cursor.insert ("\u2018");
return false;
}
else {
cursor.insert ("\u2019");
return false;
}
return true;
};
TypeSmart.smartQuoteTextReplace = function (text) {
// Strategy :
// 1) First replace all quotes following whitespace (or parens) into opening ones.
// 2) Then all those which are after any character into closing ones.
// 3) Then remaining into opening ones.
//
// Example : (curley quotes are denoted by [ and ])
// "He went home and called his mom. 'go home, don't stay' said mom."
// 1) "He went home and called his mom. ‘go home, don't stay' said mom."
// ^
// 2) "He went home and called his mom. ‘go home, don’t stay’ said mom.”
// ^ ^ ^
// 2) “He went home and called his mom. ‘go home, don’t stay’ said mom.”
// ^
return text.replace(/(\s)'/g, "$1\u2018") // Opening singles
.replace(/(\s)"/g, "$1\u201c") // Opening doubles
.replace(/(\()'/g, "$1\u2018") // Opening singles
.replace(/(\()"/g, "$1\u201c") // Opening doubles
// After one or more whitespace.
.replace(/(.)'/g, "$1\u2019") // Closing singles
.replace(/(.)"/g, "$1\u201d") // Closing doubles
.replace(/'/g, "\u2018") // Opening singles
.replace(/"/g, "\u201c"); // Opening doubles
};
// format -> {
// 'classname' : {
// 'string to trigger func1' : func1,
// 'string to trigger func2' : func2,
// },
// 'classname2' : {
// 'string to trigger func3' : func3,
// }
// };
TypeSmart.default_custom_triggers = {
'typeSmartTypography' : {
'"' : TypeSmart.smartDoubleQuote,
"'" : TypeSmart.smartSingleQuote,
"\\random" : (function () {Cursor.new().insert("COMMAND ACTIVATED");})
}
};
// format -> {
// 'classname' : {
// 'string to be replaced' : 'replace with this one',
// 'str2' : 'str3'
// },
// 'classname2' : {
// 'another str to be replaced' : 'another replacement'
// }
// };
TypeSmart.default_replacements = {
'typeSmartEmoticons' : {
":)" : "\uD83D\uDE0A",
":-)": "\uD83D\uDE0A",
":(" : "\uD83D\uDE1E",
":-(": "\uD83D\uDE1E",
"<3" : "\u2665",
":D" : "\uD83D\uDE05",
":P" : "\uD83D\uDE0B",
":-P" : "\uD83D\uDE0B",
":p" : "\uD83D\uDE0B",
":-p" : "\uD83D\uDE0B",
":'(": "\uD83D\uDE13",
"B-)": "\uD83D\uDE0E",
"x-(": "\uD83D\uDE20",
"X-(": "\uD83D\uDE20",
":-o": "\uD83D\uDE32"
},
'typeSmartTypography' : {
"...": "\u2026",
"--" : "\u2014",
"?!" : "\u2048",
"!?" : "\u2049",
"!!" : "\u203C"
},
'typeSmartMiscUnicode' : {
'/musicdoublenote' : '\u266B',
'/musicsinglenote' : '\u266A',
'/darkknight' : '\u265E',
'/star' : '\u2605',
'/umbrella' : '\u2602',
'/rupee' : '\u20B9',
'/alpha' : '\u03B1',
'/beta' : '\u03B2',
'/theta': '\u03B8',
'/sigma': '\u03A3',
'/lambda': '\u03BB',
'/danger': '\u2620',
'/tick' : '\u2714',
'/cross' : '\u2718'
}
};
// format -> {
// 'classname' : {
// 'description of func1' : func1, // accepts and returns a string
// 'description of func2' : func2
// },
// 'classname2' : {
// 'description of func3' : func3
// }
// };
TypeSmart.default_paste_modifiers = {
'typeSmartTypography' : {
'replace single and double dumb quotes with smart ones' :
TypeSmart.smartQuoteTextReplace
}
};
// Merges two dicts and returns the result dict
// SHALLOW COPY
// mergeDicts(
// {
// 1: 2,
// 2: 4,
// 3: {"foo": "bar"}
// },
// {
// 9: 3,
// 64: 8,
// 3: {"blah": "bleh"}
// }
// );
// -> {
// 1: 2,
// 2: 4,
// 9: 3,
// 64: 8,
// 3: {
// "foo": "bar",
// "blah": "bleh"
// }
// };
TypeSmart.mergeDicts = function (dict1, dict2) {
var result = {};
for (key in dict1) { result [key] = dict1 [key];}
for (key in dict2) {
if ((key in result) &&
(typeof (result [key]) == 'object') &&
(typeof (dict2 [key]) == 'object')) {
result [key] = TypeSmart.mergeDicts (result [key], dict2 [key]);
}
else {
result [key] = dict2 [key];
}
}
return result;
};
// Convert patterns like
// replacements = {
// '123' : "one-two-three",
// '[wow]' : '<exclamation>'
// '\spellcheck' : spellcheck_func
// }
//
// To replacement functions like
// functions_from_patterns = {
// '3' : funcOneTwoThree,
// ']' : exclamationFunc
// 'k' : spellcheck_func_modified
// }
//
// Where funcOneTwoThree() will be called whenever 3 is pressed.
// when called, funcOneTwoThree will check whether the string "12"
// is right before the cursor, and if it is, it should insert
// delete that "12" and insert "one-two-three" instead.
//
// NOTE: These function, on a successful insertion, return false
// indicating that the insersion has been taken care of
// and the default response to keypress should not be executed.
TypeSmart.makeReplacementFunctions = function (replacements) {
var replacement_functions = {};
for (replacement_string in replacements) {
var trigger = replacement_string [replacement_string.length - 1];
var prefix_to_match = replacement_string.substring (
0,
replacement_string.length - 1
);
var func_to_trigger = (
function (prefix_to_match, replacement_string, replacements) {
return function () {
var cursor = Cursor.new ();
var prefix = cursor.getTextBefore(prefix_to_match.length);
if (prefix == prefix_to_match) {
cursor.insert ("");
cursor.deleteBackward (prefix.length);
// Check if we need to insert a string
// or call a function.
if (typeof replacements [replacement_string]
== 'string') {
cursor.insert (replacements [replacement_string]);
return false;
}
else if (typeof replacements [replacement_string]
== 'function') {
return replacements [replacement_string] ();
}
else {
return true;
}
}
else return true;
};
}
)(prefix_to_match, replacement_string, replacements);
// One trigger might fire more than one functions
// For example "(" needs to fire functions for ":(" and ":-("
var final_func_to_trigger = func_to_trigger;
if (trigger in replacement_functions) {
var old_func = replacement_functions [trigger];
final_func_to_trigger = (
function (old_func, func_to_trigger) {
return function () {
// If the function is applicable,
// that is, if it finds a replacement and replaces
// at the cursor, it returns false
// So, call one function and if it fails, call another
// and return.
var retval = old_func ();
if (retval == false /*succeeded*/)
return false; /*even this should succeed*/
else
return func_to_trigger ();
};
}
)(old_func, func_to_trigger);
}
replacement_functions [trigger] = final_func_to_trigger;
}
return replacement_functions;
};
Array.prototype.contains = function (element) {
for (var i = 0; i < this.length; i++) {
if (this [i] == element) return true;
}
return false;
};
// dict = {
// "key1" : {
// "foo" : bar,
// "blah" : bleh
// },
// "key2" : {
// 1 : 2,
// 3 : 4
// },
// 5 : {
// 9 : "nine",
// "88" : 99
// }
// };
// whiteList = ["key1", 5, something_else];
// unifyWithKeyWhitelist (dict, whiteList)
// returns -> {
// 'foo' : bar,
// 'blah': bleh,
// 9 : "nine",
// "88" : 99
// }
TypeSmart.unifyWithKeyWhitelist = function (object, whiteList) {
var final_object = {};
for (key in object) {
if (whiteList.contains (key)) {
final_object = TypeSmart.mergeDicts (final_object, object [key]);
}
}
return final_object;
};
TypeSmart.getPasteHandler = function (class_list) {
// Merge user-defined replacement patterns
// and replacement-functions with the default ones.
if (typeof TypeSmart.my_replacements == "undefined") {
TypeSmart.my_replacements = {};
}
if (typeof TypeSmart.my_paste_modifiers == "undefined") {
TypeSmart.my_paste_modifiers = {};
}
// replacement function directly from the text patterns.
var replacements = TypeSmart.unifyWithKeyWhitelist (
TypeSmart.mergeDicts (
TypeSmart.my_replacements,
TypeSmart.default_replacements
),
class_list
);
var direct_replacements_func = (function (replacements) {
return function (text) {
for (to_be_replaced in replacements) {
text = text.split (to_be_replaced)
.join (replacements [to_be_replaced]);
}
return text;
};
})(replacements);
// replacement function combining pre-defined replacement functions
var paste_modifiers = TypeSmart.unifyWithKeyWhitelist (
TypeSmart.mergeDicts (
TypeSmart.my_paste_modifiers,
TypeSmart.default_paste_modifiers
),
class_list
);
var combined_from_pre_defined = direct_replacements_func;
for (discription in paste_modifiers) {
var next_func = paste_modifiers [discription];
combined_from_pre_defined = (
function (func1, func2){
return function (text) {
return func1 (func2 (text));
};
}
)(combined_from_pre_defined, next_func);
}
var handler = (
function (replacement_func) {
return function (event) {
var pasted_text = event.clipboardData.getData ('text/plain');
Cursor.new ().insert (replacement_func (pasted_text));
return false;
};
}
)(combined_from_pre_defined);
return handler;
};
// Create and return a function which should be triggered on a keypress
TypeSmart.getKeypressHandler = function (class_list) {
// Merge user-defined replacement patterns
// and letter-trigger-handlers with the default ones.
if (typeof TypeSmart.my_replacements == "undefined") {
TypeSmart.my_replacements = {};
}
if (typeof TypeSmart.my_custom_triggers == "undefined") {
TypeSmart.my_custom_triggers = {};
}
var replacements = TypeSmart.mergeDicts (
TypeSmart.my_replacements,
TypeSmart.default_replacements
);
var custom_triggers = TypeSmart.mergeDicts (
TypeSmart.my_custom_triggers,
TypeSmart.default_custom_triggers
);
// Strip the class names away
// Say, replacement is as following.
// replacements = {
// "classname" : {
// "str-to-replace" : "string-to-be-inserted"
// },
// "anotherClassname" : {
// "str1" : "result1",
// "str2" : "result2"
// },
// "thirdName" : {
// "anotherpattern" : "whatever"
// }
// };
// for
// <element class="classname anotherClassname"></element>
// final_replacements after stripping the classnames would be:
// final_replacements = {
// "str-to-replace" : "string-to-be-inserted",
// "str1" : "result1",
// "str2" : "result2"
// };
// As element is not of class "thirdName", its patterns are dropped.
var final_replacements = TypeSmart.unifyWithKeyWhitelist (
replacements,
class_list
);
// Similarly, strip the classnames from the relpacement functions.
// The resultant replacement_functions will be of the form
// replacement_functions = {
// 'string to trigger the function' : function_to_be_executed,
// 'another string' : another_func
// };
// This means when 'another string' is typed,
// another_func () will be called.
var replacement_functions = TypeSmart.unifyWithKeyWhitelist (
custom_triggers,
class_list
);
replacement_functions = TypeSmart.mergeDicts (
// Convert patterns to functions
TypeSmart.makeReplacementFunctions (replacement_functions),
TypeSmart.makeReplacementFunctions (final_replacements)
);
var getChar = function (event) {
if (event.which!=0 && event.charCode!=0) {
return String.fromCharCode(event.which);
}
else {
return null; // special key
}
};
var handler = (
function (replacement_functions, getChar) {
return function (event) {
var character = getChar (event || window.event);
if (!character) return true; // special key
else if (character in replacement_functions) {
return replacement_functions [character] ();
}
else return true;
};
}
)(replacement_functions, getChar);
return handler;
};
TypeSmart.attachHandlers = function (element) {
var class_list = element.className.split (/\s+/);
element.onkeypress = TypeSmart.getKeypressHandler (class_list);
element.onpaste = TypeSmart.getPasteHandler (class_list);
};
TypeSmart.init = function () {
// Load libcursor
var libcursor = document.createElement ("script");
libcursor.setAttribute ('type', 'text/javascript');
libcursor.setAttribute (
'src',
'//sujeet.github.io/libcursor/libcursor.js'
);
document.head.appendChild (libcursor);
// Attach keypress handlers to textareas and contenteditables.
var attachableElements = document.getElementsByClassName ('typeSmart');
for (var i = 0; i < attachableElements.length; i++) {
TypeSmart.attachHandlers (attachableElements [i]);
}
};