Skip to content

Commit ee6b56d

Browse files
committed
Add support for readOnly: "nocursor"
1 parent 78ba450 commit ee6b56d

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

lib/codemirror.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ var CodeMirror = (function() {
9797
connect(wrapper, "dragenter", function(e){e.stop();});
9898
connect(wrapper, "dragover", function(e){e.stop();});
9999
connect(wrapper, "drop", operation(onDrop));
100-
connect(wrapper, "paste", function(){input.focus(); fastPoll();});
100+
connect(wrapper, "paste", function(){focusInput(); fastPoll();});
101101
connect(input, "paste", function(){fastPoll();});
102102
connect(input, "cut", function(){fastPoll();});
103103

@@ -115,11 +115,12 @@ var CodeMirror = (function() {
115115
setValue: operation(setValue),
116116
getSelection: getSelection,
117117
replaceSelection: operation(replaceSelection),
118-
focus: function(){input.focus(); onFocus(); fastPoll();},
118+
focus: function(){focusInput(); onFocus(); fastPoll();},
119119
setOption: function(option, value) {
120120
options[option] = value;
121121
if (option == "lineNumbers" || option == "gutter") gutterChanged();
122122
else if (option == "mode" || option == "indentUnit") loadMode();
123+
else if (option == "readOnly" && value == "nocursor") input.blur();
123124
},
124125
getOption: function(option) {return options[option];},
125126
undo: operation(undo),
@@ -223,7 +224,7 @@ var CodeMirror = (function() {
223224
// And then we have to see if it's a drag event, in which case
224225
// the dragged-over text must be selected.
225226
function end() {
226-
input.focus();
227+
focusInput();
227228
updateInput = true;
228229
move(); up();
229230
}
@@ -345,6 +346,7 @@ var CodeMirror = (function() {
345346
}
346347

347348
function onFocus() {
349+
if (options.readOnly == "nocursor") return;
348350
if (!focused && options.onFocus) options.onFocus(instance);
349351
focused = true;
350352
slowPoll();
@@ -614,6 +616,9 @@ var CodeMirror = (function() {
614616
editing = {text: text, from: from, to: to, start: startch, end: endch};
615617
setSelRange(input, startch, reducedSelection ? startch : endch);
616618
}
619+
function focusInput() {
620+
if (options.readOnly != "nocursor") input.focus();
621+
}
617622

618623
function scrollCursorIntoView() {
619624
var cursor = localCoords(sel.inverted ? sel.from : sel.to);
@@ -1127,7 +1132,7 @@ var CodeMirror = (function() {
11271132
"px; left: " + (e.pageX() - 1) + "px; z-index: 1000; background: white; " +
11281133
"border-width: 0; outline: none; overflow: hidden;";
11291134
var val = input.value = getSelection();
1130-
input.focus();
1135+
focusInput();
11311136
setSelRange(input, 0, input.value.length);
11321137
if (gecko) e.stop();
11331138
leaveInputAlone = true;

manual.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,9 @@ <h2 id="config">Configuration</h2>
192192

193193
<dt id="option_readOnly"><code>readOnly (boolean)</code></dt>
194194
<dd>This disables editing of the editor content by the user.
195-
(Changes through API functions will still be possible.)</dd>
195+
(Changes through API functions will still be possible.) If you
196+
also want to disable the cursor, use <code>"nocursor"</code> as
197+
a value for this option, instead of <code>true</code>.</dd>
196198

197199
<dt id="option_onChange"><code>onChange (function)</code></dt>
198200
<dd>When given, this function will be called every time the

0 commit comments

Comments
 (0)