@elastic/eui 88.5.1 → 88.5.3
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.
- package/es/components/combo_box/combo_box_input/combo_box_input.js +39 -43
- package/es/components/text_truncate/index.js +1 -1
- package/es/components/text_truncate/text_truncate.js +5 -2
- package/es/components/text_truncate/utils.js +88 -138
- package/es/services/canvas/canvas_text_utils.js +64 -0
- package/es/services/canvas/index.js +9 -0
- package/es/services/index.js +1 -0
- package/eui.d.ts +40 -31
- package/lib/components/combo_box/combo_box_input/combo_box_input.js +39 -43
- package/lib/components/text_truncate/index.js +0 -6
- package/lib/components/text_truncate/text_truncate.js +5 -2
- package/lib/components/text_truncate/utils.js +97 -148
- package/lib/services/canvas/canvas_text_utils.js +70 -0
- package/lib/services/canvas/index.js +12 -0
- package/lib/services/index.js +8 -0
- package/optimize/es/components/combo_box/combo_box_input/combo_box_input.js +37 -41
- package/optimize/es/components/text_truncate/index.js +1 -1
- package/optimize/es/components/text_truncate/text_truncate.js +5 -2
- package/optimize/es/components/text_truncate/utils.js +87 -137
- package/optimize/es/services/canvas/canvas_text_utils.js +60 -0
- package/optimize/es/services/canvas/index.js +9 -0
- package/optimize/es/services/index.js +1 -0
- package/optimize/lib/components/combo_box/combo_box_input/combo_box_input.js +37 -41
- package/optimize/lib/components/text_truncate/index.js +0 -6
- package/optimize/lib/components/text_truncate/text_truncate.js +5 -2
- package/optimize/lib/components/text_truncate/utils.js +96 -147
- package/optimize/lib/services/canvas/canvas_text_utils.js +67 -0
- package/optimize/lib/services/canvas/index.js +12 -0
- package/optimize/lib/services/index.js +8 -0
- package/package.json +6 -5
- package/test-env/components/combo_box/combo_box_input/combo_box_input.js +39 -43
- package/test-env/components/text_truncate/index.js +0 -6
- package/test-env/components/text_truncate/text_truncate.js +5 -2
- package/test-env/components/text_truncate/utils.js +232 -38
- package/test-env/services/canvas/canvas_text_utils.js +30 -0
- package/test-env/services/canvas/index.js +12 -0
- 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';
|
|
@@ -105,56 +104,51 @@ export var EuiComboBoxInput = /*#__PURE__*/function (_Component) {
|
|
|
105
104
|
}
|
|
106
105
|
}
|
|
107
106
|
});
|
|
108
|
-
_defineProperty(_assertThisInitialized(_this), "inputOnChange", function (event) {
|
|
109
|
-
var _this$props$onChange, _this$props3;
|
|
110
|
-
var value = event.target.value;
|
|
111
|
-
_this.updateInputSize(value);
|
|
112
|
-
(_this$props$onChange = (_this$props3 = _this.props).onChange) === null || _this$props$onChange === void 0 ? void 0 : _this$props$onChange.call(_this$props3, value);
|
|
113
|
-
});
|
|
114
107
|
return _this;
|
|
115
108
|
}
|
|
116
109
|
_createClass(EuiComboBoxInput, [{
|
|
117
110
|
key: "componentDidUpdate",
|
|
118
111
|
value: function componentDidUpdate(prevProps) {
|
|
119
|
-
|
|
112
|
+
if (prevProps.searchValue !== this.props.searchValue) {
|
|
113
|
+
this.updateInputSize(this.props.searchValue);
|
|
120
114
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
if (searchValue !== this.props.searchValue) {
|
|
115
|
+
// We need to update the position of everything if the user enters enough input to change
|
|
116
|
+
// the size of the input.
|
|
124
117
|
this.updatePosition();
|
|
125
118
|
}
|
|
126
119
|
}
|
|
127
120
|
}, {
|
|
128
121
|
key: "render",
|
|
129
122
|
value: function render() {
|
|
130
|
-
var _this$
|
|
131
|
-
compressed = _this$
|
|
132
|
-
focusedOptionId = _this$
|
|
133
|
-
fullWidth = _this$
|
|
134
|
-
hasSelectedOptions = _this$
|
|
135
|
-
id = _this$
|
|
136
|
-
isDisabled = _this$
|
|
137
|
-
isListOpen = _this$
|
|
138
|
-
noIcon = _this$
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
123
|
+
var _this$props3 = this.props,
|
|
124
|
+
compressed = _this$props3.compressed,
|
|
125
|
+
focusedOptionId = _this$props3.focusedOptionId,
|
|
126
|
+
fullWidth = _this$props3.fullWidth,
|
|
127
|
+
hasSelectedOptions = _this$props3.hasSelectedOptions,
|
|
128
|
+
id = _this$props3.id,
|
|
129
|
+
isDisabled = _this$props3.isDisabled,
|
|
130
|
+
isListOpen = _this$props3.isListOpen,
|
|
131
|
+
noIcon = _this$props3.noIcon,
|
|
132
|
+
_onChange = _this$props3.onChange,
|
|
133
|
+
onClear = _this$props3.onClear,
|
|
134
|
+
onClick = _this$props3.onClick,
|
|
135
|
+
onCloseListClick = _this$props3.onCloseListClick,
|
|
136
|
+
onOpenListClick = _this$props3.onOpenListClick,
|
|
137
|
+
onRemoveOption = _this$props3.onRemoveOption,
|
|
138
|
+
placeholder = _this$props3.placeholder,
|
|
139
|
+
rootId = _this$props3.rootId,
|
|
140
|
+
searchValue = _this$props3.searchValue,
|
|
141
|
+
selectedOptions = _this$props3.selectedOptions,
|
|
142
|
+
singleSelectionProp = _this$props3.singleSelection,
|
|
143
|
+
toggleButtonRef = _this$props3.toggleButtonRef,
|
|
144
|
+
value = _this$props3.value,
|
|
145
|
+
prepend = _this$props3.prepend,
|
|
146
|
+
append = _this$props3.append,
|
|
147
|
+
isLoading = _this$props3.isLoading,
|
|
148
|
+
isInvalid = _this$props3.isInvalid,
|
|
149
|
+
autoFocus = _this$props3.autoFocus,
|
|
150
|
+
ariaLabel = _this$props3['aria-label'],
|
|
151
|
+
ariaLabelledby = _this$props3['aria-labelledby'];
|
|
158
152
|
var singleSelection = Boolean(singleSelectionProp);
|
|
159
153
|
var asPlainText = singleSelectionProp && _typeof(singleSelectionProp) === 'object' && singleSelectionProp.asPlainText || false;
|
|
160
154
|
var pills = selectedOptions ? selectedOptions.map(function (option) {
|
|
@@ -259,7 +253,9 @@ export var EuiComboBoxInput = /*#__PURE__*/function (_Component) {
|
|
|
259
253
|
disabled: isDisabled,
|
|
260
254
|
id: id,
|
|
261
255
|
onBlur: this.onBlur,
|
|
262
|
-
onChange:
|
|
256
|
+
onChange: function onChange(event) {
|
|
257
|
+
return _onChange(event.target.value);
|
|
258
|
+
},
|
|
263
259
|
onFocus: this.onFocus,
|
|
264
260
|
onKeyDown: this.onKeyDown,
|
|
265
261
|
ref: this.inputRefCallback,
|
|
@@ -285,9 +281,9 @@ EuiComboBoxInput.propTypes = {
|
|
|
285
281
|
isListOpen: PropTypes.bool.isRequired,
|
|
286
282
|
noIcon: PropTypes.bool.isRequired,
|
|
287
283
|
onBlur: PropTypes.any,
|
|
288
|
-
onChange: PropTypes.func,
|
|
284
|
+
onChange: PropTypes.func.isRequired,
|
|
289
285
|
onClear: PropTypes.func,
|
|
290
|
-
onClick: PropTypes.func,
|
|
286
|
+
onClick: PropTypes.func.isRequired,
|
|
291
287
|
onCloseListClick: PropTypes.func.isRequired,
|
|
292
288
|
onFocus: PropTypes.any.isRequired,
|
|
293
289
|
onOpenListClick: PropTypes.func.isRequired,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var _excluded = ["width"],
|
|
1
|
+
var _excluded = ["width", "onResize"],
|
|
2
2
|
_excluded2 = ["width", "children", "text", "truncation", "truncationOffset", "truncationPosition", "ellipsis", "containerRef", "className"],
|
|
3
3
|
_excluded3 = ["onResize"];
|
|
4
4
|
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
@@ -29,10 +29,13 @@ import { jsx as ___EmotionJSX } from "@emotion/react";
|
|
|
29
29
|
var TRUNCATION_TYPES = ['end', 'start', 'startEnd', 'middle'];
|
|
30
30
|
export var EuiTextTruncate = function EuiTextTruncate(_ref) {
|
|
31
31
|
var width = _ref.width,
|
|
32
|
+
onResize = _ref.onResize,
|
|
32
33
|
props = _objectWithoutProperties(_ref, _excluded);
|
|
33
34
|
return width != null ? ___EmotionJSX(EuiTextTruncateWithWidth, _extends({
|
|
34
35
|
width: width
|
|
35
|
-
}, props)) : ___EmotionJSX(EuiTextTruncateWithResizeObserver,
|
|
36
|
+
}, props)) : ___EmotionJSX(EuiTextTruncateWithResizeObserver, _extends({
|
|
37
|
+
onResize: onResize
|
|
38
|
+
}, props));
|
|
36
39
|
};
|
|
37
40
|
EuiTextTruncate.propTypes = {
|
|
38
41
|
className: PropTypes.string,
|
|
@@ -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(
|
|
93
|
-
var
|
|
94
|
-
var fullText =
|
|
95
|
-
ellipsis =
|
|
96
|
-
_availableWidth =
|
|
97
|
-
rest = _objectWithoutProperties(
|
|
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
|
-
|
|
100
|
-
_defineProperty(_assertThisInitialized(
|
|
101
|
-
_defineProperty(_assertThisInitialized(
|
|
102
|
-
_defineProperty(_assertThisInitialized(
|
|
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(
|
|
107
|
-
_defineProperty(_assertThisInitialized(
|
|
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(
|
|
116
|
-
_defineProperty(_assertThisInitialized(
|
|
117
|
-
var text = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] :
|
|
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 =
|
|
70
|
+
var availableWidth = _this.availableWidth;
|
|
121
71
|
if (textToOffset) {
|
|
122
|
-
|
|
123
|
-
availableWidth = availableWidth -
|
|
72
|
+
_this.setTextToCheck(textToOffset);
|
|
73
|
+
availableWidth = availableWidth - _this.textWidth;
|
|
124
74
|
}
|
|
125
|
-
|
|
126
|
-
|
|
75
|
+
_this.setTextToCheck(text);
|
|
76
|
+
_this.widthRatio = availableWidth / _this.textWidth;
|
|
127
77
|
});
|
|
128
|
-
_defineProperty(_assertThisInitialized(
|
|
129
|
-
var characterRatio = Math.ceil(text.length *
|
|
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(
|
|
141
|
-
|
|
142
|
-
if (
|
|
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(
|
|
147
|
-
var textToCheck = truncation === 'startEnd' ? "".concat(
|
|
148
|
-
:
|
|
149
|
-
|
|
150
|
-
if (
|
|
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(
|
|
156
|
-
|
|
157
|
-
if (
|
|
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(
|
|
166
|
-
var truncatedText =
|
|
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(
|
|
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 +
|
|
177
|
-
if (
|
|
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
|
-
|
|
186
|
-
truncatedText =
|
|
187
|
-
leadingText +=
|
|
188
|
-
|
|
189
|
-
while (
|
|
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
|
-
|
|
141
|
+
_this.setTextToCheck(combinedText());
|
|
192
142
|
}
|
|
193
143
|
return combinedText();
|
|
194
144
|
});
|
|
195
|
-
_defineProperty(_assertThisInitialized(
|
|
196
|
-
var truncatedText =
|
|
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 =
|
|
203
|
-
var _splitText$at5 = splitText(
|
|
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 =
|
|
208
|
-
if (
|
|
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
|
-
|
|
217
|
-
truncatedText =
|
|
218
|
-
trailingText =
|
|
219
|
-
|
|
220
|
-
while (
|
|
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
|
-
|
|
172
|
+
_this.setTextToCheck(combinedText());
|
|
223
173
|
}
|
|
224
174
|
return combinedText();
|
|
225
175
|
});
|
|
226
|
-
_defineProperty(_assertThisInitialized(
|
|
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
|
-
|
|
230
|
-
var characterRatio = Math.floor(
|
|
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
|
|
187
|
+
return _this.truncateEnd();
|
|
238
188
|
}
|
|
239
|
-
if (truncateEnd >=
|
|
240
|
-
return
|
|
189
|
+
if (truncateEnd >= _this.fullText.length) {
|
|
190
|
+
return _this.truncateStart();
|
|
241
191
|
}
|
|
242
|
-
var truncatedText =
|
|
192
|
+
var truncatedText = _this.fullText.substring(truncateStart, truncateEnd);
|
|
243
193
|
var combinedText = function combinedText() {
|
|
244
|
-
return
|
|
194
|
+
return _this.ellipsis + truncatedText + _this.ellipsis;
|
|
245
195
|
};
|
|
246
|
-
|
|
196
|
+
_this.setTextToCheck(combinedText());
|
|
247
197
|
var alternating;
|
|
248
|
-
while (
|
|
198
|
+
while (_this.textWidth > _this.availableWidth) {
|
|
249
199
|
truncatedText = alternating ? removeLastCharacter(truncatedText) : removeFirstCharacter(truncatedText);
|
|
250
200
|
alternating = !alternating;
|
|
251
|
-
|
|
201
|
+
_this.setTextToCheck(combinedText());
|
|
252
202
|
}
|
|
253
203
|
return combinedText();
|
|
254
204
|
});
|
|
255
|
-
_defineProperty(_assertThisInitialized(
|
|
256
|
-
var middlePosition = Math.floor(
|
|
257
|
-
return
|
|
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(
|
|
260
|
-
var middlePosition = Math.floor(
|
|
261
|
-
var _splitText$at7 = splitText(
|
|
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
|
-
|
|
266
|
-
firstHalf =
|
|
267
|
-
secondHalf =
|
|
215
|
+
_this.setTextWidthRatio();
|
|
216
|
+
firstHalf = _this.getTextFromRatio(firstHalf, 'end');
|
|
217
|
+
secondHalf = _this.getTextFromRatio(secondHalf, 'start');
|
|
268
218
|
var combinedText = function combinedText() {
|
|
269
|
-
return firstHalf +
|
|
219
|
+
return firstHalf + _this.ellipsis + secondHalf;
|
|
270
220
|
};
|
|
271
|
-
|
|
221
|
+
_this.setTextToCheck(combinedText());
|
|
272
222
|
var alternating;
|
|
273
|
-
while (
|
|
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
|
-
|
|
230
|
+
_this.setTextToCheck(combinedText());
|
|
281
231
|
}
|
|
282
232
|
return combinedText();
|
|
283
233
|
});
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
return
|
|
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';
|
package/es/services/index.js
CHANGED
|
@@ -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';
|