@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,451 @@
|
|
|
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
|
+
|
|
27
|
+
import { cloneArray } from '@instructure/ui-utils'
|
|
28
|
+
import { logError as error } from '@instructure/console'
|
|
29
|
+
import {
|
|
30
|
+
getCSSStyleDeclaration,
|
|
31
|
+
getBoundingClientRect,
|
|
32
|
+
isVisible
|
|
33
|
+
} from '@instructure/ui-dom-utils'
|
|
34
|
+
|
|
35
|
+
import measureText from './measureText'
|
|
36
|
+
import cleanString from './cleanString'
|
|
37
|
+
import cleanData from './cleanData'
|
|
38
|
+
import { TruncateTextCommonProps } from '../props'
|
|
39
|
+
|
|
40
|
+
export type TruncatorOptions = {
|
|
41
|
+
parent?: Node
|
|
42
|
+
lineHeight?: number
|
|
43
|
+
} & TruncateTextCommonProps
|
|
44
|
+
|
|
45
|
+
type NodeMapData = {
|
|
46
|
+
node: Node
|
|
47
|
+
data: string[]
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* ---
|
|
52
|
+
* parent: TruncateText
|
|
53
|
+
* private: true
|
|
54
|
+
* ---
|
|
55
|
+
* Truncate the text content of an HTML element and its children.
|
|
56
|
+
*
|
|
57
|
+
* @param {DOMNode} element A single element containing the content to be truncated.
|
|
58
|
+
* @param {Object} options={} The options object.
|
|
59
|
+
* @param {DOMNode} options.parent An optional parent element to use for calculations.
|
|
60
|
+
* @param {number|string} options.maxLines Number of lines to allow before truncating.
|
|
61
|
+
* @param {string} options.position=end Where to place the ellipsis within the string.
|
|
62
|
+
* @param {string} options.truncate=character Add ellipsis after words or after any character.
|
|
63
|
+
* @param {string} options.ellipsis=\u2026 Where to place the ellipsis within the string.
|
|
64
|
+
* @param {string[]} options.ignore Characters to ignore at truncated end of string.
|
|
65
|
+
* @param {number} options.lineHeight=1.2 Unitless multiplier to use in case element can have
|
|
66
|
+
* 'normal' lineHeight. Adjust this to better match your font if needed.
|
|
67
|
+
*/
|
|
68
|
+
function truncate(element: Element, options?: TruncatorOptions) {
|
|
69
|
+
const truncator = new Truncator(element, options)
|
|
70
|
+
if (truncator) {
|
|
71
|
+
return truncator.truncate()
|
|
72
|
+
}
|
|
73
|
+
return
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
class Truncator {
|
|
77
|
+
private _options: Required<Omit<TruncatorOptions, 'parent'>> &
|
|
78
|
+
Pick<TruncatorOptions, 'parent'>
|
|
79
|
+
private _stage!: Element
|
|
80
|
+
private _parent?: Node
|
|
81
|
+
private _nodeMap!: NodeMapData[]
|
|
82
|
+
private _defaultStringData!: string[][]
|
|
83
|
+
private _nodeDataIndexes!: number[]
|
|
84
|
+
private _maxHeight!: number
|
|
85
|
+
private _maxWidth!: number
|
|
86
|
+
private _maxLines!: number
|
|
87
|
+
|
|
88
|
+
constructor(element: Element, options: TruncatorOptions = {}) {
|
|
89
|
+
const parentElement = element?.parentElement
|
|
90
|
+
? element?.parentElement
|
|
91
|
+
: undefined
|
|
92
|
+
this._options = {
|
|
93
|
+
parent: options.parent || parentElement,
|
|
94
|
+
maxLines: options.maxLines || 1,
|
|
95
|
+
position: options.position || 'end',
|
|
96
|
+
truncate: options.truncate || 'character',
|
|
97
|
+
ellipsis: options.ellipsis || '\u2026',
|
|
98
|
+
ignore: options.ignore || [' ', '.', ','],
|
|
99
|
+
lineHeight: options.lineHeight || 1.2,
|
|
100
|
+
shouldTruncateWhenInvisible: !!options.shouldTruncateWhenInvisible
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (!element) {
|
|
104
|
+
error(false, '[Truncator] No element to truncate.')
|
|
105
|
+
return
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
this._stage = element
|
|
109
|
+
|
|
110
|
+
if (options.parent) {
|
|
111
|
+
this._parent = this._options.parent
|
|
112
|
+
} else {
|
|
113
|
+
const parentEl = this._stage.parentElement
|
|
114
|
+
? this._stage.parentElement
|
|
115
|
+
: undefined
|
|
116
|
+
this._parent = this._options.maxLines === 'auto' ? parentEl : this._stage
|
|
117
|
+
}
|
|
118
|
+
this.setup()
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
setup() {
|
|
122
|
+
if (!this._stage) {
|
|
123
|
+
return
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const style = getCSSStyleDeclaration(this._parent)
|
|
127
|
+
if (!style) {
|
|
128
|
+
return
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const { maxLines, truncate, lineHeight } = this._options
|
|
132
|
+
// if no explicit lineHeight is inherited, use lineHeight multiplier for calculations
|
|
133
|
+
const actualLineHeight =
|
|
134
|
+
style.lineHeight === 'normal'
|
|
135
|
+
? lineHeight * parseFloat(style.fontSize)
|
|
136
|
+
: parseFloat(style.lineHeight)
|
|
137
|
+
const node = (this._stage.firstChild as Element).children
|
|
138
|
+
? this._stage.firstChild!
|
|
139
|
+
: this._stage
|
|
140
|
+
|
|
141
|
+
const nodeDataIndexes: number[] = []
|
|
142
|
+
const stringData: string[][] = []
|
|
143
|
+
|
|
144
|
+
this._nodeMap = this.getNodeMap(node)
|
|
145
|
+
|
|
146
|
+
for (let i = 0; i < this._nodeMap.length; i++) {
|
|
147
|
+
const item = this._nodeMap[i]
|
|
148
|
+
if (truncate === 'word' && item.data[item.data.length - 1] === ' ') {
|
|
149
|
+
// remove random whitespace data between nodes
|
|
150
|
+
item.data.length -= 1
|
|
151
|
+
}
|
|
152
|
+
stringData[i] = item.data
|
|
153
|
+
for (let j = 0; j < item.data.length; j++) {
|
|
154
|
+
// map each word or character datum index to its corresponding node
|
|
155
|
+
nodeDataIndexes.push(i)
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
this._defaultStringData = cloneArray(stringData)
|
|
160
|
+
this._nodeDataIndexes = cloneArray(nodeDataIndexes)
|
|
161
|
+
this._maxHeight =
|
|
162
|
+
maxLines === 'auto'
|
|
163
|
+
? getBoundingClientRect(this._parent).height
|
|
164
|
+
: maxLines * actualLineHeight
|
|
165
|
+
this._maxWidth = measureText(
|
|
166
|
+
this._nodeMap.map((item) => item.node),
|
|
167
|
+
this._parent
|
|
168
|
+
)
|
|
169
|
+
this._maxLines =
|
|
170
|
+
maxLines === 'auto'
|
|
171
|
+
? Math.round(this._maxHeight / actualLineHeight)
|
|
172
|
+
: maxLines
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
getNodeMap(rootNode: Node) {
|
|
176
|
+
const { shouldTruncateWhenInvisible, truncate } = this._options
|
|
177
|
+
const nodes: ChildNode[] = Array.from(rootNode.childNodes)
|
|
178
|
+
const map: NodeMapData[] = []
|
|
179
|
+
// parse child nodes and build a data map to associate each node with its data
|
|
180
|
+
nodes.forEach((node) => {
|
|
181
|
+
if (node.nodeType === 1 || node.nodeType === 3) {
|
|
182
|
+
const shouldTruncate = shouldTruncateWhenInvisible
|
|
183
|
+
? true
|
|
184
|
+
: isVisible(node, false)
|
|
185
|
+
const textContent = node.textContent + ' '
|
|
186
|
+
map.push({
|
|
187
|
+
node,
|
|
188
|
+
data:
|
|
189
|
+
truncate === 'word'
|
|
190
|
+
? shouldTruncate
|
|
191
|
+
? // eslint-disable-next-line no-useless-escape
|
|
192
|
+
textContent.match(/.*?[\.\s\/]+?/g)!
|
|
193
|
+
: ['']
|
|
194
|
+
: shouldTruncate
|
|
195
|
+
? node.textContent!.split('')
|
|
196
|
+
: []
|
|
197
|
+
})
|
|
198
|
+
}
|
|
199
|
+
})
|
|
200
|
+
return map
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
getNodeIndexes(data: string[][]): number[][] {
|
|
204
|
+
const nodeDataIndexes = []
|
|
205
|
+
for (let i = 0; i < data.length; i++) {
|
|
206
|
+
for (let j = 0; j < data[i].length; j++) {
|
|
207
|
+
nodeDataIndexes.push([i, j])
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return nodeDataIndexes
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
domString(data: string[][]) {
|
|
214
|
+
let html = ''
|
|
215
|
+
|
|
216
|
+
for (let i = 0; i < data.length; i++) {
|
|
217
|
+
const mapItem = this._nodeMap[i]
|
|
218
|
+
const text = data[i].join('')
|
|
219
|
+
const safeText = escapeHtml(text)
|
|
220
|
+
|
|
221
|
+
if (mapItem.node.nodeType === Node.ELEMENT_NODE) {
|
|
222
|
+
const name = mapItem.node.nodeName
|
|
223
|
+
const attr = (mapItem.node as Element).attributes
|
|
224
|
+
let attributes = ''
|
|
225
|
+
for (let j = 0; j < attr.length; j++) {
|
|
226
|
+
const att = attr[j]
|
|
227
|
+
attributes += ` ${att.nodeName}="${att.nodeValue}"`
|
|
228
|
+
}
|
|
229
|
+
html += `<${name}${attributes}>${safeText}</${name}>`
|
|
230
|
+
} else if (mapItem.node.nodeType === 3) {
|
|
231
|
+
html += safeText
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
return html
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
checkFit(data: string[][]) {
|
|
238
|
+
const html = this.domString(data)
|
|
239
|
+
const node = this._options.maxLines === 'auto' ? this._stage : this._parent
|
|
240
|
+
let fits = true
|
|
241
|
+
this._stage.innerHTML = html
|
|
242
|
+
// allow a 0.5 px margin of error for browser calculation discrepancies
|
|
243
|
+
if (getBoundingClientRect(node).height - this._maxHeight > 0.5) {
|
|
244
|
+
fits = false
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
return fits
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
truncate() {
|
|
251
|
+
const { ellipsis, ignore, position } = this._options
|
|
252
|
+
const middle = position === 'middle'
|
|
253
|
+
let truncated = false
|
|
254
|
+
let truncatedText = ''
|
|
255
|
+
let stringData: string[][] | null = null
|
|
256
|
+
|
|
257
|
+
if (!this._stage) {
|
|
258
|
+
return
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
const binarySearch = (
|
|
262
|
+
_low: number,
|
|
263
|
+
_high: number,
|
|
264
|
+
ev: (tryNumber: number, middle: boolean) => boolean,
|
|
265
|
+
middle: boolean,
|
|
266
|
+
// in case we can not find the result
|
|
267
|
+
_default: number | null = null
|
|
268
|
+
): number | null => {
|
|
269
|
+
let low = _low
|
|
270
|
+
let high = _high
|
|
271
|
+
let bestResult = _default
|
|
272
|
+
while (low <= high) {
|
|
273
|
+
const tryNumber = Math.floor((low + high) / 2)
|
|
274
|
+
if (ev(tryNumber, middle)) {
|
|
275
|
+
high = tryNumber - 1
|
|
276
|
+
bestResult = tryNumber
|
|
277
|
+
} else {
|
|
278
|
+
low = tryNumber + 1
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return bestResult
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
const truncateArray = (
|
|
285
|
+
truncatedLength: number,
|
|
286
|
+
originalArray: string[][],
|
|
287
|
+
indexArray: number[],
|
|
288
|
+
middle: boolean
|
|
289
|
+
): { truncated: boolean; truncatedArray: string[][] } => {
|
|
290
|
+
let truncated = false
|
|
291
|
+
const truncatedArray = cloneArray(originalArray)
|
|
292
|
+
|
|
293
|
+
switch (truncatedLength) {
|
|
294
|
+
// truncate nothing
|
|
295
|
+
case 0:
|
|
296
|
+
break
|
|
297
|
+
|
|
298
|
+
// truncate all
|
|
299
|
+
case indexArray.length:
|
|
300
|
+
truncated = true
|
|
301
|
+
|
|
302
|
+
for (let i = 0; i < truncatedArray.length; i++) {
|
|
303
|
+
truncatedArray[i] = []
|
|
304
|
+
}
|
|
305
|
+
truncatedArray[0].push(ellipsis)
|
|
306
|
+
break
|
|
307
|
+
|
|
308
|
+
// truncate a positive amount of elements
|
|
309
|
+
default:
|
|
310
|
+
truncated = true
|
|
311
|
+
|
|
312
|
+
if (middle) {
|
|
313
|
+
const dataHalves: number[] = Array(2)
|
|
314
|
+
// calculate the indexes for two parts
|
|
315
|
+
dataHalves[0] = Math.floor(
|
|
316
|
+
(indexArray.length - truncatedLength - 1) / 2
|
|
317
|
+
)
|
|
318
|
+
dataHalves[1] = dataHalves[0] + truncatedLength + 1
|
|
319
|
+
const nodeHalves = dataHalves.map((index) => indexArray[index])
|
|
320
|
+
const sliceAmounts: number[] = Array(2)
|
|
321
|
+
sliceAmounts[0] = dataHalves[0] + 1
|
|
322
|
+
sliceAmounts[1] =
|
|
323
|
+
indexArray.length - truncatedLength - sliceAmounts[0]
|
|
324
|
+
|
|
325
|
+
// keep the first part
|
|
326
|
+
for (let i = 0; i <= nodeHalves[0]; i++) {
|
|
327
|
+
switch (true) {
|
|
328
|
+
case i < nodeHalves[0]:
|
|
329
|
+
sliceAmounts[0] -= truncatedArray[i].length
|
|
330
|
+
break
|
|
331
|
+
|
|
332
|
+
case i === nodeHalves[0]:
|
|
333
|
+
truncatedArray[i] = originalArray[i].slice(0, sliceAmounts[0])
|
|
334
|
+
truncatedArray[i].push(ellipsis)
|
|
335
|
+
break
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
// and the second part
|
|
340
|
+
for (let i = originalArray.length - 1; i >= nodeHalves[1]; i--) {
|
|
341
|
+
switch (true) {
|
|
342
|
+
case i > nodeHalves[1]:
|
|
343
|
+
sliceAmounts[1] -= originalArray[i].length
|
|
344
|
+
break
|
|
345
|
+
|
|
346
|
+
case i === nodeHalves[1]:
|
|
347
|
+
if (nodeHalves[1] > nodeHalves[0])
|
|
348
|
+
truncatedArray[i] = originalArray[i].slice(-sliceAmounts[1])
|
|
349
|
+
if (nodeHalves[1] === nodeHalves[0])
|
|
350
|
+
truncatedArray[i] = truncatedArray[i].concat(
|
|
351
|
+
originalArray[i].slice(-sliceAmounts[1])
|
|
352
|
+
)
|
|
353
|
+
break
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// delete the middle
|
|
358
|
+
for (let i = nodeHalves[0] + 1; i < nodeHalves[1]; i++) {
|
|
359
|
+
truncatedArray[i] = []
|
|
360
|
+
}
|
|
361
|
+
} else {
|
|
362
|
+
// the right most index to keep from truncating
|
|
363
|
+
const indexToTry = indexArray.length - truncatedLength - 1
|
|
364
|
+
const nodeIndex = indexArray[indexToTry]
|
|
365
|
+
|
|
366
|
+
let sliceAmount = indexToTry + 1
|
|
367
|
+
|
|
368
|
+
for (let i = 0; i < originalArray.length; i++) {
|
|
369
|
+
switch (true) {
|
|
370
|
+
case i < nodeIndex:
|
|
371
|
+
sliceAmount -= truncatedArray[i].length
|
|
372
|
+
break
|
|
373
|
+
|
|
374
|
+
case i === nodeIndex:
|
|
375
|
+
truncatedArray[i] = truncatedArray[i].slice(0, sliceAmount)
|
|
376
|
+
truncatedArray[i].push(ellipsis)
|
|
377
|
+
break
|
|
378
|
+
|
|
379
|
+
case i > nodeIndex:
|
|
380
|
+
truncatedArray[i] = []
|
|
381
|
+
break
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
break
|
|
386
|
+
}
|
|
387
|
+
return { truncated, truncatedArray }
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
const truncateEvaluate = (truncatedLength: number, middle: boolean) => {
|
|
391
|
+
const { truncatedArray } = truncateArray(
|
|
392
|
+
truncatedLength,
|
|
393
|
+
this._defaultStringData,
|
|
394
|
+
this._nodeDataIndexes,
|
|
395
|
+
middle
|
|
396
|
+
)
|
|
397
|
+
return this.checkFit(truncatedArray)
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// find the best number of element to truncate
|
|
401
|
+
const finestMatch = binarySearch(
|
|
402
|
+
0,
|
|
403
|
+
this._nodeDataIndexes.length,
|
|
404
|
+
truncateEvaluate,
|
|
405
|
+
middle,
|
|
406
|
+
this._nodeDataIndexes.length
|
|
407
|
+
)
|
|
408
|
+
|
|
409
|
+
;({ truncated, truncatedArray: stringData } = truncateArray(
|
|
410
|
+
finestMatch!,
|
|
411
|
+
this._defaultStringData,
|
|
412
|
+
this._nodeDataIndexes,
|
|
413
|
+
middle
|
|
414
|
+
))
|
|
415
|
+
|
|
416
|
+
stringData = cleanData(stringData, this._options, true)
|
|
417
|
+
for (let i = 0; i < stringData.length; i++) {
|
|
418
|
+
const data = stringData[i]
|
|
419
|
+
truncatedText += data.join('')
|
|
420
|
+
}
|
|
421
|
+
if (truncated && !middle) {
|
|
422
|
+
truncatedText = cleanString(
|
|
423
|
+
truncatedText.split(ellipsis)[0],
|
|
424
|
+
ignore,
|
|
425
|
+
false,
|
|
426
|
+
true,
|
|
427
|
+
true
|
|
428
|
+
)
|
|
429
|
+
truncatedText += ellipsis
|
|
430
|
+
} else if (truncated && middle) {
|
|
431
|
+
const halves = truncatedText.split(ellipsis)
|
|
432
|
+
truncatedText =
|
|
433
|
+
cleanString(halves[0], ignore, false, true, true) +
|
|
434
|
+
ellipsis +
|
|
435
|
+
cleanString(halves[1], ignore, true, false, true)
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
return {
|
|
439
|
+
isTruncated: truncated,
|
|
440
|
+
text: truncatedText,
|
|
441
|
+
data: stringData,
|
|
442
|
+
constraints: {
|
|
443
|
+
width: this._maxWidth,
|
|
444
|
+
height: this._maxHeight,
|
|
445
|
+
lines: this._maxLines
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
export default truncate
|
package/src/exports/a.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
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
|
+
export { TruncateText } from '../TruncateText/v1'
|
|
25
|
+
export type { TruncateTextProps } from '../TruncateText/v1/props'
|
package/src/exports/b.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
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
|
+
export { TruncateText } from '../TruncateText/v2'
|
|
25
|
+
export type { TruncateTextProps } from '../TruncateText/v2/props'
|