@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,264 @@
|
|
|
1
|
+
var _dec, _class, _TruncateText;
|
|
2
|
+
/*
|
|
3
|
+
* The MIT License (MIT)
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) 2015 - present Instructure, Inc.
|
|
6
|
+
*
|
|
7
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
* in the Software without restriction, including without limitation the rights
|
|
10
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
* furnished to do so, subject to the following conditions:
|
|
13
|
+
*
|
|
14
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
* copies or substantial portions of the Software.
|
|
16
|
+
*
|
|
17
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
* SOFTWARE.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import { Children, Component } from 'react';
|
|
27
|
+
import { debounce } from '@instructure/debounce';
|
|
28
|
+
import { canUseDOM, getBoundingClientRect } from '@instructure/ui-dom-utils';
|
|
29
|
+
import { safeCloneElement, ensureSingleChild } from '@instructure/ui-react-utils';
|
|
30
|
+
import { logError as error } from '@instructure/console';
|
|
31
|
+
import { withStyle } from '@instructure/emotion';
|
|
32
|
+
import generateStyle from "./styles.js";
|
|
33
|
+
import truncate from "./utils/truncate.js";
|
|
34
|
+
import { allowedProps } from "./props.js";
|
|
35
|
+
import { jsx as _jsx, jsxs as _jsxs } from "@emotion/react/jsx-runtime";
|
|
36
|
+
/**
|
|
37
|
+
---
|
|
38
|
+
category: components
|
|
39
|
+
---
|
|
40
|
+
**/
|
|
41
|
+
let TruncateText = (_dec = withStyle(generateStyle), _dec(_class = (_TruncateText = class TruncateText extends Component {
|
|
42
|
+
constructor(props) {
|
|
43
|
+
super(props);
|
|
44
|
+
this.ref = null;
|
|
45
|
+
this._text = void 0;
|
|
46
|
+
this._debounced = void 0;
|
|
47
|
+
this._stage = null;
|
|
48
|
+
this._wasTruncated = void 0;
|
|
49
|
+
this._resizeListener = void 0;
|
|
50
|
+
this._prevWidth = null;
|
|
51
|
+
this.update = () => {
|
|
52
|
+
if (this.ref) {
|
|
53
|
+
this.setState(this.initialState);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
this.state = this.initialState;
|
|
57
|
+
}
|
|
58
|
+
get _ref() {
|
|
59
|
+
return this.ref;
|
|
60
|
+
}
|
|
61
|
+
get initialState() {
|
|
62
|
+
return {
|
|
63
|
+
isTruncated: false,
|
|
64
|
+
needsSecondRender: true,
|
|
65
|
+
truncatedElement: void 0,
|
|
66
|
+
truncatedText: void 0
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
componentDidMount() {
|
|
70
|
+
const _this$props = this.props,
|
|
71
|
+
children = _this$props.children,
|
|
72
|
+
makeStyles = _this$props.makeStyles;
|
|
73
|
+
makeStyles === null || makeStyles === void 0 ? void 0 : makeStyles();
|
|
74
|
+
if (children) {
|
|
75
|
+
var _getBoundingClientRec;
|
|
76
|
+
this.checkChildren();
|
|
77
|
+
const txt = ensureSingleChild(children);
|
|
78
|
+
this._text = txt ? txt : void 0;
|
|
79
|
+
this.truncate();
|
|
80
|
+
this._debounced = debounce(this.update, this.props.debounce, {
|
|
81
|
+
leading: true,
|
|
82
|
+
trailing: true
|
|
83
|
+
});
|
|
84
|
+
this._prevWidth = (_getBoundingClientRec = getBoundingClientRect(this.ref)) === null || _getBoundingClientRec === void 0 ? void 0 : _getBoundingClientRec.width;
|
|
85
|
+
this._resizeListener = new ResizeObserver(entries => {
|
|
86
|
+
// requestAnimationFrame call is needed becuase some truncatetext test cases
|
|
87
|
+
// failed due to ResizeObserver was not able to deliver all observations within a single animation frame
|
|
88
|
+
// see: https://stackoverflow.com/questions/49384120/resizeobserver-loop-limit-exceeded
|
|
89
|
+
requestAnimationFrame(() => {
|
|
90
|
+
for (const entry of entries) {
|
|
91
|
+
const width = entry.contentRect.width;
|
|
92
|
+
if (this._prevWidth !== width) {
|
|
93
|
+
this._prevWidth = width;
|
|
94
|
+
this.props.debounce === 0 ? this.update() : this._debounced();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
this._resizeListener.observe(this.ref);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
componentWillUnmount() {
|
|
103
|
+
if (this._resizeListener) {
|
|
104
|
+
this._resizeListener.disconnect();
|
|
105
|
+
}
|
|
106
|
+
if (this._debounced) {
|
|
107
|
+
this._debounced.cancel();
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
shallowCompare(obj1, obj2) {
|
|
111
|
+
const keys1 = Object.keys(obj1);
|
|
112
|
+
const keys2 = Object.keys(obj2);
|
|
113
|
+
if (keys1.length !== keys2.length) {
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
for (const key of keys1) {
|
|
117
|
+
if (obj1[key] !== obj2[key]) {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
componentDidUpdate(prevProps) {
|
|
124
|
+
const _this$props2 = this.props,
|
|
125
|
+
children = _this$props2.children,
|
|
126
|
+
onUpdate = _this$props2.onUpdate,
|
|
127
|
+
makeStyles = _this$props2.makeStyles;
|
|
128
|
+
makeStyles === null || makeStyles === void 0 ? void 0 : makeStyles();
|
|
129
|
+
const _this$state = this.state,
|
|
130
|
+
isTruncated = _this$state.isTruncated,
|
|
131
|
+
needsSecondRender = _this$state.needsSecondRender,
|
|
132
|
+
truncatedText = _this$state.truncatedText;
|
|
133
|
+
if (children) {
|
|
134
|
+
// for some reason in React 19 prevPros and this.props are a different
|
|
135
|
+
// object even if their contents are the same, so we cannot use !==
|
|
136
|
+
if (!this.shallowCompare(prevProps, this.props)) {
|
|
137
|
+
if (prevProps.children !== this.props.children) {
|
|
138
|
+
// reset internal text variable if children change
|
|
139
|
+
this.checkChildren();
|
|
140
|
+
const txt = ensureSingleChild(children);
|
|
141
|
+
this._text = txt ? txt : void 0;
|
|
142
|
+
}
|
|
143
|
+
// require the double render whenever props change
|
|
144
|
+
this.setState(this.initialState);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
if (!needsSecondRender && (isTruncated || this._wasTruncated)) {
|
|
148
|
+
onUpdate === null || onUpdate === void 0 ? void 0 : onUpdate(isTruncated, truncatedText);
|
|
149
|
+
this._wasTruncated = isTruncated;
|
|
150
|
+
} else {
|
|
151
|
+
this.truncate();
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
checkChildren() {
|
|
156
|
+
error(!(() => {
|
|
157
|
+
let isTooDeep = false;
|
|
158
|
+
const text = ensureSingleChild(this.props.children);
|
|
159
|
+
Children.forEach(text.props.children, child => {
|
|
160
|
+
if (child.props) {
|
|
161
|
+
Children.forEach(child.props.children, grandChild => {
|
|
162
|
+
// currently we don't support node trees deeper than 2 levels
|
|
163
|
+
// truncation will still occur on their text content, but their actual node structure will be lost
|
|
164
|
+
if (grandChild.props) {
|
|
165
|
+
isTooDeep = true;
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
return isTooDeep;
|
|
171
|
+
})(), `[TruncateText] Some children are too deep in the node tree and will not render.`);
|
|
172
|
+
}
|
|
173
|
+
truncate() {
|
|
174
|
+
if (!this.state.needsSecondRender) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
if (canUseDOM) {
|
|
178
|
+
var _this$props$styles;
|
|
179
|
+
const result = truncate(this._stage, {
|
|
180
|
+
...this.props,
|
|
181
|
+
parent: this.ref ? this.ref : void 0,
|
|
182
|
+
lineHeight: (_this$props$styles = this.props.styles) === null || _this$props$styles === void 0 ? void 0 : _this$props$styles.lineHeight
|
|
183
|
+
});
|
|
184
|
+
if (result) {
|
|
185
|
+
const element = this.renderChildren(result.isTruncated, result.data, result.constraints.width);
|
|
186
|
+
this.setState({
|
|
187
|
+
needsSecondRender: false,
|
|
188
|
+
isTruncated: result.isTruncated,
|
|
189
|
+
truncatedElement: element,
|
|
190
|
+
truncatedText: result.text
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
} else {
|
|
194
|
+
var _this$ref, _this$ref2;
|
|
195
|
+
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;
|
|
196
|
+
// if dom isn't available, use original children
|
|
197
|
+
this.setState({
|
|
198
|
+
needsSecondRender: false,
|
|
199
|
+
isTruncated: false,
|
|
200
|
+
truncatedElement: this._text,
|
|
201
|
+
truncatedText: textContent
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
renderChildren(truncated, data, width) {
|
|
206
|
+
var _this$props$styles2;
|
|
207
|
+
if (!truncated) {
|
|
208
|
+
return this._text;
|
|
209
|
+
}
|
|
210
|
+
const childElements = [];
|
|
211
|
+
// iterate over each node used in the truncated string
|
|
212
|
+
for (let i = 0; i < data.length; i++) {
|
|
213
|
+
const item = data[i];
|
|
214
|
+
const element = this._text.props.children[i];
|
|
215
|
+
const nodeText = item.join('');
|
|
216
|
+
if (element && element.props) {
|
|
217
|
+
// if node is an html element and not just a string
|
|
218
|
+
childElements.push(safeCloneElement(element, element.props, nodeText));
|
|
219
|
+
} else {
|
|
220
|
+
childElements.push(nodeText);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
// this spacer element is set to the max width the full text could
|
|
224
|
+
// potentially be without this, text in `width: auto` elements won't expand
|
|
225
|
+
// to accommodate more text, once truncated
|
|
226
|
+
// Breadcrumb is modifying this element's display to inline to prevent layout issues
|
|
227
|
+
// TODO: find a better way to handle this
|
|
228
|
+
childElements.push(_jsx("span", {
|
|
229
|
+
css: (_this$props$styles2 = this.props.styles) === null || _this$props$styles2 === void 0 ? void 0 : _this$props$styles2.spacer,
|
|
230
|
+
style: {
|
|
231
|
+
width: width || void 0
|
|
232
|
+
}
|
|
233
|
+
}, "spacer"));
|
|
234
|
+
const children = Children.map(childElements, child => child);
|
|
235
|
+
return this._text.props ? safeCloneElement(this._text, this._text.props, children) : children;
|
|
236
|
+
}
|
|
237
|
+
render() {
|
|
238
|
+
var _this$props$styles3;
|
|
239
|
+
const truncatedElement = this.state.truncatedElement;
|
|
240
|
+
const children = this.props.children;
|
|
241
|
+
return _jsxs("span", {
|
|
242
|
+
"data-cid": "TruncateText",
|
|
243
|
+
css: (_this$props$styles3 = this.props.styles) === null || _this$props$styles3 === void 0 ? void 0 : _this$props$styles3.truncateText,
|
|
244
|
+
ref: el => {
|
|
245
|
+
this.ref = el;
|
|
246
|
+
},
|
|
247
|
+
children: [children && (truncatedElement ? null : _jsx("span", {
|
|
248
|
+
ref: el => {
|
|
249
|
+
this._stage = el;
|
|
250
|
+
},
|
|
251
|
+
children: ensureSingleChild(children)
|
|
252
|
+
})), truncatedElement]
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
}, _TruncateText.displayName = "TruncateText", _TruncateText.componentId = 'TruncateText', _TruncateText.allowedProps = allowedProps, _TruncateText.defaultProps = {
|
|
256
|
+
maxLines: 1,
|
|
257
|
+
ellipsis: '\u2026',
|
|
258
|
+
truncate: 'character',
|
|
259
|
+
position: 'end',
|
|
260
|
+
ignore: [' ', '.', ','],
|
|
261
|
+
debounce: 0
|
|
262
|
+
}, _TruncateText)) || _class);
|
|
263
|
+
export default TruncateText;
|
|
264
|
+
export { TruncateText };
|
|
@@ -0,0 +1,26 @@
|
|
|
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
|
+
const allowedProps = ['children', 'maxLines', 'position', 'truncate', 'ellipsis', 'ignore', 'debounce', 'onUpdate', 'shouldTruncateWhenInvisible'];
|
|
26
|
+
export { allowedProps };
|
|
@@ -0,0 +1,58 @@
|
|
|
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
|
+
* private: true
|
|
28
|
+
* ---
|
|
29
|
+
* Generates the style object from the theme and provided additional information
|
|
30
|
+
* @param {Object} componentTheme The theme variable object.
|
|
31
|
+
* @param {Object} props the props of the component, the style is applied to
|
|
32
|
+
* @param {Object} state the state of the component, the style is applied to
|
|
33
|
+
* @return {Object} The final style object, which will be used in the component
|
|
34
|
+
*/
|
|
35
|
+
const generateStyle = componentTheme => {
|
|
36
|
+
return {
|
|
37
|
+
truncateText: {
|
|
38
|
+
label: 'truncateText',
|
|
39
|
+
display: 'block',
|
|
40
|
+
overflow: 'hidden',
|
|
41
|
+
wordWrap: 'break-word',
|
|
42
|
+
whiteSpace: 'normal',
|
|
43
|
+
fontFamily: componentTheme.fontFamily
|
|
44
|
+
},
|
|
45
|
+
auto: {
|
|
46
|
+
label: 'truncateText__auto',
|
|
47
|
+
height: '100%'
|
|
48
|
+
},
|
|
49
|
+
spacer: {
|
|
50
|
+
label: 'truncateText__spacer',
|
|
51
|
+
visibility: 'hidden',
|
|
52
|
+
maxHeight: '0',
|
|
53
|
+
display: 'block'
|
|
54
|
+
},
|
|
55
|
+
lineHeight: componentTheme.lineHeight
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
export default generateStyle;
|
|
@@ -0,0 +1,158 @@
|
|
|
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
|
+
/**
|
|
27
|
+
* ---
|
|
28
|
+
* parent: TruncateText
|
|
29
|
+
* private: true
|
|
30
|
+
* ---
|
|
31
|
+
* Removes given characters, such as whitespace and punctuation, from stringData
|
|
32
|
+
* arrays used by Truncator and TruncateText.
|
|
33
|
+
*
|
|
34
|
+
* @param {Array} stringData A stringData array.
|
|
35
|
+
* @param {Object} options The options object.
|
|
36
|
+
* @param {string} options.truncate Is the data being truncated by character or word.
|
|
37
|
+
* @param {string[]} options.ignore An array of characters to remove around the ellipsis.
|
|
38
|
+
* @param {string} options.ellipsis The string being used as an ellipsis.
|
|
39
|
+
* @param {boolean} repeat=false Do a thorough clean.
|
|
40
|
+
*/
|
|
41
|
+
function cleanData(stringData, options, repeat = false) {
|
|
42
|
+
const truncate = options.truncate,
|
|
43
|
+
ignore = options.ignore,
|
|
44
|
+
ellipsis = options.ellipsis;
|
|
45
|
+
let newData = cloneArray(stringData);
|
|
46
|
+
let ellipsisNode = -1;
|
|
47
|
+
let ellipsisIndex = -1;
|
|
48
|
+
const findEllipsis = () => {
|
|
49
|
+
for (let i = 0; i < newData.length; i++) {
|
|
50
|
+
const nodeData = newData[i];
|
|
51
|
+
if (nodeData.indexOf(ellipsis) !== -1) {
|
|
52
|
+
ellipsisNode = i;
|
|
53
|
+
ellipsisIndex = nodeData.indexOf(ellipsis);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
if (truncate === 'character') {
|
|
58
|
+
findEllipsis();
|
|
59
|
+
let node = newData[ellipsisNode];
|
|
60
|
+
if (node) {
|
|
61
|
+
const before = node[ellipsisIndex - 1];
|
|
62
|
+
if (before && ignore.indexOf(before) !== -1) {
|
|
63
|
+
// remove character immediately BEFORE the ellipsis in the same node
|
|
64
|
+
newData[ellipsisNode].splice(ellipsisIndex - 1, 1);
|
|
65
|
+
}
|
|
66
|
+
if (!before) {
|
|
67
|
+
// character before the ellipsis is part of a different node
|
|
68
|
+
// find the next node with data and remove last datum
|
|
69
|
+
let prevNode = null;
|
|
70
|
+
let prevNodeIndex = ellipsisNode - 1;
|
|
71
|
+
while (prevNodeIndex >= 0) {
|
|
72
|
+
prevNode = newData[prevNodeIndex];
|
|
73
|
+
if (prevNode.length > 0) {
|
|
74
|
+
break;
|
|
75
|
+
} else {
|
|
76
|
+
prevNodeIndex--;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (prevNode) {
|
|
80
|
+
const lastChar = String(prevNode.slice(-1));
|
|
81
|
+
if (ignore.indexOf(lastChar) !== -1) {
|
|
82
|
+
newData[prevNodeIndex].length -= 1;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
findEllipsis();
|
|
88
|
+
node = newData[ellipsisNode];
|
|
89
|
+
if (node) {
|
|
90
|
+
const after = node[ellipsisIndex + 1];
|
|
91
|
+
if (after && ignore.indexOf(after) !== -1) {
|
|
92
|
+
// remove character immediately AFTER the ellipsis in the same node
|
|
93
|
+
newData[ellipsisNode].splice(ellipsisIndex + 1, 1);
|
|
94
|
+
}
|
|
95
|
+
if (!after) {
|
|
96
|
+
// character after the ellipsis is part of a different node
|
|
97
|
+
// find the next node with data and remove first datum
|
|
98
|
+
let nextNode = null;
|
|
99
|
+
let nextNodeIndex = ellipsisNode + 1;
|
|
100
|
+
while (nextNodeIndex < newData.length) {
|
|
101
|
+
nextNode = newData[nextNodeIndex];
|
|
102
|
+
if (nextNode.length > 0) {
|
|
103
|
+
break;
|
|
104
|
+
} else {
|
|
105
|
+
nextNodeIndex++;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (nextNode) {
|
|
109
|
+
const firstChar = String(nextNode[0]);
|
|
110
|
+
if (ignore.indexOf(firstChar) !== -1) {
|
|
111
|
+
newData[nextNodeIndex].shift();
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
} else {
|
|
117
|
+
findEllipsis();
|
|
118
|
+
const node = newData[ellipsisNode];
|
|
119
|
+
if (node) {
|
|
120
|
+
const before = node[ellipsisIndex - 1];
|
|
121
|
+
if (before && ignore.indexOf(before.slice(-1)) !== -1) {
|
|
122
|
+
if (before.length === 1) {
|
|
123
|
+
// remove entire word datum
|
|
124
|
+
newData[ellipsisNode].splice(ellipsisIndex - 1, 1);
|
|
125
|
+
} else {
|
|
126
|
+
// remove word immediately BEFORE the ellipsis in the same node
|
|
127
|
+
newData[ellipsisNode][ellipsisIndex - 1] = before.slice(0, -1);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if (!before) {
|
|
131
|
+
// word before the ellipsis is part of a different node
|
|
132
|
+
// find the next node with data and remove last datum
|
|
133
|
+
let prevNode = null;
|
|
134
|
+
let prevNodeIndex = ellipsisNode - 1;
|
|
135
|
+
while (prevNodeIndex >= 0) {
|
|
136
|
+
prevNode = newData[prevNodeIndex];
|
|
137
|
+
if (prevNode.length > 0) {
|
|
138
|
+
break;
|
|
139
|
+
} else {
|
|
140
|
+
prevNodeIndex--;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (prevNode) {
|
|
144
|
+
const lastChar = String(prevNode.slice(-1)).slice(-1);
|
|
145
|
+
if (ignore.indexOf(lastChar) !== -1) {
|
|
146
|
+
const lastItem = prevNode.length - 1;
|
|
147
|
+
newData[prevNodeIndex][lastItem] = prevNode[lastItem].slice(0, -1);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
if (repeat) {
|
|
154
|
+
newData = cleanData(newData, options, false);
|
|
155
|
+
}
|
|
156
|
+
return newData;
|
|
157
|
+
}
|
|
158
|
+
export default cleanData;
|
|
@@ -0,0 +1,53 @@
|
|
|
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(string, ignore, start = true, end = true, repeat = false) {
|
|
39
|
+
let text = string;
|
|
40
|
+
const firstChar = text.charAt(0);
|
|
41
|
+
const lastChar = text.slice(-1);
|
|
42
|
+
if (start && ignore.indexOf(firstChar) !== -1) {
|
|
43
|
+
text = text.slice(1);
|
|
44
|
+
}
|
|
45
|
+
if (end && ignore.indexOf(lastChar) !== -1) {
|
|
46
|
+
text = text.slice(0, -1);
|
|
47
|
+
}
|
|
48
|
+
if (repeat) {
|
|
49
|
+
text = cleanString(text, ignore, start, end, false);
|
|
50
|
+
}
|
|
51
|
+
return text;
|
|
52
|
+
}
|
|
53
|
+
export default cleanString;
|
|
@@ -0,0 +1,74 @@
|
|
|
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, parentNode) {
|
|
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
|
+
function measure(string, domNode) {
|
|
46
|
+
const style = getCSSStyleDeclaration(domNode);
|
|
47
|
+
// we use a canvas in a doc fragment to prevent having to render the string full width in the DOM
|
|
48
|
+
const canvas = document.createElement('canvas');
|
|
49
|
+
document.createDocumentFragment().appendChild(canvas);
|
|
50
|
+
const context = canvas.getContext('2d');
|
|
51
|
+
if (!context || !string) {
|
|
52
|
+
return 0;
|
|
53
|
+
}
|
|
54
|
+
let text = string;
|
|
55
|
+
let letterOffset = 0;
|
|
56
|
+
let width = 0;
|
|
57
|
+
// grab individual font styles
|
|
58
|
+
// some browsers don't report a value for style['font']
|
|
59
|
+
context.font = [style === null || style === void 0 ? void 0 : style.fontWeight, style === null || style === void 0 ? void 0 : style.fontStyle, style === null || style === void 0 ? void 0 : style.fontSize, style === null || style === void 0 ? void 0 : style.fontFamily].join(' ');
|
|
60
|
+
if ((style === null || style === void 0 ? void 0 : style.textTransform) === 'uppercase') {
|
|
61
|
+
text = string.toUpperCase();
|
|
62
|
+
} else if ((style === null || style === void 0 ? void 0 : style.textTransform) === 'lowercase') {
|
|
63
|
+
text = string.toLowerCase();
|
|
64
|
+
} else if ((style === null || style === void 0 ? void 0 : style.textTransform) === 'capitalize') {
|
|
65
|
+
text = string.replace(/\b\w/g, str => str.toUpperCase());
|
|
66
|
+
}
|
|
67
|
+
if ((style === null || style === void 0 ? void 0 : style.letterSpacing) !== 'normal') {
|
|
68
|
+
letterOffset = text.length * parseInt(style === null || style === void 0 ? void 0 : style.letterSpacing);
|
|
69
|
+
}
|
|
70
|
+
width = context.measureText(text).width + letterOffset;
|
|
71
|
+
// returns the max potential width of the text, assuming the text was on a single line
|
|
72
|
+
return width;
|
|
73
|
+
}
|
|
74
|
+
export default measureText;
|