Mercurial > hg > rc1
comparison plugins/managesieve/codemirror/addon/selection/mark-selection.js @ 0:1e000243b222
vanilla 1.3.3 distro, I hope
author | Charlie Root |
---|---|
date | Thu, 04 Jan 2018 15:50:29 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:1e000243b222 |
---|---|
1 // CodeMirror, copyright (c) by Marijn Haverbeke and others | |
2 // Distributed under an MIT license: http://codemirror.net/LICENSE | |
3 | |
4 // Because sometimes you need to mark the selected *text*. | |
5 // | |
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. | |
9 | |
10 (function(mod) { | |
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 | |
16 mod(CodeMirror); | |
17 })(function(CodeMirror) { | |
18 "use strict"; | |
19 | |
20 CodeMirror.defineOption("styleSelectedText", false, function(cm, val, old) { | |
21 var prev = old && old != CodeMirror.Init; | |
22 if (val && !prev) { | |
23 cm.state.markedSelection = []; | |
24 cm.state.markedSelectionStyle = typeof val == "string" ? val : "CodeMirror-selectedtext"; | |
25 reset(cm); | |
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); | |
31 clear(cm); | |
32 cm.state.markedSelection = cm.state.markedSelectionStyle = null; | |
33 } | |
34 }); | |
35 | |
36 function onCursorActivity(cm) { | |
37 cm.operation(function() { update(cm); }); | |
38 } | |
39 | |
40 function onChange(cm) { | |
41 if (cm.state.markedSelection.length) | |
42 cm.operation(function() { clear(cm); }); | |
43 } | |
44 | |
45 var CHUNK_SIZE = 8; | |
46 var Pos = CodeMirror.Pos; | |
47 var cmp = CodeMirror.cmpPos; | |
48 | |
49 function coverRange(cm, from, to, addAt) { | |
50 if (cmp(from, to) == 0) return; | |
51 var array = cm.state.markedSelection; | |
52 var cls = cm.state.markedSelectionStyle; | |
53 for (var line = from.line;;) { | |
54 var start = line == from.line ? from : Pos(line, 0); | |
55 var endLine = line + CHUNK_SIZE, atEnd = endLine >= to.line; | |
56 var end = atEnd ? to : Pos(endLine, 0); | |
57 var mark = cm.markText(start, end, {className: cls}); | |
58 if (addAt == null) array.push(mark); | |
59 else array.splice(addAt++, 0, mark); | |
60 if (atEnd) break; | |
61 line = endLine; | |
62 } | |
63 } | |
64 | |
65 function clear(cm) { | |
66 var array = cm.state.markedSelection; | |
67 for (var i = 0; i < array.length; ++i) array[i].clear(); | |
68 array.length = 0; | |
69 } | |
70 | |
71 function reset(cm) { | |
72 clear(cm); | |
73 var ranges = cm.listSelections(); | |
74 for (var i = 0; i < ranges.length; i++) | |
75 coverRange(cm, ranges[i].from(), ranges[i].to()); | |
76 } | |
77 | |
78 function update(cm) { | |
79 if (!cm.somethingSelected()) return clear(cm); | |
80 if (cm.listSelections().length > 1) return reset(cm); | |
81 | |
82 var from = cm.getCursor("start"), to = cm.getCursor("end"); | |
83 | |
84 var array = cm.state.markedSelection; | |
85 if (!array.length) return coverRange(cm, from, to); | |
86 | |
87 var coverStart = array[0].find(), coverEnd = array[array.length - 1].find(); | |
88 if (!coverStart || !coverEnd || to.line - from.line < CHUNK_SIZE || | |
89 cmp(from, coverEnd.to) >= 0 || cmp(to, coverStart.from) <= 0) | |
90 return reset(cm); | |
91 | |
92 while (cmp(from, coverStart.from) > 0) { | |
93 array.shift().clear(); | |
94 coverStart = array[0].find(); | |
95 } | |
96 if (cmp(from, coverStart.from) < 0) { | |
97 if (coverStart.to.line - from.line < CHUNK_SIZE) { | |
98 array.shift().clear(); | |
99 coverRange(cm, from, coverStart.to, 0); | |
100 } else { | |
101 coverRange(cm, from, coverStart.from, 0); | |
102 } | |
103 } | |
104 | |
105 while (cmp(to, coverEnd.to) < 0) { | |
106 array.pop().clear(); | |
107 coverEnd = array[array.length - 1].find(); | |
108 } | |
109 if (cmp(to, coverEnd.to) > 0) { | |
110 if (to.line - coverEnd.from.line < CHUNK_SIZE) { | |
111 array.pop().clear(); | |
112 coverRange(cm, coverEnd.from, to); | |
113 } else { | |
114 coverRange(cm, coverEnd.to, to); | |
115 } | |
116 } | |
117 } | |
118 }); |