@elastic/eui 88.5.0 → 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.
- package/dist/eui_theme_dark.css +8 -21
- package/dist/eui_theme_dark.min.css +1 -1
- package/dist/eui_theme_light.css +8 -21
- package/dist/eui_theme_light.min.css +1 -1
- package/es/components/collapsible_nav_beta/collapsible_nav_beta.js +20 -6
- package/es/components/collapsible_nav_beta/collapsible_nav_beta.styles.js +4 -14
- package/es/components/collapsible_nav_beta/collapsible_nav_body_footer.js +57 -0
- package/es/components/collapsible_nav_beta/collapsible_nav_body_footer.styles.js +25 -0
- package/es/components/collapsible_nav_beta/collapsible_nav_group/collapsible_nav_group.js +156 -0
- package/es/components/collapsible_nav_beta/collapsible_nav_group/collapsible_nav_group.styles.js +19 -0
- package/es/components/collapsible_nav_beta/collapsible_nav_group/index.js +9 -0
- package/es/components/collapsible_nav_beta/collapsible_nav_item/collapsed/collapsed_nav_button.js +3 -7
- package/es/components/collapsible_nav_beta/collapsible_nav_item/collapsed/collapsed_nav_item.js +3 -7
- package/es/components/collapsible_nav_beta/collapsible_nav_item/collapsed/collapsed_nav_popover.js +4 -8
- package/es/components/collapsible_nav_beta/collapsible_nav_item/collapsible_nav_accordion.js +16 -63
- package/es/components/collapsible_nav_beta/collapsible_nav_item/collapsible_nav_accordion.styles.js +2 -10
- package/es/components/collapsible_nav_beta/collapsible_nav_item/collapsible_nav_item.js +122 -41
- package/es/components/collapsible_nav_beta/collapsible_nav_item/collapsible_nav_item.styles.js +13 -3
- package/es/components/combo_box/combo_box.js +12 -25
- package/es/components/combo_box/combo_box_input/combo_box_input.js +61 -54
- package/es/components/text_truncate/index.js +1 -1
- 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 +186 -105
- package/i18ntokens.json +8 -8
- package/lib/components/collapsible_nav_beta/collapsible_nav_beta.js +20 -6
- package/lib/components/collapsible_nav_beta/collapsible_nav_beta.styles.js +11 -19
- package/lib/components/collapsible_nav_beta/collapsible_nav_body_footer.js +67 -0
- package/lib/components/collapsible_nav_beta/collapsible_nav_body_footer.styles.js +33 -0
- package/lib/components/collapsible_nav_beta/collapsible_nav_group/collapsible_nav_group.js +166 -0
- package/lib/components/collapsible_nav_beta/collapsible_nav_group/collapsible_nav_group.styles.js +26 -0
- package/lib/components/collapsible_nav_beta/collapsible_nav_group/index.js +12 -0
- package/lib/components/collapsible_nav_beta/collapsible_nav_item/collapsed/collapsed_nav_button.js +3 -7
- package/lib/components/collapsible_nav_beta/collapsible_nav_item/collapsed/collapsed_nav_item.js +3 -7
- package/lib/components/collapsible_nav_beta/collapsible_nav_item/collapsed/collapsed_nav_popover.js +4 -8
- package/lib/components/collapsible_nav_beta/collapsible_nav_item/collapsible_nav_accordion.js +14 -61
- package/lib/components/collapsible_nav_beta/collapsible_nav_item/collapsible_nav_accordion.styles.js +1 -9
- package/lib/components/collapsible_nav_beta/collapsible_nav_item/collapsible_nav_item.js +122 -39
- package/lib/components/collapsible_nav_beta/collapsible_nav_item/collapsible_nav_item.styles.js +13 -4
- package/lib/components/combo_box/combo_box.js +12 -25
- package/lib/components/combo_box/combo_box_input/combo_box_input.js +60 -53
- package/lib/components/text_truncate/index.js +0 -6
- 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/collapsible_nav_beta/collapsible_nav_beta.js +17 -3
- package/optimize/es/components/collapsible_nav_beta/collapsible_nav_beta.styles.js +4 -14
- package/optimize/es/components/collapsible_nav_beta/collapsible_nav_body_footer.js +56 -0
- package/optimize/es/components/collapsible_nav_beta/collapsible_nav_body_footer.styles.js +25 -0
- package/optimize/es/components/collapsible_nav_beta/collapsible_nav_group/collapsible_nav_group.js +48 -0
- package/optimize/es/components/collapsible_nav_beta/collapsible_nav_group/collapsible_nav_group.styles.js +19 -0
- package/optimize/es/components/collapsible_nav_beta/collapsible_nav_group/index.js +9 -0
- package/optimize/es/components/collapsible_nav_beta/collapsible_nav_item/collapsible_nav_accordion.js +7 -33
- package/optimize/es/components/collapsible_nav_beta/collapsible_nav_item/collapsible_nav_accordion.styles.js +2 -10
- package/optimize/es/components/collapsible_nav_beta/collapsible_nav_item/collapsible_nav_item.js +54 -21
- package/optimize/es/components/collapsible_nav_beta/collapsible_nav_item/collapsible_nav_item.styles.js +13 -3
- package/optimize/es/components/combo_box/combo_box.js +12 -25
- package/optimize/es/components/combo_box/combo_box_input/combo_box_input.js +61 -53
- package/optimize/es/components/text_truncate/index.js +1 -1
- 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/collapsible_nav_beta/collapsible_nav_beta.js +15 -1
- package/optimize/lib/components/collapsible_nav_beta/collapsible_nav_beta.styles.js +11 -19
- package/optimize/lib/components/collapsible_nav_beta/collapsible_nav_body_footer.js +66 -0
- package/optimize/lib/components/collapsible_nav_beta/collapsible_nav_body_footer.styles.js +33 -0
- package/optimize/lib/components/collapsible_nav_beta/collapsible_nav_group/collapsible_nav_group.js +58 -0
- package/optimize/lib/components/collapsible_nav_beta/collapsible_nav_group/collapsible_nav_group.styles.js +26 -0
- package/optimize/lib/components/collapsible_nav_beta/collapsible_nav_group/index.js +12 -0
- package/optimize/lib/components/collapsible_nav_beta/collapsible_nav_item/collapsible_nav_accordion.js +5 -31
- package/optimize/lib/components/collapsible_nav_beta/collapsible_nav_item/collapsible_nav_accordion.styles.js +1 -9
- package/optimize/lib/components/collapsible_nav_beta/collapsible_nav_item/collapsible_nav_item.js +54 -20
- package/optimize/lib/components/collapsible_nav_beta/collapsible_nav_item/collapsible_nav_item.styles.js +13 -4
- package/optimize/lib/components/combo_box/combo_box.js +12 -25
- package/optimize/lib/components/combo_box/combo_box_input/combo_box_input.js +60 -52
- package/optimize/lib/components/text_truncate/index.js +0 -6
- 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 +2 -5
- package/src/components/combo_box/_combo_box.scss +12 -19
- package/test-env/components/collapsible_nav_beta/collapsible_nav_beta.js +20 -6
- package/test-env/components/collapsible_nav_beta/collapsible_nav_beta.styles.js +11 -19
- package/test-env/components/collapsible_nav_beta/collapsible_nav_body_footer.js +66 -0
- package/test-env/components/collapsible_nav_beta/collapsible_nav_body_footer.styles.js +33 -0
- package/test-env/components/collapsible_nav_beta/collapsible_nav_group/collapsible_nav_group.js +165 -0
- package/test-env/components/collapsible_nav_beta/collapsible_nav_group/collapsible_nav_group.styles.js +26 -0
- package/test-env/components/collapsible_nav_beta/collapsible_nav_group/index.js +12 -0
- package/test-env/components/collapsible_nav_beta/collapsible_nav_item/collapsed/collapsed_nav_button.js +3 -7
- package/test-env/components/collapsible_nav_beta/collapsible_nav_item/collapsed/collapsed_nav_item.js +3 -7
- package/test-env/components/collapsible_nav_beta/collapsible_nav_item/collapsed/collapsed_nav_popover.js +4 -8
- package/test-env/components/collapsible_nav_beta/collapsible_nav_item/collapsible_nav_accordion.js +14 -61
- package/test-env/components/collapsible_nav_beta/collapsible_nav_item/collapsible_nav_accordion.styles.js +1 -9
- package/test-env/components/collapsible_nav_beta/collapsible_nav_item/collapsible_nav_item.js +122 -39
- package/test-env/components/collapsible_nav_beta/collapsible_nav_item/collapsible_nav_item.styles.js +13 -4
- package/test-env/components/combo_box/combo_box.js +12 -25
- package/test-env/components/combo_box/combo_box_input/combo_box_input.js +60 -53
- package/test-env/components/text_truncate/index.js +0 -6
- package/test-env/components/text_truncate/utils.js +96 -147
- 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
|
|
31
|
-
import { keys } from '../../../services';
|
|
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';
|
|
@@ -45,8 +44,27 @@ export var EuiComboBoxInput = /*#__PURE__*/function (_Component) {
|
|
|
45
44
|
}
|
|
46
45
|
_this = _super.call.apply(_super, [this].concat(args));
|
|
47
46
|
_defineProperty(_assertThisInitialized(_this), "state", {
|
|
47
|
+
inputWidth: 2,
|
|
48
48
|
hasFocus: false
|
|
49
49
|
});
|
|
50
|
+
_defineProperty(_assertThisInitialized(_this), "widthUtils", void 0);
|
|
51
|
+
_defineProperty(_assertThisInitialized(_this), "inputRefCallback", function (el) {
|
|
52
|
+
var _this$props$inputRef, _this$props;
|
|
53
|
+
_this.widthUtils = new CanvasTextUtils({
|
|
54
|
+
container: el
|
|
55
|
+
});
|
|
56
|
+
(_this$props$inputRef = (_this$props = _this.props).inputRef) === null || _this$props$inputRef === void 0 ? void 0 : _this$props$inputRef.call(_this$props, el);
|
|
57
|
+
});
|
|
58
|
+
_defineProperty(_assertThisInitialized(_this), "updateInputSize", function (inputValue) {
|
|
59
|
+
if (!_this.widthUtils) return;
|
|
60
|
+
_this.widthUtils.setTextToCheck(inputValue);
|
|
61
|
+
// Canvas has minute subpixel differences in rendering compared to DOM
|
|
62
|
+
// We'll buffer the input by ~2px just to ensure sufficient width
|
|
63
|
+
var inputWidth = Math.ceil(_this.widthUtils.textWidth) + 2;
|
|
64
|
+
_this.setState({
|
|
65
|
+
inputWidth: inputWidth
|
|
66
|
+
});
|
|
67
|
+
});
|
|
50
68
|
_defineProperty(_assertThisInitialized(_this), "updatePosition", function () {
|
|
51
69
|
// Wait a beat for the DOM to update, since we depend on DOM elements' bounds.
|
|
52
70
|
requestAnimationFrame(function () {
|
|
@@ -68,13 +86,13 @@ export var EuiComboBoxInput = /*#__PURE__*/function (_Component) {
|
|
|
68
86
|
});
|
|
69
87
|
});
|
|
70
88
|
_defineProperty(_assertThisInitialized(_this), "onKeyDown", function (event) {
|
|
71
|
-
var _this$
|
|
72
|
-
searchValue = _this$
|
|
73
|
-
selectedOptions = _this$
|
|
74
|
-
onRemoveOption = _this$
|
|
75
|
-
singleSelection = _this$
|
|
76
|
-
isListOpen = _this$
|
|
77
|
-
onOpenListClick = _this$
|
|
89
|
+
var _this$props2 = _this.props,
|
|
90
|
+
searchValue = _this$props2.searchValue,
|
|
91
|
+
selectedOptions = _this$props2.selectedOptions,
|
|
92
|
+
onRemoveOption = _this$props2.onRemoveOption,
|
|
93
|
+
singleSelection = _this$props2.singleSelection,
|
|
94
|
+
isListOpen = _this$props2.isListOpen,
|
|
95
|
+
onOpenListClick = _this$props2.onOpenListClick;
|
|
78
96
|
|
|
79
97
|
// When backspacing from an empty input, delete the last pill option in the list
|
|
80
98
|
var searchIsEmpty = !searchValue.length;
|
|
@@ -87,18 +105,10 @@ export var EuiComboBoxInput = /*#__PURE__*/function (_Component) {
|
|
|
87
105
|
}
|
|
88
106
|
});
|
|
89
107
|
_defineProperty(_assertThisInitialized(_this), "inputOnChange", function (event) {
|
|
90
|
-
var _this$
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
onChange(event.target.value);
|
|
95
|
-
}
|
|
96
|
-
});
|
|
97
|
-
_defineProperty(_assertThisInitialized(_this), "inputRefCallback", function (ref) {
|
|
98
|
-
var autoSizeInputRef = _this.props.autoSizeInputRef;
|
|
99
|
-
if (autoSizeInputRef) {
|
|
100
|
-
autoSizeInputRef(ref);
|
|
101
|
-
}
|
|
108
|
+
var _this$props$onChange, _this$props3;
|
|
109
|
+
var value = event.target.value;
|
|
110
|
+
_this.updateInputSize(value);
|
|
111
|
+
(_this$props$onChange = (_this$props3 = _this.props).onChange) === null || _this$props$onChange === void 0 ? void 0 : _this$props$onChange.call(_this$props3, value);
|
|
102
112
|
});
|
|
103
113
|
return _this;
|
|
104
114
|
}
|
|
@@ -116,35 +126,34 @@ export var EuiComboBoxInput = /*#__PURE__*/function (_Component) {
|
|
|
116
126
|
}, {
|
|
117
127
|
key: "render",
|
|
118
128
|
value: function render() {
|
|
119
|
-
var _this$
|
|
120
|
-
compressed = _this$
|
|
121
|
-
focusedOptionId = _this$
|
|
122
|
-
fullWidth = _this$
|
|
123
|
-
hasSelectedOptions = _this$
|
|
124
|
-
id = _this$
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
ariaLabelledby = _this$props3['aria-labelledby'];
|
|
129
|
+
var _this$props4 = this.props,
|
|
130
|
+
compressed = _this$props4.compressed,
|
|
131
|
+
focusedOptionId = _this$props4.focusedOptionId,
|
|
132
|
+
fullWidth = _this$props4.fullWidth,
|
|
133
|
+
hasSelectedOptions = _this$props4.hasSelectedOptions,
|
|
134
|
+
id = _this$props4.id,
|
|
135
|
+
isDisabled = _this$props4.isDisabled,
|
|
136
|
+
isListOpen = _this$props4.isListOpen,
|
|
137
|
+
noIcon = _this$props4.noIcon,
|
|
138
|
+
onClear = _this$props4.onClear,
|
|
139
|
+
onClick = _this$props4.onClick,
|
|
140
|
+
onCloseListClick = _this$props4.onCloseListClick,
|
|
141
|
+
onOpenListClick = _this$props4.onOpenListClick,
|
|
142
|
+
onRemoveOption = _this$props4.onRemoveOption,
|
|
143
|
+
placeholder = _this$props4.placeholder,
|
|
144
|
+
rootId = _this$props4.rootId,
|
|
145
|
+
searchValue = _this$props4.searchValue,
|
|
146
|
+
selectedOptions = _this$props4.selectedOptions,
|
|
147
|
+
singleSelectionProp = _this$props4.singleSelection,
|
|
148
|
+
toggleButtonRef = _this$props4.toggleButtonRef,
|
|
149
|
+
value = _this$props4.value,
|
|
150
|
+
prepend = _this$props4.prepend,
|
|
151
|
+
append = _this$props4.append,
|
|
152
|
+
isLoading = _this$props4.isLoading,
|
|
153
|
+
isInvalid = _this$props4.isInvalid,
|
|
154
|
+
autoFocus = _this$props4.autoFocus,
|
|
155
|
+
ariaLabel = _this$props4['aria-label'],
|
|
156
|
+
ariaLabelledby = _this$props4['aria-labelledby'];
|
|
148
157
|
var singleSelection = Boolean(singleSelectionProp);
|
|
149
158
|
var asPlainText = singleSelectionProp && _typeof(singleSelectionProp) === 'object' && singleSelectionProp.asPlainText || false;
|
|
150
159
|
var pills = selectedOptions ? selectedOptions.map(function (option) {
|
|
@@ -236,7 +245,7 @@ export var EuiComboBoxInput = /*#__PURE__*/function (_Component) {
|
|
|
236
245
|
"data-test-subj": "comboBoxInput",
|
|
237
246
|
onClick: onClick,
|
|
238
247
|
tabIndex: -1 // becomes onBlur event's relatedTarget, otherwise relatedTarget is null when clicking on this div
|
|
239
|
-
}, !singleSelection || !searchValue ? pills : null, placeholderMessage, ___EmotionJSX(
|
|
248
|
+
}, !singleSelection || !searchValue ? pills : null, placeholderMessage, ___EmotionJSX("input", {
|
|
240
249
|
"aria-activedescendant": focusedOptionId,
|
|
241
250
|
"aria-autocomplete": "list",
|
|
242
251
|
"aria-controls": isListOpen ? rootId('listbox') : '',
|
|
@@ -248,7 +257,6 @@ export var EuiComboBoxInput = /*#__PURE__*/function (_Component) {
|
|
|
248
257
|
"data-test-subj": "comboBoxSearchInput",
|
|
249
258
|
disabled: isDisabled,
|
|
250
259
|
id: id,
|
|
251
|
-
inputRef: inputRef,
|
|
252
260
|
onBlur: this.onBlur,
|
|
253
261
|
onChange: this.inputOnChange,
|
|
254
262
|
onFocus: this.onFocus,
|
|
@@ -256,7 +264,7 @@ export var EuiComboBoxInput = /*#__PURE__*/function (_Component) {
|
|
|
256
264
|
ref: this.inputRefCallback,
|
|
257
265
|
role: "combobox",
|
|
258
266
|
style: {
|
|
259
|
-
|
|
267
|
+
inlineSize: this.state.inputWidth
|
|
260
268
|
},
|
|
261
269
|
value: searchValue,
|
|
262
270
|
autoFocus: autoFocus
|
|
@@ -266,7 +274,6 @@ export var EuiComboBoxInput = /*#__PURE__*/function (_Component) {
|
|
|
266
274
|
return EuiComboBoxInput;
|
|
267
275
|
}(Component);
|
|
268
276
|
EuiComboBoxInput.propTypes = {
|
|
269
|
-
autoSizeInputRef: PropTypes.any,
|
|
270
277
|
compressed: PropTypes.bool.isRequired,
|
|
271
278
|
focusedOptionId: PropTypes.string,
|
|
272
279
|
fullWidth: PropTypes.bool,
|
|
@@ -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';
|