Discussion:
setting readOnly after CodeMirror has started
Julian Todd
2010-11-04 20:55:08 UTC
Permalink
I want to change to readOnly after the window has started up and been used.

There is no setReadOnly() function available in the manual.
http://codemirror.net/manual.html

Is CodeMirrorConfig.readOnly = true supposed to work (because it
doesn't seem to)?

Julian.
Marijn Haverbeke
2010-11-04 21:29:31 UTC
Permalink
Post by Julian Todd
I want to change to readOnly after the window has started up and been used.
You can't, for browser-support reasons. Many browsers will completely
reset (clear) the document when turning designMode on or off, and kill
the scripts inside of it. Thus, CodeMirror would have to re-initialize
itself to support this. You might as well re-initialize CodeMirror
from your code.

(If you like pain, you can experiment with a toggle that intercepts
all keyboard and mouse events and stops them, and reacts to focus
events by unfocusing the editor, somehow. That would allow a more
'light-weight' read-only mode.)

Best,
Marijn
Julian Todd
2010-11-04 23:59:59 UTC
Permalink
How counter-intuitive. You'd think it was such a trivial property.

I have one solution involving overlaying the whole window with a
translucent div image that just blocks everything and stops you
scrolling.

I guess another way is to call undo() in the onChange function every
time the poor sucker types anything!

Do you know how to change the background colour of the codemirror
frame to a dark shade of grey to show it's in a different state?

This doesn't work:
codemirroriframe.style.backgroundColor = 'red'
But this does something:
codemirroriframe.style.border = 'thick red solid'

Julian.
Post by Marijn Haverbeke
Post by Julian Todd
I want to change to readOnly after the window has started up and been used.
You can't, for browser-support reasons. Many browsers will completely
reset (clear) the document when turning designMode on or off, and kill
the scripts inside of it. Thus, CodeMirror would have to re-initialize
itself to support this. You might as well re-initialize CodeMirror
from your code.
(If you like pain, you can experiment with a toggle that intercepts
all keyboard and mouse events and stops them, and reacts to focus
events by unfocusing the editor, somehow. That would allow a more
'light-weight' read-only mode.)
Best,
Marijn
--
Marijn Haverbeke
2010-11-05 07:16:11 UTC
Permalink
Post by Julian Todd
I have one solution involving overlaying the whole window with a
translucent div image that just blocks everything and stops you
scrolling.
Ah, right, scrolling and selection should still work.
Post by Julian Todd
I guess another way is to call undo() in the onChange function every
time the poor sucker types anything!
That'll be confusing -- you can see your changes for a few
milliseconds, and then they disappear again.
Post by Julian Todd
Do you know how to change the background colour of the codemirror
frame to a dark shade of grey to show it's in a different state?
Use instance.win.document.body.style .

Best,
Marijn
Julian Todd
2010-11-05 10:52:52 UTC
Permalink
Post by Marijn Haverbeke
Post by Julian Todd
I have one solution involving overlaying the whole window with a
translucent div image that just blocks everything and stops you
scrolling.
Ah, right, scrolling and selection should still work.
Not in my horrible implementation. I've decided this isn't the way to go.
Post by Marijn Haverbeke
Post by Julian Todd
I guess another way is to call undo() in the onChange function every
time the poor sucker types anything!
That'll be confusing -- you can see your changes for a few
milliseconds, and then they disappear again.
It's not friendly, but it would discourage them pretty fast. I'm
thinking of doing something more along the lines of watching that undo
stack value, so if the user does make any changes, the code frame no
longer automatically reloads. It's good to have a lot of options to
choose from.
Post by Marijn Haverbeke
Post by Julian Todd
Do you know how to change the background colour of the codemirror
frame to a dark shade of grey to show it's in a different state?
Use instance.win.document.body.style .
Did the job. Also was able to change the background image to
something more striking and obviously different. People can get used
to shades of grey pretty fast that chances are they wouldn't notice.

Julian.
MtnViewMark
2010-11-05 13:55:18 UTC
Permalink
Funny, I was wanting something similar, too. For my part, in "light
weight" read-only mode I want the whole editor to work as normal
except for modifying the content. So scrolling, selecting, copying,
brace-hiliting, etc. would all work.

Ideally, in this state, on first modification operation, there would
be a call back. If it returned true, then the editor goes into normal
mode, and completes the edit (so the user doesn't have to type or
paste again). If the call back returns false, the edit is abandoned
and the editor stays in is mode.

I, too, want to change the look on first change (though in my case, I
change the class of the CodeMirror-Wrapper div). I guessing that at
undo commit time might be too late from a UX point of view.
山田和弘
2014-01-22 09:52:24 UTC
Permalink
editor = CodeMirror.fromTextArea(document.getElementById("code"), {
lineNumbers : true
matchBrackets : true
tabSize : 2
indentUnit : 2
indentWithTabs : false
mode : "text/x-java"
})

# set readOnly mode
editor.setOption("readOnly", true)

# clear readOnly mode
editor.setOption("readOnly", false)
Post by Julian Todd
I want to change to readOnly after the window has started up and been used.
There is no setReadOnly() function available in the manual.
http://codemirror.net/manual.html
Is CodeMirrorConfig.readOnly = true supposed to work (because it
doesn't seem to)?
Julian.
--
--
You are receiving this because you are currently a member of the CodeMirror Google group. To send something to list, use codemirror-/***@public.gmane.org, to unsubscribe, send a message to codemirror-unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
---
You received this message because you are subscribed to the Google Groups "CodeMirror" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codemirror+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Marijn Haverbeke
2014-01-22 10:11:35 UTC
Permalink
This thread was about a pre-2.0 version of CodeMirror. Since 2.0, it
is indeed trivially possible to change the readOnly setting at
run-time.
Post by 山田和弘
editor = CodeMirror.fromTextArea(document.getElementById("code"), {
lineNumbers : true
matchBrackets : true
tabSize : 2
indentUnit : 2
indentWithTabs : false
mode : "text/x-java"
})
# set readOnly mode
editor.setOption("readOnly", true)
# clear readOnly mode
editor.setOption("readOnly", false)
Post by Julian Todd
I want to change to readOnly after the window has started up and been used.
There is no setReadOnly() function available in the manual.
http://codemirror.net/manual.html
Is CodeMirrorConfig.readOnly = true supposed to work (because it
doesn't seem to)?
Julian.
--
--
You are receiving this because you are currently a member of the CodeMirror
---
You received this message because you are subscribed to the Google Groups
"CodeMirror" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/groups/opt_out.
--
--
You are receiving this because you are currently a member of the CodeMirror Google group. To send something to list, use codemirror-/***@public.gmane.org, to unsubscribe, send a message to codemirror-unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
---
You received this message because you are subscribed to the Google Groups "CodeMirror" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codemirror+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Loading...