@textbus/platform-browser 5.4.9 → 5.4.11

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.esm.js CHANGED
@@ -4,13 +4,13 @@ import { InjectionToken, Injectable, Inject, Optional } from '@viewfly/core';
4
4
 
5
5
  /**
6
6
  * 选区焦点可视位置
7
- */ function _array_like_to_array$2(arr, len) {
7
+ */ function _array_like_to_array$4(arr, len) {
8
8
  if (len == null || len > arr.length) len = arr.length;
9
9
  for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
10
10
  return arr2;
11
11
  }
12
- function _array_without_holes$2(arr) {
13
- if (Array.isArray(arr)) return _array_like_to_array$2(arr);
12
+ function _array_without_holes$3(arr) {
13
+ if (Array.isArray(arr)) return _array_like_to_array$4(arr);
14
14
  }
15
15
  function _instanceof$4(left, right) {
16
16
  "@swc/helpers - instanceof";
@@ -20,22 +20,22 @@ function _instanceof$4(left, right) {
20
20
  return left instanceof right;
21
21
  }
22
22
  }
23
- function _iterable_to_array$2(iter) {
23
+ function _iterable_to_array$3(iter) {
24
24
  if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
25
25
  }
26
- function _non_iterable_spread$2() {
26
+ function _non_iterable_spread$3() {
27
27
  throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
28
28
  }
29
- function _to_consumable_array$2(arr) {
30
- return _array_without_holes$2(arr) || _iterable_to_array$2(arr) || _unsupported_iterable_to_array$2(arr) || _non_iterable_spread$2();
29
+ function _to_consumable_array$3(arr) {
30
+ return _array_without_holes$3(arr) || _iterable_to_array$3(arr) || _unsupported_iterable_to_array$4(arr) || _non_iterable_spread$3();
31
31
  }
32
- function _unsupported_iterable_to_array$2(o, minLen) {
32
+ function _unsupported_iterable_to_array$4(o, minLen) {
33
33
  if (!o) return;
34
- if (typeof o === "string") return _array_like_to_array$2(o, minLen);
34
+ if (typeof o === "string") return _array_like_to_array$4(o, minLen);
35
35
  var n = Object.prototype.toString.call(o).slice(8, -1);
36
36
  if (n === "Object" && o.constructor) n = o.constructor.name;
37
37
  if (n === "Map" || n === "Set") return Array.from(n);
38
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array$2(o, minLen);
38
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array$4(o, minLen);
39
39
  }
40
40
  var OVERFLOW_CLIP_VALUES = new Set([
41
41
  'auto',
@@ -98,7 +98,7 @@ function readOverflowClipAxes(style) {
98
98
  }
99
99
  return {
100
100
  clipAncestors: clipAncestors,
101
- scrollContainers: _to_consumable_array$2(scrollContainers),
101
+ scrollContainers: _to_consumable_array$3(scrollContainers),
102
102
  firstScrollContainer: (_ref1 = (_clipAncestors_ = clipAncestors[0]) === null || _clipAncestors_ === void 0 ? void 0 : _clipAncestors_.element) !== null && _ref1 !== void 0 ? _ref1 : documentElement
103
103
  };
104
104
  }
@@ -283,7 +283,7 @@ function createElement(tagName) {
283
283
  var el = document.createElement(tagName);
284
284
  if (options.classes) {
285
285
  var _el_classList;
286
- (_el_classList = el.classList).add.apply(_el_classList, _to_consumable_array$2(options.classes));
286
+ (_el_classList = el.classList).add.apply(_el_classList, _to_consumable_array$3(options.classes));
287
287
  }
288
288
  if (options.attrs) {
289
289
  Object.keys(options.attrs).forEach(function(key) {
@@ -418,13 +418,13 @@ var isMobileBrowser = function isMobileBrowser() {
418
418
  * 编辑器容器遮罩层 token
419
419
  */ var VIEW_MASK = new InjectionToken('VIEW_MASK');
420
420
 
421
- function _array_like_to_array$1(arr, len) {
421
+ function _array_like_to_array$3(arr, len) {
422
422
  if (len == null || len > arr.length) len = arr.length;
423
423
  for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
424
424
  return arr2;
425
425
  }
426
- function _array_without_holes$1(arr) {
427
- if (Array.isArray(arr)) return _array_like_to_array$1(arr);
426
+ function _array_without_holes$2(arr) {
427
+ if (Array.isArray(arr)) return _array_like_to_array$3(arr);
428
428
  }
429
429
  function _class_call_check$7(instance, Constructor) {
430
430
  if (!(instance instanceof Constructor)) {
@@ -466,22 +466,22 @@ function _instanceof$3(left, right) {
466
466
  return left instanceof right;
467
467
  }
468
468
  }
469
- function _iterable_to_array$1(iter) {
469
+ function _iterable_to_array$2(iter) {
470
470
  if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
471
471
  }
472
- function _non_iterable_spread$1() {
472
+ function _non_iterable_spread$2() {
473
473
  throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
474
474
  }
475
- function _to_consumable_array$1(arr) {
476
- return _array_without_holes$1(arr) || _iterable_to_array$1(arr) || _unsupported_iterable_to_array$1(arr) || _non_iterable_spread$1();
475
+ function _to_consumable_array$2(arr) {
476
+ return _array_without_holes$2(arr) || _iterable_to_array$2(arr) || _unsupported_iterable_to_array$3(arr) || _non_iterable_spread$2();
477
477
  }
478
- function _unsupported_iterable_to_array$1(o, minLen) {
478
+ function _unsupported_iterable_to_array$3(o, minLen) {
479
479
  if (!o) return;
480
- if (typeof o === "string") return _array_like_to_array$1(o, minLen);
480
+ if (typeof o === "string") return _array_like_to_array$3(o, minLen);
481
481
  var n = Object.prototype.toString.call(o).slice(8, -1);
482
482
  if (n === "Object" && o.constructor) n = o.constructor.name;
483
483
  if (n === "Map" || n === "Set") return Array.from(n);
484
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array$1(o, minLen);
484
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array$3(o, minLen);
485
485
  }
486
486
  function _ts_decorate$4(decorators, target, key, desc) {
487
487
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
@@ -505,9 +505,9 @@ var Parser = /*#__PURE__*/ function() {
505
505
  _define_property$6(this, "formatLoaders", void 0);
506
506
  _define_property$6(this, "attributeLoaders", void 0);
507
507
  this.textbus = textbus;
508
- var componentLoaders = _to_consumable_array$1(options.componentLoaders || []);
509
- var formatLoaders = _to_consumable_array$1(options.formatLoaders || []);
510
- var attributeLoaders = _to_consumable_array$1(options.attributeLoaders || []);
508
+ var componentLoaders = _to_consumable_array$2(options.componentLoaders || []);
509
+ var formatLoaders = _to_consumable_array$2(options.formatLoaders || []);
510
+ var attributeLoaders = _to_consumable_array$2(options.attributeLoaders || []);
511
511
  // options.imports?.forEach(i => {
512
512
  // componentLoaders.push(...(i.componentLoaders || []))
513
513
  // formatLoaders.push(...(i.formatLoaders || []))
@@ -551,7 +551,7 @@ var Parser = /*#__PURE__*/ function() {
551
551
  slot.insert('\n');
552
552
  return;
553
553
  }
554
- var schema = _to_consumable_array$1(slot.schema);
554
+ var schema = _to_consumable_array$2(slot.schema);
555
555
  var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
556
556
  try {
557
557
  for(var _iterator = this.componentLoaders[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
@@ -1074,13 +1074,10 @@ var SelectionBridge = /*#__PURE__*/ function() {
1074
1074
  value: function getLinePosition(currentPosition, toNext) {
1075
1075
  var _this = this;
1076
1076
  clearTimeout(this.cacheCaretPositionTimer);
1077
- var p;
1078
- if (this.oldCaretPosition) {
1079
- p = toNext ? this.getNextLinePositionByOffset(currentPosition, this.oldCaretPosition.left) : this.getPreviousLinePositionByOffset(currentPosition, this.oldCaretPosition.left);
1080
- } else {
1077
+ if (!this.oldCaretPosition) {
1081
1078
  this.oldCaretPosition = this.getRect(currentPosition);
1082
- p = toNext ? this.getNextLinePositionByOffset(currentPosition, this.oldCaretPosition.left) : this.getPreviousLinePositionByOffset(currentPosition, this.oldCaretPosition.left);
1083
1079
  }
1080
+ var p = this.getVerticalMovePosition(currentPosition, this.oldCaretPosition.left, toNext);
1084
1081
  this.cacheCaretPositionTimer = setTimeout(function() {
1085
1082
  _this.oldCaretPosition = null;
1086
1083
  }, 3000);
@@ -1088,119 +1085,110 @@ var SelectionBridge = /*#__PURE__*/ function() {
1088
1085
  }
1089
1086
  },
1090
1087
  {
1091
- key: "getPreviousLinePositionByOffset",
1088
+ key: "getVerticalMovePosition",
1092
1089
  value: /**
1093
- * 获取选区向上移动一行的位置。
1094
- * @param currentPosition
1095
- * @param startLeft 参考位置。
1096
- */ function getPreviousLinePositionByOffset(currentPosition, startLeft) {
1097
- var isToPrevLine = false;
1098
- var loopCount = 0;
1099
- var minLeft = startLeft;
1100
- var focusSlot = currentPosition.slot;
1101
- var focusOffset = currentPosition.offset;
1102
- var minTop = this.getRect({
1103
- slot: focusSlot,
1104
- offset: focusOffset
1105
- }).top;
1106
- var position;
1107
- var oldPosition;
1108
- var oldLeft = 0;
1090
+ * 通过遍历模型位置 + 视觉 rect 判断换行,计算光标到下一行或上一行的位置。
1091
+ * 不使用 Selection.modify(),避免复杂布局中的死循环。
1092
+ */ function getVerticalMovePosition(currentPosition, startLeft, toNext) {
1093
+ var _this = this;
1094
+ var startRect = this.getRect(currentPosition);
1095
+ if (!startRect) return currentPosition;
1096
+ var startTop = startRect.top;
1097
+ var startBottom = startTop + startRect.height;
1098
+ var step = toNext ? function(pos) {
1099
+ return _this.selection.getNextPositionByPosition(pos.slot, pos.offset);
1100
+ } : function(pos) {
1101
+ return _this.selection.getPreviousPositionByPosition(pos.slot, pos.offset);
1102
+ };
1103
+ var cur = currentPosition;
1109
1104
  while(true){
1110
- loopCount++;
1111
- position = this.selection.getPreviousPositionByPosition(focusSlot, focusOffset);
1112
- focusSlot = position.slot;
1113
- focusOffset = position.offset;
1114
- var rect2 = this.getRect(position);
1115
- if (!isToPrevLine) {
1116
- if (rect2.left > minLeft || rect2.top + rect2.height <= minTop) {
1117
- isToPrevLine = true;
1118
- } else if (rect2.left === minLeft && rect2.top === minTop) {
1119
- return position;
1120
- }
1121
- minLeft = rect2.left;
1122
- minTop = rect2.top;
1123
- // oldPosition = position
1124
- }
1125
- if (isToPrevLine) {
1126
- if (rect2.left <= startLeft) {
1127
- return position;
1128
- }
1129
- if (oldPosition) {
1130
- if (rect2.left >= oldLeft) {
1131
- return oldPosition;
1132
- }
1133
- }
1134
- oldLeft = rect2.left;
1135
- oldPosition = position;
1136
- }
1137
- if (loopCount > 10000) {
1138
- break;
1105
+ var next = step(cur);
1106
+ if (next.slot === cur.slot && next.offset === cur.offset) break;
1107
+ var rect = this.getRect(next);
1108
+ if (!rect) break;
1109
+ cur = next;
1110
+ if (this.isDifferentLine(startTop, startBottom, rect, toNext)) {
1111
+ return this.refineXOnLine(cur, startLeft, rect);
1139
1112
  }
1140
1113
  }
1141
- return position || {
1142
- offset: 0,
1143
- slot: focusSlot
1144
- };
1114
+ return this.getDocumentBoundary(toNext);
1115
+ }
1116
+ },
1117
+ {
1118
+ key: "isDifferentLine",
1119
+ value: /** 目标位置是否已离开起始行 */ function isDifferentLine(startTop, startBottom, targetRect, toNext) {
1120
+ if (toNext) {
1121
+ return targetRect.top >= startBottom;
1122
+ }
1123
+ return targetRect.top + targetRect.height <= startTop;
1145
1124
  }
1146
1125
  },
1147
1126
  {
1148
- key: "getNextLinePositionByOffset",
1127
+ key: "refineXOnLine",
1149
1128
  value: /**
1150
- * 获取选区向下移动一行的位置。
1151
- * @param currentPosition
1152
- * @param startLeft 参考位置。
1153
- */ function getNextLinePositionByOffset(currentPosition, startLeft) {
1154
- var isToNextLine = false;
1155
- var loopCount = 0;
1156
- var maxRight = startLeft;
1157
- var focusSlot = currentPosition.slot;
1158
- var focusOffset = currentPosition.offset;
1159
- var rect = this.getRect({
1160
- slot: focusSlot,
1161
- offset: focusOffset
1162
- });
1163
- var minTop = rect.top;
1164
- var oldPosition;
1165
- var oldLeft = 0;
1129
+ * 沿目标行微调位置。
1130
+ * 起点在 targetX 右侧 → 向左找到离 targetX 最近的右侧位置;
1131
+ * 起点在 targetX 左侧 → 向右找到第一个右侧位置。
1132
+ */ function refineXOnLine(position, targetX, lineRect) {
1133
+ var startRect = this.getRect(position);
1134
+ var startLeft = startRect.left;
1135
+ var lineTop = lineRect.top;
1136
+ var lineBottom = lineTop + Math.max(lineRect.height, 12);
1137
+ if (startLeft === targetX) return position;
1138
+ var cur = position;
1139
+ if (startLeft > targetX) {
1140
+ var rightSide = position;
1141
+ while(true){
1142
+ var prev = this.selection.getPreviousPositionByPosition(cur.slot, cur.offset);
1143
+ if (prev.slot === cur.slot && prev.offset === cur.offset) break;
1144
+ var rect = this.getRect(prev);
1145
+ if (!rect) break;
1146
+ if (rect.top >= lineBottom || rect.top + rect.height <= lineTop) break;
1147
+ cur = prev;
1148
+ if (rect.left === targetX) return prev;
1149
+ if (rect.left < targetX) return rightSide;
1150
+ rightSide = prev;
1151
+ }
1152
+ return rightSide;
1153
+ }
1166
1154
  while(true){
1167
- loopCount++;
1168
- var position = this.selection.getNextPositionByPosition(focusSlot, focusOffset);
1169
- focusSlot = position.slot;
1170
- focusOffset = position.offset;
1171
- var rect2 = this.getRect(position);
1172
- if (!isToNextLine) {
1173
- if (rect2.left < maxRight || rect2.top >= minTop + rect.height) {
1174
- isToNextLine = true;
1175
- } else if (rect2.left === maxRight && rect2.top === minTop) {
1176
- return position;
1177
- }
1178
- maxRight = rect2.left;
1179
- minTop = rect2.top;
1180
- oldPosition = position;
1181
- }
1182
- if (isToNextLine) {
1183
- if (rect2.left > startLeft) {
1184
- return oldPosition;
1185
- }
1186
- if (oldPosition) {
1187
- if (rect2.left <= oldLeft) {
1188
- return oldPosition;
1189
- }
1190
- }
1191
- oldPosition = position;
1192
- oldLeft = rect2.left;
1193
- }
1194
- if (loopCount > 10000) {
1195
- break;
1196
- }
1155
+ var next = this.selection.getNextPositionByPosition(cur.slot, cur.offset);
1156
+ if (next.slot === cur.slot && next.offset === cur.offset) break;
1157
+ var rect1 = this.getRect(next);
1158
+ if (!rect1) break;
1159
+ if (rect1.top >= lineBottom || rect1.top + rect1.height <= lineTop) break;
1160
+ cur = next;
1161
+ if (rect1.left === targetX) return cur;
1162
+ if (rect1.left > targetX) return cur;
1163
+ }
1164
+ return cur;
1165
+ }
1166
+ },
1167
+ {
1168
+ key: "getDocumentBoundary",
1169
+ value: /** 跳转到文档开头或结尾 */ function getDocumentBoundary(toNext) {
1170
+ var slots = this.rootComponentRef.component.slots;
1171
+ if (toNext) {
1172
+ var lastSlot = slots[slots.length - 1];
1173
+ return {
1174
+ slot: lastSlot,
1175
+ offset: lastSlot.length
1176
+ };
1197
1177
  }
1198
- return oldPosition || {
1199
- offset: focusSlot.length,
1200
- slot: focusSlot
1178
+ var firstSlot = slots[0];
1179
+ return {
1180
+ slot: firstSlot,
1181
+ offset: 0
1201
1182
  };
1202
1183
  }
1203
1184
  },
1185
+ {
1186
+ key: "caretPositionFromPoint",
1187
+ value: function caretPositionFromPoint(x, rect) {
1188
+ var _document_caretPositionFromPoint, _document;
1189
+ return (_document_caretPositionFromPoint = (_document = document).caretPositionFromPoint) === null || _document_caretPositionFromPoint === void 0 ? void 0 : _document_caretPositionFromPoint.call(_document, x, rect.top + rect.height / 2);
1190
+ }
1191
+ },
1204
1192
  {
1205
1193
  key: "unListen",
1206
1194
  value: function unListen() {
@@ -1500,7 +1488,7 @@ var SelectionBridge = /*#__PURE__*/ function() {
1500
1488
  };
1501
1489
  }
1502
1490
  var firstChild = toAfter ? node.firstChild : node.lastChild;
1503
- if (firstChild) {
1491
+ if (firstChild && !excludeNodes.includes(firstChild)) {
1504
1492
  return this.findFocusNode(firstChild, toAfter, excludeNodes);
1505
1493
  }
1506
1494
  var nextSibling = toAfter ? node.nextSibling : node.previousSibling;
@@ -1515,6 +1503,13 @@ var SelectionBridge = /*#__PURE__*/ function() {
1515
1503
  value: function findFocusNodeByParent(node, toAfter, excludeNodes) {
1516
1504
  var parentNode = node.parentNode;
1517
1505
  if (parentNode) {
1506
+ if (excludeNodes.includes(parentNode)) {
1507
+ var nextNode = toAfter ? parentNode.nextSibling : parentNode.previousSibling;
1508
+ if (nextNode) {
1509
+ return this.findFocusNode(nextNode, toAfter, excludeNodes);
1510
+ }
1511
+ return this.findFocusNodeByParent(parentNode, toAfter, excludeNodes);
1512
+ }
1518
1513
  var parentPosition = this.domAdapter.getLocationByNativeNode(parentNode);
1519
1514
  if (parentPosition) {
1520
1515
  return {
@@ -1522,8 +1517,12 @@ var SelectionBridge = /*#__PURE__*/ function() {
1522
1517
  offset: toAfter ? parentPosition.endIndex : parentPosition.startIndex
1523
1518
  };
1524
1519
  }
1525
- excludeNodes.push(node);
1526
- return this.findFocusNode(parentNode, toAfter, excludeNodes);
1520
+ excludeNodes.push(parentNode);
1521
+ var nextNode1 = toAfter ? parentNode.nextSibling : parentNode.previousSibling;
1522
+ if (nextNode1) {
1523
+ return this.findFocusNode(nextNode1, toAfter, excludeNodes);
1524
+ }
1525
+ return this.findFocusNodeByParent(parentNode, toAfter, excludeNodes);
1527
1526
  }
1528
1527
  return null;
1529
1528
  }
@@ -1547,6 +1546,14 @@ SelectionBridge = _ts_decorate$3([
1547
1546
  ])
1548
1547
  ], SelectionBridge);
1549
1548
 
1549
+ function _array_like_to_array$2(arr, len) {
1550
+ if (len == null || len > arr.length) len = arr.length;
1551
+ for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
1552
+ return arr2;
1553
+ }
1554
+ function _array_without_holes$1(arr) {
1555
+ if (Array.isArray(arr)) return _array_like_to_array$2(arr);
1556
+ }
1550
1557
  function _assert_this_initialized$1(self) {
1551
1558
  if (self === void 0) {
1552
1559
  throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
@@ -1607,6 +1614,12 @@ function _inherits$1(subClass, superClass) {
1607
1614
  });
1608
1615
  if (superClass) _set_prototype_of$1(subClass, superClass);
1609
1616
  }
1617
+ function _iterable_to_array$1(iter) {
1618
+ if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
1619
+ }
1620
+ function _non_iterable_spread$1() {
1621
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
1622
+ }
1610
1623
  function _possible_constructor_return$1(self, call) {
1611
1624
  if (call && (_type_of$1(call) === "object" || typeof call === "function")) {
1612
1625
  return call;
@@ -1620,10 +1633,21 @@ function _set_prototype_of$1(o, p) {
1620
1633
  };
1621
1634
  return _set_prototype_of$1(o, p);
1622
1635
  }
1636
+ function _to_consumable_array$1(arr) {
1637
+ return _array_without_holes$1(arr) || _iterable_to_array$1(arr) || _unsupported_iterable_to_array$2(arr) || _non_iterable_spread$1();
1638
+ }
1623
1639
  function _type_of$1(obj) {
1624
1640
  "@swc/helpers - typeof";
1625
1641
  return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
1626
1642
  }
1643
+ function _unsupported_iterable_to_array$2(o, minLen) {
1644
+ if (!o) return;
1645
+ if (typeof o === "string") return _array_like_to_array$2(o, minLen);
1646
+ var n = Object.prototype.toString.call(o).slice(8, -1);
1647
+ if (n === "Object" && o.constructor) n = o.constructor.name;
1648
+ if (n === "Map" || n === "Set") return Array.from(n);
1649
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array$2(o, minLen);
1650
+ }
1627
1651
  function _is_native_reflect_construct$1() {
1628
1652
  try {
1629
1653
  var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
@@ -1981,25 +2005,49 @@ var ExperimentalCaret = /*#__PURE__*/ function() {
1981
2005
  {
1982
2006
  key: "scrollCaretIntoViewIfNeeded",
1983
2007
  value: function scrollCaretIntoViewIfNeeded(layoutElRect, clipContext) {
1984
- var _scrollContainer_ownerDocument;
1985
2008
  if (!this.changeFromSelf) {
1986
2009
  return;
1987
2010
  }
1988
2011
  this.changeFromSelf = false;
1989
- var scrollContainer = clipContext.firstScrollContainer;
1990
- var doc = (_scrollContainer_ownerDocument = scrollContainer.ownerDocument) !== null && _scrollContainer_ownerDocument !== void 0 ? _scrollContainer_ownerDocument : document;
1991
- var scrollRect = scrollContainer === doc.documentElement ? {
1992
- top: 0,
1993
- bottom: doc.documentElement.clientHeight
1994
- } : scrollContainer.getBoundingClientRect();
1995
2012
  var limit = this.getLimit();
1996
- var scrollTop = Math.max(limit.top, scrollRect.top);
1997
- var scrollBottom = Math.min(limit.bottom, scrollRect.bottom);
1998
- var layoutBottom = layoutElRect.top + layoutElRect.height;
1999
- if (layoutElRect.top < scrollTop) {
2000
- scrollContainer.scrollTop -= scrollTop - layoutElRect.top;
2001
- } else if (layoutBottom > scrollBottom) {
2002
- scrollContainer.scrollTop += layoutBottom - scrollBottom;
2013
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
2014
+ try {
2015
+ // 从外到内逐层处理,每滚动一层后重新获取光标位置再处理内层
2016
+ for(var _iterator = _to_consumable_array$1(clipContext.scrollContainers).reverse()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
2017
+ var scrollContainer = _step.value;
2018
+ var _scrollContainer_ownerDocument;
2019
+ var doc = (_scrollContainer_ownerDocument = scrollContainer.ownerDocument) !== null && _scrollContainer_ownerDocument !== void 0 ? _scrollContainer_ownerDocument : document;
2020
+ var isDoc = scrollContainer === doc.documentElement;
2021
+ var scrollRect = isDoc ? {
2022
+ top: 0,
2023
+ bottom: doc.documentElement.clientHeight
2024
+ } : scrollContainer.getBoundingClientRect();
2025
+ var visibleTop = Math.max(limit.top, scrollRect.top);
2026
+ var visibleBottom = Math.min(limit.bottom, scrollRect.bottom);
2027
+ var caretBottom = layoutElRect.top + layoutElRect.height;
2028
+ if (layoutElRect.top >= visibleTop && caretBottom <= visibleBottom) {
2029
+ continue; // 光标已在当前容器可视区域内,无需滚动
2030
+ }
2031
+ if (layoutElRect.top < visibleTop) {
2032
+ scrollContainer.scrollTop -= visibleTop - layoutElRect.top;
2033
+ } else if (caretBottom > visibleBottom) {
2034
+ scrollContainer.scrollTop += caretBottom - visibleBottom;
2035
+ }
2036
+ layoutElRect = this.elementRef.getBoundingClientRect();
2037
+ }
2038
+ } catch (err) {
2039
+ _didIteratorError = true;
2040
+ _iteratorError = err;
2041
+ } finally{
2042
+ try {
2043
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
2044
+ _iterator.return();
2045
+ }
2046
+ } finally{
2047
+ if (_didIteratorError) {
2048
+ throw _iteratorError;
2049
+ }
2050
+ }
2003
2051
  }
2004
2052
  }
2005
2053
  }
@@ -2131,9 +2179,7 @@ var MagicInput = /*#__PURE__*/ function(Input) {
2131
2179
  }), fromEvent(textarea, 'focus').subscribe(function() {
2132
2180
  _this.nativeFocus = true;
2133
2181
  }), this.caret.onStyleChange.subscribe(function(style) {
2134
- Object.assign(textarea.style, {
2135
- fontSize: style.fontSize
2136
- });
2182
+ Object.assign(textarea.style, style);
2137
2183
  }));
2138
2184
  this.handleInput(textarea);
2139
2185
  this.handleShortcut(textarea);
@@ -2377,8 +2423,9 @@ var MagicInput = /*#__PURE__*/ function(Input) {
2377
2423
  border: 'none',
2378
2424
  width: '100%',
2379
2425
  display: 'block',
2380
- height: '16px',
2381
- position: 'relative',
2426
+ height: '100%',
2427
+ position: 'absolute',
2428
+ left: 0,
2382
2429
  bottom: this.isWindows ? '3px' : '0'
2383
2430
  }
2384
2431
  });
@@ -2402,13 +2449,13 @@ MagicInput = _ts_decorate$2([
2402
2449
  ])
2403
2450
  ], MagicInput);
2404
2451
 
2405
- function _array_like_to_array(arr, len) {
2452
+ function _array_like_to_array$1(arr, len) {
2406
2453
  if (len == null || len > arr.length) len = arr.length;
2407
2454
  for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
2408
2455
  return arr2;
2409
2456
  }
2410
2457
  function _array_without_holes(arr) {
2411
- if (Array.isArray(arr)) return _array_like_to_array(arr);
2458
+ if (Array.isArray(arr)) return _array_like_to_array$1(arr);
2412
2459
  }
2413
2460
  function _class_call_check$2(instance, Constructor) {
2414
2461
  if (!(instance instanceof Constructor)) {
@@ -2448,15 +2495,15 @@ function _non_iterable_spread() {
2448
2495
  throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
2449
2496
  }
2450
2497
  function _to_consumable_array(arr) {
2451
- return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
2498
+ return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array$1(arr) || _non_iterable_spread();
2452
2499
  }
2453
- function _unsupported_iterable_to_array(o, minLen) {
2500
+ function _unsupported_iterable_to_array$1(o, minLen) {
2454
2501
  if (!o) return;
2455
- if (typeof o === "string") return _array_like_to_array(o, minLen);
2502
+ if (typeof o === "string") return _array_like_to_array$1(o, minLen);
2456
2503
  var n = Object.prototype.toString.call(o).slice(8, -1);
2457
2504
  if (n === "Object" && o.constructor) n = o.constructor.name;
2458
2505
  if (n === "Map" || n === "Set") return Array.from(n);
2459
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
2506
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array$1(o, minLen);
2460
2507
  }
2461
2508
  function _ts_decorate$1(decorators, target, key, desc) {
2462
2509
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
@@ -2789,6 +2836,14 @@ CollaborateCursor = _ts_decorate$1([
2789
2836
  ])
2790
2837
  ], CollaborateCursor);
2791
2838
 
2839
+ function _array_like_to_array(arr, len) {
2840
+ if (len == null || len > arr.length) len = arr.length;
2841
+ for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
2842
+ return arr2;
2843
+ }
2844
+ function _array_with_holes(arr) {
2845
+ if (Array.isArray(arr)) return arr;
2846
+ }
2792
2847
  function _assert_this_initialized(self) {
2793
2848
  if (self === void 0) {
2794
2849
  throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
@@ -2857,6 +2912,33 @@ function _instanceof$1(left, right) {
2857
2912
  return left instanceof right;
2858
2913
  }
2859
2914
  }
2915
+ function _iterable_to_array_limit(arr, i) {
2916
+ var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
2917
+ if (_i == null) return;
2918
+ var _arr = [];
2919
+ var _n = true;
2920
+ var _d = false;
2921
+ var _s, _e;
2922
+ try {
2923
+ for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
2924
+ _arr.push(_s.value);
2925
+ if (i && _arr.length === i) break;
2926
+ }
2927
+ } catch (err) {
2928
+ _d = true;
2929
+ _e = err;
2930
+ } finally{
2931
+ try {
2932
+ if (!_n && _i["return"] != null) _i["return"]();
2933
+ } finally{
2934
+ if (_d) throw _e;
2935
+ }
2936
+ }
2937
+ return _arr;
2938
+ }
2939
+ function _non_iterable_rest() {
2940
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
2941
+ }
2860
2942
  function _possible_constructor_return(self, call) {
2861
2943
  if (call && (_type_of(call) === "object" || typeof call === "function")) {
2862
2944
  return call;
@@ -2870,10 +2952,21 @@ function _set_prototype_of(o, p) {
2870
2952
  };
2871
2953
  return _set_prototype_of(o, p);
2872
2954
  }
2955
+ function _sliced_to_array(arr, i) {
2956
+ return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
2957
+ }
2873
2958
  function _type_of(obj) {
2874
2959
  "@swc/helpers - typeof";
2875
2960
  return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
2876
2961
  }
2962
+ function _unsupported_iterable_to_array(o, minLen) {
2963
+ if (!o) return;
2964
+ if (typeof o === "string") return _array_like_to_array(o, minLen);
2965
+ var n = Object.prototype.toString.call(o).slice(8, -1);
2966
+ if (n === "Object" && o.constructor) n = o.constructor.name;
2967
+ if (n === "Map" || n === "Set") return Array.from(n);
2968
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
2969
+ }
2877
2970
  function _is_native_reflect_construct() {
2878
2971
  try {
2879
2972
  var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
@@ -2891,6 +2984,150 @@ function _ts_decorate(decorators, target, key, desc) {
2891
2984
  function _ts_metadata(k, v) {
2892
2985
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
2893
2986
  }
2987
+ /**
2988
+ * 轻量级 DOM 文本变化记录器,仅在 composition 期间激活。
2989
+ * 记录 composition 期间 DOM 中新增/修改的文本节点,在 compositionend 时从 DOM 读取最终文本。
2990
+ * 写入模型后清理浏览器创建的新文本节点,避免残留在文档中。
2991
+ */ var CompositionRecorder = /*#__PURE__*/ function() {
2992
+ function CompositionRecorder() {
2993
+ _class_call_check$1(this, CompositionRecorder);
2994
+ _define_property$1(this, "observer", null);
2995
+ _define_property$1(this, "nodeOldValues", new Map());
2996
+ }
2997
+ _create_class$1(CompositionRecorder, [
2998
+ {
2999
+ key: "start",
3000
+ value: function start(target) {
3001
+ var _this = this;
3002
+ this.nodeOldValues.clear();
3003
+ this.observer = new MutationObserver(function(mutations) {
3004
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
3005
+ try {
3006
+ for(var _iterator = mutations[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
3007
+ var m = _step.value;
3008
+ if (m.type === 'childList') {
3009
+ var _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = undefined;
3010
+ try {
3011
+ for(var _iterator1 = Array.from(m.addedNodes)[Symbol.iterator](), _step1; !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = true){
3012
+ var node = _step1.value;
3013
+ if (node.nodeType === Node.TEXT_NODE && !_this.nodeOldValues.has(node)) {
3014
+ _this.nodeOldValues.set(node, null);
3015
+ }
3016
+ }
3017
+ } catch (err) {
3018
+ _didIteratorError1 = true;
3019
+ _iteratorError1 = err;
3020
+ } finally{
3021
+ try {
3022
+ if (!_iteratorNormalCompletion1 && _iterator1.return != null) {
3023
+ _iterator1.return();
3024
+ }
3025
+ } finally{
3026
+ if (_didIteratorError1) {
3027
+ throw _iteratorError1;
3028
+ }
3029
+ }
3030
+ }
3031
+ } else if (m.type === 'characterData') {
3032
+ var _$target = m.target;
3033
+ if (!_this.nodeOldValues.has(_$target)) {
3034
+ _this.nodeOldValues.set(_$target, m.oldValue);
3035
+ }
3036
+ }
3037
+ }
3038
+ } catch (err) {
3039
+ _didIteratorError = true;
3040
+ _iteratorError = err;
3041
+ } finally{
3042
+ try {
3043
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
3044
+ _iterator.return();
3045
+ }
3046
+ } finally{
3047
+ if (_didIteratorError) {
3048
+ throw _iteratorError;
3049
+ }
3050
+ }
3051
+ }
3052
+ });
3053
+ this.observer.observe(target, {
3054
+ childList: true,
3055
+ subtree: true,
3056
+ characterData: true,
3057
+ characterDataOldValue: true
3058
+ });
3059
+ }
3060
+ },
3061
+ {
3062
+ key: "readText",
3063
+ value: function readText() {
3064
+ var text = '';
3065
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
3066
+ try {
3067
+ for(var _iterator = this.nodeOldValues[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
3068
+ var _step_value = _sliced_to_array(_step.value, 2), node = _step_value[0], oldValue = _step_value[1];
3069
+ if (!node.isConnected) continue;
3070
+ var current = node.textContent || '';
3071
+ if (!current) continue;
3072
+ if (oldValue === null) {
3073
+ text += current;
3074
+ } else {
3075
+ text += this.diffText(oldValue, current);
3076
+ }
3077
+ }
3078
+ } catch (err) {
3079
+ _didIteratorError = true;
3080
+ _iteratorError = err;
3081
+ } finally{
3082
+ try {
3083
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
3084
+ _iterator.return();
3085
+ }
3086
+ } finally{
3087
+ if (_didIteratorError) {
3088
+ throw _iteratorError;
3089
+ }
3090
+ }
3091
+ }
3092
+ return text;
3093
+ }
3094
+ },
3095
+ {
3096
+ /** 移除 composition 期间浏览器创建的文本节点(焦点处),光标位置不可能被协作触及 */ key: "cleanup",
3097
+ value: function cleanup(committedText, focusNode) {
3098
+ if (_instanceof$1(focusNode, Text) && focusNode.isConnected && focusNode.textContent === committedText && this.nodeOldValues.has(focusNode) && focusNode.parentNode) {
3099
+ focusNode.parentNode.removeChild(focusNode);
3100
+ }
3101
+ this.nodeOldValues.clear();
3102
+ }
3103
+ },
3104
+ {
3105
+ key: "stop",
3106
+ value: function stop() {
3107
+ var _this_observer;
3108
+ (_this_observer = this.observer) === null || _this_observer === void 0 ? void 0 : _this_observer.disconnect();
3109
+ this.observer = null;
3110
+ }
3111
+ },
3112
+ {
3113
+ key: "diffText",
3114
+ value: function diffText(oldStr, newStr) {
3115
+ var start = 0;
3116
+ while(start < oldStr.length && start < newStr.length && oldStr[start] === newStr[start]){
3117
+ start++;
3118
+ }
3119
+ var oldEnd = oldStr.length - 1;
3120
+ var newEnd = newStr.length - 1;
3121
+ while(oldEnd >= start && newEnd >= start && oldStr[oldEnd] === newStr[newEnd]){
3122
+ oldEnd--;
3123
+ newEnd--;
3124
+ }
3125
+ return newStr.slice(start, newEnd + 1);
3126
+ }
3127
+ }
3128
+ ]);
3129
+ return CompositionRecorder;
3130
+ }();
2894
3131
  var NativeCaret = /*#__PURE__*/ function() {
2895
3132
  function NativeCaret() {
2896
3133
  _class_call_check$1(this, NativeCaret);
@@ -2962,8 +3199,7 @@ var NativeInput = /*#__PURE__*/ function(Input) {
2962
3199
  _class_call_check$1(this, NativeInput);
2963
3200
  var _this;
2964
3201
  _this = _call_super(this, NativeInput), _define_property$1(_this, "parser", void 0), _define_property$1(_this, "selection", void 0), _define_property$1(_this, "keyboard", void 0), _define_property$1(_this, "domAdapter", void 0), _define_property$1(_this, "commander", void 0), _define_property$1(_this, "controller", void 0), _define_property$1(_this, "caret", void 0), _define_property$1(_this, "composition", void 0), // compositionState: CompositionState | null = null
2965
- _define_property$1(_this, "onReady", void 0), _define_property$1(_this, "_disabled", void 0), _define_property$1(_this, "documentView", void 0), _define_property$1(_this, "nativeSelection", void 0), _define_property$1(_this, "subscription", void 0), _define_property$1(_this, "nativeRange", void 0), _define_property$1(_this, "isSafari", void 0), _define_property$1(_this, "isMac", void 0), _define_property$1(_this, "isMobileBrowser", void 0), _define_property$1(_this, "ignoreComposition", void 0 // bug 版本搜狗拼音
2966
- ), _this.parser = parser, _this.selection = selection, _this.keyboard = keyboard, _this.domAdapter = domAdapter, _this.commander = commander, _this.controller = controller, _this.caret = new NativeCaret(), _this.composition = false, _this.onReady = Promise.resolve(), _this._disabled = false, _this.nativeSelection = document.getSelection(), _this.subscription = new Subscription(), _this.nativeRange = null, _this.isSafari = isSafari(), _this.isMac = isMac(), _this.isMobileBrowser = isMobileBrowser(), _this.ignoreComposition = false;
3202
+ _define_property$1(_this, "onReady", void 0), _define_property$1(_this, "_disabled", void 0), _define_property$1(_this, "documentView", void 0), _define_property$1(_this, "nativeSelection", void 0), _define_property$1(_this, "subscription", void 0), _define_property$1(_this, "nativeRange", void 0), _define_property$1(_this, "isSafari", void 0), _define_property$1(_this, "isMac", void 0), _define_property$1(_this, "compositionEndedAt", void 0), _this.parser = parser, _this.selection = selection, _this.keyboard = keyboard, _this.domAdapter = domAdapter, _this.commander = commander, _this.controller = controller, _this.caret = new NativeCaret(), _this.composition = false, _this.onReady = Promise.resolve(), _this._disabled = false, _this.nativeSelection = document.getSelection(), _this.subscription = new Subscription(), _this.nativeRange = null, _this.isSafari = isSafari(), _this.isMac = isMac(), _this.compositionEndedAt = 0;
2967
3203
  _this.documentView = textbus.get(VIEW_DOCUMENT);
2968
3204
  if (!controller.readonly) {
2969
3205
  _this.documentView.contentEditable = 'true';
@@ -3111,27 +3347,15 @@ var NativeInput = /*#__PURE__*/ function(Input) {
3111
3347
  key: "handleShortcut",
3112
3348
  value: function handleShortcut(input) {
3113
3349
  var _this = this;
3114
- var isWriting = false;
3115
- var isIgnore = false;
3116
- this.subscription.add(fromEvent(input, 'compositionstart').subscribe(function() {
3117
- isWriting = true;
3118
- }), fromEvent(input, 'compositionend').subscribe(function() {
3119
- isWriting = false;
3120
- }), fromEvent(input, 'beforeinput').subscribe(function(ev) {
3121
- if (_this.isSafari) {
3122
- if (ev.inputType === 'insertFromComposition') {
3123
- isIgnore = true;
3124
- }
3125
- }
3126
- }), fromEvent(input, 'keydown').pipe(filter(function() {
3127
- if (_this.isSafari && isIgnore) {
3128
- isIgnore = false;
3350
+ this.subscription.add(fromEvent(input, 'keydown').pipe(filter(function() {
3351
+ // Safari: IME 确认键(Enter)会在 compositionend 后紧接着触发 keydown
3352
+ // 用时间戳窗口检测并忽略
3353
+ if (_this.isSafari && _this.compositionEndedAt > 0 && Date.now() - _this.compositionEndedAt < 500) {
3354
+ _this.compositionEndedAt = 0;
3129
3355
  return false;
3130
3356
  }
3131
- return !isWriting // || !this.textarea.value
3132
- ;
3357
+ return !_this.composition;
3133
3358
  })).subscribe(function(ev) {
3134
- _this.ignoreComposition = false;
3135
3359
  var key = ev.key;
3136
3360
  var keys = ')!@#$%^Z&*(';
3137
3361
  var b = key === 'Process' && /Digit\d/.test(ev.code) && ev.shiftKey;
@@ -3151,7 +3375,6 @@ var NativeInput = /*#__PURE__*/ function(Input) {
3151
3375
  }
3152
3376
  });
3153
3377
  if (is) {
3154
- _this.ignoreComposition = true;
3155
3378
  ev.preventDefault();
3156
3379
  }
3157
3380
  }));
@@ -3160,101 +3383,70 @@ var NativeInput = /*#__PURE__*/ function(Input) {
3160
3383
  {
3161
3384
  key: "handleInput",
3162
3385
  value: function handleInput(input) {
3163
- if (this.isMobileBrowser) {
3164
- this.handleMobileInput(input);
3165
- } else {
3166
- this.handlePCInput(input);
3167
- }
3168
- }
3169
- },
3170
- {
3171
- key: "handleMobileInput",
3172
- value: function handleMobileInput(input) {
3173
3386
  var _this = this;
3174
- var isCompositionStart = true;
3175
- var startIndex;
3176
- var compositionStart = function compositionStart() {
3387
+ var recorder = new CompositionRecorder();
3388
+ var startIndex = 0;
3389
+ this.subscription.add(// ===== Composition 事件:纯开关,不读 ev.data 做模型决策 =====
3390
+ fromEvent(input, 'compositionstart').subscribe(function() {
3177
3391
  _this.composition = true;
3392
+ recorder.start(input);
3178
3393
  startIndex = _this.selection.startOffset;
3179
3394
  var startSlot = _this.selection.startSlot;
3180
3395
  var event = new Event(startSlot, {
3181
3396
  index: startIndex
3182
3397
  });
3183
3398
  invokeListener(startSlot.parent, 'onCompositionStart', event);
3184
- };
3185
- var compositionUpdate = function compositionUpdate(data) {
3399
+ }), fromEvent(input, 'compositionupdate').subscribe(function(ev) {
3186
3400
  var startSlot = _this.selection.startSlot;
3187
- var event = new Event(startSlot, {
3188
- index: startIndex,
3189
- data: data
3190
- });
3191
- invokeListener(startSlot.parent, 'onCompositionUpdate', event);
3192
- };
3193
- var compositionEnd = function compositionEnd(data) {
3401
+ if (startSlot) {
3402
+ invokeListener(startSlot.parent, 'onCompositionUpdate', new Event(startSlot, {
3403
+ index: startIndex,
3404
+ data: ev.data
3405
+ }));
3406
+ }
3407
+ }), fromEvent(input, 'compositionend').subscribe(function() {
3408
+ if (!_this.composition) return;
3194
3409
  _this.composition = false;
3195
- if (data) {
3196
- _this.commander.write(data);
3410
+ _this.compositionEndedAt = Date.now();
3411
+ // Safari: WebKit 在 compositionend 之后才更新 DOM,通过 microtask 延迟读取
3412
+ if (_this.isSafari) {
3413
+ queueMicrotask(function() {
3414
+ return _this.syncCompositionText(recorder);
3415
+ });
3416
+ } else {
3417
+ _this.syncCompositionText(recorder);
3197
3418
  }
3198
- var startSlot = _this.selection.startSlot;
3199
- if (startSlot) {
3200
- var event = new Event(startSlot, null);
3201
- invokeListener(startSlot.parent, 'onCompositionEnd', event);
3419
+ }), // ===== beforeinput:仅处理非 IME 输入 =====
3420
+ fromEvent(input, 'beforeinput').subscribe(function(ev) {
3421
+ if (ev.isComposing || _this.composition) {
3422
+ return;
3202
3423
  }
3203
- };
3204
- this.subscription.add(fromEvent(input, 'compositionstart').subscribe(function() {
3205
- compositionStart();
3206
- }), fromEvent(input, 'compositionupdate').subscribe(function(ev) {
3207
- compositionUpdate(ev.data);
3208
- }), fromEvent(input, 'compositionend').subscribe(function(ev) {
3209
- compositionEnd(ev.data);
3210
- var startContainer = _this.nativeSelection.focusNode;
3211
- if (_instanceof$1(startContainer, Text) && startContainer.textContent === ev.data) {
3212
- startContainer.remove();
3213
- }
3214
- }), fromEvent(input, 'beforeinput').subscribe(function(ev) {
3424
+ ev.preventDefault();
3215
3425
  switch(ev.inputType){
3216
3426
  case 'insertText':
3217
3427
  if (ev.data) {
3218
3428
  _this.commander.write(ev.data);
3219
- ev.preventDefault();
3220
3429
  }
3221
3430
  break;
3222
- case 'insertCompositionText':
3223
- if (isCompositionStart) {
3224
- isCompositionStart = false;
3225
- compositionStart();
3226
- } else {
3227
- compositionUpdate(ev.data || '');
3228
- }
3431
+ case 'deleteContentBackward':
3432
+ _this.commander.delete(true);
3229
3433
  break;
3230
- case 'deleteCompositionText':
3231
- _this.composition = false;
3434
+ case 'deleteContentForward':
3435
+ _this.commander.delete();
3232
3436
  break;
3233
- case 'deleteContentBackward':
3234
- {
3235
- _this.composition = false;
3236
- var range = ev.getTargetRanges()[0];
3237
- if (!range) {
3238
- break;
3239
- }
3240
- var location = _this.domAdapter.getLocationByNativeNode(range.startContainer);
3241
- var startSlot = _this.selection.startSlot;
3242
- if (startSlot) {
3243
- _this.selection.setBaseAndExtent(startSlot, location.startIndex + range.startOffset, startSlot, location.startIndex + range.endOffset);
3244
- _this.commander.delete();
3245
- }
3246
- break;
3247
- }
3248
3437
  case 'insertReplacementText':
3249
3438
  {
3250
3439
  var _ev_dataTransfer;
3251
- _this.composition = false;
3252
- var range1 = ev.getTargetRanges()[0];
3253
- var location1 = _this.domAdapter.getLocationByNativeNode(range1.startContainer);
3254
- var startSlot1 = _this.selection.startSlot;
3255
- _this.selection.setBaseAndExtent(startSlot1, location1.startIndex + range1.startOffset, startSlot1, location1.startIndex + range1.endOffset);
3256
- _this.commander.delete();
3257
- var text = ((_ev_dataTransfer = ev.dataTransfer) === null || _ev_dataTransfer === void 0 ? void 0 : _ev_dataTransfer.getData('text')) || ev.data || null;
3440
+ var range = ev.getTargetRanges()[0];
3441
+ if (range) {
3442
+ var location = _this.domAdapter.getLocationByNativeNode(range.startContainer);
3443
+ if (location) {
3444
+ var startSlot = _this.selection.startSlot;
3445
+ _this.selection.setBaseAndExtent(startSlot, location.startIndex + range.startOffset, startSlot, location.startIndex + range.endOffset);
3446
+ _this.commander.delete();
3447
+ }
3448
+ }
3449
+ var text = ((_ev_dataTransfer = ev.dataTransfer) === null || _ev_dataTransfer === void 0 ? void 0 : _ev_dataTransfer.getData('text')) || ev.data;
3258
3450
  if (text) {
3259
3451
  _this.commander.write(text);
3260
3452
  }
@@ -3265,89 +3457,19 @@ var NativeInput = /*#__PURE__*/ function(Input) {
3265
3457
  }
3266
3458
  },
3267
3459
  {
3268
- key: "handlePCInput",
3269
- value: function handlePCInput(input) {
3270
- var _this = this;
3271
- var startIndex = 0;
3272
- var isCompositionEnd = false;
3273
- this.subscription.add(fromEvent(input, 'compositionstart').pipe(filter(function() {
3274
- return !_this.ignoreComposition;
3275
- })).subscribe(function() {
3276
- _this.composition = true;
3277
- startIndex = _this.selection.startOffset;
3278
- var startSlot = _this.selection.startSlot;
3279
- var event = new Event(startSlot, {
3280
- index: startIndex
3281
- });
3282
- invokeListener(startSlot.parent, 'onCompositionStart', event);
3283
- }), fromEvent(input, 'compositionupdate').pipe(filter(function() {
3284
- return !_this.ignoreComposition;
3285
- })).subscribe(function(ev) {
3286
- var startSlot = _this.selection.startSlot;
3287
- var event = new Event(startSlot, {
3288
- index: startIndex,
3289
- data: ev.data
3290
- });
3291
- invokeListener(startSlot.parent, 'onCompositionUpdate', event);
3292
- }), merge(fromEvent(input, 'beforeinput').pipe(map(function(ev) {
3293
- ev.preventDefault();
3294
- if (ev.inputType === 'insertCompositionText') {
3295
- return null;
3296
- }
3297
- if (ev.inputType === 'insertReplacementText') {
3298
- var _ev_dataTransfer;
3299
- var range = ev.getTargetRanges()[0];
3300
- var location = _this.domAdapter.getLocationByNativeNode(range.startContainer);
3301
- var startSlot = _this.selection.startSlot;
3302
- _this.selection.setBaseAndExtent(startSlot, location.startIndex + range.startOffset, startSlot, location.startIndex + range.endOffset);
3303
- _this.commander.delete();
3304
- return ((_ev_dataTransfer = ev.dataTransfer) === null || _ev_dataTransfer === void 0 ? void 0 : _ev_dataTransfer.getData('text')) || ev.data || null;
3305
- }
3306
- isCompositionEnd = ev.inputType === 'insertFromComposition';
3307
- if (isCompositionEnd && _this.composition) {
3308
- return null;
3309
- }
3310
- if (_this.isSafari) {
3311
- if (ev.inputType === 'insertText' || isCompositionEnd) {
3312
- return ev.data;
3313
- }
3314
- }
3315
- if (!ev.isComposing && !!ev.data) {
3316
- return ev.data;
3317
- }
3318
- return null;
3319
- }), filter(function(text) {
3320
- return text;
3321
- })), this.isSafari ? new Observable() : fromEvent(input, 'compositionend').pipe(filter(function() {
3322
- return !_this.ignoreComposition;
3323
- })).pipe(filter(function() {
3324
- return _this.composition;
3325
- }), map(function(ev) {
3326
- isCompositionEnd = true;
3327
- ev.preventDefault();
3328
- return ev.data;
3329
- }), filter(function() {
3330
- var b = _this.ignoreComposition;
3331
- _this.ignoreComposition = false;
3332
- return !b;
3333
- }))).subscribe(function(text) {
3334
- _this.composition = false;
3335
- if (text) {
3336
- var startContainer = _this.nativeSelection.focusNode;
3337
- if (_instanceof$1(startContainer, Text) && startContainer.textContent === text) {
3338
- startContainer.remove();
3339
- }
3340
- _this.commander.write(text);
3341
- }
3342
- if (isCompositionEnd) {
3343
- var startSlot = _this.selection.startSlot;
3344
- if (startSlot) {
3345
- var event = new Event(startSlot, null);
3346
- invokeListener(startSlot.parent, 'onCompositionEnd', event);
3347
- }
3348
- }
3349
- isCompositionEnd = false;
3350
- }));
3460
+ key: "syncCompositionText",
3461
+ value: function syncCompositionText(recorder) {
3462
+ var text = recorder.readText();
3463
+ recorder.stop();
3464
+ if (text) {
3465
+ // 先清理 composition 期间浏览器创建的节点,确保写入模型时 DOM 是干净的
3466
+ recorder.cleanup(text, this.nativeSelection.focusNode);
3467
+ this.commander.write(text);
3468
+ }
3469
+ var startSlot = this.selection.startSlot;
3470
+ if (startSlot) {
3471
+ invokeListener(startSlot.parent, 'onCompositionEnd', new Event(startSlot, null));
3472
+ }
3351
3473
  }
3352
3474
  }
3353
3475
  ]);