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