LaravelTest
119 строк · 3.8 Кб
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: https://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) {11if (typeof exports == "object" && typeof module == "object") // CommonJS12mod(require("../../lib/codemirror"));13else if (typeof define == "function" && define.amd) // AMD14define(["../../lib/codemirror"], mod);15else // Plain browser env16mod(CodeMirror);17})(function(CodeMirror) {18"use strict";19
20CodeMirror.defineOption("styleSelectedText", false, function(cm, val, old) {21var prev = old && old != CodeMirror.Init;22if (val && !prev) {23cm.state.markedSelection = [];24cm.state.markedSelectionStyle = typeof val == "string" ? val : "CodeMirror-selectedtext";25reset(cm);26cm.on("cursorActivity", onCursorActivity);27cm.on("change", onChange);28} else if (!val && prev) {29cm.off("cursorActivity", onCursorActivity);30cm.off("change", onChange);31clear(cm);32cm.state.markedSelection = cm.state.markedSelectionStyle = null;33}34});35
36function onCursorActivity(cm) {37if (cm.state.markedSelection)38cm.operation(function() { update(cm); });39}40
41function onChange(cm) {42if (cm.state.markedSelection && cm.state.markedSelection.length)43cm.operation(function() { clear(cm); });44}45
46var CHUNK_SIZE = 8;47var Pos = CodeMirror.Pos;48var cmp = CodeMirror.cmpPos;49
50function coverRange(cm, from, to, addAt) {51if (cmp(from, to) == 0) return;52var array = cm.state.markedSelection;53var cls = cm.state.markedSelectionStyle;54for (var line = from.line;;) {55var start = line == from.line ? from : Pos(line, 0);56var endLine = line + CHUNK_SIZE, atEnd = endLine >= to.line;57var end = atEnd ? to : Pos(endLine, 0);58var mark = cm.markText(start, end, {className: cls});59if (addAt == null) array.push(mark);60else array.splice(addAt++, 0, mark);61if (atEnd) break;62line = endLine;63}64}65
66function clear(cm) {67var array = cm.state.markedSelection;68for (var i = 0; i < array.length; ++i) array[i].clear();69array.length = 0;70}71
72function reset(cm) {73clear(cm);74var ranges = cm.listSelections();75for (var i = 0; i < ranges.length; i++)76coverRange(cm, ranges[i].from(), ranges[i].to());77}78
79function update(cm) {80if (!cm.somethingSelected()) return clear(cm);81if (cm.listSelections().length > 1) return reset(cm);82
83var from = cm.getCursor("start"), to = cm.getCursor("end");84
85var array = cm.state.markedSelection;86if (!array.length) return coverRange(cm, from, to);87
88var coverStart = array[0].find(), coverEnd = array[array.length - 1].find();89if (!coverStart || !coverEnd || to.line - from.line <= CHUNK_SIZE ||90cmp(from, coverEnd.to) >= 0 || cmp(to, coverStart.from) <= 0)91return reset(cm);92
93while (cmp(from, coverStart.from) > 0) {94array.shift().clear();95coverStart = array[0].find();96}97if (cmp(from, coverStart.from) < 0) {98if (coverStart.to.line - from.line < CHUNK_SIZE) {99array.shift().clear();100coverRange(cm, from, coverStart.to, 0);101} else {102coverRange(cm, from, coverStart.from, 0);103}104}105
106while (cmp(to, coverEnd.to) < 0) {107array.pop().clear();108coverEnd = array[array.length - 1].find();109}110if (cmp(to, coverEnd.to) > 0) {111if (to.line - coverEnd.from.line < CHUNK_SIZE) {112array.pop().clear();113coverRange(cm, coverEnd.from, to);114} else {115coverRange(cm, coverEnd.to, to);116}117}118}119});120