@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
@@ -3,7 +3,8 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.TruncationUtils = exports.CanvasTextUtils = void 0;
6
+ exports.TruncationUtils = void 0;
7
+ var _canvas = require("../../services/canvas");
7
8
  var _excluded = ["fullText", "ellipsis", "availableWidth"];
8
9
  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); }
9
10
  function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
@@ -14,6 +15,9 @@ function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefine
14
15
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
15
16
  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; }
16
17
  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; }
18
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
19
+ 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); } }
20
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
17
21
  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); }
18
22
  function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; }
19
23
  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); }
@@ -23,93 +27,38 @@ function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) ===
23
27
  function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
24
28
  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; } }
25
29
  function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
26
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
27
- 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); } }
28
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
29
30
  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; }
30
31
  function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
31
- 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); }
32
- /*
33
- * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
34
- * or more contributor license agreements. Licensed under the Elastic License
35
- * 2.0 and the Server Side Public License, v 1; you may not use this file except
36
- * in compliance with, at your election, the Elastic License 2.0 or the Server
37
- * Side Public License, v 1.
38
- */
39
- /**
40
- * Under the hood, a temporary Canvas element is created for manipulating text
41
- * & determining text width.
42
- *
43
- * To accurately measure text, canvas rendering requires either a container to
44
- * compute/derive font styles from, or a static font string (useful for usage
45
- * outside the DOM). Particular care should be applied when fallback fonts are
46
- * used, as more fallback fonts can lead to less precision.
47
- *
48
- * Please note that while canvas is more significantly more performant than DOM
49
- * measurement, there are subpixel to single digit pixel differences between
50
- * DOM and canvas measurement due to the different rendering engines used.
51
- */
52
- var CanvasTextUtils = /*#__PURE__*/function () {
53
- function CanvasTextUtils(_ref) {
54
- var _this = this;
55
- var font = _ref.font,
56
- container = _ref.container;
57
- _classCallCheck(this, CanvasTextUtils);
58
- _defineProperty(this, "context", void 0);
59
- _defineProperty(this, "currentText", '');
60
- _defineProperty(this, "computeFontFromElement", function (element) {
61
- var computedStyles = window.getComputedStyle(element);
62
- // TODO: font-stretch is not included even though it potentially should be
63
- // @see https://developer.mozilla.org/en-US/docs/Web/CSS/font#constituent_properties
64
- // It appears to be unsupported and/or breaks font computation in canvas
65
- return ['font-style', 'font-variant', 'font-weight', 'font-size', 'font-family'].map(function (prop) {
66
- return computedStyles.getPropertyValue(prop);
67
- }).join(' ').trim();
68
- });
69
- _defineProperty(this, "setTextToCheck", function (text) {
70
- _this.currentText = text;
71
- });
72
- this.context = document.createElement('canvas').getContext('2d');
73
-
74
- // Set the canvas font to ensure text width calculations are correct
75
- if (font) {
76
- this.context.font = font;
77
- } else if (container) {
78
- this.context.font = this.computeFontFromElement(container);
79
- }
80
- }
81
- _createClass(CanvasTextUtils, [{
82
- key: "textWidth",
83
- get: function get() {
84
- return this.context.measureText(this.currentText).width;
85
- }
86
- }]);
87
- return CanvasTextUtils;
88
- }();
32
+ 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); } /*
33
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
34
+ * or more contributor license agreements. Licensed under the Elastic License
35
+ * 2.0 and the Server Side Public License, v 1; you may not use this file except
36
+ * in compliance with, at your election, the Elastic License 2.0 or the Server
37
+ * Side Public License, v 1.
38
+ */
89
39
  /**
90
40
  * Utilities for truncating types at various positions, as well as
91
41
  * determining whether truncation is possible or even necessary.
92
42
  */
93
- exports.CanvasTextUtils = CanvasTextUtils;
94
43
  var TruncationUtils = /*#__PURE__*/function (_CanvasTextUtils) {
95
44
  _inherits(TruncationUtils, _CanvasTextUtils);
96
45
  var _super = _createSuper(TruncationUtils);
97
- function TruncationUtils(_ref2) {
98
- var _this2;
99
- var fullText = _ref2.fullText,
100
- ellipsis = _ref2.ellipsis,
101
- _availableWidth = _ref2.availableWidth,
102
- rest = _objectWithoutProperties(_ref2, _excluded);
46
+ function TruncationUtils(_ref) {
47
+ var _this;
48
+ var fullText = _ref.fullText,
49
+ ellipsis = _ref.ellipsis,
50
+ _availableWidth = _ref.availableWidth,
51
+ rest = _objectWithoutProperties(_ref, _excluded);
103
52
  _classCallCheck(this, TruncationUtils);
104
- _this2 = _super.call(this, rest);
105
- _defineProperty(_assertThisInitialized(_this2), "fullText", void 0);
106
- _defineProperty(_assertThisInitialized(_this2), "ellipsis", void 0);
107
- _defineProperty(_assertThisInitialized(_this2), "availableWidth", void 0);
53
+ _this = _super.call(this, rest);
54
+ _defineProperty(_assertThisInitialized(_this), "fullText", void 0);
55
+ _defineProperty(_assertThisInitialized(_this), "ellipsis", void 0);
56
+ _defineProperty(_assertThisInitialized(_this), "availableWidth", void 0);
108
57
  /**
109
58
  * Performance utilities
110
59
  */
111
- _defineProperty(_assertThisInitialized(_this2), "debugPerformance", false);
112
- _defineProperty(_assertThisInitialized(_this2), "debugCounter", 0);
60
+ _defineProperty(_assertThisInitialized(_this), "debugPerformance", false);
61
+ _defineProperty(_assertThisInitialized(_this), "debugCounter", 0);
113
62
  /**
114
63
  * Internal utils for calculating a ratio based on the passed available width
115
64
  * vs the full text width.
@@ -117,21 +66,21 @@ var TruncationUtils = /*#__PURE__*/function (_CanvasTextUtils) {
117
66
  * be slightly over the available width, which we can then remove from
118
67
  * character-by-character until the text just fits within the available width.
119
68
  */
120
- _defineProperty(_assertThisInitialized(_this2), "widthRatio", 0);
121
- _defineProperty(_assertThisInitialized(_this2), "setTextWidthRatio", function () {
122
- var text = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _this2.fullText;
69
+ _defineProperty(_assertThisInitialized(_this), "widthRatio", 0);
70
+ _defineProperty(_assertThisInitialized(_this), "setTextWidthRatio", function () {
71
+ var text = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _this.fullText;
123
72
  var textToOffset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
124
73
  // Account for reduced available width due to (e.g.) truncation offset
125
- var availableWidth = _this2.availableWidth;
74
+ var availableWidth = _this.availableWidth;
126
75
  if (textToOffset) {
127
- _this2.setTextToCheck(textToOffset);
128
- availableWidth = availableWidth - _this2.textWidth;
76
+ _this.setTextToCheck(textToOffset);
77
+ availableWidth = availableWidth - _this.textWidth;
129
78
  }
130
- _this2.setTextToCheck(text);
131
- _this2.widthRatio = availableWidth / _this2.textWidth;
79
+ _this.setTextToCheck(text);
80
+ _this.widthRatio = availableWidth / _this.textWidth;
132
81
  });
133
- _defineProperty(_assertThisInitialized(_this2), "getTextFromRatio", function (text, type) {
134
- var characterRatio = Math.ceil(text.length * _this2.widthRatio);
82
+ _defineProperty(_assertThisInitialized(_this), "getTextFromRatio", function (text, type) {
83
+ var characterRatio = Math.ceil(text.length * _this.widthRatio);
135
84
  var index = type === 'start' ? text.length - characterRatio : characterRatio;
136
85
  var _splitText$at = splitText(text).at(index),
137
86
  _splitText$at2 = _slicedToArray(_splitText$at, 2),
@@ -142,24 +91,24 @@ var TruncationUtils = /*#__PURE__*/function (_CanvasTextUtils) {
142
91
  /**
143
92
  * Early return checks
144
93
  */
145
- _defineProperty(_assertThisInitialized(_this2), "checkIfTruncationIsNeeded", function () {
146
- _this2.setTextToCheck(_this2.fullText);
147
- if (_this2.availableWidth >= _this2.textWidth) {
94
+ _defineProperty(_assertThisInitialized(_this), "checkIfTruncationIsNeeded", function () {
95
+ _this.setTextToCheck(_this.fullText);
96
+ if (_this.availableWidth >= _this.textWidth) {
148
97
  return false;
149
98
  }
150
99
  });
151
- _defineProperty(_assertThisInitialized(_this2), "checkSufficientEllipsisWidth", function (truncation) {
152
- var textToCheck = truncation === 'startEnd' ? "".concat(_this2.ellipsis, " ").concat(_this2.ellipsis) // startEnd needs a little more space
153
- : _this2.ellipsis;
154
- _this2.setTextToCheck(textToCheck);
155
- if (_this2.textWidth >= _this2.availableWidth * 0.9) {
100
+ _defineProperty(_assertThisInitialized(_this), "checkSufficientEllipsisWidth", function (truncation) {
101
+ var textToCheck = truncation === 'startEnd' ? "".concat(_this.ellipsis, " ").concat(_this.ellipsis) // startEnd needs a little more space
102
+ : _this.ellipsis;
103
+ _this.setTextToCheck(textToCheck);
104
+ if (_this.textWidth >= _this.availableWidth * 0.9) {
156
105
  console.error('The truncation ellipsis is larger than the available width. No text can be rendered.');
157
106
  return false;
158
107
  }
159
108
  });
160
- _defineProperty(_assertThisInitialized(_this2), "checkTruncationOffsetWidth", function (text) {
161
- _this2.setTextToCheck(text);
162
- if (_this2.textWidth > _this2.availableWidth) {
109
+ _defineProperty(_assertThisInitialized(_this), "checkTruncationOffsetWidth", function (text) {
110
+ _this.setTextToCheck(text);
111
+ if (_this.textWidth > _this.availableWidth) {
163
112
  console.error('The passed truncationOffset is too large for the available width. Truncating the offset instead.');
164
113
  return false;
165
114
  }
@@ -167,19 +116,19 @@ var TruncationUtils = /*#__PURE__*/function (_CanvasTextUtils) {
167
116
  /**
168
117
  * Truncation types logic. This is where the magic happens
169
118
  */
170
- _defineProperty(_assertThisInitialized(_this2), "truncateStart", function (truncationOffset) {
171
- var truncatedText = _this2.fullText;
119
+ _defineProperty(_assertThisInitialized(_this), "truncateStart", function (truncationOffset) {
120
+ var truncatedText = _this.fullText;
172
121
  var leadingText = '';
173
122
  var combinedText = function combinedText() {
174
123
  return leadingText + truncatedText;
175
124
  };
176
125
  if (truncationOffset) {
177
- var _splitText$at3 = splitText(_this2.fullText).at(truncationOffset);
126
+ var _splitText$at3 = splitText(_this.fullText).at(truncationOffset);
178
127
  var _splitText$at4 = _slicedToArray(_splitText$at3, 2);
179
128
  leadingText = _splitText$at4[0];
180
129
  truncatedText = _splitText$at4[1];
181
- var widthCheck = leadingText + _this2.ellipsis;
182
- if (_this2.checkTruncationOffsetWidth(widthCheck) === false) {
130
+ var widthCheck = leadingText + _this.ellipsis;
131
+ if (_this.checkTruncationOffsetWidth(widthCheck) === false) {
183
132
  truncatedText = leadingText;
184
133
  leadingText = '';
185
134
  }
@@ -187,30 +136,30 @@ var TruncationUtils = /*#__PURE__*/function (_CanvasTextUtils) {
187
136
 
188
137
  // Get text width ratio width accounting for any truncation offset text,
189
138
  // and guesstimate an initial truncated string
190
- _this2.setTextWidthRatio(truncatedText, leadingText);
191
- truncatedText = _this2.getTextFromRatio(truncatedText, 'start');
192
- leadingText += _this2.ellipsis;
193
- _this2.setTextToCheck(combinedText());
194
- while (_this2.textWidth > _this2.availableWidth) {
139
+ _this.setTextWidthRatio(truncatedText, leadingText);
140
+ truncatedText = _this.getTextFromRatio(truncatedText, 'start');
141
+ leadingText += _this.ellipsis;
142
+ _this.setTextToCheck(combinedText());
143
+ while (_this.textWidth > _this.availableWidth) {
195
144
  truncatedText = removeFirstCharacter(truncatedText);
196
- _this2.setTextToCheck(combinedText());
145
+ _this.setTextToCheck(combinedText());
197
146
  }
198
147
  return combinedText();
199
148
  });
200
- _defineProperty(_assertThisInitialized(_this2), "truncateEnd", function (truncationOffset) {
201
- var truncatedText = _this2.fullText;
149
+ _defineProperty(_assertThisInitialized(_this), "truncateEnd", function (truncationOffset) {
150
+ var truncatedText = _this.fullText;
202
151
  var trailingText = '';
203
152
  var combinedText = function combinedText() {
204
153
  return truncatedText + trailingText;
205
154
  };
206
155
  if (truncationOffset) {
207
- var index = _this2.fullText.length - truncationOffset;
208
- var _splitText$at5 = splitText(_this2.fullText).at(index);
156
+ var index = _this.fullText.length - truncationOffset;
157
+ var _splitText$at5 = splitText(_this.fullText).at(index);
209
158
  var _splitText$at6 = _slicedToArray(_splitText$at5, 2);
210
159
  truncatedText = _splitText$at6[0];
211
160
  trailingText = _splitText$at6[1];
212
- var widthCheck = _this2.ellipsis + trailingText;
213
- if (_this2.checkTruncationOffsetWidth(widthCheck) === false) {
161
+ var widthCheck = _this.ellipsis + trailingText;
162
+ if (_this.checkTruncationOffsetWidth(widthCheck) === false) {
214
163
  truncatedText = trailingText;
215
164
  trailingText = '';
216
165
  }
@@ -218,78 +167,78 @@ var TruncationUtils = /*#__PURE__*/function (_CanvasTextUtils) {
218
167
 
219
168
  // Get text width ratio width accounting for any truncation offset text,
220
169
  // and guesstimate an initial truncated string
221
- _this2.setTextWidthRatio(truncatedText, trailingText);
222
- truncatedText = _this2.getTextFromRatio(truncatedText, 'end');
223
- trailingText = _this2.ellipsis + trailingText;
224
- _this2.setTextToCheck(combinedText());
225
- while (_this2.textWidth > _this2.availableWidth) {
170
+ _this.setTextWidthRatio(truncatedText, trailingText);
171
+ truncatedText = _this.getTextFromRatio(truncatedText, 'end');
172
+ trailingText = _this.ellipsis + trailingText;
173
+ _this.setTextToCheck(combinedText());
174
+ while (_this.textWidth > _this.availableWidth) {
226
175
  truncatedText = removeLastCharacter(truncatedText);
227
- _this2.setTextToCheck(combinedText());
176
+ _this.setTextToCheck(combinedText());
228
177
  }
229
178
  return combinedText();
230
179
  });
231
- _defineProperty(_assertThisInitialized(_this2), "truncateStartEndAtPosition", function (truncationPosition) {
180
+ _defineProperty(_assertThisInitialized(_this), "truncateStartEndAtPosition", function (truncationPosition) {
232
181
  // Split the text from the anchor position, using the width ratio
233
182
  // to get the starting and ending indices from the position
234
- _this2.setTextWidthRatio();
235
- var characterRatio = Math.floor(_this2.fullText.length * _this2.widthRatio / 2);
183
+ _this.setTextWidthRatio();
184
+ var characterRatio = Math.floor(_this.fullText.length * _this.widthRatio / 2);
236
185
  var truncateStart = truncationPosition - characterRatio;
237
186
  var truncateEnd = truncationPosition + characterRatio;
238
187
 
239
188
  // If either of the approximate start/end truncation indices go beyond the
240
189
  // bounds of the actual text, we can simply use end or start truncation instead
241
190
  if (truncateStart < 0) {
242
- return _this2.truncateEnd();
191
+ return _this.truncateEnd();
243
192
  }
244
- if (truncateEnd >= _this2.fullText.length) {
245
- return _this2.truncateStart();
193
+ if (truncateEnd >= _this.fullText.length) {
194
+ return _this.truncateStart();
246
195
  }
247
- var truncatedText = _this2.fullText.substring(truncateStart, truncateEnd);
196
+ var truncatedText = _this.fullText.substring(truncateStart, truncateEnd);
248
197
  var combinedText = function combinedText() {
249
- return _this2.ellipsis + truncatedText + _this2.ellipsis;
198
+ return _this.ellipsis + truncatedText + _this.ellipsis;
250
199
  };
251
- _this2.setTextToCheck(combinedText());
200
+ _this.setTextToCheck(combinedText());
252
201
  var alternating;
253
- while (_this2.textWidth > _this2.availableWidth) {
202
+ while (_this.textWidth > _this.availableWidth) {
254
203
  truncatedText = alternating ? removeLastCharacter(truncatedText) : removeFirstCharacter(truncatedText);
255
204
  alternating = !alternating;
256
- _this2.setTextToCheck(combinedText());
205
+ _this.setTextToCheck(combinedText());
257
206
  }
258
207
  return combinedText();
259
208
  });
260
- _defineProperty(_assertThisInitialized(_this2), "truncateStartEndAtMiddle", function () {
261
- var middlePosition = Math.floor(_this2.fullText.length / 2);
262
- return _this2.truncateStartEndAtPosition(middlePosition);
209
+ _defineProperty(_assertThisInitialized(_this), "truncateStartEndAtMiddle", function () {
210
+ var middlePosition = Math.floor(_this.fullText.length / 2);
211
+ return _this.truncateStartEndAtPosition(middlePosition);
263
212
  });
264
- _defineProperty(_assertThisInitialized(_this2), "truncateMiddle", function () {
265
- var middlePosition = Math.floor(_this2.fullText.length / 2);
266
- var _splitText$at7 = splitText(_this2.fullText).at(middlePosition),
213
+ _defineProperty(_assertThisInitialized(_this), "truncateMiddle", function () {
214
+ var middlePosition = Math.floor(_this.fullText.length / 2);
215
+ var _splitText$at7 = splitText(_this.fullText).at(middlePosition),
267
216
  _splitText$at8 = _slicedToArray(_splitText$at7, 2),
268
217
  firstHalf = _splitText$at8[0],
269
218
  secondHalf = _splitText$at8[1];
270
- _this2.setTextWidthRatio();
271
- firstHalf = _this2.getTextFromRatio(firstHalf, 'end');
272
- secondHalf = _this2.getTextFromRatio(secondHalf, 'start');
219
+ _this.setTextWidthRatio();
220
+ firstHalf = _this.getTextFromRatio(firstHalf, 'end');
221
+ secondHalf = _this.getTextFromRatio(secondHalf, 'start');
273
222
  var combinedText = function combinedText() {
274
- return firstHalf + _this2.ellipsis + secondHalf;
223
+ return firstHalf + _this.ellipsis + secondHalf;
275
224
  };
276
- _this2.setTextToCheck(combinedText());
225
+ _this.setTextToCheck(combinedText());
277
226
  var alternating;
278
- while (_this2.textWidth > _this2.availableWidth) {
227
+ while (_this.textWidth > _this.availableWidth) {
279
228
  alternating = !alternating;
280
229
  if (alternating) {
281
230
  firstHalf = removeLastCharacter(firstHalf);
282
231
  } else {
283
232
  secondHalf = removeFirstCharacter(secondHalf);
284
233
  }
285
- _this2.setTextToCheck(combinedText());
234
+ _this.setTextToCheck(combinedText());
286
235
  }
287
236
  return combinedText();
288
237
  });
289
- _this2.fullText = fullText;
290
- _this2.ellipsis = ellipsis;
291
- _this2.availableWidth = _availableWidth;
292
- return _this2;
238
+ _this.fullText = fullText;
239
+ _this.ellipsis = ellipsis;
240
+ _this.availableWidth = _availableWidth;
241
+ return _this;
293
242
  }
294
243
  _createClass(TruncationUtils, [{
295
244
  key: "textWidth",
@@ -301,7 +250,7 @@ var TruncationUtils = /*#__PURE__*/function (_CanvasTextUtils) {
301
250
  }
302
251
  }]);
303
252
  return TruncationUtils;
304
- }(CanvasTextUtils);
253
+ }(_canvas.CanvasTextUtils);
305
254
  /**
306
255
  * DRY character/substring utils
307
256
  */
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.CanvasTextUtils = void 0;
7
+ 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); }
8
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
9
+ 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); } }
10
+ 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
+ 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; }
12
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
13
+ 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); }
14
+ /*
15
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
16
+ * or more contributor license agreements. Licensed under the Elastic License
17
+ * 2.0 and the Server Side Public License, v 1; you may not use this file except
18
+ * in compliance with, at your election, the Elastic License 2.0 or the Server
19
+ * Side Public License, v 1.
20
+ */
21
+ /**
22
+ * Creates a temporary Canvas element for manipulating text & determining text width.
23
+ *
24
+ * To accurately measure text, canvas rendering requires either a container to
25
+ * compute/derive font styles from, or a static font string (useful for usage
26
+ * outside the DOM). Particular care should be applied when fallback fonts are
27
+ * used, as more fallback fonts can lead to less precision.
28
+ *
29
+ * Please note that while canvas is more significantly more performant than DOM
30
+ * measurement, there are subpixel to single digit pixel differences between
31
+ * DOM and canvas measurement due to the different rendering engines used.
32
+ */
33
+ var CanvasTextUtils = /*#__PURE__*/function () {
34
+ function CanvasTextUtils(_ref) {
35
+ var _this = this;
36
+ var font = _ref.font,
37
+ container = _ref.container;
38
+ _classCallCheck(this, CanvasTextUtils);
39
+ _defineProperty(this, "context", void 0);
40
+ _defineProperty(this, "currentText", '');
41
+ _defineProperty(this, "computeFontFromElement", function (element) {
42
+ var computedStyles = window.getComputedStyle(element);
43
+ // TODO: font-stretch is not included even though it potentially should be
44
+ // @see https://developer.mozilla.org/en-US/docs/Web/CSS/font#constituent_properties
45
+ // It appears to be unsupported and/or breaks font computation in canvas
46
+ return ['font-style', 'font-variant', 'font-weight', 'font-size', 'font-family'].map(function (prop) {
47
+ return computedStyles.getPropertyValue(prop);
48
+ }).join(' ').trim();
49
+ });
50
+ _defineProperty(this, "setTextToCheck", function (text) {
51
+ _this.currentText = text;
52
+ });
53
+ this.context = document.createElement('canvas').getContext('2d');
54
+
55
+ // Set the canvas font to ensure text width calculations are correct
56
+ if (font) {
57
+ this.context.font = font;
58
+ } else if (container) {
59
+ this.context.font = this.computeFontFromElement(container);
60
+ }
61
+ }
62
+ _createClass(CanvasTextUtils, [{
63
+ key: "textWidth",
64
+ get: function get() {
65
+ return this.context.measureText(this.currentText).width;
66
+ }
67
+ }]);
68
+ return CanvasTextUtils;
69
+ }();
70
+ 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");
@@ -21,8 +21,7 @@ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Re
21
21
 
22
22
  import React, { Component } from 'react';
23
23
  import classNames from 'classnames';
24
- import { keys } from '../../../services';
25
- import { CanvasTextUtils } from '../../text_truncate';
24
+ import { keys, CanvasTextUtils } from '../../../services';
26
25
  import { EuiScreenReaderOnly } from '../../accessibility';
27
26
  import { EuiFormControlLayout } from '../../form/form_control_layout';
28
27
  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';