3939define ( function ( require , exports , module ) {
4040
4141var oop = require ( "pilot/oop" ) ;
42+ var lang = require ( "pilot/lang" ) ;
4243var EventEmitter = require ( "pilot/event_emitter" ) . EventEmitter ;
4344
4445var Editor = require ( "ace/editor" ) . Editor ;
@@ -50,10 +51,10 @@ var Split = function(container, theme, splits) {
5051 this . $theme = theme ;
5152 this . $splits = 0 ;
5253 this . $editorCSS = "" ;
53- this . $cEditor = null ;
5454 this . $editors = [ ] ;
5555
5656 this . setSplits ( splits || 1 ) ;
57+ this . $cEditor = this . $editors [ 0 ] ;
5758
5859 this . on ( "focus" , function ( editor ) {
5960 this . $cEditor = editor ;
@@ -109,6 +110,18 @@ var Split = function(container, theme, splits) {
109110 return this . $editors [ idx ] ;
110111 }
111112
113+ this . getCurrentEditor = function ( ) {
114+ return this . $cEditor ;
115+ }
116+
117+ this . focus = function ( ) {
118+ this . $cEditor . focus ( ) ;
119+ }
120+
121+ this . blur = function ( ) {
122+ this . $cEditor . blur ( ) ;
123+ }
124+
112125 this . setTheme = function ( theme ) {
113126 this . $editors . forEach ( function ( editor ) {
114127 editor . setTheme ( theme ) ;
@@ -121,6 +134,63 @@ var Split = function(container, theme, splits) {
121134 } ) ;
122135 }
123136
137+ this . forEach = function ( callback , scope ) {
138+ this . $ditors . forEach ( callback , scope ) ;
139+ }
140+
141+ this . $cloneSession = function ( session ) {
142+ var s = new EditSession ( session . getDocument ( ) , session . getMode ( ) ) ;
143+
144+ var undoManager = session . getUndoManager ( ) ;
145+ if ( undoManager ) {
146+ var undoManagerProxy = new UndoManagerProxy ( undoManager , s ) ;
147+ s . setUndoManager ( undoManagerProxy ) ;
148+ }
149+
150+ // Overwrite the default $informUndoManager function such that new delas
151+ // aren't added to the undo manager from the new and the old session.
152+ s . $informUndoManager = lang . deferredCall ( function ( ) { s . $deltas = [ ] ; } ) ;
153+
154+ // Copy over 'settings' from the session.
155+ s . setTabSize ( session . getTabSize ( ) ) ;
156+ s . setUseSoftTabs ( session . getUseSoftTabs ( ) ) ;
157+ s . setOverwrite ( session . getOverwrite ( ) ) ;
158+ s . setBreakpoints ( session . getBreakpoints ( ) ) ;
159+ s . setUseWrapMode ( session . getUseWrapMode ( ) ) ;
160+ s . setUseWorker ( session . getUseWorker ( ) ) ;
161+ s . setWrapLimitRange ( session . $wrapLimitRange . min ,
162+ session . $wrapLimitRange . max ) ;
163+ s . $foldData = session . $cloneFoldData ( ) ;
164+
165+ return s ;
166+ }
167+
168+ this . setSession = function ( session , idx ) {
169+ var editor
170+ if ( idx == null ) {
171+ editor = this . $cEditor ;
172+ } else {
173+ editor = this . $editors [ idx ] ;
174+ }
175+
176+ // Check if the session is used already by any of the editors in the
177+ // split. If it is, we have to clone the session as two editors using
178+ // the same session can cause terrible side effects (e.g. UndoQueue goes
179+ // wrong). This also gives the user of Split the possibility to treat
180+ // each session on each split editor different.
181+ var isUsed = this . $editors . some ( function ( editor ) {
182+ return editor . session === session ;
183+ } ) ;
184+
185+ if ( isUsed ) {
186+ session = this . $cloneSession ( session ) ;
187+ }
188+ editor . setSession ( session ) ;
189+
190+ // Return the session set on the editor. This might be a cloned one.
191+ return session ;
192+ }
193+
124194 this . resize = function ( ) {
125195 var width = this . $container . clientWidth ;
126196 var height = this . $container . clientHeight ;
@@ -137,6 +207,46 @@ var Split = function(container, theme, splits) {
137207
138208} ) . call ( Split . prototype ) ;
139209
210+ function UndoManagerProxy ( undoManager , session ) {
211+ this . $u = undoManager ;
212+ this . $doc = session ;
213+ }
214+
215+ ( function ( ) {
216+ this . execute = function ( options ) {
217+ this . $u . execute ( options ) ;
218+ }
219+
220+ this . undo = function ( ) {
221+ var u = this . $u ;
222+ var deltas = u . $undoStack . pop ( ) ;
223+ if ( deltas ) {
224+ this . $doc . undoChanges ( deltas ) ;
225+ u . $redoStack . push ( deltas ) ;
226+ }
227+ }
228+
229+ this . redo = function ( ) {
230+ var u = this . $u ;
231+ var deltas = u . $redoStack . pop ( ) ;
232+ if ( deltas ) {
233+ this . $doc . redoChanges ( deltas ) ;
234+ u . $undoStack . push ( deltas ) ;
235+ }
236+ }
237+
238+ this . reset = function ( ) {
239+ this . $u . reset ( ) ;
240+ }
241+
242+ this . hasUndo = function ( ) {
243+ return this . $u . hasUndo ( ) ;
244+ }
245+
246+ this . hasRedo = function ( ) {
247+ return this . $u . hasRedo ( ) ;
248+ }
249+ } ) . call ( UndoManagerProxy . prototype ) ;
140250
141251exports . Split = Split ;
142252} ) ;
0 commit comments