@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
@@ -27,8 +27,7 @@ function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input ==
27
27
  import React, { Component } from 'react';
28
28
  import PropTypes from "prop-types";
29
29
  import classNames from 'classnames';
30
- import { keys } from '../../../services';
31
- import { CanvasTextUtils } from '../../text_truncate';
30
+ import { keys, CanvasTextUtils } from '../../../services';
32
31
  import { EuiScreenReaderOnly } from '../../accessibility';
33
32
  import { EuiFormControlLayout } from '../../form/form_control_layout';
34
33
  import { getFormControlClassNameForIconCount } from '../../form/form_control_layout/_num_icons';
@@ -7,4 +7,4 @@
7
7
  */
8
8
 
9
9
  export { EuiTextTruncate } from './text_truncate';
10
- export { CanvasTextUtils, TruncationUtils } from './utils';
10
+ export { TruncationUtils } from './utils';
@@ -8,6 +8,9 @@ function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefine
8
8
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
9
9
  function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
10
10
  function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
11
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
12
+ function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
13
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
11
14
  function _get() { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get.bind(); } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(arguments.length < 3 ? target : receiver); } return desc.value; }; } return _get.apply(this, arguments); }
12
15
  function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; }
13
16
  function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
@@ -17,9 +20,6 @@ function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) ===
17
20
  function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
18
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; } }
19
22
  function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
20
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
21
- function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
22
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
23
23
  function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
24
24
  function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
25
25
  function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
@@ -31,57 +31,7 @@ function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input ==
31
31
  * Side Public License, v 1.
32
32
  */
33
33
 
34
- /**
35
- * Under the hood, a temporary Canvas element is created for manipulating text
36
- * & determining text width.
37
- *
38
- * To accurately measure text, canvas rendering requires either a container to
39
- * compute/derive font styles from, or a static font string (useful for usage
40
- * outside the DOM). Particular care should be applied when fallback fonts are
41
- * used, as more fallback fonts can lead to less precision.
42
- *
43
- * Please note that while canvas is more significantly more performant than DOM
44
- * measurement, there are subpixel to single digit pixel differences between
45
- * DOM and canvas measurement due to the different rendering engines used.
46
- */
47
- export var CanvasTextUtils = /*#__PURE__*/function () {
48
- function CanvasTextUtils(_ref) {
49
- var _this = this;
50
- var font = _ref.font,
51
- container = _ref.container;
52
- _classCallCheck(this, CanvasTextUtils);
53
- _defineProperty(this, "context", void 0);
54
- _defineProperty(this, "currentText", '');
55
- _defineProperty(this, "computeFontFromElement", function (element) {
56
- var computedStyles = window.getComputedStyle(element);
57
- // TODO: font-stretch is not included even though it potentially should be
58
- // @see https://developer.mozilla.org/en-US/docs/Web/CSS/font#constituent_properties
59
- // It appears to be unsupported and/or breaks font computation in canvas
60
- return ['font-style', 'font-variant', 'font-weight', 'font-size', 'font-family'].map(function (prop) {
61
- return computedStyles.getPropertyValue(prop);
62
- }).join(' ').trim();
63
- });
64
- _defineProperty(this, "setTextToCheck", function (text) {
65
- _this.currentText = text;
66
- });
67
- this.context = document.createElement('canvas').getContext('2d');
68
-
69
- // Set the canvas font to ensure text width calculations are correct
70
- if (font) {
71
- this.context.font = font;
72
- } else if (container) {
73
- this.context.font = this.computeFontFromElement(container);
74
- }
75
- }
76
- _createClass(CanvasTextUtils, [{
77
- key: "textWidth",
78
- get: function get() {
79
- return this.context.measureText(this.currentText).width;
80
- }
81
- }]);
82
- return CanvasTextUtils;
83
- }();
84
-
34
+ import { CanvasTextUtils } from '../../services/canvas';
85
35
  /**
86
36
  * Utilities for truncating types at various positions, as well as
87
37
  * determining whether truncation is possible or even necessary.
@@ -89,22 +39,22 @@ export var CanvasTextUtils = /*#__PURE__*/function () {
89
39
  export var TruncationUtils = /*#__PURE__*/function (_CanvasTextUtils) {
90
40
  _inherits(TruncationUtils, _CanvasTextUtils);
91
41
  var _super = _createSuper(TruncationUtils);
92
- function TruncationUtils(_ref2) {
93
- var _this2;
94
- var fullText = _ref2.fullText,
95
- ellipsis = _ref2.ellipsis,
96
- _availableWidth = _ref2.availableWidth,
97
- rest = _objectWithoutProperties(_ref2, _excluded);
42
+ function TruncationUtils(_ref) {
43
+ var _this;
44
+ var fullText = _ref.fullText,
45
+ ellipsis = _ref.ellipsis,
46
+ _availableWidth = _ref.availableWidth,
47
+ rest = _objectWithoutProperties(_ref, _excluded);
98
48
  _classCallCheck(this, TruncationUtils);
99
- _this2 = _super.call(this, rest);
100
- _defineProperty(_assertThisInitialized(_this2), "fullText", void 0);
101
- _defineProperty(_assertThisInitialized(_this2), "ellipsis", void 0);
102
- _defineProperty(_assertThisInitialized(_this2), "availableWidth", void 0);
49
+ _this = _super.call(this, rest);
50
+ _defineProperty(_assertThisInitialized(_this), "fullText", void 0);
51
+ _defineProperty(_assertThisInitialized(_this), "ellipsis", void 0);
52
+ _defineProperty(_assertThisInitialized(_this), "availableWidth", void 0);
103
53
  /**
104
54
  * Performance utilities
105
55
  */
106
- _defineProperty(_assertThisInitialized(_this2), "debugPerformance", false);
107
- _defineProperty(_assertThisInitialized(_this2), "debugCounter", 0);
56
+ _defineProperty(_assertThisInitialized(_this), "debugPerformance", false);
57
+ _defineProperty(_assertThisInitialized(_this), "debugCounter", 0);
108
58
  /**
109
59
  * Internal utils for calculating a ratio based on the passed available width
110
60
  * vs the full text width.
@@ -112,21 +62,21 @@ export var TruncationUtils = /*#__PURE__*/function (_CanvasTextUtils) {
112
62
  * be slightly over the available width, which we can then remove from
113
63
  * character-by-character until the text just fits within the available width.
114
64
  */
115
- _defineProperty(_assertThisInitialized(_this2), "widthRatio", 0);
116
- _defineProperty(_assertThisInitialized(_this2), "setTextWidthRatio", function () {
117
- var text = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _this2.fullText;
65
+ _defineProperty(_assertThisInitialized(_this), "widthRatio", 0);
66
+ _defineProperty(_assertThisInitialized(_this), "setTextWidthRatio", function () {
67
+ var text = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _this.fullText;
118
68
  var textToOffset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
119
69
  // Account for reduced available width due to (e.g.) truncation offset
120
- var availableWidth = _this2.availableWidth;
70
+ var availableWidth = _this.availableWidth;
121
71
  if (textToOffset) {
122
- _this2.setTextToCheck(textToOffset);
123
- availableWidth = availableWidth - _this2.textWidth;
72
+ _this.setTextToCheck(textToOffset);
73
+ availableWidth = availableWidth - _this.textWidth;
124
74
  }
125
- _this2.setTextToCheck(text);
126
- _this2.widthRatio = availableWidth / _this2.textWidth;
75
+ _this.setTextToCheck(text);
76
+ _this.widthRatio = availableWidth / _this.textWidth;
127
77
  });
128
- _defineProperty(_assertThisInitialized(_this2), "getTextFromRatio", function (text, type) {
129
- var characterRatio = Math.ceil(text.length * _this2.widthRatio);
78
+ _defineProperty(_assertThisInitialized(_this), "getTextFromRatio", function (text, type) {
79
+ var characterRatio = Math.ceil(text.length * _this.widthRatio);
130
80
  var index = type === 'start' ? text.length - characterRatio : characterRatio;
131
81
  var _splitText$at = splitText(text).at(index),
132
82
  _splitText$at2 = _slicedToArray(_splitText$at, 2),
@@ -137,24 +87,24 @@ export var TruncationUtils = /*#__PURE__*/function (_CanvasTextUtils) {
137
87
  /**
138
88
  * Early return checks
139
89
  */
140
- _defineProperty(_assertThisInitialized(_this2), "checkIfTruncationIsNeeded", function () {
141
- _this2.setTextToCheck(_this2.fullText);
142
- if (_this2.availableWidth >= _this2.textWidth) {
90
+ _defineProperty(_assertThisInitialized(_this), "checkIfTruncationIsNeeded", function () {
91
+ _this.setTextToCheck(_this.fullText);
92
+ if (_this.availableWidth >= _this.textWidth) {
143
93
  return false;
144
94
  }
145
95
  });
146
- _defineProperty(_assertThisInitialized(_this2), "checkSufficientEllipsisWidth", function (truncation) {
147
- var textToCheck = truncation === 'startEnd' ? "".concat(_this2.ellipsis, " ").concat(_this2.ellipsis) // startEnd needs a little more space
148
- : _this2.ellipsis;
149
- _this2.setTextToCheck(textToCheck);
150
- if (_this2.textWidth >= _this2.availableWidth * 0.9) {
96
+ _defineProperty(_assertThisInitialized(_this), "checkSufficientEllipsisWidth", function (truncation) {
97
+ var textToCheck = truncation === 'startEnd' ? "".concat(_this.ellipsis, " ").concat(_this.ellipsis) // startEnd needs a little more space
98
+ : _this.ellipsis;
99
+ _this.setTextToCheck(textToCheck);
100
+ if (_this.textWidth >= _this.availableWidth * 0.9) {
151
101
  console.error('The truncation ellipsis is larger than the available width. No text can be rendered.');
152
102
  return false;
153
103
  }
154
104
  });
155
- _defineProperty(_assertThisInitialized(_this2), "checkTruncationOffsetWidth", function (text) {
156
- _this2.setTextToCheck(text);
157
- if (_this2.textWidth > _this2.availableWidth) {
105
+ _defineProperty(_assertThisInitialized(_this), "checkTruncationOffsetWidth", function (text) {
106
+ _this.setTextToCheck(text);
107
+ if (_this.textWidth > _this.availableWidth) {
158
108
  console.error('The passed truncationOffset is too large for the available width. Truncating the offset instead.');
159
109
  return false;
160
110
  }
@@ -162,19 +112,19 @@ export var TruncationUtils = /*#__PURE__*/function (_CanvasTextUtils) {
162
112
  /**
163
113
  * Truncation types logic. This is where the magic happens
164
114
  */
165
- _defineProperty(_assertThisInitialized(_this2), "truncateStart", function (truncationOffset) {
166
- var truncatedText = _this2.fullText;
115
+ _defineProperty(_assertThisInitialized(_this), "truncateStart", function (truncationOffset) {
116
+ var truncatedText = _this.fullText;
167
117
  var leadingText = '';
168
118
  var combinedText = function combinedText() {
169
119
  return leadingText + truncatedText;
170
120
  };
171
121
  if (truncationOffset) {
172
- var _splitText$at3 = splitText(_this2.fullText).at(truncationOffset);
122
+ var _splitText$at3 = splitText(_this.fullText).at(truncationOffset);
173
123
  var _splitText$at4 = _slicedToArray(_splitText$at3, 2);
174
124
  leadingText = _splitText$at4[0];
175
125
  truncatedText = _splitText$at4[1];
176
- var widthCheck = leadingText + _this2.ellipsis;
177
- if (_this2.checkTruncationOffsetWidth(widthCheck) === false) {
126
+ var widthCheck = leadingText + _this.ellipsis;
127
+ if (_this.checkTruncationOffsetWidth(widthCheck) === false) {
178
128
  truncatedText = leadingText;
179
129
  leadingText = '';
180
130
  }
@@ -182,30 +132,30 @@ export var TruncationUtils = /*#__PURE__*/function (_CanvasTextUtils) {
182
132
 
183
133
  // Get text width ratio width accounting for any truncation offset text,
184
134
  // and guesstimate an initial truncated string
185
- _this2.setTextWidthRatio(truncatedText, leadingText);
186
- truncatedText = _this2.getTextFromRatio(truncatedText, 'start');
187
- leadingText += _this2.ellipsis;
188
- _this2.setTextToCheck(combinedText());
189
- while (_this2.textWidth > _this2.availableWidth) {
135
+ _this.setTextWidthRatio(truncatedText, leadingText);
136
+ truncatedText = _this.getTextFromRatio(truncatedText, 'start');
137
+ leadingText += _this.ellipsis;
138
+ _this.setTextToCheck(combinedText());
139
+ while (_this.textWidth > _this.availableWidth) {
190
140
  truncatedText = removeFirstCharacter(truncatedText);
191
- _this2.setTextToCheck(combinedText());
141
+ _this.setTextToCheck(combinedText());
192
142
  }
193
143
  return combinedText();
194
144
  });
195
- _defineProperty(_assertThisInitialized(_this2), "truncateEnd", function (truncationOffset) {
196
- var truncatedText = _this2.fullText;
145
+ _defineProperty(_assertThisInitialized(_this), "truncateEnd", function (truncationOffset) {
146
+ var truncatedText = _this.fullText;
197
147
  var trailingText = '';
198
148
  var combinedText = function combinedText() {
199
149
  return truncatedText + trailingText;
200
150
  };
201
151
  if (truncationOffset) {
202
- var index = _this2.fullText.length - truncationOffset;
203
- var _splitText$at5 = splitText(_this2.fullText).at(index);
152
+ var index = _this.fullText.length - truncationOffset;
153
+ var _splitText$at5 = splitText(_this.fullText).at(index);
204
154
  var _splitText$at6 = _slicedToArray(_splitText$at5, 2);
205
155
  truncatedText = _splitText$at6[0];
206
156
  trailingText = _splitText$at6[1];
207
- var widthCheck = _this2.ellipsis + trailingText;
208
- if (_this2.checkTruncationOffsetWidth(widthCheck) === false) {
157
+ var widthCheck = _this.ellipsis + trailingText;
158
+ if (_this.checkTruncationOffsetWidth(widthCheck) === false) {
209
159
  truncatedText = trailingText;
210
160
  trailingText = '';
211
161
  }
@@ -213,78 +163,78 @@ export var TruncationUtils = /*#__PURE__*/function (_CanvasTextUtils) {
213
163
 
214
164
  // Get text width ratio width accounting for any truncation offset text,
215
165
  // and guesstimate an initial truncated string
216
- _this2.setTextWidthRatio(truncatedText, trailingText);
217
- truncatedText = _this2.getTextFromRatio(truncatedText, 'end');
218
- trailingText = _this2.ellipsis + trailingText;
219
- _this2.setTextToCheck(combinedText());
220
- while (_this2.textWidth > _this2.availableWidth) {
166
+ _this.setTextWidthRatio(truncatedText, trailingText);
167
+ truncatedText = _this.getTextFromRatio(truncatedText, 'end');
168
+ trailingText = _this.ellipsis + trailingText;
169
+ _this.setTextToCheck(combinedText());
170
+ while (_this.textWidth > _this.availableWidth) {
221
171
  truncatedText = removeLastCharacter(truncatedText);
222
- _this2.setTextToCheck(combinedText());
172
+ _this.setTextToCheck(combinedText());
223
173
  }
224
174
  return combinedText();
225
175
  });
226
- _defineProperty(_assertThisInitialized(_this2), "truncateStartEndAtPosition", function (truncationPosition) {
176
+ _defineProperty(_assertThisInitialized(_this), "truncateStartEndAtPosition", function (truncationPosition) {
227
177
  // Split the text from the anchor position, using the width ratio
228
178
  // to get the starting and ending indices from the position
229
- _this2.setTextWidthRatio();
230
- var characterRatio = Math.floor(_this2.fullText.length * _this2.widthRatio / 2);
179
+ _this.setTextWidthRatio();
180
+ var characterRatio = Math.floor(_this.fullText.length * _this.widthRatio / 2);
231
181
  var truncateStart = truncationPosition - characterRatio;
232
182
  var truncateEnd = truncationPosition + characterRatio;
233
183
 
234
184
  // If either of the approximate start/end truncation indices go beyond the
235
185
  // bounds of the actual text, we can simply use end or start truncation instead
236
186
  if (truncateStart < 0) {
237
- return _this2.truncateEnd();
187
+ return _this.truncateEnd();
238
188
  }
239
- if (truncateEnd >= _this2.fullText.length) {
240
- return _this2.truncateStart();
189
+ if (truncateEnd >= _this.fullText.length) {
190
+ return _this.truncateStart();
241
191
  }
242
- var truncatedText = _this2.fullText.substring(truncateStart, truncateEnd);
192
+ var truncatedText = _this.fullText.substring(truncateStart, truncateEnd);
243
193
  var combinedText = function combinedText() {
244
- return _this2.ellipsis + truncatedText + _this2.ellipsis;
194
+ return _this.ellipsis + truncatedText + _this.ellipsis;
245
195
  };
246
- _this2.setTextToCheck(combinedText());
196
+ _this.setTextToCheck(combinedText());
247
197
  var alternating;
248
- while (_this2.textWidth > _this2.availableWidth) {
198
+ while (_this.textWidth > _this.availableWidth) {
249
199
  truncatedText = alternating ? removeLastCharacter(truncatedText) : removeFirstCharacter(truncatedText);
250
200
  alternating = !alternating;
251
- _this2.setTextToCheck(combinedText());
201
+ _this.setTextToCheck(combinedText());
252
202
  }
253
203
  return combinedText();
254
204
  });
255
- _defineProperty(_assertThisInitialized(_this2), "truncateStartEndAtMiddle", function () {
256
- var middlePosition = Math.floor(_this2.fullText.length / 2);
257
- return _this2.truncateStartEndAtPosition(middlePosition);
205
+ _defineProperty(_assertThisInitialized(_this), "truncateStartEndAtMiddle", function () {
206
+ var middlePosition = Math.floor(_this.fullText.length / 2);
207
+ return _this.truncateStartEndAtPosition(middlePosition);
258
208
  });
259
- _defineProperty(_assertThisInitialized(_this2), "truncateMiddle", function () {
260
- var middlePosition = Math.floor(_this2.fullText.length / 2);
261
- var _splitText$at7 = splitText(_this2.fullText).at(middlePosition),
209
+ _defineProperty(_assertThisInitialized(_this), "truncateMiddle", function () {
210
+ var middlePosition = Math.floor(_this.fullText.length / 2);
211
+ var _splitText$at7 = splitText(_this.fullText).at(middlePosition),
262
212
  _splitText$at8 = _slicedToArray(_splitText$at7, 2),
263
213
  firstHalf = _splitText$at8[0],
264
214
  secondHalf = _splitText$at8[1];
265
- _this2.setTextWidthRatio();
266
- firstHalf = _this2.getTextFromRatio(firstHalf, 'end');
267
- secondHalf = _this2.getTextFromRatio(secondHalf, 'start');
215
+ _this.setTextWidthRatio();
216
+ firstHalf = _this.getTextFromRatio(firstHalf, 'end');
217
+ secondHalf = _this.getTextFromRatio(secondHalf, 'start');
268
218
  var combinedText = function combinedText() {
269
- return firstHalf + _this2.ellipsis + secondHalf;
219
+ return firstHalf + _this.ellipsis + secondHalf;
270
220
  };
271
- _this2.setTextToCheck(combinedText());
221
+ _this.setTextToCheck(combinedText());
272
222
  var alternating;
273
- while (_this2.textWidth > _this2.availableWidth) {
223
+ while (_this.textWidth > _this.availableWidth) {
274
224
  alternating = !alternating;
275
225
  if (alternating) {
276
226
  firstHalf = removeLastCharacter(firstHalf);
277
227
  } else {
278
228
  secondHalf = removeFirstCharacter(secondHalf);
279
229
  }
280
- _this2.setTextToCheck(combinedText());
230
+ _this.setTextToCheck(combinedText());
281
231
  }
282
232
  return combinedText();
283
233
  });
284
- _this2.fullText = fullText;
285
- _this2.ellipsis = ellipsis;
286
- _this2.availableWidth = _availableWidth;
287
- return _this2;
234
+ _this.fullText = fullText;
235
+ _this.ellipsis = ellipsis;
236
+ _this.availableWidth = _availableWidth;
237
+ return _this;
288
238
  }
289
239
  _createClass(TruncationUtils, [{
290
240
  key: "textWidth",
@@ -0,0 +1,64 @@
1
+ function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
2
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
3
+ function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
4
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
5
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
7
+ function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
8
+ /*
9
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
10
+ * or more contributor license agreements. Licensed under the Elastic License
11
+ * 2.0 and the Server Side Public License, v 1; you may not use this file except
12
+ * in compliance with, at your election, the Elastic License 2.0 or the Server
13
+ * Side Public License, v 1.
14
+ */
15
+
16
+ /**
17
+ * Creates a temporary Canvas element for manipulating text & determining text width.
18
+ *
19
+ * To accurately measure text, canvas rendering requires either a container to
20
+ * compute/derive font styles from, or a static font string (useful for usage
21
+ * outside the DOM). Particular care should be applied when fallback fonts are
22
+ * used, as more fallback fonts can lead to less precision.
23
+ *
24
+ * Please note that while canvas is more significantly more performant than DOM
25
+ * measurement, there are subpixel to single digit pixel differences between
26
+ * DOM and canvas measurement due to the different rendering engines used.
27
+ */
28
+ export var CanvasTextUtils = /*#__PURE__*/function () {
29
+ function CanvasTextUtils(_ref) {
30
+ var _this = this;
31
+ var font = _ref.font,
32
+ container = _ref.container;
33
+ _classCallCheck(this, CanvasTextUtils);
34
+ _defineProperty(this, "context", void 0);
35
+ _defineProperty(this, "currentText", '');
36
+ _defineProperty(this, "computeFontFromElement", function (element) {
37
+ var computedStyles = window.getComputedStyle(element);
38
+ // TODO: font-stretch is not included even though it potentially should be
39
+ // @see https://developer.mozilla.org/en-US/docs/Web/CSS/font#constituent_properties
40
+ // It appears to be unsupported and/or breaks font computation in canvas
41
+ return ['font-style', 'font-variant', 'font-weight', 'font-size', 'font-family'].map(function (prop) {
42
+ return computedStyles.getPropertyValue(prop);
43
+ }).join(' ').trim();
44
+ });
45
+ _defineProperty(this, "setTextToCheck", function (text) {
46
+ _this.currentText = text;
47
+ });
48
+ this.context = document.createElement('canvas').getContext('2d');
49
+
50
+ // Set the canvas font to ensure text width calculations are correct
51
+ if (font) {
52
+ this.context.font = font;
53
+ } else if (container) {
54
+ this.context.font = this.computeFontFromElement(container);
55
+ }
56
+ }
57
+ _createClass(CanvasTextUtils, [{
58
+ key: "textWidth",
59
+ get: function get() {
60
+ return this.context.measureText(this.currentText).width;
61
+ }
62
+ }]);
63
+ return CanvasTextUtils;
64
+ }();
@@ -0,0 +1,9 @@
1
+ /*
2
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3
+ * or more contributor license agreements. Licensed under the Elastic License
4
+ * 2.0 and the Server Side Public License, v 1; you may not use this file except
5
+ * in compliance with, at your election, the Elastic License 2.0 or the Server
6
+ * Side Public License, v 1.
7
+ */
8
+
9
+ export { CanvasTextUtils } from './canvas_text_utils';
@@ -11,6 +11,7 @@ import * as keys from './keys';
11
11
  export { accessibleClickKeys, cascadingMenuKeys, comboBoxKeys, htmlIdGenerator, useGeneratedHtmlId } from './accessibility';
12
12
  export { CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT } from './alignment';
13
13
  export { CurrentEuiBreakpointContext, CurrentEuiBreakpointProvider, useCurrentEuiBreakpoint, useIsWithinBreakpoints, useIsWithinMaxBreakpoint, useIsWithinMinBreakpoint } from './breakpoint';
14
+ export { CanvasTextUtils } from './canvas';
14
15
  export { brighten, calculateContrast, calculateLuminance, colorPalette, darken, DEFAULT_VISUALIZATION_COLOR, desaturate, euiPaletteColorBlind, euiPaletteColorBlindBehindText, euiPaletteComplimentary, euiPaletteComplementary, euiPaletteCool, euiPaletteForDarkBackground, euiPaletteForLightBackground, euiPaletteForStatus, euiPaletteForTemperature, euiPaletteGray, euiPaletteNegative, euiPalettePositive, euiPaletteWarm, getSteppedGradient, hexToHsv, hexToRgb, hsvToHex, hsvToRgb, isColorDark, isValidHex, lightness, makeDisabledContrastColor, makeHighContrastColor, rgbToHex, rgbToHsv, saturate, shade, shadeOrTint, tint, tintOrShade, transparentize, VISUALIZATION_COLORS, wcagContrastMin } from './color';
15
16
  export { useColorPickerState, useColorStopsState } from './color_picker';
16
17
  export * from './console';
package/eui.d.ts CHANGED
@@ -8032,6 +8032,40 @@ declare module '@elastic/eui/src/services/breakpoint' {
8032
8032
  export * from '@elastic/eui/src/services/breakpoint/current_breakpoint_hook';
8033
8033
  export * from '@elastic/eui/src/services/breakpoint/is_within_hooks';
8034
8034
 
8035
+ }
8036
+ declare module '@elastic/eui/src/services/canvas/canvas_text_utils' {
8037
+ import type { ExclusiveUnion } from '@elastic/eui/src/components/common';
8038
+ export type CanvasTextParams = ExclusiveUnion<{
8039
+ container: HTMLElement;
8040
+ }, {
8041
+ font: CanvasTextDrawingStyles['font'];
8042
+ }>;
8043
+ /**
8044
+ * Creates a temporary Canvas element for manipulating text & determining text width.
8045
+ *
8046
+ * To accurately measure text, canvas rendering requires either a container to
8047
+ * compute/derive font styles from, or a static font string (useful for usage
8048
+ * outside the DOM). Particular care should be applied when fallback fonts are
8049
+ * used, as more fallback fonts can lead to less precision.
8050
+ *
8051
+ * Please note that while canvas is more significantly more performant than DOM
8052
+ * measurement, there are subpixel to single digit pixel differences between
8053
+ * DOM and canvas measurement due to the different rendering engines used.
8054
+ */
8055
+ export class CanvasTextUtils {
8056
+ context: CanvasRenderingContext2D;
8057
+ currentText: string;
8058
+ constructor({ font, container }: CanvasTextParams);
8059
+ computeFontFromElement: (element: HTMLElement) => string;
8060
+ get textWidth(): number;
8061
+ setTextToCheck: (text: string) => void;
8062
+ }
8063
+
8064
+ }
8065
+ declare module '@elastic/eui/src/services/canvas' {
8066
+ export { CanvasTextUtils } from '@elastic/eui/src/services/canvas/canvas_text_utils';
8067
+ export type { CanvasTextParams } from '@elastic/eui/src/services/canvas/canvas_text_utils';
8068
+
8035
8069
  }
8036
8070
  declare module '@elastic/eui/src/services/color_picker/color_picker' {
8037
8071
  interface colorStopsType {
@@ -8470,6 +8504,7 @@ declare module '@elastic/eui/src/services' {
8470
8504
  export type { HorizontalAlignment } from '@elastic/eui/src/services/alignment';
8471
8505
  export { CurrentEuiBreakpointContext, CurrentEuiBreakpointProvider, useCurrentEuiBreakpoint, useIsWithinBreakpoints, useIsWithinMaxBreakpoint, useIsWithinMinBreakpoint, } from '@elastic/eui/src/services/breakpoint';
8472
8506
  export type { EuiBreakpointSize } from '@elastic/eui/src/services/breakpoint';
8507
+ export { CanvasTextUtils, type CanvasTextParams } from '@elastic/eui/src/services/canvas';
8473
8508
  export { brighten, calculateContrast, calculateLuminance, colorPalette, darken, DEFAULT_VISUALIZATION_COLOR, desaturate, euiPaletteColorBlind, euiPaletteColorBlindBehindText, euiPaletteComplimentary, euiPaletteComplementary, euiPaletteCool, euiPaletteForDarkBackground, euiPaletteForLightBackground, euiPaletteForStatus, euiPaletteForTemperature, euiPaletteGray, euiPaletteNegative, euiPalettePositive, euiPaletteWarm, getSteppedGradient, hexToHsv, hexToRgb, hsvToHex, hsvToRgb, isColorDark, isValidHex, lightness, makeDisabledContrastColor, makeHighContrastColor, rgbToHex, rgbToHsv, saturate, shade, shadeOrTint, tint, tintOrShade, transparentize, VISUALIZATION_COLORS, wcagContrastMin, } from '@elastic/eui/src/services/color';
8474
8509
  export type { HSV } from '@elastic/eui/src/services/color';
8475
8510
  export { useColorPickerState, useColorStopsState } from '@elastic/eui/src/services/color_picker';
@@ -11447,36 +11482,11 @@ declare module '@elastic/eui/src/components/filter_group/filter_select_item' {
11447
11482
 
11448
11483
  }
11449
11484
  declare module '@elastic/eui/src/components/text_truncate/utils' {
11450
- import type { ExclusiveUnion } from '@elastic/eui/src/components/common'; type CanvasTextParams = ExclusiveUnion<{
11451
- container: HTMLElement;
11452
- }, {
11453
- font: CanvasTextDrawingStyles['font'];
11454
- }>; type TruncationParams = CanvasTextParams & {
11485
+ import { CanvasTextParams, CanvasTextUtils } from '@elastic/eui/src/services/canvas'; type TruncationParams = CanvasTextParams & {
11455
11486
  fullText: string;
11456
11487
  ellipsis: string;
11457
11488
  availableWidth: number;
11458
11489
  };
11459
- /**
11460
- * Under the hood, a temporary Canvas element is created for manipulating text
11461
- * & determining text width.
11462
- *
11463
- * To accurately measure text, canvas rendering requires either a container to
11464
- * compute/derive font styles from, or a static font string (useful for usage
11465
- * outside the DOM). Particular care should be applied when fallback fonts are
11466
- * used, as more fallback fonts can lead to less precision.
11467
- *
11468
- * Please note that while canvas is more significantly more performant than DOM
11469
- * measurement, there are subpixel to single digit pixel differences between
11470
- * DOM and canvas measurement due to the different rendering engines used.
11471
- */
11472
- export class CanvasTextUtils {
11473
- context: CanvasRenderingContext2D;
11474
- currentText: string;
11475
- constructor({ font, container }: CanvasTextParams);
11476
- computeFontFromElement: (element: HTMLElement) => string;
11477
- get textWidth(): number;
11478
- setTextToCheck: (text: string) => void;
11479
- }
11480
11490
  /**
11481
11491
  * Utilities for truncating types at various positions, as well as
11482
11492
  * determining whether truncation is possible or even necessary.
@@ -11608,7 +11618,7 @@ declare module '@elastic/eui/src/components/text_truncate/text_truncate' {
11608
11618
  declare module '@elastic/eui/src/components/text_truncate' {
11609
11619
  export type { EuiTextTruncateProps, EuiTextTruncationTypes, } from '@elastic/eui/src/components/text_truncate/text_truncate';
11610
11620
  export { EuiTextTruncate } from '@elastic/eui/src/components/text_truncate/text_truncate';
11611
- export { CanvasTextUtils, TruncationUtils } from '@elastic/eui/src/components/text_truncate/utils';
11621
+ export { TruncationUtils } from '@elastic/eui/src/components/text_truncate/utils';
11612
11622
 
11613
11623
  }
11614
11624
  declare module '@elastic/eui/src/components/combo_box/types' {
@@ -8,7 +8,6 @@ var _react = _interopRequireWildcard(require("react"));
8
8
  var _propTypes = _interopRequireDefault(require("prop-types"));
9
9
  var _classnames = _interopRequireDefault(require("classnames"));
10
10
  var _services = require("../../../services");
11
- var _text_truncate = require("../../text_truncate");
12
11
  var _accessibility = require("../../accessibility");
13
12
  var _form_control_layout = require("../../form/form_control_layout");
14
13
  var _num_icons = require("../../form/form_control_layout/_num_icons");
@@ -58,7 +57,7 @@ var EuiComboBoxInput = /*#__PURE__*/function (_Component) {
58
57
  _defineProperty(_assertThisInitialized(_this), "widthUtils", void 0);
59
58
  _defineProperty(_assertThisInitialized(_this), "inputRefCallback", function (el) {
60
59
  var _this$props$inputRef, _this$props;
61
- _this.widthUtils = new _text_truncate.CanvasTextUtils({
60
+ _this.widthUtils = new _services.CanvasTextUtils({
62
61
  container: el
63
62
  });
64
63
  (_this$props$inputRef = (_this$props = _this.props).inputRef) === null || _this$props$inputRef === void 0 ? void 0 : _this$props$inputRef.call(_this$props, el);
@@ -3,12 +3,6 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- Object.defineProperty(exports, "CanvasTextUtils", {
7
- enumerable: true,
8
- get: function get() {
9
- return _utils.CanvasTextUtils;
10
- }
11
- });
12
6
  Object.defineProperty(exports, "EuiTextTruncate", {
13
7
  enumerable: true,
14
8
  get: function get() {