@univerjs/data-validation 0.2.11 → 0.2.13

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/es/index.js CHANGED
@@ -2,50 +2,8 @@ var __defProp = Object.defineProperty;
2
2
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: !0, configurable: !0, writable: !0, value }) : obj[key] = value;
3
3
  var __name = (target, value) => __defProp(target, "name", { value, configurable: !0 });
4
4
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key != "symbol" ? key + "" : key, value);
5
- import { toDisposable, Tools, DataValidationType, DataValidationOperator, Disposable, DataValidationStatus, ILogService, CommandType, ICommandService, IUndoRedoService, UniverInstanceType, OnLifecycle, LifecycleStages, Inject, IResourceManagerService, IUniverInstanceService, Plugin, Injector, LocaleService } from "@univerjs/core";
6
- import { BehaviorSubject, Subject, debounceTime } from "rxjs";
7
- import { SheetsSelectionsService, RemoveSheetCommand, SheetInterceptorService } from "@univerjs/sheets";
8
- var DataValidatorRegistryScope = /* @__PURE__ */ ((DataValidatorRegistryScope2) => (DataValidatorRegistryScope2.SHEET = "sheet", DataValidatorRegistryScope2))(DataValidatorRegistryScope || {});
9
- const _DataValidatorRegistryService = class _DataValidatorRegistryService {
10
- constructor() {
11
- __publicField(this, "_validatorByScopes", /* @__PURE__ */ new Map());
12
- __publicField(this, "_validatorMap", /* @__PURE__ */ new Map());
13
- __publicField(this, "_validatorsChange$", new BehaviorSubject(void 0));
14
- __publicField(this, "validatorsChange$", this._validatorsChange$.asObservable());
15
- }
16
- _addValidatorToScope(validator, scope) {
17
- this._validatorByScopes.has(scope) || this._validatorByScopes.set(scope, []);
18
- const validators = this._validatorByScopes.get(scope);
19
- if (validators.findIndex((m) => m.id === validator.id) > -1)
20
- throw new Error(`Validator item with the same id ${validator.id} has already been added!`);
21
- validators.push(validator);
22
- }
23
- _removeValidatorFromScope(validator, scope) {
24
- const validators = this._validatorByScopes.get(scope);
25
- if (!validators)
26
- return;
27
- const index = validators.findIndex((v) => v.id === validator.id);
28
- index > -1 && validators.splice(index, 1);
29
- }
30
- register(validator) {
31
- return this._validatorMap.set(validator.id, validator), Array.isArray(validator.scopes) ? validator.scopes.forEach((scope) => {
32
- this._addValidatorToScope(validator, scope);
33
- }) : this._addValidatorToScope(validator, validator.scopes), this._validatorsChange$.next(), toDisposable(() => {
34
- this._validatorMap.delete(validator.id), Array.isArray(validator.scopes) ? validator.scopes.forEach((scope) => {
35
- this._removeValidatorFromScope(validator, scope);
36
- }) : this._removeValidatorFromScope(validator, validator.scopes), this._validatorsChange$.next();
37
- });
38
- }
39
- getValidatorItem(id) {
40
- return this._validatorMap.get(id);
41
- }
42
- getValidatorsByScope(scope) {
43
- return this._validatorByScopes.get(scope);
44
- }
45
- };
46
- __name(_DataValidatorRegistryService, "DataValidatorRegistryService");
47
- let DataValidatorRegistryService = _DataValidatorRegistryService;
48
- var UpdateRuleType = /* @__PURE__ */ ((UpdateRuleType2) => (UpdateRuleType2[UpdateRuleType2.SETTING = 0] = "SETTING", UpdateRuleType2[UpdateRuleType2.RANGE = 1] = "RANGE", UpdateRuleType2[UpdateRuleType2.OPTIONS = 2] = "OPTIONS", UpdateRuleType2))(UpdateRuleType || {});
5
+ import { Disposable, Tools, ILogService, CommandType, UniverInstanceType, OnLifecycle, LifecycleStages, Inject, IResourceManagerService, IUniverInstanceService, toDisposable, ICommandService, IUndoRedoService, Plugin, Injector, IConfigService, DataValidationOperator, LocaleService } from "@univerjs/core";
6
+ import { Subject, debounceTime, BehaviorSubject } from "rxjs";
49
7
  function getRuleSetting(rule) {
50
8
  return {
51
9
  type: rule.type,
@@ -72,53 +30,44 @@ function getRuleOptions(rule) {
72
30
  };
73
31
  }
74
32
  __name(getRuleOptions, "getRuleOptions");
75
- function createDefaultNewRule(accessor) {
76
- const currentRanges = accessor.get(SheetsSelectionsService).getCurrentSelections().map((s) => s.range);
77
- return {
78
- uid: Tools.generateRandomId(6),
79
- type: DataValidationType.DECIMAL,
80
- operator: DataValidationOperator.EQUAL,
81
- formula1: "100",
82
- ranges: currentRanges != null ? currentRanges : [{ startColumn: 0, endColumn: 0, startRow: 0, endRow: 0 }]
83
- };
84
- }
85
- __name(createDefaultNewRule, "createDefaultNewRule");
86
- const _DataValidationManager = class _DataValidationManager extends Disposable {
87
- constructor(unitId, subUnitId) {
33
+ var UpdateRuleType = /* @__PURE__ */ ((UpdateRuleType2) => (UpdateRuleType2[UpdateRuleType2.SETTING = 0] = "SETTING", UpdateRuleType2[UpdateRuleType2.RANGE = 1] = "RANGE", UpdateRuleType2[UpdateRuleType2.OPTIONS = 2] = "OPTIONS", UpdateRuleType2))(UpdateRuleType || {}), __defProp$3 = Object.defineProperty, __getOwnPropDesc$3 = Object.getOwnPropertyDescriptor, __decorateClass$3 = /* @__PURE__ */ __name((decorators, target, key, kind) => {
34
+ for (var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc$3(target, key) : target, i = decorators.length - 1, decorator; i >= 0; i--)
35
+ (decorator = decorators[i]) && (result = (kind ? decorator(target, key, result) : decorator(result)) || result);
36
+ return kind && result && __defProp$3(target, key, result), result;
37
+ }, "__decorateClass$3"), __decorateParam$3 = /* @__PURE__ */ __name((index, decorator) => (target, key) => decorator(target, key, index), "__decorateParam$3"), _a;
38
+ let DataValidationModel = (_a = class extends Disposable {
39
+ constructor(_logService) {
88
40
  super();
89
- __publicField(this, "_dataValidations", []);
90
- __publicField(this, "_dataValidationMap", /* @__PURE__ */ new Map());
91
- __publicField(this, "_dataValidations$", new BehaviorSubject(this._dataValidations));
92
- __publicField(this, "unitId");
93
- __publicField(this, "subUnitId");
94
- __publicField(this, "dataValidations$", this._dataValidations$.asObservable());
95
- this.unitId = unitId, this.subUnitId = subUnitId, this._notice(), this.disposeWithMe({
41
+ __publicField(this, "_model", /* @__PURE__ */ new Map());
42
+ __publicField(this, "_ruleChange$", new Subject());
43
+ __publicField(this, "ruleChange$", this._ruleChange$.asObservable());
44
+ __publicField(this, "ruleChangeDebounce$", this.ruleChange$.pipe(debounceTime(20)));
45
+ this._logService = _logService, this.disposeWithMe({
96
46
  dispose: /* @__PURE__ */ __name(() => {
97
- this._dataValidations$.complete();
47
+ this._ruleChange$.complete();
98
48
  }, "dispose")
99
49
  });
100
50
  }
101
- _notice() {
102
- this._dataValidations$.next(this._dataValidations);
103
- }
104
- getRuleById(id) {
105
- return this._dataValidationMap.get(id);
106
- }
107
- getRuleIndex(id) {
108
- return this._dataValidations.findIndex((rule) => rule.uid === id);
51
+ _ensureMap(unitId, subUnitId) {
52
+ this._model.has(unitId) || this._model.set(unitId, /* @__PURE__ */ new Map());
53
+ const unitMap = this._model.get(unitId);
54
+ if (unitMap.has(subUnitId))
55
+ return unitMap.get(subUnitId);
56
+ const map = { map: /* @__PURE__ */ new Map(), list: [] };
57
+ return unitMap.set(subUnitId, map), map;
109
58
  }
110
- addRule(rule, index) {
111
- const rules = (Array.isArray(rule) ? rule : [rule]).filter((item) => !this._dataValidationMap.has(item.uid));
112
- typeof index == "number" && index < this._dataValidations.length ? this._dataValidations.splice(index, 0, ...rules) : this._dataValidations.push(...rules), rules.forEach((item) => {
113
- this._dataValidationMap.set(item.uid, item);
114
- }), this._notice();
59
+ _addSubUnitRule(subUnit, rule, index) {
60
+ const { map: dataValidationMap, list: dataValidations } = subUnit, rules = (Array.isArray(rule) ? rule : [rule]).filter((item) => !dataValidationMap.has(item.uid));
61
+ typeof index == "number" && index < dataValidations.length ? dataValidations.splice(index, 0, ...rules) : dataValidations.push(...rules), rules.forEach((item) => {
62
+ dataValidationMap.set(item.uid, item);
63
+ });
115
64
  }
116
- removeRule(ruleId) {
117
- const index = this._dataValidations.findIndex((item) => item.uid === ruleId);
118
- index > -1 && (this._dataValidations.splice(index, 1), this._dataValidationMap.delete(ruleId), this._notice());
65
+ _removeSubUnitRule(subUnit, ruleId) {
66
+ const { map: dataValidationMap, list: dataValidations } = subUnit, index = dataValidations.findIndex((item) => item.uid === ruleId);
67
+ index > -1 && (dataValidations.splice(index, 1), dataValidationMap.delete(ruleId));
119
68
  }
120
- updateRule(ruleId, payload) {
121
- const oldRule = this._dataValidationMap.get(ruleId), index = this._dataValidations.findIndex((rule2) => ruleId === rule2.uid);
69
+ _updateSubUnitRule(subUnit, ruleId, payload) {
70
+ const { map: dataValidationMap, list: dataValidations } = subUnit, oldRule = dataValidationMap.get(ruleId), index = dataValidations.findIndex((rule2) => ruleId === rule2.uid);
122
71
  if (!oldRule)
123
72
  throw new Error(`Data validation rule is not found, ruleId: ${ruleId}.`);
124
73
  const rule = { ...oldRule };
@@ -136,54 +85,10 @@ const _DataValidationManager = class _DataValidationManager extends Disposable {
136
85
  break;
137
86
  }
138
87
  }
139
- return this._dataValidations[index] = rule, this._dataValidationMap.set(ruleId, rule), this._notice(), rule;
140
- }
141
- getDataValidations() {
142
- return this._dataValidations;
143
- }
144
- toJSON() {
145
- return this._dataValidations;
146
- }
147
- validator(_value, _rule, _pos, _onComplete) {
148
- return DataValidationStatus.VALID;
149
- }
150
- };
151
- __name(_DataValidationManager, "DataValidationManager");
152
- let DataValidationManager = _DataValidationManager;
153
- var __defProp$4 = Object.defineProperty, __getOwnPropDesc$4 = Object.getOwnPropertyDescriptor, __decorateClass$4 = /* @__PURE__ */ __name((decorators, target, key, kind) => {
154
- for (var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc$4(target, key) : target, i = decorators.length - 1, decorator; i >= 0; i--)
155
- (decorator = decorators[i]) && (result = (kind ? decorator(target, key, result) : decorator(result)) || result);
156
- return kind && result && __defProp$4(target, key, result), result;
157
- }, "__decorateClass$4"), __decorateParam$4 = /* @__PURE__ */ __name((index, decorator) => (target, key) => decorator(target, key, index), "__decorateParam$4"), _a;
158
- let DataValidationModel = (_a = class extends Disposable {
159
- constructor(_logService) {
160
- super();
161
- __publicField(this, "_model", /* @__PURE__ */ new Map());
162
- __publicField(this, "_managerCreator", /* @__PURE__ */ __name((unitId, subUnitId) => new DataValidationManager(unitId, subUnitId), "_managerCreator"));
163
- __publicField(this, "_ruleChange$", new Subject());
164
- __publicField(this, "_validStatusChange$", new Subject());
165
- __publicField(this, "ruleChange$", this._ruleChange$.asObservable());
166
- __publicField(this, "ruleChangeDebounce$", this.ruleChange$.pipe(debounceTime(20)));
167
- __publicField(this, "validStatusChange$", this._validStatusChange$.asObservable().pipe(debounceTime(20)));
168
- this._logService = _logService, this.disposeWithMe({
169
- dispose: /* @__PURE__ */ __name(() => {
170
- this._ruleChange$.complete(), this._validStatusChange$.complete();
171
- }, "dispose")
172
- });
173
- }
174
- setManagerCreator(creator) {
175
- this._managerCreator = creator;
176
- }
177
- ensureManager(unitId, subUnitId) {
178
- this._model.has(unitId) || this._model.set(unitId, /* @__PURE__ */ new Map());
179
- const unitMap = this._model.get(unitId);
180
- if (unitMap.has(subUnitId))
181
- return unitMap.get(subUnitId);
182
- const manager = this._managerCreator(unitId, subUnitId);
183
- return unitMap.set(subUnitId, manager), this.disposeWithMe(manager), manager;
88
+ return dataValidations[index] = rule, dataValidationMap.set(ruleId, rule), rule;
184
89
  }
185
90
  _addRuleSideEffect(unitId, subUnitId, rule, source) {
186
- this.ensureManager(unitId, subUnitId).getRuleById(rule.uid) || this._ruleChange$.next({
91
+ this._ensureMap(unitId, subUnitId).map.get(rule.uid) || this._ruleChange$.next({
187
92
  rule,
188
93
  type: "add",
189
94
  unitId,
@@ -193,23 +98,28 @@ let DataValidationModel = (_a = class extends Disposable {
193
98
  }
194
99
  addRule(unitId, subUnitId, rule, source, index) {
195
100
  try {
196
- const manager = this.ensureManager(unitId, subUnitId);
101
+ const subUnitMap = this._ensureMap(unitId, subUnitId);
197
102
  (Array.isArray(rule) ? rule : [rule]).forEach((item) => {
198
103
  this._addRuleSideEffect(unitId, subUnitId, item, source);
199
- }), manager.addRule(rule, index);
104
+ }), this._addSubUnitRule(subUnitMap, rule, index);
200
105
  } catch (error) {
201
106
  this._logService.error(error);
202
107
  }
203
108
  }
204
109
  updateRule(unitId, subUnitId, ruleId, payload, source) {
205
110
  try {
206
- const rule = this.ensureManager(unitId, subUnitId).updateRule(ruleId, payload);
111
+ const subUnitMap = this._ensureMap(unitId, subUnitId), oldRule = Tools.deepClone(subUnitMap.map.get(ruleId));
112
+ if (!oldRule)
113
+ throw new Error(`Data validation rule is not found, ruleId: ${ruleId}.`);
114
+ const rule = this._updateSubUnitRule(subUnitMap, ruleId, payload);
207
115
  this._ruleChange$.next({
208
116
  rule,
209
117
  type: "update",
210
118
  unitId,
211
119
  subUnitId,
212
- source
120
+ source,
121
+ updatePayload: payload,
122
+ oldRule
213
123
  });
214
124
  } catch (error) {
215
125
  this._logService.error(error);
@@ -217,8 +127,8 @@ let DataValidationModel = (_a = class extends Disposable {
217
127
  }
218
128
  removeRule(unitId, subUnitId, ruleId, source) {
219
129
  try {
220
- const manager = this.ensureManager(unitId, subUnitId), oldRule = manager.getRuleById(ruleId);
221
- oldRule && (manager.removeRule(ruleId), this._ruleChange$.next({
130
+ const map = this._ensureMap(unitId, subUnitId), oldRule = map.map.get(ruleId);
131
+ oldRule && (this._removeSubUnitRule(map, ruleId), this._ruleChange$.next({
222
132
  rule: oldRule,
223
133
  type: "remove",
224
134
  unitId,
@@ -230,44 +140,36 @@ let DataValidationModel = (_a = class extends Disposable {
230
140
  }
231
141
  }
232
142
  getRuleById(unitId, subUnitId, ruleId) {
233
- return this.ensureManager(unitId, subUnitId).getRuleById(ruleId);
143
+ return this._ensureMap(unitId, subUnitId).map.get(ruleId);
234
144
  }
235
145
  getRuleIndex(unitId, subUnitId, ruleId) {
236
- return this.ensureManager(unitId, subUnitId).getRuleIndex(ruleId);
146
+ return this._ensureMap(unitId, subUnitId).list.findIndex((rule) => rule.uid === ruleId);
237
147
  }
238
148
  getRules(unitId, subUnitId) {
239
- return this.ensureManager(unitId, subUnitId).getDataValidations();
240
- }
241
- validator(rule, pos, value) {
242
- const { unitId, subUnitId } = pos;
243
- return this.ensureManager(unitId, subUnitId).validator(value, rule, pos, (status, changed) => {
244
- changed && this._validStatusChange$.next({
245
- unitId,
246
- subUnitId,
247
- ruleId: rule.uid,
248
- status
249
- });
250
- });
149
+ return [...this._ensureMap(unitId, subUnitId).list];
251
150
  }
252
151
  getUnitRules(unitId) {
253
152
  const unitMap = this._model.get(unitId);
254
153
  if (!unitMap)
255
154
  return [];
256
155
  const res = [];
257
- return unitMap.forEach((manager) => {
258
- res.push([manager.subUnitId, manager.getDataValidations()]);
156
+ return unitMap.forEach((manager, subUnitId) => {
157
+ res.push([subUnitId, manager.list]);
259
158
  }), res;
260
159
  }
261
160
  deleteUnitRules(unitId) {
262
161
  this._model.delete(unitId);
263
162
  }
264
163
  getSubUnitIds(unitId) {
265
- var _a6, _b;
266
- return Array.from((_b = (_a6 = this._model.get(unitId)) == null ? void 0 : _a6.keys()) != null ? _b : []);
164
+ var _a5, _b;
165
+ return Array.from((_b = (_a5 = this._model.get(unitId)) == null ? void 0 : _a5.keys()) != null ? _b : []);
166
+ }
167
+ getAll() {
168
+ return Array.from(this._model.keys()).map((unitId) => [unitId, this.getUnitRules(unitId)]);
267
169
  }
268
170
  }, __name(_a, "DataValidationModel"), _a);
269
- DataValidationModel = __decorateClass$4([
270
- __decorateParam$4(0, ILogService)
171
+ DataValidationModel = __decorateClass$3([
172
+ __decorateParam$3(0, ILogService)
271
173
  ], DataValidationModel);
272
174
  const AddDataValidationMutation = {
273
175
  type: CommandType.MUTATION,
@@ -298,11 +200,104 @@ const AddDataValidationMutation = {
298
200
  const { unitId, subUnitId, ruleId, payload, source = "command" } = params;
299
201
  return accessor.get(DataValidationModel).updateRule(unitId, subUnitId, ruleId, payload, source), !0;
300
202
  }
301
- }, AddDataValidationCommand = {
203
+ };
204
+ var __defProp$2 = Object.defineProperty, __getOwnPropDesc$2 = Object.getOwnPropertyDescriptor, __decorateClass$2 = /* @__PURE__ */ __name((decorators, target, key, kind) => {
205
+ for (var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc$2(target, key) : target, i = decorators.length - 1, decorator; i >= 0; i--)
206
+ (decorator = decorators[i]) && (result = (kind ? decorator(target, key, result) : decorator(result)) || result);
207
+ return kind && result && __defProp$2(target, key, result), result;
208
+ }, "__decorateClass$2"), __decorateParam$2 = /* @__PURE__ */ __name((index, decorator) => (target, key) => decorator(target, key, index), "__decorateParam$2");
209
+ const DATA_VALIDATION_PLUGIN_NAME = "SHEET_DATA_VALIDATION_PLUGIN";
210
+ var _a2;
211
+ let DataValidationResourceController = (_a2 = class extends Disposable {
212
+ constructor(_resourceManagerService, _univerInstanceService, _dataValidationModel) {
213
+ super(), this._resourceManagerService = _resourceManagerService, this._univerInstanceService = _univerInstanceService, this._dataValidationModel = _dataValidationModel, this._initSnapshot();
214
+ }
215
+ _initSnapshot() {
216
+ const toJson = /* @__PURE__ */ __name((unitID) => {
217
+ const map = this._dataValidationModel.getUnitRules(unitID), resultMap = {};
218
+ return map ? (map.forEach(([key, v]) => {
219
+ resultMap[key] = v;
220
+ }), JSON.stringify(resultMap)) : "";
221
+ }, "toJson"), parseJson = /* @__PURE__ */ __name((json) => {
222
+ if (!json)
223
+ return {};
224
+ try {
225
+ return JSON.parse(json);
226
+ } catch {
227
+ return {};
228
+ }
229
+ }, "parseJson");
230
+ this.disposeWithMe(
231
+ this._resourceManagerService.registerPluginResource({
232
+ pluginName: DATA_VALIDATION_PLUGIN_NAME,
233
+ businesses: [UniverInstanceType.UNIVER_SHEET],
234
+ toJson: /* @__PURE__ */ __name((unitID) => toJson(unitID), "toJson"),
235
+ parseJson: /* @__PURE__ */ __name((json) => parseJson(json), "parseJson"),
236
+ onUnLoad: /* @__PURE__ */ __name((unitID) => {
237
+ this._dataValidationModel.deleteUnitRules(unitID);
238
+ }, "onUnLoad"),
239
+ onLoad: /* @__PURE__ */ __name((unitID, value) => {
240
+ Object.keys(value).forEach((subunitId) => {
241
+ value[subunitId].forEach((rule) => {
242
+ this._dataValidationModel.addRule(unitID, subunitId, rule, "patched");
243
+ });
244
+ });
245
+ }, "onLoad")
246
+ })
247
+ );
248
+ }
249
+ }, __name(_a2, "DataValidationResourceController"), _a2);
250
+ DataValidationResourceController = __decorateClass$2([
251
+ OnLifecycle(LifecycleStages.Ready, DataValidationResourceController),
252
+ __decorateParam$2(0, IResourceManagerService),
253
+ __decorateParam$2(1, IUniverInstanceService),
254
+ __decorateParam$2(2, Inject(DataValidationModel))
255
+ ], DataValidationResourceController);
256
+ var DataValidatorRegistryScope = /* @__PURE__ */ ((DataValidatorRegistryScope2) => (DataValidatorRegistryScope2.SHEET = "sheet", DataValidatorRegistryScope2))(DataValidatorRegistryScope || {});
257
+ const _DataValidatorRegistryService = class _DataValidatorRegistryService {
258
+ constructor() {
259
+ __publicField(this, "_validatorByScopes", /* @__PURE__ */ new Map());
260
+ __publicField(this, "_validatorMap", /* @__PURE__ */ new Map());
261
+ __publicField(this, "_validatorsChange$", new BehaviorSubject(void 0));
262
+ __publicField(this, "validatorsChange$", this._validatorsChange$.asObservable());
263
+ }
264
+ _addValidatorToScope(validator, scope) {
265
+ this._validatorByScopes.has(scope) || this._validatorByScopes.set(scope, []);
266
+ const validators = this._validatorByScopes.get(scope);
267
+ if (validators.findIndex((m) => m.id === validator.id) > -1)
268
+ throw new Error(`Validator item with the same id ${validator.id} has already been added!`);
269
+ validators.push(validator);
270
+ }
271
+ _removeValidatorFromScope(validator, scope) {
272
+ const validators = this._validatorByScopes.get(scope);
273
+ if (!validators)
274
+ return;
275
+ const index = validators.findIndex((v) => v.id === validator.id);
276
+ index > -1 && validators.splice(index, 1);
277
+ }
278
+ register(validator) {
279
+ return this._validatorMap.set(validator.id, validator), Array.isArray(validator.scopes) ? validator.scopes.forEach((scope) => {
280
+ this._addValidatorToScope(validator, scope);
281
+ }) : this._addValidatorToScope(validator, validator.scopes), this._validatorsChange$.next(), toDisposable(() => {
282
+ this._validatorMap.delete(validator.id), Array.isArray(validator.scopes) ? validator.scopes.forEach((scope) => {
283
+ this._removeValidatorFromScope(validator, scope);
284
+ }) : this._removeValidatorFromScope(validator, validator.scopes), this._validatorsChange$.next();
285
+ });
286
+ }
287
+ getValidatorItem(id) {
288
+ return this._validatorMap.get(id);
289
+ }
290
+ getValidatorsByScope(scope) {
291
+ return this._validatorByScopes.get(scope);
292
+ }
293
+ };
294
+ __name(_DataValidatorRegistryService, "DataValidatorRegistryService");
295
+ let DataValidatorRegistryService = _DataValidatorRegistryService;
296
+ const AddDataValidationCommand = {
302
297
  type: CommandType.COMMAND,
303
298
  id: "data-validation.command.addRule",
304
299
  async handler(accessor, params) {
305
- if (accessor.get(ILogService).warn("[Deprecated] AddDataValidationCommand is deprecated, please use AddSheetDataValidationCommand in @univerjs/sheets-data-validation instead!"), !params)
300
+ if (accessor.get(ILogService).error("[Deprecated]: `AddDataValidationCommand` is deprecated, please use `AddSheetDataValidationCommand` in `@univerjs/sheets-data-validation` instead!"), !params)
306
301
  return !1;
307
302
  const { rule, unitId, subUnitId } = params, commandService = accessor.get(ICommandService), undoRedoService = accessor.get(IUndoRedoService), mutationParams = {
308
303
  ...params,
@@ -327,36 +322,11 @@ const AddDataValidationMutation = {
327
322
  undoMutations
328
323
  }), await commandService.executeCommand(AddDataValidationMutation.id, mutationParams), !0;
329
324
  }
330
- }, removeDataValidationUndoFactory = /* @__PURE__ */ __name((accessor, redoParams) => {
331
- const dataValidationModel = accessor.get(DataValidationModel), { unitId, subUnitId, ruleId, source } = redoParams;
332
- if (Array.isArray(ruleId)) {
333
- const rules = ruleId.map((id) => dataValidationModel.getRuleById(unitId, subUnitId, id)).filter(Boolean);
334
- return [{
335
- id: AddDataValidationMutation.id,
336
- params: {
337
- unitId,
338
- subUnitId,
339
- rule: rules,
340
- source
341
- }
342
- }];
343
- }
344
- return [{
345
- id: AddDataValidationMutation.id,
346
- params: {
347
- unitId,
348
- subUnitId,
349
- rule: {
350
- ...dataValidationModel.getRuleById(unitId, subUnitId, ruleId)
351
- },
352
- index: dataValidationModel.getRuleIndex(unitId, subUnitId, ruleId)
353
- }
354
- }];
355
- }, "removeDataValidationUndoFactory"), RemoveDataValidationCommand = {
325
+ }, RemoveDataValidationCommand = {
356
326
  type: CommandType.COMMAND,
357
327
  id: "data-validation.command.removeRule",
358
328
  handler(accessor, params) {
359
- if (!params)
329
+ if (accessor.get(ILogService).error("[Deprecated]: `RemoveDataValidationCommand` is deprecated, please use `RemoveSheetDataValidationCommand` in `@univerjs/sheets-data-validation` instead!"), !params)
360
330
  return !1;
361
331
  const { unitId, subUnitId, ruleId } = params, commandService = accessor.get(ICommandService), undoRedoService = accessor.get(IUndoRedoService), dataValidationModel = accessor.get(DataValidationModel), redoMutations = [{
362
332
  id: RemoveDataValidationMutation.id,
@@ -382,7 +352,7 @@ const AddDataValidationMutation = {
382
352
  type: CommandType.COMMAND,
383
353
  id: "data-validation.command.updateDataValidationSetting",
384
354
  handler(accessor, params) {
385
- if (accessor.get(ILogService).warn("[Deprecated] UpdateDataValidationOptionsCommand is deprecated, please use UpdateSheetDataValidationOptionsCommand in @univerjs/sheets-data-validation instead!"), !params)
355
+ if (accessor.get(ILogService).warn("[Deprecated]: `UpdateDataValidationOptionsCommand` is deprecated, please use `UpdateSheetDataValidationOptionsCommand` in `@univerjs/sheets-data-validation` instead!"), !params)
386
356
  return !1;
387
357
  const commandService = accessor.get(ICommandService), redoUndoService = accessor.get(IUndoRedoService), dataValidationModel = accessor.get(DataValidationModel), { unitId, subUnitId, ruleId, options } = params, rule = dataValidationModel.getRuleById(unitId, subUnitId, ruleId);
388
358
  if (!rule)
@@ -420,13 +390,16 @@ const AddDataValidationMutation = {
420
390
  type: CommandType.COMMAND,
421
391
  id: "data-validation.command.updateDataValidationOptions",
422
392
  handler(accessor, params) {
423
- if (accessor.get(ILogService).warn("[Deprecated] UpdateDataValidationSettingCommand is deprecated, please use UpdateSheetDataValidationSettingCommand in @univerjs/sheets-data-validation instead!"), !params)
393
+ if (accessor.get(ILogService).error("[Deprecated]: `UpdateDataValidationSettingCommand` is deprecated, please use `UpdateSheetDataValidationSettingCommand` in `@univerjs/sheets-data-validation` instead!"), !params)
424
394
  return !1;
425
395
  const commandService = accessor.get(ICommandService), redoUndoService = accessor.get(IUndoRedoService), dataValidationModel = accessor.get(DataValidationModel), dataValidatorRegistryService = accessor.get(DataValidatorRegistryService), { unitId, subUnitId, ruleId, setting } = params, validator = dataValidatorRegistryService.getValidatorItem(setting.type);
426
396
  if (!validator)
427
397
  return !1;
428
398
  const rule = dataValidationModel.getRuleById(unitId, subUnitId, ruleId);
429
- if (!rule || !validator.validatorFormula({ ...rule, ...setting }, unitId, subUnitId).success)
399
+ if (!rule)
400
+ return !1;
401
+ const newRule = { ...rule, ...setting };
402
+ if (!validator.validatorFormula(newRule, unitId, subUnitId).success)
430
403
  return !1;
431
404
  const mutationParams = {
432
405
  unitId,
@@ -434,7 +407,10 @@ const AddDataValidationMutation = {
434
407
  ruleId,
435
408
  payload: {
436
409
  type: UpdateRuleType.SETTING,
437
- payload: setting
410
+ payload: {
411
+ ...setting,
412
+ ...validator.normalizeFormula(newRule, unitId, subUnitId)
413
+ }
438
414
  }
439
415
  }, redoMutations = [{
440
416
  id: UpdateDataValidationMutation.id,
@@ -461,7 +437,7 @@ const AddDataValidationMutation = {
461
437
  type: CommandType.COMMAND,
462
438
  id: "data-validation.command.removeAll",
463
439
  handler(accessor, params) {
464
- if (!params)
440
+ if (accessor.get(ILogService).error("[Deprecated]: `RemoveAllDataValidationCommand` is deprecated, please use `RemoveSheetAllDataValidationCommand` in `@univerjs/sheets-data-validation` instead!"), !params)
465
441
  return !1;
466
442
  const { unitId, subUnitId } = params, commandService = accessor.get(ICommandService), dataValidationModel = accessor.get(DataValidationModel), undoRedoService = accessor.get(IUndoRedoService), currentRules = [...dataValidationModel.getRules(unitId, subUnitId)], redoParams = {
467
443
  unitId,
@@ -484,134 +460,25 @@ const AddDataValidationMutation = {
484
460
  unitID: unitId
485
461
  }), commandService.executeCommand(RemoveDataValidationMutation.id, redoParams), !0;
486
462
  }
487
- };
488
- var __defProp$3 = Object.defineProperty, __getOwnPropDesc$3 = Object.getOwnPropertyDescriptor, __decorateClass$3 = /* @__PURE__ */ __name((decorators, target, key, kind) => {
489
- for (var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc$3(target, key) : target, i = decorators.length - 1, decorator; i >= 0; i--)
490
- (decorator = decorators[i]) && (result = (kind ? decorator(target, key, result) : decorator(result)) || result);
491
- return kind && result && __defProp$3(target, key, result), result;
492
- }, "__decorateClass$3"), __decorateParam$3 = /* @__PURE__ */ __name((index, decorator) => (target, key) => decorator(target, key, index), "__decorateParam$3");
493
- const DATA_VALIDATION_PLUGIN_NAME = "SHEET_DATA_VALIDATION_PLUGIN";
494
- var _a2;
495
- let DataValidationResourceController = (_a2 = class extends Disposable {
496
- constructor(_resourceManagerService, _univerInstanceService, _dataValidationModel) {
497
- super(), this._resourceManagerService = _resourceManagerService, this._univerInstanceService = _univerInstanceService, this._dataValidationModel = _dataValidationModel, this._initSnapshot();
498
- }
499
- _initSnapshot() {
500
- const toJson = /* @__PURE__ */ __name((unitID) => {
501
- const map = this._dataValidationModel.getUnitRules(unitID), resultMap = {};
502
- return map ? (map.forEach(([key, v]) => {
503
- resultMap[key] = v;
504
- }), JSON.stringify(resultMap)) : "";
505
- }, "toJson"), parseJson = /* @__PURE__ */ __name((json) => {
506
- if (!json)
507
- return {};
508
- try {
509
- return JSON.parse(json);
510
- } catch {
511
- return {};
512
- }
513
- }, "parseJson");
514
- this.disposeWithMe(
515
- this._resourceManagerService.registerPluginResource({
516
- pluginName: DATA_VALIDATION_PLUGIN_NAME,
517
- businesses: [UniverInstanceType.UNIVER_SHEET],
518
- toJson: /* @__PURE__ */ __name((unitID) => toJson(unitID), "toJson"),
519
- parseJson: /* @__PURE__ */ __name((json) => parseJson(json), "parseJson"),
520
- onUnLoad: /* @__PURE__ */ __name((unitID) => {
521
- this._dataValidationModel.deleteUnitRules(unitID);
522
- }, "onUnLoad"),
523
- onLoad: /* @__PURE__ */ __name((unitID, value) => {
524
- Object.keys(value).forEach((subunitId) => {
525
- value[subunitId].forEach((rule) => {
526
- this._dataValidationModel.addRule(unitID, subunitId, rule, "patched");
527
- });
528
- });
529
- }, "onLoad")
530
- })
531
- );
532
- }
533
- }, __name(_a2, "DataValidationResourceController"), _a2);
534
- DataValidationResourceController = __decorateClass$3([
535
- OnLifecycle(LifecycleStages.Ready, DataValidationResourceController),
536
- __decorateParam$3(0, IResourceManagerService),
537
- __decorateParam$3(1, IUniverInstanceService),
538
- __decorateParam$3(2, Inject(DataValidationModel))
539
- ], DataValidationResourceController);
540
- var __defProp$2 = Object.defineProperty, __getOwnPropDesc$2 = Object.getOwnPropertyDescriptor, __decorateClass$2 = /* @__PURE__ */ __name((decorators, target, key, kind) => {
541
- for (var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc$2(target, key) : target, i = decorators.length - 1, decorator; i >= 0; i--)
542
- (decorator = decorators[i]) && (result = (kind ? decorator(target, key, result) : decorator(result)) || result);
543
- return kind && result && __defProp$2(target, key, result), result;
544
- }, "__decorateClass$2"), __decorateParam$2 = /* @__PURE__ */ __name((index, decorator) => (target, key) => decorator(target, key, index), "__decorateParam$2"), _a3;
545
- let DataValidationSheetController = (_a3 = class extends Disposable {
546
- constructor(_sheetInterceptorService, _univerInstanceService, _dataValidationModel) {
547
- super(), this._sheetInterceptorService = _sheetInterceptorService, this._univerInstanceService = _univerInstanceService, this._dataValidationModel = _dataValidationModel, this._initSheetChange();
548
- }
549
- _initSheetChange() {
550
- this.disposeWithMe(
551
- this._sheetInterceptorService.interceptCommand({
552
- getMutations: /* @__PURE__ */ __name((commandInfo) => {
553
- var _a6;
554
- if (commandInfo.id === RemoveSheetCommand.id) {
555
- const params = commandInfo.params, unitId = params.unitId || this._univerInstanceService.getCurrentUnitForType(UniverInstanceType.UNIVER_SHEET).getUnitId(), workbook = this._univerInstanceService.getUniverSheetInstance(unitId);
556
- if (!workbook)
557
- return { redos: [], undos: [] };
558
- const subUnitId = params.subUnitId || ((_a6 = workbook.getActiveSheet()) == null ? void 0 : _a6.getSheetId());
559
- if (!subUnitId)
560
- return { redos: [], undos: [] };
561
- const manager = this._dataValidationModel.ensureManager(unitId, subUnitId);
562
- if (!manager)
563
- return { redos: [], undos: [] };
564
- const rules = manager.getDataValidations(), ids = rules.map((i) => i.uid), redoParams = {
565
- unitId,
566
- subUnitId,
567
- ruleId: ids,
568
- source: "patched"
569
- }, undoParams = {
570
- unitId,
571
- subUnitId,
572
- rule: [...rules],
573
- source: "patched"
574
- };
575
- return {
576
- redos: [{
577
- id: RemoveDataValidationMutation.id,
578
- params: redoParams
579
- }],
580
- undos: [{
581
- id: AddDataValidationMutation.id,
582
- params: undoParams
583
- }]
584
- };
585
- }
586
- return { redos: [], undos: [] };
587
- }, "getMutations")
588
- })
589
- );
590
- }
591
- }, __name(_a3, "DataValidationSheetController"), _a3);
592
- DataValidationSheetController = __decorateClass$2([
593
- OnLifecycle(LifecycleStages.Ready, DataValidationSheetController),
594
- __decorateParam$2(0, Inject(SheetInterceptorService)),
595
- __decorateParam$2(1, Inject(IUniverInstanceService)),
596
- __decorateParam$2(2, Inject(DataValidationModel))
597
- ], DataValidationSheetController);
463
+ }, PLUGIN_CONFIG_KEY = "data-validation.config", defaultPluginConfig = {};
598
464
  var __defProp$1 = Object.defineProperty, __getOwnPropDesc$1 = Object.getOwnPropertyDescriptor, __decorateClass$1 = /* @__PURE__ */ __name((decorators, target, key, kind) => {
599
465
  for (var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc$1(target, key) : target, i = decorators.length - 1, decorator; i >= 0; i--)
600
466
  (decorator = decorators[i]) && (result = (kind ? decorator(target, key, result) : decorator(result)) || result);
601
467
  return kind && result && __defProp$1(target, key, result), result;
602
468
  }, "__decorateClass$1"), __decorateParam$1 = /* @__PURE__ */ __name((index, decorator) => (target, key) => decorator(target, key, index), "__decorateParam$1");
603
469
  const PLUGIN_NAME = "UNIVER_DATA_VALIDATION_PLUGIN";
604
- var _a4;
605
- let UniverDataValidationPlugin = (_a4 = class extends Plugin {
606
- constructor(_config, _injector, _commandService) {
607
- super(), this._injector = _injector, this._commandService = _commandService;
470
+ var _a3;
471
+ let UniverDataValidationPlugin = (_a3 = class extends Plugin {
472
+ constructor(_config = defaultPluginConfig, _injector, _commandService, _configService) {
473
+ super(), this._config = _config, this._injector = _injector, this._commandService = _commandService, this._configService = _configService;
474
+ const { ...rest } = this._config;
475
+ this._configService.setConfig(PLUGIN_CONFIG_KEY, rest);
608
476
  }
609
477
  onStarting() {
610
478
  [
611
479
  [DataValidationModel],
612
480
  [DataValidatorRegistryService],
613
- [DataValidationResourceController],
614
- [DataValidationSheetController]
481
+ [DataValidationResourceController]
615
482
  ].forEach((d) => this._injector.add(d)), [
616
483
  // command
617
484
  AddDataValidationCommand,
@@ -627,10 +494,11 @@ let UniverDataValidationPlugin = (_a4 = class extends Plugin {
627
494
  this._commandService.registerCommand(command);
628
495
  });
629
496
  }
630
- }, __name(_a4, "UniverDataValidationPlugin"), __publicField(_a4, "pluginName", PLUGIN_NAME), __publicField(_a4, "type", UniverInstanceType.UNIVER_SHEET), _a4);
497
+ }, __name(_a3, "UniverDataValidationPlugin"), __publicField(_a3, "pluginName", PLUGIN_NAME), __publicField(_a3, "type", UniverInstanceType.UNIVER_SHEET), _a3);
631
498
  UniverDataValidationPlugin = __decorateClass$1([
632
499
  __decorateParam$1(1, Inject(Injector)),
633
- __decorateParam$1(2, ICommandService)
500
+ __decorateParam$1(2, ICommandService),
501
+ __decorateParam$1(3, IConfigService)
634
502
  ], UniverDataValidationPlugin);
635
503
  DataValidationOperator.BETWEEN + "", DataValidationOperator.EQUAL + "", DataValidationOperator.GREATER_THAN + "", DataValidationOperator.GREATER_THAN_OR_EQUAL + "", DataValidationOperator.LESS_THAN + "", DataValidationOperator.LESS_THAN_OR_EQUAL + "", DataValidationOperator.NOT_BETWEEN + "", DataValidationOperator.NOT_EQUAL + "";
636
504
  const OperatorTitleMap = {
@@ -660,7 +528,10 @@ const OperatorTitleMap = {
660
528
  [DataValidationOperator.LESS_THAN_OR_EQUAL]: "dataValidation.textLength.errorMsg.lessThanOrEqual",
661
529
  [DataValidationOperator.NOT_BETWEEN]: "dataValidation.textLength.errorMsg.notBetween",
662
530
  [DataValidationOperator.NOT_EQUAL]: "dataValidation.textLength.errorMsg.notEqual"
663
- };
531
+ }, TWO_FORMULA_OPERATOR_COUNT = [
532
+ DataValidationOperator.BETWEEN,
533
+ DataValidationOperator.NOT_BETWEEN
534
+ ];
664
535
  var __defProp2 = Object.defineProperty, __getOwnPropDesc = Object.getOwnPropertyDescriptor, __decorateClass = /* @__PURE__ */ __name((decorators, target, key, kind) => {
665
536
  for (var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target, i = decorators.length - 1, decorator; i >= 0; i--)
666
537
  (decorator = decorators[i]) && (result = (kind ? decorator(target, key, result) : decorator(result)) || result);
@@ -676,8 +547,8 @@ const FORMULA1 = "{FORMULA1}", FORMULA2 = "{FORMULA2}", operatorNameMap = {
676
547
  [DataValidationOperator.NOT_BETWEEN]: "dataValidation.operators.notBetween",
677
548
  [DataValidationOperator.NOT_EQUAL]: "dataValidation.operators.notEqual"
678
549
  };
679
- var _a5;
680
- let BaseDataValidator = (_a5 = class {
550
+ var _a4;
551
+ let BaseDataValidator = (_a4 = class {
681
552
  constructor(localeService, injector) {
682
553
  __publicField(this, "canvasRender", null);
683
554
  __publicField(this, "dropdown");
@@ -694,15 +565,15 @@ let BaseDataValidator = (_a5 = class {
694
565
  return !1;
695
566
  }
696
567
  generateRuleName(rule) {
697
- var _a6, _b;
568
+ var _a5, _b;
698
569
  if (!rule.operator)
699
570
  return this.titleStr;
700
- const ruleName = this.localeService.t(OperatorTitleMap[rule.operator]).replace(FORMULA1, (_a6 = rule.formula1) != null ? _a6 : "").replace(FORMULA2, (_b = rule.formula2) != null ? _b : "");
571
+ const ruleName = this.localeService.t(OperatorTitleMap[rule.operator]).replace(FORMULA1, (_a5 = rule.formula1) != null ? _a5 : "").replace(FORMULA2, (_b = rule.formula2) != null ? _b : "");
701
572
  return `${this.titleStr} ${ruleName}`;
702
573
  }
703
574
  generateRuleErrorMessage(rule) {
704
- var _a6, _b;
705
- return rule.operator ? `${this.localeService.t(OperatorErrorTitleMap[rule.operator]).replace(FORMULA1, (_a6 = rule.formula1) != null ? _a6 : "").replace(FORMULA2, (_b = rule.formula2) != null ? _b : "")}` : this.titleStr;
575
+ var _a5, _b;
576
+ return rule.operator ? `${this.localeService.t(OperatorErrorTitleMap[rule.operator]).replace(FORMULA1, (_a5 = rule.formula1) != null ? _a5 : "").replace(FORMULA2, (_b = rule.formula2) != null ? _b : "")}` : this.titleStr;
706
577
  }
707
578
  getRuleFinalError(rule) {
708
579
  return rule.showErrorMessage && rule.error ? rule.error : this.generateRuleErrorMessage(rule);
@@ -710,6 +581,12 @@ let BaseDataValidator = (_a5 = class {
710
581
  isEmptyCellValue(cellValue) {
711
582
  return cellValue === "" || cellValue === void 0 || cellValue === null;
712
583
  }
584
+ normalizeFormula(rule, unitId, subUnitId) {
585
+ return {
586
+ formula1: rule.formula1,
587
+ formula2: rule.formula2
588
+ };
589
+ }
713
590
  async isValidType(cellInfo, formula, rule) {
714
591
  return !0;
715
592
  }
@@ -771,37 +648,24 @@ let BaseDataValidator = (_a5 = class {
771
648
  throw new Error("Unknown operator.");
772
649
  }
773
650
  }
774
- }, __name(_a5, "BaseDataValidator"), _a5);
651
+ }, __name(_a4, "BaseDataValidator"), _a4);
775
652
  BaseDataValidator = __decorateClass([
776
653
  __decorateParam(0, Inject(LocaleService)),
777
654
  __decorateParam(1, Inject(Injector))
778
655
  ], BaseDataValidator);
779
- const TWO_FORMULA_OPERATOR_COUNT = [
780
- DataValidationOperator.BETWEEN,
781
- DataValidationOperator.NOT_BETWEEN
782
- ];
783
656
  export {
784
- AddDataValidationCommand,
785
657
  AddDataValidationMutation,
786
658
  BaseDataValidator,
787
- DataValidationManager,
788
659
  DataValidationModel,
789
660
  DataValidationResourceController,
790
- DataValidationSheetController,
791
661
  DataValidatorRegistryScope,
792
662
  DataValidatorRegistryService,
793
- RemoveAllDataValidationCommand,
794
- RemoveDataValidationCommand,
795
663
  RemoveDataValidationMutation,
796
664
  TWO_FORMULA_OPERATOR_COUNT,
797
665
  TextLengthErrorTitleMap,
798
666
  UniverDataValidationPlugin,
799
667
  UpdateDataValidationMutation,
800
- UpdateDataValidationOptionsCommand,
801
- UpdateDataValidationSettingCommand,
802
668
  UpdateRuleType,
803
- createDefaultNewRule,
804
669
  getRuleOptions,
805
- getRuleSetting,
806
- removeDataValidationUndoFactory
670
+ getRuleSetting
807
671
  };