@leankylin-sheet/react 3.1.44 → 3.1.46

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.
@@ -0,0 +1,3 @@
1
+ export declare function getPlainTextCursorOffset(editableDiv: any): number;
2
+ export declare function getrangeseleciton(): ParentNode | ChildNode | null | undefined;
3
+ export declare function moveCursorToPosition(editableDiv: any, targetPosition: any): void;
package/dist/index.css CHANGED
@@ -1036,6 +1036,8 @@ html::-webkit-scrollbar-button {
1036
1036
  background: #fff;
1037
1037
  z-index: 1003;
1038
1038
  width: 300px;
1039
+ max-height: 300px;
1040
+ overflow: auto;
1039
1041
  }
1040
1042
 
1041
1043
  .luckysheet-formula-search-c .luckysheet-formula-search-item {
@@ -1044,7 +1046,7 @@ html::-webkit-scrollbar-button {
1044
1046
  cursor: pointer;
1045
1047
  }
1046
1048
  .luckysheet-formula-search-item:hover {
1047
- background-color: #f1f1f1 !important;
1049
+ /* background-color: #f1f1f1 !important; */
1048
1050
  }
1049
1051
 
1050
1052
  .luckysheet-formula-search-c
@@ -1036,6 +1036,8 @@ html::-webkit-scrollbar-button {
1036
1036
  background: #fff;
1037
1037
  z-index: 1003;
1038
1038
  width: 300px;
1039
+ max-height: 300px;
1040
+ overflow: auto;
1039
1041
  }
1040
1042
 
1041
1043
  .luckysheet-formula-search-c .luckysheet-formula-search-item {
@@ -1044,7 +1046,7 @@ html::-webkit-scrollbar-button {
1044
1046
  cursor: pointer;
1045
1047
  }
1046
1048
  .luckysheet-formula-search-item:hover {
1047
- background-color: #f1f1f1 !important;
1049
+ /* background-color: #f1f1f1 !important; */
1048
1050
  }
1049
1051
 
1050
1052
  .luckysheet-formula-search-c
package/dist/index.esm.js CHANGED
@@ -159,6 +159,57 @@ function _nonIterableSpread() {
159
159
  function _nonIterableRest() {
160
160
  throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
161
161
  }
162
+ function _createForOfIteratorHelper(o, allowArrayLike) {
163
+ var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
164
+ if (!it) {
165
+ if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
166
+ if (it) o = it;
167
+ var i = 0;
168
+ var F = function () {};
169
+ return {
170
+ s: F,
171
+ n: function () {
172
+ if (i >= o.length) return {
173
+ done: true
174
+ };
175
+ return {
176
+ done: false,
177
+ value: o[i++]
178
+ };
179
+ },
180
+ e: function (e) {
181
+ throw e;
182
+ },
183
+ f: F
184
+ };
185
+ }
186
+ throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
187
+ }
188
+ var normalCompletion = true,
189
+ didErr = false,
190
+ err;
191
+ return {
192
+ s: function () {
193
+ it = it.call(o);
194
+ },
195
+ n: function () {
196
+ var step = it.next();
197
+ normalCompletion = step.done;
198
+ return step;
199
+ },
200
+ e: function (e) {
201
+ didErr = true;
202
+ err = e;
203
+ },
204
+ f: function () {
205
+ try {
206
+ if (!normalCompletion && it.return != null) it.return();
207
+ } finally {
208
+ if (didErr) throw err;
209
+ }
210
+ }
211
+ };
212
+ }
162
213
 
163
214
  var defaultRefs = {
164
215
  globalCache: {
@@ -688,39 +739,295 @@ var ContentEditable = function ContentEditable(_ref) {
688
739
  }));
689
740
  };
690
741
 
742
+ function getLastTextNode(element) {
743
+ var lastText = null;
744
+ function traverse(node) {
745
+ if (node.nodeType === Node.TEXT_NODE) {
746
+ lastText = node;
747
+ } else if (node.nodeType === Node.ELEMENT_NODE) {
748
+ for (var i = node.childNodes.length - 1; i >= 0; i -= 1) {
749
+ traverse(node.childNodes[i]);
750
+ if (lastText) break;
751
+ }
752
+ }
753
+ }
754
+ traverse(element);
755
+ return lastText;
756
+ }
757
+ function getPlainTextCursorOffset(editableDiv) {
758
+ var selection = window.getSelection();
759
+ if (!selection || selection.rangeCount === 0) return -1;
760
+ var range = selection.getRangeAt(0);
761
+ if (!editableDiv.contains(range.commonAncestorContainer)) {
762
+ return -1;
763
+ }
764
+ var textNodes = [];
765
+ function collectTextNodes(node) {
766
+ if (node.nodeType === Node.TEXT_NODE) {
767
+ textNodes.push(node);
768
+ } else if (node.nodeType === Node.ELEMENT_NODE) {
769
+ for (var i = 0; i < node.childNodes.length; i += 1) {
770
+ collectTextNodes(node.childNodes[i]);
771
+ }
772
+ }
773
+ }
774
+ collectTextNodes(editableDiv);
775
+ var cursorTextNode = null;
776
+ var offsetInNode = 0;
777
+ var startContainer = range.startContainer;
778
+ var startOffset = range.startOffset;
779
+ if (startContainer.nodeType === Node.TEXT_NODE) {
780
+ cursorTextNode = startContainer;
781
+ offsetInNode = startOffset;
782
+ } else if (startContainer.nodeType === Node.ELEMENT_NODE) {
783
+ var childNodes = startContainer.childNodes;
784
+ var prevTextNode = null;
785
+ for (var i = 0; i < startOffset; i += 1) {
786
+ var child = childNodes[i];
787
+ if (child.nodeType === Node.TEXT_NODE) {
788
+ prevTextNode = child;
789
+ } else if (child.nodeType === Node.ELEMENT_NODE) {
790
+ var lastText = getLastTextNode(child);
791
+ if (lastText) prevTextNode = lastText;
792
+ }
793
+ }
794
+ if (prevTextNode) {
795
+ var _prevTextNode$textCon;
796
+ cursorTextNode = prevTextNode;
797
+ offsetInNode = ((_prevTextNode$textCon = prevTextNode.textContent) === null || _prevTextNode$textCon === void 0 ? void 0 : _prevTextNode$textCon.length) || 0;
798
+ } else {
799
+ return 0;
800
+ }
801
+ }
802
+ var totalOffset = 0;
803
+ for (var _i = 0, _textNodes = textNodes; _i < _textNodes.length; _i++) {
804
+ var node = _textNodes[_i];
805
+ if (node === cursorTextNode) {
806
+ totalOffset += offsetInNode;
807
+ break;
808
+ } else {
809
+ var _node$textContent;
810
+ totalOffset += ((_node$textContent = node.textContent) === null || _node$textContent === void 0 ? void 0 : _node$textContent.length) || 0;
811
+ }
812
+ }
813
+ return totalOffset;
814
+ }
815
+ function getrangeseleciton() {
816
+ var _anchorNode$parentNod, _anchorNode$parentNod2, _anchorNode$parentEle, _anchorNode$parentEle2;
817
+ var currSelection = window.getSelection();
818
+ if (!currSelection) return null;
819
+ var anchorNode = currSelection.anchorNode,
820
+ anchorOffset = currSelection.anchorOffset;
821
+ if (!anchorNode) return null;
822
+ if (((_anchorNode$parentNod = anchorNode.parentNode) === null || _anchorNode$parentNod === void 0 ? void 0 : (_anchorNode$parentNod2 = _anchorNode$parentNod.nodeName) === null || _anchorNode$parentNod2 === void 0 ? void 0 : _anchorNode$parentNod2.toLowerCase()) === "span" && anchorOffset !== 0) {
823
+ var txt = _.trim(anchorNode.textContent || "");
824
+ if (txt.length === 0 && anchorNode.parentNode.previousSibling) {
825
+ var ahr = anchorNode.parentNode.previousSibling;
826
+ txt = _.trim(ahr.textContent || "");
827
+ return ahr;
828
+ }
829
+ return anchorNode.parentNode;
830
+ }
831
+ var anchorElement = anchorNode;
832
+ if (anchorElement.id === "luckysheet-rich-text-editor" || anchorElement.id === "luckysheet-functionbox-cell") {
833
+ var _$last;
834
+ var _txt = _.trim((_$last = _.last(anchorElement.querySelectorAll("span"))) === null || _$last === void 0 ? void 0 : _$last.innerText);
835
+ if (_txt.length === 0 && anchorElement.querySelectorAll("span").length > 1) {
836
+ var _ahr = anchorElement.querySelectorAll("span");
837
+ _txt = _.trim(_ahr[_ahr.length - 2].innerText);
838
+ return _ahr === null || _ahr === void 0 ? void 0 : _ahr[0];
839
+ }
840
+ return _.last(anchorElement.querySelectorAll("span"));
841
+ }
842
+ if ((anchorNode === null || anchorNode === void 0 ? void 0 : (_anchorNode$parentEle = anchorNode.parentElement) === null || _anchorNode$parentEle === void 0 ? void 0 : _anchorNode$parentEle.id) === "luckysheet-rich-text-editor" || (anchorNode === null || anchorNode === void 0 ? void 0 : (_anchorNode$parentEle2 = anchorNode.parentElement) === null || _anchorNode$parentEle2 === void 0 ? void 0 : _anchorNode$parentEle2.id) === "luckysheet-functionbox-cell" || anchorOffset === 0) {
843
+ var newAnchorNode = anchorOffset === 0 ? anchorNode === null || anchorNode === void 0 ? void 0 : anchorNode.parentNode : anchorNode;
844
+ if (newAnchorNode === null || newAnchorNode === void 0 ? void 0 : newAnchorNode.previousSibling) {
845
+ return newAnchorNode === null || newAnchorNode === void 0 ? void 0 : newAnchorNode.previousSibling;
846
+ }
847
+ }
848
+ return null;
849
+ }
850
+ function moveCursorToPosition(editableDiv, targetPosition) {
851
+ if (!editableDiv || editableDiv.contentEditable !== "true") {
852
+ console.warn('目标元素必须是可编辑的div(contenteditable="true")');
853
+ return;
854
+ }
855
+ if (targetPosition < 0) {
856
+ targetPosition = 0;
857
+ }
858
+ var textNodesInfo = [];
859
+ var totalLength = 0;
860
+ function traverseNodes(node) {
861
+ if (!node) return;
862
+ if (node.nodeType === Node.TEXT_NODE) {
863
+ var textLength = node.textContent.length;
864
+ totalLength += textLength;
865
+ textNodesInfo.push({
866
+ node: node,
867
+ cumulativeLength: totalLength
868
+ });
869
+ } else if (node.nodeType === Node.ELEMENT_NODE) {
870
+ Array.from(node.childNodes).forEach(function (child) {
871
+ return traverseNodes(child);
872
+ });
873
+ }
874
+ }
875
+ traverseNodes(editableDiv);
876
+ var targetNode = null;
877
+ var offsetInNode = 0;
878
+ if (totalLength <= targetPosition) {
879
+ if (textNodesInfo.length > 0) {
880
+ var lastNodeInfo = textNodesInfo[textNodesInfo.length - 1];
881
+ targetNode = lastNodeInfo.node;
882
+ offsetInNode = lastNodeInfo.node.textContent.length;
883
+ }
884
+ } else {
885
+ var prevCumulativeLength = 0;
886
+ var _iterator = _createForOfIteratorHelper(textNodesInfo),
887
+ _step;
888
+ try {
889
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
890
+ var info = _step.value;
891
+ if (info.cumulativeLength > targetPosition) {
892
+ targetNode = info.node;
893
+ offsetInNode = targetPosition - prevCumulativeLength;
894
+ break;
895
+ }
896
+ prevCumulativeLength = info.cumulativeLength;
897
+ }
898
+ } catch (err) {
899
+ _iterator.e(err);
900
+ } finally {
901
+ _iterator.f();
902
+ }
903
+ }
904
+ if (targetNode) {
905
+ var selection = window.getSelection();
906
+ selection.removeAllRanges();
907
+ var range = document.createRange();
908
+ range.setStart(targetNode, offsetInNode);
909
+ range.setEnd(targetNode, offsetInNode);
910
+ selection.addRange(range);
911
+ }
912
+ }
913
+
691
914
  var FormulaSearch = function FormulaSearch(props) {
692
915
  var _useContext = useContext(WorkbookContext),
693
916
  context = _useContext.context,
694
917
  refs = _useContext.refs,
695
918
  settings = _useContext.settings;
919
+ var _useState = useState(""),
920
+ _useState2 = _slicedToArray(_useState, 2),
921
+ n = _useState2[0],
922
+ setN = _useState2[1];
923
+ var wrapperRf = useRef(null);
924
+ var nRef = useRef(n);
925
+ nRef.current = n;
926
+ useEffect(function () {
927
+ if (context.functionCandidates.length) {
928
+ setN(context.functionCandidates[0].n);
929
+ }
930
+ }, [context.functionCandidates]);
931
+ var fListRef = useRef([]);
932
+ fListRef.current = context.functionCandidates;
933
+ var inputN = function inputN(fnName) {
934
+ var _refs$cellInput$curre, _editor$textContent;
935
+ var offsetIndex = getPlainTextCursorOffset(refs.cellInput.current);
936
+ var text = ((_refs$cellInput$curre = refs.cellInput.current) === null || _refs$cellInput$curre === void 0 ? void 0 : _refs$cellInput$curre.innerText) || "";
937
+ var editor = getrangeseleciton();
938
+ var replaceText = (_editor$textContent = editor.textContent) === null || _editor$textContent === void 0 ? void 0 : _editor$textContent.trim();
939
+ var replaceTextLength = (replaceText === null || replaceText === void 0 ? void 0 : replaceText.length) || 0;
940
+ var newText = (text === null || text === void 0 ? void 0 : text.slice(0, offsetIndex - replaceTextLength)) + fnName + (text === null || text === void 0 ? void 0 : text.slice(offsetIndex));
941
+ refs.cellInput.current.innerHTML = functionHTMLGenerate(newText);
942
+ moveCursorToPosition(refs.cellInput.current, offsetIndex - replaceTextLength + fnName.length);
943
+ };
944
+ var viewActive = function viewActive() {
945
+ var _wrapperRf$current;
946
+ var aItem = (_wrapperRf$current = wrapperRf.current) === null || _wrapperRf$current === void 0 ? void 0 : _wrapperRf$current.querySelector(".luckysheet-formula-search-item-active");
947
+ if (aItem) {
948
+ aItem === null || aItem === void 0 ? void 0 : aItem.scrollIntoView({
949
+ block: "nearest",
950
+ behavior: "smooth"
951
+ });
952
+ }
953
+ };
954
+ useEffect(function () {
955
+ var _refs$cellInput$curre2;
956
+ var onChange = function onChange(e) {
957
+ var _fListRef$current;
958
+ if (((_fListRef$current = fListRef.current) === null || _fListRef$current === void 0 ? void 0 : _fListRef$current.length) <= 0) {
959
+ return;
960
+ }
961
+ if (e.key === "ArrowDown") {
962
+ e.preventDefault();
963
+ setN(function (_n) {
964
+ var _fListRef$current2, _fListRef$current3;
965
+ var currentIndex = Math.max((_fListRef$current2 = fListRef.current) === null || _fListRef$current2 === void 0 ? void 0 : _fListRef$current2.findIndex(function (item) {
966
+ return item.n === _n;
967
+ }), 0);
968
+ return (_fListRef$current3 = fListRef.current[(currentIndex + 1) % fListRef.current.length]) === null || _fListRef$current3 === void 0 ? void 0 : _fListRef$current3.n;
969
+ });
970
+ setTimeout(function () {
971
+ viewActive();
972
+ }, 200);
973
+ } else if (e.key === "ArrowUp") {
974
+ e.preventDefault();
975
+ setN(function (_n) {
976
+ var _fListRef$current4, _fListRef$current5;
977
+ var currentIndex = Math.max((_fListRef$current4 = fListRef.current) === null || _fListRef$current4 === void 0 ? void 0 : _fListRef$current4.findIndex(function (item) {
978
+ return item.n === _n;
979
+ }), 0);
980
+ return (_fListRef$current5 = fListRef.current[(currentIndex - 1) % fListRef.current.length]) === null || _fListRef$current5 === void 0 ? void 0 : _fListRef$current5.n;
981
+ });
982
+ setTimeout(function () {
983
+ viewActive();
984
+ }, 200);
985
+ } else if (e.key === "Enter") {
986
+ e.preventDefault();
987
+ e.stopPropagation();
988
+ if (nRef.current) {
989
+ inputN("".concat(nRef.current, "("));
990
+ }
991
+ }
992
+ };
993
+ (_refs$cellInput$curre2 = refs.cellInput.current) === null || _refs$cellInput$curre2 === void 0 ? void 0 : _refs$cellInput$curre2.addEventListener("keydown", onChange);
994
+ return function () {
995
+ var _refs$cellInput$curre3;
996
+ (_refs$cellInput$curre3 = refs.cellInput.current) === null || _refs$cellInput$curre3 === void 0 ? void 0 : _refs$cellInput$curre3.removeEventListener("keydown", onChange);
997
+ };
998
+ }, [refs.cellInput.current]);
696
999
  if (_.isEmpty(context.functionCandidates)) return null;
697
- function moveCursorToEnd(editor) {
698
- var leftParen = editor.querySelector(".luckysheet-formula-text-lpar");
699
- var rightParen = editor.querySelector(".luckysheet-formula-text-rpar");
700
- var range = document.createRange();
701
- var selection = window.getSelection();
702
- range.setStartAfter(leftParen);
703
- range.setEndBefore(rightParen);
704
- selection.removeAllRanges();
705
- selection.addRange(range);
706
- }
707
1000
  if (settings.renderFormulaSearch) {
708
1001
  return settings.renderFormulaSearch(context.functionCandidates, functionHTMLGenerate, refs.cellInput.current);
709
1002
  }
710
1003
  return /*#__PURE__*/React.createElement("div", _objectSpread2(_objectSpread2({}, props), {}, {
711
1004
  id: "luckysheet-formula-search-c",
712
- className: "luckysheet-formula-search-c"
1005
+ className: "luckysheet-formula-search-c",
1006
+ ref: wrapperRf,
1007
+ onWheel: function onWheel(e) {
1008
+ e.preventDefault();
1009
+ e.stopPropagation();
1010
+ },
1011
+ onWheelCapture: function onWheelCapture(e) {
1012
+ e.preventDefault();
1013
+ e.stopPropagation();
1014
+ }
713
1015
  }), context.functionCandidates.map(function (v) {
714
1016
  return /*#__PURE__*/React.createElement("div", {
715
- onClick: function onClick() {
716
- var _refs$cellInput$curre;
717
- refs.cellInput.current.innerHTML = functionHTMLGenerate("=".concat(v.n, "()"));
718
- (_refs$cellInput$curre = refs.cellInput.current) === null || _refs$cellInput$curre === void 0 ? void 0 : _refs$cellInput$curre.focus();
719
- moveCursorToEnd(refs.cellInput.current);
1017
+ onClick: function onClick(e) {
1018
+ e.preventDefault();
1019
+ e.stopPropagation();
1020
+ inputN("".concat(v.n, "("));
1021
+ },
1022
+ onMouseEnter: function onMouseEnter() {
1023
+ setN(v.n);
1024
+ },
1025
+ onMouseDown: function onMouseDown(e) {
1026
+ return e.preventDefault();
720
1027
  },
721
1028
  key: v.n,
722
1029
  "data-func": v.n,
723
- className: "luckysheet-formula-search-item"
1030
+ className: "luckysheet-formula-search-item ".concat(n === v.n && "luckysheet-formula-search-item-active")
724
1031
  }, /*#__PURE__*/React.createElement("div", {
725
1032
  className: "luckysheet-formula-search-func"
726
1033
  }, v.n), /*#__PURE__*/React.createElement("div", {
@@ -1014,7 +1321,7 @@ var InputBox = function InputBox() {
1014
1321
  onKeyDown: onKeyDown,
1015
1322
  onPaste: onPaste,
1016
1323
  allowEdit: edit ? !isHidenRC : edit
1017
- })), document.activeElement === inputRef.current && ( /*#__PURE__*/React.createElement(React.Fragment, null, (_settings$renderEdito = settings.renderEditorSelector) === null || _settings$renderEdito === void 0 ? void 0 : _settings$renderEdito.call(settings, inputRef.current), /*#__PURE__*/React.createElement(FormulaSearch, {
1324
+ })), document.activeElement === inputRef.current && ( /*#__PURE__*/React.createElement(React.Fragment, null, (_settings$renderEdito = settings.renderEditorSelector) === null || _settings$renderEdito === void 0 ? void 0 : _settings$renderEdito.call(settings, inputRef.current, context.functionCandidates, context.functionHint, context.formulaCache.functionlistMap), /*#__PURE__*/React.createElement(FormulaSearch, {
1018
1325
  style: {
1019
1326
  top: ((firstSelection === null || firstSelection === void 0 ? void 0 : firstSelection.height_move) || 0) + 4
1020
1327
  }
@@ -5532,8 +5839,10 @@ var Toolbar = function Toolbar(_ref) {
5532
5839
  if (!container) return;
5533
5840
  var moreButtonWidth = 50;
5534
5841
  for (var i = itemLocations.length - 1; i >= 0; i -= 1) {
5842
+ var _container$querySelec;
5535
5843
  var loc = itemLocations[i];
5536
- if (loc + moreButtonWidth < container.clientWidth) {
5844
+ var wrapperWidth = container.clientWidth - (((_container$querySelec = container.querySelector(".leankylin-toolbar-right")) === null || _container$querySelec === void 0 ? void 0 : _container$querySelec.clientWidth) || 0);
5845
+ if (loc + moreButtonWidth < wrapperWidth) {
5537
5846
  setToolbarWrapIndex(i - itemLocations.length + settings.toolbarItems.length);
5538
5847
  if (i === itemLocations.length - 1) {
5539
5848
  setMoreItems(null);
@@ -6551,7 +6860,9 @@ var Toolbar = function Toolbar(_ref) {
6551
6860
  }));
6552
6861
  }
6553
6862
  }
6554
- })) : null, (_settings$toolbarRigh = settings.toolbarRightRender) === null || _settings$toolbarRigh === void 0 ? void 0 : _settings$toolbarRigh.call(settings));
6863
+ })) : null, settings.toolbarRightRender ? ( /*#__PURE__*/React.createElement("div", {
6864
+ className: "leankylin-toolbar-right"
6865
+ }, (_settings$toolbarRigh = settings.toolbarRightRender) === null || _settings$toolbarRigh === void 0 ? void 0 : _settings$toolbarRigh.call(settings))) : null);
6555
6866
  };
6556
6867
 
6557
6868
  var LocationBox = function LocationBox() {