@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,60 @@
|
|
|
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 type { NewComponentTypes } from '@instructure/ui-themes'
|
|
26
|
+
import type { TruncateTextStyle } from './props'
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* ---
|
|
30
|
+
* private: true
|
|
31
|
+
* ---
|
|
32
|
+
* Generates the style object from the theme and provided additional information
|
|
33
|
+
* @param {Object} componentTheme The theme variable object.
|
|
34
|
+
* @param {Object} props the props of the component, the style is applied to
|
|
35
|
+
* @param {Object} state the state of the component, the style is applied to
|
|
36
|
+
* @return {Object} The final style object, which will be used in the component
|
|
37
|
+
*/
|
|
38
|
+
const generateStyle = (
|
|
39
|
+
componentTheme: NewComponentTypes['TruncateText']
|
|
40
|
+
): TruncateTextStyle => {
|
|
41
|
+
return {
|
|
42
|
+
truncateText: {
|
|
43
|
+
label: 'truncateText',
|
|
44
|
+
display: 'block',
|
|
45
|
+
overflow: 'hidden',
|
|
46
|
+
wordWrap: 'break-word',
|
|
47
|
+
whiteSpace: 'normal',
|
|
48
|
+
fontFamily: componentTheme.fontFamily
|
|
49
|
+
},
|
|
50
|
+
auto: { label: 'truncateText__auto', height: '100%' },
|
|
51
|
+
spacer: {
|
|
52
|
+
label: 'truncateText__spacer',
|
|
53
|
+
visibility: 'hidden',
|
|
54
|
+
maxHeight: '0',
|
|
55
|
+
display: 'block'
|
|
56
|
+
},
|
|
57
|
+
lineHeight: componentTheme.lineHeight
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
export default generateStyle
|
|
@@ -0,0 +1,178 @@
|
|
|
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 { cloneArray } from '@instructure/ui-utils'
|
|
26
|
+
import type { CleanDataOptions } from '../props'
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* ---
|
|
30
|
+
* parent: TruncateText
|
|
31
|
+
* private: true
|
|
32
|
+
* ---
|
|
33
|
+
* Removes given characters, such as whitespace and punctuation, from stringData
|
|
34
|
+
* arrays used by Truncator and TruncateText.
|
|
35
|
+
*
|
|
36
|
+
* @param {Array} stringData A stringData array.
|
|
37
|
+
* @param {Object} options The options object.
|
|
38
|
+
* @param {string} options.truncate Is the data being truncated by character or word.
|
|
39
|
+
* @param {string[]} options.ignore An array of characters to remove around the ellipsis.
|
|
40
|
+
* @param {string} options.ellipsis The string being used as an ellipsis.
|
|
41
|
+
* @param {boolean} repeat=false Do a thorough clean.
|
|
42
|
+
*/
|
|
43
|
+
function cleanData(
|
|
44
|
+
stringData: string[][],
|
|
45
|
+
options: Required<CleanDataOptions>,
|
|
46
|
+
repeat = false
|
|
47
|
+
) {
|
|
48
|
+
const { truncate, ignore, ellipsis } = options
|
|
49
|
+
|
|
50
|
+
let newData = cloneArray(stringData)
|
|
51
|
+
let ellipsisNode = -1
|
|
52
|
+
let ellipsisIndex = -1
|
|
53
|
+
|
|
54
|
+
const findEllipsis = () => {
|
|
55
|
+
for (let i = 0; i < newData.length; i++) {
|
|
56
|
+
const nodeData = newData[i]
|
|
57
|
+
if (nodeData.indexOf(ellipsis) !== -1) {
|
|
58
|
+
ellipsisNode = i
|
|
59
|
+
ellipsisIndex = nodeData.indexOf(ellipsis)
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (truncate === 'character') {
|
|
65
|
+
findEllipsis()
|
|
66
|
+
|
|
67
|
+
let node = newData[ellipsisNode]
|
|
68
|
+
|
|
69
|
+
if (node) {
|
|
70
|
+
const before = node[ellipsisIndex - 1]
|
|
71
|
+
if (before && ignore.indexOf(before) !== -1) {
|
|
72
|
+
// remove character immediately BEFORE the ellipsis in the same node
|
|
73
|
+
newData[ellipsisNode].splice(ellipsisIndex - 1, 1)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (!before) {
|
|
77
|
+
// character before the ellipsis is part of a different node
|
|
78
|
+
// find the next node with data and remove last datum
|
|
79
|
+
let prevNode = null
|
|
80
|
+
let prevNodeIndex = ellipsisNode - 1
|
|
81
|
+
while (prevNodeIndex >= 0) {
|
|
82
|
+
prevNode = newData[prevNodeIndex]
|
|
83
|
+
if (prevNode.length > 0) {
|
|
84
|
+
break
|
|
85
|
+
} else {
|
|
86
|
+
prevNodeIndex--
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (prevNode) {
|
|
91
|
+
const lastChar = String(prevNode.slice(-1))
|
|
92
|
+
if (ignore.indexOf(lastChar) !== -1) {
|
|
93
|
+
newData[prevNodeIndex].length -= 1
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
findEllipsis()
|
|
100
|
+
|
|
101
|
+
node = newData[ellipsisNode]
|
|
102
|
+
|
|
103
|
+
if (node) {
|
|
104
|
+
const after = node[ellipsisIndex + 1]
|
|
105
|
+
if (after && ignore.indexOf(after) !== -1) {
|
|
106
|
+
// remove character immediately AFTER the ellipsis in the same node
|
|
107
|
+
newData[ellipsisNode].splice(ellipsisIndex + 1, 1)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (!after) {
|
|
111
|
+
// character after the ellipsis is part of a different node
|
|
112
|
+
// find the next node with data and remove first datum
|
|
113
|
+
let nextNode = null
|
|
114
|
+
let nextNodeIndex = ellipsisNode + 1
|
|
115
|
+
while (nextNodeIndex < newData.length) {
|
|
116
|
+
nextNode = newData[nextNodeIndex]
|
|
117
|
+
if (nextNode.length > 0) {
|
|
118
|
+
break
|
|
119
|
+
} else {
|
|
120
|
+
nextNodeIndex++
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
if (nextNode) {
|
|
124
|
+
const firstChar = String(nextNode[0])
|
|
125
|
+
if (ignore.indexOf(firstChar) !== -1) {
|
|
126
|
+
newData[nextNodeIndex].shift()
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
} else {
|
|
132
|
+
findEllipsis()
|
|
133
|
+
|
|
134
|
+
const node = newData[ellipsisNode]
|
|
135
|
+
if (node) {
|
|
136
|
+
const before = node[ellipsisIndex - 1]
|
|
137
|
+
if (before && ignore.indexOf(before.slice(-1)) !== -1) {
|
|
138
|
+
if (before.length === 1) {
|
|
139
|
+
// remove entire word datum
|
|
140
|
+
newData[ellipsisNode].splice(ellipsisIndex - 1, 1)
|
|
141
|
+
} else {
|
|
142
|
+
// remove word immediately BEFORE the ellipsis in the same node
|
|
143
|
+
newData[ellipsisNode][ellipsisIndex - 1] = before.slice(0, -1)
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
if (!before) {
|
|
147
|
+
// word before the ellipsis is part of a different node
|
|
148
|
+
// find the next node with data and remove last datum
|
|
149
|
+
let prevNode = null
|
|
150
|
+
let prevNodeIndex = ellipsisNode - 1
|
|
151
|
+
while (prevNodeIndex >= 0) {
|
|
152
|
+
prevNode = newData[prevNodeIndex]
|
|
153
|
+
if (prevNode.length > 0) {
|
|
154
|
+
break
|
|
155
|
+
} else {
|
|
156
|
+
prevNodeIndex--
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (prevNode) {
|
|
161
|
+
const lastChar = String(prevNode.slice(-1)).slice(-1)
|
|
162
|
+
if (ignore.indexOf(lastChar) !== -1) {
|
|
163
|
+
const lastItem = prevNode.length - 1
|
|
164
|
+
newData[prevNodeIndex][lastItem] = prevNode[lastItem].slice(0, -1)
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (repeat) {
|
|
172
|
+
newData = cleanData(newData, options, false)
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return newData
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export default cleanData
|
|
@@ -0,0 +1,61 @@
|
|
|
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
|
+
/**
|
|
26
|
+
* ---
|
|
27
|
+
* parent: TruncateText
|
|
28
|
+
* private: true
|
|
29
|
+
* ---
|
|
30
|
+
* Removes given characters, such as whitespace and punctuation, from either end of a string.
|
|
31
|
+
*
|
|
32
|
+
* @param {string} string The string to clean.
|
|
33
|
+
* @param {string[]} ignore Array of characters to remove.
|
|
34
|
+
* @param {boolean} start Whether or not to clean start of string.
|
|
35
|
+
* @param {boolean} end Whether or not to clean end of string.
|
|
36
|
+
* @param {boolean} repeat=false Do a thorough clean.
|
|
37
|
+
*/
|
|
38
|
+
function cleanString(
|
|
39
|
+
string: string,
|
|
40
|
+
ignore: string[],
|
|
41
|
+
start = true,
|
|
42
|
+
end = true,
|
|
43
|
+
repeat = false
|
|
44
|
+
) {
|
|
45
|
+
let text = string
|
|
46
|
+
const firstChar = text.charAt(0)
|
|
47
|
+
const lastChar = text.slice(-1)
|
|
48
|
+
|
|
49
|
+
if (start && ignore.indexOf(firstChar) !== -1) {
|
|
50
|
+
text = text.slice(1)
|
|
51
|
+
}
|
|
52
|
+
if (end && ignore.indexOf(lastChar) !== -1) {
|
|
53
|
+
text = text.slice(0, -1)
|
|
54
|
+
}
|
|
55
|
+
if (repeat) {
|
|
56
|
+
text = cleanString(text, ignore, start, end, false)
|
|
57
|
+
}
|
|
58
|
+
return text
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export default cleanString
|
|
@@ -0,0 +1,86 @@
|
|
|
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 { getCSSStyleDeclaration } from '@instructure/ui-dom-utils'
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* ---
|
|
29
|
+
* parent: TruncateText
|
|
30
|
+
* private: true
|
|
31
|
+
* ---
|
|
32
|
+
* Finds the max potential width of set of DOM nodes.
|
|
33
|
+
*
|
|
34
|
+
* @param {DOMNode[]} nodes Array of DOM nodes.
|
|
35
|
+
* @param {DOMNode} parentNode The node to inherit default styles from.
|
|
36
|
+
*/
|
|
37
|
+
function measureText(nodes: Node[], parentNode?: Node) {
|
|
38
|
+
let width = 0
|
|
39
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
40
|
+
const node = nodes[i]
|
|
41
|
+
width += measure(node.textContent, node.nodeType === 1 ? node : parentNode)
|
|
42
|
+
}
|
|
43
|
+
return width
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function measure(string: string | null, domNode?: Node) {
|
|
47
|
+
const style = getCSSStyleDeclaration(domNode)
|
|
48
|
+
// we use a canvas in a doc fragment to prevent having to render the string full width in the DOM
|
|
49
|
+
const canvas = document.createElement('canvas')
|
|
50
|
+
document.createDocumentFragment().appendChild(canvas)
|
|
51
|
+
|
|
52
|
+
const context = canvas.getContext('2d')
|
|
53
|
+
if (!context || !string) {
|
|
54
|
+
return 0
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
let text = string
|
|
58
|
+
let letterOffset = 0
|
|
59
|
+
let width = 0
|
|
60
|
+
// grab individual font styles
|
|
61
|
+
// some browsers don't report a value for style['font']
|
|
62
|
+
context.font = [
|
|
63
|
+
style?.fontWeight,
|
|
64
|
+
style?.fontStyle,
|
|
65
|
+
style?.fontSize,
|
|
66
|
+
style?.fontFamily
|
|
67
|
+
].join(' ')
|
|
68
|
+
|
|
69
|
+
if (style?.textTransform === 'uppercase') {
|
|
70
|
+
text = string.toUpperCase()
|
|
71
|
+
} else if (style?.textTransform === 'lowercase') {
|
|
72
|
+
text = string.toLowerCase()
|
|
73
|
+
} else if (style?.textTransform === 'capitalize') {
|
|
74
|
+
text = string.replace(/\b\w/g, (str: string) => str.toUpperCase())
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (style?.letterSpacing !== 'normal') {
|
|
78
|
+
letterOffset = text.length * parseInt(style?.letterSpacing as string)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
width = context.measureText(text).width + letterOffset
|
|
82
|
+
// returns the max potential width of the text, assuming the text was on a single line
|
|
83
|
+
return width
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export default measureText
|