UNPKG

21.3 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5var _typeof3 = require("@babel/runtime/helpers/typeof");
6
7Object.defineProperty(exports, "__esModule", {
8 value: true
9});
10exports["default"] = void 0;
11
12var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
13
14var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
15
16var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
17
18var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
19
20var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
21
22var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
23
24var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
25
26var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));
27
28var React = _interopRequireWildcard(require("react"));
29
30var _classnames = _interopRequireDefault(require("classnames"));
31
32var _toArray = _interopRequireDefault(require("rc-util/lib/Children/toArray"));
33
34var _copyToClipboard = _interopRequireDefault(require("copy-to-clipboard"));
35
36var _omit = _interopRequireDefault(require("rc-util/lib/omit"));
37
38var _EditOutlined = _interopRequireDefault(require("@ant-design/icons/EditOutlined"));
39
40var _CheckOutlined = _interopRequireDefault(require("@ant-design/icons/CheckOutlined"));
41
42var _CopyOutlined = _interopRequireDefault(require("@ant-design/icons/CopyOutlined"));
43
44var _rcResizeObserver = _interopRequireDefault(require("rc-resize-observer"));
45
46var _configProvider = require("../config-provider");
47
48var _LocaleReceiver = _interopRequireDefault(require("../locale-provider/LocaleReceiver"));
49
50var _devWarning = _interopRequireDefault(require("../_util/devWarning"));
51
52var _transButton = _interopRequireDefault(require("../_util/transButton"));
53
54var _raf = _interopRequireDefault(require("../_util/raf"));
55
56var _styleChecker = require("../_util/styleChecker");
57
58var _tooltip = _interopRequireDefault(require("../tooltip"));
59
60var _Typography = _interopRequireDefault(require("./Typography"));
61
62var _Editable = _interopRequireDefault(require("./Editable"));
63
64var _util = _interopRequireDefault(require("./util"));
65
66function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
67
68function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof3(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
69
70var __rest = void 0 && (void 0).__rest || function (s, e) {
71 var t = {};
72
73 for (var p in s) {
74 if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
75 }
76
77 if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
78 if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
79 }
80 return t;
81};
82
83var isLineClampSupport = (0, _styleChecker.isStyleSupport)('webkitLineClamp');
84var isTextOverflowSupport = (0, _styleChecker.isStyleSupport)('textOverflow');
85
86function wrapperDecorations(_ref, content) {
87 var mark = _ref.mark,
88 code = _ref.code,
89 underline = _ref.underline,
90 del = _ref["delete"],
91 strong = _ref.strong,
92 keyboard = _ref.keyboard,
93 italic = _ref.italic;
94 var currentContent = content;
95
96 function wrap(needed, tag) {
97 if (!needed) return;
98 currentContent = /*#__PURE__*/React.createElement(tag, {}, currentContent);
99 }
100
101 wrap(strong, 'strong');
102 wrap(underline, 'u');
103 wrap(del, 'del');
104 wrap(code, 'code');
105 wrap(mark, 'mark');
106 wrap(keyboard, 'kbd');
107 wrap(italic, 'i');
108 return currentContent;
109}
110
111function getNode(dom, defaultNode, needDom) {
112 if (dom === true || dom === undefined) {
113 return defaultNode;
114 }
115
116 return dom || needDom && defaultNode;
117}
118
119var ELLIPSIS_STR = '...';
120
121var Base = /*#__PURE__*/function (_React$Component) {
122 (0, _inherits2["default"])(Base, _React$Component);
123
124 var _super = (0, _createSuper2["default"])(Base);
125
126 function Base() {
127 var _this;
128
129 (0, _classCallCheck2["default"])(this, Base);
130 _this = _super.apply(this, arguments);
131 _this.contentRef = /*#__PURE__*/React.createRef();
132 _this.state = {
133 edit: false,
134 copied: false,
135 ellipsisText: '',
136 ellipsisContent: null,
137 isEllipsis: false,
138 expanded: false,
139 clientRendered: false
140 };
141
142 _this.getPrefixCls = function () {
143 var customizePrefixCls = _this.props.prefixCls;
144 var getPrefixCls = _this.context.getPrefixCls;
145 return getPrefixCls('typography', customizePrefixCls);
146 }; // =============== Expand ===============
147
148
149 _this.onExpandClick = function (e) {
150 var _a;
151
152 var _this$getEllipsis = _this.getEllipsis(),
153 onExpand = _this$getEllipsis.onExpand;
154
155 _this.setState({
156 expanded: true
157 });
158
159 (_a = onExpand) === null || _a === void 0 ? void 0 : _a(e);
160 }; // ================ Edit ================
161
162
163 _this.onEditClick = function (e) {
164 e.preventDefault();
165
166 _this.triggerEdit(true);
167 };
168
169 _this.onEditChange = function (value) {
170 var _this$getEditable = _this.getEditable(),
171 onChange = _this$getEditable.onChange;
172
173 onChange === null || onChange === void 0 ? void 0 : onChange(value);
174
175 _this.triggerEdit(false);
176 };
177
178 _this.onEditCancel = function () {
179 var _a, _b;
180
181 (_b = (_a = _this.getEditable()).onCancel) === null || _b === void 0 ? void 0 : _b.call(_a);
182
183 _this.triggerEdit(false);
184 }; // ================ Copy ================
185
186
187 _this.onCopyClick = function (e) {
188 e.preventDefault();
189 var _this$props = _this.props,
190 children = _this$props.children,
191 copyable = _this$props.copyable;
192 var copyConfig = (0, _extends2["default"])({}, (0, _typeof2["default"])(copyable) === 'object' ? copyable : null);
193
194 if (copyConfig.text === undefined) {
195 copyConfig.text = String(children);
196 }
197
198 (0, _copyToClipboard["default"])(copyConfig.text || '');
199
200 _this.setState({
201 copied: true
202 }, function () {
203 if (copyConfig.onCopy) {
204 copyConfig.onCopy();
205 }
206
207 _this.copyId = window.setTimeout(function () {
208 _this.setState({
209 copied: false
210 });
211 }, 3000);
212 });
213 };
214
215 _this.setEditRef = function (node) {
216 _this.editIcon = node;
217 };
218
219 _this.triggerEdit = function (edit) {
220 var _this$getEditable2 = _this.getEditable(),
221 onStart = _this$getEditable2.onStart;
222
223 if (edit && onStart) {
224 onStart();
225 }
226
227 _this.setState({
228 edit: edit
229 }, function () {
230 if (!edit && _this.editIcon) {
231 _this.editIcon.focus();
232 }
233 });
234 }; // ============== Ellipsis ==============
235
236
237 _this.resizeOnNextFrame = function () {
238 _raf["default"].cancel(_this.rafId);
239
240 _this.rafId = (0, _raf["default"])(function () {
241 // Do not bind `syncEllipsis`. It need for test usage on prototype
242 _this.syncEllipsis();
243 });
244 };
245
246 return _this;
247 }
248
249 (0, _createClass2["default"])(Base, [{
250 key: "componentDidMount",
251 value: function componentDidMount() {
252 this.setState({
253 clientRendered: true
254 });
255 this.resizeOnNextFrame();
256 }
257 }, {
258 key: "componentDidUpdate",
259 value: function componentDidUpdate(prevProps) {
260 var children = this.props.children;
261 var ellipsis = this.getEllipsis();
262 var prevEllipsis = this.getEllipsis(prevProps);
263
264 if (children !== prevProps.children || ellipsis.rows !== prevEllipsis.rows) {
265 this.resizeOnNextFrame();
266 }
267 }
268 }, {
269 key: "componentWillUnmount",
270 value: function componentWillUnmount() {
271 window.clearTimeout(this.copyId);
272
273 _raf["default"].cancel(this.rafId);
274 }
275 }, {
276 key: "getEditable",
277 value: function getEditable(props) {
278 var edit = this.state.edit;
279
280 var _ref2 = props || this.props,
281 editable = _ref2.editable;
282
283 if (!editable) return {
284 editing: edit
285 };
286 return (0, _extends2["default"])({
287 editing: edit
288 }, (0, _typeof2["default"])(editable) === 'object' ? editable : null);
289 }
290 }, {
291 key: "getEllipsis",
292 value: function getEllipsis(props) {
293 var _ref3 = props || this.props,
294 ellipsis = _ref3.ellipsis;
295
296 if (!ellipsis) return {};
297 return (0, _extends2["default"])({
298 rows: 1,
299 expandable: false
300 }, (0, _typeof2["default"])(ellipsis) === 'object' ? ellipsis : null);
301 }
302 }, {
303 key: "canUseCSSEllipsis",
304 value: function canUseCSSEllipsis() {
305 var clientRendered = this.state.clientRendered;
306 var _this$props2 = this.props,
307 editable = _this$props2.editable,
308 copyable = _this$props2.copyable;
309
310 var _this$getEllipsis2 = this.getEllipsis(),
311 rows = _this$getEllipsis2.rows,
312 expandable = _this$getEllipsis2.expandable,
313 suffix = _this$getEllipsis2.suffix,
314 onEllipsis = _this$getEllipsis2.onEllipsis,
315 tooltip = _this$getEllipsis2.tooltip;
316
317 if (suffix || tooltip) return false; // Can't use css ellipsis since we need to provide the place for button
318
319 if (editable || copyable || expandable || !clientRendered || onEllipsis) {
320 return false;
321 }
322
323 if (rows === 1) {
324 return isTextOverflowSupport;
325 }
326
327 return isLineClampSupport;
328 }
329 }, {
330 key: "syncEllipsis",
331 value: function syncEllipsis() {
332 var _this$state = this.state,
333 ellipsisText = _this$state.ellipsisText,
334 isEllipsis = _this$state.isEllipsis,
335 expanded = _this$state.expanded;
336
337 var _this$getEllipsis3 = this.getEllipsis(),
338 rows = _this$getEllipsis3.rows,
339 suffix = _this$getEllipsis3.suffix,
340 onEllipsis = _this$getEllipsis3.onEllipsis;
341
342 var children = this.props.children;
343 if (!rows || rows < 0 || !this.contentRef.current || expanded) return; // Do not measure if css already support ellipsis
344
345 if (this.canUseCSSEllipsis()) return;
346 (0, _devWarning["default"])((0, _toArray["default"])(children).every(function (child) {
347 return typeof child === 'string';
348 }), 'Typography', '`ellipsis` should use string as children only.');
349
350 var _measure = (0, _util["default"])(this.contentRef.current, {
351 rows: rows,
352 suffix: suffix
353 }, children, this.renderOperations(true), ELLIPSIS_STR),
354 content = _measure.content,
355 text = _measure.text,
356 ellipsis = _measure.ellipsis;
357
358 if (ellipsisText !== text || isEllipsis !== ellipsis) {
359 this.setState({
360 ellipsisText: text,
361 ellipsisContent: content,
362 isEllipsis: ellipsis
363 });
364
365 if (isEllipsis !== ellipsis && onEllipsis) {
366 onEllipsis(ellipsis);
367 }
368 }
369 }
370 }, {
371 key: "renderExpand",
372 value: function renderExpand(forceRender) {
373 var _this$getEllipsis4 = this.getEllipsis(),
374 expandable = _this$getEllipsis4.expandable,
375 symbol = _this$getEllipsis4.symbol;
376
377 var _this$state2 = this.state,
378 expanded = _this$state2.expanded,
379 isEllipsis = _this$state2.isEllipsis;
380 if (!expandable) return null; // force render expand icon for measure usage or it will cause dead loop
381
382 if (!forceRender && (expanded || !isEllipsis)) return null;
383 var expandContent;
384
385 if (symbol) {
386 expandContent = symbol;
387 } else {
388 expandContent = this.expandStr;
389 }
390
391 return /*#__PURE__*/React.createElement("a", {
392 key: "expand",
393 className: "".concat(this.getPrefixCls(), "-expand"),
394 onClick: this.onExpandClick,
395 "aria-label": this.expandStr
396 }, expandContent);
397 }
398 }, {
399 key: "renderEdit",
400 value: function renderEdit() {
401 var editable = this.props.editable;
402 if (!editable) return;
403 var icon = editable.icon,
404 tooltip = editable.tooltip;
405 var title = (0, _toArray["default"])(tooltip)[0] || this.editStr;
406 var ariaLabel = typeof title === 'string' ? title : '';
407 return /*#__PURE__*/React.createElement(_tooltip["default"], {
408 key: "edit",
409 title: tooltip === false ? '' : title
410 }, /*#__PURE__*/React.createElement(_transButton["default"], {
411 ref: this.setEditRef,
412 className: "".concat(this.getPrefixCls(), "-edit"),
413 onClick: this.onEditClick,
414 "aria-label": ariaLabel
415 }, icon || /*#__PURE__*/React.createElement(_EditOutlined["default"], {
416 role: "button"
417 })));
418 }
419 }, {
420 key: "renderCopy",
421 value: function renderCopy() {
422 var copied = this.state.copied;
423 var copyable = this.props.copyable;
424 if (!copyable) return;
425 var prefixCls = this.getPrefixCls();
426 var tooltips = copyable.tooltips,
427 icon = copyable.icon;
428 var tooltipNodes = Array.isArray(tooltips) ? tooltips : [tooltips];
429 var iconNodes = Array.isArray(icon) ? icon : [icon];
430 var title = copied ? getNode(tooltipNodes[1], this.copiedStr) : getNode(tooltipNodes[0], this.copyStr);
431 var systemStr = copied ? this.copiedStr : this.copyStr;
432 var ariaLabel = typeof title === 'string' ? title : systemStr;
433 return /*#__PURE__*/React.createElement(_tooltip["default"], {
434 key: "copy",
435 title: title
436 }, /*#__PURE__*/React.createElement(_transButton["default"], {
437 className: (0, _classnames["default"])("".concat(prefixCls, "-copy"), copied && "".concat(prefixCls, "-copy-success")),
438 onClick: this.onCopyClick,
439 "aria-label": ariaLabel
440 }, copied ? getNode(iconNodes[1], /*#__PURE__*/React.createElement(_CheckOutlined["default"], null), true) : getNode(iconNodes[0], /*#__PURE__*/React.createElement(_CopyOutlined["default"], null), true)));
441 }
442 }, {
443 key: "renderEditInput",
444 value: function renderEditInput() {
445 var _this$props3 = this.props,
446 children = _this$props3.children,
447 className = _this$props3.className,
448 style = _this$props3.style;
449 var direction = this.context.direction;
450
451 var _this$getEditable3 = this.getEditable(),
452 maxLength = _this$getEditable3.maxLength,
453 autoSize = _this$getEditable3.autoSize,
454 onEnd = _this$getEditable3.onEnd;
455
456 return /*#__PURE__*/React.createElement(_Editable["default"], {
457 value: typeof children === 'string' ? children : '',
458 onSave: this.onEditChange,
459 onCancel: this.onEditCancel,
460 onEnd: onEnd,
461 prefixCls: this.getPrefixCls(),
462 className: className,
463 style: style,
464 direction: direction,
465 maxLength: maxLength,
466 autoSize: autoSize
467 });
468 }
469 }, {
470 key: "renderOperations",
471 value: function renderOperations(forceRenderExpanded) {
472 return [this.renderExpand(forceRenderExpanded), this.renderEdit(), this.renderCopy()].filter(function (node) {
473 return node;
474 });
475 }
476 }, {
477 key: "renderContent",
478 value: function renderContent() {
479 var _this2 = this;
480
481 var _this$state3 = this.state,
482 ellipsisContent = _this$state3.ellipsisContent,
483 isEllipsis = _this$state3.isEllipsis,
484 expanded = _this$state3.expanded;
485
486 var _a = this.props,
487 component = _a.component,
488 children = _a.children,
489 className = _a.className,
490 type = _a.type,
491 disabled = _a.disabled,
492 style = _a.style,
493 restProps = __rest(_a, ["component", "children", "className", "type", "disabled", "style"]);
494
495 var direction = this.context.direction;
496
497 var _this$getEllipsis5 = this.getEllipsis(),
498 rows = _this$getEllipsis5.rows,
499 suffix = _this$getEllipsis5.suffix,
500 tooltip = _this$getEllipsis5.tooltip;
501
502 var prefixCls = this.getPrefixCls();
503 var textProps = (0, _omit["default"])(restProps, ['prefixCls', 'editable', 'copyable', 'ellipsis', 'mark', 'code', 'delete', 'underline', 'strong', 'keyboard'].concat((0, _toConsumableArray2["default"])(_configProvider.configConsumerProps)));
504 var cssEllipsis = this.canUseCSSEllipsis();
505 var cssTextOverflow = rows === 1 && cssEllipsis;
506 var cssLineClamp = rows && rows > 1 && cssEllipsis;
507 var textNode = children; // Only use js ellipsis when css ellipsis not support
508
509 if (rows && isEllipsis && !expanded && !cssEllipsis) {
510 var title = restProps.title;
511 var restContent = title || '';
512
513 if (!title && (typeof children === 'string' || typeof children === 'number')) {
514 restContent = String(children);
515 } // show rest content as title on symbol
516
517
518 restContent = restContent.slice(String(ellipsisContent || '').length); // We move full content to outer element to avoid repeat read the content by accessibility
519
520 textNode = /*#__PURE__*/React.createElement(React.Fragment, null, ellipsisContent, /*#__PURE__*/React.createElement("span", {
521 title: restContent,
522 "aria-hidden": "true"
523 }, ELLIPSIS_STR), suffix); // If provided tooltip, we need wrap with span to let Tooltip inject events
524
525 if (tooltip) {
526 textNode = /*#__PURE__*/React.createElement(_tooltip["default"], {
527 title: tooltip === true ? children : tooltip
528 }, /*#__PURE__*/React.createElement("span", null, textNode));
529 }
530 } else {
531 textNode = /*#__PURE__*/React.createElement(React.Fragment, null, children, suffix);
532 }
533
534 textNode = wrapperDecorations(this.props, textNode);
535 return /*#__PURE__*/React.createElement(_LocaleReceiver["default"], {
536 componentName: "Text"
537 }, function (_ref4) {
538 var _classNames;
539
540 var edit = _ref4.edit,
541 copyStr = _ref4.copy,
542 copied = _ref4.copied,
543 expand = _ref4.expand;
544 _this2.editStr = edit;
545 _this2.copyStr = copyStr;
546 _this2.copiedStr = copied;
547 _this2.expandStr = expand;
548 return /*#__PURE__*/React.createElement(_rcResizeObserver["default"], {
549 onResize: _this2.resizeOnNextFrame,
550 disabled: cssEllipsis
551 }, /*#__PURE__*/React.createElement(_Typography["default"], (0, _extends2["default"])({
552 className: (0, _classnames["default"])((_classNames = {}, (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-").concat(type), type), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-disabled"), disabled), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-ellipsis"), rows), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-single-line"), rows === 1), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-ellipsis-single-line"), cssTextOverflow), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-ellipsis-multiple-line"), cssLineClamp), _classNames), className),
553 style: (0, _extends2["default"])((0, _extends2["default"])({}, style), {
554 WebkitLineClamp: cssLineClamp ? rows : undefined
555 }),
556 component: component,
557 ref: _this2.contentRef,
558 direction: direction
559 }, textProps), textNode, _this2.renderOperations()));
560 });
561 }
562 }, {
563 key: "render",
564 value: function render() {
565 var _this$getEditable4 = this.getEditable(),
566 editing = _this$getEditable4.editing;
567
568 if (editing) {
569 return this.renderEditInput();
570 }
571
572 return this.renderContent();
573 }
574 }], [{
575 key: "getDerivedStateFromProps",
576 value: function getDerivedStateFromProps(nextProps) {
577 var children = nextProps.children,
578 editable = nextProps.editable;
579 (0, _devWarning["default"])(!editable || typeof children === 'string', 'Typography', 'When `editable` is enabled, the `children` should use string.');
580 return {};
581 }
582 }]);
583 return Base;
584}(React.Component);
585
586Base.contextType = _configProvider.ConfigContext;
587Base.defaultProps = {
588 children: ''
589};
590var _default = Base;
591exports["default"] = _default;
\No newline at end of file