Index: trunk/extensions/VisualEditor/contentEditable/ime.html |
— | — | @@ -53,6 +53,41 @@ |
54 | 54 | } |
55 | 55 | |
56 | 56 | } ); |
| 57 | + |
| 58 | + var oldText = 'Test'; |
| 59 | + var newText = 'Teeeeeest'; |
| 60 | + |
| 61 | + var sameFromLeft = 0, |
| 62 | + sameFromRight = 0, |
| 63 | + l = oldText.length; |
| 64 | + |
| 65 | + while ( sameFromLeft < l && oldText[sameFromLeft] == newText[sameFromLeft] ) { |
| 66 | + ++sameFromLeft; |
| 67 | + } |
| 68 | + if ( oldText.length > sameFromLeft ) { |
| 69 | + l = l - sameFromLeft; |
| 70 | + while ( sameFromRight < l && oldText[oldText.length - 1 - sameFromRight] == newText[newText.length - 1 - sameFromRight] ) { |
| 71 | + ++sameFromRight; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + console.log('sameFromLeft: ' + sameFromLeft); |
| 76 | + console.log('sameFromRight: ' + sameFromRight); |
| 77 | + |
| 78 | + if ( sameFromLeft + sameFromRight !== oldText.length ) { |
| 79 | + var toDelete = oldText.split(''); |
| 80 | + console.log(toDelete.slice(sameFromLeft,oldText.length - sameFromRight).join('')); |
| 81 | + } |
| 82 | + |
| 83 | + if ( sameFromLeft + sameFromRight !== newText.length ) { |
| 84 | + var toAdd = newText.split(''); |
| 85 | + console.log(toAdd.slice(sameFromLeft, newText.length - sameFromRight).join('')); |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | + |
57 | 92 | </script> |
58 | 93 | </head> |
59 | 94 | <body> |
Index: trunk/extensions/VisualEditor/modules/ve/ce/ve.es.Surface.js |
— | — | @@ -30,6 +30,10 @@ |
31 | 31 | _this.emitUpdate( 25 ); |
32 | 32 | } ); |
33 | 33 | |
| 34 | + this.$.mousedown( function(e) { |
| 35 | + return _this.onMouseDown( e ); |
| 36 | + } ); |
| 37 | + |
34 | 38 | this.$ |
35 | 39 | .on('cut copy', function(event) { |
36 | 40 | var key = rangy.getSelection().getRangeAt(0).toString().replace(/( |\r\n|\n|\r|\t)/gm,""); |
— | — | @@ -45,8 +49,8 @@ |
46 | 50 | |
47 | 51 | var selection = _this.getSelection(); |
48 | 52 | var tx = _this.model.getDocument().prepareRemoval( selection ); |
49 | | - _this.model.transact( tx ); |
50 | | - _this.showCursorAt(selection.start); |
| 53 | + //_this.model.transact( tx ); |
| 54 | + //_this.showCursorAt(selection.start); |
51 | 55 | }, 1); |
52 | 56 | } |
53 | 57 | |
— | — | @@ -75,10 +79,77 @@ |
76 | 80 | |
77 | 81 | // Initialization |
78 | 82 | this.documentView.renderContent(); |
| 83 | + |
| 84 | + this.worker = null; |
| 85 | + this.node = null; |
79 | 86 | }; |
80 | 87 | |
81 | 88 | /* Methods */ |
82 | 89 | |
| 90 | +ve.es.Surface.prototype.onMouseDown = function( e ) { |
| 91 | + if ( this.worker !== null ) { |
| 92 | + clearInterval( this.worker ); |
| 93 | + } |
| 94 | + |
| 95 | + var _this = this; |
| 96 | + |
| 97 | + setTimeout( function() { |
| 98 | + _this.node = rangy.getSelection().anchorNode; |
| 99 | + var prevText = _this.node.textContent; |
| 100 | + _this.worker = setInterval( function() { |
| 101 | + var text = _this.node.textContent; |
| 102 | + |
| 103 | + if ( text === prevText ) { |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + var nodeOffset = _this.getOffset( _this.node, 0 ); |
| 108 | + |
| 109 | + var sameFromLeft = 0, |
| 110 | + sameFromRight = 0, |
| 111 | + l = prevText.length; |
| 112 | + |
| 113 | + while ( sameFromLeft < l && prevText[sameFromLeft] == text[sameFromLeft] ) { |
| 114 | + ++sameFromLeft; |
| 115 | + } |
| 116 | + if ( prevText.length > sameFromLeft ) { |
| 117 | + l = l - sameFromLeft; |
| 118 | + while ( sameFromRight < l && prevText[prevText.length - 1 - sameFromRight] == text[text.length - 1 - sameFromRight] ) { |
| 119 | + ++sameFromRight; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + if ( sameFromLeft + sameFromRight !== prevText.length ) { |
| 124 | + // delete |
| 125 | + var range = new ve.Range( nodeOffset + sameFromLeft, nodeOffset + prevText.length - sameFromRight ); |
| 126 | + var tx = _this.model.getDocument().prepareRemoval( range ); |
| 127 | + _this.model.transact( tx ); |
| 128 | + } |
| 129 | + |
| 130 | + if ( sameFromLeft + sameFromRight !== text.length ) { |
| 131 | + // insert |
| 132 | + var data = text.split('').slice(sameFromLeft, text.length - sameFromRight); |
| 133 | + var tx = _this.documentView.model.prepareInsertion( nodeOffset + sameFromLeft, data); |
| 134 | + _this.model.transact( tx ); |
| 135 | + } |
| 136 | + |
| 137 | + prevText = text; |
| 138 | + }, 50 ); |
| 139 | + }, 1 ); |
| 140 | + |
| 141 | + |
| 142 | + /* |
| 143 | + |
| 144 | + var sel = rangy.getSelection(); |
| 145 | + |
| 146 | + |
| 147 | + if ( sel.anchorOffset === sel.focusOffset && sel.anchorNode === sel.focusNode ) { |
| 148 | + console.log("123"); |
| 149 | + } |
| 150 | + |
| 151 | + */ |
| 152 | +}; |
| 153 | + |
83 | 154 | ve.es.Surface.prototype.attachContextView = function( contextView ) { |
84 | 155 | this.contextView = contextView; |
85 | 156 | }; |
— | — | @@ -104,6 +175,11 @@ |
105 | 176 | |
106 | 177 | ve.es.Surface.prototype.getOffset = function( localNode, localOffset ) { |
107 | 178 | var $node = $( localNode ); |
| 179 | + |
| 180 | + if ( $node.hasClass( 'ce-leafNode' ) ) { |
| 181 | + return this.documentView.getOffsetFromNode( $node.data('view') ) + 1; |
| 182 | + } |
| 183 | + |
108 | 184 | while( !$node.hasClass( 'ce-leafNode' ) ) { |
109 | 185 | $node = $node.parent(); |
110 | 186 | } |
Index: trunk/extensions/VisualEditor/modules/ve/ce/ve.es.Content.js |
— | — | @@ -10,7 +10,7 @@ |
11 | 11 | // Events |
12 | 12 | var _this = this; |
13 | 13 | this.model.on( 'update', function( offset ) { |
14 | | - _this.render( offset || 0 ); |
| 14 | + //_this.render( offset || 0 ); |
15 | 15 | } ); |
16 | 16 | } |
17 | 17 | }; |