@leankylin-sheet/react 3.1.43 → 3.1.45

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: {
@@ -630,6 +681,8 @@ var RowHeader = function RowHeader() {
630
681
  var ContentEditable = function ContentEditable(_ref) {
631
682
  var props = _extends({}, (_objectDestructuringEmpty(_ref), _ref));
632
683
  var lastHtml = useRef("");
684
+ var _useContext = useContext(WorkbookContext),
685
+ settings = _useContext.settings;
633
686
  var root = useRef(null);
634
687
  var autoFocus = props.autoFocus,
635
688
  initialContent = props.initialContent,
@@ -667,7 +720,9 @@ var ContentEditable = function ContentEditable(_ref) {
667
720
  return e.stopPropagation();
668
721
  },
669
722
  onClick: function onClick(e) {
670
- return e.stopPropagation();
723
+ var _settings$onClickCont;
724
+ e.stopPropagation();
725
+ (_settings$onClickCont = settings.onClickContentEditable) === null || _settings$onClickCont === void 0 ? void 0 : _settings$onClickCont.call(settings, e);
671
726
  }
672
727
  }, _.omit(props, "innerRef", "onChange", "html", "onBlur", "autoFocus", "allowEdit", "initialContent")), {}, {
673
728
  ref: function ref(e) {
@@ -684,39 +739,277 @@ var ContentEditable = function ContentEditable(_ref) {
684
739
  }));
685
740
  };
686
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
+
687
914
  var FormulaSearch = function FormulaSearch(props) {
688
915
  var _useContext = useContext(WorkbookContext),
689
916
  context = _useContext.context,
690
917
  refs = _useContext.refs,
691
918
  settings = _useContext.settings;
919
+ var _useState = useState(""),
920
+ _useState2 = _slicedToArray(_useState, 2),
921
+ n = _useState2[0],
922
+ setN = _useState2[1];
923
+ var nRef = useRef(n);
924
+ nRef.current = n;
925
+ useEffect(function () {
926
+ if (context.functionCandidates.length) {
927
+ setN(context.functionCandidates[0].n);
928
+ }
929
+ }, [context.functionCandidates]);
930
+ var fListRef = useRef([]);
931
+ fListRef.current = context.functionCandidates;
932
+ var inputN = function inputN(fnName) {
933
+ var _refs$cellInput$curre, _editor$textContent;
934
+ var offsetIndex = getPlainTextCursorOffset(refs.cellInput.current);
935
+ var text = ((_refs$cellInput$curre = refs.cellInput.current) === null || _refs$cellInput$curre === void 0 ? void 0 : _refs$cellInput$curre.innerText) || "";
936
+ var editor = getrangeseleciton();
937
+ var replaceText = (_editor$textContent = editor.textContent) === null || _editor$textContent === void 0 ? void 0 : _editor$textContent.trim();
938
+ var replaceTextLength = (replaceText === null || replaceText === void 0 ? void 0 : replaceText.length) || 0;
939
+ 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));
940
+ refs.cellInput.current.innerHTML = functionHTMLGenerate(newText);
941
+ moveCursorToPosition(refs.cellInput.current, offsetIndex - replaceTextLength + fnName.length);
942
+ };
943
+ useEffect(function () {
944
+ var _refs$cellInput$curre2;
945
+ var onChange = function onChange(e) {
946
+ var _fListRef$current;
947
+ if (((_fListRef$current = fListRef.current) === null || _fListRef$current === void 0 ? void 0 : _fListRef$current.length) <= 0) {
948
+ return;
949
+ }
950
+ if (e.key === "ArrowDown") {
951
+ e.preventDefault();
952
+ setN(function (_n) {
953
+ var _fListRef$current2, _fListRef$current3;
954
+ var currentIndex = Math.max((_fListRef$current2 = fListRef.current) === null || _fListRef$current2 === void 0 ? void 0 : _fListRef$current2.findIndex(function (item) {
955
+ return item.n === _n;
956
+ }), 0);
957
+ return (_fListRef$current3 = fListRef.current[(currentIndex + 1) % fListRef.current.length]) === null || _fListRef$current3 === void 0 ? void 0 : _fListRef$current3.n;
958
+ });
959
+ } else if (e.key === "ArrowUp") {
960
+ e.preventDefault();
961
+ setN(function (_n) {
962
+ var _fListRef$current4, _fListRef$current5;
963
+ var currentIndex = Math.max((_fListRef$current4 = fListRef.current) === null || _fListRef$current4 === void 0 ? void 0 : _fListRef$current4.findIndex(function (item) {
964
+ return item.n === _n;
965
+ }), 0);
966
+ return (_fListRef$current5 = fListRef.current[(currentIndex - 1) % fListRef.current.length]) === null || _fListRef$current5 === void 0 ? void 0 : _fListRef$current5.n;
967
+ });
968
+ } else if (e.key === "Enter") {
969
+ e.preventDefault();
970
+ e.stopPropagation();
971
+ if (nRef.current) {
972
+ inputN("".concat(nRef.current, "("));
973
+ }
974
+ }
975
+ };
976
+ (_refs$cellInput$curre2 = refs.cellInput.current) === null || _refs$cellInput$curre2 === void 0 ? void 0 : _refs$cellInput$curre2.addEventListener("keydown", onChange);
977
+ return function () {
978
+ var _refs$cellInput$curre3;
979
+ (_refs$cellInput$curre3 = refs.cellInput.current) === null || _refs$cellInput$curre3 === void 0 ? void 0 : _refs$cellInput$curre3.removeEventListener("keydown", onChange);
980
+ };
981
+ }, [refs.cellInput.current]);
692
982
  if (_.isEmpty(context.functionCandidates)) return null;
693
- function moveCursorToEnd(editor) {
694
- var leftParen = editor.querySelector(".luckysheet-formula-text-lpar");
695
- var rightParen = editor.querySelector(".luckysheet-formula-text-rpar");
696
- var range = document.createRange();
697
- var selection = window.getSelection();
698
- range.setStartAfter(leftParen);
699
- range.setEndBefore(rightParen);
700
- selection.removeAllRanges();
701
- selection.addRange(range);
702
- }
703
983
  if (settings.renderFormulaSearch) {
704
984
  return settings.renderFormulaSearch(context.functionCandidates, functionHTMLGenerate, refs.cellInput.current);
705
985
  }
706
986
  return /*#__PURE__*/React.createElement("div", _objectSpread2(_objectSpread2({}, props), {}, {
707
987
  id: "luckysheet-formula-search-c",
708
- className: "luckysheet-formula-search-c"
988
+ className: "luckysheet-formula-search-c",
989
+ onWheel: function onWheel(e) {
990
+ e.preventDefault();
991
+ e.stopPropagation();
992
+ },
993
+ onWheelCapture: function onWheelCapture(e) {
994
+ e.preventDefault();
995
+ e.stopPropagation();
996
+ }
709
997
  }), context.functionCandidates.map(function (v) {
710
998
  return /*#__PURE__*/React.createElement("div", {
711
- onClick: function onClick() {
712
- var _refs$cellInput$curre;
713
- refs.cellInput.current.innerHTML = functionHTMLGenerate("=".concat(v.n, "()"));
714
- (_refs$cellInput$curre = refs.cellInput.current) === null || _refs$cellInput$curre === void 0 ? void 0 : _refs$cellInput$curre.focus();
715
- moveCursorToEnd(refs.cellInput.current);
999
+ onClick: function onClick(e) {
1000
+ e.preventDefault();
1001
+ e.stopPropagation();
1002
+ inputN("".concat(v.n, "("));
1003
+ },
1004
+ onMouseEnter: function onMouseEnter() {
1005
+ setN(v.n);
1006
+ },
1007
+ onMouseDown: function onMouseDown(e) {
1008
+ return e.preventDefault();
716
1009
  },
717
1010
  key: v.n,
718
1011
  "data-func": v.n,
719
- className: "luckysheet-formula-search-item"
1012
+ className: "luckysheet-formula-search-item ".concat(n === v.n && "luckysheet-formula-search-item-active")
720
1013
  }, /*#__PURE__*/React.createElement("div", {
721
1014
  className: "luckysheet-formula-search-func"
722
1015
  }, v.n), /*#__PURE__*/React.createElement("div", {
@@ -1010,7 +1303,7 @@ var InputBox = function InputBox() {
1010
1303
  onKeyDown: onKeyDown,
1011
1304
  onPaste: onPaste,
1012
1305
  allowEdit: edit ? !isHidenRC : edit
1013
- })), 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, {
1306
+ })), 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, {
1014
1307
  style: {
1015
1308
  top: ((firstSelection === null || firstSelection === void 0 ? void 0 : firstSelection.height_move) || 0) + 4
1016
1309
  }
@@ -5528,8 +5821,10 @@ var Toolbar = function Toolbar(_ref) {
5528
5821
  if (!container) return;
5529
5822
  var moreButtonWidth = 50;
5530
5823
  for (var i = itemLocations.length - 1; i >= 0; i -= 1) {
5824
+ var _container$querySelec;
5531
5825
  var loc = itemLocations[i];
5532
- if (loc + moreButtonWidth < container.clientWidth) {
5826
+ var wrapperWidth = container.clientWidth - (((_container$querySelec = container.querySelector(".leankylin-toolbar-right")) === null || _container$querySelec === void 0 ? void 0 : _container$querySelec.clientWidth) || 0);
5827
+ if (loc + moreButtonWidth < wrapperWidth) {
5533
5828
  setToolbarWrapIndex(i - itemLocations.length + settings.toolbarItems.length);
5534
5829
  if (i === itemLocations.length - 1) {
5535
5830
  setMoreItems(null);
@@ -6547,7 +6842,9 @@ var Toolbar = function Toolbar(_ref) {
6547
6842
  }));
6548
6843
  }
6549
6844
  }
6550
- })) : null, (_settings$toolbarRigh = settings.toolbarRightRender) === null || _settings$toolbarRigh === void 0 ? void 0 : _settings$toolbarRigh.call(settings));
6845
+ })) : null, settings.toolbarRightRender ? ( /*#__PURE__*/React.createElement("div", {
6846
+ className: "leankylin-toolbar-right"
6847
+ }, (_settings$toolbarRigh = settings.toolbarRightRender) === null || _settings$toolbarRigh === void 0 ? void 0 : _settings$toolbarRigh.call(settings))) : null);
6551
6848
  };
6552
6849
 
6553
6850
  var LocationBox = function LocationBox() {