Skip to content

Commit cf15c70

Browse files
committed
Add example of using markselection to style background to demo
1 parent f09b6c5 commit cf15c70

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

demo/markselection.html

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
.CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;}
1313
.CodeMirror-selected { background-color: blue !important; }
1414
.CodeMirror-selectedtext { color: white; }
15+
.styled-background { background-color: #ff7; }
1516
</style>
1617
<div id=nav>
1718
<a href="http://codemirror.net"><img id=logo src="../doc/logo.png"></a>
@@ -28,16 +29,22 @@
2829

2930
<article>
3031
<h2>Selection Marking Demo</h2>
31-
<form><textarea id="code" name="code">Select something from here.
32-
You'll see that the selection's foreground color changes to white!
33-
Since, by default, CodeMirror only puts an independent "marker" layer
34-
behind the text, you'll need something like this to change its colour.</textarea></form>
32+
<form><textarea id="code" name="code">
33+
Select something from here. You'll see that the selection's foreground
34+
color changes to white! Since, by default, CodeMirror only puts an
35+
independent "marker" layer behind the text, you'll need something like
36+
this to change its colour.
37+
38+
Also notice that turning this addon on (with the default style) allows
39+
you to safely give text a background color without screwing up the
40+
visibility of the selection.</textarea></form>
3541

3642
<script>
3743
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
3844
lineNumbers: true,
3945
styleSelectedText: true
4046
});
47+
editor.markText({line: 6, ch: 26}, {line: 6, ch: 42}, {className: "styled-background"});
4148
</script>
4249

4350
<p>Simple addon to easily mark (and style) selected text.</p>

lib/codemirror.css

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@
5757
border: 0;
5858
background: #7e7;
5959
}
60+
.CodeMirror.cm-keymap-fat-cursor div.CodeMirror-cursors {
61+
z-index: 1;
62+
}
63+
6064
.cm-animate-fat-cursor {
6165
width: auto;
6266
border: 0;
@@ -273,7 +277,7 @@ div.CodeMirror span.CodeMirror-nonmatchingbracket {color: #f22;}
273277
div.CodeMirror-cursors {
274278
visibility: hidden;
275279
position: relative;
276-
z-index: 1;
280+
z-index: 3;
277281
}
278282
.CodeMirror-focused div.CodeMirror-cursors {
279283
visibility: visible;
@@ -300,3 +304,6 @@ div.CodeMirror-cursors {
300304
visibility: hidden;
301305
}
302306
}
307+
308+
/* Help users use markselection to safely style text background */
309+
span.CodeMirror-selectedtext { background: none; }

0 commit comments

Comments
 (0)