Skip to content

Commit c38ddf3

Browse files
committed
More work on Split. Update the demo UI on changes
1 parent 0ddbfa6 commit c38ddf3

7 files changed

Lines changed: 210 additions & 18 deletions

File tree

demo/demo.js

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,23 @@ exports.launch = function(env) {
152152
docs.textile.setMode(new TextileMode());
153153
docs.textile.setUndoManager(new UndoManager());
154154

155+
// Add a "name" property to all docs
156+
for (doc in docs) {
157+
docs[doc].name = doc;
158+
}
159+
155160
var container = document.getElementById("editor");
156161
var cockpitInput = document.getElementById("cockpitInput");
157162

158163
// Splitting.
159164
var Split = require("ace/split").Split;
160165
var split = new Split(container, theme, 1);
161166
env.editor = split.getEditor(0);
167+
split.on("focus", function(editor) {
168+
env.editor = editor;
169+
updateUIEditorOptions();
170+
});
162171
env.split = split;
163-
// env.editor = new Editor(new Renderer(container, theme));
164172
window.env = env;
165173
window.ace = env.editor;
166174

@@ -187,14 +195,36 @@ exports.launch = function(env) {
187195
return modes[modeEl.value];
188196
}
189197

198+
var docEl = document.getElementById("doc");
190199
var modeEl = document.getElementById("mode");
191200
var wrapModeEl = document.getElementById("soft_wrap");
201+
var themeEl = document.getElementById("theme");
202+
var selectStyleEl = document.getElementById("select_style");
203+
var highlightActiveEl = document.getElementById("highlight_active");
204+
var showHiddenEl = document.getElementById("show_hidden");
205+
var showGutterEl = document.getElementById("show_gutter");
206+
var showPrintMarginEl = document.getElementById("show_print_margin");
207+
var highlightSelectedWordE = document.getElementById("highlight_selected_word");
208+
var showHScrollEl = document.getElementById("show_hscroll");
209+
var softTabEl = document.getElementById("soft_tab");
192210

193211
bindDropdown("doc", function(value) {
194212
var doc = docs[value];
195-
env.editor.setSession(doc);
213+
var session = env.split.setSession(doc);
214+
session.name = doc.name;
215+
216+
updateUIEditorOptions();
217+
218+
env.editor.focus();
219+
});
196220

197-
var mode = doc.getMode();
221+
function updateUIEditorOptions() {
222+
var editor = env.editor;
223+
var session = editor.session;
224+
225+
docEl.value = session.name;
226+
227+
var mode = session.getMode();
198228
if (mode instanceof JavaScriptMode) {
199229
modeEl.value = "javascript";
200230
}
@@ -244,13 +274,22 @@ exports.launch = function(env) {
244274
modeEl.value = "text";
245275
}
246276

247-
if (!doc.getUseWrapMode()) {
277+
if (!session.getUseWrapMode()) {
248278
wrapModeEl.value = "off";
249279
} else {
250-
wrapModeEl.value = doc.getWrapLimitRange().min || "free";
280+
wrapModeEl.value = session.getWrapLimitRange().min || "free";
251281
}
252-
env.editor.focus();
253-
});
282+
283+
selectStyleEl.checked = editor.getSelectionStyle() == "line"
284+
themeEl.value = editor.getTheme();
285+
highlightActiveEl.checked = editor.getHighlightActiveLine();
286+
showHiddenEl.checked = editor.getShowInvisibles();
287+
showGutterEl.checked = editor.renderer.getShowGutter();
288+
showPrintMarginEl.checked = editor.renderer.getShowPrintMargin();
289+
highlightSelectedWordE.checked = editor.getHighlightSelectedWord();
290+
showHScrollEl.checked = editor.renderer.getHScrollBarAlwaysVisible();
291+
softTabEl.checked = session.getUseSoftTabs();
292+
}
254293

255294
bindDropdown("mode", function(value) {
256295
env.editor.getSession().setMode(modes[value] || modes.text);
@@ -538,6 +577,11 @@ exports.launch = function(env) {
538577
session.addFold(placeHolder, range);
539578
}
540579
}
580+
581+
env.split.setSplits(2);
582+
sp = env.split;
583+
session = sp.setSession(sp.getEditor(0).session, 1);
584+
session.name = sp.getEditor(0).session.name;
541585
};
542586

543587
});

index.html

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@
101101
<tr>
102102
<td >
103103
<label for="select_style">Full Line Selection</label>
104-
</td><td>
104+
</td><td>
105105
<input type="checkbox" name="select_style" id="select_style" checked>
106106
</td>
107107
</tr>
@@ -115,21 +115,21 @@
115115
<tr>
116116
<td >
117117
<label for="show_hidden">Show Invisibles</label>
118-
</td><td>
118+
</td><td>
119119
<input type="checkbox" name="show_hidden" id="show_hidden" checked>
120120
</td>
121121
</tr>
122122
<tr>
123123
<td >
124124
<label for="show_hscroll">Persistent HScroll</label>
125-
</td><td>
125+
</td><td>
126126
<input type="checkbox" name="show_hscroll" id="show_hscroll">
127127
</td>
128128
</tr>
129129
<tr>
130130
<td >
131131
<label for="keybinding">Key Binding</label>
132-
</td><td>
132+
</td><td>
133133
<select id="keybinding" size="1">
134134
<option value="ace">Ace</option>
135135
<option value="vim">Vim</option>
@@ -141,7 +141,7 @@
141141
<tr>
142142
<td >
143143
<label for="soft_wrap">Soft Wrap</label>
144-
</td><td>
144+
</td><td>
145145
<select id="soft_wrap" size="1">
146146
<option value="off">Off</option>
147147
<option value="40">40 Chars</option>
@@ -153,28 +153,28 @@
153153
<tr>
154154
<td >
155155
<label for="show_gutter">Show Gutter</label>
156-
</td><td>
156+
</td><td>
157157
<input type="checkbox" id="show_gutter" checked>
158158
</td>
159159
</tr>
160160
<tr>
161161
<td >
162162
<label for="show_print_margin">Show Print Margin</label>
163-
</td><td>
163+
</td><td>
164164
<input type="checkbox" id="show_print_margin" checked>
165165
</td>
166166
</tr>
167167
<tr>
168168
<td >
169169
<label for="soft_tab">Use Soft Tab</label>
170-
</td><td>
170+
</td><td>
171171
<input type="checkbox" id="soft_tab" checked>
172172
</td>
173173
</tr>
174174
<tr>
175175
<td >
176176
<label for="highlight_selected_word">Highlight selected word</label>
177-
</td><td>
177+
</td><td>
178178
<input type="checkbox" id="highlight_selected_word" checked>
179179
</td>
180180
</tr>

lib/ace/edit_session/fold_line.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function FoldLine(foldData, folds) {
5858
this.end = this.range.end;
5959

6060
this.folds.forEach(function(fold) {
61-
fold.foldLine = this;
61+
fold.setFoldLine(this);
6262
}, this);
6363
}
6464

lib/ace/edit_session/folding.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,22 @@ Fold.prototype.toString = function() {
5959
return '"' + this.placeholder + '" ' + this.range.toString();
6060
}
6161

62+
Fold.prototype.setFoldLine = function(foldLine) {
63+
this.foldLine = foldLine;
64+
this.subFolds.forEach(function(fold) {
65+
fold.setFoldLine(foldLine);
66+
});
67+
}
68+
69+
Fold.prototype.clone = function() {
70+
var range = this.range.clone();
71+
var fold = new Fold(range, this.placeholder);
72+
this.subFolds.forEach(function(subFold) {
73+
fold.subFolds.push(subFold.clone());
74+
});
75+
return fold;
76+
}
77+
6278
function Folding() {
6379
/**
6480
* Looks up a fold at a given row/column. Possible values for side:
@@ -526,6 +542,19 @@ function Folding() {
526542
foldLine, row, endColumn, startRow, startColumn);
527543
}
528544
};
545+
546+
this.$cloneFoldData = function() {
547+
var foldData = this.$foldData;
548+
var fd = [];
549+
fd = this.$foldData.map(function(foldLine) {
550+
var folds = foldLine.folds.map(function(fold) {
551+
return fold.clone();
552+
});
553+
return new FoldLine(fd, folds);
554+
});
555+
556+
return fd;
557+
};
529558
}
530559

531560
exports.Folding = Folding;

lib/ace/editor.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@ var Editor =function(renderer, session) {
217217
this.renderer.setTheme(theme);
218218
};
219219

220+
this.getTheme = function() {
221+
this.renderer.getTheme();
222+
}
223+
220224
this.setStyle = function(style) {
221225
this.renderer.setStyle(style)
222226
};

lib/ace/split.js

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
define(function(require, exports, module) {
4040

4141
var oop = require("pilot/oop");
42+
var lang = require("pilot/lang");
4243
var EventEmitter = require("pilot/event_emitter").EventEmitter;
4344

4445
var 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

141251
exports.Split = Split;
142252
});

0 commit comments

Comments
 (0)