LaravelTest

Форк
0
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) {
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
});
120

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.