Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

foldFlowLines.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = foldFlowLines;
  6. exports.FOLD_QUOTED = exports.FOLD_BLOCK = exports.FOLD_FLOW = void 0;
  7. var FOLD_FLOW = 'flow';
  8. exports.FOLD_FLOW = FOLD_FLOW;
  9. var FOLD_BLOCK = 'block';
  10. exports.FOLD_BLOCK = FOLD_BLOCK;
  11. var FOLD_QUOTED = 'quoted'; // presumes i+1 is at the start of a line
  12. // returns index of last newline in more-indented block
  13. exports.FOLD_QUOTED = FOLD_QUOTED;
  14. var consumeMoreIndentedLines = function consumeMoreIndentedLines(text, i) {
  15. var ch = text[i + 1];
  16. while (ch === ' ' || ch === '\t') {
  17. do {
  18. ch = text[i += 1];
  19. } while (ch && ch !== '\n');
  20. ch = text[i + 1];
  21. }
  22. return i;
  23. };
  24. /**
  25. * Tries to keep input at up to `lineWidth` characters, splitting only on spaces
  26. * not followed by newlines or spaces unless `mode` is `'quoted'`. Lines are
  27. * terminated with `\n` and started with `indent`.
  28. *
  29. * @param {string} text
  30. * @param {string} indent
  31. * @param {string} [mode='flow'] `'block'` prevents more-indented lines
  32. * from being folded; `'quoted'` allows for `\` escapes, including escaped
  33. * newlines
  34. * @param {Object} options
  35. * @param {number} [options.indentAtStart] Accounts for leading contents on
  36. * the first line, defaulting to `indent.length`
  37. * @param {number} [options.lineWidth=80]
  38. * @param {number} [options.minContentWidth=20] Allow highly indented lines to
  39. * stretch the line width
  40. * @param {function} options.onFold Called once if the text is folded
  41. * @param {function} options.onFold Called once if any line of text exceeds
  42. * lineWidth characters
  43. */
  44. function foldFlowLines(text, indent, mode, _ref) {
  45. var indentAtStart = _ref.indentAtStart,
  46. _ref$lineWidth = _ref.lineWidth,
  47. lineWidth = _ref$lineWidth === void 0 ? 80 : _ref$lineWidth,
  48. _ref$minContentWidth = _ref.minContentWidth,
  49. minContentWidth = _ref$minContentWidth === void 0 ? 20 : _ref$minContentWidth,
  50. onFold = _ref.onFold,
  51. onOverflow = _ref.onOverflow;
  52. if (!lineWidth || lineWidth < 0) return text;
  53. var endStep = Math.max(1 + minContentWidth, 1 + lineWidth - indent.length);
  54. if (text.length <= endStep) return text;
  55. var folds = [];
  56. var escapedFolds = {};
  57. var end = lineWidth - (typeof indentAtStart === 'number' ? indentAtStart : indent.length);
  58. var split = undefined;
  59. var prev = undefined;
  60. var overflow = false;
  61. var i = -1;
  62. if (mode === FOLD_BLOCK) {
  63. i = consumeMoreIndentedLines(text, i);
  64. if (i !== -1) end = i + endStep;
  65. }
  66. for (var ch; ch = text[i += 1];) {
  67. if (mode === FOLD_QUOTED && ch === '\\') {
  68. switch (text[i + 1]) {
  69. case 'x':
  70. i += 3;
  71. break;
  72. case 'u':
  73. i += 5;
  74. break;
  75. case 'U':
  76. i += 9;
  77. break;
  78. default:
  79. i += 1;
  80. }
  81. }
  82. if (ch === '\n') {
  83. if (mode === FOLD_BLOCK) i = consumeMoreIndentedLines(text, i);
  84. end = i + endStep;
  85. split = undefined;
  86. } else {
  87. if (ch === ' ' && prev && prev !== ' ' && prev !== '\n' && prev !== '\t') {
  88. // space surrounded by non-space can be replaced with newline + indent
  89. var next = text[i + 1];
  90. if (next && next !== ' ' && next !== '\n' && next !== '\t') split = i;
  91. }
  92. if (i >= end) {
  93. if (split) {
  94. folds.push(split);
  95. end = split + endStep;
  96. split = undefined;
  97. } else if (mode === FOLD_QUOTED) {
  98. // white-space collected at end may stretch past lineWidth
  99. while (prev === ' ' || prev === '\t') {
  100. prev = ch;
  101. ch = text[i += 1];
  102. overflow = true;
  103. } // i - 2 accounts for not-dropped last char + newline-escaping \
  104. folds.push(i - 2);
  105. escapedFolds[i - 2] = true;
  106. end = i - 2 + endStep;
  107. split = undefined;
  108. } else {
  109. overflow = true;
  110. }
  111. }
  112. }
  113. prev = ch;
  114. }
  115. if (overflow && onOverflow) onOverflow();
  116. if (folds.length === 0) return text;
  117. if (onFold) onFold();
  118. var res = text.slice(0, folds[0]);
  119. for (var _i = 0; _i < folds.length; ++_i) {
  120. var fold = folds[_i];
  121. var _end = folds[_i + 1] || text.length;
  122. if (mode === FOLD_QUOTED && escapedFolds[fold]) res += "".concat(text[fold], "\\");
  123. res += "\n".concat(indent).concat(text.slice(fold + 1, _end));
  124. }
  125. return res;
  126. }