Anchor.js
10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));
var React = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _addEventListener = _interopRequireDefault(require("rc-util/lib/Dom/addEventListener"));
var _affix = _interopRequireDefault(require("../affix"));
var _configProvider = require("../config-provider");
var _scrollTo = _interopRequireDefault(require("../_util/scrollTo"));
var _getScroll = _interopRequireDefault(require("../_util/getScroll"));
var _context = _interopRequireDefault(require("./context"));
function getDefaultContainer() {
return window;
}
function getOffsetTop(element, container) {
if (!element.getClientRects().length) {
return 0;
}
var rect = element.getBoundingClientRect();
if (rect.width || rect.height) {
if (container === window) {
container = element.ownerDocument.documentElement;
return rect.top - container.clientTop;
}
return rect.top - container.getBoundingClientRect().top;
}
return rect.top;
}
var sharpMatcherRegx = /#(\S+)$/;
var Anchor = /*#__PURE__*/function (_React$Component) {
(0, _inherits2["default"])(Anchor, _React$Component);
var _super = (0, _createSuper2["default"])(Anchor);
function Anchor() {
var _this;
(0, _classCallCheck2["default"])(this, Anchor);
_this = _super.apply(this, arguments);
_this.state = {
activeLink: null
};
_this.wrapperRef = /*#__PURE__*/React.createRef();
_this.links = []; // Context
_this.registerLink = function (link) {
if (!_this.links.includes(link)) {
_this.links.push(link);
}
};
_this.unregisterLink = function (link) {
var index = _this.links.indexOf(link);
if (index !== -1) {
_this.links.splice(index, 1);
}
};
_this.getContainer = function () {
var getTargetContainer = _this.context.getTargetContainer;
var getContainer = _this.props.getContainer;
var getFunc = getContainer || getTargetContainer || getDefaultContainer;
return getFunc();
};
_this.handleScrollTo = function (link) {
var _this$props = _this.props,
offsetTop = _this$props.offsetTop,
targetOffset = _this$props.targetOffset;
_this.setCurrentActiveLink(link);
var container = _this.getContainer();
var scrollTop = (0, _getScroll["default"])(container, true);
var sharpLinkMatch = sharpMatcherRegx.exec(link);
if (!sharpLinkMatch) {
return;
}
var targetElement = document.getElementById(sharpLinkMatch[1]);
if (!targetElement) {
return;
}
var eleOffsetTop = getOffsetTop(targetElement, container);
var y = scrollTop + eleOffsetTop;
y -= targetOffset !== undefined ? targetOffset : offsetTop || 0;
_this.animating = true;
(0, _scrollTo["default"])(y, {
callback: function callback() {
_this.animating = false;
},
getContainer: _this.getContainer
});
};
_this.saveInkNode = function (node) {
_this.inkNode = node;
};
_this.setCurrentActiveLink = function (link) {
var activeLink = _this.state.activeLink;
var onChange = _this.props.onChange;
if (activeLink !== link) {
_this.setState({
activeLink: link
});
if (onChange) {
onChange(link);
}
}
};
_this.handleScroll = function () {
if (_this.animating) {
return;
}
var _this$props2 = _this.props,
offsetTop = _this$props2.offsetTop,
bounds = _this$props2.bounds,
targetOffset = _this$props2.targetOffset;
var currentActiveLink = _this.getCurrentAnchor(targetOffset !== undefined ? targetOffset : offsetTop || 0, bounds);
_this.setCurrentActiveLink(currentActiveLink);
};
_this.updateInk = function () {
var _assertThisInitialize = (0, _assertThisInitialized2["default"])(_this),
prefixCls = _assertThisInitialize.prefixCls,
wrapperRef = _assertThisInitialize.wrapperRef;
var anchorNode = wrapperRef.current;
var linkNode = anchorNode === null || anchorNode === void 0 ? void 0 : anchorNode.getElementsByClassName("".concat(prefixCls, "-link-title-active"))[0];
if (linkNode) {
_this.inkNode.style.top = "".concat(linkNode.offsetTop + linkNode.clientHeight / 2 - 4.5, "px");
}
};
_this.render = function () {
var _this$context = _this.context,
getPrefixCls = _this$context.getPrefixCls,
direction = _this$context.direction;
var _this$props3 = _this.props,
customizePrefixCls = _this$props3.prefixCls,
_this$props3$classNam = _this$props3.className,
className = _this$props3$classNam === void 0 ? '' : _this$props3$classNam,
style = _this$props3.style,
offsetTop = _this$props3.offsetTop,
affix = _this$props3.affix,
showInkInFixed = _this$props3.showInkInFixed,
children = _this$props3.children;
var activeLink = _this.state.activeLink;
var prefixCls = getPrefixCls('anchor', customizePrefixCls); // To support old version react.
// Have to add prefixCls on the instance.
// https://github.com/facebook/react/issues/12397
_this.prefixCls = prefixCls;
var inkClass = (0, _classnames["default"])("".concat(prefixCls, "-ink-ball"), {
visible: activeLink
});
var wrapperClass = (0, _classnames["default"])("".concat(prefixCls, "-wrapper"), (0, _defineProperty2["default"])({}, "".concat(prefixCls, "-rtl"), direction === 'rtl'), className);
var anchorClass = (0, _classnames["default"])(prefixCls, {
fixed: !affix && !showInkInFixed
});
var wrapperStyle = (0, _extends2["default"])({
maxHeight: offsetTop ? "calc(100vh - ".concat(offsetTop, "px)") : '100vh'
}, style);
var anchorContent = /*#__PURE__*/React.createElement("div", {
ref: _this.wrapperRef,
className: wrapperClass,
style: wrapperStyle
}, /*#__PURE__*/React.createElement("div", {
className: anchorClass
}, /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-ink")
}, /*#__PURE__*/React.createElement("span", {
className: inkClass,
ref: _this.saveInkNode
})), children));
return /*#__PURE__*/React.createElement(_context["default"].Provider, {
value: {
registerLink: _this.registerLink,
unregisterLink: _this.unregisterLink,
activeLink: _this.state.activeLink,
scrollTo: _this.handleScrollTo,
onClick: _this.props.onClick
}
}, !affix ? anchorContent : /*#__PURE__*/React.createElement(_affix["default"], {
offsetTop: offsetTop,
target: _this.getContainer
}, anchorContent));
};
return _this;
}
(0, _createClass2["default"])(Anchor, [{
key: "componentDidMount",
value: function componentDidMount() {
this.scrollContainer = this.getContainer();
this.scrollEvent = (0, _addEventListener["default"])(this.scrollContainer, 'scroll', this.handleScroll);
this.handleScroll();
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate() {
if (this.scrollEvent) {
var currentContainer = this.getContainer();
if (this.scrollContainer !== currentContainer) {
this.scrollContainer = currentContainer;
this.scrollEvent.remove();
this.scrollEvent = (0, _addEventListener["default"])(this.scrollContainer, 'scroll', this.handleScroll);
this.handleScroll();
}
}
this.updateInk();
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
if (this.scrollEvent) {
this.scrollEvent.remove();
}
}
}, {
key: "getCurrentAnchor",
value: function getCurrentAnchor() {
var offsetTop = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
var bounds = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 5;
var getCurrentAnchor = this.props.getCurrentAnchor;
if (typeof getCurrentAnchor === 'function') {
return getCurrentAnchor();
}
var linkSections = [];
var container = this.getContainer();
this.links.forEach(function (link) {
var sharpLinkMatch = sharpMatcherRegx.exec(link.toString());
if (!sharpLinkMatch) {
return;
}
var target = document.getElementById(sharpLinkMatch[1]);
if (target) {
var top = getOffsetTop(target, container);
if (top < offsetTop + bounds) {
linkSections.push({
link: link,
top: top
});
}
}
});
if (linkSections.length) {
var maxSection = linkSections.reduce(function (prev, curr) {
return curr.top > prev.top ? curr : prev;
});
return maxSection.link;
}
return '';
}
}]);
return Anchor;
}(React.Component);
exports["default"] = Anchor;
Anchor.defaultProps = {
affix: true,
showInkInFixed: false
};
Anchor.contextType = _configProvider.ConfigContext;