@textbus/platform-browser 5.4.3 → 5.4.5

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/dist/index.js CHANGED
@@ -16,6 +16,14 @@ const core = require('@viewfly/core');
16
16
  function _array_without_holes$2(arr) {
17
17
  if (Array.isArray(arr)) return _array_like_to_array$2(arr);
18
18
  }
19
+ function _instanceof$4(left, right) {
20
+ "@swc/helpers - instanceof";
21
+ if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
22
+ return !!right[Symbol.hasInstance](left);
23
+ } else {
24
+ return left instanceof right;
25
+ }
26
+ }
19
27
  function _iterable_to_array$2(iter) {
20
28
  if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
21
29
  }
@@ -33,6 +41,247 @@ function _unsupported_iterable_to_array$2(o, minLen) {
33
41
  if (n === "Map" || n === "Set") return Array.from(n);
34
42
  if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array$2(o, minLen);
35
43
  }
44
+ var OVERFLOW_CLIP_VALUES = new Set([
45
+ 'auto',
46
+ 'hidden',
47
+ 'scroll',
48
+ 'clip'
49
+ ]);
50
+ function rectRight(r) {
51
+ return r.left + r.width;
52
+ }
53
+ function rectBottom(r) {
54
+ return r.top + r.height;
55
+ }
56
+ function isOverflowClipAxis(value) {
57
+ return OVERFLOW_CLIP_VALUES.has(value);
58
+ }
59
+ function readOverflowClipAxes(style) {
60
+ var overflow = style.overflow;
61
+ var overflowX = style.overflowX;
62
+ var overflowY = style.overflowY;
63
+ if (!isOverflowClipAxis(overflow) && !isOverflowClipAxis(overflowX) && !isOverflowClipAxis(overflowY)) {
64
+ return null;
65
+ }
66
+ return {
67
+ clipX: isOverflowClipAxis(overflowX) || isOverflowClipAxis(overflow),
68
+ clipY: isOverflowClipAxis(overflowY) || isOverflowClipAxis(overflow)
69
+ };
70
+ }
71
+ /**
72
+ * 一次遍历祖先链:缓存 overflow 轴信息,并收集滚动监听目标。
73
+ */ function getOverflowClipContext(anchor, extraRoot) {
74
+ var _ref, _ref1;
75
+ var _clipAncestors_;
76
+ var clipAncestors = [];
77
+ var scrollContainers = new Set();
78
+ var node = anchor.nodeType === Node.ELEMENT_NODE ? anchor : anchor.parentNode;
79
+ while(node){
80
+ var _node_ownerDocument;
81
+ if (_instanceof$4(node, Element)) {
82
+ var flags = readOverflowClipAxes(getComputedStyle(node));
83
+ if (flags) {
84
+ clipAncestors.push({
85
+ element: node,
86
+ clipX: flags.clipX,
87
+ clipY: flags.clipY
88
+ });
89
+ scrollContainers.add(node);
90
+ }
91
+ }
92
+ if (node === ((_node_ownerDocument = node.ownerDocument) === null || _node_ownerDocument === void 0 ? void 0 : _node_ownerDocument.documentElement)) {
93
+ break;
94
+ }
95
+ node = node.parentNode;
96
+ }
97
+ var doc = anchor.ownerDocument;
98
+ var documentElement = (_ref = doc === null || doc === void 0 ? void 0 : doc.documentElement) !== null && _ref !== void 0 ? _ref : document.documentElement;
99
+ scrollContainers.add(documentElement);
100
+ if (extraRoot) {
101
+ scrollContainers.add(extraRoot);
102
+ }
103
+ return {
104
+ clipAncestors: clipAncestors,
105
+ scrollContainers: _to_consumable_array$2(scrollContainers),
106
+ firstScrollContainer: (_ref1 = (_clipAncestors_ = clipAncestors[0]) === null || _clipAncestors_ === void 0 ? void 0 : _clipAncestors_.element) !== null && _ref1 !== void 0 ? _ref1 : documentElement
107
+ };
108
+ }
109
+ function intersectRect(a, b) {
110
+ var left = Math.max(a.left, b.left);
111
+ var top = Math.max(a.top, b.top);
112
+ var right = Math.min(rectRight(a), rectRight(b));
113
+ var bottom = Math.min(rectBottom(a), rectBottom(b));
114
+ if (left >= right || top >= bottom) {
115
+ return null;
116
+ }
117
+ return {
118
+ left: left,
119
+ top: top,
120
+ width: right - left,
121
+ height: bottom - top
122
+ };
123
+ }
124
+ function rectContainsPoint(rect, x, y) {
125
+ return x >= rect.left && x <= rectRight(rect) && y >= rect.top && y <= rectBottom(rect);
126
+ }
127
+ function intersectRectWithClipAncestor(ancestor, visible) {
128
+ var el = ancestor.element;
129
+ var border = el.getBoundingClientRect();
130
+ var left = visible.left;
131
+ var top = visible.top;
132
+ var right = rectRight(visible);
133
+ var bottom = rectBottom(visible);
134
+ if (ancestor.clipX) {
135
+ var clipLeft = border.left + el.clientLeft;
136
+ var clipRight = clipLeft + el.clientWidth;
137
+ left = Math.max(left, clipLeft);
138
+ right = Math.min(right, clipRight);
139
+ }
140
+ if (ancestor.clipY) {
141
+ var clipTop = border.top + el.clientTop;
142
+ var clipBottom = clipTop + el.clientHeight;
143
+ top = Math.max(top, clipTop);
144
+ bottom = Math.min(bottom, clipBottom);
145
+ }
146
+ if (left >= right || top >= bottom) {
147
+ return null;
148
+ }
149
+ return {
150
+ left: left,
151
+ top: top,
152
+ width: right - left,
153
+ height: bottom - top
154
+ };
155
+ }
156
+ function getVisibleRectInOverflowClipsFromAncestors(rect, clipAncestors) {
157
+ var visible = rect;
158
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
159
+ try {
160
+ for(var _iterator = clipAncestors[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
161
+ var ancestor = _step.value;
162
+ var next = intersectRectWithClipAncestor(ancestor, visible);
163
+ if (!next) {
164
+ return null;
165
+ }
166
+ visible = next;
167
+ }
168
+ } catch (err) {
169
+ _didIteratorError = true;
170
+ _iteratorError = err;
171
+ } finally{
172
+ try {
173
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
174
+ _iterator.return();
175
+ }
176
+ } finally{
177
+ if (_didIteratorError) {
178
+ throw _iteratorError;
179
+ }
180
+ }
181
+ }
182
+ return visible;
183
+ }
184
+ /** 锚点是否被 CSS 隐藏(opacity / visibility 等);不做点命中,避免 mask 层挡住误判 */ function isCaretAnchorCssVisible(anchor) {
185
+ var anchorEl = anchor.nodeType === Node.ELEMENT_NODE ? anchor : anchor.parentElement;
186
+ if (!(anchorEl === null || anchorEl === void 0 ? void 0 : anchorEl.isConnected)) {
187
+ return false;
188
+ }
189
+ if (typeof anchorEl.checkVisibility === 'function') {
190
+ return anchorEl.checkVisibility({
191
+ checkOpacity: true,
192
+ checkVisibilityCSS: true
193
+ });
194
+ }
195
+ return true;
196
+ }
197
+ var CLIP_INSET_EPSILON = 0.5;
198
+ function clipPathInsetFromVisibleRegion(elRect, visible) {
199
+ var top = Math.max(0, visible.top - elRect.top);
200
+ var left = Math.max(0, visible.left - elRect.left);
201
+ var bottom = Math.max(0, rectBottom(elRect) - rectBottom(visible));
202
+ var right = Math.max(0, rectRight(elRect) - rectRight(visible));
203
+ if (top < CLIP_INSET_EPSILON && left < CLIP_INSET_EPSILON && bottom < CLIP_INSET_EPSILON && right < CLIP_INSET_EPSILON) {
204
+ return 'none';
205
+ }
206
+ return "inset(".concat(top, "px ").concat(right, "px ").concat(bottom, "px ").concat(left, "px)");
207
+ }
208
+ /** 根据行高与内容矩形计算光标盒高度与顶部(视口坐标) */ function measureCaretBoxLayout(contentRect, style) {
209
+ var fontSize = style.fontSize, lineHeight = style.lineHeight;
210
+ var height;
211
+ if (isNaN(+lineHeight)) {
212
+ var f = parseFloat(lineHeight);
213
+ height = isNaN(f) ? parseFloat(fontSize) : f;
214
+ } else {
215
+ height = parseFloat(fontSize) * parseFloat(lineHeight);
216
+ }
217
+ var boxHeight = Math.max(Math.floor(Math.max(height, contentRect.height)), 12);
218
+ var rectTop = contentRect.top;
219
+ if (contentRect.height < height) {
220
+ rectTop -= (height - contentRect.height) / 2;
221
+ }
222
+ return {
223
+ boxHeight: boxHeight,
224
+ rectTop: Math.floor(rectTop)
225
+ };
226
+ }
227
+ /** 测量行内旋转角度(writing-mode 继承) */ function measureInlineCaretRotate(node, initialRotate, writingMode) {
228
+ var rotate = initialRotate;
229
+ if (rotate !== 0) {
230
+ var hackEle = document.createElement('span');
231
+ hackEle.style.cssText = 'display: inline-block; width: 10px; height: 10px; position: relative; contain: layout style size; writing-mode: inherit';
232
+ var pointEle = document.createElement('span');
233
+ pointEle.style.cssText = 'position: absolute; left: 0; top: 0; width:0;height:0';
234
+ hackEle.append(pointEle);
235
+ node.append(hackEle);
236
+ var p1 = pointEle.getBoundingClientRect();
237
+ pointEle.style.right = '0';
238
+ pointEle.style.left = '';
239
+ var p2 = pointEle.getBoundingClientRect();
240
+ rotate = Math.atan2(p1.y - p2.y, p1.x - p2.x) * 180 / Math.PI;
241
+ hackEle.remove();
242
+ }
243
+ if (writingMode === 'vertical-lr' || writingMode === 'vertical-rl') {
244
+ rotate += 90;
245
+ }
246
+ return rotate;
247
+ }
248
+ /**
249
+ * 在 element 样式已写入后调用:计算裁剪、是否移出视口及对外事件数据。
250
+ */ function computeCaretPresentation(options) {
251
+ var anchor = options.anchor, maskRect = options.maskRect, clipAncestors = options.clipAncestors, maskLeft = options.maskLeft, maskTop = options.maskTop, boxHeight = options.boxHeight, rectTop = options.rectTop, contentRect = options.contentRect, fontSize = options.fontSize, color = options.color, measureElementRect = options.measureElementRect;
252
+ var caretCenterX = contentRect.left + contentRect.width / 2;
253
+ var caretCenterY = rectTop + boxHeight / 2;
254
+ var layoutElRect = {
255
+ left: maskRect.left + maskLeft,
256
+ top: maskRect.top + maskTop,
257
+ width: 2,
258
+ height: boxHeight
259
+ };
260
+ var visibleRect = getVisibleRectInOverflowClipsFromAncestors(layoutElRect, clipAncestors);
261
+ visibleRect = visibleRect ? intersectRect(visibleRect, maskRect) : null;
262
+ var inClipBounds = !!visibleRect && rectContainsPoint(visibleRect, caretCenterX, caretCenterY);
263
+ var outOfView = !inClipBounds || !isCaretAnchorCssVisible(anchor);
264
+ var clipPath = 'none';
265
+ if (!outOfView && visibleRect) {
266
+ clipPath = clipPathInsetFromVisibleRegion(measureElementRect(), visibleRect);
267
+ }
268
+ return {
269
+ outOfView: outOfView,
270
+ clipPath: clipPath,
271
+ layoutElRect: layoutElRect,
272
+ position: {
273
+ left: maskLeft,
274
+ top: rectTop,
275
+ height: boxHeight
276
+ },
277
+ caretColor: color === 'rgba(0, 0, 0, 0)' ? '#000' : color,
278
+ style: {
279
+ height: boxHeight + 'px',
280
+ lineHeight: boxHeight + 'px',
281
+ fontSize: fontSize
282
+ }
283
+ };
284
+ }
36
285
  function createElement(tagName) {
37
286
  var options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
38
287
  var el = document.createElement(tagName);
@@ -213,7 +462,7 @@ function _define_property$6(obj, key, value) {
213
462
  }
214
463
  return obj;
215
464
  }
216
- function _instanceof$4(left, right) {
465
+ function _instanceof$3(left, right) {
217
466
  "@swc/helpers - instanceof";
218
467
  if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
219
468
  return !!right[Symbol.hasInstance](left);
@@ -320,7 +569,7 @@ exports.Parser = /*#__PURE__*/ function() {
320
569
  if (!result) {
321
570
  return;
322
571
  }
323
- if (_instanceof$4(result, core$1.Slot)) {
572
+ if (_instanceof$3(result, core$1.Slot)) {
324
573
  result.toDelta().forEach(function(i) {
325
574
  return slot.insert(i.insert, i.formats);
326
575
  });
@@ -442,6 +691,15 @@ function _class_call_check$6(instance, Constructor) {
442
691
  throw new TypeError("Cannot call a class as a function");
443
692
  }
444
693
  }
694
+ function caretPositionEqual(a, b) {
695
+ if (a === b) {
696
+ return true;
697
+ }
698
+ if (a === null || b === null) {
699
+ return false;
700
+ }
701
+ return a.left === b.left && a.top === b.top && a.height === b.height;
702
+ }
445
703
  var Input = function Input() {
446
704
  _class_call_check$6(this, Input);
447
705
  };
@@ -526,7 +784,7 @@ var DomAdapter = /*#__PURE__*/ function(Adapter) {
526
784
  _this = _call_super$2(this, DomAdapter, arguments), _define_property$5(_this, "onViewUpdated", new stream.Subject()), _define_property$5(_this, "host", createElement('div', {
527
785
  styles: {
528
786
  cursor: 'text',
529
- wordBreak: 'break-all',
787
+ overflowWrap: 'break-word',
530
788
  boxSizing: 'border-box',
531
789
  flex: 1,
532
790
  outline: 'none'
@@ -574,7 +832,7 @@ function _define_property$4(obj, key, value) {
574
832
  }
575
833
  return obj;
576
834
  }
577
- function _instanceof$3(left, right) {
835
+ function _instanceof$2(left, right) {
578
836
  "@swc/helpers - instanceof";
579
837
  if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
580
838
  return !!right[Symbol.hasInstance](left);
@@ -1081,7 +1339,7 @@ exports.SelectionBridge = /*#__PURE__*/ function() {
1081
1339
  try {
1082
1340
  for(var _iterator = nodes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
1083
1341
  var node = _step.value;
1084
- if (_instanceof$3(node, Text)) {
1342
+ if (_instanceof$2(node, Text)) {
1085
1343
  continue;
1086
1344
  }
1087
1345
  if (node.nodeName === 'BR') {
@@ -1125,7 +1383,7 @@ exports.SelectionBridge = /*#__PURE__*/ function() {
1125
1383
  try {
1126
1384
  for(var _iterator1 = nodes[Symbol.iterator](), _step1; !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = true){
1127
1385
  var node1 = _step1.value;
1128
- if (_instanceof$3(node1, Element)) {
1386
+ if (_instanceof$2(node1, Element)) {
1129
1387
  if (node1.tagName === 'BR') {
1130
1388
  var position1 = this.domAdapter.getLocationByNativeNode(node1);
1131
1389
  if (position1) {
@@ -1353,14 +1611,6 @@ function _inherits$1(subClass, superClass) {
1353
1611
  });
1354
1612
  if (superClass) _set_prototype_of$1(subClass, superClass);
1355
1613
  }
1356
- function _instanceof$2(left, right) {
1357
- "@swc/helpers - instanceof";
1358
- if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
1359
- return !!right[Symbol.hasInstance](left);
1360
- } else {
1361
- return left instanceof right;
1362
- }
1363
- }
1364
1614
  function _possible_constructor_return$1(self, call) {
1365
1615
  if (call && (_type_of$1(call) === "object" || typeof call === "function")) {
1366
1616
  return call;
@@ -1412,7 +1662,13 @@ var ExperimentalCaret = /*#__PURE__*/ function() {
1412
1662
  _define_property$3(this, "caret", void 0);
1413
1663
  _define_property$3(this, "_display", void 0);
1414
1664
  _define_property$3(this, "flashing", void 0);
1665
+ /** 被 overflow 裁出可视区(不用 display:none,避免触发 Input.hide) */ _define_property$3(this, "caretClippedOut", void 0);
1415
1666
  _define_property$3(this, "subscription", void 0);
1667
+ _define_property$3(this, "scrollSubscription", void 0);
1668
+ _define_property$3(this, "scrollRaf", void 0);
1669
+ _define_property$3(this, "clipContext", void 0);
1670
+ _define_property$3(this, "clipContextAnchor", void 0);
1671
+ _define_property$3(this, "scrollListenerAnchor", void 0);
1416
1672
  _define_property$3(this, "positionChangeEvent", void 0);
1417
1673
  _define_property$3(this, "styleChangeEvent", void 0);
1418
1674
  _define_property$3(this, "oldRange", void 0);
@@ -1429,11 +1685,17 @@ var ExperimentalCaret = /*#__PURE__*/ function() {
1429
1685
  this.timer = null;
1430
1686
  this._display = true;
1431
1687
  this.flashing = true;
1688
+ this.caretClippedOut = false;
1432
1689
  this.subscription = new stream.Subscription();
1690
+ this.scrollSubscription = new stream.Subscription();
1691
+ this.scrollRaf = 0;
1692
+ this.clipContext = null;
1693
+ this.clipContextAnchor = null;
1694
+ this.scrollListenerAnchor = null;
1433
1695
  this.positionChangeEvent = new stream.Subject();
1434
1696
  this.styleChangeEvent = new stream.Subject();
1435
1697
  this.oldRange = null;
1436
- this.onPositionChange = this.positionChangeEvent.pipe(stream.distinctUntilChanged());
1698
+ this.onPositionChange = this.positionChangeEvent.pipe(stream.distinctUntilChanged(caretPositionEqual));
1437
1699
  this.onStyleChange = this.styleChangeEvent.asObservable();
1438
1700
  this.elementRef = createElement('div', {
1439
1701
  styles: {
@@ -1474,14 +1736,17 @@ var ExperimentalCaret = /*#__PURE__*/ function() {
1474
1736
  },
1475
1737
  set: function set(v) {
1476
1738
  this._display = v;
1477
- this.caret.style.visibility = v ? 'visible' : 'hidden';
1739
+ if (!this.caretClippedOut) {
1740
+ this.caret.style.visibility = v ? 'visible' : 'hidden';
1741
+ }
1478
1742
  }
1479
1743
  },
1480
1744
  {
1481
1745
  key: "refresh",
1482
1746
  value: function refresh() {
1483
- if (this.oldRange) {
1484
- this.show(this.oldRange, false);
1747
+ var _this_oldRange;
1748
+ if ((_this_oldRange = this.oldRange) === null || _this_oldRange === void 0 ? void 0 : _this_oldRange.collapsed) {
1749
+ this.updateCursorPosition(this.oldRange);
1485
1750
  }
1486
1751
  }
1487
1752
  },
@@ -1493,8 +1758,15 @@ var ExperimentalCaret = /*#__PURE__*/ function() {
1493
1758
  if (restart || this.scheduler.lastChangesHasLocalUpdate) {
1494
1759
  clearTimeout(this.timer);
1495
1760
  }
1761
+ if (range.collapsed && (restart || this.scheduler.lastChangesHasLocalUpdate)) {
1762
+ this._display = true;
1763
+ }
1496
1764
  this.updateCursorPosition(range);
1497
1765
  if (range.collapsed) {
1766
+ var anchor = range.startContainer.nodeType === Node.ELEMENT_NODE ? range.startContainer : range.startContainer.parentNode;
1767
+ if (anchor) {
1768
+ this.bindScrollListeners(anchor);
1769
+ }
1498
1770
  if (restart || this.scheduler.lastChangesHasLocalUpdate) {
1499
1771
  this.display = true;
1500
1772
  var toggleShowHide = function toggleShowHide1() {
@@ -1505,6 +1777,7 @@ var ExperimentalCaret = /*#__PURE__*/ function() {
1505
1777
  this.timer = setTimeout(toggleShowHide, 400);
1506
1778
  }
1507
1779
  } else {
1780
+ this.unbindScrollListeners();
1508
1781
  this.display = false;
1509
1782
  clearTimeout(this.timer);
1510
1783
  }
@@ -1513,8 +1786,14 @@ var ExperimentalCaret = /*#__PURE__*/ function() {
1513
1786
  {
1514
1787
  key: "hide",
1515
1788
  value: function hide() {
1789
+ this.unbindScrollListeners();
1790
+ this.clearClipContext();
1516
1791
  this.display = false;
1517
1792
  clearTimeout(this.timer);
1793
+ this.caretClippedOut = false;
1794
+ this.elementRef.style.clipPath = 'none';
1795
+ this.elementRef.style.visibility = '';
1796
+ this.caret.style.visibility = 'hidden';
1518
1797
  this.positionChangeEvent.next(null);
1519
1798
  }
1520
1799
  },
@@ -1522,127 +1801,210 @@ var ExperimentalCaret = /*#__PURE__*/ function() {
1522
1801
  key: "destroy",
1523
1802
  value: function destroy() {
1524
1803
  clearTimeout(this.timer);
1525
- // this.caret.
1804
+ this.unbindScrollListeners();
1526
1805
  this.subscription.unsubscribe();
1527
1806
  }
1528
1807
  },
1808
+ {
1809
+ key: "bindScrollListeners",
1810
+ value: function bindScrollListeners(anchor) {
1811
+ var _this = this;
1812
+ var _anchor_ownerDocument;
1813
+ if (this.scrollListenerAnchor === anchor) {
1814
+ return;
1815
+ }
1816
+ this.unbindScrollListeners();
1817
+ this.scrollListenerAnchor = anchor;
1818
+ var onScroll = function onScroll() {
1819
+ if (!_this.oldRange || _this.scrollRaf) {
1820
+ return;
1821
+ }
1822
+ _this.scrollRaf = requestAnimationFrame(function() {
1823
+ _this.scrollRaf = 0;
1824
+ if (_this.oldRange) {
1825
+ _this.updateCursorPosition(_this.oldRange);
1826
+ }
1827
+ });
1828
+ };
1829
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
1830
+ try {
1831
+ for(var _iterator = this.ensureClipContext(anchor).scrollContainers[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
1832
+ var el = _step.value;
1833
+ this.scrollSubscription.add(stream.fromEvent(el, 'scroll').subscribe(onScroll));
1834
+ }
1835
+ } catch (err) {
1836
+ _didIteratorError = true;
1837
+ _iteratorError = err;
1838
+ } finally{
1839
+ try {
1840
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
1841
+ _iterator.return();
1842
+ }
1843
+ } finally{
1844
+ if (_didIteratorError) {
1845
+ throw _iteratorError;
1846
+ }
1847
+ }
1848
+ }
1849
+ var win = (_anchor_ownerDocument = anchor.ownerDocument) === null || _anchor_ownerDocument === void 0 ? void 0 : _anchor_ownerDocument.defaultView;
1850
+ if (win) {
1851
+ this.scrollSubscription.add(stream.fromEvent(win, 'scroll').subscribe(onScroll));
1852
+ }
1853
+ }
1854
+ },
1855
+ {
1856
+ key: "unbindScrollListeners",
1857
+ value: function unbindScrollListeners() {
1858
+ if (this.scrollRaf) {
1859
+ cancelAnimationFrame(this.scrollRaf);
1860
+ this.scrollRaf = 0;
1861
+ }
1862
+ this.scrollSubscription.unsubscribe();
1863
+ this.scrollSubscription = new stream.Subscription();
1864
+ this.scrollListenerAnchor = null;
1865
+ }
1866
+ },
1867
+ {
1868
+ key: "ensureClipContext",
1869
+ value: function ensureClipContext(anchor) {
1870
+ if (this.clipContextAnchor !== anchor) {
1871
+ this.clipContextAnchor = anchor;
1872
+ this.clipContext = getOverflowClipContext(anchor, this.editorMask);
1873
+ }
1874
+ return this.clipContext;
1875
+ }
1876
+ },
1877
+ {
1878
+ key: "clearClipContext",
1879
+ value: function clearClipContext() {
1880
+ this.clipContext = null;
1881
+ this.clipContextAnchor = null;
1882
+ }
1883
+ },
1529
1884
  {
1530
1885
  key: "updateCursorPosition",
1531
1886
  value: function updateCursorPosition(nativeRange) {
1532
- var startContainer = nativeRange.startContainer;
1533
- var node = startContainer.nodeType === Node.ELEMENT_NODE ? startContainer : startContainer.parentNode;
1534
- if ((node === null || node === void 0 ? void 0 : node.nodeType) !== Node.ELEMENT_NODE) {
1887
+ var _this = this;
1888
+ var node = this.resolveCaretAnchor(nativeRange);
1889
+ if (!node) {
1535
1890
  this.positionChangeEvent.next(null);
1536
1891
  return;
1537
1892
  }
1538
- var compositionNode = this.domRenderer.compositionNode;
1539
- if (compositionNode) {
1540
- nativeRange = nativeRange.cloneRange();
1541
- nativeRange.selectNodeContents(compositionNode);
1542
- nativeRange.collapse();
1543
- }
1893
+ nativeRange = this.normalizeCollapsedRange(nativeRange);
1544
1894
  this.caret.style.display = nativeRange.collapsed ? 'block' : 'none';
1545
1895
  if (!nativeRange.collapsed) {
1546
1896
  return;
1547
1897
  }
1548
- var rect = getLayoutRectByRange(nativeRange);
1549
- var _getComputedStyle = getComputedStyle(node), fontSize = _getComputedStyle.fontSize, lineHeight = _getComputedStyle.lineHeight, color = _getComputedStyle.color, writingMode = _getComputedStyle.writingMode;
1550
- var height;
1551
- if (isNaN(+lineHeight)) {
1552
- var f = parseFloat(lineHeight);
1553
- if (isNaN(f)) {
1554
- height = parseFloat(fontSize);
1555
- } else {
1556
- height = f;
1898
+ var contentRect = getLayoutRectByRange(nativeRange);
1899
+ var nodeStyle = getComputedStyle(node);
1900
+ var _measureCaretBoxLayout = measureCaretBoxLayout(contentRect, nodeStyle), boxHeight = _measureCaretBoxLayout.boxHeight, rectTop = _measureCaretBoxLayout.rectTop;
1901
+ var maskRect = this.editorMask.getBoundingClientRect();
1902
+ var clipContext = this.ensureClipContext(node);
1903
+ var maskLeft = Math.floor(contentRect.left + contentRect.width / 2 - maskRect.left);
1904
+ var maskTop = Math.floor(rectTop - maskRect.top);
1905
+ var initialRotate = Math.round(Math.atan2(contentRect.width, contentRect.height) * 180 / Math.PI);
1906
+ var rotate = measureInlineCaretRotate(node, initialRotate, nodeStyle.writingMode);
1907
+ this.applyCaretElementLayout(maskLeft, maskTop, boxHeight, nodeStyle.fontSize, rotate);
1908
+ var presentation = computeCaretPresentation({
1909
+ anchor: node,
1910
+ maskRect: maskRect,
1911
+ clipAncestors: clipContext.clipAncestors,
1912
+ maskLeft: maskLeft,
1913
+ maskTop: maskTop,
1914
+ boxHeight: boxHeight,
1915
+ rectTop: rectTop,
1916
+ contentRect: contentRect,
1917
+ fontSize: nodeStyle.fontSize,
1918
+ color: nodeStyle.color,
1919
+ measureElementRect: function measureElementRect() {
1920
+ return _this.elementRef.getBoundingClientRect();
1557
1921
  }
1558
- } else {
1559
- height = parseFloat(fontSize) * parseFloat(lineHeight);
1560
- }
1561
- var boxHeight = Math.max(Math.floor(Math.max(height, rect.height)), 12);
1562
- // const boxHeight = Math.floor(height)
1563
- var rectTop = rect.top;
1564
- if (rect.height < height) {
1565
- rectTop -= (height - rect.height) / 2;
1566
- }
1567
- rectTop = Math.floor(rectTop);
1568
- var containerRect = this.editorMask.getBoundingClientRect();
1569
- var top = Math.floor(rectTop - containerRect.top);
1570
- var left = Math.floor(rect.left + rect.width / 2 - containerRect.left);
1571
- var rotate = 0;
1572
- if (nativeRange.collapsed) {
1573
- rotate = Math.round(Math.atan2(rect.width, rect.height) * 180 / Math.PI);
1574
- if (rotate !== 0) {
1575
- var hackEle = document.createElement('span');
1576
- // eslint-disable-next-line max-len
1577
- hackEle.style.cssText = 'display: inline-block; width: 10px; height: 10px; position: relative; contain: layout style size; writing-mode: inherit';
1578
- var pointEle = document.createElement('span');
1579
- pointEle.style.cssText = 'position: absolute; left: 0; top: 0; width:0;height:0';
1580
- hackEle.append(pointEle);
1581
- node.append(hackEle);
1582
- var p1 = pointEle.getBoundingClientRect();
1583
- pointEle.style.right = '0';
1584
- pointEle.style.left = '';
1585
- var p2 = pointEle.getBoundingClientRect();
1586
- var x = p1.x - p2.x;
1587
- var y = p1.y - p2.y;
1588
- rotate = Math.atan2(y, x) * 180 / Math.PI;
1589
- hackEle.remove();
1590
- }
1591
- }
1592
- if (writingMode === 'vertical-lr' || writingMode === 'vertical-rl') {
1593
- rotate += 90;
1922
+ });
1923
+ this.applyCaretPresentation(presentation);
1924
+ this.emitCaretPresentation(presentation);
1925
+ this.scrollCaretIntoViewIfNeeded(presentation.layoutElRect, clipContext);
1926
+ }
1927
+ },
1928
+ {
1929
+ key: "resolveCaretAnchor",
1930
+ value: function resolveCaretAnchor(nativeRange) {
1931
+ var startContainer = nativeRange.startContainer;
1932
+ var node = startContainer.nodeType === Node.ELEMENT_NODE ? startContainer : startContainer.parentNode;
1933
+ return (node === null || node === void 0 ? void 0 : node.nodeType) === Node.ELEMENT_NODE ? node : null;
1934
+ }
1935
+ },
1936
+ {
1937
+ key: "normalizeCollapsedRange",
1938
+ value: function normalizeCollapsedRange(nativeRange) {
1939
+ var compositionNode = this.domRenderer.compositionNode;
1940
+ if (!compositionNode) {
1941
+ return nativeRange;
1594
1942
  }
1943
+ var range = nativeRange.cloneRange();
1944
+ range.selectNodeContents(compositionNode);
1945
+ range.collapse();
1946
+ return range;
1947
+ }
1948
+ },
1949
+ {
1950
+ key: "applyCaretElementLayout",
1951
+ value: function applyCaretElementLayout(maskLeft, maskTop, boxHeight, fontSize, rotate) {
1595
1952
  Object.assign(this.elementRef.style, {
1596
- left: left + 'px',
1597
- top: top + 'px',
1953
+ left: maskLeft + 'px',
1954
+ top: maskTop + 'px',
1598
1955
  height: boxHeight + 'px',
1599
1956
  lineHeight: boxHeight + 'px',
1600
1957
  fontSize: fontSize,
1601
1958
  transform: "rotate(".concat(rotate, "deg)")
1602
1959
  });
1603
- this.caret.style.backgroundColor = color === 'rgba(0, 0, 0, 0)' ? '#000' : color;
1604
- this.styleChangeEvent.next({
1605
- height: boxHeight + 'px',
1606
- lineHeight: boxHeight + 'px',
1607
- fontSize: fontSize
1608
- });
1609
- this.positionChangeEvent.next({
1610
- left: left,
1611
- top: rectTop,
1612
- height: boxHeight
1613
- });
1614
- if (this.changeFromSelf) {
1615
- this.changeFromSelf = false;
1616
- var selfRect = this.elementRef.getBoundingClientRect();
1617
- var scrollContainer = this.getScrollContainer(startContainer);
1618
- var scrollRect = scrollContainer === document.documentElement ? {
1619
- top: 0,
1620
- bottom: document.documentElement.clientHeight
1621
- } : scrollContainer.getBoundingClientRect();
1622
- var limit = this.getLimit();
1623
- var top1 = Math.max(limit.top, scrollRect.top);
1624
- var bottom = Math.min(limit.bottom, scrollRect.bottom);
1625
- if (selfRect.top < top1) {
1626
- scrollContainer.scrollTop -= top1 - selfRect.top;
1627
- } else if (selfRect.bottom > bottom) {
1628
- scrollContainer.scrollTop += selfRect.bottom - bottom;
1629
- }
1960
+ }
1961
+ },
1962
+ {
1963
+ key: "applyCaretPresentation",
1964
+ value: function applyCaretPresentation(presentation) {
1965
+ this.caretClippedOut = presentation.outOfView;
1966
+ if (presentation.outOfView) {
1967
+ this.elementRef.style.clipPath = 'none';
1968
+ this.elementRef.style.visibility = 'hidden';
1969
+ this.caret.style.visibility = 'hidden';
1970
+ } else {
1971
+ this.elementRef.style.clipPath = presentation.clipPath;
1972
+ this.elementRef.style.visibility = '';
1973
+ this.caret.style.visibility = this._display ? 'visible' : 'hidden';
1630
1974
  }
1975
+ this.caret.style.backgroundColor = presentation.caretColor;
1631
1976
  }
1632
1977
  },
1633
1978
  {
1634
- key: "getScrollContainer",
1635
- value: function getScrollContainer(container) {
1636
- while(container){
1637
- if (_instanceof$2(container, Element)) {
1638
- var styles = getComputedStyle(container);
1639
- if (styles.overflow !== 'visible' || styles.overflowX !== 'visible' || styles.overflowY !== 'visible') {
1640
- return container;
1641
- }
1642
- }
1643
- container = container.parentNode;
1979
+ key: "emitCaretPresentation",
1980
+ value: function emitCaretPresentation(presentation) {
1981
+ this.styleChangeEvent.next(presentation.style);
1982
+ this.positionChangeEvent.next(presentation.position);
1983
+ }
1984
+ },
1985
+ {
1986
+ key: "scrollCaretIntoViewIfNeeded",
1987
+ value: function scrollCaretIntoViewIfNeeded(layoutElRect, clipContext) {
1988
+ var _scrollContainer_ownerDocument;
1989
+ if (!this.changeFromSelf) {
1990
+ return;
1991
+ }
1992
+ this.changeFromSelf = false;
1993
+ var scrollContainer = clipContext.firstScrollContainer;
1994
+ var doc = (_scrollContainer_ownerDocument = scrollContainer.ownerDocument) !== null && _scrollContainer_ownerDocument !== void 0 ? _scrollContainer_ownerDocument : document;
1995
+ var scrollRect = scrollContainer === doc.documentElement ? {
1996
+ top: 0,
1997
+ bottom: doc.documentElement.clientHeight
1998
+ } : scrollContainer.getBoundingClientRect();
1999
+ var limit = this.getLimit();
2000
+ var scrollTop = Math.max(limit.top, scrollRect.top);
2001
+ var scrollBottom = Math.min(limit.bottom, scrollRect.bottom);
2002
+ var layoutBottom = layoutElRect.top + layoutElRect.height;
2003
+ if (layoutElRect.top < scrollTop) {
2004
+ scrollContainer.scrollTop -= scrollTop - layoutElRect.top;
2005
+ } else if (layoutBottom > scrollBottom) {
2006
+ scrollContainer.scrollTop += layoutBottom - scrollBottom;
1644
2007
  }
1645
- return document.documentElement;
1646
2008
  }
1647
2009
  }
1648
2010
  ]);
@@ -2538,7 +2900,7 @@ var NativeCaret = /*#__PURE__*/ function() {
2538
2900
  _define_property$1(this, "_nativeRange", null);
2539
2901
  _define_property$1(this, "subs", []);
2540
2902
  _define_property$1(this, "positionChangeEvent", new stream.Subject());
2541
- this.onPositionChange = this.positionChangeEvent.pipe(stream.distinctUntilChanged());
2903
+ this.onPositionChange = this.positionChangeEvent.pipe(stream.distinctUntilChanged(caretPositionEqual));
2542
2904
  }
2543
2905
  _create_class$1(NativeCaret, [
2544
2906
  {
@@ -3391,11 +3753,16 @@ exports.Input = Input;
3391
3753
  exports.VIEW_CONTAINER = VIEW_CONTAINER;
3392
3754
  exports.VIEW_DOCUMENT = VIEW_DOCUMENT;
3393
3755
  exports.VIEW_MASK = VIEW_MASK;
3756
+ exports.caretPositionEqual = caretPositionEqual;
3757
+ exports.computeCaretPresentation = computeCaretPresentation;
3394
3758
  exports.createElement = createElement;
3395
3759
  exports.getLayoutRectByRange = getLayoutRectByRange;
3760
+ exports.getOverflowClipContext = getOverflowClipContext;
3396
3761
  exports.isFirefox = isFirefox;
3397
3762
  exports.isMac = isMac;
3398
3763
  exports.isMobileBrowser = isMobileBrowser;
3399
3764
  exports.isSafari = isSafari;
3400
3765
  exports.isWindows = isWindows;
3766
+ exports.measureCaretBoxLayout = measureCaretBoxLayout;
3767
+ exports.measureInlineCaretRotate = measureInlineCaretRotate;
3401
3768
  //# sourceMappingURL=index.js.map