@zhenliang/sheet 0.2.5-beta.0 → 0.2.5-beta.2

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.
@@ -10,5 +10,5 @@ export interface FormulaInputProps {
10
10
  options: OptionItem[];
11
11
  value?: string;
12
12
  }
13
- export declare const getFormulaInputEditor: (options: OptionItem[]) => SheetType.CellEditor;
13
+ export declare const getFormulaInputEditor: (options: OptionItem[], replaceIndex?: string) => SheetType.CellEditor;
14
14
  export default getFormulaInputEditor;
@@ -12,7 +12,7 @@ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len
12
12
  function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
13
13
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
14
14
  import { Dropdown, Menu, Tooltip } from 'antd';
15
- import { head, isEmpty, isNil } from 'lodash';
15
+ import { get, head, isEmpty, isNil } from 'lodash';
16
16
  import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
17
17
  import { InfoCircleOutlined } from '@ant-design/icons';
18
18
  import { flattenOptions, getStringDiff, replaceLabelsWithValues, replaceLongestDiff, replaceValuesWithLabels, tokenize, validateVariables, getCursorPositionInSpan } from "./utils";
@@ -32,21 +32,23 @@ var getSpanAtCursor = function getSpanAtCursor(startContainer) {
32
32
  }
33
33
  return null;
34
34
  };
35
- export var getFormulaInputEditor = function getFormulaInputEditor(options) {
35
+ export var getFormulaInputEditor = function getFormulaInputEditor(options, replaceIndex) {
36
36
  var FormulaInputEditor = function FormulaInputEditor(props) {
37
- var value = props.value,
37
+ var _get;
38
+ var originValue = props.value,
38
39
  onChange = props.onChange,
39
- isEditing = props.isEditing;
40
- var _useState = useState(value),
41
- _useState2 = _slicedToArray(_useState, 2),
42
- inputValue = _useState2[0],
43
- setInputValue = _useState2[1];
40
+ isEditing = props.isEditing,
41
+ record = props.record;
42
+ var calcValue = replaceIndex ? "".concat((_get = get(record, [replaceIndex])) !== null && _get !== void 0 ? _get : '') : originValue;
43
+ var _useState = useState(calcValue),
44
+ _useState2 = _slicedToArray(_useState, 1),
45
+ inputValue = _useState2[0];
44
46
  var _useState3 = useState(0),
45
47
  _useState4 = _slicedToArray(_useState3, 2),
46
48
  offsetX = _useState4[0],
47
49
  setOffsetX = _useState4[1];
48
50
  // 为了不重新render,再写一个
49
- var _useState5 = useState(value),
51
+ var _useState5 = useState(calcValue),
50
52
  _useState6 = _slicedToArray(_useState5, 2),
51
53
  containerValue = _useState6[0],
52
54
  setContainerValue = _useState6[1];
@@ -132,19 +134,40 @@ export var getFormulaInputEditor = function getFormulaInputEditor(options) {
132
134
  var currentSpan = startContainer.nodeType === Node.TEXT_NODE ? startContainer.parentElement : startContainer;
133
135
  if (!currentSpan || currentSpan === container) return;
134
136
 
135
- // 删除 inputAfterDropDownShow
137
+ // 获取删除 inputAfterDropDownShow 后的前半部分和后半部分
136
138
  var spanText = currentSpan.textContent || '';
137
139
  var deleteText = inputAfterDropDownShow;
138
140
  var deleteIndex = spanText.indexOf(deleteText);
139
- if (deleteIndex !== -1) {
140
- currentSpan.textContent = spanText.slice(0, deleteIndex) + spanText.slice(deleteIndex + deleteText.length);
141
+ // 如果找不到 deleteText,用当前光标位置作为 deleteIndex
142
+ if (deleteIndex === -1 || !deleteText) {
143
+ deleteIndex = range.startOffset;
141
144
  }
145
+ var beforePart = spanText.slice(0, deleteIndex);
146
+ var afterPart = spanText.slice(deleteIndex + deleteText.length);
142
147
 
143
- // 新建 label span,内容为 opt.label,插入到当前 span 后面
148
+ // 删除当前 span,用三个 span 替换:other(前)、label、other(后)
144
149
  var labelSpan = document.createElement('span');
145
150
  labelSpan.className = 'formula-editor-token-label';
146
151
  labelSpan.textContent = opt.label;
147
- currentSpan.after(labelSpan);
152
+
153
+ // 创建待插入的 span 列表
154
+ var spansToInsert = [];
155
+ if (beforePart) {
156
+ var beforeSpan = document.createElement('span');
157
+ beforeSpan.className = 'formula-editor-token-other';
158
+ beforeSpan.textContent = beforePart;
159
+ spansToInsert.push(beforeSpan);
160
+ }
161
+ spansToInsert.push(labelSpan);
162
+ if (afterPart) {
163
+ var afterSpan = document.createElement('span');
164
+ afterSpan.className = 'formula-editor-token-other';
165
+ afterSpan.textContent = afterPart;
166
+ spansToInsert.push(afterSpan);
167
+ }
168
+ // 插入所有 span
169
+ currentSpan.after.apply(currentSpan, spansToInsert);
170
+ currentSpan.remove();
148
171
 
149
172
  // 将光标移到下一个 span 的开头
150
173
  var allSpans = Array.from(container.querySelectorAll('span'));
@@ -194,7 +217,7 @@ export var getFormulaInputEditor = function getFormulaInputEditor(options) {
194
217
  if (open) {
195
218
  var _getStringDiff2 = getStringDiff(containerValueSnapShot.current, newValue),
196
219
  addedOnSnapShot = _getStringDiff2.added;
197
- isBeginWithNumber = /^\d$/.test(head(addedOnSnapShot));
220
+ isBeginWithNumber = /^\d$/.test(head(addedOnSnapShot.join('').trim()));
198
221
  inputStringAfterDropDownShow = addedOnSnapShot.join('');
199
222
  }
200
223
  if (pressAOperator) {
@@ -265,6 +288,11 @@ export var getFormulaInputEditor = function getFormulaInputEditor(options) {
265
288
  }, [closeDorpDown]);
266
289
  var handleKeyDown = useCallback(function (e) {
267
290
  var _currentSpan$textCont, _currentSpan$textCont2;
291
+ if (e.key === 'ArrowUp' || e.key === "ArrowDown") {
292
+ e.preventDefault();
293
+ e.stopPropagation();
294
+ return;
295
+ }
268
296
  var div = containerRef.current;
269
297
  if (!div) return;
270
298
  var selection = window.getSelection();
@@ -344,6 +372,16 @@ export var getFormulaInputEditor = function getFormulaInputEditor(options) {
344
372
 
345
373
  // 删除键逻辑
346
374
  if (e.key !== 'Delete' && e.key !== 'Backspace') return;
375
+ if (e.key === 'Backspace') {
376
+ // 检查删除后是否只剩一个空的 span
377
+ var allSpansNow = Array.from(div.querySelectorAll('span'));
378
+ var isOnlyOneEmptySpan = allSpansNow.length === 1 && allSpansNow[0].textContent === '';
379
+ if (isOnlyOneEmptySpan) {
380
+ e.preventDefault();
381
+ e.stopPropagation();
382
+ return;
383
+ }
384
+ }
347
385
 
348
386
  // 不在 label span 内,在 span 开头且前一个是 label 时,删除整个 label span
349
387
  if (startOffset === 0 && e.key === 'Backspace' && currentIndex > 0) {
@@ -544,13 +582,13 @@ export var getFormulaInputEditor = function getFormulaInputEditor(options) {
544
582
  spellCheck: false,
545
583
  children: isEmpty(tokens) ? /*#__PURE__*/_jsx("span", {
546
584
  style: {
547
- minWidth: 8,
585
+ minWidth: 1,
548
586
  height: 40
549
587
  }
550
588
  }) : tokens.map(function (token, index) {
551
589
  return token.type === 'end' ? /*#__PURE__*/_jsx("span", {
552
590
  style: {
553
- minWidth: 8,
591
+ minWidth: 1,
554
592
  height: 40
555
593
  }
556
594
  }, index) : /*#__PURE__*/_jsx("span", {
@@ -614,7 +652,7 @@ export var getFormulaInputEditor = function getFormulaInputEditor(options) {
614
652
  if (!validateVariables(expr, flatOptions)) {
615
653
  return Infinity;
616
654
  }
617
- return currentValue;
655
+ return value;
618
656
  };
619
657
 
620
658
  // parser:粘贴时处理,将 label 转换为 value
@@ -4,7 +4,7 @@
4
4
  white-space: nowrap;
5
5
  overflow: hidden;
6
6
  outline: none;
7
- min-width: 95%;
7
+ min-width: 90%;
8
8
  max-height: 100%;
9
9
  box-shadow: inset 0 -100px 0 rgba(33, 133, 208, 15%);
10
10
  position: absolute;
@@ -115,6 +115,9 @@ var splitSegmentByLabels = function splitSegmentByLabels(segment, sortedLabels)
115
115
  * 有效 label 为 type 'label',其他为 'other'
116
116
  */
117
117
  export var tokenize = function tokenize(text, flatOptions) {
118
+ if (!text) {
119
+ return [];
120
+ }
118
121
  var tokens = [];
119
122
 
120
123
  // 将 value 转换为 label
@@ -78,11 +78,14 @@ var Cell = function Cell(props) {
78
78
  return;
79
79
  }
80
80
  if (confirm) {
81
- var _cell$dataEditor3;
81
+ var _cell$dataEditor3, _cell$dataEditor5, _cell$dataEditor6, _cell$dataEditor6$che;
82
82
  setEventState({
83
83
  confirm: false
84
84
  });
85
85
  var newValue = value;
86
+ if (newValue === valueRef.current) {
87
+ return;
88
+ }
86
89
  // 转化一下公式
87
90
  if (cell !== null && cell !== void 0 && (_cell$dataEditor3 = cell.dataEditor) !== null && _cell$dataEditor3 !== void 0 && _cell$dataEditor3.formula) {
88
91
  var _cell$dataEditor4;
@@ -113,20 +116,17 @@ var Cell = function Cell(props) {
113
116
  return;
114
117
  }
115
118
  }
116
- if (newValue !== valueRef.current) {
117
- var _cell$dataEditor5, _cell$dataEditor6, _cell$dataEditor6$che;
118
- if (cell !== null && cell !== void 0 && (_cell$dataEditor5 = cell.dataEditor) !== null && _cell$dataEditor5 !== void 0 && _cell$dataEditor5.checker && ((_cell$dataEditor6 = cell.dataEditor) === null || _cell$dataEditor6 === void 0 || (_cell$dataEditor6$che = _cell$dataEditor6.checker) === null || _cell$dataEditor6$che === void 0 ? void 0 : _cell$dataEditor6$che.call(_cell$dataEditor6, newValue, cell.record)) === false) {
119
- setValue(valueRef.current);
120
- return;
121
- }
122
- eventBus.emit('cell-change', {
123
- row: row,
124
- col: col,
125
- id: cell.id,
126
- value: newValue,
127
- key: cell.dataIndex
128
- });
119
+ if (cell !== null && cell !== void 0 && (_cell$dataEditor5 = cell.dataEditor) !== null && _cell$dataEditor5 !== void 0 && _cell$dataEditor5.checker && ((_cell$dataEditor6 = cell.dataEditor) === null || _cell$dataEditor6 === void 0 || (_cell$dataEditor6$che = _cell$dataEditor6.checker) === null || _cell$dataEditor6$che === void 0 ? void 0 : _cell$dataEditor6$che.call(_cell$dataEditor6, newValue, cell.record)) === false) {
120
+ setValue(valueRef.current);
121
+ return;
129
122
  }
123
+ eventBus.emit('cell-change', {
124
+ row: row,
125
+ col: col,
126
+ id: cell.id,
127
+ value: newValue,
128
+ key: cell.dataIndex
129
+ });
130
130
  }
131
131
  }, [confirm, eventBus, value, cell]);
132
132
  var handleCommit = useCallback(function (value) {
@@ -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, "columns" | "showRemark" | "rowSelection" | "dataSource" | "rowKey"> & {
4
4
  checked: boolean[];
5
5
  }) => any[][];
@@ -67,8 +67,7 @@ var columns = [{
67
67
  })
68
68
  }, {
69
69
  title: 'Formula',
70
- dataIndex: 'formula',
71
- key: 'formula',
70
+ dataIndex: 'amount',
72
71
  width: 200,
73
72
  cellConfig: {
74
73
  className: 'testCell'
@@ -93,7 +92,7 @@ var columns = [{
93
92
  label: '批发价',
94
93
  value: 'wholesale'
95
94
  }]
96
- }])
95
+ }], 'formula')
97
96
  }, {
98
97
  title: 'Address',
99
98
  editable: false,
@@ -142,17 +141,20 @@ var data = [{
142
141
  age: 32,
143
142
  address: 'New York No. 1 Lake Park',
144
143
  tags: ['nice', 'developer'],
145
- formula: '=price*qty'
144
+ formula: '=price*qty',
145
+ amount: 100
146
146
  }, {
147
147
  key: '2',
148
148
  name: 'Jim Green',
149
149
  age: 42,
150
150
  address: 'London No. 1 Lake Park',
151
151
  tags: ['loser'],
152
- formula: '=price+discount'
152
+ formula: '=price+discount',
153
+ amount: 100
153
154
  }, {
154
155
  key: '3',
155
156
  name: 'Joe Black',
157
+ amount: 100,
156
158
  age: 32,
157
159
  address: 'Sidney No. 1 Lake Park',
158
160
  tags: ['cool', 'teacher'],
@@ -163,14 +165,24 @@ var data = [{
163
165
  age: 92,
164
166
  address: 'Sidney No. 1 Lake Park',
165
167
  tags: ['cool', 'teacher'],
166
- formula: '=(123 + 456) * price'
168
+ formula: '=(123 + 456) * price',
169
+ amount: 100
167
170
  }, {
168
171
  key: '5',
169
172
  name: 'Joe ll',
170
173
  age: 37,
171
174
  address: 'Sidney No. 1 Lake Park',
172
175
  tags: ['cool', 'teacher'],
173
- formula: '=(123 + 456) * price + 888'
176
+ formula: '=(123 + 456) * price + 888',
177
+ amount: 100
178
+ }, {
179
+ key: '6',
180
+ name: 'Joe 23',
181
+ age: 41,
182
+ address: 'Sidney No. 1 Lake Park',
183
+ tags: ['cool', 'teacher'],
184
+ formula: null,
185
+ amount: 100
174
186
  }];
175
187
  var App = function App() {
176
188
  var _useState = useState(data),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zhenliang/sheet",
3
- "version": "0.2.5-beta.0",
3
+ "version": "0.2.5-beta.2",
4
4
  "description": "A react library developed with dumi",
5
5
  "license": "MIT",
6
6
  "module": "dist/index.js",