@zhenliang/sheet 0.2.5-beta.12 → 0.2.5-beta.14

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 = ['=', '+', '-', '*', '/', '('];
@@ -67,7 +67,6 @@ export var getFormulaInputEditor = function getFormulaInputEditor(options, repla
67
67
  setInputAfterDropDownShow = _useState10[1];
68
68
  var tempSpanRef = useRef(null);
69
69
  var containerRef = useRef(null);
70
- var inputOperatorPosition = useRef(0);
71
70
 
72
71
  // 扁平化所有选项
73
72
  var flatOptions = useMemo(function () {
@@ -124,12 +123,6 @@ export var getFormulaInputEditor = function getFormulaInputEditor(options, repla
124
123
  setOpen(false);
125
124
  return;
126
125
  }
127
- // 原逻辑:删除 inputAfterDropDownShow,插入新 label
128
- var newValueUser = replaceLongestDiff(containerValueSnapShot.current, opt.label, inputOperatorPosition.current);
129
- var expr = newValueUser;
130
- var valueExprUser = replaceLabelsWithValues(expr, flatOptions);
131
- setContainerValue(valueExprUser);
132
- onChange(valueExprUser);
133
126
  var range = selection.getRangeAt(0);
134
127
  var startContainer = range.startContainer;
135
128
  var currentSpan = startContainer.nodeType === Node.TEXT_NODE ? startContainer.parentElement : startContainer;
@@ -174,6 +167,11 @@ export var getFormulaInputEditor = function getFormulaInputEditor(options, repla
174
167
  targetSpan = createSpan(OTHER, '');
175
168
  labelSpan.after(targetSpan);
176
169
  }
170
+ var newValueUser = container.textContent || '';
171
+ var expr = newValueUser;
172
+ var valueExprUser = replaceLabelsWithValues(expr, flatOptions);
173
+ setContainerValue(valueExprUser);
174
+ onChange(valueExprUser);
177
175
  moveCursorToSpan(targetSpan, true, selection);
178
176
  setOpen(false);
179
177
  }, [inputAfterDropDownShow]);
@@ -189,14 +187,6 @@ export var getFormulaInputEditor = function getFormulaInputEditor(options, repla
189
187
  var processedValue = newValue;
190
188
  var expr = processedValue;
191
189
  var valueExpr = replaceLabelsWithValues(expr, flatOptions);
192
- var oldLabelText = replaceValuesWithLabels(containerValue, flatOptions);
193
- var firstDiffPosition = 0;
194
- for (var i = 0; i < newValue.length; i++) {
195
- if (newValue[i] !== oldLabelText[i]) {
196
- firstDiffPosition = i;
197
- break;
198
- }
199
- }
200
190
  var _getStringDiff = getStringDiff(containerValue, valueExpr),
201
191
  added = _getStringDiff.added,
202
192
  removed = _getStringDiff.removed;
@@ -215,7 +205,6 @@ export var getFormulaInputEditor = function getFormulaInputEditor(options, repla
215
205
  // 按下了运算符,下拉框刚打开,重置输入记录
216
206
  containerValueSnapShot.current = expr;
217
207
  inputStringAfterDropDownShow = '';
218
- inputOperatorPosition.current = firstDiffPosition;
219
208
 
220
209
  // 获取当前光标距离 containerRef 最左边的距离
221
210
  var selection = window.getSelection();
@@ -241,6 +230,7 @@ export var getFormulaInputEditor = function getFormulaInputEditor(options, repla
241
230
  closeDorpDown: closeDorpDown,
242
231
  setOffsetX: setOffsetX,
243
232
  setOpen: setOpen,
233
+ handleInput: handleInput,
244
234
  containerRef: containerRef,
245
235
  tempSpanRef: tempSpanRef,
246
236
  inputAfterDropDownShow: inputAfterDropDownShow,
@@ -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;
@@ -8,11 +8,12 @@ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol
8
8
  function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
9
9
  function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
10
10
  import { useCallback, useMemo } from 'react';
11
- import { getCursorPositionInSpan, getSpanAtCursor, moveCursorToSpan } from "./utils";
11
+ import { createSpan, getCursorPositionInSpan, getSpanAtCursor, moveCursorToSpan, SpanType } from "./utils";
12
12
  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' || e.key === 'Delete') {
58
+ if (e.key === 'ArrowUp' || e.key === 'ArrowDown') {
58
59
  e.preventDefault();
59
60
  e.stopPropagation();
60
61
  return;
@@ -122,17 +123,14 @@ var useKeyEventEffect = function useKeyEventEffect(_ref) {
122
123
  }
123
124
  return;
124
125
  }
125
-
126
+ var isDeleteOpt = e.key === 'Backspace' || e.key === 'Delete';
126
127
  // 删除键逻辑
127
- if (e.key !== 'Delete' && e.key !== 'Backspace') return;
128
- if (e.key === 'Backspace' || e.key === 'Delete') {
128
+ if (!isDeleteOpt) return;
129
+ if (isDeleteOpt) {
129
130
  if (tempSpanRef.current) {
130
131
  closeDorpDown();
131
132
  }
132
- // 检查删除后是否只剩一个空的 span
133
- var allSpansNow = Array.from(div.querySelectorAll('span'));
134
- var isOnlyOneEmptySpan = allSpansNow.length === 1 && allSpansNow[0].textContent === '';
135
- if (isOnlyOneEmptySpan) {
133
+ if (!div.textContent || div.textContent === '') {
136
134
  e.preventDefault();
137
135
  e.stopPropagation();
138
136
  return;
@@ -140,7 +138,7 @@ var useKeyEventEffect = function useKeyEventEffect(_ref) {
140
138
  }
141
139
 
142
140
  // 不在 label span 内,在 span 开头且前一个是 label 时,删除整个 label span
143
- if (startOffset === 0 && e.key === 'Backspace' && currentIndex > 0) {
141
+ if (startOffset === 0 && e.key === 'BackSpace' && currentIndex > 0) {
144
142
  var prevSpan = allSpans[currentIndex - 1];
145
143
  if (prevSpan.classList.contains('formula-editor-token-label')) {
146
144
  e.preventDefault();
@@ -176,7 +174,7 @@ var useKeyEventEffect = function useKeyEventEffect(_ref) {
176
174
  }
177
175
 
178
176
  // 在 span 开头 + Delete 键,光标移到前一个 span 的末尾
179
- if (startOffset === 0 && e.key === 'Backspace' && currentIndex > 0) {
177
+ if (startOffset === 0 && e.key === 'BackSpace' && currentIndex > 0) {
180
178
  var _prevSpan = allSpans[currentIndex - 1];
181
179
  // 前一个 span 是非 label span 时,光标移到其末尾
182
180
  if (!_prevSpan.classList.contains('formula-editor-token-label')) {
@@ -187,6 +185,7 @@ var useKeyEventEffect = function useKeyEventEffect(_ref) {
187
185
  _prevSpan.textContent = prevText.slice(0, -1);
188
186
  }
189
187
  moveCursorToSpan(_prevSpan, false, selection);
188
+ handleInput();
190
189
  }
191
190
  }
192
191
 
@@ -225,7 +224,13 @@ var useKeyEventEffect = function useKeyEventEffect(_ref) {
225
224
  return;
226
225
  }
227
226
  }
228
- if (!currentSpanNow || currentSpanNow === div) return;
227
+ if (!currentSpanNow || currentSpanNow === div) {
228
+ // 插入一个空的 other span
229
+ var emptySpan = createSpan(SpanType.OTHER, '');
230
+ div.appendChild(emptySpan);
231
+ moveCursorToSpan(emptySpan, true, selection);
232
+ return;
233
+ }
229
234
  if (currentSpanNow.textContent === '') {
230
235
  var currentIndexNow = allSpansNow.indexOf(currentSpanNow);
231
236
  var _newRange = document.createRange();
@@ -6,7 +6,7 @@ function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" !=
6
6
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
7
7
  import { ExclamationCircleOutlined } from '@ant-design/icons';
8
8
  import { Modal } from 'antd';
9
- import { isNil } from 'lodash';
9
+ import { get, isNil } from 'lodash';
10
10
  import React, { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react';
11
11
  import { useSetState, useSheetEvent } from "../../hooks";
12
12
  import { renderValue } from "../util";
@@ -30,6 +30,7 @@ var Cell = function Cell(props) {
30
30
  valueViewer = props.valueViewer,
31
31
  attributesRenderer = props.attributesRenderer,
32
32
  cellFixedInfo = props.cellFixedInfo;
33
+ var replaceIndex = cell.replaceIndex;
33
34
  var eventBus = useSheetEvent();
34
35
  var _useSetState = useSetState(),
35
36
  _useSetState2 = _slicedToArray(_useSetState, 2),
@@ -82,8 +83,9 @@ var Cell = function Cell(props) {
82
83
  setEventState({
83
84
  confirm: false
84
85
  });
86
+ var oldValue = replaceIndex ? get(cell.record, [replaceIndex(cell.record)]) : valueRef.current;
85
87
  var newValue = value;
86
- if (newValue === valueRef.current) {
88
+ if (newValue === oldValue) {
87
89
  return;
88
90
  }
89
91
  // 转化一下公式
@@ -165,6 +165,7 @@ export var formatGroupData = function formatGroupData(param) {
165
165
  valueViewer: colInfo.render ? colInfo.render : undefined,
166
166
  dataEditor: colInfo.editor ? colInfo.editor : undefined,
167
167
  searchKey: colInfo.searchKey,
168
+ replaceIndex: colInfo.replaceIndex,
168
169
  row: currentIndex,
169
170
  className: classNames(!(((_colInfo$cellConfig = colInfo.cellConfig) === null || _colInfo$cellConfig === void 0 ? void 0 : _colInfo$cellConfig.className) instanceof Function) ? (_colInfo$cellConfig2 = colInfo.cellConfig) === null || _colInfo$cellConfig2 === void 0 ? void 0 : _colInfo$cellConfig2.className : (_colInfo$cellConfig3 = colInfo.cellConfig) === null || _colInfo$cellConfig3 === void 0 ? void 0 : _colInfo$cellConfig3.className(value, itemRow, currentIndex), hasRemark ? 'remark-cell' : null),
170
171
  dataIndex: colInfo.dataIndex,
@@ -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" | "showRemark" | "rowSelection" | "dataSource" | "rowKey"> & {
3
+ export declare const formatSelectionData: (param: Pick<SheetTableType.TableProps, "columns" | "dataSource" | "rowKey" | "showRemark" | "rowSelection"> & {
4
4
  checked: boolean[];
5
5
  }) => any[][];
@@ -82,6 +82,7 @@ export var formatSelectionData = function formatSelectionData(param) {
82
82
  valueViewer: colInfo.render ? colInfo.render : undefined,
83
83
  dataEditor: colInfo.editor ? colInfo.editor : undefined,
84
84
  searchKey: colInfo.searchKey,
85
+ replaceIndex: colInfo.replaceIndex,
85
86
  className: classNames(!(((_colInfo$cellConfig = colInfo.cellConfig) === null || _colInfo$cellConfig === void 0 ? void 0 : _colInfo$cellConfig.className) instanceof Function) ? (_colInfo$cellConfig2 = colInfo.cellConfig) === null || _colInfo$cellConfig2 === void 0 ? void 0 : _colInfo$cellConfig2.className : (_colInfo$cellConfig3 = colInfo.cellConfig) === null || _colInfo$cellConfig3 === void 0 ? void 0 : _colInfo$cellConfig3.className(value, item, row), hasRemark ? 'remark-cell' : null),
86
87
  row: row,
87
88
  col: col,
@@ -92,7 +92,10 @@ var columns = [{
92
92
  label: '批发价',
93
93
  value: 'wholesale'
94
94
  }]
95
- }], 'formula')
95
+ }], 'formula'),
96
+ replaceIndex: function replaceIndex(record) {
97
+ return 'formula';
98
+ }
96
99
  }, {
97
100
  title: 'Address',
98
101
  editable: false,
@@ -141,7 +144,7 @@ var data = [{
141
144
  age: 32,
142
145
  address: 'New York No. 1 Lake Park',
143
146
  tags: ['nice', 'developer'],
144
- formula: '=price*qty',
147
+ formula: '=price*qty+wholesale',
145
148
  amount: 100
146
149
  }, {
147
150
  key: '2',
@@ -26,6 +26,7 @@ export type Cell = {
26
26
  dataIndex?: string;
27
27
  valueViewer?: CellViewer;
28
28
  searchKey?: (value: unknown, record?: unknown) => string;
29
+ replaceIndex?: (record?: unknown) => string;
29
30
  className?: string;
30
31
  align?: CellAlign;
31
32
  fixed?: Omit<CellAlign, 'center'>;
@@ -40,6 +40,7 @@ export type ColumnProps = {
40
40
  render?: SheetType.CellViewer;
41
41
  editor?: SheetType.CellEditor;
42
42
  searchKey?: (value: unknown, record: Record<string, unknown>) => string;
43
+ replaceIndex?: (record?: unknown) => string;
43
44
  fixedAllowSelect?: boolean | RecordRowMap<boolean>;
44
45
  };
45
46
  export type TableChange = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zhenliang/sheet",
3
- "version": "0.2.5-beta.12",
3
+ "version": "0.2.5-beta.14",
4
4
  "description": "A react library developed with dumi",
5
5
  "license": "MIT",
6
6
  "module": "dist/index.js",