Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into refactor/facade-extend
Browse files Browse the repository at this point in the history
  • Loading branch information
wzhudev committed Oct 15, 2024
2 parents 2a31e9e + e16ead3 commit 254e5da
Show file tree
Hide file tree
Showing 63 changed files with 2,871 additions and 961 deletions.
10 changes: 8 additions & 2 deletions packages/design/src/components/popup/RectPopup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,15 @@ function RectPopup(props: IRectPopupProps) {
}, [anchorRect, anchorRect.bottom, anchorRect.left, anchorRect.right, anchorRect.top, clickOtherFn, excludeOutside]);

useEffect(() => {
window.addEventListener('contextmenu', contextMenuFn);
const handleContextMenu = (e: MouseEvent) => {
if (e.ctrlKey && e.button === 0) {
return;
}
contextMenuFn();
};
window.addEventListener('contextmenu', handleContextMenu);
return () => {
window.removeEventListener('contextmenu', contextMenuFn);
window.removeEventListener('contextmenu', handleContextMenu);
};
}, [contextMenuFn]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ const dialogOnlyInputStyle: React.CSSProperties = {
pointerEvents: 'none',
};

/**
* @deprecated
*/
export function RangeSelector(props: IRangeSelectorProps) {
const { dialogOnly, onChange, id, value = '', width = 220, placeholder = '', size = 'middle', onActive, onValid, isSingleChoice = false, openForSheetUnitId, openForSheetSubUnitId, isReadonly = false, className, textEditorClassName, onSelectorVisibleChange: _onSelectorVisibleChange } = props;
const onSelectorVisibleChange = useEvent(_onSelectorVisibleChange);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class DocBackScrollRenderController extends RxDisposable implements IRend
const { startOffset } = range;
const anchorNodePosition = skeleton.findNodePositionByCharIndex(startOffset);

this.scrollToNode(anchorNodePosition);
anchorNodePosition && this.scrollToNode(anchorNodePosition);
}

scrollToNode(startNodePosition: Nullable<INodePosition>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ export class EditorService extends Disposable implements IEditorService, IDispos
this._contextService.setContextValue(FOCUSING_UNIVER_EDITOR_STANDALONE_SINGLE_MODE, editor.isSingle());

if (!this._spreadsheetFocusState) {
this.singleSelection(editor.isSingleChoice());
this.singleSelection(!!editor.isSingleChoice());
}

this._focusStyle$.next(editorUnitId);
Expand Down
10 changes: 9 additions & 1 deletion packages/docs-ui/src/services/editor/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import type { DocumentDataModel, ICommandService, IDocumentData, IDocumentStyle,
import type { DocSelectionManagerService } from '@univerjs/docs';
import type { IDocSelectionInnerParam, IRender, ISuccinctDocRangeParam, ITextRangeWithStyle } from '@univerjs/engine-render';
import { DEFAULT_STYLES, Disposable, UniverInstanceType } from '@univerjs/core';
import { KeyCode } from '@univerjs/ui';
import { merge, type Observable, Subject } from 'rxjs';
import { filter } from 'rxjs/operators';
import { ReplaceSnapshotCommand } from '../../commands/commands/replace-content.command';
import { DocSelectionRenderService, type IEditorInputConfig } from '../selection/doc-selection-render.service';

Expand Down Expand Up @@ -222,7 +224,13 @@ export class Editor extends Disposable implements IEditor {
this.disposeWithMe(
merge(
docSelectionRenderService.onInput$,
docSelectionRenderService.onKeydown$,
docSelectionRenderService.onKeydown$.pipe(filter((e) => {
const event = e.event as KeyboardEvent;
if (event.ctrlKey || event.metaKey) {
return [KeyCode.X, KeyCode.V].includes(event.keyCode);
}
return [KeyCode.BACKSPACE].includes(event.keyCode);
})),
docSelectionRenderService.onCompositionupdate$,
docSelectionRenderService.onCompositionend$,
docSelectionRenderService.onPaste$
Expand Down
4 changes: 2 additions & 2 deletions packages/docs/src/services/doc-selection-manager.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import type {
} from '@univerjs/engine-render';
import { ICommandService, IUniverInstanceService, RxDisposable, UniverInstanceType } from '@univerjs/core';
import { NORMAL_TEXT_SELECTION_PLUGIN_STYLE } from '@univerjs/engine-render';
import { BehaviorSubject, takeUntil } from 'rxjs';
import { BehaviorSubject, Subject, takeUntil } from 'rxjs';
import { SetTextSelectionsOperation } from '../commands/operations/text-selection.operation';

interface IDocSelectionManagerSearchParam {
Expand Down Expand Up @@ -51,7 +51,7 @@ export class DocSelectionManagerService extends RxDisposable {

private readonly _textSelectionInfo: ITextSelectionInfo = new Map();

private readonly _textSelection$ = new BehaviorSubject<Nullable<ITextSelectionManagerInsertParam>>(null);
private readonly _textSelection$ = new Subject<ITextSelectionManagerInsertParam>();
readonly textSelection$ = this._textSelection$.asObservable();

private readonly _refreshSelection$ = new BehaviorSubject<Nullable<IRefreshSelectionParam>>(null);
Expand Down
3 changes: 2 additions & 1 deletion packages/engine-formula/src/basics/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* limitations under the License.
*/

import { isRealNum, numfmt } from '@univerjs/core';
import type { BaseValueObject } from '../engine/value-object/base-value-object';
import { isRealNum, numfmt } from '@univerjs/core';
import { ErrorValueObject } from '../engine/value-object/base-value-object';
import { ErrorType } from './error-type';

Expand Down Expand Up @@ -515,6 +515,7 @@ export function isLeapYear1900(year: number): boolean {
const daysInMonthL = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
const daysInMonthR = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

// month is 0 based
export function getDaysInMonth(year: number, month: number): number {
return isLeapYear(year) ? daysInMonthL[month] : daysInMonthR[month];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

import { describe, expect, it } from 'vitest';

import { FUNCTION_NAMES_DATE } from '../../function-names';
import { Datedif } from '../index';
import { BooleanValueObject, NumberValueObject, StringValueObject } from '../../../../engine/value-object/primitive-object';
import { ErrorType } from '../../../../basics/error-type';
import { ArrayValueObject, transformToValueObject } from '../../../../engine/value-object/array-value-object';
import { ErrorValueObject } from '../../../../engine/value-object/base-value-object';
import { ErrorType } from '../../../../basics/error-type';
import { BooleanValueObject, NumberValueObject, StringValueObject } from '../../../../engine/value-object/primitive-object';
import { FUNCTION_NAMES_DATE } from '../../function-names';
import { Datedif } from '../index';

describe('Test datedif function', () => {
const testFunction = new Datedif(FUNCTION_NAMES_DATE.DATEDIF);
Expand Down Expand Up @@ -70,14 +70,32 @@ describe('Test datedif function', () => {
const unit = StringValueObject.create('YY');
const result = testFunction.calculate(startDate, endDate, unit);
expect(result.getValue()).toStrictEqual(ErrorType.NUM);

const unit2 = ErrorValueObject.create(ErrorType.NAME);
const result2 = testFunction.calculate(startDate, endDate, unit2);
expect(result2.getValue()).toStrictEqual(ErrorType.NAME);

const unit3 = BooleanValueObject.create(true);
const result3 = testFunction.calculate(startDate, endDate, unit3);
expect(result3.getValue()).toStrictEqual(ErrorType.NUM);

const unit4 = NumberValueObject.create(11);
const result4 = testFunction.calculate(startDate, endDate, unit4);
expect(result4.getValue()).toStrictEqual(ErrorType.NUM);
});

it('value is error', () => {
const startDate = NumberValueObject.create(1);
const startDate = StringValueObject.create('2011/1/29');
const endDate = ErrorValueObject.create(ErrorType.NAME);
const unit = StringValueObject.create('YY');
const unit = StringValueObject.create('d');
const result = testFunction.calculate(startDate, endDate, unit);
expect(result.getValue()).toStrictEqual(ErrorType.NAME);

const startDate2 = ErrorValueObject.create(ErrorType.NAME);
const endDate2 = StringValueObject.create('2021/3/31');
const unit2 = StringValueObject.create('d');
const result2 = testFunction.calculate(startDate2, endDate2, unit2);
expect(result2.getValue()).toStrictEqual(ErrorType.NAME);
});

it('value is boolean', () => {
Expand All @@ -90,10 +108,16 @@ describe('Test datedif function', () => {

it('value is normal string', () => {
const startDate = StringValueObject.create('test');
const endDate = StringValueObject.create('2021-12-31');
const endDate = StringValueObject.create('2021-3-31');
const unit = StringValueObject.create('d');
const result = testFunction.calculate(startDate, endDate, unit);
expect(result.getValue()).toStrictEqual(ErrorType.VALUE);

const startDate2 = StringValueObject.create('2011-1-29');
const endDate2 = StringValueObject.create('test');
const unit2 = StringValueObject.create('d');
const result2 = testFunction.calculate(startDate2, endDate2, unit2);
expect(result2.getValue()).toStrictEqual(ErrorType.VALUE);
});

it('value is array', () => {
Expand Down Expand Up @@ -138,7 +162,85 @@ describe('Test datedif function', () => {
});

const result = testFunction.calculate(startDate, endDate, unit);
expect(result.getValue()).toStrictEqual(6);
expect(result.getValue()).toStrictEqual(5);
});

it('M more test', () => {
const startDate = StringValueObject.create('2011/1/1');
const endDate = StringValueObject.create('2013/9/1');
const unit = StringValueObject.create('M');
const result = testFunction.calculate(startDate, endDate, unit);
expect(result.getValue()).toStrictEqual(32);

const startDate2 = StringValueObject.create('2014/3/1');
const endDate2 = StringValueObject.create('2016/2/1');
const result2 = testFunction.calculate(startDate2, endDate2, unit);
expect(result2.getValue()).toStrictEqual(23);

const startDate3 = StringValueObject.create('2009/1/2');
const endDate3 = StringValueObject.create('2012/1/1');
const result3 = testFunction.calculate(startDate3, endDate3, unit);
expect(result3.getValue()).toStrictEqual(35);
});

it('MD more test', () => {
const startDate = StringValueObject.create('2010-7-29');
const endDate = StringValueObject.create('2012-1-28');
const unit = StringValueObject.create('MD');
const result = testFunction.calculate(startDate, endDate, unit);
expect(result.getValue()).toStrictEqual(30);

const endDate2 = StringValueObject.create('2012-2-28');
const result2 = testFunction.calculate(startDate, endDate2, unit);
expect(result2.getValue()).toStrictEqual(30);

const endDate3 = StringValueObject.create('2012-3-28');
const result3 = testFunction.calculate(startDate, endDate3, unit);
expect(result3.getValue()).toStrictEqual(28);

const endDate4 = StringValueObject.create('2012-4-28');
const result4 = testFunction.calculate(startDate, endDate4, unit);
expect(result4.getValue()).toStrictEqual(30);

const endDate5 = StringValueObject.create('2012-5-28');
const result5 = testFunction.calculate(startDate, endDate5, unit);
expect(result5.getValue()).toStrictEqual(29);
});

it('YM more test', () => {
const startDate = StringValueObject.create('2011/1/1');
const endDate = StringValueObject.create('2013/9/1');
const unit = StringValueObject.create('YM');
const result = testFunction.calculate(startDate, endDate, unit);
expect(result.getValue()).toStrictEqual(8);

const startDate2 = StringValueObject.create('2014/3/1');
const endDate2 = StringValueObject.create('2016/2/1');
const result2 = testFunction.calculate(startDate2, endDate2, unit);
expect(result2.getValue()).toStrictEqual(11);

const startDate3 = StringValueObject.create('2009/1/2');
const endDate3 = StringValueObject.create('2012/1/1');
const result3 = testFunction.calculate(startDate3, endDate3, unit);
expect(result3.getValue()).toStrictEqual(11);
});

it('YD more test', () => {
const startDate = StringValueObject.create('2011/1/1');
const endDate = StringValueObject.create('2013/9/1');
const unit = StringValueObject.create('YD');
const result = testFunction.calculate(startDate, endDate, unit);
expect(result.getValue()).toStrictEqual(243);

const startDate2 = StringValueObject.create('2014/3/1');
const endDate2 = StringValueObject.create('2016/2/1');
const result2 = testFunction.calculate(startDate2, endDate2, unit);
expect(result2.getValue()).toStrictEqual(337);

const startDate3 = StringValueObject.create('2009/1/2');
const endDate3 = StringValueObject.create('2012/1/1');
const result3 = testFunction.calculate(startDate3, endDate3, unit);
expect(result3.getValue()).toStrictEqual(364);
});
});
});
55 changes: 45 additions & 10 deletions packages/engine-formula/src/functions/date/datedif/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* limitations under the License.
*/

import { excelDateSerial, excelSerialToDate, getDateSerialNumberByObject } from '../../../basics/date';
import { ErrorType } from '../../../basics/error-type';
import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';
import { excelDateSerial, excelSerialToDate, getDateSerialNumberByObject, getDaysInMonth } from '../../../basics/date';
import { ErrorType } from '../../../basics/error-type';
import { type BaseValueObject, ErrorValueObject } from '../../../engine/value-object/base-value-object';
import { NumberValueObject } from '../../../engine/value-object/primitive-object';
import { BaseFunction } from '../../base-function';
Expand Down Expand Up @@ -91,31 +91,66 @@ export class Datedif extends BaseFunction {

const unitValue = `${unit.getValue()}`.toLocaleUpperCase();

let _endDateSerialNumber;
let diff = 0;
let newDate;

switch (unitValue) {
case 'Y':
// The number of complete years in the period.
return NumberValueObject.create(endYear - startYear);
diff = endYear - startYear;

if (endMonth < startMonth || (endMonth === startMonth && endDay < startDay)) {
diff -= 1;
}
break;
case 'M':
// The number of complete months in the period.
return NumberValueObject.create((endYear - startYear) * 12 + endMonth - startMonth);
diff = (endYear - startYear) * 12 + endMonth - startMonth;

if (endDay < startDay) {
diff -= 1;
}
break;
case 'D':
// The number of days in the period.
return NumberValueObject.create(Math.floor(endDateSerialNumber) - Math.floor(startDateSerialNumber));
diff = Math.floor(endDateSerialNumber) - Math.floor(startDateSerialNumber);
break;
case 'MD':
// The difference between the days in start_date and end_date. The months and years of the dates are ignored.
return NumberValueObject.create(endDay - startDay);
diff = endDay - startDay;

if (endDay < startDay) {
newDate = new Date(Date.UTC(endYear, endMonth - 1, 0));
diff += getDaysInMonth(newDate.getUTCFullYear(), newDate.getUTCMonth());
}
break;
case 'YM':
// The difference between the months in start_date and end_date. The days and years of the dates are ignored.
return NumberValueObject.create(endMonth - startMonth);
diff = endMonth - startMonth;

if (endMonth < startMonth || (endMonth === startMonth && endDay < startDay)) {
diff += 12;
}

if (endDay < startDay) {
diff -= 1;
}
break;
case 'YD':
// The difference between the days of start_date and end_date. The years of the dates are ignored.
// The year is the year of the start date
_endDateSerialNumber = excelDateSerial(new Date(Date.UTC(startYear, endMonth - 1, endDay)));
return NumberValueObject.create(Math.floor(_endDateSerialNumber) - Math.floor(startDateSerialNumber));
newDate = new Date(Date.UTC(startYear, endMonth - 1, endDay));

if (endMonth < startMonth || (endMonth === startMonth && endDay < startDay)) {
newDate = new Date(Date.UTC(startYear + 1, endMonth - 1, endDay));
}

diff = Math.floor(excelDateSerial(newDate)) - Math.floor(startDateSerialNumber);
break;
default:
return ErrorValueObject.create(ErrorType.NUM);
}

return NumberValueObject.create(diff);
}
}
2 changes: 2 additions & 0 deletions packages/sheets-conditional-formatting-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,14 @@
"@univerjs/protocol": "0.1.39-alpha.15",
"@univerjs/sheets": "workspace:*",
"@univerjs/sheets-conditional-formatting": "workspace:*",
"@univerjs/sheets-formula-ui": "workspace:*",
"@univerjs/sheets-ui": "workspace:*",
"@univerjs/ui": "workspace:*",
"clsx": "^2.1.1",
"react-grid-layout": "^1.4.4",
"react-resizable": "^3.0.5"
},

"devDependencies": {
"@types/react-grid-layout": "^1.3.5",
"@univerjs-infra/shared": "workspace:*",
Expand Down
Loading

0 comments on commit 254e5da

Please sign in to comment.