@zhenliang/sheet 0.2.5-beta.11 → 0.2.5-beta.13

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.
@@ -18,7 +18,7 @@ import { formulaString } from "../numberEditor";
18
18
  import DropMenu from "./DropMenu";
19
19
  import "./index.less";
20
20
  import { useKeyEventEffect } from "./keyEventEffect";
21
- import { createSpan, flattenOptions, getStringDiff, moveCursorToSpan, replaceLabelsWithValues, replaceLongestDiff, replaceValuesWithLabels, SpanType, tokenize, validateVariables } from "./utils";
21
+ import { createSpan, flattenOptions, getStringDiff, moveCursorToSpan, replaceLabelsWithValues, replaceValuesWithLabels, SpanType, tokenize, validateVariables } from "./utils";
22
22
  import validateCalculationExpr from "./vaildFormula";
23
23
  import { jsx as _jsx } from "react/jsx-runtime";
24
24
  var OPERATORS = ['=', '+', '-', '*', '/', '('];
@@ -43,6 +43,7 @@ export var getFormulaInputEditor = function getFormulaInputEditor(options, repla
43
43
  inputArgs = Object.assign({}, (_objectDestructuringEmpty(_ref2), _ref2));
44
44
  var replaceValue = replaceIndex ? (_get = get(record, [replaceIndex])) !== null && _get !== void 0 ? _get : '' : originValue;
45
45
  var calcValue = replaceValue.startsWith('=') ? replaceValue : "=".concat(replaceValue);
46
+ var originContainerValue = replaceValue === '' || replaceValue === null ? '' : calcValue;
46
47
  var _useState = useState(calcValue),
47
48
  _useState2 = _slicedToArray(_useState, 1),
48
49
  inputValue = _useState2[0];
@@ -51,7 +52,7 @@ export var getFormulaInputEditor = function getFormulaInputEditor(options, repla
51
52
  offsetX = _useState4[0],
52
53
  setOffsetX = _useState4[1];
53
54
  // 为了不重新render,再写一个
54
- var _useState5 = useState(replaceValue),
55
+ var _useState5 = useState(originContainerValue),
55
56
  _useState6 = _slicedToArray(_useState5, 2),
56
57
  containerValue = _useState6[0],
57
58
  setContainerValue = _useState6[1];
@@ -66,7 +67,6 @@ export var getFormulaInputEditor = function getFormulaInputEditor(options, repla
66
67
  setInputAfterDropDownShow = _useState10[1];
67
68
  var tempSpanRef = useRef(null);
68
69
  var containerRef = useRef(null);
69
- var inputOperatorPosition = useRef(0);
70
70
 
71
71
  // 扁平化所有选项
72
72
  var flatOptions = useMemo(function () {
@@ -123,12 +123,6 @@ export var getFormulaInputEditor = function getFormulaInputEditor(options, repla
123
123
  setOpen(false);
124
124
  return;
125
125
  }
126
- // 原逻辑:删除 inputAfterDropDownShow,插入新 label
127
- var newValueUser = replaceLongestDiff(containerValueSnapShot.current, opt.label, inputOperatorPosition.current);
128
- var expr = newValueUser;
129
- var valueExprUser = replaceLabelsWithValues(expr, flatOptions);
130
- setContainerValue(valueExprUser);
131
- onChange(valueExprUser);
132
126
  var range = selection.getRangeAt(0);
133
127
  var startContainer = range.startContainer;
134
128
  var currentSpan = startContainer.nodeType === Node.TEXT_NODE ? startContainer.parentElement : startContainer;
@@ -173,6 +167,11 @@ export var getFormulaInputEditor = function getFormulaInputEditor(options, repla
173
167
  targetSpan = createSpan(OTHER, '');
174
168
  labelSpan.after(targetSpan);
175
169
  }
170
+ var newValueUser = container.textContent || '';
171
+ var expr = newValueUser;
172
+ var valueExprUser = replaceLabelsWithValues(expr, flatOptions);
173
+ setContainerValue(valueExprUser);
174
+ onChange(valueExprUser);
176
175
  moveCursorToSpan(targetSpan, true, selection);
177
176
  setOpen(false);
178
177
  }, [inputAfterDropDownShow]);
@@ -188,14 +187,6 @@ export var getFormulaInputEditor = function getFormulaInputEditor(options, repla
188
187
  var processedValue = newValue;
189
188
  var expr = processedValue;
190
189
  var valueExpr = replaceLabelsWithValues(expr, flatOptions);
191
- var oldLabelText = replaceValuesWithLabels(containerValue, flatOptions);
192
- var firstDiffPosition = 0;
193
- for (var i = 0; i < newValue.length; i++) {
194
- if (newValue[i] !== oldLabelText[i]) {
195
- firstDiffPosition = i;
196
- break;
197
- }
198
- }
199
190
  var _getStringDiff = getStringDiff(containerValue, valueExpr),
200
191
  added = _getStringDiff.added,
201
192
  removed = _getStringDiff.removed;
@@ -214,7 +205,6 @@ export var getFormulaInputEditor = function getFormulaInputEditor(options, repla
214
205
  // 按下了运算符,下拉框刚打开,重置输入记录
215
206
  containerValueSnapShot.current = expr;
216
207
  inputStringAfterDropDownShow = '';
217
- inputOperatorPosition.current = firstDiffPosition;
218
208
 
219
209
  // 获取当前光标距离 containerRef 最左边的距离
220
210
  var selection = window.getSelection();
@@ -240,6 +230,7 @@ export var getFormulaInputEditor = function getFormulaInputEditor(options, repla
240
230
  closeDorpDown: closeDorpDown,
241
231
  setOffsetX: setOffsetX,
242
232
  setOpen: setOpen,
233
+ handleInput: handleInput,
243
234
  containerRef: containerRef,
244
235
  tempSpanRef: tempSpanRef,
245
236
  inputAfterDropDownShow: inputAfterDropDownShow,
@@ -11,6 +11,7 @@
11
11
  top: 0;
12
12
  background: #fff;
13
13
  padding-right: 8px;
14
+ z-index: 5;
14
15
  }
15
16
 
16
17
  .formula-editor-equal {
@@ -1,9 +1,10 @@
1
1
  import type { KeyboardEvent as ReactKeyboardEvent } from 'react';
2
2
  import { OptionItem } from './utils';
3
- declare const useKeyEventEffect: ({ closeDorpDown, setOffsetX, setOpen, containerRef, tempSpanRef, inputAfterDropDownShow, options }: {
3
+ declare const useKeyEventEffect: ({ closeDorpDown, setOffsetX, setOpen, handleInput, containerRef, tempSpanRef, inputAfterDropDownShow, options }: {
4
4
  closeDorpDown: () => void;
5
5
  setOffsetX: (value: React.SetStateAction<number>) => void;
6
6
  setOpen: (value: React.SetStateAction<boolean>) => void;
7
+ handleInput: () => void;
7
8
  containerRef: React.RefObject<HTMLDivElement>;
8
9
  tempSpanRef: React.MutableRefObject<HTMLElement | null>;
9
10
  inputAfterDropDownShow: string;
@@ -13,6 +13,7 @@ var useKeyEventEffect = function useKeyEventEffect(_ref) {
13
13
  var closeDorpDown = _ref.closeDorpDown,
14
14
  setOffsetX = _ref.setOffsetX,
15
15
  setOpen = _ref.setOpen,
16
+ handleInput = _ref.handleInput,
16
17
  containerRef = _ref.containerRef,
17
18
  tempSpanRef = _ref.tempSpanRef,
18
19
  inputAfterDropDownShow = _ref.inputAfterDropDownShow,
@@ -54,7 +55,7 @@ var useKeyEventEffect = function useKeyEventEffect(_ref) {
54
55
  }, [closeDorpDown, setOffsetX, setOpen]);
55
56
  var handleKeyDown = useCallback(function (e) {
56
57
  var _currentSpan$textCont, _currentSpan$textCont2;
57
- if (e.key === 'ArrowUp' || e.key === 'ArrowDown') {
58
+ if (e.key === 'ArrowUp' || e.key === 'ArrowDown' || e.key === 'Delete') {
58
59
  e.preventDefault();
59
60
  e.stopPropagation();
60
61
  return;
@@ -187,6 +188,7 @@ var useKeyEventEffect = function useKeyEventEffect(_ref) {
187
188
  _prevSpan.textContent = prevText.slice(0, -1);
188
189
  }
189
190
  moveCursorToSpan(_prevSpan, false, selection);
191
+ handleInput();
190
192
  }
191
193
  }
192
194
 
@@ -1,5 +1,5 @@
1
1
  import { SheetTableType } from "../..";
2
2
  export declare const useRowSelection: (dataSource: Record<string, unknown>[], rowSelection?: SheetTableType.TableRowSelection, hasChildren?: boolean) => [boolean[], (value: boolean[]) => void];
3
- export declare const formatSelectionData: (param: Pick<SheetTableType.TableProps, "columns" | "dataSource" | "rowKey" | "showRemark" | "rowSelection"> & {
3
+ export declare const formatSelectionData: (param: Pick<SheetTableType.TableProps, "dataSource" | "columns" | "rowKey" | "showRemark" | "rowSelection"> & {
4
4
  checked: boolean[];
5
5
  }) => any[][];
@@ -141,7 +141,7 @@ var data = [{
141
141
  age: 32,
142
142
  address: 'New York No. 1 Lake Park',
143
143
  tags: ['nice', 'developer'],
144
- formula: '=price*qty',
144
+ formula: '=price*qty+wholesale',
145
145
  amount: 100
146
146
  }, {
147
147
  key: '2',
@@ -197,7 +197,7 @@ var App = function App() {
197
197
  var row = _ref4.row,
198
198
  key = _ref4.key,
199
199
  value = _ref4.value;
200
- newData[row] = _objectSpread(_objectSpread({}, newData[row]), {}, _defineProperty({}, key, key === 'tags' ? (_String = String(value)) === null || _String === void 0 ? void 0 : _String.split(',') : value));
200
+ newData[row] = _objectSpread(_objectSpread({}, newData[row]), {}, _defineProperty({}, key === 'amount' ? 'formula' : key, key === 'tags' ? (_String = String(value)) === null || _String === void 0 ? void 0 : _String.split(',') : value));
201
201
  });
202
202
  setData(newData);
203
203
  }, [dataSource]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zhenliang/sheet",
3
- "version": "0.2.5-beta.11",
3
+ "version": "0.2.5-beta.13",
4
4
  "description": "A react library developed with dumi",
5
5
  "license": "MIT",
6
6
  "module": "dist/index.js",