adfaa62d1a619a496fe5803d106c85b41157f415
1 // CodeMirror, copyright (c) by Marijn Haverbeke and others
2 // Distributed under an MIT license: https://codemirror.net/LICENSE
4 // Because sometimes you need to mark the selected *text*.
6 // Adds an option 'styleSelectedText' which, when enabled, gives
7 // selected text the CSS class given as option value, or
8 // "CodeMirror-selectedtext" when the value is not a string.
11 if (typeof exports
== "object" && typeof module
== "object") // CommonJS
12 mod(require("../../lib/codemirror"));
13 else if (typeof define
== "function" && define
.amd
) // AMD
14 define(["../../lib/codemirror"], mod
);
15 else // Plain browser env
17 })(function(CodeMirror
) {
20 CodeMirror
.defineOption("styleSelectedText", false, function(cm
, val
, old
) {
21 var prev
= old
&& old
!= CodeMirror
.Init
;
23 cm
.state
.markedSelection
= [];
24 cm
.state
.markedSelectionStyle
= typeof val
== "string" ? val
: "CodeMirror-selectedtext";
26 cm
.on("cursorActivity", onCursorActivity
);
27 cm
.on("change", onChange
);
28 } else if (!val
&& prev
) {
29 cm
.off("cursorActivity", onCursorActivity
);
30 cm
.off("change", onChange
);
32 cm
.state
.markedSelection
= cm
.state
.markedSelectionStyle
= null;
36 function onCursorActivity(cm
) {
37 if (cm
.state
.markedSelection
)
38 cm
.operation(function() { update(cm
); });
41 function onChange(cm
) {
42 if (cm
.state
.markedSelection
&& cm
.state
.markedSelection
.length
)
43 cm
.operation(function() { clear(cm
); });
47 var Pos
= CodeMirror
.Pos
;
48 var cmp
= CodeMirror
.cmpPos
;
50 function coverRange(cm
, from, to
, addAt
) {
51 if (cmp(from, to
) == 0) return;
52 var array
= cm
.state
.markedSelection
;
53 var cls
= cm
.state
.markedSelectionStyle
;
54 for (var line
= from.line
;;) {
55 var start
= line
== from.line
? from : Pos(line
, 0);
56 var endLine
= line
+ CHUNK_SIZE
, atEnd
= endLine
>= to
.line
;
57 var end
= atEnd
? to
: Pos(endLine
, 0);
58 var mark
= cm
.markText(start
, end
, {className
: cls
});
59 if (addAt
== null) array
.push(mark
);
60 else array
.splice(addAt
++, 0, mark
);
67 var array
= cm
.state
.markedSelection
;
68 for (var i
= 0; i
< array
.length
; ++i
) array
[i
].clear();
74 var ranges
= cm
.listSelections();
75 for (var i
= 0; i
< ranges
.length
; i
++)
76 coverRange(cm
, ranges
[i
].from(), ranges
[i
].to());
80 if (!cm
.somethingSelected()) return clear(cm
);
81 if (cm
.listSelections().length
> 1) return reset(cm
);
83 var from = cm
.getCursor("start"), to
= cm
.getCursor("end");
85 var array
= cm
.state
.markedSelection
;
86 if (!array
.length
) return coverRange(cm
, from, to
);
88 var coverStart
= array
[0].find(), coverEnd
= array
[array
.length
- 1].find();
89 if (!coverStart
|| !coverEnd
|| to
.line
- from.line
<= CHUNK_SIZE
||
90 cmp(from, coverEnd
.to
) >= 0 || cmp(to
, coverStart
.from) <= 0)
93 while (cmp(from, coverStart
.from) > 0) {
94 array
.shift().clear();
95 coverStart
= array
[0].find();
97 if (cmp(from, coverStart
.from) < 0) {
98 if (coverStart
.to
.line
- from.line
< CHUNK_SIZE
) {
99 array
.shift().clear();
100 coverRange(cm
, from, coverStart
.to
, 0);
102 coverRange(cm
, from, coverStart
.from, 0);
106 while (cmp(to
, coverEnd
.to
) < 0) {
108 coverEnd
= array
[array
.length
- 1].find();
110 if (cmp(to
, coverEnd
.to
) > 0) {
111 if (to
.line
- coverEnd
.from.line
< CHUNK_SIZE
) {
113 coverRange(cm
, coverEnd
.from, to
);
115 coverRange(cm
, coverEnd
.to
, to
);