Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
fix: optimize the the menu and prefer apply-type of auto-fill (#2638)
YuHongb1e1ef43052f
diff --git a/packages/sheets-data-validation/src/controllers/dv-auto-fill.controller.ts b/packages/sheets-data-validation/src/controllers/dv-auto-fill.controller.tsindex c62505ae0d..305ba35dba 100644--- a/packages/sheets-data-validation/src/controllers/dv-auto-fill.controller.ts+++ b/packages/sheets-data-validation/src/controllers/dv-auto-fill.controller.ts@@ -132,7 +132,6 @@ export class DataValidationAutoFillController extends Disposable {return noopReturnFunc();},onAfterFillData: () => {- this._autoFillService.setDisableApplyType(APPLY_TYPE.SERIES, false);},};this.disposeWithMe(this._autoFillService.addHook(hook));diff --git a/packages/sheets-ui/src/controllers/auto-fill.controller.ts b/packages/sheets-ui/src/controllers/auto-fill.controller.tsindex bc47b1f77b..d2657cc0e0 100644--- a/packages/sheets-ui/src/controllers/auto-fill.controller.ts+++ b/packages/sheets-ui/src/controllers/auto-fill.controller.ts@@ -107,7 +107,7 @@ export class AutoFillController extends Disposable {type: AutoFillHookType.Default,priority: 0,onBeforeFillData: (location: IAutoFillLocation, direction: Direction) => {- this._presetAndCacheData(location, direction);+ return this._presetAndCacheData(location, direction);},onFillData: (location: IAutoFillLocation, direction: Direction, applyType: APPLY_TYPE) => {return this._fillData(location, direction, applyType);@@ -364,16 +364,27 @@ export class AutoFillController extends Disposable {subUnitId,};+ const preferTypes: APPLY_TYPE[] = [];const activeHooks = this._autoFillService.getActiveHooks();activeHooks.forEach((hook) => {- hook?.onBeforeFillData?.({ source: autoFillSource, target: autoFillTarget, unitId, subUnitId }, direction!);+ const type = hook?.onBeforeFillData?.({ source: autoFillSource, target: autoFillTarget, unitId, subUnitId }, direction!);+ if (type) {+ preferTypes.unshift(type);+ }});// set apply type will trigger fillData- if (!this._autoFillService.menu.find((m) => m.value === APPLY_TYPE.SERIES)?.disable) {- this._autoFillService.applyType = APPLY_TYPE.SERIES;- } else {- this._autoFillService.applyType = APPLY_TYPE.COPY;+ for (let i = 0; i < preferTypes.length; i++) {+ const menuItem = this._autoFillService.menu.find((m) => m.value === preferTypes[i]);+ if (menuItem && !menuItem.disable) {+ this._autoFillService.applyType = preferTypes[i];+ return;+ }+ }+ // if no hook return apply type, use first available apply type+ const first = this._autoFillService.menu.find((m) => m.disable === false)?.value;+ if (first) {+ this._autoFillService.applyType = first;}}@@ -544,12 +555,6 @@ export class AutoFillController extends Disposable {}private _getCopyData(source: IDiscreteRange, direction: Direction) {- // const {- // startRow: copyStartRow,- // startColumn: copyStartColumn,- // endRow: copyEndRow,- // endColumn: copyEndColumn,- // } = source;const worksheet = this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!.getActiveSheet();@@ -571,6 +576,11 @@ export class AutoFillController extends Disposable {aArray = source.rows;bArray = source.cols;}++ const accessor = {+ get: this._injector.get.bind(this._injector),+ };+aArray.forEach((a) => {// a copyDataPiece is an array of original cells in same column or row, depending on direction (horizontal or vertical)const copyDataPiece = this._getEmptyCopyDataPiece();@@ -585,7 +595,7 @@ export class AutoFillController extends Disposable {} else {data = currentCellDatas.getValue(a, b);}- const { type, isContinue } = rules.find((r) => r.match(data)) || otherRule;+ const { type, isContinue } = rules.find((r) => r.match(data, accessor)) || otherRule;if (isContinue(prevData, data)) {const typeInfo = copyDataPiece[type];@@ -725,11 +735,13 @@ export class AutoFillController extends Disposable {this._beforeApplyData = applyData;this._copyData = this._getCopyData(source, direction);this._currentLocation = location;- if (this._hasSeries(this._copyData)) {- this._autoFillService.setDisableApplyType(APPLY_TYPE.SERIES, false);- } else {+ if (this._shouldDisableSeries(this._copyData)) {this._autoFillService.setDisableApplyType(APPLY_TYPE.SERIES, true);+ return APPLY_TYPE.COPY;+ } else {+ this._autoFillService.setDisableApplyType(APPLY_TYPE.SERIES, false);}+ return this._getPreferredApplyType(this._copyData);}// auto fill entry@@ -937,18 +949,28 @@ export class AutoFillController extends Disposable {};}- private _hasSeries(copyData: ICopyDataPiece[]) {- return copyData.some((copyDataPiece) => {- const res = Object.keys(copyDataPiece).some((type) => {- if (- copyDataPiece[type as DATA_TYPE]?.length &&- ![DATA_TYPE.OTHER, DATA_TYPE.FORMULA].includes(type as DATA_TYPE)- ) {- return true;- }- return false;- });- return res;- });+ private _shouldDisableSeries(copyData: ICopyDataPiece[]) {+ // only formula or other type data, disable series+ return copyData.every((copyDataPiece) =>+ Object.keys(copyDataPiece).every((type) => (+ copyDataPiece[type as DATA_TYPE]?.length === 0+ || [DATA_TYPE.OTHER, DATA_TYPE.FORMULA].includes(type as DATA_TYPE)+ )+ )+ );+ }++ private _getPreferredApplyType(copyData: ICopyDataPiece[]) {+ // if all data is number and only one cell in piece, prefer copy+ const preferCopy = copyData.every((copyDataPiece) =>+ Object.keys(copyDataPiece).every((type) => (+ copyDataPiece[type as DATA_TYPE]?.length === 0+ || (copyDataPiece[type as DATA_TYPE]?.length === 1+ && copyDataPiece[type as DATA_TYPE][0].data.length === 1+ && DATA_TYPE.NUMBER === type as DATA_TYPE)+ )+ )+ );+ return preferCopy ? APPLY_TYPE.COPY : APPLY_TYPE.SERIES;}}diff --git a/packages/sheets-ui/src/services/auto-fill/auto-fill.service.ts b/packages/sheets-ui/src/services/auto-fill/auto-fill.service.tsindex dd42ea3687..2c4003674a 100644--- a/packages/sheets-ui/src/services/auto-fill/auto-fill.service.ts+++ b/packages/sheets-ui/src/services/auto-fill/auto-fill.service.ts@@ -39,6 +39,7 @@ import {chnNumberRule,chnWeek2Rule,chnWeek3Rule,+ dateRule,extendNumberRule,loopSeriesRule,numberRule,@@ -137,6 +138,7 @@ export class AutoFillService extends Disposable implements IAutoFillService {private _init() {this._rules = [+ dateRule,numberRule,extendNumberRule,chnNumberRule,diff --git a/packages/sheets-ui/src/services/auto-fill/rules.ts b/packages/sheets-ui/src/services/auto-fill/rules.tsindex 22da9956af..ac567e1f5e 100644--- a/packages/sheets-ui/src/services/auto-fill/rules.ts+++ b/packages/sheets-ui/src/services/auto-fill/rules.ts@@ -14,7 +14,9 @@* limitations under the License.*/-import { CellValueType, Direction } from '@univerjs/core';+import type { Workbook } from '@univerjs/core';+import { CellValueType, Direction, IUniverInstanceService } from '@univerjs/core';+import numfmt from '@univerjs/engine-numfmt';import {chineseToNumber,@@ -35,6 +37,43 @@ import {import type { IAutoFillRule } from './type';import { APPLY_TYPE, DATA_TYPE } from './type';+export const dateRule: IAutoFillRule = {+ type: DATA_TYPE.DATE,+ priority: 1100,+ match: (cellData, accessor) => {+ if ((typeof cellData?.v === 'number' || cellData?.t === CellValueType.NUMBER)+ && cellData.s) {+ if (typeof cellData.s === 'string') {+ const workbook = accessor.get(IUniverInstanceService).getFocusedUnit() as Workbook;+ const style = workbook.getStyles().get(cellData.s);+ const pattern = style?.n?.pattern;+ if (pattern) {+ return numfmt.getInfo(pattern).isDate;+ }+ } else if (cellData.s.n && numfmt.getInfo(cellData.s.n.pattern).isDate) {+ return true;+ }+ }+ return false;+ },+ isContinue: (prev, cur) => {+ if (prev.type === DATA_TYPE.DATE) {+ return true;+ }+ return false;+ },+ applyFunctions: {+ [APPLY_TYPE.SERIES]: (dataWithIndex, len, direction) => {+ const { data } = dataWithIndex;+ if (direction === Direction.LEFT || direction === Direction.UP) {+ data.reverse();+ return fillSeries(data, len, direction).reverse();+ }+ return fillSeries(data, len, direction);+ },+ },+};+export const numberRule: IAutoFillRule = {type: DATA_TYPE.NUMBER,priority: 1000,diff --git a/packages/sheets-ui/src/services/auto-fill/tools.ts b/packages/sheets-ui/src/services/auto-fill/tools.tsindex ba529ae878..434a314569 100644--- a/packages/sheets-ui/src/services/auto-fill/tools.ts+++ b/packages/sheets-ui/src/services/auto-fill/tools.ts@@ -444,7 +444,7 @@ export function forecast(x: number, yArr: number[], xArr: number[], forward = trlet b;if (sum_n === 0) {- b = 0;+ b = forward ? 1 : -1;} else {b = sum_d / sum_n;}diff --git a/packages/sheets-ui/src/services/auto-fill/type.ts b/packages/sheets-ui/src/services/auto-fill/type.tsindex b34a7cb626..99616cce8f 100644--- a/packages/sheets-ui/src/services/auto-fill/type.ts+++ b/packages/sheets-ui/src/services/auto-fill/type.ts@@ -15,6 +15,7 @@*/import type { Direction, ICellData, IMutationInfo, Nullable } from '@univerjs/core';+import type { IAccessor } from '@wendellhu/redi';import type { IDiscreteRange } from '../../controllers/utils/range-tools';export enum AutoFillHookType {@@ -35,7 +36,7 @@ export interface ISheetAutoFillHook {type?: AutoFillHookType;bindUnit?: string;disable?: (location: IAutoFillLocation, direction: Direction, applyType: APPLY_TYPE) => boolean;- onBeforeFillData?(location: IAutoFillLocation, direction: Direction): boolean | void;+ onBeforeFillData?(location: IAutoFillLocation, direction: Direction): APPLY_TYPE | void;onFillData?(location: IAutoFillLocation,direction: Direction,@@ -72,7 +73,7 @@ export type ICopyDataInTypeIndexInfo = number[];export interface IAutoFillRule {type: string;- match: (cellData: Nullable<ICellData>) => boolean;+ match: (cellData: Nullable<ICellData>, accessor: IAccessor) => boolean;isContinue: (prev: IRuleConfirmedData, cur: Nullable<ICellData>) => boolean;applyFunctions?: APPLY_FUNCTIONS;priority: number;@@ -98,8 +99,8 @@ export type APPLY_FUNCTIONS = {};export enum APPLY_TYPE {- COPY = '0',- SERIES = '1',- ONLY_FORMAT = '2',- NO_FORMAT = '3',+ COPY = 'COPY',+ SERIES = 'SERIES',+ ONLY_FORMAT = 'ONLY_FORMAT',+ NO_FORMAT = 'NO_FORMAT',}