autoWrap.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334
  1. import { __read, __spreadArray } from "tslib";
  2. import { isAxisHorizontal } from '../guides/line';
  3. import { boundTest } from '../utils/test';
  4. function inferTextBaseline(attr) {
  5. var type = attr.type, labelDirection = attr.labelDirection;
  6. if (type === 'linear' && isAxisHorizontal(attr)) {
  7. return labelDirection === 'negative' ? 'bottom' : 'top';
  8. }
  9. return 'middle';
  10. }
  11. export default function wrapLabels(labels, overlapCfg, attr, utils) {
  12. 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;
  13. var defaultLines = labels.map(function (label) { return label.attr('maxLines') || 1; });
  14. var minLines = Math.min.apply(Math, __spreadArray([], __read(defaultLines), false));
  15. var runAndPassed = function () { return boundTest(labels, attr, margin).length < 1; };
  16. var textBaseline = inferTextBaseline(attr);
  17. var setLabelsWrap = function (lines) {
  18. return labels.forEach(function (label, index) {
  19. var maxLines = Array.isArray(lines) ? lines[index] : lines;
  20. utils.wrap(label, wordWrapWidth, maxLines, textBaseline);
  21. });
  22. };
  23. if (minLines > maxLines)
  24. return;
  25. for (var lines = minLines; lines <= maxLines; lines++) {
  26. setLabelsWrap(lines);
  27. if (runAndPassed())
  28. return;
  29. }
  30. if (recoverWhenFailed) {
  31. setLabelsWrap(defaultLines);
  32. }
  33. }
  34. //# sourceMappingURL=autoWrap.js.map