Path: blob/master/web-gui/buildyourownbotnet/assets/js/codemirror/addon/selection/mark-selection.js
1294 views
// Because sometimes you need to mark the selected *text*.1//2// Adds an option 'styleSelectedText' which, when enabled, gives3// selected text the CSS class given as option value, or4// "CodeMirror-selectedtext" when the value is not a string.56(function() {7"use strict";89CodeMirror.defineOption("styleSelectedText", false, function(cm, val, old) {10var prev = old && old != CodeMirror.Init;11if (val && !prev) {12cm.state.markedSelection = [];13cm.state.markedSelectionStyle = typeof val == "string" ? val : "CodeMirror-selectedtext";14reset(cm);15cm.on("cursorActivity", onCursorActivity);16cm.on("change", onChange);17} else if (!val && prev) {18cm.off("cursorActivity", onCursorActivity);19cm.off("change", onChange);20clear(cm);21cm.state.markedSelection = cm.state.markedSelectionStyle = null;22}23});2425function onCursorActivity(cm) {26cm.operation(function() { update(cm); });27}2829function onChange(cm) {30if (cm.state.markedSelection.length)31cm.operation(function() { clear(cm); });32}3334var CHUNK_SIZE = 8;35var Pos = CodeMirror.Pos;3637function cmp(pos1, pos2) {38return pos1.line - pos2.line || pos1.ch - pos2.ch;39}4041function coverRange(cm, from, to, addAt) {42if (cmp(from, to) == 0) return;43var array = cm.state.markedSelection;44var cls = cm.state.markedSelectionStyle;45for (var line = from.line;;) {46var start = line == from.line ? from : Pos(line, 0);47var endLine = line + CHUNK_SIZE, atEnd = endLine >= to.line;48var end = atEnd ? to : Pos(endLine, 0);49var mark = cm.markText(start, end, {className: cls});50if (addAt == null) array.push(mark);51else array.splice(addAt++, 0, mark);52if (atEnd) break;53line = endLine;54}55}5657function clear(cm) {58var array = cm.state.markedSelection;59for (var i = 0; i < array.length; ++i) array[i].clear();60array.length = 0;61}6263function reset(cm) {64clear(cm);65var from = cm.getCursor("start"), to = cm.getCursor("end");66coverRange(cm, from, to);67}6869function update(cm) {70var from = cm.getCursor("start"), to = cm.getCursor("end");71if (cmp(from, to) == 0) return clear(cm);7273var array = cm.state.markedSelection;74if (!array.length) return coverRange(cm, from, to);7576var coverStart = array[0].find(), coverEnd = array[array.length - 1].find();77if (!coverStart || !coverEnd || to.line - from.line < CHUNK_SIZE ||78cmp(from, coverEnd.to) >= 0 || cmp(to, coverStart.from) <= 0)79return reset(cm);8081while (cmp(from, coverStart.from) > 0) {82array.shift().clear();83coverStart = array[0].find();84}85if (cmp(from, coverStart.from) < 0) {86if (coverStart.to.line - from.line < CHUNK_SIZE) {87array.shift().clear();88coverRange(cm, from, coverStart.to, 0);89} else {90coverRange(cm, from, coverStart.from, 0);91}92}9394while (cmp(to, coverEnd.to) < 0) {95array.pop().clear();96coverEnd = array[array.length - 1].find();97}98if (cmp(to, coverEnd.to) > 0) {99if (to.line - coverEnd.from.line < CHUNK_SIZE) {100array.pop().clear();101coverRange(cm, coverEnd.from, to);102} else {103coverRange(cm, coverEnd.to, to);104}105}106}107})();108109110