@zhenliang/sheet 0.2.5-beta.8 → 0.2.5-beta.9

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.
@@ -1,18 +1,5 @@
1
1
  import type { SheetType } from "../../../type";
2
- import { InputNumberProps } from 'antd';
3
2
  import './index.less';
4
- export interface OptionItem {
5
- label: string;
6
- value: string;
7
- info?: string;
8
- children?: OptionItem[];
9
- }
10
- type inputProps = Partial<Pick<InputNumberProps, 'max' | 'min' | 'addonBefore' | 'addonAfter' | 'precision'> & {
11
- warnMethod?: (record: any) => void;
12
- rangeMethod?: (record: any) => {
13
- max?: number;
14
- min?: number;
15
- };
16
- }>;
3
+ import { OptionItem, inputProps } from './utils';
17
4
  export declare const getFormulaInputEditor: (options: OptionItem[], replaceIndex?: string, extraProps?: inputProps, getExtraProps?: ((props: SheetType.CellEditorProps) => inputProps) | undefined, choseEditor?: ((record: any) => boolean) | undefined) => SheetType.CellEditor;
18
5
  export default getFormulaInputEditor;
@@ -18,22 +18,28 @@ import { get, head, isEmpty, isNil } from 'lodash';
18
18
  import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
19
19
  import { formulaString } from "../numberEditor";
20
20
  import "./index.less";
21
- import { flattenOptions, getCursorPositionInSpan, getStringDiff, replaceLabelsWithValues, replaceLongestDiff, replaceValuesWithLabels, tokenize, validateVariables } from "./utils";
21
+ import { flattenOptions, getCursorPositionInSpan, getSpanAtCursor, getStringDiff, moveCursorToSpan, replaceLabelsWithValues, replaceLongestDiff, replaceValuesWithLabels, tokenize, validateVariables } from "./utils";
22
22
  import validateCalculationExpr from "./vaildFormula";
23
23
  import { jsx as _jsx } from "react/jsx-runtime";
24
24
  import { jsxs as _jsxs } from "react/jsx-runtime";
25
- var OPERATORS = ['=', '+', '-', '*', '/'];
26
-
27
- // 获取光标所在的 span
28
- var getSpanAtCursor = function getSpanAtCursor(startContainer) {
29
- if (startContainer.nodeType === Node.TEXT_NODE) {
30
- return startContainer.parentElement;
31
- }
32
- if (startContainer.nodeType === Node.ELEMENT_NODE) {
33
- return startContainer;
34
- }
35
- return null;
36
- };
25
+ var OPERATORS = ['=', '+', '-', '*', '/', '('];
26
+
27
+ // // 创建 label span
28
+ // const createLabelSpan = (label: string): HTMLElement => {
29
+ // const span = document.createElement('span');
30
+ // span.className = 'formula-editor-token-label';
31
+ // span.textContent = label;
32
+ // return span;
33
+ // };
34
+
35
+ // // 创建 empty other span
36
+ // const createEmptyOtherSpan = (): HTMLElement => {
37
+ // const span = document.createElement('span');
38
+ // span.className = 'formula-editor-token-other';
39
+ // span.textContent = '';
40
+ // return span;
41
+ // };
42
+
37
43
  export var getFormulaInputEditor = function getFormulaInputEditor(options, replaceIndex, extraProps, getExtraProps, choseEditor) {
38
44
  var _ref = extraProps || {},
39
45
  warnMethod = _ref.warnMethod,
@@ -158,14 +164,7 @@ export var getFormulaInputEditor = function getFormulaInputEditor(options, repla
158
164
  _targetSpan.textContent = '';
159
165
  replacedSpan.after(_targetSpan);
160
166
  }
161
- var _selection = window.getSelection();
162
- if (_selection && _selection.rangeCount > 0) {
163
- var _newRange = document.createRange();
164
- _newRange.selectNodeContents(_targetSpan);
165
- _newRange.collapse(true);
166
- _selection.removeAllRanges();
167
- _selection.addRange(_newRange);
168
- }
167
+ moveCursorToSpan(_targetSpan);
169
168
 
170
169
  // 更新 containerValue
171
170
  var newValue = container.textContent || '';
@@ -235,13 +234,7 @@ export var getFormulaInputEditor = function getFormulaInputEditor(options, repla
235
234
  targetSpan.textContent = '';
236
235
  labelSpan.after(targetSpan);
237
236
  }
238
-
239
- // 将光标移到目标 span 的开头
240
- var newRange = document.createRange();
241
- newRange.selectNodeContents(targetSpan);
242
- newRange.collapse(true);
243
- selection.removeAllRanges();
244
- selection.addRange(newRange);
237
+ moveCursorToSpan(targetSpan);
245
238
  }, [inputAfterDropDownShow]);
246
239
 
247
240
  // 处理输入变化
@@ -350,7 +343,7 @@ export var getFormulaInputEditor = function getFormulaInputEditor(options, repla
350
343
  selection.addRange(newRange);
351
344
  }
352
345
  }
353
- }, [closeDorpDown]);
346
+ }, [closeDorpDown, setOffsetX, setOpen]);
354
347
  var handleKeyDown = useCallback(function (e) {
355
348
  var _currentSpan$textCont, _currentSpan$textCont2;
356
349
  if (e.key === 'ArrowUp' || e.key === 'ArrowDown') {
@@ -388,39 +381,27 @@ export var getFormulaInputEditor = function getFormulaInputEditor(options, repla
388
381
  if (targetIndex < allSpans.length && targetIndex !== currentIndex + 1) {
389
382
  e.preventDefault();
390
383
  var targetSpan = allSpans[targetIndex];
391
- var newRange = document.createRange();
392
- newRange.selectNodeContents(targetSpan);
393
- newRange.collapse(true);
394
- selection.removeAllRanges();
395
- selection.addRange(newRange);
384
+ moveCursorToSpan(targetSpan);
396
385
  }
397
- } else if (e.key === 'ArrowLeft' && isAtStart) {
386
+ } else if (e.key === 'ArrowRight' && isAlmostEnd) {
398
387
  closeDorpDown();
399
- // 向左 + 在开头 -> 跳到上一个 span 的末尾(跳过 label span)
400
- var _targetIndex = currentIndex - 1;
401
- while (_targetIndex >= 0 && allSpans[_targetIndex].classList.contains('formula-editor-token-label')) {
402
- _targetIndex--;
403
- }
404
- if (_targetIndex >= 0 && _targetIndex !== currentIndex - 1) {
388
+ var _targetIndex = currentIndex + 1;
389
+ if (_targetIndex < allSpans.length && !allSpans[_targetIndex].classList.contains('formula-editor-token-label')) {
405
390
  e.preventDefault();
406
391
  var _targetSpan2 = allSpans[_targetIndex];
407
- var _newRange2 = document.createRange();
408
- _newRange2.selectNodeContents(_targetSpan2);
409
- _newRange2.collapse(false);
410
- selection.removeAllRanges();
411
- selection.addRange(_newRange2);
392
+ moveCursorToSpan(_targetSpan2);
412
393
  }
413
- } else if (e.key === 'ArrowRight' && isAlmostEnd) {
394
+ } else if (e.key === 'ArrowLeft' && isAtStart) {
414
395
  closeDorpDown();
415
- var _targetIndex2 = currentIndex + 1;
416
- if (_targetIndex2 < allSpans.length && !allSpans[_targetIndex2].classList.contains('formula-editor-token-label')) {
396
+ // 向左 + 在开头 -> 跳到上一个 span 的末尾(跳过 label span)
397
+ var _targetIndex2 = currentIndex - 1;
398
+ while (_targetIndex2 >= 0 && allSpans[_targetIndex2].classList.contains('formula-editor-token-label')) {
399
+ _targetIndex2--;
400
+ }
401
+ if (_targetIndex2 >= 0 && _targetIndex2 !== currentIndex - 1) {
417
402
  e.preventDefault();
418
403
  var _targetSpan3 = allSpans[_targetIndex2];
419
- var _newRange3 = document.createRange();
420
- _newRange3.selectNodeContents(_targetSpan3);
421
- _newRange3.collapse(true);
422
- selection.removeAllRanges();
423
- selection.addRange(_newRange3);
404
+ moveCursorToSpan(_targetSpan3, false);
424
405
  }
425
406
  } else if (e.key === 'ArrowLeft' && isAlmostStart) {
426
407
  closeDorpDown();
@@ -428,11 +409,7 @@ export var getFormulaInputEditor = function getFormulaInputEditor(options, repla
428
409
  if (_targetIndex3 >= 0 && !allSpans[_targetIndex3].classList.contains('formula-editor-token-label')) {
429
410
  e.preventDefault();
430
411
  var _targetSpan4 = allSpans[_targetIndex3];
431
- var _newRange4 = document.createRange();
432
- _newRange4.selectNodeContents(_targetSpan4);
433
- _newRange4.collapse(false);
434
- selection.removeAllRanges();
435
- selection.addRange(_newRange4);
412
+ moveCursorToSpan(_targetSpan4, false);
436
413
  }
437
414
  }
438
415
  return;
@@ -461,12 +438,12 @@ export var getFormulaInputEditor = function getFormulaInputEditor(options, repla
461
438
  e.preventDefault();
462
439
  e.stopPropagation();
463
440
  prevSpan.remove();
464
- var _newRange5 = document.createRange();
441
+ var newRange = document.createRange();
465
442
 
466
443
  // 如果当前 span 有内容,光标放在开头;否则找最近的有内容的 span
467
444
  if (currentSpan.textContent !== '') {
468
- _newRange5.selectNodeContents(currentSpan);
469
- _newRange5.collapse(true);
445
+ newRange.selectNodeContents(currentSpan);
446
+ newRange.collapse(true);
470
447
  } else {
471
448
  // 找前一个最近的有内容的 span
472
449
  var _targetSpan5;
@@ -477,15 +454,15 @@ export var getFormulaInputEditor = function getFormulaInputEditor(options, repla
477
454
  }
478
455
  }
479
456
  if (_targetSpan5) {
480
- _newRange5.selectNodeContents(_targetSpan5);
481
- _newRange5.collapse(false);
457
+ newRange.selectNodeContents(_targetSpan5);
458
+ newRange.collapse(false);
482
459
  } else {
483
- _newRange5.selectNodeContents(div);
484
- _newRange5.collapse(true);
460
+ newRange.selectNodeContents(div);
461
+ newRange.collapse(true);
485
462
  }
486
463
  }
487
464
  selection.removeAllRanges();
488
- selection.addRange(_newRange5);
465
+ selection.addRange(newRange);
489
466
  return;
490
467
  }
491
468
  }
@@ -501,11 +478,11 @@ export var getFormulaInputEditor = function getFormulaInputEditor(options, repla
501
478
  if (prevText.length > 0) {
502
479
  _prevSpan.textContent = prevText.slice(0, -1);
503
480
  }
504
- var _newRange6 = document.createRange();
505
- _newRange6.selectNodeContents(_prevSpan);
506
- _newRange6.collapse(false);
481
+ var _newRange = document.createRange();
482
+ _newRange.selectNodeContents(_prevSpan);
483
+ _newRange.collapse(false);
507
484
  selection.removeAllRanges();
508
- selection.addRange(_newRange6);
485
+ selection.addRange(_newRange);
509
486
  }
510
487
  }
511
488
 
@@ -533,29 +510,29 @@ export var getFormulaInputEditor = function getFormulaInputEditor(options, repla
533
510
  var labelIndex = allSpansNow.indexOf(_targetSpan6);
534
511
  if (labelIndex < allSpansNow.length - 1) {
535
512
  var nextSpan = allSpansNow[labelIndex + 1];
536
- var _newRange7 = document.createRange();
537
- _newRange7.selectNodeContents(nextSpan);
538
- _newRange7.collapse(true);
513
+ var _newRange2 = document.createRange();
514
+ _newRange2.selectNodeContents(nextSpan);
515
+ _newRange2.collapse(true);
539
516
  selection.removeAllRanges();
540
- selection.addRange(_newRange7);
517
+ selection.addRange(_newRange2);
541
518
  return;
542
519
  }
543
520
  }
544
521
 
545
522
  // 否则把光标放在找到的 span 的末尾
546
523
  if (_targetSpan6) {
547
- var _newRange8 = document.createRange();
548
- _newRange8.selectNodeContents(_targetSpan6);
549
- _newRange8.collapse(false);
524
+ var _newRange3 = document.createRange();
525
+ _newRange3.selectNodeContents(_targetSpan6);
526
+ _newRange3.collapse(false);
550
527
  selection.removeAllRanges();
551
- selection.addRange(_newRange8);
528
+ selection.addRange(_newRange3);
552
529
  return;
553
530
  }
554
531
  }
555
532
  if (!currentSpanNow || currentSpanNow === div) return;
556
533
  if (currentSpanNow.textContent === '') {
557
534
  var currentIndexNow = allSpansNow.indexOf(currentSpanNow);
558
- var _newRange9 = document.createRange();
535
+ var _newRange4 = document.createRange();
559
536
  if (currentIndexNow > 0) {
560
537
  // 移到前一个 span 的末尾,但如果前一个 span 是 label,则移到后一个 span 的开头
561
538
  var _targetSpan7 = allSpansNow[currentIndexNow - 1];
@@ -563,30 +540,30 @@ export var getFormulaInputEditor = function getFormulaInputEditor(options, repla
563
540
  // 移到 label span 的后一个 span 的开头
564
541
  if (currentIndexNow < allSpansNow.length - 1) {
565
542
  var _nextSpan = allSpansNow[currentIndexNow + 1];
566
- _newRange9.selectNodeContents(_nextSpan);
567
- _newRange9.collapse(true);
543
+ _newRange4.selectNodeContents(_nextSpan);
544
+ _newRange4.collapse(true);
568
545
  } else {
569
- _newRange9.selectNodeContents(div);
570
- _newRange9.collapse(false);
546
+ _newRange4.selectNodeContents(div);
547
+ _newRange4.collapse(false);
571
548
  }
572
549
  } else {
573
- _newRange9.selectNodeContents(_targetSpan7);
574
- _newRange9.collapse(false);
550
+ _newRange4.selectNodeContents(_targetSpan7);
551
+ _newRange4.collapse(false);
575
552
  }
576
553
  currentSpanNow.remove();
577
554
  } else if (allSpansNow.length > 1) {
578
555
  // 第一个 span 为空,移到下一个 span 的开头
579
556
  var _nextSpan2 = allSpansNow[currentIndexNow + 1];
580
- _newRange9.selectNodeContents(_nextSpan2);
581
- _newRange9.collapse(true);
557
+ _newRange4.selectNodeContents(_nextSpan2);
558
+ _newRange4.collapse(true);
582
559
  currentSpanNow.remove();
583
560
  } else {
584
561
  // 只有一个空 span,保持空
585
- _newRange9.selectNodeContents(div);
586
- _newRange9.collapse(true);
562
+ _newRange4.selectNodeContents(div);
563
+ _newRange4.collapse(true);
587
564
  }
588
565
  selection.removeAllRanges();
589
- selection.addRange(_newRange9);
566
+ selection.addRange(_newRange4);
590
567
  }
591
568
  }, 0);
592
569
  }, []);
@@ -1,3 +1,4 @@
1
+ import { InputNumberProps } from "antd";
1
2
  export declare const OPERATORS: string[];
2
3
  export declare const VARIABLE_REGEX: RegExp;
3
4
  export declare const NUMBER_REGEX: RegExp;
@@ -57,3 +58,18 @@ export declare const longestCommonSubsequence: (arr1: string, arr2: string) => s
57
58
  export declare const getStringDiff: (str1: any, str2: any, mode?: string) => any;
58
59
  export declare const replaceLongestDiff: (a: string, str: string, position: number) => string;
59
60
  export declare const getCursorPositionInSpan: (span: any) => any;
61
+ export interface OptionItem {
62
+ label: string;
63
+ value: string;
64
+ info?: string;
65
+ children?: OptionItem[];
66
+ }
67
+ export type inputProps = Partial<Pick<InputNumberProps, 'max' | 'min' | 'addonBefore' | 'addonAfter' | 'precision'> & {
68
+ warnMethod?: (record: any) => void;
69
+ rangeMethod?: (record: any) => {
70
+ max?: number;
71
+ min?: number;
72
+ };
73
+ }>;
74
+ export declare const getSpanAtCursor: (startContainer: any) => HTMLElement | null;
75
+ export declare const moveCursorToSpan: (targetSpan: HTMLElement, atStart?: boolean) => void;
@@ -338,4 +338,26 @@ export var getCursorPositionInSpan = function getCursorPositionInSpan(span) {
338
338
  preCaretRange.selectNodeContents(span);
339
339
  preCaretRange.setEnd(range.startContainer, range.startOffset);
340
340
  return preCaretRange.toString().length;
341
+ };
342
+ // 获取光标所在的 span
343
+ export var getSpanAtCursor = function getSpanAtCursor(startContainer) {
344
+ if (startContainer.nodeType === Node.TEXT_NODE) {
345
+ return startContainer.parentElement;
346
+ }
347
+ if (startContainer.nodeType === Node.ELEMENT_NODE) {
348
+ return startContainer;
349
+ }
350
+ return null;
351
+ };
352
+
353
+ // 将光标移到目标 span 的开头或末尾
354
+ export var moveCursorToSpan = function moveCursorToSpan(targetSpan) {
355
+ var atStart = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
356
+ var selection = window.getSelection();
357
+ if (!selection) return;
358
+ var newRange = document.createRange();
359
+ newRange.selectNodeContents(targetSpan);
360
+ newRange.collapse(atStart);
361
+ selection.removeAllRanges();
362
+ selection.addRange(newRange);
341
363
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zhenliang/sheet",
3
- "version": "0.2.5-beta.8",
3
+ "version": "0.2.5-beta.9",
4
4
  "description": "A react library developed with dumi",
5
5
  "license": "MIT",
6
6
  "module": "dist/index.js",