autoWrap.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var tslib_1 = require("tslib");
  4. var line_1 = require("../guides/line");
  5. var test_1 = require("../utils/test");
  6. function inferTextBaseline(attr) {
  7. var type = attr.type, labelDirection = attr.labelDirection;
  8. if (type === 'linear' && (0, line_1.isAxisHorizontal)(attr)) {
  9. return labelDirection === 'negative' ? 'bottom' : 'top';
  10. }
  11. return 'middle';
  12. }
  13. function wrapLabels(labels, overlapCfg, attr, utils) {
  14. var _a = overlapCfg.wordWrapWidth, wordWrapWidth = _a === void 0 ? 50 : _a, _b = overlapCfg.maxLines, maxLines = _b === void 0 ? 3 : _b, _c = overlapCfg.recoverWhenFailed, recoverWhenFailed = _c === void 0 ? true : _c, _d = overlapCfg.margin, margin = _d === void 0 ? [0, 0, 0, 0] : _d;
  15. var defaultLines = labels.map(function (label) { return label.attr('maxLines') || 1; });
  16. var minLines = Math.min.apply(Math, tslib_1.__spreadArray([], tslib_1.__read(defaultLines), false));
  17. var runAndPassed = function () { return (0, test_1.boundTest)(labels, attr, margin).length < 1; };
  18. var textBaseline = inferTextBaseline(attr);
  19. var setLabelsWrap = function (lines) {
  20. return labels.forEach(function (label, index) {
  21. var maxLines = Array.isArray(lines) ? lines[index] : lines;
  22. utils.wrap(label, wordWrapWidth, maxLines, textBaseline);
  23. });
  24. };
  25. if (minLines > maxLines)
  26. return;
  27. for (var lines = minLines; lines <= maxLines; lines++) {
  28. setLabelsWrap(lines);
  29. if (runAndPassed())
  30. return;
  31. }
  32. if (recoverWhenFailed) {
  33. setLabelsWrap(defaultLines);
  34. }
  35. }
  36. exports.default = wrapLabels;
  37. //# sourceMappingURL=autoWrap.js.map