Skip to content

Commit 1c432b9

Browse files
committed
fix toggle show invisibles and add it to the demo
1 parent 9ddc3db commit 1c432b9

File tree

4 files changed

+64
-42
lines changed

4 files changed

+64
-42
lines changed

demo/demo_startup.js

+47-30
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,37 @@ exports.launch = function(env) {
7777
docs.php.setMode(new PhpMode());
7878
docs.php.setUndoManager(new UndoManager());
7979

80-
var docEl = document.getElementById("doc");
8180

8281
var container = document.getElementById("editor");
8382
env.editor = new Editor(new Renderer(container, theme));
83+
84+
var modes = {
85+
text: new TextMode(),
86+
xml: new XmlMode(),
87+
html: new HtmlMode(),
88+
css: new CssMode(),
89+
javascript: new JavaScriptMode(),
90+
python: new PythonMode(),
91+
php: new PhpMode()
92+
};
93+
94+
function getMode() {
95+
return modes[modeEl.value];
96+
}
97+
98+
99+
var modeEl = document.getElementById("mode");
100+
function setMode() {
101+
env.editor.getDocument().setMode(modes[modeEl.value] || modes.text);
102+
}
103+
modeEl.onchange = setMode;
104+
setMode();
84105

106+
var docEl = document.getElementById("doc");
85107
function onDocChange() {
86-
var doc = getDoc();
108+
var doc = docs[docEl.value];
87109
env.editor.setDocument(doc);
88-
110+
89111
var mode = doc.getMode();
90112
if (mode instanceof JavaScriptMode) {
91113
modeEl.value = "javascript";
@@ -108,55 +130,50 @@ exports.launch = function(env) {
108130
else {
109131
modeEl.value = "text";
110132
}
111-
133+
112134
env.editor.focus();
113135
}
114136
docEl.onchange = onDocChange;
137+
onDocChange();
115138

116-
function getDoc() {
117-
return docs[docEl.value];
118-
}
119-
120-
var modeEl = document.getElementById("mode");
121-
modeEl.onchange = function() {
122-
env.editor.getDocument().setMode(modes[modeEl.value] || modes.text);
123-
};
124-
125-
var modes = {
126-
text: new TextMode(),
127-
xml: new XmlMode(),
128-
html: new HtmlMode(),
129-
css: new CssMode(),
130-
javascript: new JavaScriptMode(),
131-
python: new PythonMode(),
132-
php: new PhpMode()
133-
};
134-
135-
function getMode() {
136-
return modes[modeEl.value];
137-
}
138139

139140
var themeEl = document.getElementById("theme");
140-
themeEl.onchange = function() {
141+
function setTheme() {
141142
env.editor.setTheme(themeEl.value);
142143
};
144+
themeEl.onchange = setTheme;
145+
setTheme();
146+
143147

144148
var selectEl = document.getElementById("select_style");
145-
selectEl.onchange = function() {
149+
function setSelectionStyle() {
146150
if (selectEl.checked) {
147151
env.editor.setSelectionStyle("line");
148152
} else {
149153
env.editor.setSelectionStyle("text");
150154
}
151155
};
156+
selectEl.onchange = setSelectionStyle;
157+
setSelectionStyle();
158+
152159

153160
var activeEl = document.getElementById("highlight_active");
154-
activeEl.onchange = function() {
161+
function setHighlightActiveLine() {
155162
env.editor.setHighlightActiveLine(!!activeEl.checked);
156163
};
164+
activeEl.onchange = setHighlightActiveLine;
165+
setHighlightActiveLine();
166+
167+
168+
var showHiddenEl = document.getElementById("show_hidden");
169+
function setShowInvisibles() {
170+
env.editor.setShowInvisibles(!!showHiddenEl.checked);
171+
};
172+
showHiddenEl.onchange = setShowInvisibles;
173+
setShowInvisibles();
157174

158-
onDocChange();
159175

176+
// for debugging
160177
window.jump = function() {
161178
var jump = document.getElementById("jump");
162179
var cursor = env.editor.getCursorPosition();

editor.html

+4
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@
113113
<label for="highlight_active">Highlight active line</label>
114114
<input type="checkbox" name="highlight_active" id="highlight_active" checked>
115115
</td>
116+
<td>
117+
<label for="show_hidden">Show invisibles</label>
118+
<input type="checkbox" name="show_hidden" id="show_hidden">
119+
</td>
116120
<td align="right">
117121
<img src="demo/logo.png">
118122
</td>

lib/ace/layer/text.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,18 @@ var Text = function(parentEl) {
129129
this.doc = doc;
130130
};
131131

132-
this.$showInvisibles = false;
132+
this.showInvisibles = false;
133133
this.setShowInvisibles = function(showInvisibles) {
134-
this.$showInvisibles = showInvisibles;
134+
if (this.showInvisibles == showInvisibles)
135+
return false;
136+
137+
this.showInvisibles = showInvisibles;
138+
return true;
135139
};
136140

137141
this.$computeTabString = function() {
138142
var tabSize = this.doc.getTabSize();
139-
if (this.$showInvisibles) {
143+
if (this.showInvisibles) {
140144
var halfTab = (tabSize) / 2;
141145
this.$tabString = "<span class='ace_invisible'>"
142146
+ new Array(Math.floor(halfTab)).join("&nbsp;")
@@ -263,7 +267,7 @@ var Text = function(parentEl) {
263267
};
264268

265269
this.$renderLine = function(stringBuilder, row, tokens) {
266-
// if (this.$showInvisibles) {
270+
// if (this.showInvisibles) {
267271
// var self = this;
268272
// var spaceRe = /[\v\f \u00a0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000]+/g;
269273
// var spaceReplace = function(space) {
@@ -294,7 +298,7 @@ var Text = function(parentEl) {
294298
}
295299
};
296300

297-
if (this.$showInvisibles) {
301+
if (this.showInvisibles) {
298302
if (row !== this.doc.getLength() - 1) {
299303
stringBuilder.push("<span class='ace_invisible'>" + this.EOL_CHAR + "</span>");
300304
} else {

lib/ace/virtual_renderer.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -229,16 +229,13 @@ var VirtualRenderer = function(container, theme) {
229229
});
230230
};
231231

232-
this.$showInvisibles = true;
233232
this.setShowInvisibles = function(showInvisibles) {
234-
this.$showInvisibles = showInvisibles;
235-
this.$textLayer.setShowInvisibles(showInvisibles);
236-
237-
this.$loop.schedule(this.CHANGE_TEXT);
233+
if (this.$textLayer.setShowInvisibles(showInvisibles))
234+
this.$loop.schedule(this.CHANGE_TEXT);
238235
};
239236

240237
this.getShowInvisibles = function() {
241-
return this.$showInvisibles;
238+
return this.$textLayer.showInvisibles;
242239
};
243240

244241
this.$showPrintMargin = true;
@@ -453,7 +450,7 @@ var VirtualRenderer = function(container, theme) {
453450

454451
this.$getLongestLine = function() {
455452
var charCount = this.doc.getScreenWidth();
456-
if (this.$showInvisibles)
453+
if (this.$textLayer.showInvisibles)
457454
charCount += 1;
458455

459456
return Math.max(this.$size.scrollerWidth - this.$padding * 2, Math.round(charCount * this.characterWidth));

0 commit comments

Comments
 (0)