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

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;
@@ -32,21 +32,22 @@ 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 originValue = props.value,
38
38
  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];
39
+ isEditing = props.isEditing,
40
+ record = props.record;
41
+ var calcValue = replaceIndex ? record === null || record === void 0 ? void 0 : record[replaceIndex] : originValue;
42
+ var _useState = useState(calcValue),
43
+ _useState2 = _slicedToArray(_useState, 1),
44
+ inputValue = _useState2[0];
44
45
  var _useState3 = useState(0),
45
46
  _useState4 = _slicedToArray(_useState3, 2),
46
47
  offsetX = _useState4[0],
47
48
  setOffsetX = _useState4[1];
48
49
  // 为了不重新render,再写一个
49
- var _useState5 = useState(value),
50
+ var _useState5 = useState(calcValue),
50
51
  _useState6 = _slicedToArray(_useState5, 2),
51
52
  containerValue = _useState6[0],
52
53
  setContainerValue = _useState6[1];
@@ -132,19 +133,40 @@ export var getFormulaInputEditor = function getFormulaInputEditor(options) {
132
133
  var currentSpan = startContainer.nodeType === Node.TEXT_NODE ? startContainer.parentElement : startContainer;
133
134
  if (!currentSpan || currentSpan === container) return;
134
135
 
135
- // 删除 inputAfterDropDownShow
136
+ // 获取删除 inputAfterDropDownShow 后的前半部分和后半部分
136
137
  var spanText = currentSpan.textContent || '';
137
138
  var deleteText = inputAfterDropDownShow;
138
139
  var deleteIndex = spanText.indexOf(deleteText);
139
- if (deleteIndex !== -1) {
140
- currentSpan.textContent = spanText.slice(0, deleteIndex) + spanText.slice(deleteIndex + deleteText.length);
140
+ // 如果找不到 deleteText,用当前光标位置作为 deleteIndex
141
+ if (deleteIndex === -1 || !deleteText) {
142
+ deleteIndex = range.startOffset;
141
143
  }
144
+ var beforePart = spanText.slice(0, deleteIndex);
145
+ var afterPart = spanText.slice(deleteIndex + deleteText.length);
142
146
 
143
- // 新建 label span,内容为 opt.label,插入到当前 span 后面
147
+ // 删除当前 span,用三个 span 替换:other(前)、label、other(后)
144
148
  var labelSpan = document.createElement('span');
145
149
  labelSpan.className = 'formula-editor-token-label';
146
150
  labelSpan.textContent = opt.label;
147
- currentSpan.after(labelSpan);
151
+
152
+ // 创建待插入的 span 列表
153
+ var spansToInsert = [];
154
+ if (beforePart) {
155
+ var beforeSpan = document.createElement('span');
156
+ beforeSpan.className = 'formula-editor-token-other';
157
+ beforeSpan.textContent = beforePart;
158
+ spansToInsert.push(beforeSpan);
159
+ }
160
+ spansToInsert.push(labelSpan);
161
+ if (afterPart) {
162
+ var afterSpan = document.createElement('span');
163
+ afterSpan.className = 'formula-editor-token-other';
164
+ afterSpan.textContent = afterPart;
165
+ spansToInsert.push(afterSpan);
166
+ }
167
+ // 插入所有 span
168
+ currentSpan.after.apply(currentSpan, spansToInsert);
169
+ currentSpan.remove();
148
170
 
149
171
  // 将光标移到下一个 span 的开头
150
172
  var allSpans = Array.from(container.querySelectorAll('span'));
@@ -265,6 +287,11 @@ export var getFormulaInputEditor = function getFormulaInputEditor(options) {
265
287
  }, [closeDorpDown]);
266
288
  var handleKeyDown = useCallback(function (e) {
267
289
  var _currentSpan$textCont, _currentSpan$textCont2;
290
+ if (e.key === 'ArrowUp' || e.key === "ArrowDown") {
291
+ e.preventDefault();
292
+ e.stopPropagation();
293
+ return;
294
+ }
268
295
  var div = containerRef.current;
269
296
  if (!div) return;
270
297
  var selection = window.getSelection();
@@ -614,7 +641,7 @@ export var getFormulaInputEditor = function getFormulaInputEditor(options) {
614
641
  if (!validateVariables(expr, flatOptions)) {
615
642
  return Infinity;
616
643
  }
617
- return currentValue;
644
+ return value;
618
645
  };
619
646
 
620
647
  // parser:粘贴时处理,将 label 转换为 value
@@ -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) {
@@ -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,16 @@ 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
174
178
  }];
175
179
  var App = function App() {
176
180
  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.1",
4
4
  "description": "A react library developed with dumi",
5
5
  "license": "MIT",
6
6
  "module": "dist/index.js",