LaravelTest
159 строк · 4.9 Кб
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: https://codemirror.net/LICENSE
3
4(function(mod) {5if (typeof exports == "object" && typeof module == "object") // CommonJS6mod(require("../../lib/codemirror"));7else if (typeof define == "function" && define.amd) // AMD8define(["../../lib/codemirror"], mod);9else // Plain browser env10mod(CodeMirror);11})(function(CodeMirror) {12"use strict";13
14function doFold(cm, pos, options, force) {15if (options && options.call) {16var finder = options;17options = null;18} else {19var finder = getOption(cm, options, "rangeFinder");20}21if (typeof pos == "number") pos = CodeMirror.Pos(pos, 0);22var minSize = getOption(cm, options, "minFoldSize");23
24function getRange(allowFolded) {25var range = finder(cm, pos);26if (!range || range.to.line - range.from.line < minSize) return null;27if (force === "fold") return range;28
29var marks = cm.findMarksAt(range.from);30for (var i = 0; i < marks.length; ++i) {31if (marks[i].__isFold) {32if (!allowFolded) return null;33range.cleared = true;34marks[i].clear();35}36}37return range;38}39
40var range = getRange(true);41if (getOption(cm, options, "scanUp")) while (!range && pos.line > cm.firstLine()) {42pos = CodeMirror.Pos(pos.line - 1, 0);43range = getRange(false);44}45if (!range || range.cleared || force === "unfold") return;46
47var myWidget = makeWidget(cm, options, range);48CodeMirror.on(myWidget, "mousedown", function(e) {49myRange.clear();50CodeMirror.e_preventDefault(e);51});52var myRange = cm.markText(range.from, range.to, {53replacedWith: myWidget,54clearOnEnter: getOption(cm, options, "clearOnEnter"),55__isFold: true56});57myRange.on("clear", function(from, to) {58CodeMirror.signal(cm, "unfold", cm, from, to);59});60CodeMirror.signal(cm, "fold", cm, range.from, range.to);61}62
63function makeWidget(cm, options, range) {64var widget = getOption(cm, options, "widget");65
66if (typeof widget == "function") {67widget = widget(range.from, range.to);68}69
70if (typeof widget == "string") {71var text = document.createTextNode(widget);72widget = document.createElement("span");73widget.appendChild(text);74widget.className = "CodeMirror-foldmarker";75} else if (widget) {76widget = widget.cloneNode(true)77}78return widget;79}80
81// Clumsy backwards-compatible interface82CodeMirror.newFoldFunction = function(rangeFinder, widget) {83return function(cm, pos) { doFold(cm, pos, {rangeFinder: rangeFinder, widget: widget}); };84};85
86// New-style interface87CodeMirror.defineExtension("foldCode", function(pos, options, force) {88doFold(this, pos, options, force);89});90
91CodeMirror.defineExtension("isFolded", function(pos) {92var marks = this.findMarksAt(pos);93for (var i = 0; i < marks.length; ++i)94if (marks[i].__isFold) return true;95});96
97CodeMirror.commands.toggleFold = function(cm) {98cm.foldCode(cm.getCursor());99};100CodeMirror.commands.fold = function(cm) {101cm.foldCode(cm.getCursor(), null, "fold");102};103CodeMirror.commands.unfold = function(cm) {104cm.foldCode(cm.getCursor(), { scanUp: false }, "unfold");105};106CodeMirror.commands.foldAll = function(cm) {107cm.operation(function() {108for (var i = cm.firstLine(), e = cm.lastLine(); i <= e; i++)109cm.foldCode(CodeMirror.Pos(i, 0), { scanUp: false }, "fold");110});111};112CodeMirror.commands.unfoldAll = function(cm) {113cm.operation(function() {114for (var i = cm.firstLine(), e = cm.lastLine(); i <= e; i++)115cm.foldCode(CodeMirror.Pos(i, 0), { scanUp: false }, "unfold");116});117};118
119CodeMirror.registerHelper("fold", "combine", function() {120var funcs = Array.prototype.slice.call(arguments, 0);121return function(cm, start) {122for (var i = 0; i < funcs.length; ++i) {123var found = funcs[i](cm, start);124if (found) return found;125}126};127});128
129CodeMirror.registerHelper("fold", "auto", function(cm, start) {130var helpers = cm.getHelpers(start, "fold");131for (var i = 0; i < helpers.length; i++) {132var cur = helpers[i](cm, start);133if (cur) return cur;134}135});136
137var defaultOptions = {138rangeFinder: CodeMirror.fold.auto,139widget: "\u2194",140minFoldSize: 0,141scanUp: false,142clearOnEnter: true143};144
145CodeMirror.defineOption("foldOptions", null);146
147function getOption(cm, options, name) {148if (options && options[name] !== undefined)149return options[name];150var editorOptions = cm.options.foldOptions;151if (editorOptions && editorOptions[name] !== undefined)152return editorOptions[name];153return defaultOptions[name];154}155
156CodeMirror.defineExtension("foldOption", function(options, name) {157return getOption(this, options, name);158});159});160