@elastic/eui 88.5.1 → 88.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. package/es/components/combo_box/combo_box_input/combo_box_input.js +1 -2
  2. package/es/components/text_truncate/index.js +1 -1
  3. package/es/components/text_truncate/utils.js +88 -138
  4. package/es/services/canvas/canvas_text_utils.js +64 -0
  5. package/es/services/canvas/index.js +9 -0
  6. package/es/services/index.js +1 -0
  7. package/eui.d.ts +37 -27
  8. package/lib/components/combo_box/combo_box_input/combo_box_input.js +1 -2
  9. package/lib/components/text_truncate/index.js +0 -6
  10. package/lib/components/text_truncate/utils.js +97 -148
  11. package/lib/services/canvas/canvas_text_utils.js +70 -0
  12. package/lib/services/canvas/index.js +12 -0
  13. package/lib/services/index.js +8 -0
  14. package/optimize/es/components/combo_box/combo_box_input/combo_box_input.js +1 -2
  15. package/optimize/es/components/text_truncate/index.js +1 -1
  16. package/optimize/es/components/text_truncate/utils.js +87 -137
  17. package/optimize/es/services/canvas/canvas_text_utils.js +60 -0
  18. package/optimize/es/services/canvas/index.js +9 -0
  19. package/optimize/es/services/index.js +1 -0
  20. package/optimize/lib/components/combo_box/combo_box_input/combo_box_input.js +1 -2
  21. package/optimize/lib/components/text_truncate/index.js +0 -6
  22. package/optimize/lib/components/text_truncate/utils.js +96 -147
  23. package/optimize/lib/services/canvas/canvas_text_utils.js +67 -0
  24. package/optimize/lib/services/canvas/index.js +12 -0
  25. package/optimize/lib/services/index.js +8 -0
  26. package/package.json +1 -1
  27. package/test-env/components/combo_box/combo_box_input/combo_box_input.js +1 -2
  28. package/test-env/components/text_truncate/index.js +0 -6
  29. package/test-env/components/text_truncate/utils.js +232 -38
  30. package/test-env/services/canvas/canvas_text_utils.js +30 -0
  31. package/test-env/services/canvas/index.js +12 -0
  32. package/test-env/services/index.js +8 -0
@@ -4,15 +4,19 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
4
4
  Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
- exports.TruncationUtils = exports.CanvasTextUtils = void 0;
7
+ exports.TruncationUtils = void 0;
8
+ var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
9
+ var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
10
+ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
11
+ var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
8
12
  var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
13
+ var _get2 = _interopRequireDefault(require("@babel/runtime/helpers/get"));
9
14
  var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
10
15
  var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
11
16
  var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
12
- var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
13
- var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
14
17
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
15
- var _utils = require("./utils");
18
+ var _canvas = require("../../services/canvas");
19
+ var _excluded = ["fullText", "ellipsis", "availableWidth"];
16
20
  function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2.default)(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2.default)(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2.default)(this, result); }; }
17
21
  function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } /*
18
22
  * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
@@ -21,45 +25,235 @@ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Re
21
25
  * in compliance with, at your election, the Elastic License 2.0 or the Server
22
26
  * Side Public License, v 1.
23
27
  */
24
- var CanvasTextUtils = /*#__PURE__*/(0, _createClass2.default)(function CanvasTextUtils(_) {
25
- var _this = this;
26
- (0, _classCallCheck2.default)(this, CanvasTextUtils);
27
- (0, _defineProperty2.default)(this, "computeFontFromElement", function (_) {
28
- return '';
29
- });
30
- (0, _defineProperty2.default)(this, "textWidth", 0);
31
- (0, _defineProperty2.default)(this, "currentText", '');
32
- (0, _defineProperty2.default)(this, "setTextToCheck", function (text) {
33
- _this.currentText = text;
34
- });
35
- });
36
- exports.CanvasTextUtils = CanvasTextUtils;
37
- var TruncationUtils = /*#__PURE__*/function (_TruncationUtils2) {
38
- (0, _inherits2.default)(TruncationUtils, _TruncationUtils2);
28
+ /**
29
+ * Utilities for truncating types at various positions, as well as
30
+ * determining whether truncation is possible or even necessary.
31
+ */
32
+ var TruncationUtils = /*#__PURE__*/function (_CanvasTextUtils) {
33
+ (0, _inherits2.default)(TruncationUtils, _CanvasTextUtils);
39
34
  var _super = _createSuper(TruncationUtils);
40
- function TruncationUtils(props) {
41
- var _this2;
35
+ function TruncationUtils(_ref) {
36
+ var _this;
37
+ var fullText = _ref.fullText,
38
+ ellipsis = _ref.ellipsis,
39
+ _availableWidth = _ref.availableWidth,
40
+ rest = (0, _objectWithoutProperties2.default)(_ref, _excluded);
42
41
  (0, _classCallCheck2.default)(this, TruncationUtils);
43
- _this2 = _super.call(this, props);
44
- // Jest perf optimization - since there's no meaningful truncation we can make
45
- // without meaningful width calculations, just return the full untruncated text
46
- (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this2), "truncateStart", function (_) {
47
- return _this2.fullText;
42
+ _this = _super.call(this, rest);
43
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "fullText", void 0);
44
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "ellipsis", void 0);
45
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "availableWidth", void 0);
46
+ /**
47
+ * Performance utilities
48
+ */
49
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "debugPerformance", false);
50
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "debugCounter", 0);
51
+ /**
52
+ * Internal utils for calculating a ratio based on the passed available width
53
+ * vs the full text width.
54
+ * This ratio is used to get an initial _approximate_ text string that should
55
+ * be slightly over the available width, which we can then remove from
56
+ * character-by-character until the text just fits within the available width.
57
+ */
58
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "widthRatio", 0);
59
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "setTextWidthRatio", function () {
60
+ var text = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _this.fullText;
61
+ var textToOffset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
62
+ // Account for reduced available width due to (e.g.) truncation offset
63
+ var availableWidth = _this.availableWidth;
64
+ if (textToOffset) {
65
+ _this.setTextToCheck(textToOffset);
66
+ availableWidth = availableWidth - _this.textWidth;
67
+ }
68
+ _this.setTextToCheck(text);
69
+ _this.widthRatio = availableWidth / _this.textWidth;
70
+ });
71
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "getTextFromRatio", function (text, type) {
72
+ var characterRatio = Math.ceil(text.length * _this.widthRatio);
73
+ var index = type === 'start' ? text.length - characterRatio : characterRatio;
74
+ var _splitText$at = splitText(text).at(index),
75
+ _splitText$at2 = (0, _slicedToArray2.default)(_splitText$at, 2),
76
+ end = _splitText$at2[0],
77
+ start = _splitText$at2[1];
78
+ return type === 'start' ? start : end;
79
+ });
80
+ /**
81
+ * Early return checks
82
+ */
83
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "checkIfTruncationIsNeeded", function () {
84
+ _this.setTextToCheck(_this.fullText);
85
+ if (_this.availableWidth >= _this.textWidth) {
86
+ return false;
87
+ }
48
88
  });
49
- (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this2), "truncateEnd", function (_) {
50
- return _this2.fullText;
89
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "checkSufficientEllipsisWidth", function (truncation) {
90
+ var textToCheck = truncation === 'startEnd' ? "".concat(_this.ellipsis, " ").concat(_this.ellipsis) // startEnd needs a little more space
91
+ : _this.ellipsis;
92
+ _this.setTextToCheck(textToCheck);
93
+ if (_this.textWidth >= _this.availableWidth * 0.9) {
94
+ console.error('The truncation ellipsis is larger than the available width. No text can be rendered.');
95
+ return false;
96
+ }
51
97
  });
52
- (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this2), "truncateStartEndAtPosition", function (_) {
53
- return _this2.fullText;
98
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "checkTruncationOffsetWidth", function (text) {
99
+ _this.setTextToCheck(text);
100
+ if (_this.textWidth > _this.availableWidth) {
101
+ console.error('The passed truncationOffset is too large for the available width. Truncating the offset instead.');
102
+ return false;
103
+ }
104
+ });
105
+ /**
106
+ * Truncation types logic. This is where the magic happens
107
+ */
108
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "truncateStart", function (truncationOffset) {
109
+ var truncatedText = _this.fullText;
110
+ var leadingText = '';
111
+ var combinedText = function combinedText() {
112
+ return leadingText + truncatedText;
113
+ };
114
+ if (truncationOffset) {
115
+ var _splitText$at3 = splitText(_this.fullText).at(truncationOffset);
116
+ var _splitText$at4 = (0, _slicedToArray2.default)(_splitText$at3, 2);
117
+ leadingText = _splitText$at4[0];
118
+ truncatedText = _splitText$at4[1];
119
+ var widthCheck = leadingText + _this.ellipsis;
120
+ if (_this.checkTruncationOffsetWidth(widthCheck) === false) {
121
+ truncatedText = leadingText;
122
+ leadingText = '';
123
+ }
124
+ }
125
+
126
+ // Get text width ratio width accounting for any truncation offset text,
127
+ // and guesstimate an initial truncated string
128
+ _this.setTextWidthRatio(truncatedText, leadingText);
129
+ truncatedText = _this.getTextFromRatio(truncatedText, 'start');
130
+ leadingText += _this.ellipsis;
131
+ _this.setTextToCheck(combinedText());
132
+ while (_this.textWidth > _this.availableWidth) {
133
+ truncatedText = removeFirstCharacter(truncatedText);
134
+ _this.setTextToCheck(combinedText());
135
+ }
136
+ return combinedText();
137
+ });
138
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "truncateEnd", function (truncationOffset) {
139
+ var truncatedText = _this.fullText;
140
+ var trailingText = '';
141
+ var combinedText = function combinedText() {
142
+ return truncatedText + trailingText;
143
+ };
144
+ if (truncationOffset) {
145
+ var index = _this.fullText.length - truncationOffset;
146
+ var _splitText$at5 = splitText(_this.fullText).at(index);
147
+ var _splitText$at6 = (0, _slicedToArray2.default)(_splitText$at5, 2);
148
+ truncatedText = _splitText$at6[0];
149
+ trailingText = _splitText$at6[1];
150
+ var widthCheck = _this.ellipsis + trailingText;
151
+ if (_this.checkTruncationOffsetWidth(widthCheck) === false) {
152
+ truncatedText = trailingText;
153
+ trailingText = '';
154
+ }
155
+ }
156
+
157
+ // Get text width ratio width accounting for any truncation offset text,
158
+ // and guesstimate an initial truncated string
159
+ _this.setTextWidthRatio(truncatedText, trailingText);
160
+ truncatedText = _this.getTextFromRatio(truncatedText, 'end');
161
+ trailingText = _this.ellipsis + trailingText;
162
+ _this.setTextToCheck(combinedText());
163
+ while (_this.textWidth > _this.availableWidth) {
164
+ truncatedText = removeLastCharacter(truncatedText);
165
+ _this.setTextToCheck(combinedText());
166
+ }
167
+ return combinedText();
168
+ });
169
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "truncateStartEndAtPosition", function (truncationPosition) {
170
+ // Split the text from the anchor position, using the width ratio
171
+ // to get the starting and ending indices from the position
172
+ _this.setTextWidthRatio();
173
+ var characterRatio = Math.floor(_this.fullText.length * _this.widthRatio / 2);
174
+ var truncateStart = truncationPosition - characterRatio;
175
+ var truncateEnd = truncationPosition + characterRatio;
176
+
177
+ // If either of the approximate start/end truncation indices go beyond the
178
+ // bounds of the actual text, we can simply use end or start truncation instead
179
+ if (truncateStart < 0) {
180
+ return _this.truncateEnd();
181
+ }
182
+ if (truncateEnd >= _this.fullText.length) {
183
+ return _this.truncateStart();
184
+ }
185
+ var truncatedText = _this.fullText.substring(truncateStart, truncateEnd);
186
+ var combinedText = function combinedText() {
187
+ return _this.ellipsis + truncatedText + _this.ellipsis;
188
+ };
189
+ _this.setTextToCheck(combinedText());
190
+ var alternating;
191
+ while (_this.textWidth > _this.availableWidth) {
192
+ truncatedText = alternating ? removeLastCharacter(truncatedText) : removeFirstCharacter(truncatedText);
193
+ alternating = !alternating;
194
+ _this.setTextToCheck(combinedText());
195
+ }
196
+ return combinedText();
54
197
  });
55
- (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this2), "truncateStartEndAtMiddle", function () {
56
- return _this2.fullText;
198
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "truncateStartEndAtMiddle", function () {
199
+ var middlePosition = Math.floor(_this.fullText.length / 2);
200
+ return _this.truncateStartEndAtPosition(middlePosition);
57
201
  });
58
- (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this2), "truncateMiddle", function () {
59
- return _this2.fullText;
202
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "truncateMiddle", function () {
203
+ var middlePosition = Math.floor(_this.fullText.length / 2);
204
+ var _splitText$at7 = splitText(_this.fullText).at(middlePosition),
205
+ _splitText$at8 = (0, _slicedToArray2.default)(_splitText$at7, 2),
206
+ firstHalf = _splitText$at8[0],
207
+ secondHalf = _splitText$at8[1];
208
+ _this.setTextWidthRatio();
209
+ firstHalf = _this.getTextFromRatio(firstHalf, 'end');
210
+ secondHalf = _this.getTextFromRatio(secondHalf, 'start');
211
+ var combinedText = function combinedText() {
212
+ return firstHalf + _this.ellipsis + secondHalf;
213
+ };
214
+ _this.setTextToCheck(combinedText());
215
+ var alternating;
216
+ while (_this.textWidth > _this.availableWidth) {
217
+ alternating = !alternating;
218
+ if (alternating) {
219
+ firstHalf = removeLastCharacter(firstHalf);
220
+ } else {
221
+ secondHalf = removeFirstCharacter(secondHalf);
222
+ }
223
+ _this.setTextToCheck(combinedText());
224
+ }
225
+ return combinedText();
60
226
  });
61
- return _this2;
227
+ _this.fullText = fullText;
228
+ _this.ellipsis = ellipsis;
229
+ _this.availableWidth = _availableWidth;
230
+ return _this;
62
231
  }
63
- return (0, _createClass2.default)(TruncationUtils);
64
- }(_utils.TruncationUtils);
65
- exports.TruncationUtils = TruncationUtils;
232
+ (0, _createClass2.default)(TruncationUtils, [{
233
+ key: "textWidth",
234
+ get: function get() {
235
+ if (this.debugPerformance) {
236
+ this.debugCounter++;
237
+ }
238
+ return (0, _get2.default)((0, _getPrototypeOf2.default)(TruncationUtils.prototype), "textWidth", this);
239
+ }
240
+ }]);
241
+ return TruncationUtils;
242
+ }(_canvas.CanvasTextUtils);
243
+ /**
244
+ * DRY character/substring utils
245
+ */
246
+ exports.TruncationUtils = TruncationUtils;
247
+ var removeLastCharacter = function removeLastCharacter(text) {
248
+ return text.substring(0, text.length - 1);
249
+ };
250
+ var removeFirstCharacter = function removeFirstCharacter(text) {
251
+ return text.substring(1);
252
+ };
253
+ var splitText = function splitText(text) {
254
+ return {
255
+ at: function at(index) {
256
+ return [text.substring(0, index), text.substring(index)];
257
+ }
258
+ };
259
+ };
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.CanvasTextUtils = void 0;
8
+ var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
9
+ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
10
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
+ /*
12
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
13
+ * or more contributor license agreements. Licensed under the Elastic License
14
+ * 2.0 and the Server Side Public License, v 1; you may not use this file except
15
+ * in compliance with, at your election, the Elastic License 2.0 or the Server
16
+ * Side Public License, v 1.
17
+ */
18
+ var CanvasTextUtils = /*#__PURE__*/(0, _createClass2.default)(function CanvasTextUtils(_) {
19
+ var _this = this;
20
+ (0, _classCallCheck2.default)(this, CanvasTextUtils);
21
+ (0, _defineProperty2.default)(this, "computeFontFromElement", function (_) {
22
+ return '';
23
+ });
24
+ (0, _defineProperty2.default)(this, "textWidth", 0);
25
+ (0, _defineProperty2.default)(this, "currentText", '');
26
+ (0, _defineProperty2.default)(this, "setTextToCheck", function (text) {
27
+ _this.currentText = text;
28
+ });
29
+ });
30
+ exports.CanvasTextUtils = CanvasTextUtils;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "CanvasTextUtils", {
7
+ enumerable: true,
8
+ get: function get() {
9
+ return _canvas_text_utils.CanvasTextUtils;
10
+ }
11
+ });
12
+ var _canvas_text_utils = require("./canvas_text_utils");
@@ -20,6 +20,7 @@ var _exportNames = {
20
20
  useIsWithinBreakpoints: true,
21
21
  useIsWithinMaxBreakpoint: true,
22
22
  useIsWithinMinBreakpoint: true,
23
+ CanvasTextUtils: true,
23
24
  brighten: true,
24
25
  calculateContrast: true,
25
26
  calculateLuminance: true,
@@ -97,6 +98,12 @@ Object.defineProperty(exports, "CENTER_ALIGNMENT", {
97
98
  return _alignment.CENTER_ALIGNMENT;
98
99
  }
99
100
  });
101
+ Object.defineProperty(exports, "CanvasTextUtils", {
102
+ enumerable: true,
103
+ get: function get() {
104
+ return _canvas.CanvasTextUtils;
105
+ }
106
+ });
100
107
  Object.defineProperty(exports, "Comparators", {
101
108
  enumerable: true,
102
109
  get: function get() {
@@ -601,6 +608,7 @@ exports.keys = keys;
601
608
  var _accessibility = require("./accessibility");
602
609
  var _alignment = require("./alignment");
603
610
  var _breakpoint = require("./breakpoint");
611
+ var _canvas = require("./canvas");
604
612
  var _color = require("./color");
605
613
  var _color_picker = require("./color_picker");
606
614
  var _console = require("./console");