@univerjs/sheets 1.1.0 → 1.2.0

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.
package/lib/index.js CHANGED
@@ -1,6 +1,6 @@
1
- import { AUTO_HEIGHT_FOR_MERGED_CELLS, BORDER_KEYS, BORDER_STYLE_KEYS, BooleanNumber, BorderStyleTypes, BorderType, BuildTextUtils, COLOR_STYLE_KEYS, CellModeEnum, CellValueType, CommandType, CustomCommandExecutionError, DependentOn, Dimension, Direction, Disposable, DisposableCollection, DocumentDataModel, ErrorService, FontItalic, FontWeight, HorizontalAlign, IAuthzIoService, ICommandService, IConfigService, IConfirmService, IContextService, ILogService, IPermissionService, IResourceManagerService, IS_ROW_STYLE_PRECEDE_COLUMN_STYLE, IUndoRedoService, IUniverInstanceService, Inject, Injector, InterceptorEffectEnum, InterceptorManager, LRUMap, LocaleService, MAX_COLUMN_COUNT, MAX_ROW_COUNT, ObjectMatrix, Optional, PADDING_KEYS, PermissionStatus, Plugin, RANGE_TYPE, RTree, Range, Rectangle, RxDisposable, SHEET_TEXT_LINE_GAP, STYLE_KEYS, Styles, TEXT_DECORATION_KEYS, TEXT_ROTATION_KEYS, TextX, Tools, UniverInstanceType, UserManagerService, WorksheetHiddenState, WrapStrategy, cellToRange, cloneWorksheetData, composeInterceptors, concatMatrixArray, createIdentifier, createInterceptorKey, createRowColIter, generateRandomId, getArrayLength, getIntersectRange, getNumfmtLocaleTag, getNumfmtParseValueFilter, isBooleanString, isDefaultFormat, isFormulaId, isFormulaString, isICellData, isRealNum, isSafeNumeric, isTextFormat, mapObjectMatrix, merge, mergeIntervals, mergeOverrideWithDependencies, mergeWorksheetSnapshotWithDefault, moveMatrixArray, normalizeTextRuns, normalizeWorkbookCalculationSettings, numfmt, regexp, registerDependencies, remove, selectionToArray, sequenceExecute, sliceMatrixArray, spliceArray, throttle, toDisposable, touchDependencies, willLoseNumericPrecision } from "@univerjs/core";
1
+ import { AUTO_HEIGHT_FOR_MERGED_CELLS, BORDER_KEYS, BORDER_STYLE_KEYS, BooleanNumber, BorderStyleTypes, BorderType, BuildTextUtils, COLOR_STYLE_KEYS, CellModeEnum, CellValueType, CommandType, CustomCommandExecutionError, DependentOn, Dimension, Direction, Disposable, DisposableCollection, DocumentDataModel, ErrorService, FontItalic, FontWeight, HorizontalAlign, IAuthzIoService, ICommandService, IConfigService, IConfirmService, IContextService, ILogService, IPermissionService, IResourceManagerService, IS_ROW_STYLE_PRECEDE_COLUMN_STYLE, IUndoRedoService, IUniverInstanceService, Inject, Injector, InterceptorEffectEnum, InterceptorManager, LRUMap, LocaleService, MAX_COLUMN_COUNT, MAX_ROW_COUNT, ObjectMatrix, Optional, PADDING_KEYS, PermissionStatus, Plugin, RANGE_TYPE, RTree, Range, Rectangle, RxDisposable, SHEET_TEXT_LINE_GAP, STYLE_KEYS, Styles, TEXT_DECORATION_KEYS, TEXT_ROTATION_KEYS, TextX, Tools, UniverInstanceType, UserManagerService, WorksheetHiddenState, WrapStrategy, cellToRange, cloneWorksheetData, composeInterceptors, concatMatrixArray, createIdentifier, createInterceptorKey, createRowColIter, generateRandomId, getArrayLength, getIntersectRange, getNumfmtLocaleTag, getNumfmtParseValueFilter, isBooleanString, isDefaultFormat, isFormulaId, isFormulaString, isICellData, isRealNum, isSafeNumeric, isTextFormat, mapObjectMatrix, merge, mergeIntervals, mergeOverrideWithDependencies, mergeWorksheetSnapshotWithDefault, moveMatrixArray, moveRangeByOffset, normalizeTextRuns, normalizeWorkbookCalculationSettings, numfmt, regexp, registerDependencies, remove, selectionToArray, sequenceExecute, sliceMatrixArray, spliceArray, throttle, toDisposable, touchDependencies, willLoseNumericPrecision } from "@univerjs/core";
2
2
  import { BehaviorSubject, Subject, distinctUntilChanged, filter, map, merge as merge$1, of, share, shareReplay, skip, switchMap, take, takeUntil } from "rxjs";
3
- import { IDefinedNamesService, LexerTreeBuilder, RemoveDefinedNameMutation, SetDefinedNameMutation, SetDefinedNameMutationFactory, SetFormulaCalculationResultMutation, UniverFormulaEnginePlugin, deserializeRangeWithSheet, deserializeRangeWithSheetWithCache, handleNumfmtInCell, isReferenceStringWithEffectiveColumn, operatorToken, sequenceNodeType, stripErrorMargin } from "@univerjs/engine-formula";
3
+ import { FormulaDataModel, IDefinedNamesService, IFunctionService, LexerTreeBuilder, RemoveDefinedNameMutation, SetDefinedNameMutation, SetDefinedNameMutationFactory, SetFormulaCalculationResultMutation, UniverFormulaEnginePlugin, deserializeRangeWithSheetWithCache, handleNumfmtInCell, handleRefStringInfo, isReferenceStringWithEffectiveColumn, matchToken, normalizeFormulaUnitName, operatorToken, sequenceNodeType, stripErrorMargin } from "@univerjs/engine-formula";
4
4
  import { SpreadsheetSkeleton, cjk, precisionTo } from "@univerjs/engine-render";
5
5
  import { UnitAction, UnitObject } from "@univerjs/protocol";
6
6
  import { filter as filter$1, map as map$1, takeUntil as takeUntil$1 } from "rxjs/operators";
@@ -11971,6 +11971,7 @@ let AutoFillService = class AutoFillService extends Disposable {
11971
11971
  direction = Direction.RIGHT;
11972
11972
  target.startColumn = source.endColumn + 1;
11973
11973
  } else return false;
11974
+ this.setShowMenu(false);
11974
11975
  this.direction = direction;
11975
11976
  const autoFillSource = this._injector.invoke((accessor) => rangeToDiscreteRange(source, accessor, unitId, subUnitId));
11976
11977
  const autoFillTarget = this._injector.invoke((accessor) => rangeToDiscreteRange(target, accessor, unitId, subUnitId));
@@ -12023,7 +12024,7 @@ let AutoFillService = class AutoFillService extends Disposable {
12023
12024
  getAllHooks() {
12024
12025
  return this._hooks;
12025
12026
  }
12026
- getActiveHooks() {
12027
+ getActiveHooks(applyType = this.applyType) {
12027
12028
  const { source, target, unitId, subUnitId } = this.autoFillLocation || {};
12028
12029
  if (!source || !target || !unitId || !subUnitId) return [];
12029
12030
  const enabledHooks = this._hooks.filter((h) => {
@@ -12033,7 +12034,7 @@ let AutoFillService = class AutoFillService extends Disposable {
12033
12034
  target,
12034
12035
  unitId,
12035
12036
  subUnitId
12036
- }, this._direction, this.applyType)) === true;
12037
+ }, this._direction, applyType)) === true;
12037
12038
  });
12038
12039
  const onlyHooks = enabledHooks.filter((h) => h.type === "ONLY");
12039
12040
  if (onlyHooks.length > 0) return this._getOneByPriority(onlyHooks);
@@ -12082,7 +12083,6 @@ let AutoFillService = class AutoFillService extends Disposable {
12082
12083
  }
12083
12084
  fillData(applyType) {
12084
12085
  var _activeWorkbook$getAc;
12085
- this.applyType = applyType;
12086
12086
  const activeWorkbook = this._univerInstanceService.getCurrentUnitOfType(UniverInstanceType.UNIVER_SHEET);
12087
12087
  const activeUnitId = activeWorkbook === null || activeWorkbook === void 0 ? void 0 : activeWorkbook.getUnitId();
12088
12088
  const activeSubUnitId = activeWorkbook === null || activeWorkbook === void 0 || (_activeWorkbook$getAc = activeWorkbook.getActiveSheet()) === null || _activeWorkbook$getAc === void 0 ? void 0 : _activeWorkbook$getAc.getSheetId();
@@ -12093,12 +12093,30 @@ let AutoFillService = class AutoFillService extends Disposable {
12093
12093
  const worksheet = workbook.getSheetBySheetId(subUnitId);
12094
12094
  if (!worksheet) return false;
12095
12095
  const direction = this.direction;
12096
+ const activeHooks = this.getActiveHooks(applyType);
12097
+ const fillCallbacks = activeHooks.map((hook) => {
12098
+ var _hook$onPrepareFillDa;
12099
+ return ((_hook$onPrepareFillDa = hook.onPrepareFillData) === null || _hook$onPrepareFillDa === void 0 ? void 0 : _hook$onPrepareFillDa.call(hook, {
12100
+ source,
12101
+ target,
12102
+ unitId,
12103
+ subUnitId
12104
+ }, direction, applyType)) ?? (() => {
12105
+ var _hook$onFillData;
12106
+ return (_hook$onFillData = hook.onFillData) === null || _hook$onFillData === void 0 ? void 0 : _hook$onFillData.call(hook, {
12107
+ source,
12108
+ target,
12109
+ unitId,
12110
+ subUnitId
12111
+ }, direction, applyType);
12112
+ });
12113
+ });
12114
+ this.applyType = applyType;
12096
12115
  if (this._prevUndos.length > 0) this._prevUndos.forEach((undo) => {
12097
12116
  this._commandService.syncExecuteCommand(undo.id, undo.params);
12098
12117
  });
12099
12118
  this._prevUndos = [];
12100
12119
  const selection = Rectangle.union(discreteRangeToRange(source), discreteRangeToRange(target));
12101
- const activeHooks = this.getActiveHooks();
12102
12120
  if (unitId === activeUnitId && subUnitId === activeSubUnitId) this._commandService.syncExecuteCommand(SetSelectionsOperation.id, {
12103
12121
  selections: [{
12104
12122
  primary: getPrimaryForRange({
@@ -12117,14 +12135,8 @@ let AutoFillService = class AutoFillService extends Disposable {
12117
12135
  });
12118
12136
  const undos = [];
12119
12137
  const redos = [];
12120
- activeHooks.forEach((hook) => {
12121
- var _hook$onFillData;
12122
- const { undos: hookUndos, redos: hookRedos } = ((_hook$onFillData = hook.onFillData) === null || _hook$onFillData === void 0 ? void 0 : _hook$onFillData.call(hook, {
12123
- source,
12124
- target,
12125
- unitId,
12126
- subUnitId
12127
- }, direction, applyType)) || {};
12138
+ fillCallbacks.forEach((fill) => {
12139
+ const { undos: hookUndos, redos: hookRedos } = fill() || {};
12128
12140
  if (hookUndos) undos.push(...hookUndos);
12129
12141
  if (hookRedos) redos.push(...hookRedos);
12130
12142
  });
@@ -17472,8 +17484,9 @@ let AutoFillController = class AutoFillController extends Disposable {
17472
17484
  onBeforeFillData: (location, direction) => {
17473
17485
  return this._presetAndCacheData(location, direction);
17474
17486
  },
17475
- onFillData: (location, direction, applyType) => {
17476
- return this._fillData(location, direction, applyType);
17487
+ onPrepareFillData: (location, direction, applyType) => {
17488
+ const prepared = this._prepareFillData(location, direction, applyType);
17489
+ return () => this._fillData(location, direction, applyType, prepared);
17477
17490
  }
17478
17491
  }));
17479
17492
  }
@@ -17522,7 +17535,7 @@ let AutoFillController = class AutoFillController extends Disposable {
17522
17535
  const arrData = this._applyFunctions(copySquad, len, direction, applyType, customApplyFunctions, copyDataPiece, location);
17523
17536
  const arrIndex = getDataIndex(csLen, asLen, copySquad.index);
17524
17537
  applyDataInTypes[type].push({
17525
- data: arrData,
17538
+ data: arrData ?? [],
17526
17539
  index: arrIndex
17527
17540
  });
17528
17541
  });
@@ -17681,28 +17694,9 @@ let AutoFillController = class AutoFillController extends Disposable {
17681
17694
  }
17682
17695
  return applyMergeRanges;
17683
17696
  }
17684
- _fillData(location, direction, applyType) {
17685
- var _this$_univerInstance4;
17686
- const undos = [];
17687
- const redos = [];
17688
- let hasStyle = true;
17689
- if (applyType === "NO_FORMAT") {
17690
- hasStyle = false;
17691
- applyType = "SERIES";
17692
- }
17693
- const { source, target, unitId, subUnitId } = location;
17694
- if (!source || !target || direction == null) return {
17695
- undos,
17696
- redos
17697
- };
17698
- const sourceRange = discreteRangeToRange(source);
17699
- const targetRange = discreteRangeToRange(target);
17700
- const { cols: targetCols, rows: targetRows } = target;
17701
- const { cols: sourceCols, rows: sourceRows } = source;
17702
- const copyData = this._copyData;
17703
- let csLen;
17704
- if (direction === Direction.DOWN || direction === Direction.UP) csLen = sourceRows.length;
17705
- else csLen = sourceCols.length;
17697
+ _getFillCells(location, direction, applyType, hasStyle, csLen) {
17698
+ const { rows: targetRows, cols: targetCols } = location.target;
17699
+ const copyData = Tools.deepClone(this._copyData);
17706
17700
  const applyDatas = [];
17707
17701
  if (direction === Direction.DOWN || direction === Direction.UP) {
17708
17702
  const asLen = targetRows.length;
@@ -17733,20 +17727,26 @@ let AutoFillController = class AutoFillController extends Disposable {
17733
17727
  applyDatas.push(row);
17734
17728
  });
17735
17729
  }
17736
- let applyMergeRanges = [];
17730
+ return applyDatas;
17731
+ }
17732
+ _prepareFillData(location, direction, applyType) {
17733
+ var _this$_univerInstance4;
17734
+ const hasStyle = applyType !== "NO_FORMAT";
17735
+ const effectiveApplyType = hasStyle ? applyType : "SERIES";
17736
+ const { source, target, unitId } = location;
17737
+ const sourceRange = discreteRangeToRange(source);
17738
+ const targetRange = discreteRangeToRange(target);
17739
+ const { cols: targetCols, rows: targetRows } = target;
17740
+ const { cols: sourceCols, rows: sourceRows } = source;
17741
+ let csLen;
17742
+ if (direction === Direction.DOWN || direction === Direction.UP) csLen = sourceRows.length;
17743
+ else csLen = sourceCols.length;
17744
+ const applyDatas = this._getFillCells(location, direction, effectiveApplyType, hasStyle, csLen);
17745
+ const applyMergeRanges = hasStyle ? this._getMergeApplyData(sourceRange, targetRange, direction, csLen, location) : [];
17737
17746
  const style = (_this$_univerInstance4 = this._univerInstanceService.getUnit(unitId)) === null || _this$_univerInstance4 === void 0 ? void 0 : _this$_univerInstance4.getStyles();
17738
- if (hasStyle) {
17739
- applyMergeRanges = this._getMergeApplyData(sourceRange, targetRange, direction, csLen, location);
17740
- applyDatas.forEach((row) => {
17741
- row.forEach((cellData) => {
17742
- if (cellData && style) {
17743
- if (style) cellData.s = style.getStyleByCell(cellData);
17744
- }
17745
- });
17746
- });
17747
- } else applyDatas.forEach((row, rowIndex) => {
17747
+ applyDatas.forEach((row, rowIndex) => {
17748
17748
  row.forEach((cellData, colIndex) => {
17749
- if (cellData && style) cellData.s = style.getStyleByCell(this._beforeApplyData[rowIndex][colIndex]) || null;
17749
+ if (cellData && style) cellData.s = hasStyle ? style.getStyleByCell(cellData) : style.getStyleByCell(this._beforeApplyData[rowIndex][colIndex]) || null;
17750
17750
  });
17751
17751
  });
17752
17752
  if (applyType === "ONLY_FORMAT") applyDatas.forEach((row, rowIndex) => {
@@ -17762,30 +17762,59 @@ let AutoFillController = class AutoFillController extends Disposable {
17762
17762
  }
17763
17763
  });
17764
17764
  });
17765
- if (hasStyle) {
17766
- var _this$_univerInstance5;
17767
- const deleteMergeRanges = [];
17768
- const mergeData = (_this$_univerInstance5 = this._univerInstanceService.getUnit(unitId, UniverInstanceType.UNIVER_SHEET)) === null || _this$_univerInstance5 === void 0 || (_this$_univerInstance5 = _this$_univerInstance5.getSheetBySheetId(subUnitId)) === null || _this$_univerInstance5 === void 0 ? void 0 : _this$_univerInstance5.getMergeData();
17769
- if (mergeData) mergeData.forEach((merge) => {
17770
- if (Rectangle.intersects(merge, targetRange)) deleteMergeRanges.push(merge);
17765
+ const cellValue = new ObjectMatrix();
17766
+ targetRows.forEach((row, rowIndex) => {
17767
+ targetCols.forEach((col, colIndex) => {
17768
+ const cell = applyDatas[rowIndex][colIndex];
17769
+ if (cell) cellValue.setValue(row, col, cell);
17770
+ });
17771
+ });
17772
+ const cellValueMatrix = cellValue.getMatrix();
17773
+ this._autoFillService.getActiveHooks(applyType).forEach((hook) => {
17774
+ var _hook$onValidateFillD;
17775
+ (_hook$onValidateFillD = hook.onValidateFillData) === null || _hook$onValidateFillD === void 0 || _hook$onValidateFillD.call(hook, location, direction, applyType, cellValueMatrix);
17776
+ });
17777
+ return {
17778
+ cellValue: cellValueMatrix,
17779
+ applyMergeRanges,
17780
+ hasStyle
17781
+ };
17782
+ }
17783
+ _getRemoveMergeMutations(location) {
17784
+ var _this$_univerInstance5;
17785
+ const { unitId, subUnitId, target } = location;
17786
+ const targetRange = discreteRangeToRange(target);
17787
+ const undos = [];
17788
+ const redos = [];
17789
+ const deleteMergeRanges = ((_this$_univerInstance5 = this._univerInstanceService.getUnit(unitId, UniverInstanceType.UNIVER_SHEET)) === null || _this$_univerInstance5 === void 0 || (_this$_univerInstance5 = _this$_univerInstance5.getSheetBySheetId(subUnitId)) === null || _this$_univerInstance5 === void 0 ? void 0 : _this$_univerInstance5.getMergeData().filter((merge) => Rectangle.intersects(merge, targetRange))) ?? [];
17790
+ if (deleteMergeRanges.length) {
17791
+ const removeMergeMutationParams = {
17792
+ unitId,
17793
+ subUnitId,
17794
+ ranges: deleteMergeRanges
17795
+ };
17796
+ const undoRemoveMergeMutationParams = this._injector.invoke(RemoveMergeUndoMutationFactory, removeMergeMutationParams);
17797
+ redos.push({
17798
+ id: RemoveWorksheetMergeMutation.id,
17799
+ params: removeMergeMutationParams
17800
+ });
17801
+ undos.unshift({
17802
+ id: AddWorksheetMergeMutation.id,
17803
+ params: undoRemoveMergeMutationParams
17771
17804
  });
17772
- if (deleteMergeRanges.length) {
17773
- const removeMergeMutationParams = {
17774
- unitId,
17775
- subUnitId,
17776
- ranges: deleteMergeRanges
17777
- };
17778
- const undoRemoveMergeMutationParams = this._injector.invoke(RemoveMergeUndoMutationFactory, removeMergeMutationParams);
17779
- redos.push({
17780
- id: RemoveWorksheetMergeMutation.id,
17781
- params: removeMergeMutationParams
17782
- });
17783
- undos.unshift({
17784
- id: AddWorksheetMergeMutation.id,
17785
- params: undoRemoveMergeMutationParams
17786
- });
17787
- }
17788
17805
  }
17806
+ return {
17807
+ undos,
17808
+ redos
17809
+ };
17810
+ }
17811
+ _fillData(location, direction, applyType, prepared) {
17812
+ const { target, unitId, subUnitId } = location;
17813
+ const { cellValue, applyMergeRanges, hasStyle } = prepared;
17814
+ const { undos, redos } = hasStyle ? this._getRemoveMergeMutations(location) : {
17815
+ undos: [],
17816
+ redos: []
17817
+ };
17789
17818
  const clearMutationParams = {
17790
17819
  subUnitId,
17791
17820
  unitId,
@@ -17800,22 +17829,15 @@ let AutoFillController = class AutoFillController extends Disposable {
17800
17829
  id: SetRangeValuesMutation.id,
17801
17830
  params: undoClearMutationParams
17802
17831
  });
17803
- const cellValue = new ObjectMatrix();
17804
- targetRows.forEach((row, rowIndex) => {
17805
- targetCols.forEach((col, colIndex) => {
17806
- if (applyDatas[rowIndex][colIndex]) cellValue.setValue(row, col, applyDatas[rowIndex][colIndex]);
17807
- });
17808
- });
17809
- const cellValueMatrix = cellValue.getMatrix();
17810
17832
  const setRangeValuesMutationParams = {
17811
17833
  subUnitId,
17812
17834
  unitId,
17813
- cellValue: cellValueMatrix
17835
+ cellValue
17814
17836
  };
17815
17837
  const undoSetRangeValuesMutationParams = this._injector.invoke(SetRangeValuesUndoMutationFactory, setRangeValuesMutationParams);
17816
- this._autoFillService.getActiveHooks().forEach((hook) => {
17838
+ this._autoFillService.getActiveHooks(applyType).forEach((hook) => {
17817
17839
  var _hook$onBeforeSubmit;
17818
- (_hook$onBeforeSubmit = hook.onBeforeSubmit) === null || _hook$onBeforeSubmit === void 0 || _hook$onBeforeSubmit.call(hook, location, direction, applyType, cellValueMatrix);
17840
+ (_hook$onBeforeSubmit = hook.onBeforeSubmit) === null || _hook$onBeforeSubmit === void 0 || _hook$onBeforeSubmit.call(hook, location, direction, applyType, cellValue);
17819
17841
  });
17820
17842
  undos.unshift({
17821
17843
  id: SetRangeValuesMutation.id,
@@ -18172,7 +18194,7 @@ SheetsFreezeSyncController = __decorate([
18172
18194
  //#endregion
18173
18195
  //#region src/controllers/permission/sheet-permission-check.controller.ts
18174
18196
  let SheetPermissionCheckController = class SheetPermissionCheckController extends Disposable {
18175
- constructor(_commandService, _univerInstanceService, _permissionService, _selectionManagerService, _rangeProtectionRuleModel, _worksheetProtectionRuleModel, _localeService, _lexerTreeBuilder, _definedNamesService) {
18197
+ constructor(_commandService, _univerInstanceService, _permissionService, _selectionManagerService, _rangeProtectionRuleModel, _worksheetProtectionRuleModel, _localeService, _lexerTreeBuilder, _definedNamesService, _autoFillService, _formulaDataModel, _functionService) {
18176
18198
  super();
18177
18199
  this._commandService = _commandService;
18178
18200
  this._univerInstanceService = _univerInstanceService;
@@ -18183,6 +18205,9 @@ let SheetPermissionCheckController = class SheetPermissionCheckController extend
18183
18205
  this._localeService = _localeService;
18184
18206
  this._lexerTreeBuilder = _lexerTreeBuilder;
18185
18207
  this._definedNamesService = _definedNamesService;
18208
+ this._autoFillService = _autoFillService;
18209
+ this._formulaDataModel = _formulaDataModel;
18210
+ this._functionService = _functionService;
18186
18211
  _defineProperty(this, "disposableCollection", new DisposableCollection());
18187
18212
  _defineProperty(this, "_triggerPermissionUIEvent$", new Subject());
18188
18213
  _defineProperty(this, "triggerPermissionUIEvent$", this._triggerPermissionUIEvent$.asObservable());
@@ -18190,6 +18215,29 @@ let SheetPermissionCheckController = class SheetPermissionCheckController extend
18190
18215
  }
18191
18216
  _initialize() {
18192
18217
  this._commandExecutedListener();
18218
+ this.disposeWithMe(this._autoFillService.addHook({
18219
+ id: "permission-check",
18220
+ onValidateFillData: ({ unitId, subUnitId }, _direction, applyType, cellValue) => {
18221
+ if (applyType === "ONLY_FORMAT") return;
18222
+ const formulas = new ObjectMatrix(this._formulaDataModel.updateFormulaData(unitId, subUnitId, cellValue));
18223
+ new ObjectMatrix(cellValue).forValue((row, column) => {
18224
+ const formula = formulas.getValue(row, column);
18225
+ if (!(formula === null || formula === void 0 ? void 0 : formula.f)) return;
18226
+ const formulaString = this._lexerTreeBuilder.moveFormulaRefOffset(formula.f, formula.x ?? 0, formula.y ?? 0);
18227
+ if (!this._permissionCheckWithFormula({
18228
+ unitId,
18229
+ subUnitId,
18230
+ range: {
18231
+ startRow: row,
18232
+ endRow: row,
18233
+ startColumn: column,
18234
+ endColumn: column
18235
+ },
18236
+ value: { f: formulaString }
18237
+ })) this.blockExecuteWithoutPermission(this._localeService.t("sheets.permission.dialog.formulaErr"));
18238
+ });
18239
+ }
18240
+ }));
18193
18241
  }
18194
18242
  _commandExecutedListener() {
18195
18243
  this.disposeWithMe(this._commandService.beforeCommandExecuted((commandInfo) => {
@@ -18514,61 +18562,18 @@ let SheetPermissionCheckController = class SheetPermissionCheckController extend
18514
18562
  _permissionCheckWithFormula(params) {
18515
18563
  const target = getSheetCommandTarget(this._univerInstanceService, params);
18516
18564
  if (!target) return false;
18517
- const { workbook, unitId, subUnitId } = target;
18565
+ const { unitId, subUnitId } = target;
18518
18566
  const formulaString = params.value.f;
18519
18567
  if (formulaString) {
18520
- const definedNameStr = formulaString.substring(1);
18521
- const definedName = this._definedNamesService.getValueByName(unitId, definedNameStr);
18522
- if (definedName) {
18523
- let formulaOrRefString = definedName.formulaOrRefString;
18524
- if (formulaOrRefString.startsWith(operatorToken.EQUALS)) formulaOrRefString = formulaOrRefString.slice(1);
18525
- const refRangesArr = formulaOrRefString.split(",");
18526
- for (let i = 0; i < refRangesArr.length; i++) {
18527
- const refRange = refRangesArr[i];
18528
- const { sheetName, range } = deserializeRangeWithSheet(refRange);
18529
- if (sheetName) {
18530
- const targetSheet = workbook.getSheetBySheetName(sheetName);
18531
- if (!targetSheet) return true;
18532
- const { startRow, endRow, startColumn, endColumn } = range;
18533
- for (let r = startRow; r <= endRow; r++) for (let c = startColumn; c <= endColumn; c++) {
18534
- var _cell$selectionProtec2;
18535
- const cell = targetSheet.getCell(r, c);
18536
- const permission = cell === null || cell === void 0 || (_cell$selectionProtec2 = cell.selectionProtection) === null || _cell$selectionProtec2 === void 0 ? void 0 : _cell$selectionProtec2[0];
18537
- if ((permission === null || permission === void 0 ? void 0 : permission[UnitAction.View]) === false) return false;
18538
- }
18539
- }
18540
- }
18541
- return true;
18542
- } else {
18543
- const sequenceNodes = this._lexerTreeBuilder.sequenceNodesBuilder(formulaString);
18544
- if (!sequenceNodes) return true;
18545
- for (let i = 0; i < sequenceNodes.length; i++) {
18546
- const node = sequenceNodes[i];
18547
- if (typeof node === "string" || node.nodeType !== sequenceNodeType.REFERENCE) continue;
18548
- const { token } = node;
18549
- const { unitId: sequenceGridUnitId, sheetName, range } = deserializeRangeWithSheetWithCache(token);
18550
- let sequenceGridWorkbook = workbook;
18551
- if (sequenceGridUnitId && sequenceGridUnitId !== unitId) {
18552
- const _sequenceGridWorkbook = this._univerInstanceService.getUnit(sequenceGridUnitId, UniverInstanceType.UNIVER_SHEET);
18553
- if (!_sequenceGridWorkbook) return true;
18554
- sequenceGridWorkbook = _sequenceGridWorkbook;
18555
- }
18556
- const sequenceGridWorksheet = sheetName ? sequenceGridWorkbook.getSheetBySheetName(sheetName) : sequenceGridWorkbook.getActiveSheet();
18557
- if (!sequenceGridWorksheet) return true;
18558
- if (sheetName) {
18559
- const viewPermission = this._permissionService.getPermissionPoint(new WorksheetViewPermission(sequenceGridWorkbook.getUnitId(), sequenceGridWorksheet.getSheetId()).id);
18560
- if (!viewPermission || viewPermission.value === false) return false;
18561
- }
18562
- const { startRow, endRow, startColumn, endColumn } = range;
18563
- for (let r = startRow; r <= endRow; r++) for (let c = startColumn; c <= endColumn; c++) {
18564
- var _cell$selectionProtec3;
18565
- const cell = sequenceGridWorksheet.getCell(r, c);
18566
- const permission = cell === null || cell === void 0 || (_cell$selectionProtec3 = cell.selectionProtection) === null || _cell$selectionProtec3 === void 0 ? void 0 : _cell$selectionProtec3[0];
18567
- if ((permission === null || permission === void 0 ? void 0 : permission[UnitAction.View]) === false) return false;
18568
- }
18569
- }
18570
- return true;
18571
- }
18568
+ var _this$_selectionManag3;
18569
+ const ranges = params.range ? [params.range] : ((_this$_selectionManag3 = this._selectionManagerService.getCurrentSelections()) === null || _this$_selectionManag3 === void 0 ? void 0 : _this$_selectionManag3.map((selection) => selection.range)) ?? [];
18570
+ for (const range of ranges) for (let row = range.startRow; row <= range.endRow; row++) for (let column = range.startColumn; column <= range.endColumn; column++) if (!this._permissionCheckFormulaReferences(formulaString, {
18571
+ unitId,
18572
+ subUnitId,
18573
+ row,
18574
+ column
18575
+ })) return false;
18576
+ return true;
18572
18577
  }
18573
18578
  const { range } = params;
18574
18579
  if (range) {
@@ -18579,6 +18584,73 @@ let SheetPermissionCheckController = class SheetPermissionCheckController extend
18579
18584
  }
18580
18585
  return true;
18581
18586
  }
18587
+ _getFormulaReferenceWorkbook(unitId, qualifier) {
18588
+ const workbook = this._univerInstanceService.getUnit(qualifier || unitId, UniverInstanceType.UNIVER_SHEET);
18589
+ if (workbook || !qualifier) return workbook;
18590
+ const workbooks = this._univerInstanceService.getAllUnitsForType(UniverInstanceType.UNIVER_SHEET).filter((item) => item.getSnapshot().name.length > 0);
18591
+ const exactMatches = workbooks.filter((item) => item.getSnapshot().name.toLowerCase() === qualifier.toLowerCase());
18592
+ const matches = exactMatches.length > 0 ? exactMatches : workbooks.filter((item) => normalizeFormulaUnitName(item.getSnapshot().name) === normalizeFormulaUnitName(qualifier));
18593
+ return matches.length === 1 ? matches[0] : null;
18594
+ }
18595
+ _permissionCheckFormulaReferences(formulaString, context, visitingNames = /* @__PURE__ */ new Set(), referenceOffset) {
18596
+ const { unitId, subUnitId, row, column } = context;
18597
+ const sequenceNodes = this._lexerTreeBuilder.sequenceNodesBuilder(formulaString) ?? [];
18598
+ for (let index = 0; index < sequenceNodes.length; index++) {
18599
+ const node = sequenceNodes[index];
18600
+ if (typeof node === "string" || node.nodeType !== sequenceNodeType.REFERENCE && node.nodeType !== sequenceNodeType.FUNCTION) continue;
18601
+ const nextNode = sequenceNodes[index + 1];
18602
+ if (node.nodeType === sequenceNodeType.FUNCTION && typeof nextNode === "string" && nextNode.trim() === matchToken.OPEN_BRACKET && this._functionService.hasExecutor(node.token.trim().toUpperCase())) continue;
18603
+ let nameToken = node.token.trim();
18604
+ if (node.nodeType === sequenceNodeType.FUNCTION) {
18605
+ const parts = nameToken.split("!");
18606
+ if (parts.length === 2) {
18607
+ var _this$_univerInstance;
18608
+ const unqualifiedName = parts[1].trim();
18609
+ const unscopedName = this._definedNamesService.getValueByName(unitId, unqualifiedName);
18610
+ const localSheetId = unscopedName === null || unscopedName === void 0 ? void 0 : unscopedName.localSheetId;
18611
+ const localSheet = (_this$_univerInstance = this._univerInstanceService.getUnit(unitId, UniverInstanceType.UNIVER_SHEET)) === null || _this$_univerInstance === void 0 ? void 0 : _this$_univerInstance.getSheetBySheetId(localSheetId ?? "");
18612
+ if (unscopedName && (localSheetId == null || localSheetId === "AllDefaultWorkbook" || (localSheet === null || localSheet === void 0 ? void 0 : localSheet.getName()) === parts[0])) nameToken = unqualifiedName;
18613
+ }
18614
+ }
18615
+ const definedName = this._definedNamesService.getValueByName(unitId, nameToken, subUnitId);
18616
+ if (definedName) {
18617
+ const nameKey = `${unitId}:${definedName.id}`;
18618
+ if (visitingNames.has(nameKey)) continue;
18619
+ visitingNames.add(nameKey);
18620
+ const definition = definedName.formulaOrRefString;
18621
+ const isFormula = definition.startsWith(operatorToken.EQUALS);
18622
+ const offset = isFormula ? void 0 : {
18623
+ x: column - (definedName.localSheetId === "AllDefaultWorkbook" ? 1 : 0),
18624
+ y: row
18625
+ };
18626
+ const permitted = this._permissionCheckFormulaReferences(isFormula ? definition : `${operatorToken.EQUALS}${definition}`, context, visitingNames, offset);
18627
+ visitingNames.delete(nameKey);
18628
+ if (!permitted) return false;
18629
+ continue;
18630
+ }
18631
+ if (node.nodeType !== sequenceNodeType.REFERENCE) continue;
18632
+ const { unitQualifier, sheetName } = handleRefStringInfo(node.token.trim());
18633
+ const referenceWorkbook = this._getFormulaReferenceWorkbook(unitId, unitQualifier);
18634
+ const referenceWorksheet = sheetName ? referenceWorkbook === null || referenceWorkbook === void 0 ? void 0 : referenceWorkbook.getSheetBySheetName(sheetName) : referenceWorkbook === null || referenceWorkbook === void 0 ? void 0 : referenceWorkbook.getSheetBySheetId(subUnitId);
18635
+ if (!referenceWorkbook || !referenceWorksheet) continue;
18636
+ const referenceUnitId = referenceWorkbook.getUnitId();
18637
+ const referenceSheetId = referenceWorksheet.getSheetId();
18638
+ const { range } = deserializeRangeWithSheetWithCache(node.token);
18639
+ const referenceRange = referenceOffset ? moveRangeByOffset({ ...range }, referenceOffset.x, referenceOffset.y) : range;
18640
+ if (referenceOffset) {
18641
+ const wrap = (value, limit) => (value % limit + limit) % limit;
18642
+ referenceRange.startRow = wrap(referenceRange.startRow, MAX_ROW_COUNT);
18643
+ referenceRange.endRow = wrap(referenceRange.endRow, MAX_ROW_COUNT);
18644
+ referenceRange.startColumn = wrap(referenceRange.startColumn, MAX_COLUMN_COUNT);
18645
+ referenceRange.endColumn = wrap(referenceRange.endColumn, MAX_COLUMN_COUNT);
18646
+ }
18647
+ if (!this.permissionCheckWithRanges({
18648
+ worksheetTypes: [WorksheetViewPermission],
18649
+ rangeTypes: [RangeProtectionPermissionViewPoint]
18650
+ }, [referenceRange], referenceUnitId, referenceSheetId)) return false;
18651
+ }
18652
+ return true;
18653
+ }
18582
18654
  _permissionCheckBySetRangeValue(permissionTypes, params) {
18583
18655
  const { unitId, subUnitId, value, range } = params;
18584
18656
  let ranges = [];
@@ -18674,11 +18746,11 @@ let SheetPermissionCheckController = class SheetPermissionCheckController extend
18674
18746
  return true;
18675
18747
  }
18676
18748
  _permissionCheckWithInsertOrDeleteMoveRange(direction, params) {
18677
- var _this$_selectionManag3;
18749
+ var _this$_selectionManag4;
18678
18750
  const target = getSheetCommandTarget(this._univerInstanceService);
18679
18751
  if (!target) return false;
18680
18752
  const { worksheet, unitId, subUnitId } = target;
18681
- const range = (params === null || params === void 0 ? void 0 : params.range) ?? ((_this$_selectionManag3 = this._selectionManagerService.getCurrentLastSelection()) === null || _this$_selectionManag3 === void 0 ? void 0 : _this$_selectionManag3.range);
18753
+ const range = (params === null || params === void 0 ? void 0 : params.range) ?? ((_this$_selectionManag4 = this._selectionManagerService.getCurrentLastSelection()) === null || _this$_selectionManag4 === void 0 ? void 0 : _this$_selectionManag4.range);
18682
18754
  if (!range) return false;
18683
18755
  if (!this.permissionCheckWithRanges({
18684
18756
  workbookTypes: [WorkbookEditablePermission],
@@ -18703,7 +18775,10 @@ SheetPermissionCheckController = __decorate([
18703
18775
  __decorateParam(5, Inject(WorksheetProtectionRuleModel)),
18704
18776
  __decorateParam(6, Inject(LocaleService)),
18705
18777
  __decorateParam(7, Inject(LexerTreeBuilder)),
18706
- __decorateParam(8, IDefinedNamesService)
18778
+ __decorateParam(8, IDefinedNamesService),
18779
+ __decorateParam(9, IAutoFillService),
18780
+ __decorateParam(10, Inject(FormulaDataModel)),
18781
+ __decorateParam(11, IFunctionService)
18707
18782
  ], SheetPermissionCheckController);
18708
18783
 
18709
18784
  //#endregion
@@ -19569,7 +19644,7 @@ RangeProtectionCache = __decorate([
19569
19644
  //#endregion
19570
19645
  //#region package.json
19571
19646
  var name = "@univerjs/sheets";
19572
- var version = "1.1.0";
19647
+ var version = "1.2.0";
19573
19648
 
19574
19649
  //#endregion
19575
19650
  //#region src/controllers/active-worksheet.controller.ts
@@ -21499,7 +21574,10 @@ function convertPositionCellToSheetOverGrid(unitId, subUnitId, cellOverGridPosit
21499
21574
  });
21500
21575
  const left = precisionTo(startSelectionX + fromColumnOffset, 1);
21501
21576
  const top = precisionTo(startSelectionY + fromRowOffset, 1);
21502
- const endSelectionCell = skeleton.getCellIndexAndOffsetByPosition(left + width, top + height);
21577
+ const endSelectionCell = skeleton.getCellIndexAndOffsetByPosition(left + width, top + height, 1, 1, {
21578
+ x: 0,
21579
+ y: 0
21580
+ });
21503
21581
  return {
21504
21582
  unitId,
21505
21583
  subUnitId,
@@ -31,6 +31,9 @@ export declare class AutoFillController extends Disposable {
31
31
  private _getCopyData;
32
32
  private _getEmptyCopyDataPiece;
33
33
  private _getMergeApplyData;
34
+ private _getFillCells;
35
+ private _prepareFillData;
36
+ private _getRemoveMergeMutations;
34
37
  private _fillData;
35
38
  private _shouldDisableSeries;
36
39
  private _getPreferredApplyType;
@@ -15,10 +15,11 @@
15
15
  */
16
16
  import type { IPermissionTypes, IRange } from '@univerjs/core';
17
17
  import { Disposable, DisposableCollection, ICommandService, IPermissionService, IUniverInstanceService, LocaleService } from '@univerjs/core';
18
- import { IDefinedNamesService, LexerTreeBuilder } from '@univerjs/engine-formula';
18
+ import { FormulaDataModel, IDefinedNamesService, IFunctionService, LexerTreeBuilder } from '@univerjs/engine-formula';
19
19
  import { RangeProtectionRuleModel } from '../../models/range-protection-rule.model';
20
- import { WorksheetProtectionRuleModel } from '../../services/permission/worksheet-permission';
21
- import { SheetsSelectionsService } from '../../services/selections';
20
+ import { IAutoFillService } from '../../services/auto-fill/auto-fill.service';
21
+ import { WorksheetProtectionRuleModel } from '../../services/permission/worksheet-permission/worksheet-permission-rule.model';
22
+ import { SheetsSelectionsService } from '../../services/selections/selection.service';
22
23
  export declare class SheetPermissionCheckController extends Disposable {
23
24
  private readonly _commandService;
24
25
  private readonly _univerInstanceService;
@@ -29,10 +30,13 @@ export declare class SheetPermissionCheckController extends Disposable {
29
30
  private readonly _localeService;
30
31
  private readonly _lexerTreeBuilder;
31
32
  private readonly _definedNamesService;
33
+ private readonly _autoFillService;
34
+ private readonly _formulaDataModel;
35
+ private readonly _functionService;
32
36
  disposableCollection: DisposableCollection;
33
37
  private _triggerPermissionUIEvent$;
34
38
  triggerPermissionUIEvent$: import("rxjs").Observable<string>;
35
- constructor(_commandService: ICommandService, _univerInstanceService: IUniverInstanceService, _permissionService: IPermissionService, _selectionManagerService: SheetsSelectionsService, _rangeProtectionRuleModel: RangeProtectionRuleModel, _worksheetProtectionRuleModel: WorksheetProtectionRuleModel, _localeService: LocaleService, _lexerTreeBuilder: LexerTreeBuilder, _definedNamesService: IDefinedNamesService);
39
+ constructor(_commandService: ICommandService, _univerInstanceService: IUniverInstanceService, _permissionService: IPermissionService, _selectionManagerService: SheetsSelectionsService, _rangeProtectionRuleModel: RangeProtectionRuleModel, _worksheetProtectionRuleModel: WorksheetProtectionRuleModel, _localeService: LocaleService, _lexerTreeBuilder: LexerTreeBuilder, _definedNamesService: IDefinedNamesService, _autoFillService: IAutoFillService, _formulaDataModel: FormulaDataModel, _functionService: IFunctionService);
36
40
  private _initialize;
37
41
  private _commandExecutedListener;
38
42
  blockExecuteWithoutPermission(errorMsg: string): void;
@@ -40,6 +44,8 @@ export declare class SheetPermissionCheckController extends Disposable {
40
44
  permissionCheckWithRanges(permissionTypes: IPermissionTypes, selectionRanges?: IRange[], unitId?: string, subUnitId?: string): boolean;
41
45
  permissionCheckWithoutRange(permissionTypes: IPermissionTypes, unitId?: string, subUnitId?: string): boolean;
42
46
  private _permissionCheckWithFormula;
47
+ private _getFormulaReferenceWorkbook;
48
+ private _permissionCheckFormulaReferences;
43
49
  private _permissionCheckBySetRangeValue;
44
50
  private _permissionCheckByMoveRowsColsCommand;
45
51
  private _permissionCheckByMoveRangeCommand;
@@ -15,8 +15,8 @@
15
15
  */
16
16
  import type { IDisposable, IRange, Nullable } from '@univerjs/core';
17
17
  import type { Observable } from 'rxjs';
18
- import type { IAutoFillLocation, IAutoFillRule, ISheetAutoFillHook } from './type';
19
18
  import { Direction, Disposable, ICommandService, Injector, IUndoRedoService, IUniverInstanceService } from '@univerjs/core';
19
+ import type { IAutoFillLocation, IAutoFillRule, ISheetAutoFillHook } from './type';
20
20
  import { AUTO_FILL_APPLY_TYPE } from './type';
21
21
  export interface IAutoFillService {
22
22
  applyType$: Observable<AUTO_FILL_APPLY_TYPE>;
@@ -34,7 +34,7 @@ export interface IAutoFillService {
34
34
  setFillingStyle(isFillingStyle: boolean): void;
35
35
  registerRule(rule: IAutoFillRule): void;
36
36
  getAllHooks(): ISheetAutoFillHook[];
37
- getActiveHooks(): ISheetAutoFillHook[];
37
+ getActiveHooks(applyType?: AUTO_FILL_APPLY_TYPE): ISheetAutoFillHook[];
38
38
  addHook(hook: ISheetAutoFillHook): IDisposable;
39
39
  fillData(applyType: AUTO_FILL_APPLY_TYPE): boolean;
40
40
  triggerAutoFill(unitId: string, subUnitId: string, source: IRange, target: IRange, manualApplyType?: AUTO_FILL_APPLY_TYPE): Promise<boolean>;
@@ -71,7 +71,7 @@ export declare class AutoFillService extends Disposable implements IAutoFillServ
71
71
  registerRule(rule: IAutoFillRule): void;
72
72
  getRules(): IAutoFillRule[];
73
73
  getAllHooks(): ISheetAutoFillHook[];
74
- getActiveHooks(): ISheetAutoFillHook[];
74
+ getActiveHooks(applyType?: AUTO_FILL_APPLY_TYPE): ISheetAutoFillHook[];
75
75
  get applyType(): AUTO_FILL_APPLY_TYPE;
76
76
  set applyType(type: AUTO_FILL_APPLY_TYPE);
77
77
  get menu(): IApplyMenuItem[];
@@ -14,7 +14,7 @@
14
14
  * limitations under the License.
15
15
  */
16
16
  import type { Direction, IAccessor, ICellData, IMutationInfo, IObjectMatrixPrimitiveType, Nullable } from '@univerjs/core';
17
- import type { IDiscreteRange } from '../../basics';
17
+ import type { IDiscreteRange } from '../../basics/interfaces/selection-config';
18
18
  export declare enum AUTO_FILL_APPLY_TYPE {
19
19
  COPY = "COPY",
20
20
  SERIES = "SERIES",
@@ -50,11 +50,18 @@ export interface ISheetAutoFillHook {
50
50
  bindUnit?: string;
51
51
  disable?: (location: IAutoFillLocation, direction: Direction, applyType: AUTO_FILL_APPLY_TYPE) => boolean;
52
52
  onBeforeFillData?(location: IAutoFillLocation, direction: Direction): AUTO_FILL_APPLY_TYPE | void;
53
+ /** Prepare and validate without mutations; the returned callback captures undo data after reverting a previous fill. */
54
+ onPrepareFillData?(location: IAutoFillLocation, direction: Direction, applyType: AUTO_FILL_APPLY_TYPE): () => {
55
+ undos: IMutationInfo[];
56
+ redos: IMutationInfo[];
57
+ };
53
58
  onFillData?(location: IAutoFillLocation, direction: Direction, applyType: AUTO_FILL_APPLY_TYPE): {
54
59
  undos: IMutationInfo[];
55
60
  redos: IMutationInfo[];
56
61
  };
57
62
  onAfterFillData?(location: IAutoFillLocation, direction: Direction, applyType: AUTO_FILL_APPLY_TYPE): boolean | void;
63
+ /** Validate proposed values before reverting or applying any fill mutations. Throw to reject the entire fill. */
64
+ onValidateFillData?(location: IAutoFillLocation, direction: Direction, applyType: AUTO_FILL_APPLY_TYPE, cellValue: IObjectMatrixPrimitiveType<Nullable<ICellData>>): void;
58
65
  onBeforeSubmit?: (location: IAutoFillLocation, direction: Direction, applyType: AUTO_FILL_APPLY_TYPE, cellValue: IObjectMatrixPrimitiveType<Nullable<ICellData>>) => void;
59
66
  }
60
67
  export type IAutoFillCopyDataInTypeIndexInfo = number[];