-
Notifications
You must be signed in to change notification settings - Fork 2
/
jquery.rich_textarea.js
5276 lines (3581 loc) · 153 KB
/
jquery.rich_textarea.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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (c) 2014 Flying Brick Software, LLC (http://www.flyingbricksoftware.com)
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
// if ddt is not included add an empty shim.
if ( typeof( ddt ) == 'undefined' ) {
var ddt = {
on: function() {},
off: function() {},
log: function() {},
warn: function() {},
error: function() {}
};
}
// --------------------------------------------------------------
/**
* A rich textarea with tagged autocomplete. (@mention)
*
* Implements a contenteditable <div> textarea replacement with the ability
* to include trigger character invoked in-place autocompletes. (e.g. @mentions, #tags, etc)
* in addition to defining callbacks that get triggered as the user types based on an array
* of regexes.
*
* Support is also included to add other rich text objects. Each such object
* is treated as a single character from the perspective of arrow keys, deletes,
* backspacing, etc.
*
* USAGE:
*
* $( selector ).rich_textarea( trigger_definitions, regex_definitions )
*
* where:
*
* trigger_definitions is an array of objects.
*
* Each object has keys:
*
* trigger: single trigger character that marks the start of a trigger word. (e.g. @, #, etc)
* callback: callback invoked on autocomplete. It expects two parameters, the string that
* triggered the autocomplete and the autcomplete response callback to pass the
* autocompletions to per jQuery autocomplete docs. {@link http://api.jqueryui.com/autocomplete/#option-source}
*
* regex_definitions is an array of objects
*
* regex: regular expression to match againt words entered into the area
* callback: callback to execute is a patten is matched. Accepts a word_entry object as a
* parameter which include startNode, startOffset, endNode, endOffset and word keys.
*
* EVENTS:
*
* enter.rich_textarea raised on the rich textarea div when the user presses the ENTER key. Useful for setting
* rich_textarea up as a search box.
*
* NOTE: The current version normalizes all browsers to use <BR> for newlines. There is still code left over from the previous
* attempt to handle each browser types idiosyncracies. See comments in code.
*
* @author <a href="http://miles-by-motorcycle.com/yermo">Yermo Lamers</a>, Co-Founder, Flying Brick Software, LLC
*
* @copyright Flying Brick Software, LLC
* @license MIT License http://www.opensource.org/licenses/mit-license.php
*
* @version 2017-03 Alpha 3
*
* @see https://github.com/Yermo/rich_textarea
*
* @see http://miles-by-motorcycle.com/static/rich_textarea/index.html
*
* @see http://api.jqueryui.com/jQuery.widget/
*
* @see http://miles-by-motorcycle.com/fv-b-8-664/the-richtextarea-jquery-plugin-----a-relatively-non-technical-description-
* @see http://miles-by-motorcycle.com/fv-b-8-665/you-can---t-select-that-----webkit-browser-selections-and-ranges-in-chrome-and-safari
*
* @see http://stackoverflow.com/questions/14027559/event-keycode-character-pointed-to-by-current-selection-when-typing-fast
* @see http://stackoverflow.com/questions/14098303/how-to-set-caret-cursor-position-in-a-contenteditable-div-between-two-divs
* @see http://stackoverflow.com/questions/14108115/chrome-divs-with-display-inline-block-breaks-up-and-down-arrows
*
* @todo implement skipping over zero space and extending the mult-char range while selecting using SHIFT->ARROW KEY.
*/
(function($) {
/**
* placeholder for rangy or native (document) selections.
*/
// var RANGE_HANDLER = rangy;
// var CREATERANGE_HANDLER = rangy;
var RANGE_HANDLER = document;
var CREATERANGE_HANDLER = document;
/**
* the rich_textarea widget definition
*/
$.widget( 'ui.rich_textarea', {
options: {
/**
* placeholder text.
*
* placeholder text to display in an empty rich_textarea until the user enters something in the box.
*/
placeholder: '',
/**
* definition of autocomplete triggers.
*
* an array of trigger definitions each containing keys:
*
* trigger: single character to use as a trigger
* callback: callback, with the autocomplete trigger word as a parameter, to feed completion list to autocomplete
*/
triggers: [],
/**
* definition of regexes.
*
* an array of regexes each containing keys:
*
* regex: regex to match entered words against.
* callback: callback with a single parameter, word_entry, containing startNode, startOffset, endNode, endOffset, and word keys.
*/
regexes: []
},
/**
* jQuery UI widget factory create method.
*
* @see http://api.jqueryui.com/jQuery.widget/
*/
_create: function() {
// fun with javascript scoping rules.
var richtext_widget = this;
/**
* the current trigger entry
*
* used in the autcomplete select callback to replace the trigger word
* with the selected value
*/
this.current_trigger = false;
/**
* saved copy of current range
*
* @see insertObject()
*/
this.currentRange = false;
/**
* flag used to let onKeyUp know not to process ENTER.
*
* @see _insertSelection()
*/
this.selectionEntered = false;
/**
* flag used to indicate whether or not to do regex replacements
*
* there are some chicken and eggs problems with autocomplete and the ENTER key.
*/
this.do_regex = true;
/**
* whether or not the autocomplete menu is open
*/
this.autocomplete_open = false;
/**
* make sure we have focus
*
* @todo this has negative side effects in too many cases causing the page to scroll when it really shouldn't.
*/
// this.element.focus();
/**
* if we have a placeholder and we have no content, add the placeholder.
*/
this.placeholderVisible = false;
if (( this.element.html() == '' ) && ( typeof this.options.placeholder != '' )) {
this.element.html( '<span style="color: gray">' + this.options.placeholder + '</span>' );
this.placeholderVisible = true;
}
// widget factory style event binding. First is the name of the
// event mapped onto the string name of the method to call.
this._on({
keyup: function( event ) { this._onKeyUp( event ); },
keypress: function( event ) { this._onKeyPress( event ); },
keydown: function( event ) { this._onKeyDown( event ); },
mouseup: function( event ) { this._onMouseUp( event ); },
focus: function( event ) { this._onFocus( event ); },
blur: function( event ) { this._onBlur( event ); },
paste: function( event ) { this._onPaste( event ); },
prepaste: function( event ) { this._onPrePaste( event ); },
postpaste: function( event ) { this._onPostPaste( event ); }
});
/**
* bind the standard jquery-ui autocomplete to our element
*
* (not sure if using this.element here is an acceptable way of getting at the element.)
*/
this.element.autocomplete({
/**
* add support to format dropdown entries in HTML so we can embed images.
*
* @see jquery.ui.autocomplete.html.js
*/
html: 'html',
/**
* blur event
*/
blur: function( event, ui ) {
ddt.log( "autocompete blur event" );
},
/**
* callback on open
*/
open: function( event, ui ) {
ddt.log( "autocomplete open: event" );
richtext_widget.autocomplete_open = true;
},
/**
* callback on close
*/
close: function( event, ui ) {
ddt.log( "autocomplete close: event" );
richtext_widget.autocomplete_open = false;
richtext_widget.do_regex = true;
},
/**
* change event handler
*/
change: function( event, ui ) {
ddt.log( "autocomplete change event" );
},
/**
* the autocomplete data source
*
* uses the source callback in the trigger definition to get the list of
* possible completions. As a result, each different trigger character (@,#, et al ) can
* pull an autocompletion from a different source.
*
* The request value sent as the request parameter by the autocomplete plugin contains
* the entire contents of the div so instead we ignore the request and look for the
* trigger word in the editable div contents at the current cursor position.
*
* @param {String} request (ignored) the request from auto-complete
* @param {Function} response the autocomplete response callback that expects an array of responses
*
* @see http://api.jqueryui.com/autocomplete/#option-source
* @see _checkForTrigger()
*
* @todo make minLength configurable.
*/
source: function( request, response ) {
ddt.log( "source(): got source event" );
// this might be NULL when set in a mouseup event.
var trigger_entry = richtext_widget._checkForTrigger();
if (( trigger_entry == false ) || ( trigger_entry == null )) {
ddt.log( "source(): no trigger" );
return;
}
ddt.log( "source(): got source event with trigger ", trigger_entry );
// we're passing in the trigger term outside the normal autocomplete
// path, so the minLength option in autocomplete doesn't work. We'll set it
// as 2 here for now.
if (( trigger_entry != false ) &&
( trigger_entry.word.length >= 2 )) {
ddt.log( "source(): invoking response" );
// this causes the autocomplete menu to be populated
trigger_entry.callback( trigger_entry.word, response );
}
},
/**
* focus callback
*
* when the user moves through the list of items in the dropdown we don't want
* the value of the div to be replaced. We selectively replace the trigger word
* in the select callback.
*
* @see http://api.jqueryui.com/autocomplete/#event-focus
*/
focus: function( event, ui ) {
ddt.log( "focus(): autocomplete got focus event", ui );
event.preventDefault();
return false;
},
/**
* select callback
*
* By default jQuery autocomplete will replace the entire contents of the DIV with
* the selected value. We prevent that behavior and override it with our own approach.
*
* Once the user has selected something from the select dropdown we want to replace
* the current trigger word with the selection.
*
* It's not immediately clear from the jquery ui docs, but the selected value is
* returned in ui.item.value and ui.item.name.
*
* The current start and end offsets for the trigger word are in the current_trigger member.
*
* @see http://api.jqueryui.com/autocomplete/#event-select
*/
select: function( event, ui ) {
ddt.log( "select(): got select event ", event, " and ui element ", ui, " current range is ", this.currentRange );
// replace the trigger word, including trigger character, with the selected
// value.
richtext_widget._insertSelection( richtext_widget.current_trigger, ui.item.value );
// it's just been inserted so the cursor should be right behind the thing.
richtext_widget._highlightObject();
// prevent jQuery autocomplete from replacing all content in the div.
event.preventDefault();
// prevent regexes from running.
richtext_widget.do_regex = false;
}
}); // end of this.element.autocomplete
}, // end of _create()
/**
* handle keypresses near inserted objects
*
* Handles:
*
* . the case where the user attempts to backspace over an embedded object.
* . the case of using an arrow key to jump over the object
* . the case where the user is using the arrow keys or backspace over a zero width whitespace character.
*
* We handle this case in KeyDown in order to intercept the keystroke
* before it affects the content area. It's important to note that onKeyDown is called BEFORE
* any changes are made to the content area, so the current selection is the position BEFORE the
* action of the keystroke is performed.
*
* BUG: Chrome will return keyCode 0 if the Delete key is pressed on the number keypad under Fedora Linux.
* (it will, however, frustratingly then proceed to delete the character)
*/
_onKeyDown: function( event ) {
ddt.log( "_onKeyDown(): with key '" + event.keyCode + "' event: ", event );
var object = null;
var location = null;
var sel = null;
// if the autocomplete menu is open don't do anything with up and down arrow keys
if ( this.autocomplete_open ) {
if (( event.which == $.ui.keyCode.UP ) || ( event.which == $.ui.keyCode.DOWN ) || ( event.which == $.ui.keyCode.ENTER )) {
event.preventDefault();
return true;
}
}
// we may have a multiple char selection in which case we want to let the browser handle
// it. (see _onKeyUp)
sel = RANGE_HANDLER.getSelection();
if ( sel.rangeCount ) {
var range = RANGE_HANDLER.getSelection().getRangeAt(0);
if ( range.collapsed == false ) {
ddt.log( "_onKeyDown(): multi-char range. skipping onKeyDown processing" );
return;
}
}
switch ( event.which ) {
case 0:
// is i tbetter to have it do nothing than to make the contents of the editable div
// inconsistent? In my testing with Chrome 17.0.963.56 under Linux, we only get a 0 with
// the number pad delete key. Seems to work correctly on other platforms.
ddt.error( "_onKeyDown(): WEBKIT BUG: Keycode 0 returned. probably delete on Linux keypad but can't be sure" );
// event.preventDefault();
break;
case $.ui.keyCode.BACKSPACE:
ddt.log( "_onKeyDown(): BACKSPACE pressed" );
var retval = false;
if (( retval = this._backspaceZeroSpace()) == 'stop' ) {
ddt.log( "_onKeyDown(): BACKSPACE: stop word encountered. stopping" );
return;
}
// is the previous character an embedded object?
ddt.log( "_onKeyDown(): BACKSPACE: checking to see if we are next to an object" );
if ( object = this._checkForAdjacentObject( 'left' )) {
ddt.log( "_onKeyDown(): backspacing over an object :", object );
this.deleteObject( object.dom_node );
// if we deleted any whitespace characters prevent the browser from
// deleting whatever the next character is
if ( object.preventDefault ) {
event.preventDefault();
}
}
ddt.log( "_onKeyDown(): BACKSPACE: done" );
break;
case $.ui.keyCode.DELETE:
ddt.log( "_onKeyDown(): DELETE pressed" );
var retval = false;
if (( retval = this._deleteZeroSpace()) == 'stop' ) {
ddt.log( "_onKeyDown(): DELETEE: stop word encountered. stopping" );
return;
}
// is the next character an embedded object?
ddt.log( "_onKeyDown(): DELETE: checking to see if we are next to an object" );
if ( object = this._checkForAdjacentObject( 'right' )) {
ddt.log( "_onKeyDown(): DELETE: deleting an object :", object );
this.deleteObject( object.dom_node );
// after the delete we may be edge up against another object.
event.preventDefault();
}
ddt.log( "_onKeyDown(): DELETE done" );
break;
case $.ui.keyCode.LEFT:
ddt.log( "_onKeyDown(): LEFT pressed" );
// where is the caret now?
var caret = this._getCaretPosition();
ddt.log( "_onKeyDown(): _getCaretPosition() returned node '" + caret.dom_node + "' with offset '" + caret.offset + "'" );
// are we immediately next to an object? Jump the cursor over it.
if ( object = this._isEmbeddedObject( caret.dom_node ) ) {
ddt.log( "_onKeyDown(): setting caret before object: ", object );
// this._setCaretPositionRelative( object, 'before' );
this._setCaretPositionRelative( object.previousSibling, 'end' )
// FIXME: prevent the caret from jumping an additional space.
//
// In WebKit in the left arrow case without preventDefault() the
// caret jumps an additional position. HOWEVER, in the right case
// with it enabled the caret does not jump over the end of the span
// even if I select it.
event.preventDefault();
break;
}
// We are not right next to an object but there may be some number of zero width
// characters and/or textnodes between the current caret position and the object
//
// _moveCaret() may move the cursor into another container in which
// case we do NOT try to jump over any next object.
if ( location = this._moveCaret( caret.dom_node, caret.offset, 'left' ) ) {
ddt.log( "_onKeyDown(): LEFT: _moveCaret returned:", location );
// unless we've just moved into a container (i.e. new line) , we want to jump over any
// embedded objects we've arrived next to.
if (( location.checkForObjects ) && ( object = this._checkForAdjacentObject( 'left' ))) {
ddt.log( "_onKeyDown(): LEFT: jumping over object to the left" );
this._setCaretPositionRelative( object.dom_node.previousSibling, 'end' )
event.preventDefault();
}
// HACK: special fix for WebKit. When jumping out of containers don't let the
// browser do it's thing otherwise it'll jump one too far. Same for Mozilla.
if ( location.preventDefault ) {
ddt.log( "_onKeyDown(): Leaving a container, etc. preventing default" );
event.preventDefault();
}
// If it's a text node we want to let the browser move the cursor for us.
}
ddt.log( "_onKeyDown(): LEFT done" );
break;
case $.ui.keyCode.RIGHT:
ddt.log( "_onKeyDown(): RIGHT pressed" );
// where is the caret now?
var caret = this._getCaretPosition();
ddt.log( "_onKeyDown(): _getCaretPosition() returned node '" + caret.dom_node + "' with offset '" + caret.offset + "'" );
// are we next to an object? Jump the cursor over it.
if ( object = this._isEmbeddedObject( caret.dom_node ) ) {
ddt.log( "_onKeyDown(): isEmbedded true. moving to beginning of nextSibling" );
this._setCaretPositionRelative( object.nextSibling, 'beginning' )
// FIXME: prevent the caret from jumping an additional space.
// This does not make sense to me. In the LEFT arrow case without the preventDefault
// the cursor jumps an additional space in WebKit. However, in the RIGHT arrow case
// if I preventDefault, the caret cannot be moved outside of the span when jumping over
// an object. Clearly there is something I do not understand.
// event.preventDefault();
break;
}
// _moveCaret() may move the cursor into another container in which
// case we do NOT try to jump over any next object.
if ( location = this._moveCaret( caret.dom_node, caret.offset, 'right' ) ) {
ddt.log( "_onKeyDown(): RIGHT: after skipping over zero space. checking for adjacent objects :", location );
// if we are right next to an object we want to jump the
// cursor over it but ONLY if we are not moving onto a new line (i.e. moving into
// a child container)
if (( location.checkForObjects ) && ( object = this._checkForAdjacentObject( 'right' ))) {
ddt.log( "_onKeyDown(): RIGHT: right arrowing over an object" );
this._setCaretPositionRelative( object.dom_node.nextSibling, 'beginning' )
// FIXME: see above. preventingDefault() here breaks jumping across objects.
// event.preventDefault();
}
// HACK: special fix for WebKit. When jumping into or out of containers don't let the
// browser do it's thing otherwise it'll jump one too far. Same for Mozilla and a BR.
if ( location.preventDefault ) {
ddt.log( "_onKeyDown(): entering or leaving a container. preventing default" );
event.preventDefault();
}
}
ddt.log( "_onKeyDown(): RIGHT done." );
break;
case $.ui.keyCode.ENTER:
// the trick that took so long to figure out is that we can actually prevent the default
// behavior of the browser by preventing the default behavior in the onKeyDown event.
//
// Without this, each major browser puts different markup in the div.
//
// @see _handleEnter()
this._handleEnter( event );
// prevent default behavior
return false;
break;
} // end of switch
}, // end of _onKeyDown()
/**
* keypress handler
*
* There's a conflict between our regex processing and how autocomplete handles kepresses.
*
* We want autocomplete to run first then our stuff, but before the content area has a chance
* to update.
*/
_onKeyPress: function( event ) {
ddt.log( "_onKeyPress(): with key '" + event.keyCode + "' event: ", event );
// if the autocomplete menu is open don't do anything with up and down arrow keys
if ( this.autocomplete_open ) {
if (( event.which == $.ui.keyCode.UP ) || ( event.which == $.ui.keyCode.DOWN ) || ( event.which == $.ui.keyCode.ENTER )) {
event.preventDefault();
return true;
}
}
switch ( event.which ) {
case $.ui.keyCode.SPACE:
// on pressing SPACE check to see if we match any regexes.
ddt.log( "_onKeyPress(): SPACE pressed. Checking regexes" );
this._checkRegexes( event );
break;
case $.ui.keyCode.ENTER:
// we have to be careful not to let regexes conflict with the autocomplete
// dropdown. The dropdown catches the keypress event for ENTER but does not, for
// whatever reason, stop propagation so we get that keypress here.
ddt.log( "_onKeyPress(): ENTER pressed. Checking regexes" );
this._checkRegexes( event );
break;
}
}, // end of onKeyPress()
/**
* check for spaces and arrow key moves
*
* This method manages cancelling any active autocomplete. It also handles the case
* where the user UP or DOWN arrows into the middle of an embedded object.
*
* @see http://api.jquery.com/keyup/
* @see _insertSelection()
*/
_onKeyUp: function( event ) {
var caret = null;
ddt.log( "_onKeyUp(): with key '" + event.which + "':", event );
// if the autocomplete menu is open don't do anything with up and down arrow keys
if ( this.autocomplete_open ) {
if (( event.which == $.ui.keyCode.UP ) || ( event.which == $.ui.keyCode.DOWN ) || ( event.which == $.ui.keyCode.ENTER )) {
event.preventDefault();
return true;
}
}
// we may have a multiple char selection
if ( this._handleRangeSelection() ) {
// let the user do with the range whatever they want. The browser will
// take care of it.
return;
}
// save the range in case we click out of the div and then want to insert
// something.
this._saveRange();
// if we are at the end of an object, highlight it to indicate
// that it'll get deleted on backspace.
this._highlightObject();
// we may arrive here because of arrow key and other events.
switch ( event.which ) {
case $.ui.keyCode.ENTER:
ddt.log( "_onKeyUp(): ENTER: pressed. Closing autocomplete menu." );
// we close the autocomplete menu on any of these keys.
this.element.autocomplete( 'close' );
this.current_trigger = false;
// FIXME: see _insertSelection(). jQuery (or maybe my) bug where this
// callback is still getting invoked even when enter is pressed in the autocomplete
// dropdown.
if ( this.selectionEntered ) {
this.selectionEntered = false;
break;
}
this._onEnterFixUp( event );
ddt.log( "_onKeyUp(): ENTER. done." );
break;
case $.ui.keyCode.SPACE:
case $.ui.keyCode.TAB:
case $.ui.keyCode.HOME:
case $.ui.keyCode.END:
// we close the autocomplete menu on any of these keys.
this.element.autocomplete( 'close' );
this.current_trigger = false;
ddt.log( "_onKeyUp(): closed autocomplete menu" );
break;
case $.ui.keyCode.LEFT:
case $.ui.keyCode.RIGHT:
case $.ui.keyCode.BACKSPACE:
ddt.log( '_onKeyUp(): arrow/backspace pressed' );
// using CNTRL-LEFT AND RIGHT it's possible to get inside an object.
caret = this._getCaretPosition();
var object_node = this._clickedOnObject( caret.dom_node );
if ( object_node != false ) {
ddt.log( "_onKeyUp(): currently in an object. moving caret before object" );
this._setCaretPositionRelative( object_node, 'before' );
return;
}
// have we moved beyond the limit of a trigger character?
if ( ! this._checkForTrigger() ) {
ddt.log( "_onKeyUp(): outside of trigger" );
this.element.autocomplete( 'close' );
this.current_trigger = false;
} else {
ddt.log( '_onKeyUp(): in trigger' );
// autocomplete BUG? If we do not pass a value for the second argument here
// search is not invoked.
//
// We call this here to force the autocomplete menu open if we move the cursor
// over a trigger word. autocomplete does not do that automatically.
this.element.autocomplete( 'search', 'test' );
}
break;
case $.ui.keyCode.UP:
case $.ui.keyCode.DOWN:
// it's always something. WebKit, if you delete the newline at the beginning
// of the line (wrapping div) will delete any embedded contenteditable=false spans
// when joining the lines. Ugh.
//
// So this means everything needs to be contenteditable so we need to enforce NOT
// moving into the middle of an embedded object ourselves.
//
// So if the user moves up or down into an object move the cursor to the beginning of
// the object.
caret = this._getCaretPosition();
var object_node = this._clickedOnObject( caret.dom_node );
if ( object_node != false ) {
this._setCaretPositionRelative( object_node, 'before' );
}
break;
} // end of switch over keys.
}, // end of _onKeyUp()
/**
* check to see if mouse click is inside a trigger.
*
* check to see if mouse click is inside a trigger or an embedded object. Also
* handle the case where the user made a click drag selection into the middle of
* objects on either end.
*/
_onMouseUp: function( event ) {
var trigger_entry;
// make sure the autocomplete menu is closed.
this.element.autocomplete( 'close' );
// scrollbar causes this event to fire so we need to guard against the fact
// the editable div may not have focus.
if ( ! $( '#' + this.element.attr( 'id' ) ).is( ":focus" ) ) {
ddt.log( "_onMouseUp(): the div does not have focus" );
return true;
}
if ( this._handleRangeSelection() ) {
// let the user do with the range whatever they want. The browser will
// take care of it.
return;
}
// save the range in case we click out of the div and then want to insert
// something.
this._saveRange();
ddt.log( "_onMouseUp(): did we click on an object?:", event.target );
var object_node = this._clickedOnObject( event.target );
if ( object_node != false ) {
ddt.log( "_onMouseUp(): preventing default action" );
this._setCaretPositionRelative( object_node, 'before' );
event.preventDefault();
}
// if we are at the end of an object, highlight it to indicate
// that it'll get deleted on backspace.
this._highlightObject();
if ( trigger_entry = this._checkForTrigger() ) {
ddt.log( "_onMouseUp(): calling autocomplete from onMouseUp handler with term '" + trigger_entry.word + "'" );
// FIXME: for some reason when _checkForTrigger() is called from the autocomplete source callback
// the selection is lost. So _checkForTrigger() sets a class level copy which source checks. Ugly.
this.element.autocomplete( 'search', 'test' );
}
}, // end of _onMouseUp()
/**
* handle focus event
*
* Prevents the selection from becoming invalid if the autocomplete menu is open.
*/
_onFocus: function( event ) {
ddt.log( "focus(): top. Triggering bubble focus." );
// do we have a placeholder?
if ( this.placeholderVisible ) {
this._clearPlaceholder();
}
$( this.element ).trigger( 'bubblefocus' );
if ( this.autocomplete_open ) {