[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/media/vendor/codemirror/addon/wrap/ -> hardwrap.js (source)

   1  // CodeMirror, copyright (c) by Marijn Haverbeke and others
   2  // Distributed under an MIT license: https://codemirror.net/5/LICENSE
   3  
   4  (function(mod) {
   5    if (typeof exports == "object" && typeof module == "object") // CommonJS
   6      mod(require("../../lib/codemirror"));
   7    else if (typeof define == "function" && define.amd) // AMD
   8      define(["../../lib/codemirror"], mod);
   9    else // Plain browser env
  10      mod(CodeMirror);
  11  })(function(CodeMirror) {
  12    "use strict";
  13  
  14    var Pos = CodeMirror.Pos;
  15  
  16    function findParagraph(cm, pos, options) {
  17      var startRE = options.paragraphStart || cm.getHelper(pos, "paragraphStart");
  18      for (var start = pos.line, first = cm.firstLine(); start > first; --start) {
  19        var line = cm.getLine(start);
  20        if (startRE && startRE.test(line)) break;
  21        if (!/\S/.test(line)) { ++start; break; }
  22      }
  23      var endRE = options.paragraphEnd || cm.getHelper(pos, "paragraphEnd");
  24      for (var end = pos.line + 1, last = cm.lastLine(); end <= last; ++end) {
  25        var line = cm.getLine(end);
  26        if (endRE && endRE.test(line)) { ++end; break; }
  27        if (!/\S/.test(line)) break;
  28      }
  29      return {from: start, to: end};
  30    }
  31  
  32    function findBreakPoint(text, column, wrapOn, killTrailingSpace, forceBreak) {
  33      var at = column
  34      while (at < text.length && text.charAt(at) == " ") at++
  35      for (; at > 0; --at)
  36        if (wrapOn.test(text.slice(at - 1, at + 1))) break;
  37  
  38      if (!forceBreak && at <= text.match(/^[ \t]*/)[0].length) {
  39        // didn't find a break point before column, in non-forceBreak mode try to
  40        // find one after 'column'.
  41        for (at = column + 1; at < text.length - 1; ++at) {
  42          if (wrapOn.test(text.slice(at - 1, at + 1))) break;
  43        }
  44      }
  45  
  46      for (var first = true;; first = false) {
  47        var endOfText = at;
  48        if (killTrailingSpace)
  49          while (text.charAt(endOfText - 1) == " ") --endOfText;
  50        if (endOfText == 0 && first) at = column;
  51        else return {from: endOfText, to: at};
  52      }
  53    }
  54  
  55    function wrapRange(cm, from, to, options) {
  56      from = cm.clipPos(from); to = cm.clipPos(to);
  57      var column = options.column || 80;
  58      var wrapOn = options.wrapOn || /\s\S|-[^\.\d]/;
  59      var forceBreak = options.forceBreak !== false;
  60      var killTrailing = options.killTrailingSpace !== false;
  61      var changes = [], curLine = "", curNo = from.line;
  62      var lines = cm.getRange(from, to, false);
  63      if (!lines.length) return null;
  64      var leadingSpace = lines[0].match(/^[ \t]*/)[0];
  65      if (leadingSpace.length >= column) column = leadingSpace.length + 1
  66  
  67      for (var i = 0; i < lines.length; ++i) {
  68        var text = lines[i], oldLen = curLine.length, spaceInserted = 0;
  69        if (curLine && text && !wrapOn.test(curLine.charAt(curLine.length - 1) + text.charAt(0))) {
  70          curLine += " ";
  71          spaceInserted = 1;
  72        }
  73        var spaceTrimmed = "";
  74        if (i) {
  75          spaceTrimmed = text.match(/^\s*/)[0];
  76          text = text.slice(spaceTrimmed.length);
  77        }
  78        curLine += text;
  79        if (i) {
  80          var firstBreak = curLine.length > column && leadingSpace == spaceTrimmed &&
  81            findBreakPoint(curLine, column, wrapOn, killTrailing, forceBreak);
  82          // If this isn't broken, or is broken at a different point, remove old break
  83          if (!firstBreak || firstBreak.from != oldLen || firstBreak.to != oldLen + spaceInserted) {
  84            changes.push({text: [spaceInserted ? " " : ""],
  85                          from: Pos(curNo, oldLen),
  86                          to: Pos(curNo + 1, spaceTrimmed.length)});
  87          } else {
  88            curLine = leadingSpace + text;
  89            ++curNo;
  90          }
  91        }
  92        while (curLine.length > column) {
  93          var bp = findBreakPoint(curLine, column, wrapOn, killTrailing, forceBreak);
  94          if (bp.from != bp.to ||
  95              forceBreak && leadingSpace !== curLine.slice(0, bp.to)) {
  96            changes.push({text: ["", leadingSpace],
  97                          from: Pos(curNo, bp.from),
  98                          to: Pos(curNo, bp.to)});
  99            curLine = leadingSpace + curLine.slice(bp.to);
 100            ++curNo;
 101          } else {
 102            break;
 103          }
 104        }
 105      }
 106      if (changes.length) cm.operation(function() {
 107        for (var i = 0; i < changes.length; ++i) {
 108          var change = changes[i];
 109          if (change.text || CodeMirror.cmpPos(change.from, change.to))
 110            cm.replaceRange(change.text, change.from, change.to);
 111        }
 112      });
 113      return changes.length ? {from: changes[0].from, to: CodeMirror.changeEnd(changes[changes.length - 1])} : null;
 114    }
 115  
 116    CodeMirror.defineExtension("wrapParagraph", function(pos, options) {
 117      options = options || {};
 118      if (!pos) pos = this.getCursor();
 119      var para = findParagraph(this, pos, options);
 120      return wrapRange(this, Pos(para.from, 0), Pos(para.to - 1), options);
 121    });
 122  
 123    CodeMirror.commands.wrapLines = function(cm) {
 124      cm.operation(function() {
 125        var ranges = cm.listSelections(), at = cm.lastLine() + 1;
 126        for (var i = ranges.length - 1; i >= 0; i--) {
 127          var range = ranges[i], span;
 128          if (range.empty()) {
 129            var para = findParagraph(cm, range.head, {});
 130            span = {from: Pos(para.from, 0), to: Pos(para.to - 1)};
 131          } else {
 132            span = {from: range.from(), to: range.to()};
 133          }
 134          if (span.to.line >= at) continue;
 135          at = span.from.line;
 136          wrapRange(cm, span.from, span.to, {});
 137        }
 138      });
 139    };
 140  
 141    CodeMirror.defineExtension("wrapRange", function(from, to, options) {
 142      return wrapRange(this, from, to, options || {});
 143    });
 144  
 145    CodeMirror.defineExtension("wrapParagraphsInRange", function(from, to, options) {
 146      options = options || {};
 147      var cm = this, paras = [];
 148      for (var line = from.line; line <= to.line;) {
 149        var para = findParagraph(cm, Pos(line, 0), options);
 150        paras.push(para);
 151        line = para.to;
 152      }
 153      var madeChange = false;
 154      if (paras.length) cm.operation(function() {
 155        for (var i = paras.length - 1; i >= 0; --i)
 156          madeChange = madeChange || wrapRange(cm, Pos(paras[i].from, 0), Pos(paras[i].to - 1), options);
 157      });
 158      return madeChange;
 159    });
 160  });


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer