Index: trunk/parsers/wikidom/lib/es/es.Surface.js |
— | — | @@ -1,3 +1,17 @@ |
| 2 | +/*@cc_on |
| 3 | +(function(f) { |
| 4 | + window.setTimeout = f(window.setTimeout); // overwrites the global function! |
| 5 | + window.setInterval = f(window.setInterval); // overwrites the global function! |
| 6 | +})(function(f) { |
| 7 | + return function(c, t) { |
| 8 | + var a = [].slice.call(arguments, 2); // gathers the extra args |
| 9 | + return f(function() { |
| 10 | + c.apply(this, a); // passes them to your function |
| 11 | + }, t); |
| 12 | + }; |
| 13 | + }); |
| 14 | +@*/ |
| 15 | + |
2 | 16 | /** |
3 | 17 | * |
4 | 18 | * @param $container |
— | — | @@ -8,7 +22,7 @@ |
9 | 23 | this.$ = $container; |
10 | 24 | this.document = document; |
11 | 25 | this.rendered = false; |
12 | | - this.cursorInitialized = false; |
| 26 | + this.cursorInterval = null; |
13 | 27 | this.location = null; |
14 | 28 | this.render(); |
15 | 29 | |
— | — | @@ -19,8 +33,40 @@ |
20 | 34 | return surface.onMouseDown( e ); |
21 | 35 | } |
22 | 36 | }); |
| 37 | + |
| 38 | + // Cursor |
| 39 | + this.$cursor = $( '<div class="editSurface-cursor"></div>' ); |
| 40 | + this.$.after( this.$cursor ); |
| 41 | + |
| 42 | + // Hidden input |
| 43 | + this.$input = $( '<input/>' ); |
| 44 | + this.$.before( this.$input ); |
| 45 | + this.$input.bind({ |
| 46 | + 'keydown' : function(e) { |
| 47 | + return surface.onKeyDown( e ); |
| 48 | + } |
| 49 | + }); |
23 | 50 | } |
24 | 51 | |
| 52 | +Surface.prototype.onKeyDown = function( e ) { |
| 53 | + switch ( e.keyCode ) { |
| 54 | + case 37: // Left arrow |
| 55 | + this.moveCursorLeft(); |
| 56 | + break; |
| 57 | + case 38: // Up arrow |
| 58 | + this.moveCursorUp(); |
| 59 | + break; |
| 60 | + case 39: // Right arrow |
| 61 | + this.moveCursorRight(); |
| 62 | + break; |
| 63 | + case 40: // Down arrow |
| 64 | + this.moveCursorDown(); |
| 65 | + break; |
| 66 | + |
| 67 | + } |
| 68 | + return true; |
| 69 | +} |
| 70 | + |
25 | 71 | Surface.prototype.onMouseDown = function( e ) { |
26 | 72 | var $target = $( e.target ); |
27 | 73 | $block = $target.is( '.editSurface-block' ) ? $target : $target.closest( '.editSurface-block' ), |
— | — | @@ -34,6 +80,8 @@ |
35 | 81 | e.pageY - $block.offset().top ); |
36 | 82 | var offset = block.flow.getOffset( position ); |
37 | 83 | this.setCursor( new Location( block, offset ) ); |
| 84 | + this.$input.focus(); |
| 85 | + return false; |
38 | 86 | }; |
39 | 87 | |
40 | 88 | /** |
— | — | @@ -46,22 +94,17 @@ |
47 | 95 | |
48 | 96 | var position = this.location.block.getPosition( this.location.offset ); |
49 | 97 | var offset = this.location.block.$.offset(); |
50 | | - |
51 | | - if( !this.cursorInitialized ) { |
52 | | - this.$cursor = $( '<div class="editSurface-cursor"></div>' ); |
53 | | - this.$.after( this.$cursor ); |
54 | | - |
55 | | - setInterval( function( surface ) { |
56 | | - surface.$cursor.css('display') == 'block' ? surface.$cursor.hide() : surface.$cursor.show(); |
57 | | - }, 500, this ); |
58 | | - |
59 | | - this.cursorInitialized = true; |
60 | | - } |
61 | 98 | |
62 | 99 | this.$cursor.css({ |
63 | 100 | 'left': position.left + offset.left, |
64 | 101 | 'top': position.top + offset.top |
65 | 102 | }).show(); |
| 103 | + |
| 104 | + // Cursor blinking |
| 105 | + if( this.cursorInterval ) { |
| 106 | + clearInterval( this.cursorInterval ); |
| 107 | + } |
| 108 | + this.cursorInterval = setInterval( function( surface ) { surface.$cursor.css( 'display' ) == 'block' ? surface.$cursor.hide() : surface.$cursor.show(); }, 500, this ); |
66 | 109 | }; |
67 | 110 | |
68 | 111 | /** |
— | — | @@ -70,7 +113,7 @@ |
71 | 114 | * @returns {Location} |
72 | 115 | */ |
73 | 116 | Surface.prototype.getCursor = function() { |
74 | | - // return location |
| 117 | + return this.location; |
75 | 118 | }; |
76 | 119 | |
77 | 120 | /** |
— | — | @@ -178,7 +221,7 @@ |
179 | 222 | */ |
180 | 223 | Surface.prototype.moveCursorRight = function() { |
181 | 224 | var location = this.getCursor(); |
182 | | - if ( location.block.length < location.offset + 1 ) { |
| 225 | + if ( 1 || location.block.length > location.offset + 1 ) { |
183 | 226 | location.offset++; |
184 | 227 | } else { |
185 | 228 | var next = location.block.next(); |