forked from laughinghan/mathquill
/
rootelements.js
506 lines (451 loc) · 14.4 KB
/
rootelements.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
498
499
500
501
502
503
504
505
/*********************************************
* Root math elements with event delegation.
********************************************/
function createRoot(jQ, root, textbox, editable) {
var contents = jQ.contents().detach();
if (!textbox) {
jQ.addClass('mathquill-rendered-math');
}
root.jQ = jQ.data(jQueryDataKey, {
block: root,
revert: function() {
jQ.empty().unbind('.mathquill')
.removeClass('mathquill-rendered-math mathquill-editable mathquill-textbox')
.append(contents);
}
});
var cursor = root.cursor = new Cursor(root);
root.renderLatex(contents.text());
//textarea stuff
var textareaSpan = root.textarea = $('<span class="textarea"><textarea></textarea></span>'),
textarea = textareaSpan.children();
/******
* TODO [Han]: Document this
*/
var textareaSelectionTimeout;
root.selectionChanged = function() {
if (textareaSelectionTimeout === undefined) {
textareaSelectionTimeout = setTimeout(setTextareaSelection);
}
forceIERedraw(jQ[0]);
};
function setTextareaSelection() {
textareaSelectionTimeout = undefined;
var latex = cursor.selection ? '$'+cursor.selection.latex()+'$' : '';
textarea.val(latex);
if (latex) {
textarea[0].select();
}
}
//prevent native selection except textarea
jQ.bind('selectstart.mathquill', function(e) {
if (e.target !== textarea[0]) e.preventDefault();
e.stopPropagation();
});
//drag-to-select event handling
var anticursor, blink = cursor.blink;
jQ.bind('mousedown.mathquill', function(e) {
function mousemove(e) {
cursor.seek($(e.target), e.pageX, e.pageY);
if (cursor.prev !== anticursor.prev
|| cursor.parent !== anticursor.parent) {
cursor.selectFrom(anticursor);
}
return false;
}
// docmousemove is attached to the document, so that
// selection still works when the mouse leaves the window.
function docmousemove(e) {
// [Han]: i delete the target because of the way seek works.
// it will not move the mouse to the target, but will instead
// just seek those X and Y coordinates. If there is a target,
// it will try to move the cursor to document, which will not work.
// cursor.seek needs to be refactored.
delete e.target;
return mousemove(e);
}
function mouseup(e) {
anticursor = undefined;
cursor.blink = blink;
if (!cursor.selection) {
if (editable) {
cursor.show();
}
else {
textareaSpan.detach();
}
}
// delete the mouse handlers now that we're not dragging anymore
jQ.unbind('mousemove', mousemove);
$(document).unbind('mousemove', docmousemove).unbind('mouseup', mouseup);
}
setTimeout(function() { textarea.focus(); });
// preventDefault won't prevent focus on mousedown in IE<9
// that means immediately after this mousedown, whatever was
// mousedown-ed will receive focus
// http://bugs.jquery.com/ticket/10345
cursor.blink = $.noop;
cursor.seek($(e.target), e.pageX, e.pageY);
anticursor = new MathFragment(cursor.parent, cursor.prev, cursor.next);
if (!editable) jQ.prepend(textareaSpan);
jQ.mousemove(mousemove);
$(document).mousemove(docmousemove).mouseup(mouseup);
return false;
});
if (!editable) {
jQ.bind('cut paste', false).bind('copy', setTextareaSelection)
.prepend('<span class="selectable">$'+root.latex()+'$</span>');
textarea.blur(function() {
cursor.clearSelection();
setTimeout(detach); //detaching during blur explodes in WebKit
});
function detach() {
textareaSpan.detach();
}
return;
}
jQ.prepend(textareaSpan);
//root CSS classes
jQ.addClass('mathquill-editable');
if (textbox)
jQ.addClass('mathquill-textbox');
//focus and blur handling
textarea.focus(function(e) {
if (!cursor.parent)
cursor.appendTo(root);
cursor.parent.jQ.addClass('hasCursor');
if (cursor.selection) {
cursor.selection.jQ.removeClass('blur');
setTimeout(root.selectionChanged); //select textarea after focus
}
else
cursor.show();
e.stopPropagation();
}).blur(function(e) {
cursor.hide().parent.blur();
if (cursor.selection)
cursor.selection.jQ.addClass('blur');
e.stopPropagation();
});
jQ.bind('focus.mathquill blur.mathquill', function(e) {
textarea.trigger(e);
}).blur();
//clipboard event handling
jQ.bind('cut', function(e) {
setTextareaSelection();
if (cursor.selection) {
setTimeout(function() {
cursor.deleteSelection();
cursor.redraw();
});
}
e.stopPropagation();
})
.bind('copy', function(e) {
setTextareaSelection();
e.stopPropagation();
})
.bind('paste', function(e) {
pasting = true;
setTimeout(paste);
e.stopPropagation();
});
function paste() {
//FIXME HACK the parser in RootTextBlock needs to be moved to
//Cursor::writeLatex or something so this'll work with MathQuill textboxes
var latex = textarea.val();
if (latex.slice(0,1) === '$' && latex.slice(-1) === '$') {
latex = latex.slice(1, -1);
}
else {
latex = '\\text{' + latex + '}';
}
cursor.writeLatex(latex).show();
textarea.val('');
pasting = false;
}
//keyboard events and text input, see Wiki page "Keyboard Events"
var lastKeydn, lastKeydnHappened, lastKeypressWhich, pasting = false;
jQ.bind('keydown.mathquill', function(e) {
lastKeydn = e;
lastKeydnHappened = true;
if (cursor.parent.keydown(e) === false)
e.preventDefault();
}).bind('keypress.mathquill', function(e) {
if (lastKeydnHappened)
lastKeydnHappened = false;
else {
//there's two ways keypress might be triggered without a keydown happening first:
if (lastKeypressWhich !== e.which)
//all browsers do that if this textarea is given focus during the keydown of
//a different focusable element, i.e. by that element's keydown event handler.
//No way of knowing original keydown, so ignore this keypress
return;
else
//some browsers do that when auto-repeating key events, replay the keydown
cursor.parent.keydown(lastKeydn);
}
lastKeypressWhich = e.which;
//make sure setTextareaSelection() doesn't happen before textInput(), where we
//check if any text was typed
if (textareaSelectionTimeout !== undefined)
clearTimeout(textareaSelectionTimeout);
//after keypress event, trigger virtual textInput event if text was
//input to textarea
setTimeout(textInput);
});
function textInput() {
if (pasting || (
'selectionStart' in textarea[0]
&& textarea[0].selectionStart !== textarea[0].selectionEnd
)) return;
var text = textarea.val();
if (text) {
textarea.val('');
for (var i = 0; i < text.length; i += 1) {
cursor.parent.textInput(text.charAt(i));
}
textareaSelectionTimeout = undefined;
}
else {
if (textareaSelectionTimeout !== undefined)
setTextareaSelection();
}
}
}
function RootMathBlock(){}
_ = RootMathBlock.prototype = new MathBlock;
_.latex = function() {
return MathBlock.prototype.latex.call(this).replace(/(\\[a-z]+) (?![a-z])/ig,'$1');
};
_.text = function() {
return this.foldChildren('', function(text, child) {
return text + child.text();
});
};
_.renderLatex = function(latex) {
var jQ = this.jQ;
jQ.children().slice(1).remove();
this.firstChild = this.lastChild = 0;
// temporarily take the element out of the displayed DOM
// while we add stuff to it. Grab the next or parent node
// so we know where to put it back.
// NOTE: careful that it be the next or parent DOM **node** and not
// HTML element, lest we skip a text node.
var next = jQ[0].nextSibling,
parent = jQ[0].parentNode;
jQ.detach();
this.cursor.appendTo(this).writeLatex(latex);
// Put. the element. back.
// if there's no next element, it's at the end of its parent
next ? jQ.insertBefore(next) : jQ.appendTo(parent);
// XXX HACK ALERT
this.jQ.mathquill('redraw');
this.blur();
};
_.keydown = function(e)
{
e.ctrlKey = e.ctrlKey || e.metaKey;
switch ((e.originalEvent && e.originalEvent.keyIdentifier) || e.which) {
case 8: //backspace
case 'Backspace':
case 'U+0008':
if (e.ctrlKey)
while (this.cursor.prev || this.cursor.selection)
this.cursor.backspace();
else
this.cursor.backspace();
break;
case 27: //may as well be the same as tab until we figure out what to do with it
case 'Esc':
case 'U+001B':
case 9: //tab
case 'Tab':
case 'U+0009':
if (e.ctrlKey) break;
var parent = this.cursor.parent;
if (e.shiftKey) { //shift+Tab = go one block left if it exists, else escape left.
if (parent === this.cursor.root) //cursor is in root editable, continue default
return this.skipTextInput = true;
else if (parent.prev) //go one block left
this.cursor.appendTo(parent.prev);
else //get out of the block
this.cursor.insertBefore(parent.parent);
}
else { //plain Tab = go one block right if it exists, else escape right.
if (parent === this.cursor.root) //cursor is in root editable, continue default
return this.skipTextInput = true;
else if (parent.next) //go one block right
this.cursor.prependTo(parent.next);
else //get out of the block
this.cursor.insertAfter(parent.parent);
}
this.cursor.clearSelection();
break;
case 13: //enter
case 'Enter':
break;
case 35: //end
case 'End':
if (e.shiftKey)
while (this.cursor.next || (e.ctrlKey && this.cursor.parent !== this))
this.cursor.selectRight();
else //move to the end of the root block or the current block.
this.cursor.clearSelection().appendTo(e.ctrlKey ? this : this.cursor.parent);
break;
case 36: //home
case 'Home':
if (e.shiftKey)
while (this.cursor.prev || (e.ctrlKey && this.cursor.parent !== this))
this.cursor.selectLeft();
else //move to the start of the root block or the current block.
this.cursor.clearSelection().prependTo(e.ctrlKey ? this : this.cursor.parent);
break;
case 37: //left
case 'Left':
if (e.ctrlKey) break;
if (e.shiftKey)
this.cursor.selectLeft();
else
this.cursor.moveLeft();
break;
case 38: //up
case 'Up':
if (e.ctrlKey) break;
if (e.shiftKey) {
if (this.cursor.prev)
while (this.cursor.prev)
this.cursor.selectLeft();
else
this.cursor.selectLeft();
}
else if (this.cursor.parent.prev)
this.cursor.clearSelection().appendTo(this.cursor.parent.prev);
else if (this.cursor.prev)
this.cursor.clearSelection().prependTo(this.cursor.parent);
else if (this.cursor.parent !== this)
this.cursor.clearSelection().insertBefore(this.cursor.parent.parent);
break;
case 39: //right
case 'Right':
if (e.ctrlKey) break;
if (e.shiftKey)
this.cursor.selectRight();
else
this.cursor.moveRight();
break;
case 40: //down
case 'Down':
if (e.ctrlKey) break;
if (e.shiftKey) {
if (this.cursor.next)
while (this.cursor.next)
this.cursor.selectRight();
else
this.cursor.selectRight();
}
else if (this.cursor.parent.next)
this.cursor.clearSelection().prependTo(this.cursor.parent.next);
else if (this.cursor.next)
this.cursor.clearSelection().appendTo(this.cursor.parent);
else if (this.cursor.parent !== this)
this.cursor.clearSelection().insertAfter(this.cursor.parent.parent);
break;
case 46: //delete
case 'Del':
case 'U+007F':
if (e.ctrlKey)
while (this.cursor.next || this.cursor.selection)
this.cursor.deleteForward();
else
this.cursor.deleteForward();
break;
case 65: //the 'A' key, as in Ctrl+A Select All
case 'A':
case 'U+0041':
if (e.ctrlKey && !e.shiftKey && !e.altKey) {
if (this !== this.cursor.root) //so not stopPropagation'd at RootMathCommand
return this.parent.keydown(e);
this.cursor.clearSelection().appendTo(this);
while (this.cursor.prev)
this.cursor.selectLeft();
break;
}
default:
this.skipTextInput = false;
return true;
}
this.skipTextInput = true;
return false;
};
_.textInput = function(ch) {
if (!this.skipTextInput)
this.cursor.write(ch);
};
function RootMathCommand(cursor) {
this.init('$');
this.firstChild.cursor = cursor;
this.firstChild.textInput = function(ch) {
if (this.skipTextInput) return;
if (ch !== '$' || cursor.parent !== this)
cursor.write(ch);
else if (this.isEmpty()) {
cursor.insertAfter(this.parent).backspace()
.insertNew(new VanillaSymbol('\\$','$')).show();
}
else if (!cursor.next)
cursor.insertAfter(this.parent);
else if (!cursor.prev)
cursor.insertBefore(this.parent);
else
cursor.write(ch);
};
}
_ = RootMathCommand.prototype = new MathCommand;
_.html_template = ['<span class="mathquill-rendered-math"></span>'];
_.initBlocks = function() {
this.firstChild =
this.lastChild =
this.jQ.data(jQueryDataKey).block =
new RootMathBlock;
this.firstChild.parent = this;
this.firstChild.jQ = this.jQ;
};
_.latex = function() {
return '$' + this.firstChild.latex() + '$';
};
function RootTextBlock(){}
_ = RootTextBlock.prototype = new MathBlock;
_.renderLatex = function(latex) {
var self = this, cursor = self.cursor;
self.jQ.children().slice(1).remove();
self.firstChild = self.lastChild = 0;
cursor.show().appendTo(self);
latex = latex.match(/(?:\\\$|[^$])+|\$(?:\\\$|[^$])*\$|\$(?:\\\$|[^$])*$/g) || '';
for (var i = 0; i < latex.length; i += 1) {
var chunk = latex[i];
if (chunk[0] === '$') {
if (chunk[-1+chunk.length] === '$' && chunk[-2+chunk.length] !== '\\')
chunk = chunk.slice(1, -1);
else
chunk = chunk.slice(1);
var root = new RootMathCommand(cursor);
cursor.insertNew(root);
root.firstChild.renderLatex(chunk);
cursor.show().insertAfter(root);
}
else {
for (var j = 0; j < chunk.length; j += 1)
this.cursor.insertNew(new VanillaSymbol(chunk[j]));
}
}
};
_.keydown = RootMathBlock.prototype.keydown;
_.textInput = function(ch) {
if (this.skipTextInput) return;
this.cursor.deleteSelection();
if (ch === '$')
this.cursor.insertNew(new RootMathCommand(this.cursor));
else
this.cursor.insertNew(new VanillaSymbol(ch));
};