@univerjs/data-validation 0.1.6
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/LICENSE +176 -0
- package/README.md +28 -0
- package/lib/cjs/index.js +1 -0
- package/lib/es/index.js +667 -0
- package/lib/types/commands/commands/data-validation.command.d.ts +30 -0
- package/lib/types/commands/mutations/data-validation.mutation.d.ts +18 -0
- package/lib/types/common/util.d.ts +12 -0
- package/lib/types/index.d.ts +35 -0
- package/lib/types/models/data-validation-manager.d.ts +22 -0
- package/lib/types/models/data-validation-model.d.ts +39 -0
- package/lib/types/plugin.d.ts +9 -0
- package/lib/types/services/data-validator-registry.service.d.ts +19 -0
- package/lib/types/types/const/operator-text-map.d.ts +5 -0
- package/lib/types/types/const/two-formula-operators.d.ts +3 -0
- package/lib/types/types/enum/update-rule-type.d.ts +20 -0
- package/lib/types/types/interfaces/i-data-validation-drop-down.d.ts +25 -0
- package/lib/types/types/interfaces/i-data-validator-operator-config.d.ts +9 -0
- package/lib/types/types/interfaces/i-formula-input.d.ts +16 -0
- package/lib/types/types/interfaces/i-update-rule-payload.d.ts +16 -0
- package/lib/types/validators/base-data-validator.d.ts +53 -0
- package/lib/types/validators/base-widget.d.ts +5 -0
- package/lib/umd/index.js +1 -0
- package/package.json +60 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { IAccessor } from '@wendellhu/redi';
|
|
2
|
+
import { DataValidationOperator, DataValidationType, IDataValidationRule, IDataValidationRuleBase, IDataValidationRuleOptions } from '@univerjs/core';
|
|
3
|
+
|
|
4
|
+
export declare function getRuleSetting(rule: IDataValidationRule): IDataValidationRuleBase;
|
|
5
|
+
export declare function getRuleOptions(rule: IDataValidationRule): IDataValidationRuleOptions;
|
|
6
|
+
export declare function createDefaultNewRule(accessor: IAccessor): {
|
|
7
|
+
uid: string;
|
|
8
|
+
type: DataValidationType;
|
|
9
|
+
operator: DataValidationOperator;
|
|
10
|
+
formula1: string;
|
|
11
|
+
ranges: import('@univerjs/core').IRange[];
|
|
12
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2023-present DreamNum Inc.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
export { UniverDataValidationPlugin } from './plugin';
|
|
17
|
+
export { DataValidatorRegistryService, DataValidatorRegistryScope } from './services/data-validator-registry.service';
|
|
18
|
+
export { DataValidationModel } from './models/data-validation-model';
|
|
19
|
+
export { AddDataValidationCommand, RemoveDataValidationCommand, RemoveAllDataValidationCommand, UpdateDataValidationOptionsCommand, UpdateDataValidationSettingCommand, } from './commands/commands/data-validation.command';
|
|
20
|
+
export type { IRemoveDataValidationCommandParams, IAddDataValidationCommandParams, IUpdateDataValidationOptionsCommandParams, IUpdateDataValidationSettingCommandParams, IRemoveAllDataValidationCommandParams, } from './commands/commands/data-validation.command';
|
|
21
|
+
export { AddDataValidationMutation, RemoveDataValidationMutation, UpdateDataValidationMutation, } from './commands/mutations/data-validation.mutation';
|
|
22
|
+
export type { IAddDataValidationMutationParams, IRemoveDataValidationMutationParams, IUpdateDataValidationMutationParams, } from './commands/mutations/data-validation.mutation';
|
|
23
|
+
export { createDefaultNewRule, getRuleOptions, getRuleSetting, } from './common/util';
|
|
24
|
+
export { UpdateRuleType } from './types/enum/update-rule-type';
|
|
25
|
+
export type { IDataValidatorOperatorConfig } from './types/interfaces/i-data-validator-operator-config';
|
|
26
|
+
export type { IFormulaInputProps, IFormulaValue, FormulaInputType } from './types/interfaces/i-formula-input';
|
|
27
|
+
export type { IUpdateRuleOptionsPayload, IUpdateRulePayload, IUpdateRuleRangePayload, IUpdateRuleSettingPayload } from './types/interfaces/i-update-rule-payload';
|
|
28
|
+
export type { IDataValidationDropdownProps } from './types/interfaces/i-data-validation-drop-down';
|
|
29
|
+
export { BaseDataValidator } from './validators/base-data-validator';
|
|
30
|
+
export type { IFormulaResult, IValidatorCellInfo } from './validators/base-data-validator';
|
|
31
|
+
export type { IBaseDataValidationWidget } from './validators/base-widget';
|
|
32
|
+
export { DataValidationManager } from './models/data-validation-manager';
|
|
33
|
+
export type { IFormulaValidResult } from './validators/base-data-validator';
|
|
34
|
+
export { removeDataValidationUndoFactory } from './commands/commands/data-validation.command';
|
|
35
|
+
export { TWO_FORMULA_OPERATOR_COUNT } from './types/const/two-formula-operators';
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { IUpdateRulePayload } from '../types/interfaces/i-update-rule-payload';
|
|
2
|
+
import { CellValue, IDataValidationRule, Nullable, DataValidationStatus, Disposable } from '@univerjs/core';
|
|
3
|
+
|
|
4
|
+
export declare class DataValidationManager<T extends IDataValidationRule> extends Disposable {
|
|
5
|
+
private _dataValidations;
|
|
6
|
+
private _dataValidationMap;
|
|
7
|
+
private _dataValidations$;
|
|
8
|
+
readonly unitId: string;
|
|
9
|
+
readonly subUnitId: string;
|
|
10
|
+
readonly dataValidations$: import('rxjs').Observable<T[]>;
|
|
11
|
+
constructor(unitId: string, subUnitId: string, dataValidations: T[] | undefined);
|
|
12
|
+
private _notice;
|
|
13
|
+
private _insertRules;
|
|
14
|
+
getRuleById(id: string): T | undefined;
|
|
15
|
+
getRuleIndex(id: string): number;
|
|
16
|
+
addRule(rule: T, index?: number): void;
|
|
17
|
+
removeRule(ruleId: string): void;
|
|
18
|
+
updateRule(ruleId: string, payload: IUpdateRulePayload): T;
|
|
19
|
+
getDataValidations(): T[];
|
|
20
|
+
toJSON(): T[];
|
|
21
|
+
validator(_content: Nullable<CellValue>, _rule: IDataValidationRule, _pos: any, _onCompele: (status: DataValidationStatus) => void): DataValidationStatus;
|
|
22
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { DataValidationManager } from './data-validation-manager';
|
|
2
|
+
import { IUpdateRulePayload } from '../types/interfaces/i-update-rule-payload';
|
|
3
|
+
import { CellValue, DataValidationStatus, Disposable, IDataValidationRule, ILogService, Nullable } from '@univerjs/core';
|
|
4
|
+
|
|
5
|
+
type RuleChangeType = 'update' | 'add' | 'remove';
|
|
6
|
+
export interface IRuleChange<T extends IDataValidationRule> {
|
|
7
|
+
rule?: T;
|
|
8
|
+
type: RuleChangeType;
|
|
9
|
+
unitId: string;
|
|
10
|
+
subUnitId: string;
|
|
11
|
+
}
|
|
12
|
+
export interface IValidStatusChange {
|
|
13
|
+
unitId: string;
|
|
14
|
+
subUnitId: string;
|
|
15
|
+
ruleId: string;
|
|
16
|
+
status: DataValidationStatus;
|
|
17
|
+
}
|
|
18
|
+
export declare class DataValidationModel<T extends IDataValidationRule = IDataValidationRule> extends Disposable {
|
|
19
|
+
private readonly _logService;
|
|
20
|
+
private readonly _model;
|
|
21
|
+
private _managerCreator;
|
|
22
|
+
private readonly _ruleChange$;
|
|
23
|
+
private readonly _validStatusChange$;
|
|
24
|
+
ruleChange$: import('rxjs').Observable<IRuleChange<T>>;
|
|
25
|
+
ruleChangeDebounce$: import('rxjs').Observable<IRuleChange<T>>;
|
|
26
|
+
validStatusChange$: import('rxjs').Observable<IValidStatusChange>;
|
|
27
|
+
constructor(_logService: ILogService);
|
|
28
|
+
setManagerCreator(creator: (unitId: string, subUnitId: string) => DataValidationManager<T>): void;
|
|
29
|
+
ensureManager(unitId: string, subUnitId: string): DataValidationManager<T>;
|
|
30
|
+
addRule(unitId: string, subUnitId: string, rule: T, index?: number): void;
|
|
31
|
+
updateRule(unitId: string, subUnitId: string, ruleId: string, payload: IUpdateRulePayload): void;
|
|
32
|
+
removeRule(unitId: string, subUnitId: string, ruleId: string): void;
|
|
33
|
+
getRuleById(unitId: string, subUnitId: string, ruleId: string): T | undefined;
|
|
34
|
+
getRuleIndex(unitId: string, subUnitId: string, ruleId: string): number;
|
|
35
|
+
getRules(unitId: string, subUnitId: string): T[];
|
|
36
|
+
validator(content: Nullable<CellValue>, rule: T, pos: any): DataValidationStatus;
|
|
37
|
+
getUnitRules(unitId: string): [string, IDataValidationRule[]][];
|
|
38
|
+
}
|
|
39
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Injector } from '@wendellhu/redi';
|
|
2
|
+
import { ICommandService, Plugin } from '@univerjs/core';
|
|
3
|
+
|
|
4
|
+
export declare class UniverDataValidationPlugin extends Plugin {
|
|
5
|
+
protected _injector: Injector;
|
|
6
|
+
private _commandService;
|
|
7
|
+
constructor(_injector: Injector, _commandService: ICommandService);
|
|
8
|
+
onStarting(injector: Injector): void;
|
|
9
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { BaseDataValidator } from '../validators/base-data-validator';
|
|
2
|
+
|
|
3
|
+
export declare enum DataValidatorRegistryScope {
|
|
4
|
+
SHEET = "sheet"
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Register data validator
|
|
8
|
+
*/
|
|
9
|
+
export declare class DataValidatorRegistryService {
|
|
10
|
+
private _validatorByScopes;
|
|
11
|
+
private _validatorMap;
|
|
12
|
+
private _validatorsChange$;
|
|
13
|
+
validatorsChange$: import('rxjs').Observable<void>;
|
|
14
|
+
private _addValidatorToScope;
|
|
15
|
+
private _removeValidatorFromScope;
|
|
16
|
+
register(validator: BaseDataValidator<any>): import('@wendellhu/redi').IDisposable;
|
|
17
|
+
getValidatorItem(id: string): BaseDataValidator<import('@univerjs/core').CellValue> | undefined;
|
|
18
|
+
getValidatorsByScope(scope: string): BaseDataValidator<import('@univerjs/core').CellValue>[] | undefined;
|
|
19
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { DataValidationOperator } from '@univerjs/core';
|
|
2
|
+
|
|
3
|
+
export declare const OperatorTextMap: Record<DataValidationOperator, string>;
|
|
4
|
+
export declare const OperatorTitleMap: Record<DataValidationOperator, string>;
|
|
5
|
+
export declare const OperatorErrorTitleMap: Record<DataValidationOperator, string>;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2023-present DreamNum Inc.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
export declare enum UpdateRuleType {
|
|
17
|
+
SETTING = 0,
|
|
18
|
+
RANGE = 1,
|
|
19
|
+
OPTIONS = 2
|
|
20
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2023-present DreamNum Inc.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
export interface IDataValidationDropdownProps {
|
|
17
|
+
top: number;
|
|
18
|
+
left: number;
|
|
19
|
+
width: number;
|
|
20
|
+
height: number;
|
|
21
|
+
unitId: string;
|
|
22
|
+
subUnitId: string;
|
|
23
|
+
row: number;
|
|
24
|
+
col: number;
|
|
25
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { IFormulaValidResult } from '../../validators/base-data-validator';
|
|
2
|
+
|
|
3
|
+
export interface IFormulaValue {
|
|
4
|
+
formula1?: string;
|
|
5
|
+
formula2?: string;
|
|
6
|
+
}
|
|
7
|
+
export interface IFormulaInputProps {
|
|
8
|
+
isTwoFormula?: boolean;
|
|
9
|
+
value?: IFormulaValue;
|
|
10
|
+
onChange?: (value?: IFormulaValue) => void;
|
|
11
|
+
unitId: string;
|
|
12
|
+
subUnitId: string;
|
|
13
|
+
showError?: boolean;
|
|
14
|
+
validResult?: IFormulaValidResult;
|
|
15
|
+
}
|
|
16
|
+
export type FormulaInputType = React.ComponentType<IFormulaInputProps>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { UpdateRuleType } from '../enum/update-rule-type';
|
|
2
|
+
import { IDataValidationRuleBase, IDataValidationRuleOptions } from '@univerjs/core';
|
|
3
|
+
|
|
4
|
+
export interface IUpdateRuleRangePayload {
|
|
5
|
+
type: UpdateRuleType.RANGE;
|
|
6
|
+
payload: any[];
|
|
7
|
+
}
|
|
8
|
+
export interface IUpdateRuleSettingPayload {
|
|
9
|
+
type: UpdateRuleType.SETTING;
|
|
10
|
+
payload: IDataValidationRuleBase;
|
|
11
|
+
}
|
|
12
|
+
export interface IUpdateRuleOptionsPayload {
|
|
13
|
+
type: UpdateRuleType.OPTIONS;
|
|
14
|
+
payload: Partial<IDataValidationRuleOptions>;
|
|
15
|
+
}
|
|
16
|
+
export type IUpdateRulePayload = IUpdateRuleRangePayload | IUpdateRuleSettingPayload | IUpdateRuleOptionsPayload;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { IBaseDataValidationWidget } from './base-widget';
|
|
2
|
+
import { Injector } from '@wendellhu/redi';
|
|
3
|
+
import { CellValue, IDataValidationRule, IDataValidationRuleBase, Nullable, DataValidationOperator, LocaleService } from '@univerjs/core';
|
|
4
|
+
|
|
5
|
+
export interface IValidatorCellInfo<DataType = Nullable<CellValue>> {
|
|
6
|
+
value: DataType;
|
|
7
|
+
row: number;
|
|
8
|
+
column: number;
|
|
9
|
+
unitId: string;
|
|
10
|
+
subUnitId: string;
|
|
11
|
+
}
|
|
12
|
+
export interface IFormulaResult<T = any> {
|
|
13
|
+
formula1: T;
|
|
14
|
+
formula2: T;
|
|
15
|
+
}
|
|
16
|
+
export interface IFormulaValidResult {
|
|
17
|
+
success: boolean;
|
|
18
|
+
formula1?: string;
|
|
19
|
+
formula2?: string;
|
|
20
|
+
}
|
|
21
|
+
export declare abstract class BaseDataValidator<DataType = CellValue> {
|
|
22
|
+
readonly localeService: LocaleService;
|
|
23
|
+
readonly injector: Injector;
|
|
24
|
+
abstract id: string;
|
|
25
|
+
abstract title: string;
|
|
26
|
+
abstract operators: DataValidationOperator[];
|
|
27
|
+
abstract scopes: string[] | string;
|
|
28
|
+
abstract formulaInput: string;
|
|
29
|
+
canvasRender: Nullable<IBaseDataValidationWidget>;
|
|
30
|
+
dropdown: string | undefined;
|
|
31
|
+
optionsInput: string | undefined;
|
|
32
|
+
constructor(localeService: LocaleService, injector: Injector);
|
|
33
|
+
get operatorNames(): string[];
|
|
34
|
+
get titleStr(): string;
|
|
35
|
+
skipDefaultFontRender(rule: IDataValidationRule, cellValue: Nullable<CellValue>, pos: any): boolean;
|
|
36
|
+
generateRuleName(rule: IDataValidationRuleBase): string;
|
|
37
|
+
generateRuleErrorMessage(rule: IDataValidationRuleBase): string;
|
|
38
|
+
getRuleFinalError(rule: IDataValidationRule): string;
|
|
39
|
+
isEmptyCellValue(cellValue: Nullable<CellValue>): cellValue is null | undefined | void;
|
|
40
|
+
abstract parseFormula(rule: IDataValidationRule, unitId: string, subUnitId: string): Promise<IFormulaResult>;
|
|
41
|
+
abstract validatorFormula(rule: IDataValidationRuleBase): IFormulaValidResult;
|
|
42
|
+
isValidType(cellInfo: IValidatorCellInfo, formula: IFormulaResult, rule: IDataValidationRule): Promise<boolean>;
|
|
43
|
+
transform(cellInfo: IValidatorCellInfo, formula: IFormulaResult, rule: IDataValidationRule): IValidatorCellInfo<DataType>;
|
|
44
|
+
validatorIsEqual(cellInfo: IValidatorCellInfo<DataType>, formula: IFormulaResult, rule: IDataValidationRule): Promise<boolean>;
|
|
45
|
+
validatorIsNotEqual(cellInfo: IValidatorCellInfo<DataType>, formula: IFormulaResult, rule: IDataValidationRule): Promise<boolean>;
|
|
46
|
+
validatorIsBetween(cellInfo: IValidatorCellInfo<DataType>, formula: IFormulaResult, rule: IDataValidationRule): Promise<boolean>;
|
|
47
|
+
validatorIsNotBetween(cellInfo: IValidatorCellInfo<DataType>, formula: IFormulaResult, rule: IDataValidationRule): Promise<boolean>;
|
|
48
|
+
validatorIsGreaterThan(cellInfo: IValidatorCellInfo<DataType>, formula: IFormulaResult, rule: IDataValidationRule): Promise<boolean>;
|
|
49
|
+
validatorIsGreaterThanOrEqual(cellInfo: IValidatorCellInfo<DataType>, formula: IFormulaResult, rule: IDataValidationRule): Promise<boolean>;
|
|
50
|
+
validatorIsLessThan(cellInfo: IValidatorCellInfo<DataType>, formula: IFormulaResult, rule: IDataValidationRule): Promise<boolean>;
|
|
51
|
+
validatorIsLessThanOrEqual(cellInfo: IValidatorCellInfo<DataType>, formula: IFormulaResult, rule: IDataValidationRule): Promise<boolean>;
|
|
52
|
+
validator(cellInfo: IValidatorCellInfo, rule: IDataValidationRule): Promise<boolean>;
|
|
53
|
+
}
|
package/lib/umd/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(l,n){typeof exports=="object"&&typeof module<"u"?n(exports,require("@univerjs/core"),require("@wendellhu/redi"),require("rxjs"),require("@univerjs/sheets")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","@wendellhu/redi","rxjs","@univerjs/sheets"],n):(l=typeof globalThis<"u"?globalThis:l||self,n(l.UniverDataValidation={},l.UniverCore,l["@wendellhu/redi"],l.rxjs,l.UniverSheets))})(this,function(l,n,h,E,b){"use strict";var ia=Object.defineProperty;var na=(l,n,h)=>n in l?ia(l,n,{enumerable:!0,configurable:!0,writable:!0,value:h}):l[n]=h;var p=(l,n,h)=>(na(l,typeof n!="symbol"?n+"":n,h),h);var M=(r=>(r.SHEET="sheet",r))(M||{});class T{constructor(){p(this,"_validatorByScopes",new Map);p(this,"_validatorMap",new Map);p(this,"_validatorsChange$",new E.BehaviorSubject(void 0));p(this,"validatorsChange$",this._validatorsChange$.asObservable())}_addValidatorToScope(t,a){this._validatorByScopes.has(a)||this._validatorByScopes.set(a,[]);const e=this._validatorByScopes.get(a);if(e.findIndex(i=>i.id===t.id)>-1)throw new Error(`Validator item with the same id ${t.id} has already been added!`);e.push(t)}_removeValidatorFromScope(t,a){const e=this._validatorByScopes.get(a);if(!e)return;const i=e.findIndex(o=>o.id===t.id);i>-1&&e.splice(i,1)}register(t){return this._validatorMap.set(t.id,t),Array.isArray(t.scopes)?t.scopes.forEach(a=>{this._addValidatorToScope(t,a)}):this._addValidatorToScope(t,t.scopes),this._validatorsChange$.next(),n.toDisposable(()=>{this._validatorMap.delete(t.id),Array.isArray(t.scopes)?t.scopes.forEach(a=>{this._removeValidatorFromScope(t,a)}):this._removeValidatorFromScope(t,t.scopes),this._validatorsChange$.next()})}getValidatorItem(t){return this._validatorMap.get(t)}getValidatorsByScope(t){return this._validatorByScopes.get(t)}}var $=Object.defineProperty,P=Object.getOwnPropertyDescriptor,q=(r,t,a,e)=>{for(var i=e>1?void 0:e?P(t,a):t,o=r.length-1,d;o>=0;o--)(d=r[o])&&(i=(e?d(t,a,i):d(i))||i);return e&&i&&$(t,a,i),i},j=(r,t)=>(a,e)=>t(a,e,r);l.DataValidationModel=class extends n.Disposable{constructor(a){super();p(this,"_model",new Map);p(this,"_managerCreator");p(this,"_ruleChange$",new E.Subject);p(this,"_validStatusChange$",new E.Subject);p(this,"ruleChange$",this._ruleChange$.asObservable());p(this,"ruleChangeDebounce$",this.ruleChange$.pipe(E.debounceTime(20)));p(this,"validStatusChange$",this._validStatusChange$.asObservable().pipe(E.debounceTime(20)));this._logService=a,this.disposeWithMe({dispose:()=>{this._ruleChange$.complete(),this._validStatusChange$.complete()}})}setManagerCreator(a){this._managerCreator=a}ensureManager(a,e){this._model.has(a)||this._model.set(a,new Map);const i=this._model.get(a);if(i.has(e))return i.get(e);const o=this._managerCreator(a,e);return i.set(e,o),o}addRule(a,e,i,o){try{this.ensureManager(a,e).addRule(i,o),this._ruleChange$.next({rule:i,type:"add",unitId:a,subUnitId:e})}catch(d){this._logService.error(d)}}updateRule(a,e,i,o){try{const s=this.ensureManager(a,e).updateRule(i,o);this._ruleChange$.next({rule:s,type:"update",unitId:a,subUnitId:e})}catch(d){this._logService.error(d)}}removeRule(a,e,i){try{const o=this.ensureManager(a,e),d=o.getRuleById(i);o.removeRule(i),this._ruleChange$.next({rule:d,type:"remove",unitId:a,subUnitId:e})}catch(o){this._logService.error(o)}}getRuleById(a,e,i){return this.ensureManager(a,e).getRuleById(i)}getRuleIndex(a,e,i){return this.ensureManager(a,e).getRuleIndex(i)}getRules(a,e){return this.ensureManager(a,e).getDataValidations()}validator(a,e,i){const{unitId:o,subUnitId:d}=i;return this.ensureManager(o,d).validator(a,e,i,c=>{this._validStatusChange$.next({unitId:o,subUnitId:d,ruleId:e.uid,status:c})})}getUnitRules(a){const e=this._model.get(a);if(!e)return[];const i=[];return e.forEach(o=>{i.push([o.subUnitId,o.getDataValidations()])}),i}},l.DataValidationModel=q([j(0,n.ILogService)],l.DataValidationModel);const g={type:n.CommandType.MUTATION,id:"data-validation.mutation.addRule",handler(r,t){if(!t)return!1;const{unitId:a,subUnitId:e,rule:i,index:o}=t,d=r.get(l.DataValidationModel);return Array.isArray(i)?i.forEach(s=>{d.addRule(a,e,s,o)}):d.addRule(a,e,i,o),!0}},v={type:n.CommandType.MUTATION,id:"data-validation.mutation.removeRule",handler(r,t){if(!t)return!1;const{unitId:a,subUnitId:e,ruleId:i}=t,o=r.get(l.DataValidationModel);return Array.isArray(i)?i.forEach(d=>{o.removeRule(a,e,d)}):o.removeRule(a,e,i),!0}},_={type:n.CommandType.MUTATION,id:"data-validation.mutation.updateRule",handler(r,t){if(!t)return!1;const{unitId:a,subUnitId:e,ruleId:i,payload:o}=t;return r.get(l.DataValidationModel).updateRule(a,e,i,o),!0}};function R(r){return{type:r.type,operator:r.operator,formula1:r.formula1,formula2:r.formula2,allowBlank:r.allowBlank}}function S(r){return{error:r.error,errorStyle:r.errorStyle,errorTitle:r.errorTitle,imeMode:r.imeMode,prompt:r.prompt,promptTitle:r.promptTitle,showDropDown:r.showDropDown,showErrorMessage:r.showErrorMessage,showInputMessage:r.showInputMessage,renderMode:r.renderMode}}function G(r){const a=r.get(b.SelectionManagerService).getSelectionRanges();return{uid:n.Tools.generateRandomId(6),type:n.DataValidationType.DECIMAL,operator:n.DataValidationOperator.EQUAL,formula1:"100",ranges:a!=null?a:[{startColumn:0,endColumn:0,startRow:0,endRow:0}]}}var V=(r=>(r[r.SETTING=0]="SETTING",r[r.RANGE=1]="RANGE",r[r.OPTIONS=2]="OPTIONS",r))(V||{});const I={type:n.CommandType.COMMAND,id:"data-validation.command.addRule",async handler(r,t){if(!t)return!1;const{rule:a,unitId:e,subUnitId:i}=t,o=r.get(n.ICommandService),d=r.get(n.IUndoRedoService),s={...t,rule:{...t.rule,ranges:[t.rule.range]}},c=[{id:g.id,params:s}],u=[{id:v.id,params:{unitId:e,subUnitId:i,ruleId:a.uid}}];return d.pushUndoRedo({unitID:e,redoMutations:c,undoMutations:u}),await o.executeCommand(g.id,s),!0}},H=(r,t)=>{const a=r.get(l.DataValidationModel),{unitId:e,subUnitId:i,ruleId:o}=t;if(Array.isArray(o)){const s=o.map(c=>a.getRuleById(e,i,c)).filter(Boolean);return[{id:g.id,params:{unitId:e,subUnitId:i,rule:s}}]}return[{id:g.id,params:{unitId:e,subUnitId:i,rule:{...a.getRuleById(e,i,o)},index:a.getRuleIndex(e,i,o)}}]},y={type:n.CommandType.COMMAND,id:"data-validation.command.removeRule",handler(r,t){if(!t)return!1;const{unitId:a,subUnitId:e,ruleId:i}=t,o=r.get(n.ICommandService),d=r.get(n.IUndoRedoService),s=r.get(l.DataValidationModel),c=[{id:v.id,params:t}],u=[{id:g.id,params:{unitId:a,subUnitId:e,rule:{...s.getRuleById(a,e,i)},index:s.getRuleIndex(a,e,i)}}];return d.pushUndoRedo({undoMutations:u,redoMutations:c,unitID:t.unitId}),o.executeCommand(v.id,t),!0}},A={type:n.CommandType.COMMAND,id:"data-validation.command.updateDataValidationSetting",handler(r,t){if(!t)return!1;const a=r.get(n.ICommandService),e=r.get(n.IUndoRedoService),i=r.get(l.DataValidationModel),{unitId:o,subUnitId:d,ruleId:s,options:c}=t,u=i.getRuleById(o,d,s);if(!u)return!1;const m={unitId:o,subUnitId:d,ruleId:s,payload:{type:V.OPTIONS,payload:c}},O=[{id:_.id,params:m}],f={unitId:o,subUnitId:d,ruleId:s,payload:{type:V.OPTIONS,payload:S(u)}},D=[{id:_.id,params:f}];return e.pushUndoRedo({unitID:o,redoMutations:O,undoMutations:D}),a.executeCommand(_.id,m),!0}},N={type:n.CommandType.COMMAND,id:"data-validation.command.updateDataValidationOptions",handler(r,t){if(!t)return!1;const a=r.get(n.ICommandService),e=r.get(n.IUndoRedoService),i=r.get(l.DataValidationModel),o=r.get(T),{unitId:d,subUnitId:s,ruleId:c,setting:u}=t,m=o.getValidatorItem(u.type);if(!m||!m.validatorFormula(u).success)return!1;const O=i.getRuleById(d,s,c);if(!O)return!1;const f={unitId:d,subUnitId:s,ruleId:c,payload:{type:V.SETTING,payload:u}},D=[{id:_.id,params:f}],ta={unitId:d,subUnitId:s,ruleId:c,payload:{type:V.SETTING,payload:R(O)}},ea=[{id:_.id,params:ta}];return e.pushUndoRedo({unitID:d,redoMutations:D,undoMutations:ea}),a.executeCommand(_.id,f),!0}},C={type:n.CommandType.COMMAND,id:"data-validation.command.removeAll",handler(r,t){if(!t)return!1;const{unitId:a,subUnitId:e}=t,i=r.get(n.ICommandService),o=r.get(l.DataValidationModel),d=r.get(n.IUndoRedoService),s=[...o.getRules(a,e)],c={unitId:a,subUnitId:e,ruleId:s.map(O=>O.uid)},u=[{id:v.id,params:c}],m=[{id:g.id,params:{unitId:a,subUnitId:e,rule:s}}];return d.pushUndoRedo({redoMutations:u,undoMutations:m,unitID:a}),i.executeCommand(v.id,c),!0}};var Q=Object.defineProperty,W=Object.getOwnPropertyDescriptor,F=(r,t,a,e)=>{for(var i=e>1?void 0:e?W(t,a):t,o=r.length-1,d;o>=0;o--)(d=r[o])&&(i=(e?d(t,a,i):d(i))||i);return e&&i&&Q(t,a,i),i},U=(r,t)=>(a,e)=>t(a,e,r);const J="data-validation";l.UniverDataValidationPlugin=class extends n.Plugin{constructor(t,a){super(J),this._injector=t,this._commandService=a}onStarting(t){[[l.DataValidationModel],[T]].forEach(a=>{t.add(a)}),[I,C,A,N,y,g,_,v].forEach(a=>{this._commandService.registerCommand(a)})}},l.UniverDataValidationPlugin=F([U(0,h.Inject(h.Injector)),U(1,n.ICommandService)],l.UniverDataValidationPlugin),n.DataValidationOperator.BETWEEN+"",n.DataValidationOperator.EQUAL+"",n.DataValidationOperator.GREATER_THAN+"",n.DataValidationOperator.GREATER_THAN_OR_EQUAL+"",n.DataValidationOperator.LESS_THAN+"",n.DataValidationOperator.LESS_THAN_OR_EQUAL+"",n.DataValidationOperator.NOT_BETWEEN+"",n.DataValidationOperator.NOT_EQUAL+"";const z={[n.DataValidationOperator.BETWEEN]:"dataValidation.ruleName.between",[n.DataValidationOperator.EQUAL]:"dataValidation.ruleName.equal",[n.DataValidationOperator.GREATER_THAN]:"dataValidation.ruleName.greaterThan",[n.DataValidationOperator.GREATER_THAN_OR_EQUAL]:"dataValidation.ruleName.greaterThanOrEqual",[n.DataValidationOperator.LESS_THAN]:"dataValidation.ruleName.lessThan",[n.DataValidationOperator.LESS_THAN_OR_EQUAL]:"dataValidation.ruleName.lessThanOrEqual",[n.DataValidationOperator.NOT_BETWEEN]:"dataValidation.ruleName.notBetween",[n.DataValidationOperator.NOT_EQUAL]:"dataValidation.ruleName.notEqual"},K={[n.DataValidationOperator.BETWEEN]:"dataValidation.errorMsg.between",[n.DataValidationOperator.EQUAL]:"dataValidation.errorMsg.equal",[n.DataValidationOperator.GREATER_THAN]:"dataValidation.errorMsg.greaterThan",[n.DataValidationOperator.GREATER_THAN_OR_EQUAL]:"dataValidation.errorMsg.greaterThanOrEqual",[n.DataValidationOperator.LESS_THAN]:"dataValidation.errorMsg.lessThan",[n.DataValidationOperator.LESS_THAN_OR_EQUAL]:"dataValidation.errorMsg.lessThanOrEqual",[n.DataValidationOperator.NOT_BETWEEN]:"dataValidation.errorMsg.notBetween",[n.DataValidationOperator.NOT_EQUAL]:"dataValidation.errorMsg.notEqual"};var X=Object.defineProperty,Y=Object.getOwnPropertyDescriptor,Z=(r,t,a,e)=>{for(var i=e>1?void 0:e?Y(t,a):t,o=r.length-1,d;o>=0;o--)(d=r[o])&&(i=(e?d(t,a,i):d(i))||i);return e&&i&&X(t,a,i),i},w=(r,t)=>(a,e)=>t(a,e,r);const L="{FORMULA1}",B="{FORMULA2}",x={[n.DataValidationOperator.BETWEEN]:"dataValidation.operators.between",[n.DataValidationOperator.EQUAL]:"dataValidation.operators.equal",[n.DataValidationOperator.GREATER_THAN]:"dataValidation.operators.greaterThan",[n.DataValidationOperator.GREATER_THAN_OR_EQUAL]:"dataValidation.operators.greaterThanOrEqual",[n.DataValidationOperator.LESS_THAN]:"dataValidation.operators.lessThan",[n.DataValidationOperator.LESS_THAN_OR_EQUAL]:"dataValidation.operators.lessThanOrEqual",[n.DataValidationOperator.NOT_BETWEEN]:"dataValidation.operators.notBetween",[n.DataValidationOperator.NOT_EQUAL]:"dataValidation.operators.notEqual"};l.BaseDataValidator=class{constructor(t,a){p(this,"canvasRender",null);p(this,"dropdown");p(this,"optionsInput");this.localeService=t,this.injector=a}get operatorNames(){return this.operators.map(t=>this.localeService.t(x[t]))}get titleStr(){return this.localeService.t(this.title)}skipDefaultFontRender(t,a,e){return!1}generateRuleName(t){var e,i;if(!t.operator)return this.titleStr;const a=this.localeService.t(z[t.operator]).replace(L,(e=t.formula1)!=null?e:"").replace(B,(i=t.formula2)!=null?i:"");return`${this.titleStr} ${a}`}generateRuleErrorMessage(t){var e,i;return t.operator?`${this.localeService.t(K[t.operator]).replace(L,(e=t.formula1)!=null?e:"").replace(B,(i=t.formula2)!=null?i:"")}`:this.titleStr}getRuleFinalError(t){return t.showInputMessage&&t.error?t.error:this.generateRuleErrorMessage(t)}isEmptyCellValue(t){return t===""||t===void 0||t===null}async isValidType(t,a,e){return!0}transform(t,a,e){return t}async validatorIsEqual(t,a,e){return!0}async validatorIsNotEqual(t,a,e){return!0}async validatorIsBetween(t,a,e){return!0}async validatorIsNotBetween(t,a,e){return!0}async validatorIsGreaterThan(t,a,e){return!0}async validatorIsGreaterThanOrEqual(t,a,e){return!0}async validatorIsLessThan(t,a,e){return!0}async validatorIsLessThanOrEqual(t,a,e){return!0}async validator(t,a){const{value:e,unitId:i,subUnitId:o}=t,d=this.isEmptyCellValue(e),{allowBlank:s=!0,operator:c}=a;if(d)return s;const u=await this.parseFormula(a,i,o);if(!await this.isValidType(t,u,a))return!1;if(!n.Tools.isDefine(c))return!0;const m=this.transform(t,u,a);switch(c){case n.DataValidationOperator.BETWEEN:return this.validatorIsBetween(m,u,a);case n.DataValidationOperator.EQUAL:return this.validatorIsEqual(m,u,a);case n.DataValidationOperator.GREATER_THAN:return this.validatorIsGreaterThan(m,u,a);case n.DataValidationOperator.GREATER_THAN_OR_EQUAL:return this.validatorIsGreaterThanOrEqual(m,u,a);case n.DataValidationOperator.LESS_THAN:return this.validatorIsLessThan(m,u,a);case n.DataValidationOperator.LESS_THAN_OR_EQUAL:return this.validatorIsLessThanOrEqual(m,u,a);case n.DataValidationOperator.NOT_BETWEEN:return this.validatorIsNotBetween(m,u,a);case n.DataValidationOperator.NOT_EQUAL:return this.validatorIsNotEqual(m,u,a);default:throw new Error("Unknown operator.")}}},l.BaseDataValidator=Z([w(0,h.Inject(n.LocaleService)),w(1,h.Inject(h.Injector))],l.BaseDataValidator);class k extends n.Disposable{constructor(a,e,i){super();p(this,"_dataValidations");p(this,"_dataValidationMap",new Map);p(this,"_dataValidations$",new E.Subject);p(this,"unitId");p(this,"subUnitId");p(this,"dataValidations$",this._dataValidations$.asObservable());this.unitId=a,this.subUnitId=e,i&&(this._insertRules(i),this._notice(),this.disposeWithMe({dispose:()=>{this._dataValidations$.complete()}}))}_notice(){this._dataValidations$.next(this._dataValidations)}_insertRules(a){this._dataValidations=a,a.forEach(e=>{this._dataValidationMap.set(e.uid,e)})}getRuleById(a){return this._dataValidationMap.get(a)}getRuleIndex(a){return this._dataValidations.findIndex(e=>e.uid===a)}addRule(a,e){typeof e=="number"&&e<this._dataValidations.length?this._dataValidations.splice(e,0,a):this._dataValidations.push(a),this._dataValidationMap.set(a.uid,a),this._notice()}removeRule(a){const e=this._dataValidations.findIndex(i=>i.uid===a);this._dataValidations.splice(e,1),this._dataValidationMap.delete(a),this._notice()}updateRule(a,e){const i=this._dataValidationMap.get(a),o=this._dataValidations.findIndex(s=>a===s.uid);if(!i)throw new Error(`Data validation rule is not found, ruleId: ${a}.`);const d={...i};switch(e.type){case V.RANGE:{d.ranges=e.payload;break}case V.SETTING:{Object.assign(d,e.payload);break}case V.OPTIONS:{Object.assign(d,e.payload);break}}return this._dataValidations[o]=d,this._dataValidationMap.set(a,d),this._notice(),d}getDataValidations(){return this._dataValidations}toJSON(){return this._dataValidations}validator(a,e,i,o){return n.DataValidationStatus.VALID}}const aa=[n.DataValidationOperator.BETWEEN,n.DataValidationOperator.NOT_BETWEEN];l.AddDataValidationCommand=I,l.AddDataValidationMutation=g,l.DataValidationManager=k,l.DataValidatorRegistryScope=M,l.DataValidatorRegistryService=T,l.RemoveAllDataValidationCommand=C,l.RemoveDataValidationCommand=y,l.RemoveDataValidationMutation=v,l.TWO_FORMULA_OPERATOR_COUNT=aa,l.UpdateDataValidationMutation=_,l.UpdateDataValidationOptionsCommand=A,l.UpdateDataValidationSettingCommand=N,l.UpdateRuleType=V,l.createDefaultNewRule=G,l.getRuleOptions=S,l.getRuleSetting=R,l.removeDataValidationUndoFactory=H,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@univerjs/data-validation",
|
|
3
|
+
"version": "0.1.6",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "",
|
|
6
|
+
"author": "univer.ai",
|
|
7
|
+
"license": "",
|
|
8
|
+
"keywords": [],
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": "./lib/es/index.js",
|
|
12
|
+
"require": "./lib/cjs/index.js",
|
|
13
|
+
"types": "./lib/types/index.d.ts"
|
|
14
|
+
},
|
|
15
|
+
"./*": {
|
|
16
|
+
"import": "./lib/es/*",
|
|
17
|
+
"require": "./lib/cjs/*",
|
|
18
|
+
"types": "./lib/types/index.d.ts"
|
|
19
|
+
},
|
|
20
|
+
"./lib/*": "./lib/*"
|
|
21
|
+
},
|
|
22
|
+
"main": "./lib/cjs/index.js",
|
|
23
|
+
"module": "./lib/es/index.js",
|
|
24
|
+
"types": "./lib/types/index.d.ts",
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
28
|
+
"directories": {
|
|
29
|
+
"lib": "lib"
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"lib"
|
|
33
|
+
],
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"@wendellhu/redi": "0.13.0",
|
|
36
|
+
"rxjs": ">=7.0.0",
|
|
37
|
+
"@univerjs/core": "0.1.6",
|
|
38
|
+
"@univerjs/sheets": "0.1.6"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"dayjs": "^1.11.10"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@wendellhu/redi": "^0.13.0",
|
|
45
|
+
"rxjs": "^7.8.1",
|
|
46
|
+
"typescript": "^5.4.3",
|
|
47
|
+
"vite": "^5.2.7",
|
|
48
|
+
"vitest": "^1.4.0",
|
|
49
|
+
"@univerjs/core": "0.1.6",
|
|
50
|
+
"@univerjs/shared": "0.1.6",
|
|
51
|
+
"@univerjs/sheets": "0.1.6"
|
|
52
|
+
},
|
|
53
|
+
"scripts": {
|
|
54
|
+
"dev": "vite",
|
|
55
|
+
"test": "vitest run",
|
|
56
|
+
"test:watch": "vitest",
|
|
57
|
+
"coverage": "vitest run --coverage",
|
|
58
|
+
"build": "tsc && vite build"
|
|
59
|
+
}
|
|
60
|
+
}
|