@instructure/ui-truncate-text 11.6.0 → 11.6.1-snapshot-129
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/CHANGELOG.md +29 -277
- package/es/TruncateText/{index.js → v1/index.js} +1 -1
- package/es/TruncateText/v2/index.js +264 -0
- package/es/TruncateText/v2/props.js +26 -0
- package/es/TruncateText/v2/styles.js +58 -0
- package/es/TruncateText/v2/utils/cleanData.js +158 -0
- package/es/TruncateText/v2/utils/cleanString.js +53 -0
- package/es/TruncateText/v2/utils/measureText.js +74 -0
- package/es/TruncateText/v2/utils/truncate.js +341 -0
- package/es/{index.js → exports/a.js} +1 -1
- package/{src/index.ts → es/exports/b.js} +1 -2
- package/lib/TruncateText/{index.js → v1/index.js} +1 -1
- package/lib/TruncateText/v2/index.js +271 -0
- package/lib/TruncateText/v2/props.js +31 -0
- package/lib/TruncateText/v2/styles.js +64 -0
- package/lib/TruncateText/v2/utils/cleanData.js +164 -0
- package/lib/TruncateText/v2/utils/cleanString.js +59 -0
- package/lib/TruncateText/v2/utils/measureText.js +79 -0
- package/lib/TruncateText/v2/utils/truncate.js +350 -0
- package/lib/{index.js → exports/a.js} +2 -2
- package/lib/exports/b.js +12 -0
- package/package.json +42 -20
- package/src/TruncateText/{index.tsx → v1/index.tsx} +1 -1
- package/src/TruncateText/v2/README.md +230 -0
- package/src/TruncateText/v2/index.tsx +314 -0
- package/src/TruncateText/v2/props.ts +113 -0
- package/src/TruncateText/v2/styles.ts +60 -0
- package/src/TruncateText/v2/utils/cleanData.ts +178 -0
- package/src/TruncateText/v2/utils/cleanString.ts +61 -0
- package/src/TruncateText/v2/utils/measureText.ts +86 -0
- package/src/TruncateText/v2/utils/truncate.ts +451 -0
- package/src/exports/a.ts +25 -0
- package/src/exports/b.ts +25 -0
- package/tsconfig.build.tsbuildinfo +1 -1
- package/types/TruncateText/v1/index.d.ts.map +1 -0
- package/types/TruncateText/v1/props.d.ts.map +1 -0
- package/types/TruncateText/v1/styles.d.ts.map +1 -0
- package/types/TruncateText/v1/theme.d.ts.map +1 -0
- package/types/TruncateText/v1/utils/cleanData.d.ts.map +1 -0
- package/types/TruncateText/v1/utils/cleanString.d.ts.map +1 -0
- package/types/TruncateText/v1/utils/measureText.d.ts.map +1 -0
- package/types/TruncateText/v1/utils/truncate.d.ts.map +1 -0
- package/types/TruncateText/v2/index.d.ts +47 -0
- package/types/TruncateText/v2/index.d.ts.map +1 -0
- package/types/TruncateText/v2/props.d.ts +61 -0
- package/types/TruncateText/v2/props.d.ts.map +1 -0
- package/types/TruncateText/v2/styles.d.ts +15 -0
- package/types/TruncateText/v2/styles.d.ts.map +1 -0
- package/types/TruncateText/v2/utils/cleanData.d.ts +19 -0
- package/types/TruncateText/v2/utils/cleanData.d.ts.map +1 -0
- package/types/TruncateText/v2/utils/cleanString.d.ts +16 -0
- package/types/TruncateText/v2/utils/cleanString.d.ts.map +1 -0
- package/types/TruncateText/v2/utils/measureText.d.ts +13 -0
- package/types/TruncateText/v2/utils/measureText.d.ts.map +1 -0
- package/types/TruncateText/v2/utils/truncate.d.ts +35 -0
- package/types/TruncateText/v2/utils/truncate.d.ts.map +1 -0
- package/types/exports/a.d.ts +3 -0
- package/types/exports/a.d.ts.map +1 -0
- package/types/exports/b.d.ts +3 -0
- package/types/exports/b.d.ts.map +1 -0
- package/types/TruncateText/index.d.ts.map +0 -1
- package/types/TruncateText/props.d.ts.map +0 -1
- package/types/TruncateText/styles.d.ts.map +0 -1
- package/types/TruncateText/theme.d.ts.map +0 -1
- package/types/TruncateText/utils/cleanData.d.ts.map +0 -1
- package/types/TruncateText/utils/cleanString.d.ts.map +0 -1
- package/types/TruncateText/utils/measureText.d.ts.map +0 -1
- package/types/TruncateText/utils/truncate.d.ts.map +0 -1
- package/types/index.d.ts +0 -3
- package/types/index.d.ts.map +0 -1
- /package/es/TruncateText/{props.js → v1/props.js} +0 -0
- /package/es/TruncateText/{styles.js → v1/styles.js} +0 -0
- /package/es/TruncateText/{theme.js → v1/theme.js} +0 -0
- /package/es/TruncateText/{utils → v1/utils}/cleanData.js +0 -0
- /package/es/TruncateText/{utils → v1/utils}/cleanString.js +0 -0
- /package/es/TruncateText/{utils → v1/utils}/measureText.js +0 -0
- /package/es/TruncateText/{utils → v1/utils}/truncate.js +0 -0
- /package/lib/TruncateText/{props.js → v1/props.js} +0 -0
- /package/lib/TruncateText/{styles.js → v1/styles.js} +0 -0
- /package/lib/TruncateText/{theme.js → v1/theme.js} +0 -0
- /package/lib/TruncateText/{utils → v1/utils}/cleanData.js +0 -0
- /package/lib/TruncateText/{utils → v1/utils}/cleanString.js +0 -0
- /package/lib/TruncateText/{utils → v1/utils}/measureText.js +0 -0
- /package/lib/TruncateText/{utils → v1/utils}/truncate.js +0 -0
- /package/src/TruncateText/{README.md → v1/README.md} +0 -0
- /package/src/TruncateText/{props.ts → v1/props.ts} +0 -0
- /package/src/TruncateText/{styles.ts → v1/styles.ts} +0 -0
- /package/src/TruncateText/{theme.ts → v1/theme.ts} +0 -0
- /package/src/TruncateText/{utils → v1/utils}/cleanData.ts +0 -0
- /package/src/TruncateText/{utils → v1/utils}/cleanString.ts +0 -0
- /package/src/TruncateText/{utils → v1/utils}/measureText.ts +0 -0
- /package/src/TruncateText/{utils → v1/utils}/truncate.ts +0 -0
- /package/types/TruncateText/{index.d.ts → v1/index.d.ts} +0 -0
- /package/types/TruncateText/{props.d.ts → v1/props.d.ts} +0 -0
- /package/types/TruncateText/{styles.d.ts → v1/styles.d.ts} +0 -0
- /package/types/TruncateText/{theme.d.ts → v1/theme.d.ts} +0 -0
- /package/types/TruncateText/{utils → v1/utils}/cleanData.d.ts +0 -0
- /package/types/TruncateText/{utils → v1/utils}/cleanString.d.ts +0 -0
- /package/types/TruncateText/{utils → v1/utils}/measureText.d.ts +0 -0
- /package/types/TruncateText/{utils → v1/utils}/truncate.d.ts +0 -0
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License (MIT)
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2015 - present Instructure, Inc.
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import escapeHtml from 'escape-html';
|
|
26
|
+
import { cloneArray } from '@instructure/ui-utils';
|
|
27
|
+
import { logError as error } from '@instructure/console';
|
|
28
|
+
import { getCSSStyleDeclaration, getBoundingClientRect, isVisible } from '@instructure/ui-dom-utils';
|
|
29
|
+
import measureText from "./measureText.js";
|
|
30
|
+
import cleanString from "./cleanString.js";
|
|
31
|
+
import cleanData from "./cleanData.js";
|
|
32
|
+
/**
|
|
33
|
+
* ---
|
|
34
|
+
* parent: TruncateText
|
|
35
|
+
* private: true
|
|
36
|
+
* ---
|
|
37
|
+
* Truncate the text content of an HTML element and its children.
|
|
38
|
+
*
|
|
39
|
+
* @param {DOMNode} element A single element containing the content to be truncated.
|
|
40
|
+
* @param {Object} options={} The options object.
|
|
41
|
+
* @param {DOMNode} options.parent An optional parent element to use for calculations.
|
|
42
|
+
* @param {number|string} options.maxLines Number of lines to allow before truncating.
|
|
43
|
+
* @param {string} options.position=end Where to place the ellipsis within the string.
|
|
44
|
+
* @param {string} options.truncate=character Add ellipsis after words or after any character.
|
|
45
|
+
* @param {string} options.ellipsis=\u2026 Where to place the ellipsis within the string.
|
|
46
|
+
* @param {string[]} options.ignore Characters to ignore at truncated end of string.
|
|
47
|
+
* @param {number} options.lineHeight=1.2 Unitless multiplier to use in case element can have
|
|
48
|
+
* 'normal' lineHeight. Adjust this to better match your font if needed.
|
|
49
|
+
*/
|
|
50
|
+
function truncate(element, options) {
|
|
51
|
+
const truncator = new Truncator(element, options);
|
|
52
|
+
if (truncator) {
|
|
53
|
+
return truncator.truncate();
|
|
54
|
+
}
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
class Truncator {
|
|
58
|
+
constructor(element, options = {}) {
|
|
59
|
+
this._options = void 0;
|
|
60
|
+
this._stage = void 0;
|
|
61
|
+
this._parent = void 0;
|
|
62
|
+
this._nodeMap = void 0;
|
|
63
|
+
this._defaultStringData = void 0;
|
|
64
|
+
this._nodeDataIndexes = void 0;
|
|
65
|
+
this._maxHeight = void 0;
|
|
66
|
+
this._maxWidth = void 0;
|
|
67
|
+
this._maxLines = void 0;
|
|
68
|
+
const parentElement = element !== null && element !== void 0 && element.parentElement ? element === null || element === void 0 ? void 0 : element.parentElement : void 0;
|
|
69
|
+
this._options = {
|
|
70
|
+
parent: options.parent || parentElement,
|
|
71
|
+
maxLines: options.maxLines || 1,
|
|
72
|
+
position: options.position || 'end',
|
|
73
|
+
truncate: options.truncate || 'character',
|
|
74
|
+
ellipsis: options.ellipsis || '\u2026',
|
|
75
|
+
ignore: options.ignore || [' ', '.', ','],
|
|
76
|
+
lineHeight: options.lineHeight || 1.2,
|
|
77
|
+
shouldTruncateWhenInvisible: !!options.shouldTruncateWhenInvisible
|
|
78
|
+
};
|
|
79
|
+
if (!element) {
|
|
80
|
+
error(false, '[Truncator] No element to truncate.');
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
this._stage = element;
|
|
84
|
+
if (options.parent) {
|
|
85
|
+
this._parent = this._options.parent;
|
|
86
|
+
} else {
|
|
87
|
+
const parentEl = this._stage.parentElement ? this._stage.parentElement : void 0;
|
|
88
|
+
this._parent = this._options.maxLines === 'auto' ? parentEl : this._stage;
|
|
89
|
+
}
|
|
90
|
+
this.setup();
|
|
91
|
+
}
|
|
92
|
+
setup() {
|
|
93
|
+
if (!this._stage) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
const style = getCSSStyleDeclaration(this._parent);
|
|
97
|
+
if (!style) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const _this$_options = this._options,
|
|
101
|
+
maxLines = _this$_options.maxLines,
|
|
102
|
+
truncate = _this$_options.truncate,
|
|
103
|
+
lineHeight = _this$_options.lineHeight;
|
|
104
|
+
// if no explicit lineHeight is inherited, use lineHeight multiplier for calculations
|
|
105
|
+
const actualLineHeight = style.lineHeight === 'normal' ? lineHeight * parseFloat(style.fontSize) : parseFloat(style.lineHeight);
|
|
106
|
+
const node = this._stage.firstChild.children ? this._stage.firstChild : this._stage;
|
|
107
|
+
const nodeDataIndexes = [];
|
|
108
|
+
const stringData = [];
|
|
109
|
+
this._nodeMap = this.getNodeMap(node);
|
|
110
|
+
for (let i = 0; i < this._nodeMap.length; i++) {
|
|
111
|
+
const item = this._nodeMap[i];
|
|
112
|
+
if (truncate === 'word' && item.data[item.data.length - 1] === ' ') {
|
|
113
|
+
// remove random whitespace data between nodes
|
|
114
|
+
item.data.length -= 1;
|
|
115
|
+
}
|
|
116
|
+
stringData[i] = item.data;
|
|
117
|
+
for (let j = 0; j < item.data.length; j++) {
|
|
118
|
+
// map each word or character datum index to its corresponding node
|
|
119
|
+
nodeDataIndexes.push(i);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
this._defaultStringData = cloneArray(stringData);
|
|
123
|
+
this._nodeDataIndexes = cloneArray(nodeDataIndexes);
|
|
124
|
+
this._maxHeight = maxLines === 'auto' ? getBoundingClientRect(this._parent).height : maxLines * actualLineHeight;
|
|
125
|
+
this._maxWidth = measureText(this._nodeMap.map(item => item.node), this._parent);
|
|
126
|
+
this._maxLines = maxLines === 'auto' ? Math.round(this._maxHeight / actualLineHeight) : maxLines;
|
|
127
|
+
}
|
|
128
|
+
getNodeMap(rootNode) {
|
|
129
|
+
const _this$_options2 = this._options,
|
|
130
|
+
shouldTruncateWhenInvisible = _this$_options2.shouldTruncateWhenInvisible,
|
|
131
|
+
truncate = _this$_options2.truncate;
|
|
132
|
+
const nodes = Array.from(rootNode.childNodes);
|
|
133
|
+
const map = [];
|
|
134
|
+
// parse child nodes and build a data map to associate each node with its data
|
|
135
|
+
nodes.forEach(node => {
|
|
136
|
+
if (node.nodeType === 1 || node.nodeType === 3) {
|
|
137
|
+
const shouldTruncate = shouldTruncateWhenInvisible ? true : isVisible(node, false);
|
|
138
|
+
const textContent = node.textContent + ' ';
|
|
139
|
+
map.push({
|
|
140
|
+
node,
|
|
141
|
+
data: truncate === 'word' ? shouldTruncate ?
|
|
142
|
+
// eslint-disable-next-line no-useless-escape
|
|
143
|
+
textContent.match(/.*?[\.\s\/]+?/g) : [''] : shouldTruncate ? node.textContent.split('') : []
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
return map;
|
|
148
|
+
}
|
|
149
|
+
getNodeIndexes(data) {
|
|
150
|
+
const nodeDataIndexes = [];
|
|
151
|
+
for (let i = 0; i < data.length; i++) {
|
|
152
|
+
for (let j = 0; j < data[i].length; j++) {
|
|
153
|
+
nodeDataIndexes.push([i, j]);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return nodeDataIndexes;
|
|
157
|
+
}
|
|
158
|
+
domString(data) {
|
|
159
|
+
let html = '';
|
|
160
|
+
for (let i = 0; i < data.length; i++) {
|
|
161
|
+
const mapItem = this._nodeMap[i];
|
|
162
|
+
const text = data[i].join('');
|
|
163
|
+
const safeText = escapeHtml(text);
|
|
164
|
+
if (mapItem.node.nodeType === Node.ELEMENT_NODE) {
|
|
165
|
+
const name = mapItem.node.nodeName;
|
|
166
|
+
const attr = mapItem.node.attributes;
|
|
167
|
+
let attributes = '';
|
|
168
|
+
for (let j = 0; j < attr.length; j++) {
|
|
169
|
+
const att = attr[j];
|
|
170
|
+
attributes += ` ${att.nodeName}="${att.nodeValue}"`;
|
|
171
|
+
}
|
|
172
|
+
html += `<${name}${attributes}>${safeText}</${name}>`;
|
|
173
|
+
} else if (mapItem.node.nodeType === 3) {
|
|
174
|
+
html += safeText;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return html;
|
|
178
|
+
}
|
|
179
|
+
checkFit(data) {
|
|
180
|
+
const html = this.domString(data);
|
|
181
|
+
const node = this._options.maxLines === 'auto' ? this._stage : this._parent;
|
|
182
|
+
let fits = true;
|
|
183
|
+
this._stage.innerHTML = html;
|
|
184
|
+
// allow a 0.5 px margin of error for browser calculation discrepancies
|
|
185
|
+
if (getBoundingClientRect(node).height - this._maxHeight > 0.5) {
|
|
186
|
+
fits = false;
|
|
187
|
+
}
|
|
188
|
+
return fits;
|
|
189
|
+
}
|
|
190
|
+
truncate() {
|
|
191
|
+
const _this$_options3 = this._options,
|
|
192
|
+
ellipsis = _this$_options3.ellipsis,
|
|
193
|
+
ignore = _this$_options3.ignore,
|
|
194
|
+
position = _this$_options3.position;
|
|
195
|
+
const middle = position === 'middle';
|
|
196
|
+
let truncated = false;
|
|
197
|
+
let truncatedText = '';
|
|
198
|
+
let stringData = null;
|
|
199
|
+
if (!this._stage) {
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
const binarySearch = (_low, _high, ev, middle,
|
|
203
|
+
// in case we can not find the result
|
|
204
|
+
_default = null) => {
|
|
205
|
+
let low = _low;
|
|
206
|
+
let high = _high;
|
|
207
|
+
let bestResult = _default;
|
|
208
|
+
while (low <= high) {
|
|
209
|
+
const tryNumber = Math.floor((low + high) / 2);
|
|
210
|
+
if (ev(tryNumber, middle)) {
|
|
211
|
+
high = tryNumber - 1;
|
|
212
|
+
bestResult = tryNumber;
|
|
213
|
+
} else {
|
|
214
|
+
low = tryNumber + 1;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return bestResult;
|
|
218
|
+
};
|
|
219
|
+
const truncateArray = (truncatedLength, originalArray, indexArray, middle) => {
|
|
220
|
+
let truncated = false;
|
|
221
|
+
const truncatedArray = cloneArray(originalArray);
|
|
222
|
+
switch (truncatedLength) {
|
|
223
|
+
// truncate nothing
|
|
224
|
+
case 0:
|
|
225
|
+
break;
|
|
226
|
+
|
|
227
|
+
// truncate all
|
|
228
|
+
case indexArray.length:
|
|
229
|
+
truncated = true;
|
|
230
|
+
for (let i = 0; i < truncatedArray.length; i++) {
|
|
231
|
+
truncatedArray[i] = [];
|
|
232
|
+
}
|
|
233
|
+
truncatedArray[0].push(ellipsis);
|
|
234
|
+
break;
|
|
235
|
+
|
|
236
|
+
// truncate a positive amount of elements
|
|
237
|
+
default:
|
|
238
|
+
truncated = true;
|
|
239
|
+
if (middle) {
|
|
240
|
+
const dataHalves = Array(2);
|
|
241
|
+
// calculate the indexes for two parts
|
|
242
|
+
dataHalves[0] = Math.floor((indexArray.length - truncatedLength - 1) / 2);
|
|
243
|
+
dataHalves[1] = dataHalves[0] + truncatedLength + 1;
|
|
244
|
+
const nodeHalves = dataHalves.map(index => indexArray[index]);
|
|
245
|
+
const sliceAmounts = Array(2);
|
|
246
|
+
sliceAmounts[0] = dataHalves[0] + 1;
|
|
247
|
+
sliceAmounts[1] = indexArray.length - truncatedLength - sliceAmounts[0];
|
|
248
|
+
|
|
249
|
+
// keep the first part
|
|
250
|
+
for (let i = 0; i <= nodeHalves[0]; i++) {
|
|
251
|
+
switch (true) {
|
|
252
|
+
case i < nodeHalves[0]:
|
|
253
|
+
sliceAmounts[0] -= truncatedArray[i].length;
|
|
254
|
+
break;
|
|
255
|
+
case i === nodeHalves[0]:
|
|
256
|
+
truncatedArray[i] = originalArray[i].slice(0, sliceAmounts[0]);
|
|
257
|
+
truncatedArray[i].push(ellipsis);
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// and the second part
|
|
263
|
+
for (let i = originalArray.length - 1; i >= nodeHalves[1]; i--) {
|
|
264
|
+
switch (true) {
|
|
265
|
+
case i > nodeHalves[1]:
|
|
266
|
+
sliceAmounts[1] -= originalArray[i].length;
|
|
267
|
+
break;
|
|
268
|
+
case i === nodeHalves[1]:
|
|
269
|
+
if (nodeHalves[1] > nodeHalves[0]) truncatedArray[i] = originalArray[i].slice(-sliceAmounts[1]);
|
|
270
|
+
if (nodeHalves[1] === nodeHalves[0]) truncatedArray[i] = truncatedArray[i].concat(originalArray[i].slice(-sliceAmounts[1]));
|
|
271
|
+
break;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// delete the middle
|
|
276
|
+
for (let i = nodeHalves[0] + 1; i < nodeHalves[1]; i++) {
|
|
277
|
+
truncatedArray[i] = [];
|
|
278
|
+
}
|
|
279
|
+
} else {
|
|
280
|
+
// the right most index to keep from truncating
|
|
281
|
+
const indexToTry = indexArray.length - truncatedLength - 1;
|
|
282
|
+
const nodeIndex = indexArray[indexToTry];
|
|
283
|
+
let sliceAmount = indexToTry + 1;
|
|
284
|
+
for (let i = 0; i < originalArray.length; i++) {
|
|
285
|
+
switch (true) {
|
|
286
|
+
case i < nodeIndex:
|
|
287
|
+
sliceAmount -= truncatedArray[i].length;
|
|
288
|
+
break;
|
|
289
|
+
case i === nodeIndex:
|
|
290
|
+
truncatedArray[i] = truncatedArray[i].slice(0, sliceAmount);
|
|
291
|
+
truncatedArray[i].push(ellipsis);
|
|
292
|
+
break;
|
|
293
|
+
case i > nodeIndex:
|
|
294
|
+
truncatedArray[i] = [];
|
|
295
|
+
break;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
break;
|
|
300
|
+
}
|
|
301
|
+
return {
|
|
302
|
+
truncated,
|
|
303
|
+
truncatedArray
|
|
304
|
+
};
|
|
305
|
+
};
|
|
306
|
+
const truncateEvaluate = (truncatedLength, middle) => {
|
|
307
|
+
const _truncateArray = truncateArray(truncatedLength, this._defaultStringData, this._nodeDataIndexes, middle),
|
|
308
|
+
truncatedArray = _truncateArray.truncatedArray;
|
|
309
|
+
return this.checkFit(truncatedArray);
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
// find the best number of element to truncate
|
|
313
|
+
const finestMatch = binarySearch(0, this._nodeDataIndexes.length, truncateEvaluate, middle, this._nodeDataIndexes.length);
|
|
314
|
+
var _truncateArray2 = truncateArray(finestMatch, this._defaultStringData, this._nodeDataIndexes, middle);
|
|
315
|
+
truncated = _truncateArray2.truncated;
|
|
316
|
+
stringData = _truncateArray2.truncatedArray;
|
|
317
|
+
stringData = cleanData(stringData, this._options, true);
|
|
318
|
+
for (let i = 0; i < stringData.length; i++) {
|
|
319
|
+
const data = stringData[i];
|
|
320
|
+
truncatedText += data.join('');
|
|
321
|
+
}
|
|
322
|
+
if (truncated && !middle) {
|
|
323
|
+
truncatedText = cleanString(truncatedText.split(ellipsis)[0], ignore, false, true, true);
|
|
324
|
+
truncatedText += ellipsis;
|
|
325
|
+
} else if (truncated && middle) {
|
|
326
|
+
const halves = truncatedText.split(ellipsis);
|
|
327
|
+
truncatedText = cleanString(halves[0], ignore, false, true, true) + ellipsis + cleanString(halves[1], ignore, true, false, true);
|
|
328
|
+
}
|
|
329
|
+
return {
|
|
330
|
+
isTruncated: truncated,
|
|
331
|
+
text: truncatedText,
|
|
332
|
+
data: stringData,
|
|
333
|
+
constraints: {
|
|
334
|
+
width: this._maxWidth,
|
|
335
|
+
height: this._maxHeight,
|
|
336
|
+
lines: this._maxLines
|
|
337
|
+
}
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
export default truncate;
|
|
@@ -21,5 +21,4 @@
|
|
|
21
21
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
22
|
* SOFTWARE.
|
|
23
23
|
*/
|
|
24
|
-
export { TruncateText } from
|
|
25
|
-
export type { TruncateTextProps } from './TruncateText/props'
|
|
24
|
+
export { TruncateText } from "../TruncateText/v2/index.js";
|
|
@@ -47,7 +47,7 @@ var _dec, _class, _TruncateText;
|
|
|
47
47
|
category: components
|
|
48
48
|
---
|
|
49
49
|
**/
|
|
50
|
-
let TruncateText = exports.TruncateText = (_dec = (0, _emotion.
|
|
50
|
+
let TruncateText = exports.TruncateText = (_dec = (0, _emotion.withStyleLegacy)(_styles.default, _theme.default), _dec(_class = (_TruncateText = class TruncateText extends _react.Component {
|
|
51
51
|
constructor(props) {
|
|
52
52
|
super(props);
|
|
53
53
|
this.ref = null;
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.default = exports.TruncateText = void 0;
|
|
8
|
+
var _react = require("react");
|
|
9
|
+
var _debounce = require("@instructure/debounce");
|
|
10
|
+
var _canUseDOM = require("@instructure/ui-dom-utils/lib/canUseDOM.js");
|
|
11
|
+
var _getBoundingClientRect = require("@instructure/ui-dom-utils/lib/getBoundingClientRect.js");
|
|
12
|
+
var _safeCloneElement = require("@instructure/ui-react-utils/lib/safeCloneElement.js");
|
|
13
|
+
var _ensureSingleChild = require("@instructure/ui-react-utils/lib/ensureSingleChild.js");
|
|
14
|
+
var _console = require("@instructure/console");
|
|
15
|
+
var _emotion = require("@instructure/emotion");
|
|
16
|
+
var _styles = _interopRequireDefault(require("./styles"));
|
|
17
|
+
var _truncate = _interopRequireDefault(require("./utils/truncate"));
|
|
18
|
+
var _props = require("./props");
|
|
19
|
+
var _jsxRuntime = require("@emotion/react/jsx-runtime");
|
|
20
|
+
var _dec, _class, _TruncateText;
|
|
21
|
+
/*
|
|
22
|
+
* The MIT License (MIT)
|
|
23
|
+
*
|
|
24
|
+
* Copyright (c) 2015 - present Instructure, Inc.
|
|
25
|
+
*
|
|
26
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
27
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
28
|
+
* in the Software without restriction, including without limitation the rights
|
|
29
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
30
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
31
|
+
* furnished to do so, subject to the following conditions:
|
|
32
|
+
*
|
|
33
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
34
|
+
* copies or substantial portions of the Software.
|
|
35
|
+
*
|
|
36
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
37
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
38
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
39
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
40
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
41
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
42
|
+
* SOFTWARE.
|
|
43
|
+
*/
|
|
44
|
+
/**
|
|
45
|
+
---
|
|
46
|
+
category: components
|
|
47
|
+
---
|
|
48
|
+
**/
|
|
49
|
+
let TruncateText = exports.TruncateText = (_dec = (0, _emotion.withStyle)(_styles.default), _dec(_class = (_TruncateText = class TruncateText extends _react.Component {
|
|
50
|
+
constructor(props) {
|
|
51
|
+
super(props);
|
|
52
|
+
this.ref = null;
|
|
53
|
+
this._text = void 0;
|
|
54
|
+
this._debounced = void 0;
|
|
55
|
+
this._stage = null;
|
|
56
|
+
this._wasTruncated = void 0;
|
|
57
|
+
this._resizeListener = void 0;
|
|
58
|
+
this._prevWidth = null;
|
|
59
|
+
this.update = () => {
|
|
60
|
+
if (this.ref) {
|
|
61
|
+
this.setState(this.initialState);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
this.state = this.initialState;
|
|
65
|
+
}
|
|
66
|
+
get _ref() {
|
|
67
|
+
return this.ref;
|
|
68
|
+
}
|
|
69
|
+
get initialState() {
|
|
70
|
+
return {
|
|
71
|
+
isTruncated: false,
|
|
72
|
+
needsSecondRender: true,
|
|
73
|
+
truncatedElement: void 0,
|
|
74
|
+
truncatedText: void 0
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
componentDidMount() {
|
|
78
|
+
const _this$props = this.props,
|
|
79
|
+
children = _this$props.children,
|
|
80
|
+
makeStyles = _this$props.makeStyles;
|
|
81
|
+
makeStyles === null || makeStyles === void 0 ? void 0 : makeStyles();
|
|
82
|
+
if (children) {
|
|
83
|
+
var _getBoundingClientRec;
|
|
84
|
+
this.checkChildren();
|
|
85
|
+
const txt = (0, _ensureSingleChild.ensureSingleChild)(children);
|
|
86
|
+
this._text = txt ? txt : void 0;
|
|
87
|
+
this.truncate();
|
|
88
|
+
this._debounced = (0, _debounce.debounce)(this.update, this.props.debounce, {
|
|
89
|
+
leading: true,
|
|
90
|
+
trailing: true
|
|
91
|
+
});
|
|
92
|
+
this._prevWidth = (_getBoundingClientRec = (0, _getBoundingClientRect.getBoundingClientRect)(this.ref)) === null || _getBoundingClientRec === void 0 ? void 0 : _getBoundingClientRec.width;
|
|
93
|
+
this._resizeListener = new ResizeObserver(entries => {
|
|
94
|
+
// requestAnimationFrame call is needed becuase some truncatetext test cases
|
|
95
|
+
// failed due to ResizeObserver was not able to deliver all observations within a single animation frame
|
|
96
|
+
// see: https://stackoverflow.com/questions/49384120/resizeobserver-loop-limit-exceeded
|
|
97
|
+
requestAnimationFrame(() => {
|
|
98
|
+
for (const entry of entries) {
|
|
99
|
+
const width = entry.contentRect.width;
|
|
100
|
+
if (this._prevWidth !== width) {
|
|
101
|
+
this._prevWidth = width;
|
|
102
|
+
this.props.debounce === 0 ? this.update() : this._debounced();
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
this._resizeListener.observe(this.ref);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
componentWillUnmount() {
|
|
111
|
+
if (this._resizeListener) {
|
|
112
|
+
this._resizeListener.disconnect();
|
|
113
|
+
}
|
|
114
|
+
if (this._debounced) {
|
|
115
|
+
this._debounced.cancel();
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
shallowCompare(obj1, obj2) {
|
|
119
|
+
const keys1 = Object.keys(obj1);
|
|
120
|
+
const keys2 = Object.keys(obj2);
|
|
121
|
+
if (keys1.length !== keys2.length) {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
for (const key of keys1) {
|
|
125
|
+
if (obj1[key] !== obj2[key]) {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
componentDidUpdate(prevProps) {
|
|
132
|
+
const _this$props2 = this.props,
|
|
133
|
+
children = _this$props2.children,
|
|
134
|
+
onUpdate = _this$props2.onUpdate,
|
|
135
|
+
makeStyles = _this$props2.makeStyles;
|
|
136
|
+
makeStyles === null || makeStyles === void 0 ? void 0 : makeStyles();
|
|
137
|
+
const _this$state = this.state,
|
|
138
|
+
isTruncated = _this$state.isTruncated,
|
|
139
|
+
needsSecondRender = _this$state.needsSecondRender,
|
|
140
|
+
truncatedText = _this$state.truncatedText;
|
|
141
|
+
if (children) {
|
|
142
|
+
// for some reason in React 19 prevPros and this.props are a different
|
|
143
|
+
// object even if their contents are the same, so we cannot use !==
|
|
144
|
+
if (!this.shallowCompare(prevProps, this.props)) {
|
|
145
|
+
if (prevProps.children !== this.props.children) {
|
|
146
|
+
// reset internal text variable if children change
|
|
147
|
+
this.checkChildren();
|
|
148
|
+
const txt = (0, _ensureSingleChild.ensureSingleChild)(children);
|
|
149
|
+
this._text = txt ? txt : void 0;
|
|
150
|
+
}
|
|
151
|
+
// require the double render whenever props change
|
|
152
|
+
this.setState(this.initialState);
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
if (!needsSecondRender && (isTruncated || this._wasTruncated)) {
|
|
156
|
+
onUpdate === null || onUpdate === void 0 ? void 0 : onUpdate(isTruncated, truncatedText);
|
|
157
|
+
this._wasTruncated = isTruncated;
|
|
158
|
+
} else {
|
|
159
|
+
this.truncate();
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
checkChildren() {
|
|
164
|
+
(0, _console.logError)(!(() => {
|
|
165
|
+
let isTooDeep = false;
|
|
166
|
+
const text = (0, _ensureSingleChild.ensureSingleChild)(this.props.children);
|
|
167
|
+
_react.Children.forEach(text.props.children, child => {
|
|
168
|
+
if (child.props) {
|
|
169
|
+
_react.Children.forEach(child.props.children, grandChild => {
|
|
170
|
+
// currently we don't support node trees deeper than 2 levels
|
|
171
|
+
// truncation will still occur on their text content, but their actual node structure will be lost
|
|
172
|
+
if (grandChild.props) {
|
|
173
|
+
isTooDeep = true;
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
return isTooDeep;
|
|
179
|
+
})(), `[TruncateText] Some children are too deep in the node tree and will not render.`);
|
|
180
|
+
}
|
|
181
|
+
truncate() {
|
|
182
|
+
if (!this.state.needsSecondRender) {
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
if (_canUseDOM.canUseDOM) {
|
|
186
|
+
var _this$props$styles;
|
|
187
|
+
const result = (0, _truncate.default)(this._stage, {
|
|
188
|
+
...this.props,
|
|
189
|
+
parent: this.ref ? this.ref : void 0,
|
|
190
|
+
lineHeight: (_this$props$styles = this.props.styles) === null || _this$props$styles === void 0 ? void 0 : _this$props$styles.lineHeight
|
|
191
|
+
});
|
|
192
|
+
if (result) {
|
|
193
|
+
const element = this.renderChildren(result.isTruncated, result.data, result.constraints.width);
|
|
194
|
+
this.setState({
|
|
195
|
+
needsSecondRender: false,
|
|
196
|
+
isTruncated: result.isTruncated,
|
|
197
|
+
truncatedElement: element,
|
|
198
|
+
truncatedText: result.text
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
} else {
|
|
202
|
+
var _this$ref, _this$ref2;
|
|
203
|
+
const textContent = (_this$ref = this.ref) !== null && _this$ref !== void 0 && _this$ref.textContent ? (_this$ref2 = this.ref) === null || _this$ref2 === void 0 ? void 0 : _this$ref2.textContent : void 0;
|
|
204
|
+
// if dom isn't available, use original children
|
|
205
|
+
this.setState({
|
|
206
|
+
needsSecondRender: false,
|
|
207
|
+
isTruncated: false,
|
|
208
|
+
truncatedElement: this._text,
|
|
209
|
+
truncatedText: textContent
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
renderChildren(truncated, data, width) {
|
|
214
|
+
var _this$props$styles2;
|
|
215
|
+
if (!truncated) {
|
|
216
|
+
return this._text;
|
|
217
|
+
}
|
|
218
|
+
const childElements = [];
|
|
219
|
+
// iterate over each node used in the truncated string
|
|
220
|
+
for (let i = 0; i < data.length; i++) {
|
|
221
|
+
const item = data[i];
|
|
222
|
+
const element = this._text.props.children[i];
|
|
223
|
+
const nodeText = item.join('');
|
|
224
|
+
if (element && element.props) {
|
|
225
|
+
// if node is an html element and not just a string
|
|
226
|
+
childElements.push((0, _safeCloneElement.safeCloneElement)(element, element.props, nodeText));
|
|
227
|
+
} else {
|
|
228
|
+
childElements.push(nodeText);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
// this spacer element is set to the max width the full text could
|
|
232
|
+
// potentially be without this, text in `width: auto` elements won't expand
|
|
233
|
+
// to accommodate more text, once truncated
|
|
234
|
+
// Breadcrumb is modifying this element's display to inline to prevent layout issues
|
|
235
|
+
// TODO: find a better way to handle this
|
|
236
|
+
childElements.push((0, _jsxRuntime.jsx)("span", {
|
|
237
|
+
css: (_this$props$styles2 = this.props.styles) === null || _this$props$styles2 === void 0 ? void 0 : _this$props$styles2.spacer,
|
|
238
|
+
style: {
|
|
239
|
+
width: width || void 0
|
|
240
|
+
}
|
|
241
|
+
}, "spacer"));
|
|
242
|
+
const children = _react.Children.map(childElements, child => child);
|
|
243
|
+
return this._text.props ? (0, _safeCloneElement.safeCloneElement)(this._text, this._text.props, children) : children;
|
|
244
|
+
}
|
|
245
|
+
render() {
|
|
246
|
+
var _this$props$styles3;
|
|
247
|
+
const truncatedElement = this.state.truncatedElement;
|
|
248
|
+
const children = this.props.children;
|
|
249
|
+
return (0, _jsxRuntime.jsxs)("span", {
|
|
250
|
+
"data-cid": "TruncateText",
|
|
251
|
+
css: (_this$props$styles3 = this.props.styles) === null || _this$props$styles3 === void 0 ? void 0 : _this$props$styles3.truncateText,
|
|
252
|
+
ref: el => {
|
|
253
|
+
this.ref = el;
|
|
254
|
+
},
|
|
255
|
+
children: [children && (truncatedElement ? null : (0, _jsxRuntime.jsx)("span", {
|
|
256
|
+
ref: el => {
|
|
257
|
+
this._stage = el;
|
|
258
|
+
},
|
|
259
|
+
children: (0, _ensureSingleChild.ensureSingleChild)(children)
|
|
260
|
+
})), truncatedElement]
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
}, _TruncateText.displayName = "TruncateText", _TruncateText.componentId = 'TruncateText', _TruncateText.allowedProps = _props.allowedProps, _TruncateText.defaultProps = {
|
|
264
|
+
maxLines: 1,
|
|
265
|
+
ellipsis: '\u2026',
|
|
266
|
+
truncate: 'character',
|
|
267
|
+
position: 'end',
|
|
268
|
+
ignore: [' ', '.', ','],
|
|
269
|
+
debounce: 0
|
|
270
|
+
}, _TruncateText)) || _class);
|
|
271
|
+
var _default = exports.default = TruncateText;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.allowedProps = void 0;
|
|
7
|
+
/*
|
|
8
|
+
* The MIT License (MIT)
|
|
9
|
+
*
|
|
10
|
+
* Copyright (c) 2015 - present Instructure, Inc.
|
|
11
|
+
*
|
|
12
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
13
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
14
|
+
* in the Software without restriction, including without limitation the rights
|
|
15
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
16
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
17
|
+
* furnished to do so, subject to the following conditions:
|
|
18
|
+
*
|
|
19
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
20
|
+
* copies or substantial portions of the Software.
|
|
21
|
+
*
|
|
22
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
23
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
24
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
25
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
26
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
27
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
28
|
+
* SOFTWARE.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
const allowedProps = exports.allowedProps = ['children', 'maxLines', 'position', 'truncate', 'ellipsis', 'ignore', 'debounce', 'onUpdate', 'shouldTruncateWhenInvisible'];
|