Browse Source

small changes

cl-refactor
yann300 10 years ago
parent
commit
c105714c63
  1. 4
      mix/qml/html/cm/codemirror.css
  2. 144
      mix/qml/html/cm/mark-selection.js
  3. 1
      mix/qml/html/cm/solarized.css

4
mix/qml/html/cm/codemirror.css

@ -311,5 +311,5 @@ div.CodeMirror-cursors {
/* See issue #2901 */ /* See issue #2901 */
.cm-tab-wrap-hack:after { content: ''; } .cm-tab-wrap-hack:after { content: ''; }
/* Help users use markselection to safely style text background */ /* Help users use markselection to safely style text background
span.CodeMirror-selectedtext { color: #586e75; } span.CodeMirror-selectedtext { color: #586e75; } */

144
mix/qml/html/cm/mark-selection.js

@ -9,37 +9,37 @@
(function(mod) { (function(mod) {
if (typeof exports == "object" && typeof module == "object") // CommonJS if (typeof exports == "object" && typeof module == "object") // CommonJS
mod(require("../../lib/codemirror")); mod(require("../../lib/codemirror"));
else if (typeof define == "function" && define.amd) // AMD else if (typeof define == "function" && define.amd) // AMD
define(["../../lib/codemirror"], mod); define(["../../lib/codemirror"], mod);
else // Plain browser env else // Plain browser env
mod(CodeMirror); mod(CodeMirror);
})(function(CodeMirror) { })(function(CodeMirror) {
"use strict"; "use strict";
CodeMirror.defineOption("styleSelectedText", false, function(cm, val, old) { CodeMirror.defineOption("styleSelectedText", false, function(cm, val, old) {
var prev = old && old != CodeMirror.Init; var prev = old && old != CodeMirror.Init;
if (val && !prev) { if (val && !prev) {
cm.state.markedSelection = []; cm.state.markedSelection = [];
cm.state.markedSelectionStyle = typeof val == "string" ? val : "CodeMirror-selectedtext"; cm.state.markedSelectionStyle = typeof val == "string" ? val : "CodeMirror-selectedtext";
reset(cm); reset(cm);
cm.on("cursorActivity", onCursorActivity); cm.on("cursorActivity", onCursorActivity);
cm.on("change", onChange); cm.on("change", onChange);
} else if (!val && prev) { } else if (!val && prev) {
cm.off("cursorActivity", onCursorActivity); cm.off("cursorActivity", onCursorActivity);
cm.off("change", onChange); cm.off("change", onChange);
clear(cm); clear(cm);
cm.state.markedSelection = cm.state.markedSelectionStyle = null; cm.state.markedSelection = cm.state.markedSelectionStyle = null;
} }
}); });
function onCursorActivity(cm) { function onCursorActivity(cm) {
cm.operation(function() { update(cm); }); cm.operation(function() { update(cm); });
} }
function onChange(cm) { function onChange(cm) {
if (cm.state.markedSelection.length) if (cm.state.markedSelection.length)
cm.operation(function() { clear(cm); }); cm.operation(function() { clear(cm); });
} }
var CHUNK_SIZE = 8; var CHUNK_SIZE = 8;
@ -47,72 +47,72 @@
var cmp = CodeMirror.cmpPos; var cmp = CodeMirror.cmpPos;
function coverRange(cm, from, to, addAt) { function coverRange(cm, from, to, addAt) {
if (cmp(from, to) == 0) return; if (cmp(from, to) == 0) return;
var array = cm.state.markedSelection; var array = cm.state.markedSelection;
var cls = cm.state.markedSelectionStyle; var cls = cm.state.markedSelectionStyle;
for (var line = from.line;;) { for (var line = from.line;;) {
var start = line == from.line ? from : Pos(line, 0); var start = line == from.line ? from : Pos(line, 0);
var endLine = line + CHUNK_SIZE, atEnd = endLine >= to.line; var endLine = line + CHUNK_SIZE, atEnd = endLine >= to.line;
var end = atEnd ? to : Pos(endLine, 0); var end = atEnd ? to : Pos(endLine, 0);
var mark = cm.markText(start, end, {className: cls}); var mark = cm.markText(start, end, {className: cls});
if (addAt == null) array.push(mark); if (addAt == null) array.push(mark);
else array.splice(addAt++, 0, mark); else array.splice(addAt++, 0, mark);
if (atEnd) break; if (atEnd) break;
line = endLine; line = endLine;
} }
} }
function clear(cm) { function clear(cm) {
var array = cm.state.markedSelection; var array = cm.state.markedSelection;
for (var i = 0; i < array.length; ++i) array[i].clear(); for (var i = 0; i < array.length; ++i) array[i].clear();
array.length = 0; array.length = 0;
} }
function reset(cm) { function reset(cm) {
clear(cm); clear(cm);
var ranges = cm.listSelections(); var ranges = cm.listSelections();
for (var i = 0; i < ranges.length; i++) for (var i = 0; i < ranges.length; i++)
coverRange(cm, ranges[i].from(), ranges[i].to()); coverRange(cm, ranges[i].from(), ranges[i].to());
} }
function update(cm) { function update(cm) {
if (!cm.somethingSelected()) return clear(cm); if (!cm.somethingSelected()) return clear(cm);
if (cm.listSelections().length > 1) return reset(cm); if (cm.listSelections().length > 1) return reset(cm);
var from = cm.getCursor("start"), to = cm.getCursor("end"); var from = cm.getCursor("start"), to = cm.getCursor("end");
var array = cm.state.markedSelection; var array = cm.state.markedSelection;
if (!array.length) return coverRange(cm, from, to); if (!array.length) return coverRange(cm, from, to);
var coverStart = array[0].find(), coverEnd = array[array.length - 1].find(); var coverStart = array[0].find(), coverEnd = array[array.length - 1].find();
if (!coverStart || !coverEnd || to.line - from.line < CHUNK_SIZE || if (!coverStart || !coverEnd || to.line - from.line < CHUNK_SIZE ||
cmp(from, coverEnd.to) >= 0 || cmp(to, coverStart.from) <= 0) cmp(from, coverEnd.to) >= 0 || cmp(to, coverStart.from) <= 0)
return reset(cm); return reset(cm);
while (cmp(from, coverStart.from) > 0) { while (cmp(from, coverStart.from) > 0) {
array.shift().clear(); array.shift().clear();
coverStart = array[0].find(); coverStart = array[0].find();
} }
if (cmp(from, coverStart.from) < 0) { if (cmp(from, coverStart.from) < 0) {
if (coverStart.to.line - from.line < CHUNK_SIZE) { if (coverStart.to.line - from.line < CHUNK_SIZE) {
array.shift().clear(); array.shift().clear();
coverRange(cm, from, coverStart.to, 0); coverRange(cm, from, coverStart.to, 0);
} else { } else {
coverRange(cm, from, coverStart.from, 0); coverRange(cm, from, coverStart.from, 0);
} }
} }
while (cmp(to, coverEnd.to) < 0) { while (cmp(to, coverEnd.to) < 0) {
array.pop().clear(); array.pop().clear();
coverEnd = array[array.length - 1].find(); coverEnd = array[array.length - 1].find();
} }
if (cmp(to, coverEnd.to) > 0) { if (cmp(to, coverEnd.to) > 0) {
if (to.line - coverEnd.from.line < CHUNK_SIZE) { if (to.line - coverEnd.from.line < CHUNK_SIZE) {
array.pop().clear(); array.pop().clear();
coverRange(cm, coverEnd.from, to); coverRange(cm, coverEnd.from, to);
} else { } else {
coverRange(cm, coverEnd.to, to); coverRange(cm, coverEnd.to, to);
} }
} }
} }
}); });

1
mix/qml/html/cm/solarized.css

@ -183,4 +183,3 @@ view-port
} }
span.CodeMirror-selectedtext { color: #586e75 !important; } span.CodeMirror-selectedtext { color: #586e75 !important; }

Loading…
Cancel
Save