Mercurial
comparison .cms/lib/codemirror/addon/selection/mark-selection.js @ 0:78edf6b517a0 draft
24.10
author | Coffee CMS <info@coffee-cms.ru> |
---|---|
date | Fri, 11 Oct 2024 22:40:23 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:78edf6b517a0 |
---|---|
1 // CodeMirror, copyright (c) by Marijn Haverbeke and others | |
2 // Distributed under an MIT license: https://codemirror.net/5/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 if (cm.state.markedSelection) | |
38 cm.operation(function() { update(cm); }); | |
39 } | |
40 | |
41 function onChange(cm) { | |
42 if (cm.state.markedSelection && cm.state.markedSelection.length) | |
43 cm.operation(function() { clear(cm); }); | |
44 } | |
45 | |
46 var CHUNK_SIZE = 8; | |
47 var Pos = CodeMirror.Pos; | |
48 var cmp = CodeMirror.cmpPos; | |
49 | |
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); | |
61 if (atEnd) break; | |
62 line = endLine; | |
63 } | |
64 } | |
65 | |
66 function clear(cm) { | |
67 var array = cm.state.markedSelection; | |
68 for (var i = 0; i < array.length; ++i) array[i].clear(); | |
69 array.length = 0; | |
70 } | |
71 | |
72 function reset(cm) { | |
73 clear(cm); | |
74 var ranges = cm.listSelections(); | |
75 for (var i = 0; i < ranges.length; i++) | |
76 coverRange(cm, ranges[i].from(), ranges[i].to()); | |
77 } | |
78 | |
79 function update(cm) { | |
80 if (!cm.somethingSelected()) return clear(cm); | |
81 if (cm.listSelections().length > 1) return reset(cm); | |
82 | |
83 var from = cm.getCursor("start"), to = cm.getCursor("end"); | |
84 | |
85 var array = cm.state.markedSelection; | |
86 if (!array.length) return coverRange(cm, from, to); | |
87 | |
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) | |
91 return reset(cm); | |
92 | |
93 while (cmp(from, coverStart.from) > 0) { | |
94 array.shift().clear(); | |
95 coverStart = array[0].find(); | |
96 } | |
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); | |
101 } else { | |
102 coverRange(cm, from, coverStart.from, 0); | |
103 } | |
104 } | |
105 | |
106 while (cmp(to, coverEnd.to) < 0) { | |
107 array.pop().clear(); | |
108 coverEnd = array[array.length - 1].find(); | |
109 } | |
110 if (cmp(to, coverEnd.to) > 0) { | |
111 if (to.line - coverEnd.from.line < CHUNK_SIZE) { | |
112 array.pop().clear(); | |
113 coverRange(cm, coverEnd.from, to); | |
114 } else { | |
115 coverRange(cm, coverEnd.to, to); | |
116 } | |
117 } | |
118 } | |
119 }); |